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

📄 otg_demo.c

📁 使用ISP1362芯片的USB_OTG参考设计源代码比较新的版本
💻 C
📖 第 1 页 / 共 5 页
字号:
		return;
	*(status_bf + status_rx_size)	= 0;
	mprintf( WHITE, CONTINUE, "   file transfer state : %s\r", status_bf );
	
	mprintf( WHITE, CONTINUE, "\r\n" );

	_dos_close( file_handle );
	
	
	/*
	**	File transfer done
	**		Confirm update.
	*/
	
	
	if ( OTG_command_to_device( status_bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_APP_UPDATE, OTG_DEMO_SUB_COMMAND_GET_SIZE_AND_CHECKSUM, 8, &status_rx_size, 1 ) )
	{
		OTG_release_far_end_device();
		return;
	}
	

	mprintf( WHITE, CONTINUE, "File transfer done, total_size=%lu\r\n", total_size );
	mprintf( WHITE, CONTINUE, "    remote (original)  total_size=%lu, checksum=0x%08lX\r\n", *(((unsigned long *)status_bf) + 0), *(((unsigned long *)status_bf) + 1) );
	mprintf( WHITE, CONTINUE, "    local  (copied)    total_size=%lu, checksum=0x%08lX\r\n", total_size, checksum );
	
	if ( total_size != *(((unsigned long *)status_bf) + 0) )
	{
		mprintf( LIGHTRED, CONTINUE, "   Update aborted : file size different.\r\n" );
		OTG_release_far_end_device();

		return;
	}
	
	if ( checksum != *(((unsigned long *)status_bf) + 1) )
	{
		mprintf( LIGHTRED, CONTINUE, "   Update aborted : checksum different.\r\n" );
		OTG_release_far_end_device();

		return;
	}
	
	while ( True )
	{
		mprintf( LIGHTRED, CONTINUE, "Are you sure to update?  [y] or [n]  >> " );
		
		key		= ui_key_wait() & 0xFF;

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

		if ( key == 'y' )
		{
			remove( gp_app_path_name );
			rename( tmp_file_name, gp_app_path_name );
			mprintf( WHITE, CONTINUE, "Update done, total_size=%lu.\r\n", total_size );
			
			mprintf( WHITE, CONTINUE, "\r\n\r\n\r\n\r\n\r\n\r\n\r\n\r\n" );
			mprintf( WHITE, CONTINUE, "          " );
			mprintf( (RED << 4) | WHITE, CONTINUE, "       Restart the application. Press any key to quit.      " );
		
			for ( i = 0; i < 7; i++ )
			{
				wait_ms( 150 );
				mprintf( WHITE, CONTINUE, "\r\n" );
			}
			
			ui_key_wait();
			exit( 0 );
		
			break;
		}
		else if ( key == 'n' )
		{
			mprintf( WHITE, CONTINUE, "\r\nUpdate canceled.\r\n" );
			break;
		}
		else
		{
		}
	}
	
	OTG_release_far_end_device();
}

//    000            000            000            000            000            000            000          
//   0   0          0   0          0   0          0   0          0   0          0   0          0   0         
//   0   0          0   0          0   0          0   0          0   0          0   0          0   0         
//   0   0          0   0          0   0          0   0          0   0          0   0          0   0            
//   0   0          0   0          0   0          0   0          0   0          0   0          0   0         
//    000            000            000            000            000            000            000          

//   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 "Executable update through OTG"
**
**		Peripheral side service
*/


void OTG_demo_peripheral_side_0( char *s, unsigned short *length, unsigned char index )
{
	static unsigned char	bf[ 64 ];				//	To keep previous data for retry send
	static unsigned long	total_size		= 0;
	static unsigned long	source_size;
	static unsigned long	checksum		= 0;
	static int				file_handle		= 0;
	static unsigned long	timer;
	unsigned short			rd_size;
//	unsigned short			tx_size;
	unsigned char			i;
	unsigned char			ci;

	if ( index == 1 )
	{
		sprintf( s, "file transfer %7lu / %7lu  (%6.2f%)", total_size, source_size, ((double)total_size / (double)source_size) * 100.00 );
		*length		= strlen( s );
		
		return;
	}
	
	else if ( (index & ~OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER) == OTG_DEMO_SUB_COMMAND_TRANSFER_RESET )
	{
		if ( file_handle )
			_dos_close( file_handle );
			
		file_handle		= 0;
		total_size		= 0;
		checksum		= 0;
		
		sprintf( s, "file transfer reset" );
		*length		= strlen( s );
		
		return;
	}

	else if ( (index & ~OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER) == OTG_DEMO_SUB_COMMAND_GET_FILE_MODIFIED_TIME )
	{
		*((unsigned long *)(s))		= get_application_file_time();
		*length		= sizeof( unsigned long );

		return;
	}
	
	else if ( (index & ~OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER) == OTG_DEMO_SUB_COMMAND_GET_BUILD_INFO )
	{
		sprintf( s, "v %s / %s, %s", VERSION_NUMBER, __DATE__, __TIME__ );
		*length		= strlen( s );

		return;
	}

	else if ( (index & ~OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER) == OTG_DEMO_SUB_COMMAND_GET_SIZE_AND_CHECKSUM )
	{
		*(((unsigned long *)s) + 0)		= total_size;
		*(((unsigned long *)s) + 1)		= checksum;
		*length		= sizeof( unsigned long ) << 1;

		return;
	}


	else if ( (index & ~OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER) == 0 )
	{
		if ( 0 == file_handle )
		{
			if ( 0 != _dos_open( gp_app_path_name, O_RDONLY, &file_handle ) )
			{
				mprintf( LIGHTGREEN, CONTINUE, "\r\n    error @ opening print source file \"%s\" in OTG_demo_host_side_0()\r\n", gp_app_path_name );
				*length		= 0;
				return;
			}
			else
			{
				total_size		= 0;
				checksum		= 0;
				source_size		= file_size( file_handle );
				mprintf( WHITE,    CONTINUE, "\r\n\r\nApplication program file transfer requested from remote.\r\n" );			
				mprintf( WHITE,    CONTINUE, "    file :  ", gp_app_path_name );			
				mprintf( LIGHTRED, CONTINUE, "%s\r\n", gp_app_path_name );			
				
				timer	= gp_sof_counter;
			}
		}

		if ( index & OTG_DEMO_SUB_COMMAND_RETRY_TRANSFER )
		{
			mprintf( WHITE, CONTINUE, "    file transfer retry requested       \r\n" );
			
			memcpy( s, bf, 64 );			
			*length		= 64;
		}
		else
		{
			_dos_read( file_handle, bf, 64, (unsigned *)length );

			for	( ci = 0; ci < *length; ci++ )
				checksum	+= (unsigned long)*((unsigned char *)bf + ci);

			memcpy( s, bf, 64 );
			
			total_size	+= *length;

			mprintf( WHITE, CONTINUE, "    file transfer %7lu / %7lu.\r", total_size, source_size );

			if ( *length != 64 )
			{
				_dos_close( file_handle );
				file_handle		= 0;
				
				mprintf( WHITE, CONTINUE, "    file transfer %7lu/%7lu.  checksum=0x%08lX\r", total_size, source_size, checksum );

				mprintf( WHITE, CONTINUE, "\r\n    file transfer done.  %lu ms.\r\n\r\n", gp_sof_counter - timer );
			}
		}
	}
}


unsigned long get_application_file_time( void )
{
	unsigned long	t;
	int				file_handle;

	if ( 0 != _dos_open( gp_app_path_name, O_RDONLY, &file_handle ) )
		t	= 0;
	else
		t	= file_modified_time( file_handle );

	mprintf( LIGHTRED, CONTINUE, "\r\n>>> %lX  %lu\r\n", t,t );

	_dos_close( file_handle );
	
	return ( t );
}


unsigned long file_modified_time( int fh )
{
	struct stat		stat_buffer;
	
	fstat( fh, &stat_buffer );

	return ( (unsigned long)(stat_buffer.st_atime) );
}


//     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           

//   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 "Clock synchronize"
 ********* ********** ********** ********** ********** ********** ********** ********** ********** **********/

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


void OTG_demo_host_side_1( void )
{
	char				bf[ 64 ];
	unsigned short		rx_size;
	time_t				local_peer_time				= 0;
	time_t				remote_peer_time;
	unsigned long		local_peer_time_stamp;
	unsigned long		remote_offset_ticks;
	unsigned long		timing_for_set_time;
	unsigned short		time_out_adjust__previous_value;
	unsigned char		i;
	int					key;
	unsigned long		display_change0		= 0xFFFFFFFF;
	unsigned long		display_change1		= 0xFFFFFFFF;
		
	
	if ( OTG_grab_far_end_device() )
		return;
	
	mprintf( LIGHTCYAN,  CONTINUE, "\r\n\r\nOTG feature demo :   " );
	mprintf( WHITE, CONTINUE, "Set local time to remote time.\r\n" );

	mprintf( WHITE,     CONTINUE, "\r\n" );
	mprintf( WHITE,     CONTINUE, "   Clock synchronization test.\r\n" );
	mprintf( LIGHTGRAY, CONTINUE, "   This machine's calendar clock will be synchronized to remote machine\r\n" );
	mprintf( LIGHTGRAY, CONTINUE, "   press " );
	mprintf( LIGHTRED,  CONTINUE, "[y]" );
	mprintf( LIGHTGRAY, CONTINUE, " or " );
	mprintf( LIGHTRED,  CONTINUE, "[c]" );
	mprintf( LIGHTGRAY, CONTINUE, " to continue process.\r\n" );
	mprintf( LIGHTGRAY, CONTINUE, "   press " );
	mprintf( LIGHTRED,  CONTINUE, "[d]" );
	mprintf( LIGHTGRAY, CONTINUE, " to differ the local clock.\r\n" );
	mprintf( LIGHTGRAY, CONTINUE, "   other key to cancel.\r\n\r\n" );



	//
	//	Set remote to generate beep in each 10 seconds
	//
	
	if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_CLOCK_SYNC, 1, 1, &rx_size, 1 ) )
	{
		mprintf( WHITE, CONTINUE, "    failed to get remote response (timeout).\r\n" );
		OTG_release_far_end_device();
		return;
	}
	

	do
	{
		wait_ms( 0 );		//	This is required for "beep()" (to fill the sing buffer)
	
		if ( local_peer_time	!= time( NULL ) )
		{
			local_peer_time	= time( NULL );
			
			display_change0	= gp_sof_counter + 700;
			display_change1	= gp_sof_counter + 850;
			
			strftime( bf, 64, "%d-%b-%Y %H:%M:%S", localtime( &local_peer_time ) );
			mprintf( LIGHTRED, CONTINUE, "   Current clock %s\r", bf );

			switch ( local_peer_time % 10 )
			{
				case 7 :
				case 8 :
				case 9 :
					beep( 880.00, 0.10, SUSTAIN, MONO );
					break;
				case 0 :
					beep( 1760.00, 2.00, DECAY, MONO );
					break;
				default :
					beep( 220.00, 0.10, SUSTAIN, MONO );
					break;
			}
		}
		else
		{
			if ( display_change0 == gp_sof_counter )
				mprintf( RED, CONTINUE, "   Current clock %s\r", bf );		
			else if ( display_change1 == gp_sof_counter )
				mprintf( BLACK, CONTINUE, "   Current clock %s\r", bf );		
		}
	}
	while ( !(key	= ui_key_in() & 0xFF) );
	
	
	//
	//	Clear remote to generate beep in each 10 seconds
	//
	
	if ( OTG_command_to_device( bf, OTG_CONTROL_TR_DIR_IN, OTG_DEMO_CLOCK_SYNC, 2, 1, &rx_size, 1 ) )
	{
		mprintf( WHITE, CONTINUE, "    failed to get remote response (timeout).\r\n" );
		OTG_release_far_end_device();
		return;
	}
	
		
	if ( 'd' == key )
	{
		unsigned long	timing_for_set_time;
		time_t			local_peer_time;

		mprintf( LIGHTRED, CONTINUE, "\r\n   Clock differ.\r\n" );

		local_peer_time	= time( NULL );
		
		while ( local_peer_time == time( NULL ) )
			;

		timing_for_set_time		= gp_sof_counter + 500;
		
		while ( gp_sof_counter < timing_for_set_time )
			;
		
		local_peer_time	-= 10;

⌨️ 快捷键说明

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