?? usbdma.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzhou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: USBCI.c
** Created by: MingYuan Zheng
** Created date: 2005-1-6
** Last modified Date:
** Last Version: V1.0
** Descriptions: LPC2148 USB DMA
**
*******************************************************************************************************/
#include "config.h"
#include "USBConfig.h"
#include "USBCI.h"
#include "descriptor.h"
#include "USBDriver.h"
#include "USBdma.h"
#include "string.h"
#if DMA_ENGINE_EN
/*
******************************************************************************
* uCOS-II相關 API 函數 API Function releated with uCOS-II
******************************************************************************
*/
/*************************************************************************************************************************
** 函數名稱: USB_ReadPortDMA() Name: USB_ReadPortDMA()
** 功能描述: 讀端點(DMA方式) Function: read data from endpoint(DMA Mode)
** 輸 入: INT8U endp: 物理端點索引號 Input: INT8U endp: the physical endpoint number
CTRL_USB *pUsb: 接收/發送數據控制塊 CTRL_USB *pUsb: the control block of receiving/sending
INT32U len: 接收字節個數 INT32U len: the numbers(Bytes) that will be received
INT8U *recbuff: 接收緩沖區 INT8U *recbuff: the reception buffer
INT16U timeout: 超時等待時間, 等于0表示無限等待 INT16U timeout: timeout of receiving,0 indicates limitless waiting
** 輸 出: 0: 讀成功 > 0 讀失敗(錯誤碼) Output: 0: sucessfully >0: fail (it is error code)
**************************************************************************************************************************/
INT8U USB_ReadPortDMA(INT8U endp, CTRL_USB *pUsb, INT32U len, INT8U *recbuff, INT16U timeout)
{
INT8U err;
OS_ENTER_CRITICAL();
err = USB_RW_Param(pUsb, recbuff); /* 檢查參數正確性 check the parameter */
if (err != USB_NO_ERR)
{
OS_EXIT_CRITICAL();
return err; /* 返回錯誤碼 return error code */
}
OS_EXIT_CRITICAL();
OS_ENTER_CRITICAL();
pUsb->buff = recbuff;
pUsb->len = len; /* 要接收的字節長度 the bytes length that will be received */
pUsb->cnt = 0;
USB_DMASetTransLength(endp, len);
OS_EXIT_CRITICAL();
OSSemPend(pUsb->Ep_Sem, timeout, &err); /* 等待DMA接收數據的完成 wait for finishing receiving data */
OS_ENTER_CRITICAL();
pUsb->bEpUsed = 0;
OS_EXIT_CRITICAL();
if (err == OS_NO_ERR)
return USB_NO_ERR; /* 接收成功 receive sucessfully */
else
return USB_ERR_WR_TIMEOUT; /* 接收超時錯誤 receive fail because it is timeout */
}
/*************************************************************************************************************************
** 函數名稱: USB_WritePortDMA() Name: USB_WritePortDMA()
** 功能描述: 向USB主機發送數據(DMA方式) Function: send data to USB Host(DMA Mode)
** 輸 入: INT8U endp: 物理端點索引號 Input: INT8U endp: the physical endpoint number
CTRL_USB *pUsb: 接收/發送數據控制塊 CTRL_USB *pUsb: the control block of receiving/sending
INT8U *sendbuff: 接收緩沖區 INT8U *sendbuff: the transmision buffer
INT32U len: 接收字節個數 INT32U len: the numbers(Bytes) that will be received
INT16U timeout: 超時等待時間, 等于0表示無限等待 INT16U timeout: timeout of transmision,0 indicates limitless waiting
** 輸 出: 0: 讀成功 > 0 讀失敗(錯誤碼) Output: 0: sucessfully >0: fail (it is error code)
**************************************************************************************************************************/
INT8U USB_WritePortDMA(INT8U endp, CTRL_USB *pUsb, INT8U *sendbuff, INT32U len, INT16U timeout)
{
INT8U err,*pdmabuf;
INT32U sendlen;
OS_ENTER_CRITICAL();
err = USB_RW_Param(pUsb, sendbuff); /* 檢查參數正確性 check the parameter */
if (err != USB_NO_ERR)
{
OS_EXIT_CRITICAL();
return err; /* 返回錯誤碼 return error code */
}
OS_EXIT_CRITICAL();
OS_ENTER_CRITICAL();
pUsb->buff = sendbuff;
pUsb->len = len;
pUsb->cnt = 0;
pdmabuf = USB_DMAGetBuffer(endp); /* 取得該端點的DMA緩沖區首地址 get the DMA buffer */
sendlen = USB_DMASetTransLength(endp, len);
memcpy(pdmabuf, sendbuff, sendlen); /* 復制數據到DMA發送緩沖區 copy data to DMA send buffer */
pUsb->cnt = pUsb->cnt + sendlen; /* 計數器計數 counter counting */
USB_DMAStart_IN(endp); /* 啟動 IN 端點的 DMA 傳輸 start the DMA transfer */
OS_EXIT_CRITICAL();
OSSemPend(pUsb->Ep_Sem, timeout, &err); /* 等待 DMA 發送數據的完成 wait for finishing transmitting data */
pUsb->bEpUsed = 0;
if (err == OS_NO_ERR)
{
USBEpIntEn |= (0x01 << endp);
return USB_NO_ERR; /* 發送成功 transmit sucessfully */
}
else
{
USBEpIntEn |= (0x01 << endp);
return USB_ERR_WR_TIMEOUT; /* 發送成功 transmit sucessfully */
}
}
/*
***********************************************************************
* 用戶可使用的API函數 API Function
***********************************************************************
*/
/************************************************************************************************************
** 函數名稱: USB_DMAInit() Name : USB_DMAInit()
** 功能描述: LPC23xx DMA引擎初始化 Function : Initialize DMA engine of LPC23xx USB
************************************************************************************************************/
void USB_DMAInit(void)
{
USB_InitUdcaTable(); /* 初始化UDCA表 Initialize the UDCA table */
USB_InitEndpointDD(2); /* 初始化各端點的DD Initialize the DD of each endpoint */
USB_InitEndpointDD(3);
USB_InitEndpointDD(4);
USB_InitEndpointDD(5);
/* Enable System error, New DD Request and End of Transfer Interrupt of DMA */
USBDMAIntEn = USBDMA_EOT_INT | USBDMA_NDD_INT | USBDMA_ERR_INT; /* 使能DMA的系統錯誤中斷, 新DD請求中斷, 傳輸結束中斷 */
USBEpDMAEn = (0x01 << 2) | (0x01 << 3) | (0x01 << 4) | (0x01 << 5);
/* 使能端點DMA功能 Enable Endpoint DMA */
}
/************************************************************************************************************
** 函數名稱: USB_DMASetTransLength() Name : USB_DMASetTransLength()
** 功能描述: 設置 DMA 傳輸長度 Function : Config DMA transfer length
** 輸 入: INT8U endp: 物理端點號 Input : INT8U endp: physical endpoint
INT16U len: 傳輸字節長度 INT16U len: transfer byte length
** 輸 出: 實際 DMA 傳輸長度 Output : the actual transfer length
************************************************************************************************************/
INT32U USB_DMASetTransLength(INT8U endp, INT32U len)
{
DD_DESCRIPTOR *pDD = USB_GetDDPointer(endp); /* 取得 DD 指針 Get the DD pointer of the endpoint */
USB_InitEndpointDD(endp); /* 初始化DD Initialize the DD */
if (len > (pDD->control >> 16)) /* 長度超過本端點DMA緩沖區 len is beyond the dma buffer of the endpoint*/
len = pDD->control >> 16;
pDD->control &= 0x0000FFFF;
pDD->control |= (len << 16); /* 將len寫入DD Write the len into DD */
return len;
}
/************************************************************************************************************
** 函數名稱: USB_DMAGetRecLength() Name : USB_DMAGetRecLength()
** 功能描述: 取得當前 DMA 傳輸長度 Function : get the current DMA transfer length
** 輸 入: INT8U endp: 物理端點號 Input : INT8U endp: physical endpoint
** 輸 出: 當前 DMA 傳輸長度 Output : current transfer length
************************************************************************************************************/
INT32U USB_DMAGetRecLength(INT8U endp)
{
DD_DESCRIPTOR *pDD = USB_GetDDPointer(endp); /* 取得 DD 指針 Get the DD pointer of the endpoint */
return (pDD->status & 0xFFFF0000) >> 16; /* 返回長度值 return the length value */
}
/************************************************************************************************************
** 函數名稱: USB_DMAStart_IN() Name : USB_DMAStart_IN()
** 功能描述: 啟動 IN 端點的 DMA 傳輸 Function : start the DMA transfer of IN endpoint
** 輸 入: INT8U endp: 物理端點號 Input : INT8U endp: physical endpoint
** 輸 出: 無 Output : NULL
************************************************************************************************************/
void USB_DMAStart_IN(INT8U endp)
{
USBEpIntEn &= ~(0x01 << endp); /* 清0 從機端點中斷使能寄存器來啟動 IN 傳輸 */
} /* clear slave endpoint interrupt enable register to start IN transfer */
/************************************************************************************************************
** 函數名稱: USB_DMAGetBuffer() Name : USB_DMAGetBuffer()
** 功能描述: 得到DMA緩沖區首址 Function : Initialize DMA engine of LPC23xx USB
************************************************************************************************************/
INT8U* USB_DMAGetBuffer(INT8U endp)
{
const INT32U DmaTbl[4] = {DMA_BUFFER_ADDR_EP02, DMA_BUFFER_ADDR_EP03,
DMA_BUFFER_ADDR_EP04, DMA_BUFFER_ADDR_EP05};
return (INT8U *)((INT32U *)DmaTbl[endp - 2]); /* 返回緩沖區字節指針 return the byte buffer pointer */
}
/*
***********************************************************************
* DMA 中斷服務程序 The Interrupt Service of the DMA
***********************************************************************
*/
/************************************************************************************************************
** 函數名稱: Usb_DMAService() Name : Usb_DMAService()
** 功能描述: DMA 中斷服務程序 Function : USB DMA Interrupt Service Program
************************************************************************************************************/
void USB_DMAService(void)
{
INT32U dma_st;
dma_st = USBDMAIntSt; /* 讀DMA中斷狀態寄存器 Read USBDMAIntSt */
if (dma_st & USBDMA_EOT_INT)
USB_DMATransferEnd(); /* 傳輸結束中斷 End of Transfer Interrupt */
if (dma_st & USBDMA_NDD_INT)
USB_DMANewDDRequest(); /* 新DD請求中斷 New DD Request Interrupt */
if (dma_st & USBDMA_ERR_INT)
USB_DMASystemError(); /* 系統錯誤中斷 System Error Interrupt */
}
/************************************************************************************************************************
** 函數名稱: USB_DMAWriteISR() Name: USB_DMAWriteISR()
** 功能描述: USB 端點發送中斷服務程序(DMA) Function: the transmission 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_DMAWriteISR(CTRL_USB *pUsb, INT8U endp)
{
INT8U *pBuff,*sendbuf;
INT32U len,sendlen;
if (pUsb->bEpUsed != 0)
{ /* 有任務在等待發送 exist task waiting for transmission */
if (pUsb->cnt >= pUsb->len)
{
OSSemPost(Ctrl_Usb[endp - 2].Ep_Sem); /* 數據全部發送完成 all data have been send */
return;
}
len = pUsb->len - pUsb->cnt; /* 計算未發送字節數 calculate the numbers of bytes that will be transmitted */
pBuff = pUsb->buff;
if (len > 0)
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -