單片機-基于stm32usb程序開發(fā)筆記_第1頁
單片機-基于stm32usb程序開發(fā)筆記_第2頁
單片機-基于stm32usb程序開發(fā)筆記_第3頁
單片機-基于stm32usb程序開發(fā)筆記_第4頁
單片機-基于stm32usb程序開發(fā)筆記_第5頁
已閱讀5頁,還剩61頁未讀, 繼續(xù)免費閱讀

下載本文檔

版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請進行舉報或認領(lǐng)

文檔簡介

去去基于STM32USB程序開發(fā)筆以前一直就有打玩USB的想法,最近時間充足于是決心打玩STM32的USB, 月,在固件、上位機驅(qū)動以及應(yīng)用程序的 面終于有所突破,這期間通過網(wǎng)絡(luò)上授尋了許多相關(guān)資料,主要來自ST提供的說確實存在許多的知識壁壘,本著開源精神,在此對STM32的USB固件程序的編寫、DriverStudio+WindowsXPDDK+VC6驅(qū)動開發(fā)第一篇:需要準(zhǔn)備的一1:STM32的參考手冊,這對于設(shè)備底層USB的硬件配置以及事件驅(qū) 操作,比如CNTR、ISTR、EPnR、DADDR等等,如果你想學(xué)習(xí)USB,這個手冊是必須的。的所謂的中文版的USB2.0協(xié)議不是 英文版的USB2.0協(xié)議, STM32 去去 去去去去去去 USB設(shè)備(DEVICE)從來只 觸發(fā),USB主機(HOST)掌握主動權(quán),發(fā)送什么數(shù)據(jù),什么時候發(fā)送,是給設(shè)備數(shù)據(jù)還是從設(shè)備Function

::ThisfunctionhandlesUSBHighPriorityorCANTX:::void{}去Function去

::ThisfunctionhandlesUSBLowPriorityorCANRX0:: :voidUSB_LP_CAN_RX0_IRQHandler(void){}相關(guān)處理。中斷傳輸(interrupt)、控制傳輸(control)、大流量傳輸(bulk)USB_LPI()響應(yīng),大流量傳輸(bulk)同樣可能響應(yīng)USB_HPI(),同步傳輸(isochronous)只響應(yīng)USB_HPI()。USB_HPI()函數(shù)未做任何處理,在此開源希望大家能完善與糾正錯誤并能共享喜悅。以下是USB_LPI()函數(shù):////Function////////

::LowPriorityInterrupt'sservice:::去//去void{unsignedshortwValISTR=#if(CNTR_MASK&ISTR_RESET) //Resetif(wValISTR&ISTR_RESET&vwInterruptMask)去去{}#if(CNTR_MASK&ISTR_DOVR) //DMAOver/Underrunif(wValISTR&ISTR_DOVR&vwInterruptMask){}#if(CNTR_MASK&ISTR_ERR) //Errorif(wValISTR&ISTR_ERR&vwInterruptMask){}#if(CNTR_MASK&ISTR_WKUP) //Wakeupif(wValISTR&ISTR_WKUP&vwInterruptMask){去去}#if(CNTR_MASK&ISTR_SUSP) //Suspendif(wValISTR&ISTR_SUSP&vwInterruptMask){ //mustbedoneaftersettingof}#if(CNTR_MASK&ISTR_SOF) //StartOfFrameif(wValISTR&ISTR_SOF&vwInterruptMask){}#if(CNTR_MASK&ISTR_ESOF) //ExpectedStartOfFrameif(wValISTR&ISTR_ESOF&vwInterruptMask){}#if(CNTR_MASK&ISTR_CTR) //CorrectTransferif(wValISTR&ISTR_CTR&vwInterruptMask){}}////Function////////

::HighPriorityInterrupt'sservice:::去//去void{}INT_ISTR_CTR()響應(yīng)一次正確的數(shù)據(jù)傳輸中斷,故名思意,在完成一次正確的數(shù)據(jù)傳輸操作后,就會響應(yīng)此函數(shù)。voidISTR_CTR(void);voidISTR_SOF(void);voidISTR_ESOF(void);voidISTR_DOVR(void);voidISTR_ERROR(void);voidISTR_RESET(void);voidISTR_WAKEUP(void);//CNTRmask#defineCNTR_MASK CNTR_CTRM|CNTR_WKUPM|CNTR_SUSPM|CNTR_ERRM| CNTR_SOFM|CNTR_ESOFM|CNTR_RESETM| //去//Function去////////

::ISTRResetInterruptservice:::去去//void{//Setthebuffertable//Set pointtype:ENDP0SetEPR_Type(ENDP0,//Set pointdatabufferaddress:ENDP0RXSetBuffDescTable_RXAddr(ENDP0,ENDP0_RXADDR);//Set pointdatabufferaddress:ENDP0TXSetBuffDescTable_TXCount(ENDP0,0);SetBuffDescTable_TXAddr(ENDP0,ENDP0_TXADDR);//InitializetheRX/TXstatus:ENDP0SetEPR_RXStatus(ENDP0,EP_RX_VALID);SetEPR_TXStatus(ENDP0,//Set pointaddress:ENDP0SetEPR_Address(ENDP0,ENDP0);去去//TODO:Addyoucode// pointtype:SetEPR_Type(ENDP1,//Set pointdatabufferaddress:ENDP1RXSetBuffDescTable_RXAddr(ENDP1,ENDP1_RXADDR);//Set pointdatabufferaddress:ENDP1TXSetBuffDescTable_TXCount(ENDP1,0);SetBuffDescTable_TXAddr(ENDP1,ENDP1_TXADDR);//InitializetheRX/TXstatus:ENDP1SetEPR_RXStatus(ENDP1,EP_RX_VALID);SetEPR_TXStatus(ENDP1,//Set pointaddress:ENDP1SetEPR_Address(ENDP1,ENDP1);SetEPR_Type(ENDP2,去去//Set pointdatabufferaddress:ENDP2RXSetBuffDescTable_RXAddr(ENDP2,ENDP2_RXADDR);//Set pointdatabufferaddress:ENDP2TXSetBuffDescTable_TXCount(ENDP2,0);SetBuffDescTable_TXAddr(ENDP2,ENDP2_TXADDR);//InitializetheRX/TXstatus:ENDP2SetEPR_RXStatus(ENDP2,EP_RX_DIS);SetEPR_TXStatus(ENDP2,EP_TX_VALID);//Set pointaddress:ENDP2SetEPR_Address(ENDP2,ENDP2);//EndofyouvsDeviceInfo.eDeviceState=DS_DEFAULT;vsDeviceInfo.bCurrentFeature=0x00;vsDeviceInfo.bCurrentConfiguration=0x00;vsDeviceInfo.bCurrentInterface=0x00;去去vsDeviceInfo.bCurrentAlternateSetting=vsDeviceInfo.uStatusInfo.w=}RX/TX緩沖描述表說明是數(shù)據(jù) //USB_IPPacketMemoryAreabaseaddress#definePMAAddr(0x40006000L)//BufferTableaddress#defineBTABLE((volatileunsigned*)(RegBase+////Packetmemoryarea:Total//#define ////PMAAddr+BASEADDR_BTABLE+0x00000000://PMAAddr+BASEADDR_BTABLE+0x00000002://PMAAddr+BASEADDR_BTABLE+0x00000004://PMAAddr+BASEADDR_BTABLE+0x00000006://PMAAddr+BASEADDR_BTABLE+0x00000008://PMAAddr+BASEADDR_BTABLE+0x0000000A:去去//PMAAddr+BASEADDR_BTABLE+0x0000000C://PMAAddr+BASEADDR_BTABLE+0x0000000E://PMAAddr+BASEADDR_BTABLE+0x00000010://PMAAddr+BASEADDR_BTABLE+0x00000012://PMAAddr+BASEADDR_BTABLE+0x00000014://PMAAddr+BASEADDR_BTABLE+0x00000016://PMAAddr+BASEADDR_BTABLE+0x00000018://PMAAddr+BASEADDR_BTABLE+0x0000001A://PMAAddr+BASEADDR_BTABLE+0x0000001C://PMAAddr+BASEADDR_BTABLE+0x0000001E://PMAAddr+BASEADDR_BTABLE+0x00000020://PMAAddr+BASEADDR_BTABLE+0x00000022://PMAAddr+BASEADDR_BTABLE+0x00000024://PMAAddr+BASEADDR_BTABLE+0x00000026://PMAAddr+BASEADDR_BTABLE+0x00000028://PMAAddr+BASEADDR_BTABLE+0x0000002A://PMAAddr+BASEADDR_BTABLE+0x0000002C://PMAAddr+BASEADDR_BTABLE+0x0000002E://PMAAddr+BASEADDR_BTABLE+0x00000030://PMAAddr+BASEADDR_BTABLE+0x00000032://PMAAddr+BASEADDR_BTABLE+0x00000034://PMAAddr+BASEADDR_BTABLE+0x00000036://PMAAddr+BASEADDR_BTABLE+0x00000038://PMAAddr+BASEADDR_BTABLE+0x0000003A://PMAAddr+BASEADDR_BTABLE+0x0000003C://PMAAddr+BASEADDR_BTABLE+0x0000003E:////PMAAddr+BASEADDR_BTABLE+(0x00000040-0x000001FF):assignedtodata//#define (BASEADDR_BTABLE+//ENP0

(ENDP0_RXADDR+去去//#define #defineENDP1_RXADDR (ENDP0_TXADDR+ENDP0_PACKETSIZE)#define (ENDP1_RXADDR+//#define #define (ENDP1_TXADDR+去去+//++//++//++//++//++////Function////////

::ISTRCorrectTransferInterruptservice:::去//去void{unsigned unsignedshortwValISTR;unsignedshortwhile(((wValISTR=GetISTR())&ISTR_CTR)!=0{//Gettheindexnumber Index=wValISTR& Index=={//Setendpoint0RX/TXstatus:NAK(Negative-SetEPR_TXStatus(ENDP0,去去//Transferif((wValISTR&ISTR_DIR)=={//DIR=0://DIR=0impliesthatEP_CTR_TXalways}{//DIR=1:SETUPor//DIR=1impliesthatCTR_TXorCTR_RXalwayswValENDP=GetEPR(ENDP0);if((wValENDP&EP_CTR_TX)!=0){}elseif((wValENDP&EP_SETUP)!={去去}elseif((wValENDP&EP_CTR_RX)!={}}}//Other{wValENDP= Index,if((wValENDP&EP_CTR_TX)!={ClrEPR_CTR_TX( {caseENDP1:CTR_IN1();caseENDP2:CTR_IN2();break;caseENDP3:CTR_IN3();break;caseENDP4:CTR_IN4();break;去去caseENDP5:CTR_IN5();break;caseENDP6:CTR_IN6();break;caseENDP7:CTR_IN7();break;default:break;}}if((wValENDP&EP_CTR_RX)!={ClrEPR_CTR_RX( {caseENDP1:CTR_OUT1();caseENDP2:CTR_OUT2();break;caseENDP3:CTR_OUT3();break;caseENDP4:CTR_OUT4();break;caseENDP5:CTR_OUT5();break;caseENDP6:CTR_OUT6();break;caseENDP7:CTR_OUT7();break;default:break;}}}}}去去去去去去去去去去去去去去去去volatileDEVICE_INFO////typedefstruct_DEVICE_INFO{unsignedcharunsignedcharbCurrentFeature;unsignedcharbCurrentConfiguration;unsignedcharbCurrentInterface;

去CONTROL_STATE去 TRANSFER_INFO}typedefstruct{unsignedcharunsignedchar

//request// 去去WORD_2BYTEwValue;WORD_2BYTEwIndex;WORD_2BYTE}typedefunion{unsignedshort{unsignedcharunsignedchar}去去CTR_SETUP0()函數(shù)將SETUP數(shù)據(jù)提取出來,SETUP數(shù)據(jù)結(jié)構(gòu)有0長度和非0長度的數(shù)據(jù)結(jié)構(gòu),詳細參閱USB2.0 這將兩種區(qū)別開來分別執(zhí)行SETUP0_NoData()和SETUP0_Data()函數(shù),并返回結(jié)果,根據(jù)返回結(jié)果再響應(yīng)USB主機////FunctionName:// // // // //void{RESULTBufferCopy_PMAToUser((unsignedcharif(vsDeviceInfo.SetupData.wLength.w=={eResult=}去去{eResult=}{casecasecasecaseSetEPR_TXStatus(ENDP0,}}done"的部分表明此部分功能已完成。對于未完成部////RoutineGroups:RESULTSETUP0_Data(void){//SetupData.bRequest: {casecaseSR_GET_DESCRIPTOR:caseSR_SET_DESCRIPTOR:caseSR_GET_INTERFACE:case

returnreturnSR_GetDescriptor();returnSR_SetDescriptor();returnSR_GetInterface();returnSR_SynchFrame();

////////////去去default:return}}////RoutineGroups://RESULT{//SetupData.bRequest: {casecasecaseSR_SET_INTERFACE:

returnSR_ClearFeature();returnSR_SetFeature();returnSR_SetAddress();returnSR_SetInterface();

//////////default:return}}SetEPR_RXStatus(ENDP0,SetEPR_TXStatus(ENDP0,//去//Function去//////

::::// RESULTSETUP0_Trans0Data(void){//Send0-lengthdataframeasACKtoSetBuffDescTable_TXCount(ENDP0,0);SetEPR_RXStatus(ENDP0,EP_RX_NAK);return}////Function////////

:::::去//去RESULT{unsignedshortwLength=unsignedshortwOffset=vsDeviceInfo.TransInfo.wOffset;unsignedshortwMaxSize=vsDeviceInfo.TransInfo. {if(wLength>{wLength=}//Copythetransferbufferto point0'sbuffer

//transfer//endpoint0TX去去SetBuffDescTable_TXCount(ENDP0,wLength);SetEPR_RXStatus(ENDP0,EP_RX_NAK);SetEPR_TXStatus(ENDP0,EP_TX_VALID);//UpdatethedatalengthsvsDeviceInfo.TransInfo.wLength-=wLength;vsDeviceInfo.TransInfo.wOffset+=return}return}//////typedefstruct{unsignedshortunsignedshortwOffset;unsignedshort unsignedchar*pBuffer;}

//totallengthsdatawillbe//numberofdatabe//endpointspacketmax//addressofdata去去機將無條件退出請求;TXVALID,USBUSB設(shè)備數(shù)據(jù)。CTR_SETUP0()->SETUP0_Data()->SR_GetDescriptor()-RESULT{//RequestType:device->host,standardrequestanddeviceif(vsDeviceInfo.SetupData.bmRequestType=={//SetupData.wValue.b.MSB:descriptor//SetupData.wValue.b.LSB:descriptorindex{casecaseDESCRIPTOR_CONFIG:caseDESCRIPTOR_STRING:

returnreturnSR_GetDescriptor_Config();returnSR_GetDescriptor_String();去去default:return}}return}RESULT{//AssignedthedevicedescriptortothevsDeviceInfo.TransInfo.wOffset=0; cketSize=ENDP0_PACKETSIZE;vsDeviceInfo.TransInfo.wLength=DescBuffer_Device.wLen;vsDeviceInfo.eControlState=CS_GET_DESCRIPTOR;if(vsDeviceInfo.TransInfo.wLength>去去{vsDeviceInfo.TransInfo.wLength=}return} ////FunctionName:// // // // //void{{caseif(SETUP0_TransData()=={SetEPR_TXStatus(ENDP0,SetEPR_RXStatus(ENDP0,}去去caseCS_SET_ADDRESS:SetEPR_TXStatus(ENDP0,EP_TX_NAK);vsDeviceInfo.eDeviceState=DS_ADDRESSED;caseSetEPR_TXStatus(ENDP0,SetEPR_RXStatus(ENDP0,vsDeviceInfo.eDeviceState=DS_CONFIGURED;}}構(gòu)等等,條件是你必須了解這些標(biāo)準(zhǔn)設(shè)備的一些信息,好處是省去一些麻煩的驅(qū)動編寫。constunsignedcharcbDescriptor_Device[DESC_SIZE_DEVICE]{

//bLength://descriptor

//bcdUSBLSB:USBreleasenumber->//bcdUSBMSB:USBreleasenumber->

// Classinformationintheinterface//////bMaxPacketSize0:LowS(8),FullS(8,16,32,64),去去idVendorLSB:idVendoridProductidProductbcdDevicebcdDevice

//iManufacturer:Indexofstringdescriptordescribing//iProduct:Indexofstringdescriptordescribing//iSerialNumber:Indexofstringdescriptordescribingthedeviceserial

//bNumConfigurations:numberofconstunsignedcharcbDescriptor_Config[DESC_SIZE_CONFIG]{//Descriptorof

////descriptor

//Totalconfigurationdescriptorlengths//Totalconfigurationdescriptorlengths

//bNumInterfaces:Totalnumberof//bConfigurationValue:Configuration//iConfiguration:Indexofstringdescriptordescribingthe去去 //bmAttributes:bus//bit4...0 ,setto//bit//bit//bit

:Remotewakeup:Selfpower ,setto //bMaxPower:thiscurrentisusedfordetectingVbus=//Descriptorofinterface

//bInterfaceNumber:Numberof//bAlternateSetting:Alternate

//bNumEndpoints:Numberofendpointsexcept//////去去 //iInterface:Indexofstringdescriptordescribingthe//Descriptorofendpoint1OUT ////bit3...0 point//bit6...4//bit :0(OUT),

////bit1...0:Transfer 00(CONTROL),01(ISOCHRONOUS),10(BULK),//bit3...2:Synchronization 00(NoSynch),01(Asynchronous),10(Adaptive),//bit5...4:EndpointUsage 00(data),01(Feedback),10(Implicitfeedbackdataendpoint), //bit7...6 ,mustbe//packetsize//packetsize去去 //pollingintervaltime://Descriptorofendpoint2IN ////bit3...0 point//bit6...4//bit :0(OUT),

bit1...0:Transfer00(CONTROL),01(ISOCHRONOUS),10(BULK),//bit3...2//bit5...4//bit7...6

Synchronization00(NoSynch),01(Asynchronous),10(Adaptive),EndpointUsage00(data),01(Feedback),10(Implicitfeedbackdataendpoint), ,mustbe //packetsize //packetsize

//pollingintervaltime:constunsignedcharcbDescriptor_StringLangID[DESC_SIZE_STRING_LANGID]{

////bDescriptorType=String去去

//LangID//LangIDMSB:0x0409(U.S.去去constunsignedcharcbDescriptor_StringVendor[DESC_SIZE_STRING_VENDOR]{DESC_SIZE_STRING_VENDOR,// //bDescriptorType=String//String:'L',0,'a',0,'B',0,'i',0,'X',0,'i',0,'a',0,'X',0,'i',0,'a',0,'o',0,'X',0,'i',0,constunsignedcharcbDescriptor_StringProduct[DESC_SIZE_STRING_PRODUCT]{DESC_SIZE_STRING_PRODUCT,// //bDescriptorType=String//String:"STM32ezUSB-CORE'S',0,'T',0,'M',0,'3',0,'2',0,'',0,'e',0,'z',0,'U',0,'S',0,'-',0,'C',0,'O',0,'R',0,'E',0,'',0,'V',0,'1',0,'.',0,'0',0,constunsignedcharcbDescriptor_StringSerial[DESC_SIZE_STRING_SERIAL]{DESC_SIZE_STRING_SERIAL,// //bDescriptorType=String去去//String:"ezUSB-COREDemo'e',0,'z',0,'U',0,'S',0,'B',0,'-',0,'C',0,'O',0,'R',0,'E',0,''D',0,'e',0,'m',0,'o',0,'',0,'2',0,'0',0,'0',0,'8',0,'/',0,'1',0,'1',0,'/',0,'1',0, 必須的先決條件1:XPDDK(DriverDevelopmentKits),可從MS 處理稍微不當(dāng),就容易讓你系統(tǒng)直接掛了(WINDOWS著名的藍屏)。DDK提供了一些驅(qū)動代碼,有 去去WindowsXP去去去去 去去ReadFile()/WriteFile()IRP_MJ_READ/IRP_MJ_WRITEReadFrom_EP2和WriteTo_EP1NTSTATUSezUSBDevice::ReadFrom_EP2_Handler(KIrpI);NTSTATUSezUSBDevice::WriteTo_EP1_Handler(KIrpI);去去去去 去去去去 去去VC6USBCUSBAPIUSBAPI.h#defineFILE_DEVICE_EZUSB#defineEZUSB_IOCTL(index)CTL_CODE(FILE_DEVICE_EZUSB,index,METHOD_BUFFERED,#defineReadFrom_EP2CTL_CODE(FILE_DEVICE_EZUSB,0x800,METHOD_IN_DIRECT,#defineWriteTo_EP1CTL_CODE(FILE_DEVICE_EZUSB,0x801,METHOD_OUT_DIRECT, typedefstruct{ //Indexforthisstruct_NODE_ENUDEVICEINTERFACE}去去 0xE075C5B2,0xE7FB,0x4186,0xA1,0x39,0x0B,0x3F,0xD2,0x05,0xE7, typedefstruct{HWNDh HANDLEhDevice;DWORDDWORDdwInSize;DWORDdwOutSize;LPDWORD} #define#defineCORESTATUS_READWRITE_EVENT_ERROR#defineCORESTATUS_IOCONTROL_EVENT_ERROR

#defineERROR_HANDLE_WINDOW#defineERROR_HANDLE_DEVICE#defineERROR_BUFFER_LENGTH#defineERROR_BUFFER_ISNULL#define

去去 PLETION和 PLETION通知應(yīng)用程序。詳細代碼請參考源程序。////ClassmembersclassCUSBAPI{DWORDEnumDeviceInter

溫馨提示

  • 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫網(wǎng)僅提供信息存儲空間,僅對用戶上傳內(nèi)容的表現(xiàn)方式做保護處理,對用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對任何下載內(nèi)容負責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時也不承擔(dān)用戶因使用這些下載資源對自己和他人造成任何形式的傷害或損失。

評論

0/150

提交評論