UM0463
User manual
ST7 full-speed USB library
Introduction
The "ST7 Full-Speed USB Library" (hereafter "the library") is designed for ST7 full-speed
USB products. With the library, the user of the ST7265 and ST7SCR will have easy access
to the embedded USB cell. The library is provided with full source code and it can be used
for any USB application. The library also provides an optional feature to upgrade the
firmware of the microcontroller through the USB. This document describes the use and the
implementation of the library.
■
Definitions:
–
–
–
–
USB Universal Serial Bus
HID
IAP
Human Interface Devices
In-Application Programming
DFU Device Firmware Update
January 2008
Rev 1
1/30
www.st.com
Contents
UM0463
Contents
1
Overview . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
1.1
1.2
Functionality of the library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Background of ST7 full-speed USB device . . . . . . . . . . . . . . . . . . . . . . . . 5
1.2.1
USB device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
2
Programming model of the library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
2.1
2.2
2.3
Handling control endpoint 0 . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 7
Transactions on noncontrol endpoints . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
Special note on MCU interrupt . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 8
3
The USB state machine of the control endpoint . . . . . . . . . . . . . . . . . . 9
3.1
3.2
3.3
3.4
The states . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
Data structure for the SETUP packet . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Standard requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Nonstandard requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
3.4.1
3.4.2
SETUP stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Data stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 13
Data IN stage with continued data buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Data IN stage with noncontinued data buffer . . . . . . . . . . . . . . . . . . . . . . . . . . . .13
Data OUT stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .14
3.4.3
Status stage . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5
Processing of standard requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 14
3.5.1
Get status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Recipient of the device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Recipient of the interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Recipient of the endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
3.5.2
3.5.3
Clear feature and set feature . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Recipient of device . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 15
Recipient of interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .15
Recipient of endpoint . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
3.5.4
3.5.5
Set address . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Get descriptor . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 16
Constant descriptors. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .16
Nonconstant descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
Standard request with constant descriptors . . . . . . . . . . . . . . . . . . . . . . . . . . . . .17
2/30
UM0463
3.5.6
3.5.7
3.5.8
Contents
Set configuration and get configuration . . . . . . . . . . . . . . . . . . . . . . . . . 17
Set interface and get interface . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 17
Status stage of the standard requests . . . . . . . . . . . . . . . . . . . . . . . . . . 18
3.6
The execution of the state machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 18
4
Data transfer on noncontrol endpoints . . . . . . . . . . . . . . . . . . . . . . . . . 20
4.1
4.2
4.3
Sending data to the host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Receiving data from the host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 20
Handling noncontrol endpoints directly . . . . . . . . . . . . . . . . . . . . . . . . . . 21
4.3.1
4.3.2
4.3.3
4.3.4
4.3.5
Endpoint status registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 21
DMA counter registers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
DMA buffers . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Sending data to the host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
Receiving data from the host . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 22
5
USB control functions for nondata transfer . . . . . . . . . . . . . . . . . . . . . 23
5.1
5.2
5.3
Library initialization . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Device reset . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
Start of frame . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 23
6
7
Device firmware upgrade support . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 24
The library package and its configuration . . . . . . . . . . . . . . . . . . . . . . 25
7.1
7.2
7.3
The contents of the package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Configuration of the library package . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 26
Application interface to the library . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
8
9
10
Comments about the sample project . . . . . . . . . . . . . . . . . . . . . . . . . . 28
References . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 28
Revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
3/30
List of tables
UM0463
List of tables
Table 1.
Table 2.
Table 3.
Table 4.
Table 5.
Table 6.
Table 7.
Table 8.
Table 9.
Table 10.
Endpoint configuration . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
Endpoint status . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 5
USB events . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 6
States of USB state machine . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 9
SETUP packet fields . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 10
Bitmap of flag field in sUSB_vSetup . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Standard requests . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 11
Code structure . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 25
Call back functions . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 27
Document revision history . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . 29
4/30
UM0463
Overview
1
1.1
Overview
Functionality of the library
As a generic USB library, it provides the following functionalities:
●
●
●
●
●
●
Initialize the USB hardware cell
Answer all standard USB requests defined in Chapter 9 of USB specification [1]
Enumerate the USB device to the host with less user code intervention
Means for plug-in user code to handle nonstandard USB requests
Functions for sending and/or receiving data on all the endpoints
Optional DFU functionality without any user code intervention
1.2
1.2.1
Background of ST7 full-speed USB device
USB device
The ST7 full-speed USB device implements several USB endpoints. The number of
endpoints varies among products.
Table 1
lists the endpoint configuration of each product:
Table 1.
Endpoint
ST7265
ST7SCR
Endpoint configuration
0
16
8
1 IN
16
8
1 OUT
16
N/A
2 IN
64
64
2 OUT
64
64
3 IN
N/A
8
4 IN
N/A
8
5 IN
N/A
8
The figures in the above table represent the maximum packet size allowed for a particular
endpoint. An endpoint is identified with its address and its direction. The endpoint in address
0 is the control endpoint and is always bidirectional. In the table, IN identifies that the
direction of the endpoint is from the device to the host and OUT identifies that the direction
of the endpoint is from the host to the device.
The data exchange on endpoints is done through endpoint DMA buffers. The endpoint DMA
buffers are located in the fixed location of RAM area and their locations vary among
products.
There is a pair of registers corresponding to each endpoint. They are the endpoint status
register and the DMA counter register. The endpoint status register identifies four status of
the endpoint:
Table 2.
Status
DISABLE
STALL
Endpoint status
Meaning
The endpoint does not answer any bus traffic.
The endpoint stalls any IN token for IN direction or OUT token for OUT direction.
5/30