Page 1
ST 32-bit USB microcontrollers (STR71x, STR75x and STR91x). The aim of the STR7/9 USB developer kit is to use the same certified USB library across the STR7/STR9 microcontroller families and present for each at least one firmware demo per USB transfer type.
UM0290 STR7/STR9 USB firmware library STR7/STR9 USB firmware library This section describes the firmware interface (called USB Library) used to manage the STR7/9 USB 2.0 full-speed macrocell. The main purpose of this firmware library is to provide resources to ease the development of applications using the USB macrocell in all STR7/9 microcontrollers (STR71x, STR75x and the STR91x families).
STR7/STR9 USB firmware library UM0290 A detailed description of these two layers with coding rules is provided in the next two sections. USB library core Table 1 presents the USB library core modules: Table 1. USB library core modules File Description Types used in the library core.
UM0290 STR7/STR9 USB firmware library 1.2.2 usb_reg(.c, .h) The usb_regs module implements the hardware abstraction layer, it offers a set of basic functions for accessing the USB macrocell registers. Note: The available functions have two call versions: - As a macro: the call is: _NameofFunction(parameter1,...)
Page 8
STR7/STR9 USB firmware library UM0290 Endpoint register functions: All operations related to endpoint registers can be performed with the SetENDPOINT and GetENDPOINT functions. However, several functions are derived from these to permit direct action on a specific field. Endpoint set/get value...
Page 10
STR7/STR9 USB firmware library UM0290 Double-buffered endpoint 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.
Page 11
UM0290 STR7/STR9 USB firmware library wCount); bEpNum = endpoint number bDir = endpoint direction wCount = buffer counter GetEPDblBuf0Count : u16 GetEPDblBuf0Count(u8 bEpNum); GetEPDblBuf1Count : u16 GetEPDblBuf1Count(u8 bEpNum); bEpNum = endpoint number return value : buffer counter Double buffer STATUS...
Page 12
STR7/STR9 USB firmware library UM0290 Note: In unlinked mode the DMA interface doesn’t mask/clear any CTR_TX/CTR_RX interrupt (the CPU is responsible for this task). – IN Endpoint: In unlinked mode only three endpoints can be mapped on the DMA channel using...
Page 13
UM0290 STR7/STR9 USB firmware library chains) before enabling the DMA and, on termination of the DMA transfer (terminal count interrupt from the DMAC). – IN Endpoint: void DMALinkedModeTxConfig(u8 bEpNum): Configure a IN endpoint to trigger DMA Tx linked mode request.
STR7/STR9 USB firmware library UM0290 1.2.3 usb_int (.c , .h) The usb_int module handles the correct transfer interrupt service routines; it offers the link between the USB protocol events and the library core. The STR7 USB IP provides two correct transfer routines: ●...
Page 15
UM0290 STR7/STR9 USB firmware library u8 Current_Configuration; u8 Current_Interface; u8 Current_AlternateSetting; ENDPOINT_INFO Ctrl_Info; } DEVICE_INFO; A union u16_u8 is defined to easily access some fields in the DEVICE_INFO in either u16 or u8 format. typedef union { u16 w; struct BW { u8 bb1;...
Page 16
STR7/STR9 USB firmware library UM0290 Description of the structure fields: – USBbmRequestType is the copy of bmRequestType of a setup packet – USBbRequest is the copy of bRequest of a setup packet – USBwValues is defined as type: WORD_BYTE and can be accessed through 3 macros: #define USBwValue USBwValues.w...
UM0290 STR7/STR9 USB firmware library Device Property Structure: The USB core 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);...
STR7/STR9 USB firmware library UM0290 Table 3. Application interface modules File Description usb_istr (.c,.h) USB interrupt handler functions usb_conf.h USB configuration file usb_prop (.c, .h) USB application specific properties usb_endp.c CTR interrupt handlers routines for non control endpoints usb_pwr (.h, .c) USB power management module usb_desc (.c, .h)
Page 19
UM0290 STR7/STR9 USB firmware library Device property implementation The device property structure fields are described below: – void Init(void): Init procedure of the USB IP. It is called once at the start of the application to manage the initialization process.
STR7/STR9 USB firmware library UM0290 Device table implementation Description of the structure fields: – Total_Endpoint is the number of endpoints the USB application uses. – Total_Configuration is the number of configurations the USB application has. USER_STANDARD_REQUEST implementation This structure is used to manage the user implementation after receiving all standard requests (except Get descriptors).
UM0290 STR7/STR9 USB firmware library Implementing a USB application using the STR7/9 USB library 1.4.1 Implementing a no data class specific request All class-specific requests without a data transfer phase implement the field RESULT (*Class_NoData_Setup)(BYTE RequestNo) of the structure device property.
STR7/STR9 USB firmware library UM0290 pInformation->Ctrl_Info.CopyData = CopyRoutine; pInformation->Ctrl_Info.Usb_wOffset = 0; (*CopyRoutine)(0); return USB_SUCCESS; } /*End of Class_Data_Setup */ 1.4.3 How to manage data transfers in a non-control endpoint The management of data transfer using a pipe other than the default one (Endpoint 0) can be managed in the file usb_endpoint.c.
UM0290 Joystick mouse demo Joystick mouse demo A USB mouse (Human Interface Device class) is a simple example of a complete USB application. The joystick mouse uses only one interrupt endpoint (endpoint 1 in the IN direction). After normal enumeration, the host requests the HID report descriptor of the mouse.
For more details on how to use the PC applet of the custom HID, please refer to the UM0551user manual “USB HID demonstrator” available from the STMicroelectronics microcontroller website www.st.com. Descriptor topology The custom HID topology is based on two interrupt pipes used to handle the data transfer for seven different reports.
UM0290 Custom HID Each Report descriptor is related to a specific component in the evaluation board (LEDs, push button or ADC). The following section described the functionality of these reports. Custom HID implementation 3.3.1 LEDs control The STR7xx/91x evaluation boards have four LEDs. In the Custom HID demo each LED correspond to a specific report (report 1 to 4) and the LEDs states (ON/OFF) are set by the PC applet.
Device firmware upgrade UM0290 Device firmware upgrade General description This part of the document presents the implementation of a device firmware upgrade (DFU) capability in the microcontroller. It follows the DFU class specification defined by the USB Implementers Forum for reprogramming an application through USB. The DFU principle is particularly well suited to USB applications that need to be reprogrammed in the field.
UM0290 Device firmware upgrade 4.2.2 Phases There are four distinct phases required to accomplish a firmware upgrade: Enumeration: The device informs the host of its capabilities. A DFU class-interface descriptor and associated functional descriptor embedded within the device’s normal run-time descriptors serve this purpose and provide a target for class-specific requests over the control pipe.
Device firmware upgrade UM0290 DFU mode selection The host should be able to enumerate a device with DFU capability in two ways: ● as a single device with only DFU capability ● as a composite device: HID, Mass storage, or any functional class, and with DFU capability.
Page 29
See interface. bDeviceClass See interface. bDeviceSubClass See interface. bDeviceProtocol Maximum packet size for endpoint zero. 8,16,32, bMaxPacketSize0 64 bytes for the STR7 and STR9 devices 0483h Vendor ID idVendor DF11h Product ID idProduct Version of the STMicroelectronics DFU 011Ah bcdDevice...
Page 30
Device firmware upgrade UM0290 DFU mode configuration descriptor This descriptor is identical to the standard configuration descriptor described in the USB specification version 1.0, with the exception that the bNumInterfaces field must contain the value 01h. ● DFU Mode Interface Descriptor This is the descriptor for the only interface available when operating in DFU mode.
Page 31
Device firmware upgrade Another example, for STR91x Flash microcontroller, is shown below: "@Internal Flash 0 /0x00000000/8*064Kg" in case of the STR9 512KB "@Internal Flash 0 /0x00000000/32*064Kg" in case of the STR9 2MB Each Alternate setting string descriptor must follow this memory mapping else the PC Host Software would be able to decode the right mapping for the selected device: –...
Page 32
Device firmware upgrade UM0290 ● DFU Functional descriptor This descriptor is identical for both the run-time and the DFU mode descriptor sets. DFU functional descriptor Table 8. Offset Field Size Value Description bLength Size of this descriptor, in bytes. bDescriptorType DFU FUNCTIONAL descriptor type.
UM0290 Device firmware upgrade Reconfiguration phase Once the operator has identified the device and supplied the filename, the host and the device must negotiate to perform the upgrade. ● The host issues a DFU_DETACH request to Control Endpoint EP0. ● The host issues a USB reset to the device.
Device firmware upgrade UM0290 4.5.2 Special command/protocol descriptions In order to support all features (Address decoding and Memory block to erase etc.) of the DFU Extension implementation from STMicroelectronics, a few format rules are added to the DFU_DNLOAD request. They are defined as shown in Table Table 10.
UM0290 Device firmware upgrade 4.5.3 DFU state diagram Figure 6 summarizes the DFU interface states and the transitions between them. The events that rigger state transitions can be thought of as arriving on multiple “input tapes” as in the classic Turing machine concept. Figure 6.
Device firmware upgrade UM0290 4.5.4 Downloading and uploading The host slices the firmware image file into N pieces and sends them to the device by means of control-write operations in the default endpoint (Endpoint 0). The maximum number of bytes that the device can accept per control-write transaction is specified in the wTransferSize field of the DFU Functional Descriptor.
UM0290 Device firmware upgrade DFU implementation 4.6.1 DFU mode entry mechanism For the STR7/9 devices the DFU mode is entered after an MCU reset if: ● The DFU mode is forced by the user: the user presses the key push-button after a reset.
Mass storage demo UM0290 Mass storage demo The mass storage demo gives a typical example of how to use the STR USB peripheral to communicate with a PC host using bulk transfer. The demo presents two different implementations: one for each internal transfer mode supported by the STR USB IP (simple and double buffered transfer modes).
Page 39
UM0290 Mass storage demo Figure 8. Mass storage file architecture Mass Storage Files Standard Lib Tools Files Standard Lib USB Lib USB Mass Storage Application File Common Commun files Files Simple Buffer Double Buffer Transfer Mode Transfer Mode Simple buffer transfer mode implementation Double buffer transfer mode implementation This implementation uses the internal RAM like memory support.
Mass storage demo UM0290 Mass storage protocol 5.2.1 Bulk Only Transfer (BOT) The BOT protocol uses only bulk pipes to transfer command, status and data (no interrupt or control pipes). The default pipe (pipe 0, or in other words, Endpoint 0) is only used to clear the bulk pipe(s) status (clear STALL status) and to issue the two class specific requests: Mass Storage reset and Get Max LUN.
Page 41
UM0290 Mass storage demo Table 12. CSW packet fields dCSWSignature dCSWTag 8-11 dCSWDataResidue bCSWStatus ● dCSWSignature: 53425355 USBS (little Endian). ● dCSWTag: the device must set this field with the received value of dCBWTag in the related CBW. ● dCSWDataResidue: the difference between the expected data (the value of dCBWDataTransferLength field of the related CBW) and the real value of data received or sent by the device.
Mass storage demo UM0290 Figure 10. BOT state machine Ready Command transport Data OUT Data IN (from the host) (to the host) Status transport 5.2.2 Small Computer System Interface (SCSI) The SCSI command set is designed to provide efficient peer-to-peer operation of SCSI device like, for example, desks, tapes and Mass Storage devices.
UM0290 Mass storage demo Table 14. SCSI Command Set (continued) Command Command Name OpCode Description Reference Support Enable or disable the Logical Unit for Start Stop Unit 0x1B media access operations and controls certain power conditions Test Unit Ready 0x00 Request the device to report if it is ready SPC-2 Verify (10)
Mass storage demo UM0290 Figure 11. Hardware and firmware interaction diagram Standard USB Application USB Library Library USB IP Hardware (STR + evaluation board) Hardware Config interface The hardware configuration layer is represented by the two files HW_config.c and HW_config.h. For the Mass Storage demo, the hardware management layer manages the following hardware requirements: ●...
Page 45
UM0290 Mass storage demo For the bulk pipes (endpoints 1 and 2) the configuration depends on the transfer mode. For the simple buffered mode the endpoint configuration consists of these steps: ● Configure the endpoint 1 as bulk IN ● Configure the endpoint 1 Tx count and data buffer address in the BTABLE (file usb_conf.h) ●...
Mass storage demo UM0290 For more information about the double buffer transfer mode please refer to the STR71x reference manual. 5.3.3 Class specific requests The Mass Storage Class specification describes two class specific requests: Bulk-only Mass Storage reset This request is used to reset the Mass Storage device and its associated interface. This class specific request is to make the device ready for the next CBW sent by the PC host.
UM0290 Mass storage demo 5.3.4 Standard request requirements To be compliant with the BOT specification the device must respond to the two following requirements after receiving same standard requests: ● When the device switches from unconfigured to configured state, the data toggle of all endpoints must be cleared.
Mass storage demo UM0290 5.3.6 SCSI protocol implementation The aim of the SCSI Protocol is to provide a correct response to all SCSI commands needed by the operating system on the PC host. This section details the method of management for all implemented SCSI commands. ●...
UM0290 Mass storage demo 5.3.7 Memory management All the memory management functions are grouped in the two files: memory.c and memory.h. The memory management consists of two basic processes: ● Management and the validation of the address range for the command Read (10) and Write (10): this process is done by the function Address_Management_Test().
Page 50
Mass storage demo UM0290 concerned line and implement their own process. For example, for the need to support the command SCSI_FormatUnit, comment the line: // #define SCSI_FormatUnit_Cmd SCSI_Invalid_Cmd And implement a process in a function with the same name in the file usb_scsi.c: void SCSI_Invalid_Cmd (void) // your implementation In this way the custom function is called automatically by the CBW_Decode() function...
Page 51
UM0290 Mass storage demo Table 16. Configuration Descriptor Field Value Description bLength 0x09 Size of this descriptor in bytes bDescriptortype 0x02 Descriptor type(Configuration descriptor) Total length (in bytes) of the returned data wTotalLength by this descriptor (including interfaces endpoints descriptors) Number of interfaces supported by this bNumInterfaces 0x0001...
Page 52
Mass storage demo UM0290 Table 18. Endpoint descriptors Field Value Description IN ENDPOINT bLength 0x07 Size of this descriptor in bytes bDescriptortype 0x05 Descriptor type(endpoint descriptor) bEndpointAddress 0x81 IN Endpoint address 1. bmAttributes 0x02 Bulk endpoint wMaxPacketSize 0x40 64 bytes bInterval 0x00 Does not apply for bulk endpoints...
UM0290 Virtual COM port demo Virtual COM port demo In modern PCs, USB is the standard communication port for almost all peripherals. However many industrial software applications still use the classic COM Port (UART). The Virtual COM Port Demo provides a simple solution to bypass this problem. It uses the USB as a COM port with affecting the legacy PC application designed for COM Port communication.
Virtual COM port demo UM0290 Software driver installation To install the software driver of the Virtual COM port, perform the following steps: ● Load the application and run it on the evaluation board ● Plug the USB cable into the PC ●...
Page 55
UM0290 Virtual COM port demo UART 0 to the PC through USB and the endpoint 3 (OUT) used to receive the data from the PC and send it to through UART. For more information on the CDC class please refer to the Class Definitions for Communication Devices specification provided by www.usb.org web site.
USB voice demos UM0290 USB voice demos The USB voice demos give examples of how to use the STR USB peripheral to communicate with the PC host in the isochronous transfer Mode. This provides a demonstration of the correct method of configuring an isochronous endpoint, of receiving or transmitting data from/to the host and of how to use this data in a real-time application.
Page 57
UM0290 USB voice demos related functionality. This includes both audio data (analog and digital) and the functionality that is used to directly control the audio environment, such as volume and tone control. All audio devices are regrouped, from a USB point of view, in the audio interface class. This class is divided into several subclasses.
USB voice demos UM0290 STR7/9 USB audio speaker demo The purpose of the USB audio speaker demo is to receive the audio Stream (data) from a PC host using the USB and to play it back via the STR7/9 MCU. Figure 16: STR7/9 USB audio speaker demo data flow represents the data flow between the PC host and the audio...
UM0290 USB voice demos width is proportional to the amplitude of the sample data. The PWM output signal is then integrated by a low-pass filter to remove high frequency components, leaving only the low-frequency content. The output of the low-pass filter provides a reasonable of the original analog signal.
Page 60
USB voice demos UM0290 This ensures that almost the entire audio spectrum is passed through, but the PWM carrier is cut off. Since the cut-off frequency of the low-pass filter is set at 4 kHz, the R and C component Ω...
Page 61
UM0290 USB voice demos Figure 19. Hardware and firmware interaction diagram STRxxx USB Audio speaker Standard USB Application USB Library Library USB IP Hardware (STR + Board) Hardware Configuration interface The Hardware configuration layer is represented by the two files hw_config.c and hw_config.h.
USB voice demos UM0290 to configure an isochronous OUT endpoint (see the file usb_prop.c function STRSpeaker_Reset ()). * Initialize Endpoint 1 */ SetEPType(ENDP1, EP_ISOCHRONOUS); SetEPDblBuffAddr(ENDP1,ENDP1_BUF0Addr,ENDP1_BUF1Addr); SetEPDblBuffCount(ENDP1, EP_DBUF_OUT, 22); ClearDTOG_RX(ENDP1); ClearDTOG_TX(ENDP1); ToggleDTOG_TX(ENDP1); SetEPRxStatus(ENDP1, EP_RX_VALID); SetEPTxStatus(ENDP1, EP_TX_DIS); Class Specific Request This implementation supports only the Mute control. This feature is managed by the function Mute_command (file usb_prop.c).
UM0290 USB voice demos and the trigger source (USB Rx). The function DMAUnlinkedModeRxEnable() is called to enable the DMA Rx unlinked mode. As said before, the STR USB IP handles the isochronous transfer using the double buffer mode. So to manage the transfer of the data received by the device, after each end of DMA transfer (DMA terminal count interrupt) the Switch_DMA_Src_Addr() function is called to set the source address field with the next buffer to use (ENDP1_BUF0Addr or ENDP1_BUF1Addr) and to re-enable the DMA in unlinked data transfer mode.
USB voice demos UM0290 7.4.1 General characteristics ● USB characteristics: – Endpoint 0: used to enumerate the device and to respond to the class specific requests. The maximum packet size of this endpoint is 64 bytes. – Endpoint 1 (IN): This endpoint is used to transfer the audio stream data converted from the STR7/9 to the PC host with a maximum packet size up to 22 bytes.
Page 65
UM0290 USB voice demos In each timer interrupt, the ADC conversion value is stored in a buffer with a size of 22 bytes. This buffer is copied to the PMA (endpoint 1) and sent to the host after receiving the IN token.
Page 66
USB voice demos UM0290 Audio speaker descriptors Table 19. Device descriptors Field Value Description bLength 0x12 Size of this descriptor in bytes bDescriptortype 0x01 Descriptor type(Device descriptor) bcdUSB 0x0200 USB specification Release number: 2.0 bDeviceClass 0x00 Device class bDeviceSubClass 0x00 Device sub class bDeviceProtocol 0x00...
Page 67
UM0290 USB voice demos Table 21. Interface descriptors Field Value Description USB speaker standard interface AC descriptor (Interface 0, Alternate Setting 0) bLength 0x09 Size of this descriptor in bytes bDescriptortype 0x04 Descriptor type: Interface descriptor bInterfaceNumber 0x00 Interface number bAlternateSetting 0x00 Alternate setting number...
Page 68
USB voice demos UM0290 Table 21. Interface descriptors (continued) Field Value Description iChannelNames 0x00 Unused iTerminal 0x00 Unused USB speaker audio feature unit descriptor bLength 0x09 Size of this descriptor in bytes Descriptor type: AUDIO INTERFACE bDescriptortype 0x24 DESCRIPTOR TYPE DescriptorSubtype: AUDIO CONTROL bDescriptorSubtype 0x06...
Page 69
UM0290 USB voice demos Table 21. Interface descriptors (continued) Field Value Description InterfaceProtocol: AUDIO PROTOCOL bInterfaceProtocol 0x00 UNDEFINED iInterface 0x00 Unused USB speaker standard AS interface descriptor - audio streaming operational (Interface 1, Alternate Setting 1) bLength 0x09 Size of this descriptor in bytes Descriptor type: AUDIO INTERFACE bDescriptortype 0x24...
Page 70
USB voice demos UM0290 Table 22. Endpoint descriptors Field Value Description Endpoint 1 - standard descriptor bLength 0x07 Size of this descriptor in bytes bDescriptortype 0x05 Descriptor type (endpoint descriptor) bEndpointAddress 0x01 OUT endpoint address 1. bmAttributes 0x01 Isochronous endpoint wMaxPacketSize 0x0016 22 bytes...
Page 71
UM0290 USB voice demos Table 24. Configuration descriptor Field Value Description bLength 0x09 Size of this descriptor in bytes bDescriptortype 0x02 Descriptor type (Configuration descriptor) Total length (in bytes) of the returned data wTotalLength 0x64 by this descriptor (including interfaces endpoints descriptors) Number of interfaces supported by this bNumInterfaces...
Page 72
USB voice demos UM0290 Table 25. Interface descriptors (continued) Field Value Description bInCollection 0x01 Number of streaming interfaces : 1 baInterfaceNr 0x01 baInterfaceNr: 1 USB microphone input terminal descriptor bLength 0x0C Size of this descriptor in bytes: 12 Descriptor type: AUDIO INTERFACE bDescriptortype 0x24 DESCRIPTOR TYPE...
Page 73
UM0290 USB voice demos Table 25. Interface descriptors (continued) Field Value Description Interface SubClass: AUDIO SUBCLASS bInterfaceSubClass 0x02 AUDIOSTREAMING Interface Protocol: AUDIO PROTOCOL bInterfaceProtocol 0x00 UNDEFINED iInterface 0x00 Unused USB microphone standard AS interface descriptor - audio streaming operational (Interface 1, Alternate Setting 1) bLength 0x09 Size of this descriptor in bytes...
Page 74
USB voice demos UM0290 Table 25. Interface descriptors (continued) Field Value Description SubFrame Size: one byte per audio bSubFrameSize 0x01 subframe bBitResolution 0x08 Bit Resolution: 8 bits per sample bSamFreqType 0x01 One frequency supported tSamFreq 0x0055F0 22 kHz Table 26. Endpoint descriptors Field Value...
UM0290 Revision history Revision history Table 27. Document revision history Date Revision Changes 16-Jan-2007 Initial release. Added Section 3: Custom HID on page 24 13-Nov-2008 Added Section 4: Device firmware upgrade on page 26 75/76...
Page 76
No license, express or implied, by estoppel or otherwise, to any intellectual property rights is granted under this document. If any part of this document refers to any third party products or services it shall not be deemed a license grant by ST for the use of such third party products or services, or any intellectual property contained therein or considered as a warranty covering the use in any manner whatsoever of such third party products or services or any intellectual property contained therein.
Need help?
Do you have a question about the STR9 and is the answer not in the manual?
Questions and answers