⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 otg.c

📁 使用ISP1362芯片的USB_OTG参考设计源代码比较新的版本
💻 C
📖 第 1 页 / 共 2 页
字号:
/*
**  WASABI-Hot! version 1.2c    (OTG sub-unit)
**
**
**      -- copyright (c) 2001-2004 by Philips Japan, Ltd. -- All rights reserved --
**
**
**      ** This code has been made to check/learn                          ** 
**      **                             the ISP1362/ISP1363 functionalities **
**      ** Release 06-Aug-2004                                             **
**
**      OKANO, Akifumi
**      
**		Application Laboratory, Mobile and Connectivity
**      Semiconductors Div, Philips Japan Ltd.
**      akifumi.okano@philips.com
**      +81-3-3740-4668 
*/


/****************************************************************************/
/*	includes																*/
/****************************************************************************/

#include		<stdio.h>
#include		<stdlib.h>
#include		<dos.h>
#include		<string.h>
#include		<conio.h>
#include		<alloc.h>
#include		<fcntl.h>
#include		<sys\stat.h>
#include		<time.h>


#include		"general.h"
#include		"beep.h"

#include		"_hc_core/dev_ep.h"
#include		"_hc_core/isr.h"
#include		"_hc_core/port_ctl.h"

#include		"_hc_hw/hw_acces.h"

#include		"_dc/dc_flags.h"

#include 		"_otg/HAL4OTG.h"
#include 		"_otg/OTGSvc.h"
#include 		"_otg/otg.h"

#include		"ui.h"


/****************************************************************************/
/*	constants																*/
/****************************************************************************/

//#define			TEST_OTG_INTERRUPT



/****************************************************************************/
/*	global vars																*/
/****************************************************************************/

extern OTG_TCB OtgTcb;

char			gp_id_string[ ID_STRING_LENGTH + 1 ]	= "";
unsigned char	gp_full_enumeration_at_non_BR_A_HOST	= Disabled;
unsigned char	gp_emulate_peripheral_only			= Disabled;

unsigned char	OTG_HW_emulator_service_required	= False;
unsigned char	OTG_state_in_host					= False;
unsigned char	OTG_leave_from_host_state			= False;
unsigned char	OTG_state_in_peripheral				= False;

unsigned char	g_quit_grab_process;


/****************************************************************************/
/*	function prototypes														*/
/****************************************************************************/

void			OTG_hook_SOF( void );
void			OTG_asynchronous_periodic_process( void );
unsigned char	otg_wait_to_get_mastership( void );
void			OTG_enter_to_peripheral_state( void );
void			OTG_exit_from_peripheral_state( void );
void			OTG_status_monitor( void );
void			OTG_message( char *message );


/****************************************************************************/
/*	function definitions													*/
/****************************************************************************/


void OTG_initialize( void )
{
	unsigned short	v_bus_status;
	unsigned long	calibration_timer;
	unsigned long	calibration_timer_count;
	unsigned char	i;

	//
	//	OTG calibrating timer constant.
	//
	
	mprintf( WHITE, CONTINUE, "OTG calibrating timer constant.\r" );

	calibration_timer		= gp_sof_counter;
	
	for ( calibration_timer_count = 0; calibration_timer_count < 10000000; calibration_timer_count++ )
	{
		continue;
	}
	
	calibration_timer	 = gp_sof_counter - calibration_timer;
	calibration_timer	*= 1000;										//	for micro sec
	
	if ( !calibration_timer )
		mprintf( LIGHTRED, ABORT, "HC interrupt is not working.\r\n" );
		
	Hal4Sys_WaitinUS_set_calibrated_constant( calibration_timer_count / calibration_timer );


	//
	//	Wait V-bus drop
	//

	
	if ( !(read_register16( 0x67 ) & 0x0001) )
	{
		mprintf( WHITE, CONTINUE, "OTG discharging bus lines.        \r" );

		for ( i = 0; i < 20; i++ )
		{
			if ( read_register16( 0x67 ) & 0x0004 )
				break;

			wait_ms( 100 );
		}

		mprintf( BLACK, CONTINUE, "OTG discharging bus lines.   \r" );
	}
	
	
	
	OTG_Init();
	
	isr_install_SOF_service_routine( OTG_hook_SOF );		//	periodic (1ms) call of "OTG_HW_emulator()" is initiated by SOF interrupt

//#define	TEST_OTG_INTERRUPT

#ifdef	TEST_OTG_INTERRUPT
	write_register16( REG_OTG_INT_ENABLE, 0x0000 );
	isr_install_OTG_IRQ_service_routine( otg_fn_OTG_isr );
#endif

	gene_install_asynchronous_periodic_process( 5, OTG_asynchronous_periodic_process );

#ifdef	TEST_OTG_INTERRUPT
//	write_register16( REG_OTG_INT_ENABLE, 0x7BD );
//	write_register16( REG_OTG_INT_ENABLE, 0x1 );
	write_register16( REG_OTG_INT_ENABLE, 0x0200 );
#endif

}


void OTG_hook_SOF( void )
{
	OTG_HW_emulator_service_required	= True;
}


void OTG_asynchronous_periodic_process( void )
{
	static unsigned short	is_in_execution		= False;
	
//#define	OTG_INTERRUPT_REGISTER_MONITOR

#ifdef	OTG_INTERRUPT_REGISTER_MONITOR
	static unsigned short	prev_reg	= 0;
	unsigned short			tmp;
	
	if ( prev_reg != (tmp	= (read_register16( 0x67 ) & 0x3F)) )
	{
		mprintf( WHITE, CONTINUE, "                 OTG x reg = 0x%04X\r\n", tmp );
		prev_reg	= tmp;
	}
#endif
	
	if ( is_in_execution )
		return;
	else
		is_in_execution		= True;

	if ( OTG_HW_emulator_service_required )
	{
		OTG_HW_emulator_service_required	= False;
		OTG_HW_emulator();
		
		OTG_status_monitor();
	}

	if ( is_in_execution )
		is_in_execution		= False;
}


