📄 usbdma.c
字号:
{
sendbuf = USB_DMAGetBuffer(endp);
sendlen = USB_DMASetTransLength(endp, len);
memcpy(sendbuf, pBuff + pUsb->cnt, sendlen);
pUsb->cnt = pUsb->cnt + sendlen;
USB_DMAStart_IN(endp); /* 启动 IN 端点的 DMA 传输 start the DMA transfer of IN endpoint */
}
}
}
/************************************************************************************************************************
** 函数名称: USB_DMAReadISR() Name: USB_DMAReadISR()
** 功能描述: USB 端点接收中断服务程序 (DMA) Function: the reception interrupt service of USB endpoint
** 输 入: CTRL_USB *pUsb: USB接收与发送控制结构体指针 Input: CTRL_USB *pUsb: the CTRL_USB structure
INT8U endp: 物理端点号 INT8U endp: the number of physical endpoint
** 输 出: 无 Output: NULL
*************************************************************************************************************************/
void USB_DMAReadISR(CTRL_USB *pUsb, INT8U endp)
{
INT8U *pBuff,*recbuf;
INT32U len,reclen;
if (pUsb->bEpUsed != 0)
{ /* 有任务在等待接收 exist task waiting for reception */
len = pUsb->len - pUsb->cnt; /* 计算未接收字节数 calculate the numbers of bytes that will be received */
pBuff = pUsb->buff;
if (len > 0)
{
recbuf = USB_DMAGetBuffer(endp); /* DMA接收缓冲区地址 DMA recbuffer */
reclen = USB_DMAGetRecLength(endp); /* 收到的字节数 received length */
if (len >= reclen)
{
memcpy(pBuff + pUsb->cnt, recbuf, reclen);
pUsb->cnt = pUsb->cnt + reclen; /* 计数器计数 counter counting */
}
else
{
memcpy(pBuff + pUsb->cnt, recbuf, len); /* 从DMA缓冲区取出数据 get data from DMA buffer */
pUsb->cnt = pUsb->cnt + len; /* 计数器计数 counter counting */
}
}
/********** 判断是否接收完成 **********/
if (pUsb->cnt >= pUsb->len)
{ /* 如果接收完毕 if the reception is finished */
OSSemPost(Ctrl_Usb[endp - 2].Ep_Sem);
}
else
{
len = pUsb->len - pUsb->cnt;
USB_DMASetTransLength(endp, len); /* 设置要接收的字节长度set the length that will be received */
}
}
}
/************************************************************************************************************
** 函数名称: USB_DMATransferEnd() Name : USB_DMATransferEnd()
** 功能描述: DMA 传输结束中断处理 Function : Process End of DMA Transfer Interrupt
************************************************************************************************************/
void USB_DMATransferEnd(void)
{
INT32U ep_st, endp;
DD_DESCRIPTOR *pDD;
ep_st = USBEoTIntSt; /* 读传输结束中断寄存器 read End of Transfer Interrupt register */
for (endp = 2; endp <= 5; endp++)
{
if(ep_st & (0x01 << endp))
{ /* endp 端点中断传输结束 endp endpoint Interrupt transfer end */
if ((endp % 2) != 0)
USBEPIntEn |= (0x01 << endp); /* 重新使能该端点从机中断 re-Enable the endpoint slave Interrupt */
pDD = USB_GetDDPointer(endp);
if ((pDD->status & 0x1F) == DDS_OVER_RUN) /* DMA错误 DMA error */
USBEpDMAEn = (0x01 << endp); /* 重新使能该端点的DMA功能 re-enable Endpoint DMA */
if ((pDD->status & 0x1F) == DDS_NORMAL_COMP) /* DMA正常结束 DMA normal complete */
{
if ((endp % 2) != 0)
USB_DMAWriteISR(&Ctrl_Usb[endp - 2], endp);
else
USB_DMAReadISR(&Ctrl_Usb[endp - 2], endp);
}
USBEoTIntClr |= (0x01 << endp); /* 清除传输结束中断状态寄存器 clear end of transfer register */
}
}
}
/************************************************************************************************************
** 函数名称: USB_DMASystemError() Name : USB_DMASystemError()
** 功能描述: DMA 系统中断错误处理 Function : Process System Error Interrupt
************************************************************************************************************/
void USB_DMASystemError(void)
{
INT32U ep_st,endp;
ep_st = USBSysErrIntSt;
for (endp = 2; endp < 5; endp++)
{
if(ep_st & (0x01 << endp))
{
USB_InitEndpointDD(endp); /* 重新初始化DD re-Initialize DD */
USBEpDMAEn = (0x01 << endp); /* 重新使能该端点的DMA功能 re-enable Endpoint DMA */
USBSysErrIntClr |= (0x01 << endp); /* 清除系统错误中断 clear the System error interrupt */
if ((endp % 2) != 0)
USBEPIntEn |= (0x01 << endp); /* 重新使能该 IN 端点从机中断 re-Enable the IN endpoint slave Interrupt */
}
}
}
/************************************************************************************************************
** 函数名称: USB_DMANewDDDone() Name : USB_DMANewDDDone()
** 功能描述: DMA 新 DD 请求中断处理 Function : Process New DD Request Interrupt
************************************************************************************************************/
void USB_DMANewDDDone(CTRL_USB *pUsb, INT8U endp)
{
INT32U len;
if (pUsb->bEpUsed != 0)
{ /* 有任务在等待接收 exist task waiting for reception */
if (pUsb->cnt >= pUsb->len)
{ /* 如果接收完毕 if the reception is finished */
OSSemPost(Ctrl_Usb[endp - 2].Ep_Sem);
}
else
{
len = pUsb->len - pUsb->cnt;
USB_DMASetTransLength(endp, len); /* 设置DMA接收/发送的字节长度 set the length of DMA receving/transmitting */
}
}
}
/************************************************************************************************************
** 函数名称: USB_DMANewDDRequest() Name : USB_DMANewDDRequest()
** 功能描述: DMA 新 DD 请求中断处理 Function : Process New DD Request Interrupt
************************************************************************************************************/
void USB_DMANewDDRequest(void)
{
INT32U ep_st,endp;
ep_st = USBNDDRIntSt;
for (endp = 2; endp <= 5; endp++)
{
if(ep_st & (0x01 << endp))
{
USB_DMANewDDDone(&Ctrl_Usb[endp - 2], endp); /* 重新设置该端点的DD re-Initialize DD of the endpoint */
USBEpDMAEn = (0x01 << endp); /* 重新使能该端点的DMA功能 re-enable Endpoint DMA */
USBNDDRIntClr = (0x01 << endp); /* 清除新DD请求中断 clear the New DD Request Interrupt */
if ((endp % 2) != 0)
USBEPIntEn |= (0x01 << endp); /* 重新使能该端点从机中断 re-Enable the endpoint slave Interrupt */
}
}
}
/*
***********************************************************************
* 本文件用到的子程序 The Subprogram used by this file
***********************************************************************
*/
/************************************************************************************************************
** 函数名称: USB_InitUdcaTable() Name : USB_InitUdcaTable()
** 功能描述: 初始化DMA引擎的UDCA寄存器 Function : Initialize the UDCA reigister and UDCA table
和UDCA表 of DMA engine
************************************************************************************************************/
void USB_InitUdcaTable(void)
{
INT32U *pUDCA;
USBUCDAH = USB_RAM_ADDRESS; /* 初始化UDCA头寄存器 Initialize the UDCA register */
pUDCA = (INT32U *)USBUCDAH; /* 取出UDCA寄存器值 Get the value of UDCA register */
pUDCA[2] = (INT32)DD_ADDRESS_EP02; /* 建立UDCA表 Build UDCA table */
pUDCA[3] = (INT32)DD_ADDRESS_EP03;
pUDCA[4] = (INT32)DD_ADDRESS_EP04;
pUDCA[5] = (INT32)DD_ADDRESS_EP05;
}
/************************************************************************************************************
** 函数名称: USB_InitEndpointDD() Name : USB_InitEndpointDD()
** 功能描述: 初始化端点的DD Function : Initialize the DD of the endpoint
** 输 入: INT8U endp: 物理端点号 Input : INT8U endp: physical endpoint
** 输 出: 无 Output : NULL
************************************************************************************************************/
void USB_InitEndpointDD(INT8U endp)
{
DD_DESCRIPTOR *pDD = USB_GetDDPointer(endp); /* 取得 DD 指针 Get the DD pointer of the endpoint */
pDD->next_dd_addr = 0; /* 清零两个成员 Clear two members */
pDD->status = 0;
switch(endp)
{
case 0x02: pDD->control = (EP1_PACKET_SIZE << 5) + /* 写端点信息包大小 Write endpoint packet size */
(EP02_DMA_BUFFER_LENGTH << 16); /* 写DMA缓冲区大小 Write dma buffer size */
pDD->start_addr = DMA_BUFFER_ADDR_EP02; /* 写DMA缓冲区地址 Write dma buffer address */
break;
case 0x03: pDD->control = (EP1_PACKET_SIZE << 5) +
(EP03_DMA_BUFFER_LENGTH << 16);
pDD->start_addr = DMA_BUFFER_ADDR_EP03;
break;
case 0x04: pDD->control = (EP2_PACKET_SIZE << 5) +
(EP04_DMA_BUFFER_LENGTH << 16);
pDD->start_addr = DMA_BUFFER_ADDR_EP04;
break;
case 0x05: pDD->control = (EP2_PACKET_SIZE << 5) +
(EP05_DMA_BUFFER_LENGTH << 16);
pDD->start_addr = DMA_BUFFER_ADDR_EP05;
break;
default: break;
}
}
/************************************************************************************************************
** 函数名称: USB_GetDDPointer() Name : USB_GetDDPointer()
** 功能描述: 获取端点的DD指针 Function : Get the DD pointer of the endpoint
** 输 入: INT8U endp: 物理端点号 Input : INT8U endp: physical endpoint
** 返 回: DD_DESCRIPTOR * 端点的 DD 描述符指针 Return : the DD pointer of the endpoint
************************************************************************************************************/
DD_DESCRIPTOR *USB_GetDDPointer(INT8U endp)
{
INT32U dd_addr;
dd_addr = DD_BASE_ADDRESS + DD_SIZE * (endp - 2); /* 计算地址 calculate the address */
return (DD_DESCRIPTOR *)((INT32U *)dd_addr); /* 转换指针类型 change the type of the pointer */
}
#endif
/*******************************************************************************************************
** End Of File
********************************************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -