此函數(shù)庫可以直接使用PHILIPS的Demo驅(qū)動D12TEST
以下只用了端點1進行控制傳輸,端點2的數(shù)據(jù)傳輸自己添加,沒有使用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_ENDP0OUT0x0001//中斷寄存器位定義
#define D12_INT_ENDP0IN0x0002
#define D12_INT_ENDP1OUT0x0004
#define D12_INT_ENDP1IN0x0008
#define D12_INT_ENDP2OUT0x0010
#define D12_INT_ENDP2IN0x0020
#define D12_INT_BUSRESET0x0040
#define D12_INT_SUSPENDCHANGE0x0080
#define D12_INT_EOT0x0100
#define D12_SETUPPACKET 0x20//讀最后處理狀態(tài)寄存器的設(shè)置信息包0010,0000b
#define EP0_PACKET_SIZE 16//p0最大16byte
#define USB_ENDPOINT_DIRECTION_MASK 0x80//設(shè)備請求類型,傳輸方向 D7 1000,0000b
#define USB_REQUEST_TYPE_MASK 0x30//bmRequest的設(shè)置
#define USB_REQUEST_MASK 0x0f
#define USB_STANDARD_REQUEST 0x00//5,6位的定義
#define USB_VENDOR_REQUEST 0x20
#define USB_DEVICE_DESCRIPTOR_TYPE 0x01//描述符類型 設(shè)備描述符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)//取數(shù)據(jù)高8位
#define LSB(x) ((x) & 0x00FF)//取數(shù)據(jù)低8位
typedef union _Event_Flags_//定義USB事件標志數(shù)據(jù)類型
{
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;//控制端點處理狀態(tài)
//0:IDEL 空閑狀態(tài)
//1:TRANSMIT 數(shù)據(jù)發(fā)送狀態(tài)
//2:RECEIVE 數(shù)據(jù)接受狀態(tài)
unsigned char Configuration: 1;//配置標志 (0:未配置;1:已配置)
unsigned char Port1RxDone: 1;//端口1收到數(shù)據(jù)標志
unsigned char Port2RxDone: 1; //端口2收到數(shù)據(jù)標志
unsigned char Port1TxFull : 1;//端口1輸出緩沖區(qū)滿標志
unsigned char Port2TxFull: 1; //端口2輸出緩沖區(qū)滿標志
unsigned char Reserve: 3;//保留,未使用
}Bits;
unsigned short int Value;
}EVENT_FLAGS;//事件標志數(shù)據(jù)類型
typedef struct _DEVICE_REQUEST_
{
unsigned char bmRequestType;//請求類型
unsigned char bRequest;//USB請求
unsigned short wValue;//USB請求值
unsigned short wIndex;//USB請求索引
unsigned short wLength;//記數(shù)長度
}DEVICE_REQUEST;
#define MAX_CONTROLDATA_SIZE8
typedef struct _control_xfer
{
DEVICE_REQUEST DeviceRequest;//USB請求結(jié)構(gòu)體
unsigned short wLength;//傳輸數(shù)據(jù)的總字節(jié)數(shù)
unsigned short wCount;//傳輸字節(jié)數(shù)統(tǒng)計
unsigned char * pData;//傳輸數(shù)據(jù)指針
unsigned char dataBuffer[MAX_CONTROLDATA_SIZE];//請求的數(shù)據(jù)
}CONTROL_XFER;
static EVENT_FLAGS EventFlags;//定義為全局變量,用于與主程序的通信
static CONTROL_XFER ControlData;//保存SETUP包請求類型和請求數(shù)據(jù)
unsigned char idata EndPoint1Buffer[4];//控制端點緩存
unsigned char idata EndPoint2Buffer[64];//主端點緩存
//**************************************************************************
//硬件提取層,多路地址/數(shù)據(jù)總線方式讀寫
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命令接口函數(shù)
void D12_SetMode(unsigned char bConfig,unsigned char bClkDiv)
{
Outportb(D12_COMMAND,0xF3);
Outportb(D12_DATA,bConfig);
Outportb(D12_DATA,bClkDiv);
}
//設(shè)置端點
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);
}
//設(shè)置地址使能
void D12_SetAddressEnable(unsigned char bAddress,unsigned char bEnable)
{
Outportb(D12_COMMAND,0xd0);
if(bEnable) bAddress |= 0x80;
Outportb(D12_DATA,bAddress);
}
//設(shè)置端點使能
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;
}
//讀取端點狀態(tài)
unsigned char D12_ReadEndpointStatus(unsigned char EndPoint)
{
unsigned char BackValue;
if(EventFlags.Bits.InISR == 0)
EA = 0;
Outportb(D12_COMMAND, 0x80 + EndPoint);//讀取端點狀態(tài)
BackValue = Inportb(D12_DATA);
if(EventFlags.Bits.InISR == 0)
EA = 1;
return BackValue;
}
//讀端點最后處理狀態(tài)
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_