📄 usbdriver.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: USBDriver.c
** Last modified Date: 2005-8-6
** Last Version: V1.0
** Descriptions: LPC214x USB 应用层
** LPC214x USB Application Layer
**------------------------------------------------------------------------------------------------------
** 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 "string.h"
#include "USBHAL.h"
#include "USBCI.h"
#include "USBdma.h"
#include "Chap_9.h"
#include "Descriptor.h"
#include "USBDriver.h"
/* define USB event flag variable */
EPPFLAGS bEPPflags; /* 定义 USB 事件标志变量 */
/* Stack of the task that deal with control transfer */
OS_STK TaskSetupStk[128]; /* Setup包处理任务堆栈 */
/* the pointer of Event that deal SETUP packet */
OS_EVENT *pSetup_Event; /* Setup包处理任务事件指针 */
/* USB receive or transmit control block of LPC214x USB each endpoint */
CTRL_USB Ctrl_Usb[NUM_ENDPOINTS]; /* LPC214x USB 各端点对应的 USB 接收或发送控制块 */
/***********************************************************************************************************************
** 函数名称 : USB_Initialize() Name : USB_Initialize()
** 功能描述 : 初始化 USB 设备控制器 Function : Initialize the USB device controller
** 输 入 : 无 Input : NULL
** 输 出 : 0: 初始化成功 >0: 初始化失败(错误码) Output : 0: Initialize sucessfully >0: Initialize fail(error code)
***********************************************************************************************************************/
INT8U USB_Initialize(void)
{
int i;
OS_ENTER_CRITICAL();
USB_InitHareware(); /* 初始化硬件 Initialize the hardware */
bEPPflags.value = 0; /* 置USB事件标志为0 set USB event flags to zero */
if (USB_ReadTestRegister() != 0xA50F) /* 读测试寄存器 read test register */
{
OS_EXIT_CRITICAL();
return USB_ERR_READ_TESTREG; /* 初始化失败 initialize fail */
}
USB_USBDevIntConfig(); /* 配置中断 configure the USB interrupt */
USB_ConfigEndpoint(); /* 配置端点 configure the endpoint */
#if DMA_ENGINE_EN
USB_DMAInit(); /* 初始化USB DMA initialize the USB DMA */
USB_SetMode(0x28);
#else
USB_SetMode(0x00); /* 成功传输才产生中断 generate interrupt only transmit sucessfully */
#endif
pSetup_Event = OSSemCreate(0); /* SETUP包处理任务信号量 the semaphore of the task processing SETUP packet */
if (pSetup_Event == NULL)
{
OS_EXIT_CRITICAL();
return USB_ERR_CREATE_SEM; /* 创建失败,返回错误码 return error code when create fail */
}
for(i = 0; i < NUM_ENDPOINTS; i++)
{
Ctrl_Usb[i].bEpReady = 0;
Ctrl_Usb[i].bTaskWaiting = 0;
Ctrl_Usb[i].Ep_Sem = OSSemCreate(0);
if (Ctrl_Usb[i].Ep_Sem == NULL)
{
OS_EXIT_CRITICAL();
return USB_ERR_CREATE_SEM; /* 创建信号量失败 create semaphore fail */
}
}
reconnect_USB(); /* 重新连接USB reconnect the USB */
OS_EXIT_CRITICAL();
return USB_NO_ERR; /* 初始化USB成功 Initialize USB sucessfully */
}
/***********************************************************************************************************************
** 函数名称: void TaskSetup(void *pdata) Name: void TaskSetup(void *pdata)
** 功能描述: 控制传输处理 Function: deal with control transfer
** 输 入: void *pdata 任务参数 Input: void *pdata: parameter of the task
** 输 出: 无 Output: NULL
** 注 意: 该任务的优先级应高于其它任务,才能在 Note: the prior of the task must be higher than other,so
任何情况下传输SETUP包 it will be sucessful when tranfering SETUP packet
************************************************************************************************************************/
void TaskSetup(void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT8U err;
pdata = pdata; /* Prevent compiler warning */
for (;;)
{
OSSemPend(pSetup_Event,0,&err); /* 等待Setup包 wait for SETUP packet */
if (err == OS_NO_ERR){ /* 接收到Setup包 have received SETUP packet */
OS_ENTER_CRITICAL(); /* 关中断 disable all interrupt */
control_handler(); /* 进行控制传输处理 deal with control transfer */
OS_EXIT_CRITICAL(); /* 开中断 enable all interrupt */
}
}
}
/*******************************************************************************************************************************
** 函数名称: INT8U ReadPort1() Name: INT8U ReadPort1()
** 功能描述: 从端口 1 接收len个字节 Function: receive len Bytes from Port1
** 输 入: INT32U len: 要接收的字节数 Input: INT32U len: numbers will be receive
(取值范围为0x00000001 ~ 0xFFFFFFFF) (range: 0x00000001 ~ 0xFFFFFFFF)
INT8U recbuff: 接收缓冲区指针 INT8U recbuff: receive buffer
INT16U timeout: 超时等待时间, 等于0表示无限等待 INT16U timeout: timeout of receiving,0 indicates limitless waiting
** 输 出: 0: 接收成功 > 0: 接收失败(错误码) Output: 0: sucessfully >0 fail (error code)
*******************************************************************************************************************************/
INT8U ReadPort1(INT32U len, INT8U *recbuff, INT16U timeout)
{
#if DMA_ENGINE_EN
return (USB_ReadPortDMA(2, &Ctrl_Usb[0], len, recbuff, timeout)); /* DMA方式 */
#else
return (USB_ReadPort(2, 16, 1, &Ctrl_Usb[0], len, recbuff, timeout)); /* 非DMA方式 */
#endif
} //从物理端点 2 接收数据
/*******************************************************************************************************************************
** 函数名称: INT8U ReadPort2() Name: INT8U ReadPort2()
** 功能描述: 从端口 2 接收len个字节 Function: receive len Bytes from Port2
** 输 入: INT32U len: 要接收的字节数 Input: INT32U len: numbers will be receive
(取值范围为0x00000001 ~ 0xFFFFFFFF) (range: 0x00000001 ~ 0xFFFFFFFF)
INT8U recbuff: 接收缓冲区指针 INT8U sendbuff: receive buffer
INT16U timeout: 超时等待时间, 等于0表示无限等待 INT16U timeout: timeout of receiving,0 indicates limitless waiting
** 输 出: 0: 接收成功 > 0: 接收失败(错误码) Output: 0: sucessfully >0 fail (error code)
*******************************************************************************************************************************/
INT8U ReadPort2(INT32U len, INT8U *recbuff, INT16U timeout)
{
#if DMA_ENGINE_EN
return (USB_ReadPortDMA(4, &Ctrl_Usb[2], len, recbuff, timeout)); /* DMA方式 */
#else
return (USB_ReadPort(4, 64, 2, &Ctrl_Usb[2], len, recbuff, timeout)); /* 非DMA方式 */
#endif
} //从物理端点 4 接收数据
/*******************************************************************************************************************************
** 函数名称: INT8U WritePort1() Name: INT8U WritePort1()
** 功能描述: 用端口 1 发送len个字节 Function: Send len Bytes via Port1
** 输 入: INT32U len: 发送的字节数 Input: INT32U len: numbers will be send
(取值范围为0x00000001 ~ 0xFFFFFFFF) (range: 0x00000001 ~ 0xFFFFFFFF)
INT8U sendbuff: 发送缓冲区指针 INT8U sendbuff: send buffer
INT16U timeout: 超时等待时间, 等于0表示无限等待 INT16U timeout: timeout of transmitting,0 indicates limitless waiting
** 输 出: 0: 发送成功 > 0: 发送失败(错误码) Output: 0: sucessfully >0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort1(INT32U len, INT8U *sendbuff, INT16U timeout)
{
#if DMA_ENGINE_EN
return (USB_WritePortDMA(3, &Ctrl_Usb[1], sendbuff, len, timeout)); /* DMA方式 */
#else
return (USB_WritePort(3, 64, 2, &Ctrl_Usb[1], sendbuff, len, timeout)); /* 非DMA方式 */
#endif
} /* 通过物理端点 3 发送数据 */
/*******************************************************************************************************************************
** 函数名称: INT8U WritePort2() Name: INT8U WritePort2()
** 功能描述: 用端口 2 发送len个字节 Function: Send len Bytes via Port2
** 输 入: INT32U len: 发送的字节数 Input: INT32U len: numbers will be send
(取值范围为0x00000001 ~ 0xFFFFFFFF) (range: 0x00000001 ~ 0xFFFFFFFF)
INT8U sendbuff: 发送缓冲区指针 INT8U sendbuff: send buffer
INT16U timeout: 超时等待时间, 等于0表示无限等待 INT16U timeout: timeout of transmitting,0 indicates limitless waiting
** 输 出: 0: 发送成功 > 0: 发送失败(错误码) Output: 0: sucessfully >0 fail (error code)
*******************************************************************************************************************************/
INT8U WritePort2(INT32U len,INT8U *sendbuff,INT16U timeout)
{
#if DMA_ENGINE_EN
return (USB_WritePortDMA(5, &Ctrl_Usb[3], sendbuff, len, timeout)); /* DMA方式 */
#else
return (USB_WritePort(5, 64, 2, &Ctrl_Usb[3], sendbuff, len, timeout)); /* 非DMA方式 */
#endif
} /* 通过物理端点 5 发送数据 */
/***********************************************************************************************************************
** 函数名称: USB_RW_Param() Name: USB_RW_Param()
** 功能描述: 填写USB接收或发送控制块有关参数 Function: fill the CTRL_USB structure
** 输 入: CTRL_USB *pUsb : USB接收或发送控制块指针 Input: CTRL_USB *pUsb: the CTRL_USB structure
INT8U *buff : 接收或发送缓冲区指针 INT8U *recbuff: the reception/transmittion buffer
** 输 出: 0 调用成功 > 0 调用失败(错误码) Output: 0: sucessfully >0: fail (it is error code)
***********************************************************************************************************************/
INT8U USB_RW_Param(CTRL_USB *pUsb, INT8U *pbuff)
{
if (bEPPflags.bits.configuration == 0)
return USB_ERR_NO_CONFIG; /* USB总线未配置完成 the USB bus is not confirgurated */
if (pbuff == NULL)
return USB_ERR_BUFF_INVALID; /* 缓冲区指针无效错误 the pointer of buff is invalid */
if (pUsb->bEpUsed == 1)
return USB_ERR_ENDP_OCCUPY; /* 端点已被其它任务占用 the endpoint have been used */
pUsb->bEpUsed = 1; /* 本任务占用该端点 the task use the endpoint */
return USB_NO_ERR;
}
/***********************************************************************************************************************
** 函数名称: USB_WaitEpReady() Name: USB_WaitEpReady()
** 功能描述: 等待端点就绪 Function: Waiting for the endpoint is ready
** 输 入: CTRL_USB *pUsb : USB接收或发送控制块指针 Input: CTRL_USB *pUsb: the CTRL_USB structure
INT16U timeout : 超时时间 INT8U *recbuff: timeout
** 输 出: 0 调用成功 > 0 调用失败(错误码) Output: 0: sucessfully >0: fail (it is error code)
***********************************************************************************************************************/
#if (!DMA_ENGINE_EN)
INT8U USB_WaitEpReady(CTRL_USB *pUsb, INT16U timeout)
{
INT8U err;
OS_ENTER_CRITICAL();
if (pUsb->bEpReady == 1)
{ /* 端点已就绪 the endpoint is ready */
pUsb->bEpReady = 0;
OS_EXIT_CRITICAL();
err = USB_NO_ERR; /* 无须等待 need not waiting */
}
else
{
pUsb->bTaskWaiting = 1; /* 置位,表示任务进入等待的状态 set the bit, indicate the task enter the waiting status */
OS_EXIT_CRITICAL();
OSSemPend(pUsb->Ep_Sem, timeout, &err); /* 等待端点就绪(进入等待状态) wait for endpoint is ready */
}
return err;
}
#endif
/*************************************************************************************************************************
** 函数名称: USB_ReadPort() Name: USB_ReadPort()
** 功能描述: 读端点(非DMA方式) Function: read data from endpoint(Not DMA)
** 输 入: INT8U endp: 物理端点索引号 Input: INT8U endp: the physical endpoint number
INT32U eppsize: 端点缓冲区大小 INT32U eppsize: the size of endpoint buffer
INT8U buffnums: 该端点的缓冲区个数 INT8U buffnums: numbers of endpoint buffer
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)
**************************************************************************************************************************/
#if (!DMA_ENGINE_EN)
INT8U USB_ReadPort(INT8U endp, INT32U eppsize, INT8U buffnums, CTRL_USB *pUsb, INT32U len, INT8U *recbuff, INT16U timeout)
{
INT8U err,i;
INT32U reccnt = 0, tmp;
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();
while(1)
{
err = USB_WaitEpReady(pUsb, timeout); /* 等待端点收到数据 waiting for the endpoint receiving data */
/********** 下面从端点缓冲区中读取数据 *********/
if (err == OS_NO_ERR)
{
OS_ENTER_CRITICAL();
for(i = 0; i < buffnums; i++)
{
if ((USB_SelectEndpoint(endp) & 0x60) == 0)
break; /* 缓冲区为空 endpoint buffer is empty */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -