⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 usbdma.c

📁 LPC2148 USB固件程序,是学习固件不可多的的范例.
💻 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 + -