UM0424STM32USB-FS-Devicefirmware libraryAs seen in Figure 1, the USB-FS-Device library is divided into two layers:STM32_USB-FS_Device_Driver:thislayermanagesthedirectcommunicationwiththeUSB-FS_Deviceperipheral andtheUSBstandardprotocol.TheSTM32_USB-FS_Device_DriveriscompliantwiththeUSB2.0specificationandisseparatefromthestandardSTM32standardperipheral libraryApplicationInterfacelayer:thislayerprovidestheuserwithacomplete interfacebetween the library core and the final application.Note:The USB-FS peripheral interfacelayer isloaded (throughdefines atcompile time)andusedas theperipheral interface layer.Theapplicationinterfacelayerandthefinalapplicationcancommunicatewiththestandardperipherals librarytomanagethehardwareneedsoftheapplication.Adetailed description of these layers with coding rules isprovided in the next sections.Figure2showsthepackageorganizationoftheUSB-FS-Device librarywithall thedemonstrationsand subfolders.Figure 2.USB-FS-Device librarypackage organizationSTM32_USB-FS-Device_LibhtmrescLibraries: Contains the USB-FS Device driver and the standardLibrariesperipherals librariesDLCMSISSTM32_USB-FS-Device_DriverSTM32F10x_StdPeriph_DriverSTM32F30x_StdPeriph_DriverSTM32F37x_StdPeriph_DriverpSTM32L1x_StdPeriph_DriverProjectsProjects:Contains the workspaces and the sourcesfiles forthepAudio_SpeakerexamplesgivenwiththepackageDCDC_LoopBackComposite_ExamplebCustom_HIDDevice_Firmware_UpgradepJoyStickMousepMass_StorageUtilities.Contains the STM32 drivers relativeto the usedboards (SDVirtual_COM_Portcard,buttons.joystick,etc)Utilities-MSV31505V1S11/85DocID13465Rev12
UM0424 STM32 USB-FS-Device firmware library Doc ID 13465 Rev 12 11/85 As seen in Figure 1, the USB-FS-Device library is divided into two layers: – STM32_USB-FS_Device_Driver: this layer manages the direct communication with the USB-FS_Device peripheral and the USB standard protocol. The STM32_USBFS_Device_Driver is compliant with the USB 2.0 specification and is separate from the standard STM32 standard peripheral library – Application Interface layer: this layer provides the user with a complete interface between the library core and the final application. Note: The USB-FS peripheral interface layer is loaded (through defines at compile time) and used as the peripheral interface layer. The application interface layer and the final application can communicate with the standard peripherals library to manage the hardware needs of the application. A detailed description of these layers with coding rules is provided in the next sections. Figure 2 shows the package organization of the USB-FS-Device library with all the demonstrations and subfolders. Figure 2. USB-FS-Device library package organization MSv31505V1 MSxxxxx Vy
STM32USB-FS-Device firmware libraryUM04243.2USB-FS_DeviceperipheralinterfaceTable4presentstheUSB-FS_DeviceperipheralinterfacemodulesTable 4.USB-FS_DeviceperipheralinterfacemodulesFileDescriptionusb_reg (.h, c)Hardwareabstraction layerusb_int.cCorrect transfer interrupt service routineusb_mem(.h,.c)Data transfermanagement (from/topacket memoryarea)3.2.1usb_reg(.h, .c)The usb_regs module implements the hardware abstraction layer, it offers a set of basicfunctionsforaccessing the USB-FS_Deviceperipheral registers.Note:Theavailablefunctionshavetwocallversions:As a macro: the call is:_NameofFunction(parameter1, .)..As a subroutine: the call is:NameofFunction(parameter1,..)CommonregisterfunctionsThe functions in Table 5 can be used to set or get the various common USB-FS_Deviceperipheral registers.Table 5.Common registerfunctionsRegisterFunctionvoid SetCNTR (uint16_t wValue)CNTRuint16_tGetCNTR(void)void SetIsTR (uintl6t wValue)ISTRuint16.tGetISTR(void)FNRuint16_tGetFNR(void)(uint16_tvoidSetDADDRwValue)DADDRuint16t GetDADDR(void)void SetBTABLE(uint16_twValue)BTABLEuint16_t GetBTABLE(void)S12/85DocID13465Rev12
STM32 USB-FS-Device firmware library UM0424 12/85 Doc ID 13465 Rev 12 3.2 USB-FS_Device peripheral interface Table 4 presents the USB-FS_Device peripheral interface modules. l 3.2.1 usb_reg(.h, .c) The usb_regs module implements the hardware abstraction layer, it offers a set of basic functions for accessing the USB-FS_Device peripheral registers. Note: The available functions have two call versions: ● As a macro: the call is: _NameofFunction(parameter1, .) ● As a subroutine: the call is: NameofFunction(parameter1, .) Common register functions The functions in Table 5 can be used to set or get the various common USB-FS_Device peripheral registers. Table 4. USB-FS_Device peripheral interface modules File Description usb_reg (.h, .c) Hardware abstraction layer usb_int.c Correct transfer interrupt service routine usb_mem(.h,.c) Data transfer management (from/to packet memory area) Table 5. Common register functions Register Function CNTR void SetCNTR (uint16_t wValue) uint16_t GetCNTR (void) ISTR void SetISTR (uint16_t wValue) uint16_t GetISTR (void) FNR uint16_t GetFNR (void) DADDR void SetDADDR (uint16_t wValue) uint16_t GetDADDR (void) BTABLE void SetBTABLE (uint16_t wValue) uint16_t GetBTABLE (void)
UM0424STM32USB-FS-Devicefirmware libraryEndpointregisterfunctionsAll operationswith endpoint registerscan beobtained withtheSetENDPOINTandGetENDPoINTfunctions.However,manyfunctionsarederivedfromthesetooffertheadvantage of adirect actionon aspecific field.a)Endpointset/get valueSetENDPOINT:void SetENDPOINT(uint8_t bEpNum,uint16_t wRegValue)bEpNum = Endpoint number,wRegValue = Value to writeGetENDPOINT:uint16_t GetENDPOINT(uint8_tbEpNum)bEpNum = Endpoint numberreturn value: the endpoint register valueb)Endpoint TYPE fieldTheEP_TYPEfieldof theendpointregistercanassumethedefinedvaluesbelow:#define EP_BULK(0x0000)// Endpoint BULK#define EP_CONTROL(0x0200)// Endpoint CONTROL(0x0400)#defineEP_ISOCHRNOUS// Endpoint ISOCHRONOUS#define EP_INTERRUPT(0x0600)// Endpoint INTERRUPTSetEPType:void SetEPType (uint8_t bEpNum, uinti6_t wtype)bEpNum = Endpoint number, wtype = Endpoint type (value from theabovedefine's)uint16_t GetEPType(uint8_tbEpNum)GetEPType:bEpNum = Endpoint numberreturn value: a value from the above define'sc)Endpoint STATUSfieldTheSTAT_TX/STAT_RXfieldsof theendpoint registercanassumethedefinedvalues below:#define EP_TX_DIS(0x0000)// EndpointTX DISabled(0x0010)#defineEP_TX_STALL// Endpoint TX STALLed(0x0020)#defineEP_TX_NAK// Endpoint TX NAKed(0x0030)#define EP_TX_VALID// Endpoint TX VALID#define EP_RX_DIS(0x0000)// Endpoint RX DISabled(0x1000)#defineEPRX STALL// Endpoint RX STALLed(0x2000)#define EP_RX NAK// Endpoint RX NAKed#define EP_RX_VALID(0x3000)// Endpoint RX VALIDSetEPTxStatus :void SetEPTxStatus(uint8_t bEpNum,uint16_t wState)SetEPRxStatus :void SetEPRxstatus(uint8_t bEpNum,uint16_t wstate)bEpNum = Endpoint number, wstate = a value from the above define'sGetEPTxstatus :uintl6_t GetEPTxstatus(uint8_t bEpNum)GetEPRxStatus :uint16_t GetEPRxstatus (uint8_t bEpNum)bEpNum = endpoint numberreturn value:a value from the above define'sS13/85DocID13465Rev12
UM0424 STM32 USB-FS-Device firmware library Doc ID 13465 Rev 12 13/85 Endpoint register functions All operations with endpoint registers can be obtained with the SetENDPOINT and GetENDPOINT functions. However, many functions are derived from these to offer the advantage of a direct action on a specific field. a) Endpoint set/get value SetENDPOINT : void SetENDPOINT(uint8_t bEpNum,uint16_t wRegValue) bEpNum = Endpoint number, wRegValue = Value to write GetENDPOINT : uint16_t GetENDPOINT(uint8_t bEpNum) bEpNum = Endpoint number return value: the endpoint register value b) Endpoint TYPE field The EP_TYPE field of the endpoint register can assume the defined values below: #define EP_BULK (0x0000) // Endpoint BULK #define EP_CONTROL (0x0200) // Endpoint CONTROL #define EP_ISOCHRNOUS (0x0400) // Endpoint ISOCHRONOUS #define EP_INTERRUPT (0x0600) // Endpoint INTERRUPT SetEPType : void SetEPType (uint8_t bEpNum, uint16_t wtype) bEpNum = Endpoint number, wtype = Endpoint type (value from the above define’s) GetEPType : uint16_t GetEPType (uint8_t bEpNum) bEpNum = Endpoint number return value: a value from the above define’s c) Endpoint STATUS field The STAT_TX / STAT_RX fields of the endpoint register can assume the defined values below: #define EP_TX_DIS (0x0000) // Endpoint TX DISabled #define EP_TX_STALL (0x0010) // Endpoint TX STALLed #define EP_TX_NAK (0x0020) // Endpoint TX NAKed #define EP_TX_VALID (0x0030) // Endpoint TX VALID #define EP_RX_DIS (0x0000) // Endpoint RX DISabled #define EP_RX_STALL (0x1000) // Endpoint RX STALLed #define EP_RX_NAK (0x2000) // Endpoint RX NAKed #define EP_RX_VALID (0x3000) // Endpoint RX VALID SetEPTxStatus : void SetEPTxStatus(uint8_t bEpNum,uint16_t wState) SetEPRxStatus : void SetEPRxStatus(uint8_t bEpNum,uint16_t wState) bEpNum = Endpoint number, wState = a value from the above define’s GetEPTxStatus : uint16_t GetEPTxStatus(uint8_t bEpNum) GetEPRxStatus : uint16_t GetEPRxStatus(uint8_t bEpNum) bEpNum = endpoint number return value:a value from the above define’s
STM32USB-FS-Devicefirmware libraryUM0424d)EndpointKIND fieldSetEP_KINDvoid SetEP KIND(uint8 t bEpNum):ClearEP_KIND:void ClearEP KIND(uint8 t bEpNum)bEpNum = endpoint numberSet_status_Out:void Set_Status_Out(uint8_t bEpNum)clear_status_out::void Clear Status_Out(uint8_t bEpNum)bEpNum = endpoint numberSetEPDoubleBuffvoid SetEPDoubleBuff(uint8_t bEpNum)ClearEPDoubleBuffvoid ClearEPDoubleBuff(uint8_t bEpNum):bEpNum = endpoint numberTransfer Rx/TxfieldsCorrectClearEP_CTR_RXvoidClearEP_CTR_RX(uint8_t bEpNum):ClearEP_CTR_TX :void ClearEP_CTR_TX(uint8_t bEpNum)bEpNum = endpoint numbere)Data Toggle Rx/Tx fieldsToggleDTOG_RX :void ToggleDTOG_RX(uint8_t bEpNum)ToggleDTOG_TX:void ToggleDTOG_TX(uint8_t bEpNum)bEpNum = endpoint numberf)Address fieldSetEPAdreSSvoid SetEPAddress(uint8_t bEpNum,uint8_t bAddr):bEpNum = endpoint numberbAddr= address to be setGetEPAdress : uint8 t GetEPAddress(uint8 t bEpNum)bEpNum = endpoint numberS14/85DocID13465Rev12
STM32 USB-FS-Device firmware library UM0424 14/85 Doc ID 13465 Rev 12 d) Endpoint KIND field SetEP_KIND : void SetEP_KIND(uint8_t bEpNum) ClearEP_KIND : void ClearEP_KIND(uint8_t bEpNum) bEpNum = endpoint number Set_Status_Out : void Set_Status_Out(uint8_t bEpNum) Clear_Status_Out : void Clear_Status_Out(uint8_t bEpNum) bEpNum = endpoint number SetEPDoubleBuff : void SetEPDoubleBuff(uint8_t bEpNum) ClearEPDoubleBuff : void ClearEPDoubleBuff(uint8_t bEpNum) bEpNum = endpoint number Correct Transfer Rx/Tx fields ClearEP_CTR_RX : void ClearEP_CTR_RX(uint8_t bEpNum) ClearEP_CTR_TX : void ClearEP_CTR_TX(uint8_t bEpNum) bEpNum = endpoint number e) Data Toggle Rx/Tx fields ToggleDTOG_RX : void ToggleDTOG_RX(uint8_t bEpNum) ToggleDTOG_TX : void ToggleDTOG_TX(uint8_t bEpNum) bEpNum = endpoint number f) Address field SetEPAdress : void SetEPAddress(uint8_t bEpNum,uint8_t bAddr) bEpNum = endpoint number bAddr = address to be set GetEPAdress : uint8_t GetEPAddress(uint8_t bEpNum) bEpNum = endpoint number
UM0424STM32USB-FS-Devicefirmware libraryBufferdescriptiontablefunctionsThese functions are used in orderto set orgetthe endpoints'receive and transmit bufferaddresses andsizes.a)Tx/Rxbufferaddress fieldsSetEPTxAddr :void SetEPTxAddr(uint8_t bEpNum,uint16_t wAddr);SetEPRxAddr : void SetEPRxAddr(uint8 t bEpNum,uint16_t wAddr):bEpNum = endpoint numberwAddr = address to be set (expressed as PMA buffer address)GetEPTxAddr:uint16_t GetEPTxAddr(uint8_t bEpNum);GetEPRxAddr : uint16_t GetEPRxAddr(uint8_t bEpNum);bEpNum = endpoint numberreturn value : address value (expressed as PMA buffer address)b)Tx/RxbuffercounterfieldsSetEPTxCount : void SetEPTxCount(uint8_t bEpNum,uintl6_t wCount);SetEPRxCount :void SetEPRxCount(uint8_t bEpNum,uint16_t wCount);bEpNum = endpoint numberwCount = counter to be setGetEPTxCount : uint16_t GetEPTxCount(uint8_t bEpNum);GetEPRxCount : uint16_t GetEPRxCount(uint8_t bEpNum);bEpNum = endpoint numberreturn value : counter valueDouble-bufferedendpointsfunctionsToobtainhighdata-transferthroughput inbulkorisochronousmodes,double-bufferedmodehas to beprogrammed.Inthis operatingmodesomefields of the endpoint registers andbufferdescriptiontablecellshavedifferentmeanings.Toeasetheuseofthisfeatureseveralfunctionshavebeendeveloped.SetEPDoubleBuff:Anendpointprogrammedtowork inbulkmodecanbesetasOdouble-bufferedby settingtheEP-KINDbit.Thefunction SetEPDoubleBuff()accomplishesthistask:SetEPDoubleBuff :Void SetEPDoubleBuff(uint8t bEpNum);bEpNum =endpoint numberFreeUserBuffer:Indouble-buffered mode,the endpointsbecomemono-directionalandbufferdescription table cells of theunuseddirection areappliedtohandlea secondbuffer.Addressesand counters must behandled in adifferent way.RxandTxAddressesandcounter cells become BufferoandBuffer1cells.Functionsdedicatedtothis operatingmodeareprovidedfor inthe library.During a bulk transferthe linefills onebufferwhiletheotherbufferisreserved totheapplication.Auserapplication hastoprocessdatabefore thearrival ofbulk needingabuffer.Thebufferreserved totheapplicationhastobe freed in time.Tofreethebufferin usefromthe application,theFreeUserBufferfunction isprovided:FreeUserBuffer: void FreeUserBuffer(uint8_t bEpNum,uint8_tbDir);bEpNum=endpoint numberSTDoc ID13465Rev1215/85
UM0424 STM32 USB-FS-Device firmware library Doc ID 13465 Rev 12 15/85 Buffer description table functions These functions are used in order to set or get the endpoints’ receive and transmit buffer addresses and sizes. a) Tx/Rx buffer address fields SetEPTxAddr : void SetEPTxAddr(uint8_t bEpNum,uint16_t wAddr); SetEPRxAddr : void SetEPRxAddr(uint8_t bEpNum,uint16_t wAddr); bEpNum = endpoint number wAddr = address to be set (expressed as PMA buffer address) GetEPTxAddr : uint16_t GetEPTxAddr(uint8_t bEpNum); GetEPRxAddr : uint16_t GetEPRxAddr(uint8_t bEpNum); bEpNum = endpoint number return value : address value (expressed as PMA buffer address) b) Tx/Rx buffer counter fields SetEPTxCount : void SetEPTxCount(uint8_t bEpNum,uint16_t wCount); SetEPRxCount : void SetEPRxCount(uint8_t bEpNum,uint16_t wCount); bEpNum = endpoint number wCount = counter to be set GetEPTxCount : uint16_t GetEPTxCount(uint8_t bEpNum); GetEPRxCount : uint16_t GetEPRxCount(uint8_t bEpNum); bEpNum = endpoint number return value : counter value Double-buffered endpoints functions To obtain high data-transfer throughput in bulk or isochronous modes, double-buffered mode has to be programmed. In this operating mode some fields of the endpoint registers and buffer description table cells have different meanings. To ease the use of this feature several functions have been developed. ● SetEPDoubleBuff: An endpoint programmed to work in bulk mode can be set as double-buffered by setting the EP-KIND bit. The function SetEPDoubleBuff() accomplishes this task: SetEPDoubleBuff : void SetEPDoubleBuff(uint8_t bEpNum); bEpNum = endpoint number ● FreeUserBuffer: In double-buffered mode, the endpoints become mono-directional and buffer description table cells of the unused direction are applied to handle a second buffer. Addresses and counters must be handled in a different way. Rx and Tx Addresses and counter cells become Buffer0 and Buffer1 cells. Functions dedicated to this operating mode are provided for in the library. During a bulk transfer the line fills one buffer while the other buffer is reserved to the application. A user application has to process data before the arrival of bulk needing a buffer. The buffer reserved to the application has to be freed in time. To free the buffer in use from the application, the FreeUserBuffer function is provided: FreeUserBuffer: void FreeUserBuffer(uint8_t bEpNum, uint8_t bDir); bEpNum = endpoint number