此函数库可以直接使用PHILIPS的Demo驱动D12TEST
以下只用了端点1进行控制传输,端点2的数据传输自己添加,没有使用DMA功能,为简单的固件程序
/**************************************************************************
PHILIPS PDIUSBD12 FIRMWAR
COPYRIGHT (c) 2005 BY JJJ.
-- ALL RIGHTS RESERVED --
File Name: D12_USB.h
Author: Jiang Jian Jun
Created: 2005/4/3
Modified: NO
Revision: 1.0
***************************************************************************/
#ifndef __D12_USB_H_REVISION_FIRST__
#define __D12_USB_H_REVISION_FIRST__
#include
sbit SUSPEND = P3^5;
#define D12_INT_ENDP0OUT 0x0001 //中断寄存器位定义
#define D12_INT_ENDP0IN 0x0002
#define D12_INT_ENDP1OUT 0x0004
#define D12_INT_ENDP1IN 0x0008
#define D12_INT_ENDP2OUT 0x0010
#define D12_INT_ENDP2IN 0x0020
#define D12_INT_BUSRESET 0x0040
#define D12_INT_SUSPENDCHANGE 0x0080
#define D12_INT_EOT 0x0100
#define D12_SETUPPACKET 0x20 //读最后处理状态寄存器的设置信息包0010,0000b
#define EP0_PACKET_SIZE 16 //p0最大16byte
#define USB_ENDPOINT_DIRECTION_MASK 0x80 //设备请求类型,传输方向 D7 1000,0000b
#define USB_REQUEST_TYPE_MASK 0x30 //bmRequest的设置
#define USB_REQUEST_MASK 0x0f
#define USB_STANDARD_REQUEST 0x00 //5,6位的定义
#define USB_VENDOR_REQUEST 0x20
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01 //描述符类型 设备描述符01h,配置描述符02,接口描述符04,端点描述符05
#define USB_CONFIGURATION_DESCRIPTOR_TYPE 0x02
#define CONFIG_DESCRIPTOR_LENGTH 0x002E //配置描述符总长度
//**************************************************************************
//Port And Macros And Structure And Union Definitions
#define SWAP(x) ((((x) & 0x00FF) << 8) | (((x) >> 8) & 0x00FF)) //交换高低8位
#define MSB(x) (((x) >> 8) & 0x00FF) //取数据高8位
#define LSB(x) ((x) & 0x00FF) //取数据低8位
typedef union _Event_Flags_ //定义USB事件标志数据类型
{
struct _Bit_Flags_
{
unsigned char Timer : 1; //定时器益出事件标记
unsigned char BusReset : 1; //USB总线复位标志
unsigned char Suspend : 1; //USB器件挂起标志
unsigned char SetupPacket : 1; //收到SETUP包标志
unsigned char RemoteWakeup : 1; //远程唤醒标志
unsigned char InISR : 1; //USB中断服务标志
unsigned char ControlState : 2; //控制端点处理状态
//0:IDEL 空闲状态
//1:TRANSMIT 数据发送状态
//2:RECEIVE 数据接受状态
unsigned char Configuration : 1; //配置标志 (0:未配置;1:已配置)
unsigned char Port1RxDone : 1; //端口1收到数据标志
unsigned char Port2RxDone : 1; //端口2收到数据标志
unsigned char Port1TxFull : 1; //端口1输出缓冲区满标志
unsigned char Port2TxFull : 1; //端口2输出缓冲区满标志
unsigned char Reserve : 3; //保留,未使用
}Bits;
unsigned short int Value;
}EVENT_FLAGS; //事件标志数据类型
typedef struct _DEVICE_REQUEST_
{
unsigned char bmRequestType; //请求类型
unsigned char bRequest; //USB请求
unsigned short wValue; //USB请求值
unsigned short wIndex; //USB请求索引
unsigned short wLength; //记数长度
}DEVICE_REQUEST;
#define MAX_CONTROLDATA_SIZE 8
typedef struct _control_xfer
{
DEVICE_REQUEST DeviceRequest; //USB请求结构体
unsigned short wLength; //传输数据的总字节数
unsigned short wCount; //传输字节数统计
unsigned char * pData; //传输数据指针
unsigned char dataBuffer[MAX_CONTROLDATA_SIZE]; //请求的数据
}CONTROL_XFER;
static EVENT_FLAGS EventFlags; //定义为全局变量,用于与主程序的通信
static CONTROL_XFER ControlData; //保存SETUP包请求类型和请求数据
unsigned char idata EndPoint1Buffer[4]; //控制端点缓存
unsigned char idata EndPoint2Buffer[64];//主端点缓存
//**************************************************************************
//硬件提取层,多路地址/数据总线方式读写
void Outportb(unsigned int Addr, unsigned char Data)
{
*((unsigned char xdata *) Addr) = Data;
}
unsigned char Inportb(unsigned int Addr)
{
return *((unsigned char xdata *) Addr);
}
void USB_Delay1ms(unsigned int count)
{
unsigned int i,j;
for(i=0;i
}
//**************************************************************************
#define D12_DATA 0
#define D12_COMMAND 1
//PDIUSBD12命令接口函数
void D12_SetMode(unsigned char bConfig,unsigned char bClkDiv)
{
Outportb(D12_COMMAND,0xF3);
Outportb(D12_DATA,bConfig);
Outportb(D12_DATA,bClkDiv);
}
//设置端点
void D12_SetEndpointStatus(unsigned char bEndp,unsigned char bStalLED)
{
Outportb(D12_COMMAND,0x40+bEndp);
Outportb(D12_DATA,bStalled);
}
//应答!!!!!
void D12_AcknowledgeEndpoint(unsigned char endp)
{
Outportb(D12_COMMAND,endp);
Outportb(D12_COMMAND,0xF1);
if(endp==0)
Outportb(D12_COMMAND,0xF2);
}
//设置地址使能
void D12_SetAddressEnable(unsigned char bAddress,unsigned char bEnable)
{
Outportb(D12_COMMAND,0xd0);
if(bEnable) bAddress |= 0x80;
Outportb(D12_DATA,bAddress);
}
//设置端点使能
void D12_SetEndpointEnable(unsigned char bEnable)
{
Outportb(D12_COMMAND,0xD8);
if(bEnable)
Outportb(D12_DATA,1);
else
Outportb(D12_DATA,0);
}
//读中断寄存器
unsigned short D12_ReadInterruptRegister(void)
{
unsigned char b1;
unsigned int j;
Outportb(D12_COMMAND,0xF4);
b1=Inportb(D12_DATA);
j=Inportb(D12_DATA);
j<<=8;
j+=b1;
return j;
}
//读取端点状态
unsigned char D12_ReadEndpointStatus(unsigned char EndPoint)
{
unsigned char BackValue;
if(EventFlags.Bits.InISR == 0)
EA = 0;
Outportb(D12_COMMAND, 0x80 + EndPoint);//读取端点状态
BackValue = Inportb(D12_DATA);
if(EventFlags.Bits.InISR == 0)
EA = 1;
return BackValue;
}
//读端点最后处理状态
unsigned char D12_ReadLastTransactionStatus(unsigned char bEndp)
{
Outportb(D12_COMMAND,0x40+bEndp);
return Inportb(D12_DATA);
}
//读端口
unsigned char D12_ReadEndpoint(unsigned char endp,unsigned char len,unsigned char *buf)
{
unsigned char i,j;
Outportb(D12_COMMAND,endp);
if((Inportb(D12_DATA)&0xff)==0)//" define D12_FULLEMPTY as 0xFF by newer
return 0;
Outportb(D12_COMMAND,0x80+endp);
i=Inportb(D12_DATA);
i=i&0x60;
Outportb(D12_COMMAND,0xF0);
j=Inportb(D12_DATA);
j=Inportb(D12_DATA);
if(j>len)
j=len;
for(i=0;i
Outportb(D12_COMMAND,0xF2);
return j;
}
unsigned char D12_ReadEndpoint_Int(unsigned char endp,unsigned char len,unsigned char *buf)
{
unsigned char i,j;
Outportb(D12_COMMAND,endp);
if((Inportb(D12_DATA)&0xff)==0)//" define D12_FULLEMPTY as 0xFF by newer
return 0;
Outportb(D12_COMMAND,0x80+endp);
i=Inportb(D12_DATA);
i=i&0x60;
Outportb(D12_COMMAND,0xF0);
j=Inportb(D12_DATA);
j=Inportb(D12_DATA);
if(j>len)
j=len;
for(i=0;i
Outportb(D12_COMMAND,0xF2);
return j;
}
unsigned char D12_WriteEndpoint(unsigned char endp,unsigned char len,unsigned char * buf)
{
unsigned char i;
Outportb(D12_COMMAND,endp);
Inportb(D12_DATA);
Outportb(D12_COMMAND,0xF0);
Outportb(D12_DATA,0);
Outportb(D12_DATA,len);
for(i=0;i
Outportb(D12_COMMAND,0xFA);
return len;
}
unsigned char D12_WriteEndpoint_Int(unsigned char endp,unsigned char len,unsigned char * buf)
{
unsigned char i;
Outportb(D12_COMMAND,endp);
Inportb(D12_DATA);
Outportb(D12_COMMAND,0xF0);
Outportb(D12_DATA,0);
Outportb(D12_DATA,len);
for(i=0;i
Outportb(D12_COMMAND,0xFA);
return len;
}
void DisconnectUSB(void)
{
D12_SetMode(0x02,0x03);//SET TO ONE? by newer
}
void InitialUSBInt(void);
void ConnectUSB(void)
{
EventFlags.Value = 0x0000;
InitialUSBInt();
D12_SetMode(0x16,0x03);
}
void ReconnectUSB(void)
{
SUSPEND = 0;
DisconnectUSB();
USB_Delay1ms(1000);
ConnectUSB();
}
//**************************************************************************
//中断服务程序
void InitialUSBInt(void)
{
IT1=0; //低电平中断触发
EX1=1; //允许外部中断
PX1=0; //优先级低
EA =1;
}
void EP0_Out(void)
{
unsigned char ep_last,i;
ep_last=D12_ReadLastTransactionStatus(0);//interrupt symbol
if(ep_last&D12_SETUPPACKET)
{ //recieved SETUP packet ---by newer
ControlData.wLength=0;
ControlData.wCount=0;
if(D12_ReadEndpoint_Int(0,sizeof(ControlData.DeviceRequest),(unsigned char *)(&(ControlData.DeviceRequest)))!=sizeof(DEVICE_REQUEST))
{
D12_SetEndpointStatus(0,1);
D12_SetEndpointStatus(1,1);
EventFlags.Bits.ControlState=0; //should define USB_IDLE first --by newer
return;
}
ControlData.DeviceRequest.wValue=SWAP(ControlData.DeviceRequest.wValue);
ControlData.DeviceRequest.wIndex=SWAP(ControlData.DeviceRequest.wIndex);
ControlData.DeviceRequest.wLength=SWAP(ControlData.DeviceRequest.wLength);
D12_AcknowledgeEndpoint(0);
D12_AcknowledgeEndpoint(1);
ControlData.wLength=ControlData.DeviceRequest.wLength;
ControlData.wCount=0;
if(ControlData.DeviceRequest.bmRequestType&(unsigned char)USB_ENDPOINT_DIRECTION_MASK)
{
EventFlags.Bits.SetupPacket=1; //recv from host? --by newer
EventFlags.Bits.ControlState=1; //by newer
}
else
{
if(ControlData.DeviceRequest.wLength==0)
{
EventFlags.Bits.SetupPacket=1;
EventFlags.Bits.ControlState=0; //by newer
}
else
{
if(ControlData.DeviceRequest.wLength>16)//最大传16byte
{
EventFlags.Bits.ControlState=0; //by newer
D12_SetEndpointStatus(0,1);
D12_SetEndpointStatus(1,1);
}
else
{
EventFlags.Bits.ControlState=2;//by newer
}
}
}
}
else if(EventFlags.Bits.ControlState==2)
{
i=D12_ReadEndpoint_Int(0,EP0_PACKET_SIZE,ControlData.dataBuffer+ControlData.wCount);
ControlData.wCount+=i;
if(i!=EP0_PACKET_SIZE||ControlData.wCount>=ControlData.wLength)
{
EventFlags.Bits.SetupPacket=1;
EventFlags.Bits.ControlState=0;
}
}
else
EventFlags.Bits.ControlState=0;
}
void EP0_In(void)
{
short i=ControlData.wLength-ControlData.wCount;
D12_ReadLastTransactionStatus(1);
if(EventFlags.Bits.ControlState!=1) return;
if(i>=EP0_PACKET_SIZE)
{
D12_WriteEndpoint_Int(1,EP0_PACKET_SIZE,ControlData.pData+ControlData.wCount);
ControlData.wCount+=EP0_PACKET_SIZE;
EventFlags.Bits.ControlState=1;
return;
}
if(i!=0)
{
D12_WriteEndpoint_Int(1,i,ControlData.pData+ControlData.wCount);
ControlData.wCount+=i;
EventFlags.Bits.ControlState=0;
return;
}
D12_WriteEndpoint_Int(1,0,0);
EventFlags.Bits.ControlState=0;
}
void EP1_Out(void)
{
unsigned char Length;
D12_ReadLastTransactionStatus(2); /* Clear interrupt flag */
Length = D12_ReadEndpoint_Int(2, sizeof(EndPoint1Buffer),EndPoint1Buffer);
if(Length != 0)
EventFlags.Bits.Port1RxDone = 1;
}
void EP1_In(void)
{
D12_ReadLastTransactionStatus(3);
}
void EP2_Out(void)
{
unsigned char Length,EP2Status;
D12_ReadLastTransactionStatus(4); /* Clear interrupt flag */
EP2Status = D12_ReadEndpointStatus(4);
EP2Status&=0x60;
Length = D12_ReadEndpoint(4,sizeof(EndPoint2Buffer),EndPoint2Buffer);
if(EP2Status==0x60)
Length = D12_ReadEndpoint(4,sizeof(EndPoint2Buffer),EndPoint2Buffer);
if(Length != 0)
EventFlags.Bits.Port2RxDone = 1;
}
void EP2_In(void)
{
D12_ReadLastTransactionStatus(5); /* Clear interrupt flag */
}
//**************************************************************************
//请求处理
typedef struct _usb_device_descriptor
{
unsigned char bLength;
unsigned char bDescriptorType;
unsigned int bcdUSB;
unsigned char bDeviceClass;
unsigned char bDeviceSubClass;
unsigned char bDeviceProtocol;
unsigned char bMaxPacketSize0;
unsigned int idVendor;
unsigned int idProduct;
unsigned int bcdDevice;
unsigned char iManufacturer;
unsigned char iProduct;
unsigned char iSerialNumber;
unsigned char bNumConfiguations;
}USB_DEVICE_DESCRIPTOR;
code USB_DEVICE_DESCRIPTOR DeviceDescr=
{
sizeof(USB_DEVICE_DESCRIPTOR),
0x01,//USB_DEVICE_DESCRIPTOR_TYPE,
SWAP(0x0100),
0xDC,//USB_CLASS_CODE_TEST_CLASS_DEVICE,
0, 0,
EP0_PACKET_SIZE,
SWAP(0x0471),
SWAP(0x0666),
SWAP(0x0100),
0, 0, 0,
25
};
//配置描述符
typedef struct _usb_configuration_descriptor
{
unsigned char bLength[0x2e];
}USB_CONFIGURATION_DESCRIPTOR;
code USB_CONFIGURATION_DESCRIPTOR ConfigDescr=
{
0x09,0x02,0x2e,0x00,0x01,0x01,0x00,0xa0,0x32,
0x09,0x04,0x00,0x00,0x04,0xdc,0xa0,0xb0,0x00,
0x07,0x05,0x81,0x03,0x02,0x00,0x0a,
0x07,0x05,0x01,0x03,0x02,0x00,0x0a,
0x07,0x05,0x82,0x02,0x40,0x00,0x0a,
0x07,0x05,0x02,0x02,0x40,0x00,0x0a
};
//code_tramsit
void code_transmit(unsigned char code *pRomData,unsigned short len)
{
ControlData.wCount=0;
if(ControlData.wLength>len)
ControlData.wLength=len;
ControlData.pData=pRomData;
if(ControlData.wLength>=EP0_PACKET_SIZE)
{
D12_WriteEndpoint(1,EP0_PACKET_SIZE,ControlData.pData);
ControlData.wCount+=EP0_PACKET_SIZE;
EA = 0;
EventFlags.Bits.ControlState=1;
EA = 1;
}
else
{
D12_WriteEndpoint(1,ControlData.wLength,pRomData);
ControlData.wCount+=ControlData.wLength;
EA = 0;
EventFlags.Bits.ControlState=0;
EA = 1;
}
}
//获取描述符
void get_descriptor(void)
{
if(MSB(ControlData.DeviceRequest.wValue)==USB_DEVICE_DESCRIPTOR_TYPE)
{
code_transmit((unsigned char code*)&DeviceDescr,sizeof(USB_DEVICE_DESCRIPTOR));
return;
}
if(MSB(ControlData.DeviceRequest.wValue)==USB_CONFIGURATION_DESCRIPTOR_TYPE)
{
if(ControlData.DeviceRequest.wLength>CONFIG_DESCRIPTOR_LENGTH)
{
ControlData.DeviceRequest.wLength=CONFIG_DESCRIPTOR_LENGTH;
//标识符总大小2E byte,第二次请求时wlength=0x00ff
}
//这里的ConfigDescr其实应该包括其他标识符!
code_transmit((unsigned char code*)&ConfigDescr,ControlData.DeviceRequest.wLength);
return;
}
}
//single transmit
void single_transmit(unsigned char *buf,unsigned char len)
{
if(len<=EP0_PACKET_SIZE)
{
D12_WriteEndpoint(1,len,buf);
}
}
//设置地址
void set_address(void)
{
D12_SetAddressEnable((unsigned char)(ControlData.DeviceRequest.wValue&0xff),1);
//比如wValue是"02 00" 应该得到02
single_transmit(0,0);
}
//设置配置
void set_configuration(void)
{
if(ControlData.DeviceRequest.wValue==0)
{
single_transmit(0,0);
EventFlags.Bits.Configuration=0;
D12_SetEndpointEnable(0);
}
else if(ControlData.DeviceRequest.wValue==1)
{
single_transmit(0,0);
D12_SetEndpointEnable(0);
D12_SetEndpointEnable(1);
EventFlags.Bits.Configuration=1;
}
}
//读取配置
void get_configuration(void)
{
unsigned char c=EventFlags.Bits.Configuration;
single_transmit(&c,1);
}
//读取设备接口信息
void get_interface(void)
{
unsigned char txdat=0;
single_transmit(&txdat,1);
}
static code void (*StandardDeviceRequest[])(void)=
{
0,0,0,0,
0,set_address,get_descriptor,0,
get_configuration,set_configuration,get_interface,0,
0,0,0,0
};
static code void (*VendorDeviceRequest[])(void)=
{
0,0,0,0,
0,0,0,0,
0,0,0,0,
0,0,0,0
};
void ControlHandler(void)
{
unsigned char type,req;
type=ControlData.DeviceRequest.bmRequestType&USB_REQUEST_TYPE_MASK;//0011,0000b
req=ControlData.DeviceRequest.bRequest&USB_REQUEST_MASK;//0000,1111b
if(type==USB_STANDARD_REQUEST)
(*StandardDeviceRequest[req])();
else if(type==USB_VENDOR_REQUEST)
(*VendorDeviceRequest[req])();
}
void USB_ISR(void) interrupt 2
{
unsigned int i_st;
EA = 0;
EventFlags.Bits.InISR=1;
i_st=D12_ReadInterruptRegister();
if(i_st!=0)
{
if(i_st&D12_INT_ENDP0OUT)
EP0_Out();
if(i_st&D12_INT_ENDP0IN)
EP0_In();
if(i_st&D12_INT_ENDP1OUT)
EP1_Out();
if(i_st&D12_INT_ENDP1IN)
EP1_In();
if(i_st&D12_INT_ENDP2OUT)
EP2_Out();
if(i_st&D12_INT_ENDP2IN)
EP2_In();
}
EventFlags.Bits.InISR=0;
EA = 1;
}
//**************************************************************************
#endif
//简单主程序文件,自己按需要改写
#include
#include "D12_USB.h"
extern EVENT_FLAGS EventFlags; //事件信号
extern unsigned char idata EndPoint1Buffer[4];
main()
{
ReconnectUSB();
while(1)
{
if(EventFlags.Bits.SetupPacket)
{
EA = 0;
EventFlags.Bits.SetupPacket = 0;
ControlHandler();
EA = 1;
}
if(EventFlags.Bits.Timer)
{
EventFlags.Bits.Timer = 0;
}
if(EventFlags.Bits.Port1RxDone)
{
EventFlags.Bits.Port1RxDone = 0;
}
}
}
上一篇:用AT89C2051设计的智能煤气表的思路与原理
下一篇:89S51看门狗功能的使用方法如下
推荐阅读最新更新时间:2024-03-16 15:53
设计资源 培训 开发板 精华推荐
- 睿瀚医疗万斌:“脑机接口+AI+机器人”是康复赛道的未来
- 希润医疗孟铭强:手功能软体机器人,让脑卒中患者重获新生
- 柔灵科技陈涵:将小型、柔性的脑机接口睡眠设备,做到千家万户
- 微灵医疗李骁健:脑机接口技术正在开启意识与AI融合的新纪元
- USB Type-C® 和 USB Power Delivery:专为扩展功率范围和电池供电型系统而设计
- 景昱医疗耿东:脑机接口DBS治疗技术已实现国产替代
- 首都医科大学王长明:针对癫痫的数字疗法已进入使用阶段
- 非常见问题解答第223期:如何在没有软启动方程的情况下测量和确定软启动时序?
- 兆易创新GD25/55全系列车规级SPI NOR Flash荣获ISO 26262 ASIL D功能安全认证证书
- 新型IsoVu™ 隔离电流探头:为电流测量带来全新维度