以下只用了端点1进行控制传输,端点2的数据传输自己添加,没有使用DMA功能,为简单的固件程序
/**************************************************************************
PHILIPS PDIUSBD12 FIRMWARE
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__
#i nclude
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 for(j=0;j<120;j++);
}
//**************************************************************************
#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 *(buf+i)=Inportb(D12_DATA);
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 *(buf+i)=Inportb(D12_DATA);
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_DATA,*(buf+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_DATA,*(buf+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();
}
//************************