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

📄 otg_demo.c

📁 使用ISP1362芯片的USB_OTG参考设计源代码比较新的版本
💻 C
📖 第 1 页 / 共 5 页
字号:

		stime( &local_peer_time );
		
		mprintf( LIGHTRED, CONTINUE, "   Clock differed.\r\n\r\n" );
		OTG_release_far_end_device();
		return;
	}
	else if ( ('y' != key) && ('c' != key) )
	{
		mprintf( LIGHTRED, CONTINUE, "   Clock sync test canceld by user by key press [%c]\r\n\r\n", key & 0xFF );
		OTG_release_far_end_device();
		return;
	}

	mprintf( LIGHTCYAN, CONTINUE, "   Clock sync test started  wait...  " );

	time_out_adjust__previous_value		= read_register16( Com16_HcATLPTDDoneThresholdTimeOut );
	write_register16( Com16_HcATLPTDDoneThresholdTimeOut, 100 );
	local_peer_time	= time( NULL );
		
	mprintf( LIGHTRED, CONTINUE, "* " );

	while ( local_peer_time == time( NULL ) )
		;
		
	local_peer_time_stamp		= gp_sof_counter;
		
	mprintf( YELLOW, CONTINUE, "* " );

	if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_CLOCK_SYNC, 0, sizeof( time_t ) + sizeof( unsigned long ), &rx_size, 1 ) )
	{
		mprintf( WHITE, CONTINUE, "    failed to get remote response (timeout).\r\n" );
		OTG_release_far_end_device();
		return;
	}
	
	if ( rx_size != (sizeof( time_t ) + sizeof( unsigned long )) )
	{
		mprintf( WHITE, CONTINUE, "    failed to get remote response (sz=%u)\r\n", rx_size );
		OTG_release_far_end_device();
		write_register16( Com16_HcATLPTDDoneThresholdTimeOut, time_out_adjust__previous_value );

		return;
	}
	
	write_register16( Com16_HcATLPTDDoneThresholdTimeOut, time_out_adjust__previous_value );

	remote_peer_time		= *((time_t *)bf) + 1;
	remote_offset_ticks		= *((time_t *)(bf + sizeof( time_t )));
	
	timing_for_set_time		= local_peer_time_stamp + remote_offset_ticks + 992;
	
	mprintf( LIGHTCYAN, CONTINUE, "* " );

	while ( gp_sof_counter < timing_for_set_time )
		;
	
	stime( &remote_peer_time );

	mprintf( LIGHTGREEN, CONTINUE, "* " );
	
	mprintf( LIGHTCYAN, CONTINUE, "\r\n   done." );
	mprintf( LIGHTGRAY, CONTINUE, " (synchronized to remote_peer_time=%lu  diff. ticks=%lu)\r\n\r\n", remote_peer_time, remote_offset_ticks );

	OTG_release_far_end_device();
}




//     1              1              1              1              1              1              1           
//    11             11             11             11             11             11             11           
//     1              1              1              1              1              1              1           
//     1              1              1              1              1              1              1           
//     1              1              1              1              1              1              1           
//     1              1              1              1              1              1              1           

//   PPPP  EEEE RRRR  I PPPP  H   H    PPPP  EEEE RRRR  I PPPP  H   H    PPPP  EEEE RRRR  I PPPP  H   H    
//   P   P E    R   R I P   P H   H    P   P E    R   R I P   P H   H    P   P E    R   R I P   P H   H    
//   P   P EEE  R   R I P   P HHHHH    P   P EEE  R   R I P   P HHHHH    P   P EEE  R   R I P   P HHHHH    
//   PPP   E    RRRR  I PPPP  H   H    PPP   E    RRRR  I PPPP  H   H    PPP   E    RRRR  I PPPP  H   H    
//   P     EEEE R   R I P     H   H    P     EEEE R   R I P     H   H    P     EEEE R   R I P     H   H    

/*
**	OTG demo applet "Syncronize local clock to remote"
**
**		Peripheral side service
*/

void OTG_demo_peripheral_side_1( char *s, unsigned short *length, unsigned char index )
{
	time_t				local_peer_time;
	unsigned long		local_peer_time_stamp;
	unsigned long		remote_offset_ticks;
	unsigned long		timing_for_set_time;

	if ( index )
	{
		if ( index == 1 )
			gp_beat_flag		= True;
		else
			gp_beat_flag		= False;
		
		*s			= (char)index;
		*length		= 1;

		Dc_printf( WHITE, CONTINUE, "Local timing beep %s\r\n", gp_beat_flag ? "enabled" : "disabled" );
		return;
	}
			

	local_peer_time_stamp	= isr_get_frame_number();
	local_peer_time			= time( NULL );
	
	mprintf( WHITE, CONTINUE, "  Clock synchronizing from remote peer.\r\n" );

	while ( local_peer_time == time( NULL ) )
		;

	remote_offset_ticks	= isr_get_frame_number() - local_peer_time_stamp;
	mprintf( WHITE, CONTINUE, "  Reference timing generated  %lu.\r\n", remote_offset_ticks );
	
	*((time_t *)s)							= time( NULL );
	*((time_t *)(s + sizeof( time_t )))		= remote_offset_ticks;
	*length									= sizeof( time_t ) + sizeof( unsigned long );

	Dc_printf( WHITE, CONTINUE, "Sending time info 0x%08lX  0x%08lX\r\n", *((time_t *)s), *((time_t *)(s + sizeof( time_t ))) );
}



//    222            222            222            222            222            222            222          
//   2   2          2   2          2   2          2   2          2   2          2   2          2   2         
//      2              2              2              2              2              2              2          
//     2              2              2              2              2              2              2          
//    2              2              2              2              2              2              2          
//   22222          22222          22222          22222          22222          22222          22222         

//   H   H  OOO   SSSS TTTTT    H   H  OOO   SSSS TTTTT    H   H  OOO   SSSS TTTTT    H   H  OOO   SSSS TTTTT   
//   H   H O   O S       T      H   H O   O S       T      H   H O   O S       T      H   H O   O S       T       
//   HHHHH O   O  SSS    T      HHHHH O   O  SSS    T      HHHHH O   O  SSS    T      HHHHH O   O  SSS    T      
//   H   H O   O     S   T      H   H O   O     S   T      H   H O   O     S   T      H   H O   O     S   T      
//   H   H  OOO  SSSS    T      H   H  OOO  SSSS    T      H   H  OOO  SSSS    T      H   H  OOO  SSSS    T      
 

/********* ********** ********** ********** ********** ********** ********** ********** ********** **********
	OTG demo applet "Music play with OTG remote source"
 ********* ********** ********** ********** ********** ********** ********** ********** ********** **********/
  
 /*
**	OTG demo applet "Music play with OTG remote source"
**
**		Host side service
*/

#define		OTG_DEMO_SUB_COMMAND_REMOTE_F_OPEN		0x10
#define		OTG_DEMO_SUB_COMMAND_REMOTE_F_READ		0x20
#define		OTG_DEMO_SUB_COMMAND_REMOTE_F_CLOSE		0x30
#define		OTG_DEMO_SUB_COMMAND_N_OF_FILES			0x40

#define		TRANSFER_SIZE		1792	//	== 0x700 ...((((int)1764 / (int)64) + 1) * 64 )

unsigned char	g_rfp_tmp_buff[ 2048 ];

unsigned char	g_data_tr_done;
void			test_data_tr( endpoint_info_ptr dummy );
unsigned short	file_read_from_remote( short *data_buffer );
void			remote_read_callback( endpoint_info_ptr dummy );
void			play_remote_file( unsigned short (*buff_fill_func)( short *buffer_ptr ) );
void			remote_stop( void );



unsigned char OTG_demo_host_side_2( unsigned char selection )
{
	char				bf[ 64 ];
	unsigned char		*data_buffer;
	unsigned short		rx_size;
	unsigned short		i;
	unsigned char		selection_limit;
	unsigned char		tr_index;
	unsigned long		p_sec;
	int					key;


	/*
	 *	Grab remote peer
	 */
	 
	if ( OTG_grab_far_end_device() )
		return ( 0 );
	
	mprintf( LIGHTCYAN,  CONTINUE, "\r\n\r\nOTG feature demo :   " );
	mprintf( WHITE, CONTINUE, "Play music on local peer, data from remote.\r\n" );

	OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_REMOTE_BEEP, 0, 4, &rx_size, 1 );
	
	beep( 440.00, 1.0, DECAY, POLY );


	/*
	 *	Set remote to bulk transfer enabled interface
	 */
	 
//	tx_size		= 0;
//	devep_std_request( bf, g_far_end.target_device_instance_ptr, std_Dv_request_SET_INTERFACE, 1, 0, &tx_size );


	wait_ms( 100 );

	if ( selection == OTG_AUDIO_GET_N_OF_FILE )
	{	
		OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_REMOTE_AUDIO, OTG_DEMO_SUB_COMMAND_N_OF_FILES, 1, &rx_size, 1 );
		