void OTG_host_enable_command_from_A_device( unsigned short val )
{
	switch ( val )
	{
		case 3 :
			OtgTcb.b_hnp_en = 1;
			break;
		case 4 :
			OtgTcb.a_hnp_support = 1;
			break;
		case 5 :
			OtgTcb.a_alt_hnp_support = 1;
			break;
	}
}


void OTG_bus_request_Toggle( void )
{
	if( OtgTcb.bus_req == 0 )
		OTG_bus_request_ON();
	else
		OTG_bus_request_OFF();
}


void OTG_bus_drop( void )
{
	unsigned short	i;

	if ( 1 == OtgTcb.id )	//	It is mini-B connector inserted
	{
		mprintf( LIGHTRED, CONTINUE, "\r\nVbus drop command can be used at A-device only\r\n" );
		return;
	}

	mprintf( LIGHTRED, CONTINUE, "\r\nVbus drop.\r\n" );

	OtgTcb.bus_req		= 0;
	OtgTcb.bus_drop		= 1;
}


unsigned char OTG_bus_request_ON( void )
{
#ifdef	PORT1_AS_HOST_ONLY
	mprintf( LIGHTRED, CONTINUE, "\r\n\r\n!!!  Caution  !!!\r\n");		//	##	version1.2c
	mprintf( LIGHTRED, CONTINUE, "  This code has been build with PORT1_AS_HOST_ONLY flag. OTG may not work\r\n");		//	##	version1.2c
#endif

	if( OtgTcb.bus_req == 1 )
		return ( 1 );
	
	beep( 660.00, .1, SUSTAIN, MONO );
	mprintf( GREEN, CONTINUE, "\r\n   OTG Bus request = ON. \r\n");
	
	if ( gp_emulate_peripheral_only )
	{
		mprintf( GREEN, CONTINUE, "   ");
		mprintf( (RED << 4) | WHITE, CONTINUE, " Emulating PERIPHERAL ONLY device.      ");
		mprintf( GREEN, CONTINUE, "\r\n");
		mprintf( GREEN, CONTINUE, "   ");
		mprintf( (RED << 4) | WHITE, CONTINUE, " This (local) device can not be a HOST. ");
		mprintf( GREEN, CONTINUE, "\r\n");
	}
	
	disable_INT();
	
	OtgTcb.bus_req		= 1;
	OtgTcb.b_srp_done	= 0;
	
	enable_INT();

	return ( 0 );
}

unsigned char OTG_bus_request_OFF( void )
{	
	unsigned char 	previous_state;

	if( OtgTcb.bus_req == 0 )
		return ( 0 );
	
	mprintf( RED, CONTINUE, "\r\n   OTG Bus request = OFF. \r\n");
	disable_INT();
	OtgTcb.bus_req		= 0;
	OtgTcb.a_host_done	= 1;

	enable_INT();

	return ( 1 );
}


void OTG_enter_to_host_state( void )
{
	OtgTcb.b_srp_support	= 0;
	OtgTcb.b_hnp_support	= 0;

	OTG_state_in_host		= True;
	
	if ( !portctl_check_OTG_port( OtgTcb.bus_req, gp_full_enumeration_at_non_BR_A_HOST ) )	//	if enumeration failed...
	{
		mprintf( LIGHTRED, CONTINUE, "\r\n   OTG ...Device not responding properly. Bus request cleard.\r\n");
		
		OtgTcb.bus_req		= False;
		OtgTcb.a_host_done	= 1;
		
		return;
	}
	
	
	if ( (0 == OtgTcb.id) && (1 == OtgTcb.b_srp_support) && (0 == OtgTcb.b_hnp_support) && (OtgTcb.bus_req == False) )
	{
		mprintf( (RED << 4) | WHITE,  CONTINUE, "\r\n\r\n" );
		mprintf( (RED << 4) | WHITE,  CONTINUE, "    Remote is the PERIPHERAL ONLY device.\r\n" );
		mprintf( (RED << 4) | WHITE,  CONTINUE, "    To exit from host state, process \"Vbus_drop\" by pressing [/] key.\r\n" );
		mprintf( (  0 << 4) | WHITE,  CONTINUE, "\r\n" );
	}
	
	if(	(0 == OtgTcb.id) && (1 == OtgTcb.b_hnp_support) && (OtgTcb.bus_req == False) )
	{
		mprintf( LIGHTRED,  CONTINUE, "                                          *** " );
		mprintf( WHITE, CONTINUE, "Giving mastership to remote" );

		OTGSup_ADevGiveupMastership();
		OtgTcb.a_host_done	= 1;
	}
}


void OTG_exit_from_host_state( void )
{
	OTG_state_in_host			= False;
	OTG_leave_from_host_state	= True;
}



void OTG_enter_to_peripheral_state( void )
{
	OTG_state_in_peripheral		= True;
}


void OTG_exit_from_peripheral_state( void )
{
	OTG_state_in_peripheral		= False;
}




unsigned char OTG_host_operation_enabled( void )
{
	return ( OTG_state_in_host );
}


unsigned char OTG_peripheral_operation_enabled( void )
{
	return ( OTG_state_in_peripheral );

}








unsigned short		devep_std_request( unsigned char *buff_ptr, device_instance	*dvi_ptr, unsigned short req, unsigned short wValue, unsigned short wIndex, unsigned short *wLength );


unsigned char otg_enable_B_host( void )
{
	device_instance		*dvi_ptr;

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -