📄 usbms_drv.c
字号:
g_UsbMS.ms_ep_in_info->epdesc.stdep.bDescriptorType = USB_ENDPOINT;
g_UsbMS.ms_ep_in_info->epdesc.stdep.bEndpointAddress = (USB_EP_DIR_IN | ep_in_id); /*InPipe*/
g_UsbMS.ms_ep_in_info->epdesc.stdep.bmAttributes = USB_EP_BULK;
g_UsbMS.ms_ep_in_info->epdesc.stdep.wMaxPacketSize[0] = USB_EP_BULK_MAXP&0xff;
g_UsbMS.ms_ep_in_info->epdesc.stdep.wMaxPacketSize[1] = (USB_EP_BULK_MAXP>>8)&0xff;
g_UsbMS.ms_ep_in_info->epdesc.stdep.bInterval = 0;
g_UsbMS.ms_ep_in_info->ep_status.epin_status.byEP = ep_in_id;
g_UsbMS.ms_ep_in_info->ep_status.epin_status.nBytesLeft = USB_EP_NODATA;
g_UsbMS.ms_ep_out_info->epdesc.stdep.bLength = USB_EPDSC_LENGTH;
g_UsbMS.ms_ep_out_info->epdesc.stdep.bDescriptorType = USB_ENDPOINT;
g_UsbMS.ms_ep_out_info->epdesc.stdep.bEndpointAddress = (USB_EP_DIR_OUT | ep_out_id); /*OutPipe*/
g_UsbMS.ms_ep_out_info->epdesc.stdep.bmAttributes = USB_EP_BULK;
g_UsbMS.ms_ep_out_info->epdesc.stdep.wMaxPacketSize[0] = USB_EP_BULK_MAXP&0xff;
g_UsbMS.ms_ep_out_info->epdesc.stdep.wMaxPacketSize[1] = (USB_EP_BULK_MAXP>>8)&0xff;
g_UsbMS.ms_ep_out_info->epdesc.stdep.bInterval = 0;
g_UsbMS.ms_ep_out_info->ep_status.epout_status.byEP = ep_out_id;
USB_Get_DMA_Channel(ep_in_id); /* in & out eps use the same DMA channel */
}
/* interface reset function, enable EP*/
void USB_Ms_If_Reset(void)
{
ilm_struct *usb_ilm;
/* initialize state machine*/
//g_UsbMS.nState = USBMS_IDLE;
/*this must not exist because use this trivky to avoid reset abnormal operation when writing to flash*/
g_UsbMS.txpipe = &g_UsbMS.ms_ep_in_info->ep_status.epin_status;
g_UsbMS.rxpipe = &g_UsbMS.ms_ep_out_info->ep_status.epout_status;
USB_Ms_Dbg_Trace(USB_DBG_USBMS_RESET,0);
/* configure DMA setting for EP */
USB_InEPEn(g_UsbMS.txpipe->byEP, KAL_TRUE);
USB_OutEPEn(g_UsbMS.rxpipe->byEP, KAL_TRUE);
USB_CtrlEPStall(g_UsbMS.txpipe->byEP, USB_IN_EP_TYPE, KAL_FALSE, USB_CTRL_STALL_ENTRY_2);
USB_CtrlEPStall(g_UsbMS.rxpipe->byEP, USB_OUT_EP_TYPE, KAL_FALSE, USB_CTRL_STALL_ENTRY_2);
/* send reset message to usb task, task handle the ms state machine*/
if (g_UsbMS.is_send_usb_reset_ilm)
{
kal_uint32 src_mod;
/* avoid sending too many messages to task when task is busy writing*/
g_UsbMS.is_send_usb_reset_ilm = KAL_FALSE;
if(kal_if_hisr() == KAL_TRUE)
src_mod = MOD_DRV_HISR;
else
src_mod = MOD_USB;
DRV_BuildPrimitive(usb_ilm, src_mod,
MOD_USB, MSG_ID_USB_RESET_IND, NULL);
msg_send_ext_queue(usb_ilm);
}
}
/************************************************************
global variable g_UsbMS initialize and release functions
*************************************************************/
/* initialize global variable g_UsbMS */
void USB_Init_Ms_Status(void)
{
g_UsbMS.nState = USBMS_IDLE;
g_UsbMS.txpipe = NULL;
g_UsbMS.rxpipe = NULL;
g_UsbMS.max_LUN = 0;
g_UsbMS.current_LUN = 0;
g_UsbMS.is_send_usb_reset_ilm = KAL_TRUE;
g_UsbMS.CSWDataResidue = 0;
g_UsbMS.CSWStatusError = KAL_FALSE;
g_UsbMS.sensePtr = NULL;
/*initialize debug buffer*/
USB_Ms_Util_Init();
}
/* release global variable g_UsbMS */
void USB_Release_Ms_Status(void)
{
g_UsbMS.nState = USBMS_IDLE;
g_UsbMS.txpipe = NULL;
g_UsbMS.rxpipe = NULL;
g_UsbMS.max_LUN = 0;
g_UsbMS.current_LUN = 0;
g_UsbMS.is_send_usb_reset_ilm = KAL_TRUE;
g_UsbMS.CSWDataResidue = 0;
g_UsbMS.CSWStatusError = KAL_FALSE;
g_UsbMS.sensePtr = NULL;
/* release debug buffer */
USB_Ms_Util_Release();
}
/************************************************************
Tx/Rx path DMA callback function
*************************************************************/
/* RX DMA callback function*/
static void USB_Ms_Rx_DMA_Callback(void)
{
if(gUsbDevice.is_configured_now == KAL_TRUE)
{
ilm_struct *usb_ilm;
usb_ms_rec_done_conf_struct *rec_done_conf;
// USB_Ms_Dbg_Trace(USB_DBG_REC_CALLBACK,0);
g_UsbMS.rxpipe->nBytesRecv = g_UsbMS.rxpipe->nBuffLen;
/*send message to usb task, let ms state machine handle the received data*/
rec_done_conf = (usb_ms_rec_done_conf_struct *)construct_local_para(
sizeof(usb_ms_rec_done_conf_struct), TD_CTRL);
rec_done_conf->pData = g_UsbMS.rxpipe->pData;
rec_done_conf->nBuffLen = g_UsbMS.rxpipe->nBuffLen;
rec_done_conf->nBytesRecv = g_UsbMS.rxpipe->nBuffLen;
//USB_Ms_Dbg_Trace(USB_DBG_REC_CALLBACK, rec_done_conf->nBytesRecv);
DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,
MOD_USB, MSG_ID_USB_MSDRV_REC_DONE_CONF, rec_done_conf);
msg_send_ext_queue(usb_ilm);
}
}
/* TX DMA callback function */
static void USB_Ms_Tx_DMA_Callback(void)
{
if(gUsbDevice.is_configured_now == KAL_TRUE)
{
ilm_struct *usb_ilm;
// USB_Ms_Dbg_Trace(USB_DBG_TRX_CALLBACK,0);
g_UsbMS.txpipe->nBytesLeft = USB_EP_NODATA;
//USB_Ms_Dbg_Trace(USB_DBG_TRX_CALLBACK,0);
/*send message to usb task, let ms state machine handle the transmitted data*/
DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,
MOD_USB, MSG_ID_USB_MSDRV_TRX_DONE_CONF, NULL);
msg_send_ext_queue(usb_ilm);
}
}
/************************************************************
EP0 command parse functions
*************************************************************/
/* reset command handler */
static void USB_Ms_Cmd_Reset(void)
{
ilm_struct *usb_ilm;
USB_Clear_IN_EP_FIFO(g_UsbMS.txpipe->byEP);
USB_Clear_OUT_EP_FIFO(g_UsbMS.rxpipe->byEP);
/* send reset message to usb task, task handle the state machine*/
if (g_UsbMS.is_send_usb_reset_ilm)
{
/* avoid sending too many messages to task when task is busy writing*/
g_UsbMS.is_send_usb_reset_ilm = KAL_FALSE;
DRV_BuildPrimitive(usb_ilm, MOD_DRV_HISR,
MOD_USB, MSG_ID_USB_RESET_IND, NULL);
msg_send_ext_queue(usb_ilm);
}
}
/* parse MS class specific command */
static void USB_Ms_Ep0_Command(Usb_Ep0_Status *pep0state, Usb_Command *pcmd)
{
kal_uint8 bError = KAL_FALSE;
switch (pcmd->bRequest)
{
case USB_MS_RESET:
if( (pcmd->bmRequestType != USB_CMD_CLASSIFOUT) ||(pcmd->wValue != 0) )
{
bError = KAL_TRUE;
kal_prompt_trace(MOD_USB, "USBMS cmd error %d", pcmd->bRequest);
#ifndef __PRODUCTION_RELEASE__
// EXT_ASSERT(0, (kal_uint32)pcmd->bmRequestType, (kal_uint32)pcmd->wValue, 0);
#endif
}
else
{
USB_Ms_Cmd_Reset();
USB_Ms_BulkIn_Reset();
USB_Ms_BulkOut_Reset();
}
break;
case USB_MS_GETMAXLUN:
if( (pcmd->bmRequestType != USB_CMD_CLASSIFIN) ||(pcmd->wValue != 0) )
{
bError = KAL_TRUE;
kal_prompt_trace(MOD_USB, "USBMS cmd error %d", pcmd->bRequest);
#ifndef __PRODUCTION_RELEASE__
// EXT_ASSERT(0, (kal_uint32)pcmd->bmRequestType, (kal_uint32)pcmd->wValue, 0);
#endif
}
else
{
g_UsbMS.max_LUN = USB_Ms_Get_Max_LUN();
USB_Generate_EP0Data(pep0state, pcmd, (void *)&g_UsbMS.max_LUN, 1);
}
break;
default:
bError = KAL_TRUE;
kal_prompt_trace(MOD_USB, "USBMS cmd error %d", pcmd->bRequest);
#ifndef __PRODUCTION_RELEASE__
// EXT_ASSERT(0, pcmd->bRequest, 0, 0);
#endif
break;
}
if (gUsbDevice.ep0_state == USB_EP0_IDLE)
{
gUsbDevice.ep0_state = USB_EP0_RX_STATUS;
USB_Update_EP0_State(USB_EP0_DRV_STATE_READ_END, bError, KAL_TRUE);
}
else
USB_Update_EP0_State(USB_EP0_DRV_STATE_READ_END, bError, KAL_FALSE);
}
/* bulk In endpoint reset function */
static void USB_Ms_BulkIn_Reset(void)
{
return;
}
/* bulk Out endpoint reset function */
static void USB_Ms_BulkOut_Reset(void)
{
return;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -