📄 drvusb.c
字号:
/*---------------------------------------------------------------------------------------------------------*/
/* */
/* Copyright(c) 2009 Nuvoton Technology Corp. All rights reserved. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
#include "DrvUSB.h"
S_DRVUSB_DEVICE gsUsbDevice;
PFN_DRVUSB_INTCALLBACK g_FnIntCallBack;
uint32_t CFG_EP_SETTING[6];
static uint8_t *g_UsbSramBase;
static int32_t DrvUSB_Init(void);
static void DrvUSB_UnInit(void);
void WordsCpy(void *dest, void *src, int32_t size)
{
uint8_t *pu8Src, *pu8Dest;
int32_t i;
pu8Dest = (uint8_t *)dest;
pu8Src = (uint8_t *)src;
for(i=0;i<size;i++)
pu8Dest[i] = pu8Src[i];
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_GetVersion */
/* */
/* Parameters: */
/* None */
/* */
/* Returns: */
/* Version number */
/* */
/* Description: */
/* Get USB driver's version */
/* */
/*---------------------------------------------------------------------------------------------------------*/
uint32_t DrvUSB_GetVersion(void)
{
return DRVUSB_VERSION_NUM;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_DataOutTrigger */
/* */
/* Parameters: */
/* None */
/* */
/* Returns: */
/* None */
/* */
/* Description: */
/* USB interrupt handler */
/* */
/*---------------------------------------------------------------------------------------------------------*/
void USBD_IRQHandler(void)
{
DrvUSB_PreDispatchEvent();
if (g_FnIntCallBack)
g_FnIntCallBack((void *)&gsUsbDevice);
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_Open */
/* */
/* Parameters: */
/* pVoid user interrupt callback function, it will be call in UsbIntHandler */
/* */
/* Returns: */
/* 0 Success */
/* Otherwise Error */
/* */
/* Description: */
/* Open USB, include assign USB SRAM, init state and install */
/* interrupt handlerClose a file */
/* */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvUSB_Open(void * pVoid)
{
uint32_t i, j;
uint8_t *p;
DrvUSB_Init();
if (pVoid != NULL)
g_FnIntCallBack = (PFN_DRVUSB_INTCALLBACK)pVoid;
gsUsbDevice.u16MiscEventFlags = 0;
gsUsbDevice.u16EPEventFlags = 0;
p = (uint8_t *)USB_SRAM_BASE;
/* for setup packet */
gsUsbDevice.sEpCrl[MAX_EP_ID].u32EpNum = 0x0;
gsUsbDevice.sEpCrl[MAX_EP_ID].u32MaxPacketSize = 8;
gsUsbDevice.sEpCrl[MAX_EP_ID].u8SramBuffer = p;
p += 8;
i = 0;
while (sEpDescription[i].u32MaxPacketSize != 0)
{
/* There may be some EPs which have the same EP address. We will find them out here in order to use same buffer. */
for (j = 0; j < i; j++)
{
if ((sEpDescription[i].u32EpNum & 0xF) == (gsUsbDevice.sEpCrl[j].u32EpNum & 0xF))
break;
}
gsUsbDevice.sEpCrl[i].u32EpNum = sEpDescription[i].u32EpNum;
gsUsbDevice.sEpCrl[i].u32MaxPacketSize = sEpDescription[i].u32MaxPacketSize;
/* Assign the transfer buffer */
if (sEpDescription[i].u8SramBuffer == NULL)
{
if (j < i)
{
/* Use the same buffer if they have the same EP address */
gsUsbDevice.sEpCrl[i].u8SramBuffer = gsUsbDevice.sEpCrl[j].u8SramBuffer;
}
else
{
/* Assign a new buffer */
gsUsbDevice.sEpCrl[i].u8SramBuffer = p;
p += sEpDescription[i].u32MaxPacketSize;
}
/* Write back the assigned buffer */
sEpDescription[i].u8SramBuffer = gsUsbDevice.sEpCrl[i].u8SramBuffer;
}
if ((sEpDescription[i].u32EpNum & 0x1f) == 0)
{
/* Only ctrl EP needs to support STALL auto clear */
CFG_EP_SETTING[i] = (CFG_CSTALL |
((sEpDescription[i].u32EpNum&EP_INPUT)?CFG_EPT_IN:CFG_EPT_OUT)
| (sEpDescription[i].u32EpNum&0x0F));
}
else
{
CFG_EP_SETTING[i] = ((sEpDescription[i].u32EpNum&EP_INPUT)?CFG_EPT_IN:CFG_EPT_OUT)
| (sEpDescription[i].u32EpNum&0x0F);
}
i++;
}
/* Update the remind buffer base */
g_UsbSramBase = p;
gsUsbDevice.eUsbState = eDRVUSB_DETACHED;
gsUsbDevice.u32FLDET = USBD->FLDET.FLDET;
DrvUSB_PreDispatchFDTEvent(&gsUsbDevice);
DrvUSB_DispatchMiscEvent(&gsUsbDevice);
NVIC_SetPriority (USBD_IRQn, (1<<__NVIC_PRIO_BITS) - 2);
NVIC_EnableIRQ(USBD_IRQn);
return 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_PreDispatchEvent */
/* */
/* Parameters: */
/* None */
/* */
/* Returns: */
/* None */
/* */
/* Description: */
/* Pre dispatch event base on EVF register */
/* */
/*---------------------------------------------------------------------------------------------------------*/
void DrvUSB_PreDispatchEvent(void)
{
gsUsbDevice.u32INTSTS = _DRVUSB_GET_EVENT_FLAG();
if (gsUsbDevice.u32INTSTS & INTSTS_WAKEUP)
{
/* Clear wakeup event. write one clear */
_DRVUSB_SET_EVENT_FLAG(INTSTS_WAKEUP);
/* Pre-dispatch wakeup event. */
DrvUSB_PreDispatchWakeupEvent(&gsUsbDevice);
}
else if (gsUsbDevice.u32INTSTS & INTSTS_FLDET)
{
gsUsbDevice.u32FLDET = USBD->FLDET.FLDET;
/* Clear float-detection event. Write one clear */
_DRVUSB_SET_EVENT_FLAG(INTSTS_FLDET);
/* Pre-dispatch float-detection event. */
DrvUSB_PreDispatchFDTEvent(&gsUsbDevice);
}
else if (gsUsbDevice.u32INTSTS & INTSTS_BUS)
{
gsUsbDevice.u32ATTR = *((__IO uint32_t *)(&USBD->ATTR));
/* Clear bus event. Write one clear */
_DRVUSB_SET_EVENT_FLAG(INTSTS_BUS);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -