📄 usbd_sampletask.c
字号:
/*
* description: Sample Task
* Maker : Kagaya.Michiru
* Copyright : (C)2003,SEIKO EPSON Corp. All Rights Reserved.
*/
#include <string.h>
#include "kernel_id.h"
#include "kernel.h"
#include "DeviceTaskAPI.h"
#include "SampleTask.h"
#ifdef TOYA2_C
#include "reg_mx21.h" /* Please change according to the environment used */
#else
#include "ap7tdmi.h" /* Please change according to the environment used */
#endif
#include "DebugTask.h"
#ifdef TOYA2_C
#include "led_if.h"
#endif
// ---- API control of USB ATTACH, DETACH and manual power control wheile receiving USB SUSPEND ----
//#define USB_MANUAL_MODE
// ---- PLAY mode test when USB storage command has been received ------------
//#define USB_PLAY_TEST
// ----- Test for DMA transfer ---------------------------------------------------
//#define DMA_XFER_TEST
// ----- Asynchronous of data transfer in Play mode ------------------------------------
#define CMD_ASYNC_MODE
//---------------------------------------
// Constants Declaration
//---------------------------------------
#define STATUS_SUCCESS 0
#define STATUS_UNSUCCESSFUL 1
#define STOP_MODE 0 // Stop mode
#define USB_MODE 1 // USB mode
#define PLAY_MODE 2 // PLAY mode
#define DEVICEINIT_OK 1 // Completion of DEVICE initialization
#define USB_DETACH 1 // USB Detach
#define USB_ATTACH 2 // USB Attach
#define LUN0 0 // LUN number 0
#define LUN1 1 // LUN number 1
#ifdef TOYA2_C
#define WRITEDEVICEADR 0xCC00007E // Start address of DEVICE
#define READDEVICEADR 0xCC00007C // Start address of DEVICE
#else
#define DEVICEADR 0x01000000 // Start address of DEVICE
#endif
///////////////////////////////////////////////
#ifdef USB_PLAY_TEST
//
// SECTOR_CNT must be set to 64, in order to make sure that receiving whatever command is also OK,
// in Play mode test in Usb Strg
// Number of sector
#define SECTOR_CNT 256
#else
#define SECTOR_CNT 1 // Number of sector
// Number of sector
// Number of sector
// Number of sector
// Number of sector
// Number of sector
// Number of sector
#endif
#define SECTOR_SIZE 512 // Size of 1 sector
#define WRBUFF_SIZE SECTOR_SIZE * SECTOR_CNT
#define RDBUFF_SIZE SECTOR_SIZE * SECTOR_CNT
#define INQUIRY_SIZE 36 // Size of INQUIRY data
// User Cmd Data
#ifdef USB_PLAY_TEST
#define MAX_USER_CMD 3 // Specify the CPU execution command
#else
#define MAX_USER_CMD 1 // Specify the CPU execution command
#endif
//---------------------------------------
// Structure Declaration
//---------------------------------------
typedef struct _TASK_INFO {
unsigned char mode; // Operation mode
unsigned char v05initFlg; // Flag for DEVICE_TASK initialization
unsigned char vbusStat; // State of USB VBUS
unsigned char attachFlg; // Flag for completion of Attach
unsigned char DMAxferFlg; // Flag for DMA transfer
unsigned char IDEcmdCmpFlg; // Flag for completion of IDE command sequence
unsigned char userCmdFlg; // Flag for User Cmd access
} TASK_INFO, *PTASK_INFO;
typedef struct _GPIO_INFO{
unsigned char gpioApin7Old; // Former state of GPIO_A 7pin
unsigned char gpioApin7; // State of GPIO_A 7pin
unsigned char gpioBpin6Old; // Former state of GPIO_B 6pin
unsigned char gpioBpin6; // State of GPIO_B 6pin
unsigned char gpioBpin7Old; // Former state of GPIO_B 7pin
unsigned char gpioBpin7; // State of GPIO_B 7pin
} GPIO_INFO, *PGPIO_INFO;
typedef struct _STRG_CMD_INFO {
DEVICE_TASK_ATAPI_CMD atapi; // Command of ATAPI receiving
unsigned long size; // Number of data transfer
unsigned char Status; // Status of command
unsigned char XferCmpFlg; // Request of data transfer
unsigned char StsCmpFlg; // Request of status transfer
} STRG_CMD_INFO, *PSTRG_CMD_INFO;
//---------------------------------------
// Variables Declaration
//---------------------------------------
static DEVICE_TASK_ATA_CMD AtaCmd; // Information of specifying ATA command
static DEVICE_TASK_ATAPI_CMD AtapiCmd; // Information of specifying ATAPI command
static DEVICE_TASK_XFER_INFO XferInfo; // Information of data transfer
static STRG_CMD_INFO StrgCmdInfo;// Command of Storage receiving
static unsigned char *pWrBuff;
static unsigned char *pRdBuff;
static unsigned char *pInquiry;
static TASK_INFO TskInfo; // Inner information of task
static GPIO_INFO GpioInfo; // Information of GPIO input pin
static unsigned char CmdCode[MAX_USER_CMD]
#ifdef USB_PLAY_TEST
= {ATAPI_INQUIRY, ATAPI_READ10, ATAPI_WRITE10 };
#else
= {ATAPI_INQUIRY};
#endif
static unsigned char InquiryData[INQUIRY_SIZE] =
{
0x00, 0x00, 0x02, 0x02, 0x1F, 0x00, 0x00, 0x10, 'S', '1', 'R', '7', '2', 'V', '0', '5',
'S', 'a', 'm', 'p', 'l', 'e', ' ', 'F', '/', 'W', 0x20, 0x20, 0x20, 0x20, 0x20, 0x20,
0x20, 0x20, 0x20, 0x20,
};
//----------------------------------------
// Function Prototype Declaration
//----------------------------------------
static void SampleTaskInit( void );
static void GPIODefInt( void );
static long GPIOCheack( void );
static void DEVICECallbackProc ( unsigned long message, unsigned long param0, void* vpParam );
static void USBModeProc( void );
#ifdef CMD_ASYNC_MODE
static void PlayModeProc( void );
#endif
#ifdef CMD_ASYNC_MODE
static long ATA_WriteCmdAsync( void );
static long ATA_ReadCmdAsync( void );
#else
static long ATA_WriteCmdSync( void );
static long ATA_ReadCmdSync( void );
#endif
static long ReadDataCheck( void );
static long ATAPI_ReadCommand( void );
static long ATAPI_TestUnitRdy( void );
#ifdef USB_PLAY_TEST
static long ATAPI_Read10 ( unsigned short secSize, unsigned long LBA, unsigned char *pData );
static long ATAPI_Write10 ( unsigned short secSize, unsigned long LBA, unsigned char *pData );
#endif
#ifndef CMD_ASYNC_MODE
static void PlayModeTestATA( void );
static void PlayModeTestATAPI( void );
#endif
static void UsbUserCmdTest ( void );
//----------------------------------------
// Declaration of External Function Reference
//----------------------------------------
extern void flush_Dcache(unsigned long, unsigned long); /* D-Cache Invalidate for MVA entry */
extern void clean_Dcache(unsigned long, unsigned long); /* D-Cache clean for MVA entry */
/*
//=============================================================================
// Function_Name: SampleTask
// description : Sample task for customer
// argument :
// return :
// flag :
// global :
//=============================================================================
*/
void USBD_SampleTask( void )
{
// Initialize task
SampleTaskInit();
// create system
DEVICE_TASK_Create();
// Start the Debug task.
OS_ActTsk( TSKID_DBG );
//------------------------------------------------------
// Request of starting DEVICE module
//------------------------------------------------------
#ifdef CMD_ASYNC_MODE
// Use PLAY command operation in asynchronous
DEVICE_TASK_Active( DEVICECallbackProc, DEVICE_TASK_PLAY_CMD_ASYNC_MODE );
#else
// CMD_SYNC_MODE
DEVICE_TASK_Active( DEVICECallbackProc, DEVICE_TASK_PLAY_CMD_SYNC_MODE );
#endif
//-------------------------------------------
// Monitor the GPIO and switch the operation between USB/PLAY
//-------------------------------------------
// Initialize GPIO port
GPIODefInt();
while(1)
{
switch( GPIOCheack() )
{
case USB_MODE:
if( TskInfo.mode == PLAY_MODE )
{
// PLAY MODE finished
}
if( TskInfo.mode != USB_MODE )
{
// If the initialization of DEVICE has not completed, wait until it completed
while( TskInfo.v05initFlg != DEVICEINIT_OK )
{
dly_tsk( 50 );
}
USBModeProc(); // Main process for USB mode
}
break;
case PLAY_MODE:
if( TskInfo.mode == USB_MODE )
{
if ( DEVICE_TASK_STATUS_SUCCESS != DEVICE_TASK_StopUSB() )
{
// Failed to stop USB Storage mode
break;
}
}
if( TskInfo.mode != PLAY_MODE )
{
// If the initialization of DEVICE has not completed, wait until it completed
while( TskInfo.v05initFlg != DEVICEINIT_OK )
{
dly_tsk( 50 );
}
// Confirm operation of PLAY mode
#ifdef CMD_ASYNC_MODE
PlayModeProc(); // Main process for PLAY mode
#else
PlayModeTestATA();
PlayModeTestATAPI(); // Test for PLAY mode
#endif
}
break;
case STOP_MODE:
if( TskInfo.mode == USB_MODE )
{
if ( DEVICE_TASK_STATUS_SUCCESS == DEVICE_TASK_StopUSB() )
{
extern BOOL FlowToggle;
TskInfo.mode = STOP_MODE;
OS_DlyTsk(100); // Waiting USB Stop
DEVICE_TASK_Inactive();
DEVICE_TASK_Delete();
// Terminate Debug task
OS_TerTsk( TSKID_DBG );
if( FlowToggle != DBG_FLOW_DEFAULT ){
FlowToggle = DBG_FLOW_DEFAULT;
}
/* Set to finish Host mode in Sample Task */
OS_SetFlg( FLGID_SAMPLE, FLG_EVENT_HOSDEVSEL_SAMPLE );
return;
}
}
break;
default:
break;
}
}
}
/*
//=============================================================================
// Function_Name: SampleTaskInit
// description : Initialize Task
// argument :
// return :
// flag :
// global :
//=============================================================================
*/
void SampleTaskInit( void )
{
//------------------------------------------------
// Initialize the internal variables
//------------------------------------------------
memset( &TskInfo, 0, sizeof( TASK_INFO ));
memset( &GpioInfo, 0, sizeof( GPIO_INFO ));
}
/*
//=============================================================================
// Function_Name: GPIODefInt
// description : Initialize GPIO port
// argument :
// return :
// flag :
// global :
//=============================================================================
*/
void GPIODefInt( void )
{
#ifdef TOYA2_C
// Set GPIO for Switch 0 - 3: PTA12 - 15
rlMX21_PTA_DDIR &= ~0x0000F000; /* PTA12-15 is Input Port */
rlMX21_PTA_ICONFA1 &= ~0xFF000000;
rlMX21_PTA_ICONFB1 &= ~0xFF000000;
rlMX21_PTA_GUIS |= 0x0000F000; /* PTA12-15 is Use in GPIO Input */
#else
unsigned char gpiotmp;
//------------------------------------------------
// GPIO_A 7pin - SW3
//------------------------------------------------
// Select direction of I/O(Input)
gpiotmp = sfr_inl( GPIO_PORTADIR );
gpiotmp &= ~( GPIO_PIN7 );
sfr_outl( GPIO_PORTADIR, gpiotmp );
//------------------------------------------------
// GPIO_B 6pin - SW2, SW1
//------------------------------------------------
// Select direction of I/O(Input)
gpiotmp = sfr_inl( GPIO_PORTBDIR );
gpiotmp &= ~( GPIO_PIN6 | GPIO_PIN7 );
sfr_outl( GPIO_PORTBDIR, gpiotmp );
#endif
}
/*
//=============================================================================
// Function_Name: GPIOCheack
// description : Monitor status of GPIO input
// argument :
// return :
// flag :
// global :
//=============================================================================
*/
long GPIOCheack( void )
{
#ifdef TOYA2_C
// fot TOYA2
unsigned char sw[4] = {0,0,0,0} ;
#endif
//--------------------------------------------
// Confirm status of GPIO SW3:USB mode is high priority
//--------------------------------------------
for( ;; )
{
dly_tsk( 500 );
#ifdef TOYA2_C
sw[0] = (rlMX21_PTA_SSR & 0x00001000)>>8 ;
sw[1] = (rlMX21_PTA_SSR & 0x00002000)>>8 ;
sw[2] = (rlMX21_PTA_SSR & 0x00004000)>>8 ;
sw[3] = (rlMX21_PTA_SSR & 0x00008000)>>8 ;
GpioInfo.gpioApin7 = sw[1] ;
if ( GpioInfo.gpioApin7Old != GpioInfo.gpioApin7 )
{
GpioInfo.gpioApin7Old = GpioInfo.gpioApin7;
if ( GpioInfo.gpioApin7 & 0x20 ) {
return USB_MODE; // USB mode
}
}
GpioInfo.gpioBpin6 = sw[2] ;
if ( GpioInfo.gpioBpin6Old != GpioInfo.gpioBpin6 )
{
GpioInfo.gpioBpin6Old = GpioInfo.gpioBpin6;
if ( GpioInfo.gpioBpin6 & 0x40 ) {
return PLAY_MODE; // PLAY mode
}
}
GpioInfo.gpioBpin7 = sw[3];
if ( GpioInfo.gpioBpin7Old != GpioInfo.gpioBpin7 )
{
GpioInfo.gpioBpin7Old = GpioInfo.gpioBpin7;
if ( GpioInfo.gpioBpin7 & 0x80 ) {
return STOP_MODE; // Stop mode
}
}
#else
// GPIO_A 7pin - Check the change of SW3 status
GpioInfo.gpioApin7 = sfr_inl( GPIO_PORTADATA ) & GPIO_PIN7;
if ( GpioInfo.gpioApin7Old != GpioInfo.gpioApin7 )
{
GpioInfo.gpioApin7Old = GpioInfo.gpioApin7;
if ( GpioInfo.gpioApin7 & GPIO_PIN7 ) {
return USB_MODE; // USB mode
}
}
// GPIO_B 6pin - Check the change of SW2 status
GpioInfo.gpioBpin6 = sfr_inl( GPIO_PORTBDATA ) & GPIO_PIN6;
if ( GpioInfo.gpioBpin6Old != GpioInfo.gpioBpin6 )
{
GpioInfo.gpioBpin6Old = GpioInfo.gpioBpin6;
if ( GpioInfo.gpioBpin6 & GPIO_PIN6 ) {
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -