STM32USB-FS-DevicefirmwarelibraryUM0424Doublebufferaddressesa)These functions set orget bufferaddress value in the buffer description tablefordoublebufferedmodeSetEPDblBuffAddr:voidSetEPDblBuffAddr(uint8tbEpNum,uint16_t wBuf0Addr,uint16_t wBuf1Addr);SetEPDblbufoAddr:void SetEPDblBufOAddr(uint8tbEpNum,uint16twBufoAddr);SetEPDblbuf1Addr : void SetEPDblBuf1Addr(uint8_tbEpNum,uint16_t wBuf1Addr);bEpNum = endpoint numberwBufoAddr, wBufiAddr =buffer addresses(expressed as PMAbuffer addresses)GetEPDblBuf0Addr:uint16_t GetEPDblBuf0Addr(uint8_tbEpNum);GetEPDblbufiAddr:uintl6 t GetEPDblBuf1Addr(uint8_tbEpNum);bEpNum = endpoint numberreturn value :buffer addressesb)DoublebuffercountersThesefunctionssetorgetbuffercountervalue inthebufferdescriptiontablefordouble buffered mode.SetEPDbIBuffCount:void SetEPDblBuffCount(uint8_t bEpNum,uint8_tbDir, uinti6_t wCount);SetEPDblBufoCount:void SetEPDblBufOCount(uint8_t bEpNum, uint8_tbDir, uinti6_t wCount);SetEPDblBuficount:void SetEPDblBuf1Count(uint8_t bEpNum, uint8_tbDir,uint16_t wCount);bEpNum = endpoint numberbDir= endpoint directionwCount = buffer countert :uint16_t GetEPDblBuf0Count(uint8_t bEpNum);GetEPDblBufoCountGetEPDblBufiCount:uint16_tGetEPDblBuf1Count(uint8_tbEpNum);bEpNum = endpoint numberreturn value : buffer counterc)DoublebufferSTATUSThe simple and double buffer modes use the same functions to manage theEndpoint STATUS except for the STALL status for double buffer mode.Thisfunctionality is managed by the function:SetDouBleBuffEPStall:void SetDouBleBuffEPstall(uint8tbEpNum,uint8_t bDir)bEpNum = endpoint numberbDir= endpoint directionS16/85DocID13465Rev12
STM32 USB-FS-Device firmware library UM0424 16/85 Doc ID 13465 Rev 12 a) Double buffer addresses These functions set or get buffer address value in the buffer description table for double buffered mode. SetEPDblBuffAddr : void SetEPDblBuffAddr(uint8_t bEpNum,uint16_t wBuf0Addr,uint16_t wBuf1Addr); SetEPDblbuf0Addr : void SetEPDblBuf0Addr(uint8_t bEpNum,uint16_t wBuf0Addr); SetEPDblbuf1Addr : void SetEPDblBuf1Addr(uint8_t bEpNum,uint16_t wBuf1Addr); bEpNum = endpoint number wBuf0Addr, wBuf1Addr = buffer addresses (expressed as PMA buffer addresses) GetEPDblBuf0Addr : uint16_t GetEPDblBuf0Addr(uint8_t bEpNum); GetEPDblbuf1Addr : uint16_t GetEPDblBuf1Addr(uint8_t bEpNum); bEpNum = endpoint number return value : buffer addresses b) Double buffer counters These functions set or get buffer counter value in the buffer description table for double buffered mode. SetEPDblBuffCount: void SetEPDblBuffCount(uint8_t bEpNum, uint8_t bDir, uint16_t wCount); SetEPDblBuf0Count: void SetEPDblBuf0Count(uint8_t bEpNum, uint8_t bDir, uint16_t wCount); SetEPDblBuf1Count: void SetEPDblBuf1Count(uint8_t bEpNum, uint8_t bDir, uint16_t wCount); bEpNum = endpoint number bDir = endpoint direction wCount = buffer counter GetEPDblBuf0Count : uint16_t GetEPDblBuf0Count(uint8_t bEpNum); GetEPDblBuf1Count : uint16_t GetEPDblBuf1Count(uint8_t bEpNum); bEpNum = endpoint number return value : buffer counter c) Double buffer STATUS The simple and double buffer modes use the same functions to manage the Endpoint STATUS except for the STALL status for double buffer mode. This functionality is managed by the function: SetDouBleBuffEPStall: void SetDouBleBuffEPStall(uint8_t bEpNum,uint8_t bDir) bEpNum = endpoint number bDir = endpoint direction
UM0424STM32USB-FS-Devicefirmwarelibrary3.2.2usb_int (h,.c)The usb_int module handles the correcttransfer interrupt service routines; it offers the linkbetween theUSBdeviceprotocoleventsandthe library.TheSTM32USB-FS_Deviceperipheralprovidestwotransferroutines:Low-priorityinterrupt:managedbythefunctioncTRLp()andusedforcontrol.interruptandbulk (insimplebuffermode)High-priority interrupt:managedbythefunction CTR HP () and usedforfastertransfermodelikeIsochronousandbulk (indoublebuffermode).3.2.3usb_mem (.h,.c)Theusb_memcopiesabufferdatafromtheusermemoryareatothepacketmemoryarea(PMA)andviceversa.Itprovidestwodifferentfunctions:void UserToPMABufferCopy(uint8_t*pbUsrBuf,uint16_t wPMABufAddr,uint16_twNBytes);voidPMAToUserBufferCopy(uint8_t*pbUsrBuf,uint16_t wPMABufAddr,uint16_t?wNBytes);Where:pbUsrBuf is the pointer to the user memory area generally in the product's SRAM.wPMABufAddr is the address in PMA (512-byte packet memory area dedicated toUSB),wNBytes isthenumberof bytestobecopied.3.3USB-FS-Device_DrivermediumlayerTable6presentstheUSB-FS-Device_Drivermediumlayermodules:Table6.USB-FS-Device_DrivermediumlayermodulesFileDescriptionusb_init (.h,.c)USBdevice initializationglobal variablesUSBprotocolmanagement(compliantwithchapter9oftheUSB2.0usb_core (.h,.c)specification)Simplified functions for read & write accesses to the endpoints (abstractionusb_sil (.h,.c)layerfor theUSB-FS_Deviceperipheral)usb_def.h/usb_type.hUSBdefinitionsandtypesusedinthe libraryplatform_config.hDefinesthehardwaredependingontheevaluationboardused3.3.1usb_init(.h,.c)This module sets initialization routines andglobal variables thatwillbeused in the library3.3.2usb_core(.h,.c)This module is the "kernel" of the library.It implements all the functions described inChapter9oftheUSB2.0specification.STDocID13465Rev1217/85
UM0424 STM32 USB-FS-Device firmware library Doc ID 13465 Rev 12 17/85 3.2.2 usb_int (.h , .c) The usb_int module handles the correct transfer interrupt service routines; it offers the link between the USB device protocol events and the library. The STM32 USB-FS_Device peripheral provides two transfer routines: ● Low-priority interrupt: managed by the function CTR_LP() and used for control, interrupt and bulk (in simple buffer mode). ● High-priority interrupt: managed by the function CTR_HP() and used for faster transfer mode like Isochronous and bulk (in double buffer mode). 3.2.3 usb_mem (.h , .c) The usb_mem copies a buffer data from the user memory area to the packet memory area (PMA) and vice versa. It provides two different functions: ● void UserToPMABufferCopy(uint8_t *pbUsrBuf,uint16_t wPMABufAddr, uint16_t wNBytes); ● void PMAToUserBufferCopy(uint8_t *pbUsrBuf,uint16_t wPMABufAddr, uint16_t wNBytes); Where: ● pbUsrBuf is the pointer to the user memory area generally in the product’s SRAM. ● wPMABufAddr is the address in PMA (512-byte packet memory area dedicated to USB). ● wNBytes is the number of bytes to be copied. 3.3 USB-FS-Device_Driver medium layer Table 6 presents the USB-FS-Device_Driver medium layer modules: l 3.3.1 usb_init(.h,.c) This module sets initialization routines and global variables that will be used in the library. 3.3.2 usb_core (.h , .c) This module is the “kernel” of the library. It implements all the functions described in Chapter 9 of the USB 2.0 specification. Table 6. USB-FS-Device_Driver medium layer modules File Description usb_init (.h,.c) USB device initialization global variables usb_core (.h , .c) USB protocol management (compliant with chapter 9 of the USB 2.0 specification) usb_sil (.h,.c) Simplified functions for read & write accesses to the endpoints (abstraction layer for the USB-FS_Device peripheral) usb_def.h / usb_type.h USB definitions and types used in the library platform_config.h Defines the hardware depending on the evaluation board used
STM32 USB-FS-Device firmware libraryUM0424The available subroutines cover handling of UsB standard requests related to the controlendpoint (ENDPo),offeringthenecessarycodetoaccomplishthesequenceofenumerationphase.A state machine is implemented in order to process the different stages of the setuptransactions.The USB core module also implements a dynamic interface between the standard requestandtheuserimplementationusingthestructureUser_Standard_Requests.The UsB core dispatches the class specific requests and some bus events to user programwhenever it is necessary.Userhandling procedures are given in the Device_Propertystructure.Thedifferentdata and function structures usedbythekernelaredescribed inthefollowingparagraphs.1.Device table structureThe corekeepsdevicelevel information intheDevice_Tablestructure.Device_Tableis of thetype:DEVICE.typedef struct_DEVICE(uint8_t Total_Endpoint;uint8_tTotal Configuration;DEVICE;2.Deviceinformation structureTheUSBcorekeepsthesetuppacketfromthehostfortheimplementedUSBDeviceinthe Device_Info structure.This structure has the type:DEVICE_INFO.typedef struct_DEVICE_INFO(uint8_t USBbmRequestType;uint8_t USBbRequest;uint16_t_uint8_t UsBwValues;uint16_t_uint8_t UsBwIndexs;uint16_t_uint8_t UsBwLengths;uint8 tControlstate;uint8 tCurrent Feature;uint8_tCurrent_Configuration;uint8 tCurrent_Interface;uint8_t Current_Alternatesetting;ENDPOINT_INFO Ctrl_Info;DEVICE_INFO;1Anunionuint16_t_uint8_tisdefinedtoeasilyaccesssome fields intheDEVICE_INFOineitheruint16_toruint8_tformattypedef union (uint16_t w;struct Bw (uint8_t bbl;uint8_t bbo;1 bw;Juint16_t_uint8_t;S18/85DocID13465Rev12
STM32 USB-FS-Device firmware library UM0424 18/85 Doc ID 13465 Rev 12 The available subroutines cover handling of USB standard requests related to the control endpoint (ENDP0), offering the necessary code to accomplish the sequence of enumeration phase. A state machine is implemented in order to process the different stages of the setup transactions. The USB core module also implements a dynamic interface between the standard request and the user implementation using the structure User_Standard_Requests. The USB core dispatches the class specific requests and some bus events to user program whenever it is necessary. User handling procedures are given in the Device_Property structure. The different data and function structures used by the kernel are described in the following paragraphs. 1. Device table structure The core keeps device level information in the Device_Table structure. Device_Table is of the type: DEVICE. typedef struct _DEVICE { uint8_t Total_Endpoint; uint8_t Total_Configuration; } DEVICE; 2. Device information structure The USB core keeps the setup packet from the host for the implemented USB Device in the Device_Info structure. This structure has the type: DEVICE_INFO. typedef struct _DEVICE_INFO { uint8_t USBbmRequestType; uint8_t USBbRequest; uint16_t_uint8_t USBwValues; uint16_t_uint8_t USBwIndexs; uint16_t_uint8_t USBwLengths; uint8_t ControlState; uint8_t Current_Feature; uint8_t Current_Configuration; uint8_t Current_Interface; uint8_t Current_AlternateSetting; ENDPOINT_INFO Ctrl_Info; } DEVICE_INFO; An union uint16_t_uint8_t is defined to easily access some fields in the DEVICE_INFO in either uint16_t or uint8_t format. typedef union { uint16_t w; struct BW { uint8_t bb1; uint8_t bb0; } bw; } uint16_t_uint8_t;
UM0424STM32USB-FS-DevicefirmwarelibraryDescription of the structure fields:USBbmRequestType is the copy of the bmRequestType of a setup packetUSBbRequest is the copy of the bRequest of a setup packetUsBwValues isdefinedastype:uint16_t_uint8_tandcanbeaccessedthrough3macros:#define USBwValue USBwValues.w#define USBwValueo USBwValues.bw.bbo#define UsBwValuelUSBwValues.bw.bblUsBwValueisthecopyofthewValueofasetuppacketUSBwValueoisthe lowbyteofwValue,andUSBwValue1isthehighbyteofwValue.UsBwlndexs isdefinedasUSBwValuesandcanbeaccessedby3macros:#define USBwIndexUSBwIndexs.w#define USBwIndex0 USBwIndexs.bw.bb0#defineUSBwIndexl USBwIndexs.bw.bblUsBwlndex is the copy of the wlndex of a setuppacketUSBwlndexois the lowbyteof wlndex,andUSBwlndex1is thehighbyteofwindex.USBwLengths is definedas type:uint16_t_uint8_tand can be accessedthrough3macros:#define USBwLength USBwLengths.w#define USBwLengtho USBwLengths.bw.bb0#defineUSBwLengthl USBwLengths.bw.bblUsBwLengthisthecopyofthewLengthofasetuppacketUSBwLengthoandUSBwLength1arethelowandhighbytesofwLength,respectively.ControlState is the state of the core,the available values are defined inCONTROL_STATECurrent_Feature is the device feature at any time. It is affectedby theSET_FEATUREandCLEAR_FEATURErequestsandretrievedbytheGET_STATUSrequest.Usercodedoesnotusethisfield.Current_Configuration is theconfiguration thedevice is working on atanytime-It issetand retrievedbytheSET_CONFIGURATIONandGET_CONFIGURATIONrequests,respectively.Current_Interfaceis the selected interface.Current_Aiternatesetting is the alternative settingwhichhasbeen selectedforthe current working configuration and interface.It is set and retrieved by theSET_INTERFACEandGET_INTERFACErequests,respectively.Ctrl_InfohastypeENDPOINT_INFO.Sincethis structureis used everywhere inthe library,a global variableplnformationisdefinedforeasyaccesstotheDevice_Infotable,it is apointertotheDEVICE_INFOstructure.Actually,plnformation=&Device_Info.STDocID13465Rev1219/85
UM0424 STM32 USB-FS-Device firmware library Doc ID 13465 Rev 12 19/85 Description of the structure fields: – USBbmRequestType is the copy of the bmRequestType of a setup packet – USBbRequest is the copy of the bRequest of a setup packet – USBwValues is defined as type: uint16_t_uint8_t and can be accessed through 3 macros: #define USBwValue USBwValues.w #define USBwValue0 USBwValues.bw.bb0 #define USBwValue1 USBwValues.bw.bb1 USBwValue is the copy of the wValue of a setup packet USBwValue0 is the low byte of wValue, and USBwValue1 is the high byte of wValue. – USBwIndexs is defined as USBwValues and can be accessed by 3 macros: #define USBwIndex USBwIndexs.w #define USBwIndex0 USBwIndexs.bw.bb0 #define USBwIndex1 USBwIndexs.bw.bb1 USBwIndex is the copy of the wIndex of a setup packet USBwIndex0 is the low byte of wIndex, and USBwIndex1 is the high byte of wIndex. – USBwLengths is defined as type: uint16_t_uint8_t and can be accessed through 3 macros: #define USBwLength USBwLengths.w #define USBwLength0 USBwLengths.bw.bb0 #define USBwLength1 USBwLengths.bw.bb1 USBwLength is the copy of the wLength of a setup packet USBwLength0 and USBwLength1 are the low and high bytes of wLength, respectively. – ControlState is the state of the core, the available values are defined in CONTROL_STATE. – Current_Feature is the device feature at any time. It is affected by the SET_FEATURE and CLEAR_FEATURE requests and retrieved by the GET_STATUS request. User code does not use this field. – Current_Configuration is the configuration the device is working on at any time. It is set and retrieved by the SET_CONFIGURATION and GET_CONFIGURATION requests, respectively. – Current_Interface is the selected interface. – Current_Alternatesetting is the alternative setting which has been selected for the current working configuration and interface. It is set and retrieved by the SET_INTERFACE and GET_INTERFACE requests, respectively. – Ctrl_Info has type ENDPOINT_INFO. Since this structure is used everywhere in the library, a global variable pInformation is defined for easy access to the Device_Info table, it is a pointer to the DEVICE_INFO structure. Actually, pInformation = &Device_Info
STM32USB-FS-Devicefirmware libraryUM04243.DevicepropertystructureThe USBcore dispatches the control to the user program whenever it is necessary.User handling procedures aregiven in an array of Device_Property.The structure hasthetype:DEVICE_PROP:typedef struct_DEVICE_PROPvoid(*Init)(void);void (*Reset) (void);void (*Process_Status_IN)(void);void (*Process_status_OuT)(void);RESULT(*Class_Data_Setup)(uint8_t RequestNo);RESULT(*class_NoData_Setup) (uint8_t RequestNo);RESULT(*class_Get _Interface Setting) (uint8_t Interface,uint8_tAlternatesetting);uint8_t*(*GetDeviceDescriptor)(uint16_t Length);uint8_t*(*GetConfigDescriptor)(uinti6_t Length);uint8_t* (*GetstringDescriptor)(uinti6_t Length);void* RxEp buffer; /* This field is not used in current library version.It is kept onlyfor compatibility with previous versions*/uint8_t MaxPacketsize;1DEVICE_PROP;4.UserstandardrequeststructureThe User Standard Request Structure is the interface between the user code and theofthestandardThestructurehasthemanagementrequest.type:USER_STANDARD_REQUESTS:typedef struct_USER_STANDARD_REQUESTSvoid(*User_GetConfiguration)(void);void(*User_SetConfiguration)(void);void(*User_GetInterface)(void);void(*User setInterface)(void);void(*User_Getstatus)(void);void(*User_clearFeature)(void);void(*User_SetEndPointFeature)(void):void(*User_SetDeviceFeature)(void);void(*UserSetDeviceAddress)(void);USER STANDARD REQUESTS;If the userwantsto implement specific codeafter receiving a standard USB DevicerequesthehastousethecorrespondingfunctionsinthisstructureAnapplicationdevelopermustimplementthreestructureshavingtheDEvicE_PROPDevice_TableandUSER_STANDARD_REQUESTtypes inorderto manage classrequests and application specific controls.The different fields of these structures aredescribed in Section 3.3.4:usb_type.h/usb_def.h.SA20/85DocID13465Rev12
STM32 USB-FS-Device firmware library UM0424 20/85 Doc ID 13465 Rev 12 3. Device property structure The USBcore dispatches the control to the user program whenever it is necessary. User handling procedures are given in an array of Device_Property. The structure has the type: DEVICE_PROP: typedef struct _DEVICE_PROP { void (*Init)(void); void (*Reset)(void); void (*Process_Status_IN)(void); void (*Process_Status_OUT)(void); RESULT (*Class_Data_Setup)(uint8_t RequestNo); RESULT (*Class_NoData_Setup)(uint8_t RequestNo); RESULT (*Class_Get_Interface_Setting)(uint8_t Interface,uint8_t AlternateSetting); uint8_t* (*GetDeviceDescriptor)(uint16_t Length); uint8_t* (*GetConfigDescriptor)(uint16_t Length); uint8_t* (*GetStringDescriptor)(uint16_t Length); void* RxEP_buffer; /* This field is not used in current library version. It is kept only for compatibility with previous versions */ uint8_t MaxPacketSize; } DEVICE_PROP; 4. User standard request structure The User Standard Request Structure is the interface between the user code and the management of the standard request. The structure has the type: USER_STANDARD_REQUESTS: typedef struct _USER_STANDARD_REQUESTS { void(*User_GetConfiguration)(void); void(*User_SetConfiguration)(void); void(*User_GetInterface)(void); void(*User_SetInterface)(void); void(*User_GetStatus)(void); void(*User_ClearFeature)(void); void(*User_SetEndPointFeature)(void); void(*User_SetDeviceFeature)(void); void(*User_SetDeviceAddress)(void); } USER_STANDARD_REQUESTS; If the user wants to implement specific code after receiving a standard USB Device request he has to use the corresponding functions in this structure. An application developer must implement three structures having the DEVICE_PROP, Device_Table and USER_STANDARD_REQUEST types in order to manage class requests and application specific controls. The different fields of these structures are described in Section 3.3.4: usb_type.h / usb_def.h