📄 drvusb.c
字号:
/* */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvUSB_DataOutTrigger(uint32_t u32EpNum, uint32_t u32Size)
{
uint32_t u32EpId;
S_DRVUSB_DEVICE *psDevice = &gsUsbDevice;
u32EpId = DrvUSB_GetEpIdentity(u32EpNum, EP_OUTPUT);
if (u32Size > psDevice->sEpCrl[u32EpId].u32MaxPacketSize)
return E_DRVUSB_SIZE_TOO_LONG;
_DRVUSB_SET_EP_BUF(u32EpId, (uint32_t)psDevice->sEpCrl[u32EpId].u8SramBuffer);
_DRVUSB_TRIG_EP(u32EpId, u32Size);
return 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_GetOutData */
/* */
/* Parameters: */
/* u32EpNum [in] Endpoint number */
/* u32Size [out] Data size that received */
/* */
/* Returns: */
/* USB SRAM address */
/* */
/* Description: */
/* This function will return the buffer pointer of u32EpNum's out */
/* USB SRAM buffer. User can use this pointer to get the data */
/* payload of current data out packet. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
uint8_t * DrvUSB_GetOutData(uint32_t u32EpNum, uint32_t *u32Size)
{
uint32_t u32EpId;
u32EpId = DrvUSB_GetEpIdentity(u32EpNum, EP_OUTPUT);
*u32Size = _DRVUSB_GET_EP_DATA_SIZE(u32EpId);
return sEpDescription[u32EpId].u8SramBuffer;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_DataIn */
/* */
/* Parameters: */
/* u32EpNum [in] EP number, send data from it */
/* u8Buffer [in] Data buffer */
/* u32Size [in] Data size */
/* \ */
/* Returns: */
/* 0 Success */
/* Otherwise error */
/* */
/* Description: */
/* Trigger ready flag for sending data */
/* after receive IN token from host, USB will send the data */
/* if u8Buffer == NULL && u32Size == 0 then send DATA1 always */
/* else DATA0 and DATA1 by turns */
/* */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvUSB_DataIn(uint32_t u32EpNum, const uint8_t * u8Buffer, uint32_t u32Size)
{
S_DRVUSB_DEVICE *psDevice = &gsUsbDevice;
uint32_t u32EpId;
u32EpId = DrvUSB_GetEpIdentity(u32EpNum, EP_INPUT);
if (u32Size > psDevice->sEpCrl[u32EpId].u32MaxPacketSize)
return E_DRVUSB_SIZE_TOO_LONG;
if (u8Buffer && u32Size)
{
WordsCpy(psDevice->sEpCrl[u32EpId].u8SramBuffer, (void *)u8Buffer, u32Size);
}
_DRVUSB_SET_EP_BUF(u32EpId, (uint32_t)psDevice->sEpCrl[u32EpId].u8SramBuffer);
if (u8Buffer == NULL && u32Size == 0)
psDevice->abData0[u32EpId] = 0;
else
psDevice->abData0[u32EpId] = !psDevice->abData0[u32EpId];
_DRVUSB_SET_EP_TOG_BIT(u32EpId, psDevice->abData0[u32EpId]);
_DRVUSB_TRIG_EP(u32EpId, u32Size);
return 0;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_BusResetCallback */
/* */
/* Parameters: */
/* pVoid [in] Parameter passed by g_sBusOps[] */
/* */
/* Returns: */
/* None */
/* */
/* Description: */
/* Bus reset handler. After receiving bus reset event, this handler */
/* will be called. It will reset USB address, accept SETUP packet */
/* and initial the endpoints. */
/* */
/*---------------------------------------------------------------------------------------------------------*/
void DrvUSB_BusResetCallback(void * pVoid)
{
S_DRVUSB_DEVICE *psDevice =&gsUsbDevice;
int i;
/* Reset function address of USB device */
_DRVUSB_SET_FADDR(0x00);
/* Disable IN NAK Flag */
USBD->INTEN.INNAK_EN = 0;
_DRVUSB_SET_SETUP_BUF(USB_BUF_SETUP);
/* Initial USB EP according to EP description */
i = 0;
while (sEpDescription[i].u32MaxPacketSize != 0)
{
/* Write one to clear IN/OUT ready flag */
_DRVUSB_SET_CFGP(i, 0x01);
/* Initial USB EP CFG Setting */
_DRVUSB_SET_CFG(i, CFG_EP_SETTING[i]);
_DRVUSB_SET_EP_BUF(i, (uint32_t)sEpDescription[i].u8SramBuffer);
i++;
}
psDevice->u8UsbAddress = 0;
psDevice->u8UsbConfiguration = 0;
if(psDevice->eUsbState > eDRVUSB_DEFAULT)
psDevice->eUsbState = eDRVUSB_DEFAULT;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_InstallClassDevice */
/* */
/* Parameters: */
/* sUsbClass [in] USB class structure pointer */
/* */
/* Returns: */
/* None */
/* */
/* Description: */
/* Register USB class device to USB driver */
/* */
/*---------------------------------------------------------------------------------------------------------*/
void * DrvUSB_InstallClassDevice(S_DRVUSB_CLASS *sUsbClass)
{
gsUsbDevice.psUsbClass = sUsbClass;
return &gsUsbDevice;
}
/*---------------------------------------------------------------------------------------------------------*/
/* Function: DrvUSB_InstallCtrlHandler */
/* */
/* Parameters: */
/* device [in] USB driver device pointer */
/* psCtrlCallbackEntry [in] Handler structure pointer */
/* u32RegCnt [in] Handler structure size */
/* */
/* Returns: */
/* E_SUCCESS Success */
/* E_DRVUSB_NULL_POINTER NULL function pointer */
/* */
/* Description: */
/* Register ctrl pipe handler */
/* include SETUP ACK , IN ACK, OUT ACK handler */
/* for Standard/Vendor/Class command */
/* */
/*---------------------------------------------------------------------------------------------------------*/
int32_t DrvUSB_InstallCtrlHandler(void * *device,
S_DRVUSB_CTRL_CALLBACK_ENTRY *psCtrlCallbackEntry,uint32_t u32RegCnt)
{
S_DRVUSB_DEVICE *pDevice = (S_DRVUSB_DEVICE *)device;
S_DRVUSB_CTRL_CALLBACK_ENTRY *psEntry;
int i;
if (u32RegCnt == 0)
{
return 0;
}
if (psCtrlCallbackEntry == 0)
{
return E_DRVUSB_NULL_POINTER;
}
pDevice->pCtrlCallback = psCtrlCallbackEntry;
pDevice->CtrlCallbackSize = u32RegCnt;
for (i = 0; i < u32RegCnt; i++)
{
psEntry = psCtrlCallbackEntry + i;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -