?? 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;
USBUDCAH = USB_RAM_ADDRESS; /* 初始化UDCA頭寄存器 Initialize the UDCA register */
pUDCA = (INT32U *)USBUDCAH; /* 取出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 + -