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

📄 devicetask.c

📁 epson usb2.0 控制芯片 S1R72V05 固件程序。
💻 C
📖 第 1 页 / 共 5 页
字号:
/*
 * description: DEVICETask
 * Maker	  : Michiru.Kagaya
 * Copyright  : (C)2003,SEIKO EPSON Corp. All Rights Reserved.
 */

//=============================================================================
// Include files
//=============================================================================
#include <string.h>
#include "SPRDEF.h"
#include "SPRSTS.h"
#include "OSCall.h"

#include "DeviceTask.h"
#include "DeviceTaskAPI.h"
#include "IDETask.h"
#include "USBDTask.h"
//-------- Specify descriptor ------------------------------------------------
// The Descriptor used by USB device, is defined in usb_descdata.h
// Please match the content of the header file to the USB device, rewrite, then use it
// (For Vendor ID and Product ID is an ID for this sample firmware, it is necessary to rewrite)
// BMaxPower of Configuration Descriptor in this sample firmware is described with 0x31(98mA).
// Please define bMaxPower based on the maximum value of the current consumption of VBUS for the USB device used.
// (Please note that there is a possibility that the current consumption value of VBUS doesn't completely become 0mA,
// even when the current from VBUS is not used. )

#include "usb_descdata.h"

#include <Handler.h>

#include "fifo_if.h"
#include "CPUDMA_IF.h"
#include "PM_IF.h"

#include "Reg72V05.h"
//#ifdef DEBUG_C
	#include "DebugTask.h"
//#endif


#include "reg_mx21.h"

//=============================================================================
// Constants Declaration
//=============================================================================

//-------- DEVICEInfo Force Event State ------------------------------------------
#define FORCE_NONE						(0x00)			// Do nothing
#define FORCE_TASK_INIT_WAIT			(0x01)			// Waiting for initialization of lower task

//-------- USB BusState -------------------------------------------------------
#define BUS_ACTIVE						(0x00)			// Connection status.
#define BUS_SUSPEND						(0x01)			// Suspend status

//-------- Setting value for stabilization time of oscillation-------------------
// In the case that the power saving setting in this IC is sleep,
// set the stabilization time of oscillations when returning from sleep(WakeupTim_H/L).
// Because the optimal value changes by combining the resonator and the oscillation cell,
// please set it after evaluateing it.

#define WAKEUP_TIME						(0x2500)

// -------- State of Storage command -----------------------------------------
#define STRG_STATE_IDE_WAIT_RESET		(0x01)			// Waiting for the completion of the IDE reset
#define STRG_STATE_USB_WAIT_CMD_EXEC	(0x02)			// Waiting for receiving IDE command.
#define STRG_STATE_IDE_WAIT_CMD_END		(0x0E)			// Waiting for the competion of the IDE command.
#define STRG_STATE_WAIT_CMD_END			(0x0F)			// Waiting for the competion of USB command.

//-------- STREAGE XFEER TYPE -------------------------------------------------
#define STRG_XFER_MODE_SWPIO			(IDE_XFER_SPIO)	// Software PIO's transfer
#define STRG_XFER_MODE_HWPIO			(IDE_XFER_HPIO) // Hardware PIO's transfer
#define STRG_XFER_MODE_DMA				(IDE_XFER_DMA)	// DMA transfer

//-------- STRAGE XFER DIRECTION ----------------------------------------------
#define STRG_XFER_DIRIN					(USBD_DIRIN)	// Transfer direction: IN
#define STRG_XFER_DIROUT				(USBD_DIROUT)	// Transfer direction: OUT

//-------- DMA FIFO Setting ----------------------------------------------------
#define DMA_READ_FIFO					(FIFO_IF_NUM_FIFO_A)	// READ FIFO
#define DMA_WRITE_FIFO					(FIFO_IF_NUM_FIFO_A)	// WRITE FIFO
#define PLAY_READ_FIFO					(FIFO_IF_NUM_FIFO_MEDIA)	// READ FIFO
#define PLAY_WRITE_FIFO					(FIFO_IF_NUM_FIFO_MEDIA)	// WRITE FIFO

//-------- StrgCmdExecCheck ---------------------------------------------------
// Process for other module
#define EXEC_NONE						(0)				// Unprocessed
#define EXEC_IDE						(1)				// Process for IDE
#define EXEC_USER						(2)				// Process for other module

//-------- StartDMA join ------------------------------------------------------
#define IDE_ONLY						(0x00)			// IDE <-> FIFO DMA
#define IDE_TO_CPU						(0x01)			// IDE <-> FIFO <-> CPU DMA
#define IDE_ONLY_MEDIA					(0x02)			// IDE <-> Media FIFO DMA
#define IDE_TO_MEDIA					(0x03)			// IDE <-> Media FIFO DMA <-> CPU DMA

//-------- ATAPI COMMAND ------------------------------------------------------
#define CMD_TEST_UNIT_READY				(0x00)			// TEST UNIT READY
#define CMD_REQUEST_SENSE				(0x03)			// REQUEST SENSE
#define CMD_READ6						(0x08)			// READ(6)
#define CMD_WRITE6						(0x0A)			// WRITE(6)
#define CMD_MSELECT6					(0x15)			// MODE SELECT(6)
#define CMD_MSENSE6						(0x1A)			// MODE SENSE(6)
#define CMD_READ_FMT_CAPACITY			(0x23)			// READ FORMAT CAPACITY
#define CMD_READ_CAPACITY				(0x25)			// READ CAPACITY
#define CMD_READ10						(0x28)			// READ(10)
#define CMD_WRITE10						(0x2A)			// WRITE(10)
#define CMD_SEEK10						(0x2B)			// SEEK(10)
#define CMD_READ12						(0xA8)			// READ(12)
#define CMD_WRITE12						(0xAA)			// WRITE(12)
#define CMD_MSELECT10					(0x55)			// MODE SELECT(6)
#define CMD_MSENSE10					(0x5A)			// MODE SENSE(6)

// -------- Size of Bulk transfer --------------------------------------------
#define FS_BULK_FIFO_SIZE				(0x40)			// FS Bulk Endpoint Max packet size
#define HS_BULK_FIFO_SIZE				(0x200)			// HS Bulk Endpoint Max packet size

//-------- Logical unit number	-----------------------------------------------
#define IDE_DEV_NONE					(0xFF)			// None connected IDE device

//=============================================================================
// MACRO Decalration
//=============================================================================
#define	DIRECT_B(a)						*(unsigned char *)&(a)			// Direct access for structure

//=============================================================================
// Structure Declaration
//=============================================================================

// -------- Structure for force event process --------------------------------
typedef struct _FORCE_EVENT_INFO {
	UCHAR	reserved		:6;							// Reserved
	UCHAR	bUsbInit		:1;							// Flag for completion of initialization of usb device task
	UCHAR	bIdeInit		:1;							// Flag for completion of initialization of IDE task
} FORCE_EVENT_INFO, *PFORCE_EVENT_INFO;

// -------- Information for connected device ---------------------------------
typedef struct _IDE_TASK_INFO {
	UCHAR	deviceCnt;									// The number of connected device
	UCHAR	maxLun;										// The maximum number of LUN
	UCHAR	devType[2];									// Device type
	BOOL	bHddRstCmp;									// Completion of HDD initialization
	BOOL	bSuspend;									// Set to force waiting state.
} IDE_TASK_INFO, *PIDE_TASK_INFO;

// -------- USB information --------------------------------------------------
typedef struct _USB_TASK_INFO {
	UCHAR	bSetDesc;									// Completion of discriptor setting
	UCHAR	bAttached;									// USB Attach
	UCHAR	bManEnbAttach;								// Enable/disable of Attach(Only effective in manual mode)
	UCHAR	busState;									// State of USB bus(suspend / reseet / resume / active )
	UCHAR	bEnbStrg;									// Valid/invalid of Storage
	UCHAR	bSetMaxLun;									// Completion of MaxLun setting
	UCHAR	speed;										// USB operation mode
} USB_TASK_INFO, *PUSB_TASK_INFO;

// -------- Information of DEVICE Task --------------------------------------------
typedef struct _DEVICE_TASK_INFO {
	IDE_TASK_INFO		ideTaskInfo;					// The information of connected IDE device
	USB_TASK_INFO		usbTaskInfo;					// USB information
	FORCE_EVENT_INFO	forceEvntInfo;					// Force event
} DEVICE_TASK_INFO, *PDEVICE_TASK_STATE;

// -------- Information of data transfer ------------------------------------
typedef struct _USB_XFER_INFO {
	ULONG				reqSize;						// The size that request to transfer
	ULONG				totalXferSize;					// The size that actually transfered
	ULONG				size;							// Size of transfer
	OS_ID				useMpfId;						// MPFID that already got the Buffer
	UCHAR*				buffer;							// Buffer to acquire in PIO transfer
	UCHAR				mode;							// Transfer mode
	UCHAR				dir;							// Transfer direction
} USB_XFER_INFO, *PUSB_XFER_INFO;

// -------- Storage information ------------------------------------------------
typedef struct _STRG_INFO {
	UCHAR				target;							// Target of Command process
	UCHAR				cmdCode[12];					// The command code received
	BOOL				bScsiToAtapi;					// SCSI -> ATAPI command conversion
// The sense data to reply in automatic status reply mode
	USB_XFER_INFO		xferInfo;						// Information of transfer
	UCHAR				Lun;							// the LUN of transfer target
	UCHAR				cmdStatus;						// Used for saving status of IDE command
} STRG_INFO, *PSTRG_INFO;

//=============================================================================
// Variables Declaration
//=============================================================================
static DEVICE_TASK_INFO TaskInfo;							// Task information
STRG_INFO StrgInfo;										// Storage command information
USER_CMD_INFO UserCmdInfo;								// User command information

// The FIFO interrupt factor
static UCHAR CpuDmaIntEnb;								// The CPUDMA interrupt factor
extern DEVICE_MODULE_INFO DEVICEInfo;

//=============================================================================
// Static functions
//=============================================================================

//---------------------------------------
// Main process function
//---------------------------------------
static void TaskInit( void );							// DEVICE Task initialization
static void ForceCancelEvent( void );					// Force cancellation of event
static void SendMessage ( OS_ID mbxId, USHORT msgCode, USHORT size, const VOID* buffer );		// Process for mailbox transmission

static void DEVICE_IntEvent( void );						// Interrupt judgment
static void DEVICE_EventState_IntProc ( void );			// Process for interrupt
static void MsgEvent( DEVICE_MSG* pDEVICEMsg );				// Process for reception of message
static void DEVICEMailDataCancel( void );					// process for deleting mail data
static void ErrMessageProc ( DEVICE_MSG* pMsg );			// Process for error message
static OS_FLGPTN WaitMessage ( USHORT size, USHORT* msgAry, DEVICE_MSG** pMsg );	// Process for waiting of reception of message

static void ResetLowRankTsk( void );					// Process for initialization of the lower task
static void SendCallback ( ULONG msg, ULONG param0, void* pParam1 );	// Callback notification to other module
static void Panic ( void );								// Error Function

// Message handler of each status
static void StateNonExistentMsgProc ( DEVICE_MSG* pMsg );	// Unregistered status
static void StateStopMsgProc ( DEVICE_MSG* pMsg );			// Message process of Stop status
static void StateModuleInitMsgProc ( DEVICE_MSG* pMsg );	// Module's initial status
static void StateDevInitMsgProc ( DEVICE_MSG* pMsg );		// Message process of device initial status
static void StateWaitEventMsgProc ( DEVICE_MSG* pMsg );	// Message process of event waiting status
static void StateSleepAMsgProc ( DEVICE_MSG* pMsg );		// Message process of SLEEP_A status
static void StatePlayCmdExecMsgProc ( DEVICE_MSG* pMsg );	// Message process of command executing status
static void StatePlayDmaXferMsgProc ( DEVICE_MSG* pMsg );	// Message process of DMA transfer status
static void StateUsbNonConnectMsgProc ( DEVICE_MSG* pMsg );// Message process of USB unconnected status
static void StateUsbStorageMsgProc( DEVICE_MSG* pMsg );	// Message process of USB storage status
static void StateUsbSleepBMsgProc ( DEVICE_MSG* pMsg );	// Message process of USB suspend status
static void StateModuleAccMsgProc ( DEVICE_MSG* pMsg );	// Message process of other module access state



//---------------------------------------
// USB storage's process functions
//---------------------------------------
static void UsbStrgProc( void );						// Main process of USB
static BOOL UsbWaitVBusConnect ( void );				// Change notification of VBUS state
static BOOL UsbWaitAttach ( void );						// Notification of USB attach/detach
static BOOL UsbWaitEnableStrgClass ( void );			// Notification of Storage class's valid/invalid
static BOOL UsbSetMaxLun ( void );						// Process of MAX LUN setting
static BOOL UsbStrgCmdProc ( void );					// Process of storage command
static BOOL UsbStrgStop ( void );						// Process of USB device's termination
static BOOL SCSItoATAPICmd ( UCHAR* pResult, const UCHAR* pSource );		// Process of SCSI -> ATAPI packet conversion
static BOOL SCSItoATAPIData ( const UCHAR* pCmdCode, ULONG* pDataSize, UCHAR* pDataBuf );	// Process of data conversion after SCSI -> ATAPI command conversion
static void MsgIdeStrgSRSTCmp ( DEVICE_MSG* pMsg );			// Notification of software reset's completion
static void MsgIdeXferCmp ( DEVICE_MSG* pMsg );			// Process of IDE data transfer's completion
static void MsgIdeCmdCmp ( DEVICE_MSG* pMsg );				// Notification of completion of IDE command process

// Process of Mbx reception from USB Task
static void MsgUsbVBusChanged ( DEVICE_MSG* pMsg );
static void MsgUsbReset ( DEVICE_MSG* pMsg );
static void MsgUsbWakeup ( DEVICE_MSG* pMsg );
static void MsgUsbResume ( DEVICE_MSG* pMsg );
static void MsgUsbSuspend ( DEVICE_MSG* pMsg );
static void MsgUsbStrgCmd ( DEVICE_MSG* pMsg );
static void MsgIdeXferReady ( DEVICE_MSG* pMsg );
static void MsgUsbXferReady ( DEVICE_MSG* pMsg );
static void MsgUsbXferCmp( DEVICE_MSG* pMsg );
//static void MsgRcvUsbStrgReset( DEVICE_MSG* pMsg );
Inline void MsgUsbStrgReset( DEVICE_MSG* pMsg );
static void MsgUsbStrgCmdCmp ( DEVICE_MSG* pMsg );

//---------------------------------------
// Process functions of Play Mode
//---------------------------------------
static void PlayModeInit ( void );						// The initialization process before executing play operation
static void PlayExecProc( void );						// Start the play operation
static BOOL IdeWaitHRSTCmp ( void );					// Waiting for the completion of IDE connected device's reset
static BOOL PlaySendCmd ( void );						//E The command is transmitted to the IDE task
static BOOL PlayCmdResultWait( void );					// Waiting for the completion of executing Play command
static void MsgPlayIdeXferReady( DEVICE_MSG* pMsg );		// Process for the completion of preparation of IDE data transfer
static void MsgPlayIdeXferCmp( DEVICE_MSG* pMsg );			// Process for the completion of IDE data transfer
static void MsgPlayIdeCmdCmp ( DEVICE_MSG* pMsg );			// Process for the completion of IDE command process
static void MsgIdeTskInit ( void );						// Notification for the completion of IDE task initialization
static void MsgIdeHRSTCmp ( DEVICE_MSG* pMsg );			// Notification for the completion of hardware reset
static void StartDMA( UCHAR ch, UCHAR dir, ULONG size, UCHAR  fifoPath );	// Proces the DMX start
static void StopDMA( UCHAR ch );

//---------------------------------------
// Common functions of Play USB
//---------------------------------------
static UCHAR StrgCmdExecCheck( UCHAR LUN, UCHAR cmdCode );		// Check the execution target of storage command
static void StrgCmdExecIde ( PARAM_MSG_NTFY_USBD_STRG_CMD* pUsbStrgCmd );	// Request of command process for IDE task
static void StrgCmdExecUser ( PARAM_MSG_NTFY_USBD_STRG_CMD* pUsbStrgCmd );	// Request of command process for User task
static void IdeResetRequest ( void );							// IDE Initialization process (Hardware reset process)

//---------------------------------------
// Event callback functions
//---------------------------------------
static LONG DMA0CmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// CallBack: DMA0 data transfer completion
static LONG DMA1CmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// CallBack: DMA0 data transfer completion
static LONG FIFOIdeCmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// CallBack: IDE data transfer completion(Transmission from FIFO completed )
static LONG FIFO1CmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// CallBack: FIFO1 data transfer completion(Transmission from FIFO completed )
static LONG FIFO0CmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// CallBack: FIFO0 data transfer completion(Transmission from FIFO completed )
static LONG FIFOEmptyEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// Call Back : FIFO Empty
static LONG FIFOFullEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// Call Back : FIFO Full

static LONG MediaIdeCmpEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );	// CallBack: IDE data transfer completion(Transmission from FIFO completed )
static LONG MediaEmptyEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// Call Back : FIFO Empty
static LONG MediaFullEvent ( ULONG lParam0, ULONG lParam1, VOID* pParam );		// Call Back : FIFO Full#endif

//=============================================================================
// The variables for debug
//=============================================================================
#ifdef DEBUG_C
ULONG	IDEResetCount;
ULONG	USBResetCount;
ULONG	EP0InErrorCount;
ULONG	EP0OutErrorCount;
ULONG	BulkInErrorCount;
ULONG	BulkOutErrorCount;
ULONG	CBWErrorCount;
ULONG	CSWErrorCount;
#endif
/* 
//=============================================================================
// Function_Name: SendCallback
// description	: Do callback notification to the other module.
// argument		: ULONG msg 	 Message for transfer
//				: ULONG param0	 Parameter for Transfer
//				: void* pParam1  Parameter for Transfer
// return		: none

//=============================================================================
*/
Inline void SendCallback ( ULONG msg, ULONG param0, void* pParam1 )
{
	if( DEVICEInfo.pfnMsgProc != NULL )

⌨️ 快捷键说明

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