//		OTG_release_far_end_device();
		return ( *((unsigned char *)bf) );
	}
	
	

	/*
	 *	Get audio file list on remote
	 */
	
	if ( selection != OTG_AUDIO_MANUAL_SELECTION )
	{
		key		= selection;
	}
	else
	{
		mprintf( WHITE,      CONTINUE, "\r\n  remote peer " );
		mprintf( LIGHTGREEN, CONTINUE, "%s", g_far_end.id_name );
		mprintf( WHITE,      CONTINUE, " audio file list.\r\n" );
		 
		
		for ( i = 0; i < MAX_NUM_OF_AUDIO_FILES; i++ )
		{
			if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_CTRL_AUDIO, OTG_DEMO_SUB_COMMAND_LIST_ITEM | i, 64, &rx_size, 1 ) )
			{
				OTG_release_far_end_device();
				return ( 0 );
			}
		
			if ( rx_size == 0 )
				break;
				
			*(bf + rx_size)	= 0;
			mprintf( LIGHTCYAN, CONTINUE, "      [%d]   ", i );
			mprintf( WHITE, CONTINUE, "%s\r\n", bf );
		}

		selection_limit		= i;

		for ( ; i < MAX_NUM_OF_AUDIO_FILES; i++ )
			mprintf( CYAN, CONTINUE, "      [%d]   \r\n", i );

		/*
		 *	Select a file to play on local
		 */
		 
		mprintf( WHITE, CONTINUE, "    Type a number [0-%u] >> ", selection_limit - 1 );


		key		= ui_key_wait() - '0';

		mprintf( WHITE, CONTINUE, "%c\r\n", key + '0' );

		if ( !((key & 0xFF) < selection_limit) )
		{
			mprintf( LIGHTRED, CONTINUE, "\r\n    File selection error. Selected number is invalid.\r\n\r\n" );

			OTG_release_far_end_device();
			return ( 0 );
		}
		 
		key		&= 0xF;
	}

	while ( is_audio_active() )		//	wait for beep process completion
		wait_ms( 50 );

	wait_ms( 200 );


	if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_CTRL_AUDIO, OTG_DEMO_SUB_COMMAND_LIST_ITEM | key, 64, &rx_size, 1 ) )
	{
		OTG_release_far_end_device();
		return ( 0 );
	}
	*(bf + rx_size)	= 0;
	mprintf( WHITE, CONTINUE, "\r\n\r\n    Play start : " );
	mprintf( LIGHTCYAN, CONTINUE, "%s\r\n", bf );



	/*
	 *	Open file and setup remote to send data
	 */
	 
	if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_REMOTE_AUDIO, OTG_DEMO_SUB_COMMAND_REMOTE_F_OPEN | key, 4, &rx_size, 1 ) )
	{
		OTG_release_far_end_device();
		return ( 0 );
	}
	
	
	
	if ( !(*((unsigned long *)bf)) )
	{
		mprintf( LIGHTRED, CONTINUE, "\r\n    Failed to open audio file on remote peer.\r\n\r\n" );
		OTG_release_far_end_device();
		return ( 0 );
	}
	
	p_sec		= bytes_to_sec( *((unsigned long *)bf) );

	mprintf( WHITE,     CONTINUE, "\r\n    Playing music from remote data source...\r\n" );
	mprintf( LIGHTGRAY, CONTINUE, "    total data size = %lu (%4.1lfM) bytes.\r\n", *(unsigned long *)bf, (double)(*(unsigned long *)bf) / (double)(1UL << 20) );
	mprintf( LIGHTGRAY, CONTINUE, "    total play time = %lu secs ( %lu min %lu sec ).\r\n", p_sec, p_sec / 60, p_sec % 60  );
	mprintf( LIGHTGRAY, CONTINUE, "    press " );
	mprintf( LIGHTRED,  CONTINUE, "[a]" );
	mprintf( LIGHTGRAY, CONTINUE, " to abort playing. \r\n\r\n" );

	/*
	 *	Play start
	 */
 
	play_remote_file( file_read_from_remote );
	
	return ( 0 );
}


unsigned char			g_rfp_tr_index;
unsigned short			g_rfp_remaining_tr_size;
unsigned short			g_rfp_transferred_size;
unsigned char			g_rfp_transfer_stop;
transfer_instance		*g_rfp_tip;
unsigned char			*g_rfp_bf_ptr;


unsigned short file_read_from_remote( short	*data_buffer )
{
	unsigned char		dummy_buffer;
	unsigned short		size;
	unsigned long		timeout;

	/*
	 *	Vender specific command : to let remote read file data and set the bulk transfer
	 */

	g_rfp_bf_ptr				= g_rfp_tmp_buff;
	g_rfp_transferred_size		= 0;
	g_rfp_remaining_tr_size		= TRANSFER_SIZE;
	g_rfp_transfer_stop			= False;

	/*	
	 *	Prepare bulk transfer
	 */
	
	g_data_tr_done		= False;


	if ( g_rfp_tr_index == 0xFF )
		if ( (NO_OPEN_ATL_TRANSFER_SLOT == (g_rfp_tr_index	= atlmix_get_open_transfer_index( BULK_TRANSFER ))) )
			mprintf( LIGHTRED, ABORT, "error @ opening bulk_transfer, transfer list full.\r\n" );
	
	/*	
	 *	Start bulk transfer
	 */
		
	g_rfp_tip		= atlmix_set_transfer( BULK_TRANSFER, 0, g_rfp_tr_index, g_rfp_bf_ptr, g_rfp_remaining_tr_size, IN, (g_far_end.target_device_instance_ptr)->epi_ptr[ EpIN ][ 4 ], remote_read_callback );
	
	
	/*
	 *	Wait for bulk transfer done
	 *		This loop blocks the execution of non-ISR process.
	 */
	 
	timeout			= gp_sof_counter + 300;
	
	while ( !g_data_tr_done && (gp_sof_counter < timeout) )
		;

	g_rfp_transfer_stop		= True;

	
	/*
	 *	Post process for bulk transfer
	 */
	
	if ( !g_data_tr_done )	//	This is timeout
		g_rfp_transferred_size	= TRANSFER_SIZE;		//	Set fake read size
		
	/*
	 *	Copy data to buffer

⌨️ 快捷键说明

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