📄 usbci.c
字号:
/****************************************Copyright (c)**************************************************
** Guangzou ZLG-MCU Development Co.,LTD.
** graduate school
** http://www.zlgmcu.com
**
**--------------File Info-------------------------------------------------------------------------------
** File name: USBCI.c
** Last modified Date: 2005-8-6
** Last Version: V1.0
** Descriptions: LPC214x USB 接口命令层
** LPC214x USB Interface command 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 "USBCI.h"
/********************************************************
操作USB设备寄存器 operate USB device register
*********************************************************/
/***************************************************************************************************************
** 函数名称 : USB_ConfigEndpoint() Name : USB_ConfigEndpoint()
** 功能描述 : 配置 USB 设备端点 Function : Configurate the endpoint of the USB
** 输 入 : INT8U endp : 物理端点号 Input : INT8U endp: the physical endpoint number
INT8U packetsize: 该端点的最大包大小 INT8U packetsize: the max packet size of the endpoint INT8U packetsize: the max packet size of the endpoint
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_ConfigMaxPaketSize(INT8U endp, INT32U packetsize)
{
/* or with the existing value of the register */
USBReEp |= (INT32U)(0x01 << endp); /* 与原来存在的寄存器值做或操作 */
/* load endpoint index Reg with physical endpoint no. */
USBEpInd = (INT32U)endp; /* 选择端点 */
/* load the max packet size Register */
USBMaxPSize = (INT32U)packetsize; /* 写入端点最大包长度值 */
/* check whether the EP_RLSED bit is set */
while((USBDevIntSt & EP_RLZEDINT) == 0); /* 等待处理完成 */
/* clear the EP_RLZED bit */
USBDevIntClr = EP_RLZEDINT; /* 清除中断标志位 */
}
/*********************************************************
协议引擎命令 Protocol Engine Command
**********************************************************/
/***************************************************************************************************************
** 函数名称 : USB_GetData() Name : USB_GetData()
** 功能描述 : 从USB命令数据寄存器中读取数据 Function : Read data from USB Command Data Register
** 输 入 : INT32U cmd : 命令 Input : INT32U cmd: command word
** 输 出 : 读到的一个字节 Output : the read byte
****************************************************************************************************************/
INT8U USB_GetData(INT32U cmd)
{
/* command */
USBCmdCode = cmd; /* 写入命令字 */
/* wait for CDFULL = 1 */
while((USBDevIntSt & CDFULL) == 0); /* 等待USBDevIntSt寄存器的CDFULL位置1*/
/* clear the CDFULL bit */
USBDevIntClr = CDFULL; /* 清除CDFULL位 */
/* get the received data */
return USBCmdData; /* 读取数据 */
}
/***************************************************************************************************************
** 函数名称 : USB_SendCmd() Name : USB_SendCmd()
** 功能描述 : 向USB命令代码寄存器写入命令 Function : write a command to Command Code Register
** 输 入 : INT32U cmd : 命令 Input : INT32U cmd : command
INT32U data: 数据 INT32U data: data
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_SendCmd(INT32U cmd, INT32U data)
{
/* command */
USBCmdCode = cmd; /* 写入命令字 */
/* wait for CCEMPTY = 1 */
while((USBDevIntSt & CCEMPTY) == 0); /* 等待 USBDevIntSt 寄存器的 CCEMPTY 位置1 */
/* clear the CCEMPTY bit */
USBDevIntClr = CCEMPTY; /* 清除 CCEMPTY 位*/
if (data != 0) /* 如果还有数据阶段 */
{
/* command */
USBCmdCode = data; /* 将数据编码写入命令代码寄存器 */
/* wait for CCEMPTY = 1 */
while((USBDevIntSt & CCEMPTY) == 0); /* 等待 USBDevIntSt 寄存器的 CCEMPTY 位置1 */
/* clear the CCEMPTY bit */
USBDevIntClr = CCEMPTY; /* 清除 CCEMPTY 位*/
}
}
/****************************************************************
LPC214x USB控制器相关命令 Related Command of LPC214x USB
*****************************************************************/
/***************************************************************************************************************
** 函数名称 : USB_ReadTestRegister() Name : USB_ReadTestRegister()
** 功能描述 : 读测试寄存器 Function : Read the Test Register of USB
** 输 入 : 无 Input : NULL
** 输 出 : 测试寄存器的值 Output : the value of the Test Register
****************************************************************************************************************/
INT16U USB_ReadTestRegister(void)
{
INT16U temp;
USB_SendCmd(USBCMD_RDTEST_REG, 0);
temp = USB_GetData(USBDAT_RDTEST_REG); /* read LSB byte */
temp += (INT16U)USB_GetData(USBDAT_RDTEST_REG) << 8; /* read MSB byte */
return temp;
}
/***************************************************************************************************************
** 函数名称 : USB_SetAddressEnable() Name : USB_SetAddressEnable()
** 功能描述 : 设置 USB 设备地址 Function : set the USB device address
** 输 入 : INT8U bAddress: 主机分配的地址值 Input : INT8U bAddress: the address value
INT8U bEnable: 1 - 使能USB设备 INT8U bEnable: 1: enable the USB device
0 - 禁止USB设备 0: disable the device
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_SetAddressEnable(INT8U bAddress, INT8U bEnable)
{
if (bEnable)
bAddress |= 0x80;
USB_SendCmd(USBCMD_SET_ADDRESS, ((INT32U)(bAddress << 16)) | USBDAT_SET_ADDRESS);
}
/***************************************************************************************************************
** 函数名称 : USB_SetEndpointEnable() Name : USB_SetEndpointEnable()
** 功能描述 : 设置 USB 设备 Function : Configure USB Device
** 输 入 : INT8U bEnble: 1 - 器件已配置 Input : INT8U bEnble: 1 - have finished configuring
0 - 器件未配置 0 - have not configured
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_SetEndpointEnable(INT8U bEnble)
{
USB_SendCmd(USBCMD_CONFIG_DEV, ((INT32U)(bEnble << 16)) | USBDAT_CONFIG_DEV);
}
/***************************************************************************************************************
** 函数名称 : USB_SetMode() Name : USB_SetMode()
** 功能描述 : 设置模式 Function : Set Mode
** 输 入 : INT8U value: 模式值 Input : INT8U value: the mode value
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_SetMode(INT8U value)
{
USB_SendCmd(USBCMD_SET_MODE, ((INT32U)(value << 16)) | USBDAT_SET_MODE);
}
/***************************************************************************************************************
** 函数名称 : USB_SetDevStatus() Name : USB_SetDevStatus()
** 功能描述 : 设置设备状态 Function : Set Device Status
** 输 入 : INT8U value: 状态值 Input : INT8U value: the Device Status
** 输 出 : 无 Output : NULL
****************************************************************************************************************/
void USB_SetDevStatus(INT8U value)
{
USB_SendCmd(USBCMD_SET_DEVSTATUS, ((INT32U)(value << 16)) | USBDAT_SET_DEVSTATUS);
}
/***************************************************************************************************************
** 函数名称 : USB_GetDevStatus() Name : USB_GetDevStatus()
** 功能描述 : 获取设备状态 Function : Get Device Status
** 输 入 : NULL Input : NULL
** 输 出 : USB 设备状态字节 Output : the USB device status byte
****************************************************************************************************************/
INT8U USB_GetDevStatus(void)
{
USB_SendCmd(USBCMD_GET_DEVSTATUS, 0);
return USB_GetData(USBDAT_GET_DEVSTATUS);
}
/***************************************************************************************************************
** 函数名称 : USB_SelectEndpoint() Name : USB_SelectEndpoint()
** 功能描述 : 选择端点,并获取端点信息 Function : select the endpoint, and get the endpoint information
** 输 入 : INT8U endp: 物理端点号 Input : INT8U endp: the physical endpoint number
** 输 出 : 对应端点的信息 Output : the endpoint information
****************************************************************************************************************/
INT8U USB_SelectEndpoint(INT8U endp)
{
INT32U tmp = (INT32U)(endp << 16);
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -