?? usbdma.c
字號:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: USBdma.c
** Last modified Date: 2005-8-6
** Last Version: V1.0
** Descriptions: LPC2148 USB DMA 控制器相關, 本文件只使能了物理端點 2 ~ 5 的 DMA 功能
** LPC2148 USB DMA controller, the file only enable the DMA function of physical endpoint 2 ~ 5
**------------------------------------------------------------------------------------------------------
** Created by: 鄭明遠 MingYuan Zheng
** Created date: 2005-8-6
** Version: V1.0
** Descriptions: 初始版本 The original version
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
**------------------------------------------------------------------------------------------------------
** Modified by:
** Modified date:
** Version:
** Descriptions:
**
********************************************************************************************************/
#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
/*
***********************************************************************
* 用戶可使用的API函數 API Function
***********************************************************************
*/
/************************************************************************************************************
** 函數名稱: USB_DMAInit() Name : USB_DMAInit()
** 功能描述: LPC214x DMA引擎初始化 Function : Initialize DMA engine of LPC214x 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_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 LPC214x 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_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 */
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)
bEPPflags.bits.ep1_rxdma = 1; /* 標志邏輯端點1 DMA接收成功 Flag logic ep0 DMA receiving sucessfully */
if (endp == 4)
bEPPflags.bits.ep2_rxdma = 1; /* 標志邏輯端點2 DMA接收成功 Flag logic ep2 DMA receiving sucessfully */
}
USBEoTIntClr |= (0x01 << endp); /* 清除傳輸結束中斷狀態寄存器 clear end of transfer register */
USB_InitEndpointDD(endp); /* 重新初始化DD re-Initialize DD */
if ((endp % 2) != 0)
USBEPIntEn |= (0x01 << endp); /* 重新使能該端點從機中斷 re-Enable the endpoint slave Interrupt */
}
}
}
/************************************************************************************************************
** 函數名稱: 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_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_InitEndpointDD(endp); /* 重新初始化DD re-Initialize DD */
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 + -