USB通用函数API接口代码文档格式.docx
《USB通用函数API接口代码文档格式.docx》由会员分享,可在线阅读,更多相关《USB通用函数API接口代码文档格式.docx(21页珍藏版)》请在冰豆网上搜索。
returnFIFO_FLAG;
}
BYTEFIFO_WR_CHECK(BYTEnum)
//后4位表示Ready位,前四位表示Len0位的状态是否有被设置
MISC|=0X02;
voidRead_FIFO(BYTE*Fifo_Addr,BYTEFifo_Size,BYTE*buffer)
inti=0;
MISC|=0x01;
for(i=0;
i<
Fifo_Size;
i++)
{
buffer[i]=Fifo_Addr[0];
//从FIFO地址取值
0x40==0)break;
//NotReady
}
MISC^=0X02;
//改变TX位状态
=0XFE;
//ClearRequest
voidWrite_FIFO(BYTE*Fifo_Addr,BYTESendLenth,BYTE*buffer)
SendLenth;
Fifo_Addr[0]=buffer[i];
//将要发送的数据放至相应的Fifo中
BOOLCheck_Real_Cmd()
BYTEbFlag_Real_Cmd=FALSE;
0X20!
=0)bFlag_Real_Cmd=TRUE;
0X80!
returnbFlag_Real_Cmd;
voidSend_hand()
BOOLIsRealCmd,Status_Flag;
while
(1)
IsRealCmd=Check_Real_Cmd();
if(IsRealCmd==TRUE)
return;
else
Status_Flag=FIFO_WR_CHECK(0);
if(Status_Flag&
0x0f!
=0)//Fifo_Ready
break;
else
continue;
//SetRequset
voidcontrol_read(WORD*rom_dataptr,BYTE*buffer)//从中读取数据,
{//rom_dataptr是全局的在其他函数中赋值改变
BYTEtemp,i=0;
if(buffer[7]!
=0)//FIFO_WLENGTHH
if(buffer[6]==0)//FIFO_WLENGTHL
if(buffer[6]>
data_start[0])//请求的数据超出ROM数据
data_count=buffer[6];
0x40!
=0)//是控制指令
if(data_count==0)
Send_Hand_Shake();
}
//开始真正的control_read
if(bFlag_RD_HTable==0)//ReadLowByte
bFlag_RD_HTable=1;
buffer[i]=(BYTE)*rom_dataptr;
//取其低位
i++;
data_count--;
//全局变量,要发送的总长度
if(data_count<
=1)
if(i+1==8)//FIFO_size=8
else//ReadHightByte
bFlag_RD_HTable=0;
buffer[i]=(BYTE)(*rom_dataptr)>
>
8;
//取其高位
rom_dataptr++;
//将ROM指针前移
if(buffer[i]==0x3f)
if(i+1==8)
}//填充完毕,结束循环,开始向FIFO发送数据
if(Check_Real_Cmd()==TRUE)return;
if(FIFO_WR_CHECK(0)&
0xff==0)continue;
Write_FIFO(Fifo_0_Addr,0x02,buffer);
//SetAddress():
重新设定Endpoint地址函数
voidSetAddress(BYTE*buffer)
BYTEDev_Addr;
Dev_Addr=buffer[2]//FIFO_OUT3,FIFO_WVALUEL
SIES|=0x01;
//主机从设备读操作后将更新在AWR中的地址,否则为0则立即更新
Dev_Addr&
USB_AWR=Dev_Addr;
//SetConfiguration():
设置配置函数
voidSetConfiguration(BYTE*buffer,BYTE*USB_Configuration)
USVC|=0x80;
PGA_CTRL|=0x80;
*USB_Configuration=buffer[2];
//FIFO_WVALUEL
STALL&
=0x00;
//设置好以上的寄存器
//ClearFeature():
清徐Feature函数
voidClearFeature(BYTE*buffer)
BYTEtemp;
temp=buffer[2];
//FIFO_wValueL
if(temp=0x01)
STALL0|=0x01;
//SendStall0();
voidClearFeature_Endpoint(BYTE*buffer)
temp=buffer[4];
//FIFO_wIndexL
if(bFlag_SetConfiguration_Ready==TRUE);
//表明已经配置过了
temp=GetPipeBit(temp);
//获取对应位
temp=~temp;
//取反
STALL=temp&
STALL;
//与STALL寄存器相与,将需要的位置0
//SetFeature():
设置Feature函数
//包括Endpoint部分
voidSetFeature(BYTE*buffer)
BYTEFIFO_wValueL=buffer[2];
BYTEFIFO_wValueH=buffer[3];
if(FIFO_wValueH==0x00)
if(FIFO_wValueL==0x01)
elseif(FIFO_wValueH>
=0X81<
=0X84)
if(FIFO_wValueL==0x00)
elseSTALL0|=0x01;
voidSetFeature_Endpoint(BYTE*buffer)
{
if(bFlag_SetConfiguration_Ready!
=TRUE)
//有没有被SetConfiguration()设置
temp&
=0x7f;
STALL=temp|STALL;
//将指定的Enpoint置1
//SetInterface():
设置Interface函数
//
BOOLSetInterface(BYTE*buffer,BYTE*USB_Interface_Alt,BYTE*USB_Interface)
*USB_Interface_Alt=buffer[2];
//FIFO_WVALUEL
*USB_Interface=buffer[4];
//FIFO_WINDEXL
PA|=0X01;
//SetPA.0
returnTRUE;
//GetDescriptor():
获取Descriptor
//描述的函数
voidGetDescriptor(BYTE*buffer)
BYTEFIFO_WvalueH=buffer[3];
BYTEFIFO_wIndexL=buffer[4];
switch(FIFO_WvalueH)
case01:
//device,80060001
GetDeviceDescriptor();
case02:
//configurationDescriptor80060002
GetConfigurationDescriptor();
case03:
//string,80060003
GetStringDescriptor();
default:
//**********************************
//****ThentestforHIDclassDescriptor
//***********************************
if(FIFO_wIndexL==0x03)//CheckInterface3,被配置为HID
case22:
//report,81060022
GetReportDescriptor();
case21:
//HID,81060021
GetHIDDescriptor();
//无法解析的情况
voidGetDeviceDescriptor(BYTE*buffer)
BYTEdata_count;
BYTE*data_start;
data_count=device_desc_table[0];
//低字节存放长度
data_start=device_desc_table;
//GetStatus():
设置USB状态的函数
voidGetStatus(BYTE*buffer)
BYTEFIFO_SendLen=0X02;
buffer[0]=0x00;
buffer[1]=0x01;
if(Check_Real_Cmd()==TRUE)break;
//估计是Len0=1,结束会话
if(FIFO_WR_CHECK(0)==FALSE)continue;
Write_FIFO(Fifo_0_Addr,FIFO_SendLen,buffer);
GetStatus_Endpoint(BYTE*buffer)
//FIFO_wIndexL,表示要检测的Endpoint号
//获取位数如:
000000101B=>
00100000B
if(temp&
STALL==TRUE)//要检测的位置1
buffer[0]=1;
elsebuffer[0]=0;
buffer[1]=0;
//GetConfiguration():
设置配置的函数
voidGetConfiguration(BYTE*buffer,BYTEUSB_Configuration)
BYTEFIFO_SendLen;
buffer[0]=USB_Configuration;
FIFO_SendLen=0x01;
=0)//FifoReady
//GetInterface():
获取Interface接口的函数
voidGetInterface(BYTEUSB_Interface_Alt,BYTE*buffer)
buffer[0]=USB_Interface_Alt;
Write_FIFO(Fifo_0_Addr,FIFO_SendLen,buffer)
//SetReport():
设置Report的函数
BYTESetReport(BYTE*buffer)
BYTEnCmdIndex1;
if(FIFO_wValueH==0x02)//set_output_report
if(buffer[4]!
=0x03)//checkinterface
STALL|=0x01;
//ENPOINT0Error
if(buffer[6]!
=0x08)//FIFO_wLengthL,checklength
nCmdIndex1=0x21;
returnnCmdIndex1;
//返回命令索引号
//SetCur():
设置当前信息的函数
//包括VolumeControl,MuteControl
voidSetCur(BYTE*buffer)//;
2101
if(buffer[3]==0x01)//MUTE_CONTROL
MuteControl();
elseif(buffer[3]==0x02)//VOLUME_CONTROL
VolumeControl();
BYTEMuteControl(BYTE*buffer)//21010001,ifhavemorefeature,thestatemustbemodify!
!
if(buffer[5]==0x02)//FIFO_wIndexH,MuteControl_SetSpeaker();
nCmdIndex1=0x18;
elseif(buffer[5]==0x06)//MuteControl_SetMic();
nCmdIndex1=0x19;
BYTEVolumeControl(BYTE*buffer)
if(buffer[5]==0x02)//FIFO_wIndexH,VolumeControl_SetSpeaker()
nCmdIndex1=0x28;
elseifbuffer[5]==0x06)
nCmdIndex1=0x29;
//GetMin():
获取各种最小信息的函数
//包括GetMin_SetSpeaker,GetMin_SetMic
voidGetMin(BYTE*buffer)
if(buffer[5]==0x02)//FIFO_wIndex