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

📄 usbd_sampletask.c

📁 epson usb2.0 控制芯片 S1R72V05 固件程序。
💻 C
📖 第 1 页 / 共 5 页
字号:
				return PLAY_MODE;			// PLAY mode
			}
		}
		// GPIO_B 7pin - Check the change of SW1 status
		GpioInfo.gpioBpin7 = sfr_inl( GPIO_PORTBDATA ) & GPIO_PIN7;
		if ( GpioInfo.gpioBpin7Old != GpioInfo.gpioBpin7 )
		{
			GpioInfo.gpioBpin7Old = GpioInfo.gpioBpin7;
			if ( GpioInfo.gpioBpin7 & GPIO_PIN7 ) {
				return STOP_MODE;			// Stop mode
			}
		}
#endif
	}
}

/*
//=============================================================================
// Function_Name: DEVICECallbackProc
// description	: Callback function
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
void DEVICECallbackProc ( unsigned long message, unsigned long param0, void* vpParam )
{
	DEVICE_TASK_ATAPI_CMD *pAtapiCmd;               // Add for USBCV MSC Test

	switch( message )
	{
		case DEVICE_TASK_MSG_ACTIVE_CMP:			// Completion of initialization
			DBG_FlowStrPrint( "CB ACTIVE_CMP\r\n",3 );
			TskInfo.v05initFlg = DEVICEINIT_OK;
			break;

		case DEVICE_TASK_MSG_USB_VBUS_CHANGED:		// Change state of USB VBUS
			DBG_FlowStrPrint( "CB USB_VBUS_CHANGED\r\n",3 );
			TskInfo.vbusStat = param0;
			break;

		case DEVICE_TASK_MSG_USB_ATTACH:			// Completion of USB Attach
			DBG_FlowStrPrint( "CB USB_ATTACH\r\n",3 );
			TskInfo.attachFlg = USB_ATTACH;
			break;

		case DEVICE_TASK_MSG_USB_DETACH:			// Completion of USB Detach
			DBG_FlowStrPrint( "CB USB_DETACH\r\n",3 );
			TskInfo.attachFlg = USB_DETACH;
			break;

		case DEVICE_TASK_MSG_USB_RCV_SUSPEND:		// Receive USB Suspend
			DBG_FlowStrPrint( "CB RCV_SUSPEND\r\n",3 );

#ifdef USB_MANUAL_MODE
			// Manual control for current consumption(LV2 specified)
			if ( DEVICE_TASK_STATUS_SUCCESS != DEVICE_TASK_SetPowerState( DEVICE_TASK_SUSPEND_LV2 ))
			{
				// Failed to control current consumption
				DBG_FlowStrPrint( "SetPowerState Miss\r\n",3 );
			}
#endif
			break;

		case DEVICE_TASK_MSG_USB_WAKEUP:			// Resume sate of USB Suspend
			DBG_FlowStrPrint( "CB USB_WAKEUP\r\n",3 );
			break;

		case DEVICE_TASK_MSG_USB_RCV_CMD:			// Receive ATAPI command
			DBG_FlowStrPrint( "CB RCV_CMD\r\n",3 );
			// Receive Stage command
			TskInfo.userCmdFlg = TRUE;
			// Add for USBCV MSC Test
			pAtapiCmd = (DEVICE_TASK_ATAPI_CMD *)vpParam;
			// End of USBCV MSC Test
			memcpy( StrgCmdInfo.atapi.command,pAtapiCmd->command, 12);  // Mod for USBCV MSC Test
			// Add for USBCV MSC Test
			StrgCmdInfo.atapi.xferSize = pAtapiCmd->xferSize;
			StrgCmdInfo.atapi.xferDir  = pAtapiCmd->xferDir;
			// End of USBCV MSC Test
			break;
		case DEVICE_TASK_MSG_USB_SEND_STS:			// Completion of the transmission of ATAPI status
			DBG_FlowStrPrint( "CB USB_SEND_STS\r\n",3 );
			StrgCmdInfo.StsCmpFlg = TRUE;
			break;

		case DEVICE_TASK_MSG_IDE_HRST_CMP:			// Completion of reset of IDE hardware
			DBG_FlowStrPrint( "CB IDE_HRST_CMP\r\n",3 );
			break;

		case DEVICE_TASK_MSG_PLAY_READY_DMA_CMP:	// Completion of preparation of DMA transfer
			DBG_FlowStrPrint( "CB PLAY_READY_DMA_CMP\r\n",3 );
			TskInfo.DMAxferFlg = DMA_READY;
			break;

		case DEVICE_TASK_MSG_DATA_XFER_CMP:		// Completion of data transfer
			DBG_FlowStrPrint( "CB DATA_XFER_CMP\r\n",3 );
			if( TskInfo.userCmdFlg == TRUE )
			{
				// Send status
#ifndef TOYA2_C
				sfr_outl( DMA_OPERATION, 0 );	// Disable DMA transfer
#endif
				StrgCmdInfo.XferCmpFlg = TRUE;	// Set to finish transfer
			}
			else
			{
				TskInfo.DMAxferFlg = DMA_XFERCMP;
			}
			break;

		case DEVICE_TASK_MSG_PLAY_IDE_CMD_CMP:		// Completion of IDE command sequence
			DBG_FlowStrPrint( "CB PLAY_IDE_CMD_CMP\r\n",3 );
			TskInfo.IDEcmdCmpFlg = IDE_CMDSEQ_CMP;
			if( param0 != DEVICE_TASK_CMD_STATUS_PASSED )
			{
				TskInfo.IDEcmdCmpFlg = IDE_CMDSEQ_ERR;
			}
			break;

		case DEVICE_TASK_MSG_DMA_ABORTED:			// Abort DMA transfer
			DBG_FlowStrPrint( "CB DMA_ABORTED\r\n",3 );
			break;

		default:
			DBG_FlowStrPrint( "CB err message\r\n",3 );
			break;
	}
}

/*
//=============================================================================
// Function_Name: USBModeProc
// description	: Main process for USB mode
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
void USBModeProc( void )
{
	long result;
	unsigned long usbmode;
	unsigned char gpio;

	// Display menu of USB mode
	DBG_FlowStrPrint("[USB Device USB Mode Start]\r\n", 3);

	TskInfo.mode = USB_MODE;

	for( ;; )
	{
		//------------------------------------------------------
		// Set to let sample task to process Inquiry Command
		//------------------------------------------------------
		DEVICE_TASK_SetUserStrgCmd( LUN0, sizeof(CmdCode), CmdCode );

#ifdef USB_MANUAL_MODE
		//------------------------------------------------------
		// USB Manual mode
		//------------------------------------------------------

		// Wait until VBUS became state of High
		while( TskInfo.vbusStat != DEVICE_TASK_USB_VBUS_HIGH )
		{
			dly_tsk( 100 );
		}
		// Make manual control of Pull-up to ON
		usbmode = DEVICE_TASK_MODE_USB_MANUAL_ATTACH;

		// Make manual control of current consumption to ON
		usbmode |= DEVICE_TASK_MODE_USB_MANUAL_PWR_CTL;

		// Start USB Storage mode
		result =  DEVICE_TASK_StartUSB( usbmode );
		if ( result != DEVICE_TASK_STATUS_SUCCESS )
		{
			// Failed to transit to USB Storage mode
			break;
		}

		// Request of Pull-up ON(Attach)
		result =  DEVICE_TASK_PullupControl ( DEVICE_TASK_USB_PULLUP_ON );
		if ( result != DEVICE_TASK_STATUS_SUCCESS )
		{
			// Failed to transit to USB Storage mode
			break;
		}
		// Wait until Attach completed
		while( TskInfo.attachFlg != USB_ATTACH )
		{
			dly_tsk( 100 );
		}

#else // USB_AUTO_MODE

		//------------------------------------------------------
		// USB Auto mode
		//------------------------------------------------------

		// Automatical control for current consumption(LV2 specified)
		usbmode = DEVICE_TASK_MODE_USB_AUTO_PWR_LV2;

		// Start USB Storage mode
		result =  DEVICE_TASK_StartUSB( usbmode );
		if ( result != DEVICE_TASK_STATUS_SUCCESS )
		{
			// Failed to transit to USB Storage mode
			break;
		}

#endif //USB_MANUAL_MODE

		while(1)
		{
			// Receive command which has been specified by SetUserStrgCmd
			if( TskInfo.userCmdFlg == TRUE )
			{
				UsbUserCmdTest();
			}

			// Check GPIO and leave out if Stop has been received
#ifdef TOYA2_C
			gpio = (rlMX21_PTA_SSR & 0x00008000)>>8 ;
			if ( gpio & 0x80 ) {
				break;			// Stop mode
			}
#else
			gpio = sfr_inl( GPIO_PORTBDATA ) & GPIO_PIN7;
			if ( gpio & GPIO_PIN7 ) {
				break;			// Stop mode
			}
#endif

			dly_tsk( 100 );

		}
		break;
	}

}
/*
//=============================================================================
// Function_Name: PlayModeProc
// description	: Main process for PLAY mode
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
void PlayModeProc( void )
{
	int i;

	// Ono Show menu of Play mode
	DBG_FlowStrPrint("[USB Device Play Mode Start]\r\n", 3);

	TskInfo.mode = PLAY_MODE;

	//------------------------------------------------------
	// Command of TEST UNIT READY
	//------------------------------------------------------
	if ( STATUS_SUCCESS != ATAPI_TestUnitRdy() )
	{
		TskInfo.mode = STOP_MODE;
		return;
	}

	//------------------------------------------------------
	// ATA command WRITE->READ
	//------------------------------------------------------

	// Allocate buffer for data receiving
	get_mpf( MPFID_SMP_RBF, (VP)&pRdBuff );
	// Allocate buffer for data transmission
	get_mpf( MPFID_SMP_WBF, (VP)&pWrBuff );

	// Create the data of transmission for test
	for( i = 0; i < WRBUFF_SIZE; i++ )
	{
		*(pWrBuff+i) = (unsigned char)i;
	}

#ifndef TOYA2_C
	sfr_outl( DMA_OPERATION, 1 );	// Enable DMA transfer
#endif

	for( ;; )
	{
#ifdef CMD_ASYNC_MODE
		// Send ATA Write command
		if( STATUS_SUCCESS != ATA_WriteCmdAsync() )
#else
		// Send ATA Write command
		if( STATUS_SUCCESS != ATA_WriteCmdSync() )
#endif
		{
			// Failed to execute ATA command
			TskInfo.mode = STOP_MODE;
			break;
		}

#ifdef CMD_ASYNC_MODE
		// Send ATA Read command
		if( STATUS_SUCCESS != ATA_ReadCmdAsync() )
#else
		// Send ATA Read command
		if( STATUS_SUCCESS != ATA_ReadCmdSync() )
#endif
		{
			// Failed to execute ATA command
			TskInfo.mode = STOP_MODE;
			break;
		}

		LED_On(1);							/* for TOYA2 : */

		LED_Off(8);							/* for TOYA2 : */
		LED_Off(7);							/* for TOYA2 : */
		LED_Off(6);							/* for TOYA2 : */
		LED_Off(5);							/* for TOYA2 : */
		LED_Off(4);							/* for TOYA2 : */
		LED_Off(3);							/* for TOYA2 : */
		LED_Off(2);							/* for TOYA2 : */
		LED_Off(1);							/* for TOYA2 : */

		// Compare the data which has been written with the data which has been read out
		if( STATUS_SUCCESS != ReadDataCheck() )
		{
			// Failed to execute ATA command
			TskInfo.mode = STOP_MODE;
			break;
		}
		LED_On(8);							/* for TOYA2 : */
		break;
	}

#ifndef TOYA2_C
	sfr_outl( DMA_OPERATION, 0 );	// Disable DMA transfer
#endif

	rel_mpf( MPFID_SMP_RBF, pRdBuff );	// Release buffer of data receiving
	rel_mpf( MPFID_SMP_WBF, pWrBuff );	// Release buffer of data transmission

	//------------------------------------------------------
	// ATAPI command Read
	//------------------------------------------------------

	if( TskInfo.mode == PLAY_MODE )
	{
		// Allocate buffer for data receiving
		get_mpf( MPFID_SMP_INQ, (VP)&pInquiry );

		// Send ATAPI Read command
		if( STATUS_SUCCESS != ATAPI_ReadCommand() )
		{
			// Failed to execute ATAPI command
			TskInfo.mode = STOP_MODE;
		}

		rel_mpf( MPFID_SMP_INQ, pInquiry );	// Release buffer of data receiving
		TskInfo.mode = STOP_MODE;			// Change to Stop mode
	}
}
/*
//=============================================================================
// Function_Name: ATA_WriteCmdAsync
// description	: Send ATA Write command(Asynchronous mode)
// argument		:
// return		:
// flag			:
// global		:
//=============================================================================
*/
#ifdef CMD_ASYNC_MODE

long ATA_WriteCmdAsync( void )
{
	long	result;


	TskInfo.DMAxferFlg   = DMA_NOTREADY;
	TskInfo.IDEcmdCmpFlg = IDE_CMDSEQ_RDY;

#ifdef BIG_DRIVE
	// Create ATA command(BIG DRIVE WRITE [35h])
	memset( &AtaCmd, 0, sizeof( DEVICE_TASK_ATA_CMD ));
	AtaCmd.features		= 0;		// Features
	AtaCmd.secCnt		= 0x02
	AtaCmd.bicSecCnt	= 0x00;		// Bic Drive Sector count
	AtaCmd.bicSecNum	= 0;		// Bic Drive Sector countLBA[7:0]
	AtaCmd.bicCylLow	= 0;		// Bic Drive Sector countLBA[15:8]
	AtaCmd.bicCylHigh	= 0;		// Bic Drive Sector countLBA[23:16]
	AtaCmd.bicDevHead	= 0x41;		// Bic Drive Sector countDevice/Head,LBA[27:24]
	AtaCmd.command		= 0x35;		// Command
#else
	// Create ATA command(WRITE DMA[CAh])
	memset( &AtaCmd, 0, sizeof( DEVICE_TASK_ATA_CMD ));
	AtaCmd.features		= 0;					// Features
	AtaCmd.secCnt		= (unsigned char)(SECTOR_CNT);	// Sector count
	AtaCmd.secNum		= 0x00;							// LBA[7:0]
	AtaCmd.cylLow		= 0x00;							// LBA[15:8]
	AtaCmd.cylHigh		= 0x00;							// LBA[23:16]
	AtaCmd.devHead		= 0x40;							// Device/Head,LBA[27:24]
	AtaCmd.command		= ATA_WRITE_DMA;				// Command
#endif

	// Set information for data transfer
	memset( &XferInfo, 0, sizeof( DEVICE_TASK_XFER_INFO ));
	XferInfo.mode		= DEVICE_TASK_XFER_MODE_DMA;	// DMA transfer
	XferInfo.size		= WRBUFF_SIZE;				// Number of data transfer
	XferInfo.dir		= DEVICE_TASK_XFER_DIR_OUT;	// Direction of data transfer
	XferInfo.pAddress	= NULL;
	XferInfo.dmaChannel = DEVICE_TASK_DMA_CH0;			// DMA channel

	// Execute ATA command
	result = DEVICE_TASK_ExecIdeCmd( LUN0, DEVICE_TASK_IDECMD_TYPE_ATA, &AtaCmd, &XferInfo );
	if ( result != DEVICE_TASK_STATUS_SUCCESS )
	{
		// Failed to execute ATA command
		return STATUS_UNSUCCESSFUL;
	}

	// Wait until preparation of DMA transfer completed
	while( TskInfo.DMAxferFlg != DMA_READY )
	{
		dly_tsk( 100 );
	}

#ifdef TOYA2_C
	// Set register of source address for DMA Ch0
	rlMX21_SAR0 = (unsigned long)pWrBuff;

	// Set register of destination address for DMA Ch0
	rlMX21_DAR0 = WRITEDEVICEADR ;

⌨️ 快捷键说明

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