📄 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: LPC23xx USB 应用层
** LPC23xx 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 "string.h"
#include "usbdevconfig.h"
/* define USB event flag variable */
volatile 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 LPC23xx USB each endpoint */
CTRL_USB Ctrl_Usb[NUM_ENDPOINTS]; /* LPC23xx USB 各端点对应的 USB 接收或发送控制块 */
INT8U __GucDevTaskSetUpPrio = 0;
/***********************************************************************************************************************
** 函数名称 : usbDevInitialize() Name : usbDevInitialize()
** 功能描述 : 初始化 USB 设备控制器 Function : Initialize the USB device controller
** 输 入 : 无 Input : ucDevTaskSetUpPrio : TaskUp任务优先级
** 输 出 : 0: 初始化成功 >0: 初始化失败(错误码) Output : 0: Initialize sucessfully >0: Initialize fail(error code)
***********************************************************************************************************************/
INT8U usbDevInitialize (INT8U ucDevTaskSetUpPrio)
{
int i;
INT8U ucErr;
OS_ENTER_CRITICAL();
USB_InitHardware(); /* 初始化硬件 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 = OSMboxCreate(NULL); /* SETUP包处理任务消息邮箱 */
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 */
}
}
ucErr = OSTaskCreate(usbDevTaskSetup, (void *)0, &TaskSetupStk[127], ucDevTaskSetUpPrio);
if (ucErr != OS_NO_ERR) {
__GucDevTaskSetUpPrio = 0;
OS_EXIT_CRITICAL();
return USB_ERR_CREATETASK_FAIL;
}
USB_Reconnect();
reconnect_USB(); /* 重新连接USB reconnect the USB */
OS_EXIT_CRITICAL();
__GucDevTaskSetUpPrio = ucDevTaskSetUpPrio; /* 记录下该任务的优先级 */
return USB_NO_ERR; /* 初始化USB成功 Initialize USB sucessfully */
}
/***********************************************************************************************************************
** 函数名称 : usbDevDeInit()
** 功能描述 : 卸载 USB 设备控制器
** 输 入 : 无
** 输 出 : TRUE:成功, FALSE:失败
***********************************************************************************************************************/
INT8U usbDevDeInit (void)
{
OS_TCB *ptcb = NULL;
INT8U ucPrio;
INT8U i, ucErr;
USBDEBUG_SENDSTR("\r\nusbDevDeInit...\r\n");
OS_ENTER_CRITICAL();
ucPrio = __GucDevTaskSetUpPrio;
OS_EXIT_CRITICAL();
i = 0;
if (ucPrio) {
ptcb = OSTCBPrioTbl[ucPrio]; /* 查询枚举任务是否已经存在 */
if (ptcb != NULL) {
OSMboxPost(pSetup_Event, (void *)__USB_DEV_SETUP_DELTASK); /* 调度任务已存在,申请删除此任务*/
while (OSTCBPrioTbl[ucPrio] != NULL) { /* 等待任务删除完毕 */
OSTimeDly(1);
if (i++ > 200) {
i = 0;
USBDEBUG_SENDSTR("\r\nwait delete usbDevTaskSetup task complete\r\n");
}
}
__GucDevTaskSetUpPrio = 0;
}
}
pSetup_Event = OSMboxDel(pSetup_Event, OS_DEL_ALWAYS, &ucErr);
for(i = 0; i < NUM_ENDPOINTS; i++) {
Ctrl_Usb[i].Ep_Sem = OSSemDel(Ctrl_Usb[i].Ep_Sem, OS_DEL_ALWAYS, &ucErr);
}
return TRUE;
}
/***********************************************************************************************************************
** 函数名称: void usbDevTaskSetup(void *pdata) Name: void usbDevTaskSetup(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 usbDevTaskSetup(void *pdata)
{
#if OS_CRITICAL_METHOD == 3 /* Allocate storage for CPU status register */
OS_CPU_SR cpu_sr;
#endif
INT8U err;
INT32U uiMsgCode;
pdata = pdata; /* Prevent compiler warning */
for (;;)
{
uiMsgCode = (INT32U)OSMboxPend(pSetup_Event, 0, &err); /* 等待Setup包 wait for SETUP packet */
if (err == OS_NO_ERR){ /* 接收到Setup包 have received SETUP packet */
if (uiMsgCode == __USB_DEV_SETUP_NORMAL) {
OS_ENTER_CRITICAL(); /* 关中断 disable all interrupt */
control_handler(); /* 进行控制传输处理 deal with control transfer */
OS_EXIT_CRITICAL(); /* 开中断 enable all interrupt */
} else if (uiMsgCode == __USB_DEV_SETUP_DELTASK) {
USBDEBUG_SENDSTR("\r\nusbDevTaskSetup task delete!");
OSTaskDel(OS_PRIO_SELF);
}
}
}
}
/*******************************************************************************************************************************
** 函数名称: 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)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -