您好,欢迎访问三七文档
当前位置:首页 > 商业/管理/HR > 管理学资料 > USB-固件程序详细注解
USB-固件程序详细注解(一)USB的程序真是浩大,不过还好不用自己写了,花下时间读下来就行了,我从主函数main()读起,不懂的就查前面的函数和定义,为这个伟大的工程努力,不过还好自己买了周立功的书,书上有中文注释,我尽量打上去,不过圈圈写的程序和书上有些不同,也要自己慢慢找,希望我贴出来的程序对大家有帮助,欢迎指正.voidmain(void){unsignedshortID;unsignedcharhigh,low;init_port();init_special_interrupts();init_flags();ID=D12_ReadChipID();high=(unsignedchar)(ID8);low=(unsignedchar)ID;reconnect_USB();while(TRUE){if(bEPPflags.bits.bus_reset){DISABLE;bEPPflags.bits.bus_reset=0;ENABLE;//D12SUSPD=1;}/*if(bEPPflags.bits.suspend){DISABLE;bEPPflags.bits.suspend=0;ENABLE;if(D12SUSPD==1){D12SUSPD=0;P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;D12SUSPD=1;PCON|=0x02;while(1);}}//ifsuspendchange*/if(bEPPflags.bits.setup_packet){DISABLE;bEPPflags.bits.setup_packet=0;ENABLE;control_handler();//D12SUSPD=1;}//ifsetup_packetif(bEPPflags.bits.configuration)IspControl();}//MainLoop}voidinit_port(){P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;//D12SUSPD=0;}voidinit_special_interrupts(void){EA=1;IT1=0;EX1=1;PX1=1;}voidinit_flags(){bEPPflags.bits.bus_reset=0;bEPPflags.bits.suspend=0;bEPPflags.bits.setup_packet=0;bEPPflags.bits.remote_wakeup=0;bEPPflags.bits.in_isr=0;bEPPflags.bits.control_state=0;bEPPflags.bits.configuration=0;bEPPflags.bits.ep1_rxdone=0;bEPPflags.bits.main_rxdone=0;}/*//*************************************************************************//structureanduniondefinitions//**************************************************************************/typedefunion_epp_flags{struct_flags{//unsignedchartimer:1;//时间溢出unsignedcharbus_reset:1;//总线复位标志unsignedcharsuspend:1;//挂起改变标志unsignedcharsetup_packet:1;//收到setup包unsignedcharremote_wakeup:1;//远程唤醒标志(未使用)unsignedcharin_isr:1;//USB中断标志unsignedcharcontrol_state:2;//控制端点处理状态//0:空闲//1:数据发送//2:数据接收unsignedcharconfiguration:1;//配置标志(0:未配置;1:已经配置)//unsignedcharverbose:1;unsignedcharep1_rxdone:1;//端点1收到数据标志unsignedcharmain_rxdone:1;//主端点收到数据标志unsignedcharsetup_dma:2;//V2.3//unsignedchardma_state:2;//unsignedcharpower_down:1;//SmartBoard}bits;unsignedshortvalue;}EPPFLAGS;unsignedshortD12_ReadChipID(void){unsignedshorti,j;if(bEPPflags.bits.in_isr==0)DISABLE;outportb(D12_COMMAND,0xFD);//#defineD12_COMMAND0x03i=inportb(D12_DATA);//#defineD12_DATA0x02j=inportb(D12_DATA);i+=(j8);if(bEPPflags.bits.in_isr==0)ENABLE;returni;}//向PDIUSBD12发送数据或命令的子程序voidoutportb(unsignedcharport,unsignedcharval){unsignedcharxdata*ext_address;MCU_D12CS=0;//片选信号,低有效ext_address=0xff00+port;*ext_address=val;MCU_D12CS=1;}//从PDIUSBD12读取数据unsignedcharinportb(unsignedcharport){unsignedcharc;unsignedcharxdata*ext_address;MCU_D12CS=0;ext_address=0xff00+port;c=*ext_address;MCU_D12CS=1;returnc;}voidreconnect_USB(void){//D12SUSPD=0;//唤醒PDIUSBD12disconnect_USB();//断开芯片与USB总线连接connect_USB();}voiddisconnect_USB(void){//初始化PDIUSBD12工作状态(断开SoftConnet上拉电阻)D12_SetMode(D12_NOLAZYCLOCK,D12_SETTOONE|D12_CLOCK_12M);//#defineD12_NOLAZYCLOCK0x02}//#defineD12_SETTOONE0x40//#defineD12_CLOCK_12M0x03/*设置模式命令:F3H*/voidD12_SetMode(unsignedcharbConfig,unsignedcharbClkDiv){if(bEPPflags.bits.in_isr==0)DISABLE;outportb(D12_COMMAND,0xF3);outportb(D12_DATA,bConfig);outportb(D12_DATA,bClkDiv);if(bEPPflags.bits.in_isr==0)ENABLE;}voidconnect_USB(void){D12_SetDMA(D12_ENDP4INTENABLE|D12_ENDP5INTENABLE);//#defineD12_ENDP4INTENABLE0x40//#defineD12_ENDP5INTENABLE0x80D12_SetMode(D12_NOLAZYCLOCK|D12_SOFTCONNECT,D12_SETTOONE|D12_CLOCK_12M);//#defineD12_SOFTCONNECT0x10}/*设置DMA命令:FBH*/voidD12_SetDMA(unsignedcharbMode){if(bEPPflags.bits.in_isr==0)DISABLE;outportb(D12_COMMAND,0xFB);outportb(D12_DATA,bMode);if(bEPPflags.bits.in_isr==0)ENABLE;}USB-固件程序详细注解(二)看到头都大了,虽然不是全都明白,但起码走了一遍程序,附上我读完主程序涉及的所有注释.直接一句句往下读就行了,函数都在程序的后面的,找不到就用查找功能找一下.voidmain(void){unsignedshortID;unsignedcharhigh,low;init_port();init_special_interrupts();init_flags();ID=D12_ReadChipID();high=(unsignedchar)(ID8);low=(unsignedchar)ID;reconnect_USB();while(TRUE){if(bEPPflags.bits.bus_reset)//总线复位处理{DISABLE;bEPPflags.bits.bus_reset=0;//清总线复位标志ENABLE;//D12SUSPD=1;//释放PDIUSBD12挂起控制}/*if(bEPPflags.bits.suspend)//挂起改变处理{DISABLE;bEPPflags.bits.suspend=0;//清除挂起改变标志ENABLE;if(D12SUSPD==1)//进入挂起处理{D12SUSPD=0;//唤醒PDIUSBD12,使得USB器件在//MCU进入低功耗模式后再进入挂起P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;D12SUSPD=1;//释放挂起控制PCON|=0x02;//进入掉电模式while(1);//芯片进入死循环,因为标志51进入掉电}//后,只有复位才可以唤醒芯片}//ifsuspendchange*/if(bEPPflags.bits.setup_packet)//Setup包处理{DISABLE;bEPPflags.bits.setup_packet=0;ENABLE;control_handler();//调用请求处理子程序//D12SUSPD=1;//释放挂起控制}//ifsetup_packetif(bEPPflags.bits.configuration)IspControl();}//MainLoop}voidinit_port(){P0=0xFF;P1=0xFF;P2=0xFF;P3=0xFF;//D12SUSPD=0;}voidinit_special_interrupts(void){EA=1;IT1=0;EX1=1;PX1=1;}voidinit_flags(){bEPPflags.bits.bus_reset=0;bEPPflags.bits.suspend=0;bEPPflags.bits.setup_packet=0;bEPPflags.bits.remote_wakeup=0;bEPPflags.bits.in_isr=0;bEPPflags.bits.control_state=0;bEPPflags.bits.configuration=0;bEPPflags.bits.ep1_rxdone=0;bEPPflags.bits.main_rxdone=0;}/*//*************************************************************************//structureanduniondefinitions//**************************************************************************/typedefunion_epp_flags{st
本文标题:USB-固件程序详细注解
链接地址:https://www.777doc.com/doc-3391369 .html