Académique Documents
Professionnel Documents
Culture Documents
Table of Contents
1. Scope............................................................................................................................................ 3
2. USB theory of operation ............................................................................................................ 4
3. Hardware overview .................................................................................................................. 20
4. Software requirements.............................................................................................................. 26
1.
Scope
This document provides detailed software design consideration for the Universal Serial Bus (USB) for
TM4C1294 Controller with the capabilities of operating as a Full speed device which runs at 12
Mbits/sec.
1.1
Document Overview
2.1
II.
The Universal Serial Bus is host controlled, there can only be one host per bus. The USB
host is responsible for undertaking all transactions and scheduling bandwidth. Data can be
sent by various transaction methods using a token-based protocol.
III.
Up to 127 devices can be connected to any one USB bus at any one given time.
IV.
USB uses 4 shielded wires of which two are power (+5v & GND). The remaining two are
twisted pair differential data signals. It uses a NRZI (Non Return to Zero Invert) encoding
scheme to send data with a sync field to synchronise the host and receiver clocks.
V.
VI.
VII.
USB uses a NRZI (Non Return to Zero Invert) encoding scheme to send data with sync field
to synchronise the host and receiver clocks.
VIII.
USB supports plugnplug with dynamically loadable and unloadable drivers. The user
simply plugs the device into the bus. The host will detect this addition, interrogate the
newly inserted device and load the appropriate driver all in the time it takes the hourglass
to blink on your screen provided a driver is installed for your device.
IX.
The loading of the appropriate driver is done using a PID/VID (Product ID/Vendor ID)
combination. The VID is supplied by the USB Implementor's forum at a cost and this is seen
as another sticking point for USB.
X.
Once the user is finished, they can simply lug the cable out, the host will detect its absence
and automatically unload the driver.
XI.
USB uses a differential transmission pair for data. This is encoded using NRZI and is bit
stuffed to ensure adequate transitions in the data stream. On low and full speed devices, a
differential 1 is transmitted by pulling D+ over 2.8V with a 15K ohm resistor pulled to
ground and D- under 0.3V with a 1.5K ohm resistor pulled to 3.6V. A differential 0 on the
other hand is a D- greater than 2.8V and a D+ less than 0.3V with the same appropriate pull
down/up resistors. The receiver defines a differential 1 as D+ 200mV greater than D- and a
differential 0 as D+ 200mV less than D-.
XII.
A USB device will enter suspend when there is no activity on the bus for greater than
3.0ms. It then has a further 7ms to shutdown the device and draw no more than the
designated suspend current and thus must be only drawing the rated suspend current from
the bus 10mS after bus activity stopped.
XIII.
Standard internal wire colours are used in USB cables, making it easier to identify wires
from manufacturer to manufacturer.
Pin number
1
2
3
4
2.2
Cable color
Red
White
Green
Black
Function
Vbus (5 volt)
DD+
Ground
USB Protocols
Data on the USBus is transmitted LSBit first. USB packets consist of the following fields,
a) Sync
All packets must start with a sync field. The sync field is 8 bits long at low and full speed or 32
bits long for high speed and is used to synchronize the clock of the receiver with that of the
transmitter. The last two bits indicate where the PID fields starts.
b) PID
PID stands for Packet ID. This field is used to identify the type of packet that is being sent. The
following table shows the possible values.
c) ADDR
The address field specifies which device the packet is designated for. Being 7 bits in length
allows for 127 devices to be supported. Address 0 is not valid, as any device which is not yet
assigned an address must respond to packets sent to address zero.
d) ENDP
The endpoint field is made up of 4 bits, allowing 16 possible endpoints. Low speed devices,
however can only have 2 additional endpoints on top of the default pipe. (4 endpoints max).
e) CRC
Cyclic Redundancy Checks are performed on the data within the packet payload. All token
packets have a 5 bit CRC while data packets have a 16 bit CRC.
f) EOP
End of packet. Signalled by a Single Ended Zero (SE0) for approximately 2 bit times followed
by a J for 1 bit time.
2.3
PID
ADDR
ENDP
CRC5
EOP
PID
Data
CRC16
EOP
a)
b)
b)
NAK - Reports that the device temporary cannot send or received data. Also
used during interrupt transactions to inform the host there is no data to send.
c)
STALL - The device finds its in a state that it requires intervention from the
host.
PID
EOP
The SOF packet consisting of an 11-bit frame number is sent by the host every 1ms 500ns on
a full speed bus or every 125 s 0.0625 s on a high speed bus.
Sync
2.5
PID
Frame Number
CRC5
EOP
USB Functions
As shown in the figure most functions will have a series of buffers, typically 8 bytes long. Each buffer
will belong to an endpoint - EP0 IN, EP0 OUT etc. for example, the host sends a device descriptor
request. The function hardware will read the setup packet and determine from the address field whether
the packet is for itself, and if so will copy the payload of the following data packet to the appropriate
endpoint buffer dictated by the value in the endpoint field of the setup token. It will then send a
handshake packet to acknowledge the reception of the byte and generate an internal interrupt within the
semiconductor/micro-controller for the appropriate endpoint signifying it has received a packet. This is
typically all done in hardware.
The software now gets an interrupt, and should read the contents of the endpoint buffer and parse the
device descriptor request.
2.6
Endpoints
Endpoints can be described as sources or sinks of data. As the bus is host centric, endpoints occur at the
end of the communications channel at the USB function. At the software layer, your device driver may
send a packet to your devices EP1 for example. As the data is flowing out from the host, it will end up
in the EP1 OUT buffer. Your firmware will then at its leisure read this data. If it wants to return data,
the function cannot simply write to the bus as the bus is controlled by the host. Therefore it writes data
to EP1 IN which sits in the buffer until such time when the host sends a IN packet to that endpoint
requesting the data. Endpoints can also be seen as the interface between the hardware of the function
device and the firmware running on the function device.
All devices must support endpoint zero. This is the endpoint which receives all of the devices control
and status requests during enumeration and throughout the duration while the device is operational on
the bus.
2.7
Transfer Types
Each USB transfer consists of one or more transactions that carry data to or from an endpoint. There
are 4 types of transfers defined for the endpoints.
1. Control Transfers
2. Interrupt Transfers
3. Isochronous Transfers
4. Bulk Transfers
Control Transfers
I.
Control transfers are typically used for command and status operations. They are essential to set
up a USB device with all enumeration functions being performed using control transfers.
II.
The packet length of control transfers in high speed devices allow a packet size of 16, 32 or 64
bytes and full speed devices must have a packet size of 64 bytes.
III.
B. The optional Data Stage consists of one or multiple IN or OUT transfers. The setup request
indicates the amount of data to be transmitted in this stage. The data stage has two different
scenarios depending upon the direction of data transfer.
1. IN: When the host is ready to receive control data it issues an IN Token. If the function
receives the IN token with an error, then it ignores the packet. If the token was received
correctly, the device can either reply with a DATA packet containing the control data to
be sent, a stall packet indicating the endpoint has had a error or a NAK packet indicating
to the host that the endpoint is working, but temporarily has no data to send.
2. OUT: When the host needs to send the device a control data packet, it issues an OUT
token followed by a data packet containing the control data as the payload. If any part of
the OUT token or data packet is corrupt then the function ignores the packet. If the
function's endpoint buffer was empty and it has clocked the data into the endpoint buffer
it issues an ACK informing the host it has successfully received the data. If the endpoint
buffer is not empty due to processing of the previous packet, then the function returns a
NAK. However if the endpoint has had a error and its halt bit has been set, it returns a
STALL.
C Status Stage reports the status of the overall request and this once again varies due to
direction of transfer. Status reporting is always performed by the function.
1. IN: If the host sent IN token(s) during the data stage to receive data, then the host must
acknowledge the successful receipt of this data. This is done by the host sending an OUT
token followed by a zero length data packet. The function can now report its status in the
handshaking stage. An ACK indicates the function has completed the command is now
ready to accept another command. If an error occurred during the processing of this
command, then the function will issue a STALL. However if the function is still processing,
it returns a NAK indicating to the host to repeat the status stage later.
2. OUT: If the host sent OUT token(s) during the data stage to transmit data, the function
will acknowledge the successful receipt of data by sending a zero length packet in response
to an IN token. However if an error occurred, it should issue a STALL or if it is still busy
processing data, it should issue a NAK asking the host to retry the status phase later.
Interrupt Transfers
I. Interrupt transfers are useful when data has to transfer without delay. Typical applications include
keyboards, pointing devices, game controllers and hub status reports.
II. The maximum data payload size for low-speed devices is 8 bytes, full speed devices can have
maximum payload size of 64 bytes and maximum payload size of high speed devices is 1024 bytes.
III. Under Interrupt transfers if a device requires the attention of the host, it must wait until the host
polls it before it can report that it needs urgent attention.
A. IN: The host will periodically poll the interrupt endpoint. This rate of polling is specified in
the endpoint descriptor. Each poll will involve the host sending an IN Token. If the IN token is
corrupt, the function ignores the packet and continues monitoring the bus for new tokens. If an
interrupt has been queued by the device, the function will send a data packet containing data
relevant to the interrupt when it receives the IN Token. Upon successful receipt at the host, the
host will return an ACK. However if the data is corrupted, the host will return no status. If an
error has occurred on this endpoint, a STALL is sent in reply to the IN token instead.
B. OUT: When the host wants to send the device interrupt data, it issues an OUT token
followed by a data packet containing the interrupt data. If any part of the OUT token or data
packet is corrupt then the function ignores the packet. If the function's endpoint buffer was
empty and it has clocked the data into the endpoint buffer it issues an ACK informing the host it
has successfully received the data. If the endpoint buffer is not empty due to processing of a
previous packet, then the function returns a NAK. However if an error occurred with the
endpoint consequently and its halt bit has been set, it returns a STALL.
Bulk Transfers
I. Bulk transfers are useful for transferring data when time isnt critical. Uses for bulk transfers
include sending data to a printer or an image generated from a scanner. Bulk transfers provide error
correction which ensures data is transmitted and received without error.
II. A Bulk transfer can send large amounts of data without clogging the bus because the transfers defer
to the other transfer types, waiting until time is available.
III. For full speed endpoints, the maximum bulk packet size is 16, 32 or 64 bytes long. For high speed
endpoints, the maximum packet size can be up to 512 bytes long.
A. IN: When the host is ready to receive bulk data it issues an IN Token. If the function
receives the IN token with an error, it ignores the packet. If the token was received correctly, the
function can either reply with a DATA packet containing the bulk data to be sent, or a stall
packet indicating the endpoint has had a error or a NAK packet indicating to the host that the
endpoint is working, but temporary has no data to send.
B. When the host wants to send the function a bulk data packet, it issues an OUT token
followed by a data packet containing the bulk data. If any part of the OUT token or data packet
is corrupt then the function ignores the packet. If the function's endpoint buffer was empty and
it has clocked the data into the endpoint buffer it issues an ACK informing the host it has
successfully received the data. If the endpoint buffer is not empty due to processing a previous
packet, then the function returns an NAK. However if the endpoint has had an error and its halt
bit has been set, it returns a STALL.
Isochronous Transfers
I. Isochronous transfers are streaming, real-time transfers that are useful when data must arrive at a
constant rate or within a specific time limit and occasional errors are tolerable. Examples of uses for
isochronous transfers include encoded voice and music to be played in real time.
II. This can be up to a maximum of 1023 bytes for a full speed device and 1024 bytes for a high speed
device.
III. The following diagram shows the format of an Isochronous IN and OUT transaction. Isochronous
transactions do not have a handshaking stage and cannot report errors or STALL/HALT conditions.
2.7
USB Descriptors
All USB devices have a hierarchy of descriptors which describe to the host information such as what
the device is, who makes it, what version of USB it supports, how many ways it can be configured, the
number of endpoints and their types etc
The more common USB descriptors are:
1. Device Descriptors
2. Configuration Descriptors
3. Interface Descriptors
4. Endpoint Descriptors
5. String Descriptors
USB devices can only have one device descriptor. The device descriptor includes information such as
what USB revision the device complies to, the Product and Vendor IDs used to load the appropriate
drivers and the number of possible configurations the device can have. The number of configurations
indicate how many configuration descriptors branches are to follow.
The configuration descriptor specifies values such as the amount of power this particular configuration
uses, if the device is self or bus powered and the number of interfaces it has. When a device is
enumerated, the host reads the device descriptors and can make a decision of which configuration to
enable. It can only enable one configuration at a time.
The configuration settings are not limited to power differences. Each configuration could be powered
in the same way and draw the same current, yet have different interface or endpoint combinations.
However it should be noted that changing the configuration requires all activity on each endpoint to
stop. While USB offers this flexibility, very few devices have more than 1 configuration.
B. Configuration descriptor
1. The configuration descriptor contains information about the devices power
requirements and the number of interfaces it supports.
2. Each configuration descriptor has subordinate descriptors, including one or more
interface descriptors and optional endpoint descriptors.
C. Interface descriptor
1. The interface descriptor provides information about a function or feature that a
device implements. The descriptor contains class, subclass, and protocol information
and the number of endpoints the interface uses.
2. A configuration can have multiple interfaces that are active at the same time. Each
interface has its own interface descriptor and subordinate descriptors.
D. Endpoint descriptor
1. The endpoint descriptor transfers the configuration details of each endpoint
supported in a given interface. The descriptor carries details of the transfer type
supported, the maximum packet size, the endpoint number and the polling rate if it is an
interrupt pipe.
2. Endpoint zero never has a descriptor because every device must support endpoint
zero.
E. String descriptor
1. String descriptors provide human readable information and are optional. If they are
not used, any string index fields of descriptors must be set to zero indicating there is no
string descriptor available.
II. The following figure shows the hierarchy of descriptors which are transferred to the PC during the
device enumeration procedure.
3
3.1
HW overview
Universal Serial Bus (USB) Controller
Figure 3.1 Shows the USB Module Block Diagram for TM4C129XNCZAD microcontroller.
3.3
Signal Description
The table 3.2 lists the external signals of the USB controller and describes the function of each.
3.4
Functional Description
3.4.1.1 Endpoints
operating as a Device, the USB controller provides:
1. two dedicated control endpoints (IN and OUT) and
2. 14 configurable endpoints (7 IN and 7 OUT).
Can be used for communications with a Host controller. The endpoint number and direction associated
with an endpoint is directly related to its register designation.
Endpoint 0 uses the first 64 bytes of the USB controller's FIFO RAM as a shared memory for both IN
and OUT transactions. The remaining 14 endpoints can be configured as control, bulk, interrupt, or
isochronous endpoints. They should be treated as 7 configurable IN and 7 configurable OUT endpoints.
The endpoint pairs are not required to have the same type for their IN and OUT endpoint configuration.
3.4.1.2 IN Transactions as a Device
When operating as a USB Device, data for IN transactions is handled through the FIFOs attached to the
transmit endpoints. The sizes of the FIFOs for the 7 configurable IN endpoints are determined by the
USB Transmit FIFO Start Address register. The maximum size of a data packet that may be placed in a
transmit endpoint's FIFO for transmission is programmable and is determined by the value written to
the USB Maximum Transmit Data Endpoint register for that endpoint. The endpoint's FIFO can also be
from the Host once every millisecond. When the SOF packet is received, the 11-bit frame number
contained in the packet is written into the USB Frame Value (USBFRAME) register, and an SOF
interrupt is also signaled and can be handled by the application.
3.4.1.7 USB RESET
When the USB controller is in Device mode and a RESET condition is detected on the USB bus, the
USB controller automatically performs the following actions:
1. Clears the USBFADDR register.
2. Clears the USB Endpoint Index (USBEPIDX) register.
3. Flushes all endpoint FIFOs.
4. Clears all control/status registers.
5. Enables all endpoint interrupts.
6. Generates a RESET interrupt.
3.4.2 Operation as a Host
When the TM4C1294NCZAD USB controller is operating in Host mode, it can either be used for
point-to-point communications with another USB device or, when attached to a hub, for
communication with multiple devices.
Note: the functionality of the Host in Device mode, endpoint, IN and OUT Transactions is similar to
that of Device.
3.4.2.1 Transaction Scheduling
Scheduling of transactions is handled automatically by the USB Host controller. The Host controller
allows configuration of the endpoint communication scheduling based on the type of endpoint
transaction. Interrupt transactions can be scheduled to occur in the range of every frame to every 255
frames in 1 frame increments.
3.4.2.2 Babble
The USB Host controller does not start a transaction until the bus has been inactive for at least the
minimum inter-packet delay. The controller also does not start a transaction unless it can be finished
before the end of the frame. If the bus is still active at the end of a frame, then the USB Host controller
assumes that the target Device to which it is connected has malfunctioned, and the USB controller
suspends all transactions and generates a babble interrupt.
3.4.2 ULPI Interface
The ULPI PHY interface is a reduced pin-count interface conforms to the UTMI+ specification. The
reduction in pin count is achieved by allowing the relatively static UTMI+ signals to be accessed
through registers and by providing a bi-directional data bus both for the USB data and for accessing
register data on the ULPI transceiver.
3.4.3 USB DMA Controller
The USB includes an integrated eight-channel USB DMA controller for efficient loading/unloading of
the endpoint FIFOs. Any Tx Endpoints 1 through 7 and Rx Endpoints 1 through 7 can be assigned to
DMA channels 0 through 7. The DMA can operate in two modes and can handle packet sizes up to 8K.
In addition, the DMA controller can be programmed to conduct transfers using burst of four, eight and
sixteen or bursts of unspecified length.
3.4.4 DMA Operation
The DMA transfer is particularly useful when large blocks of data are to be transferred through a Bulk
endpoint. The USB protocol requires that large data blocks are transferred by sending a series of
packets of the maximum packet size for endpoint (512 bytes for high speed, 64 bytes for full speed).
The last packet in the series may be less than the maximum packet size. The receiver may use the
reception of this short packet to signal the end of the transfer. The DMA may be used in either Device
mode or Host mode to avoid the overhead of having to interrupt the processor after each individual
packet, and instead, only interrupting the processor after the transfer has completed.
SW Requirements
4.1
SysCtlClockFreqSet();
uint32_t SysCtlClockFreqSet(uint32_t ui32Config, uint32_t ui32SysClock);
This function configures the system clocking to 120 MHz with a 480 MHz PLL.
param ui32Config = 480MHz
param ui32SysClock = 120MHz
Return: The actual configured system clock frequency in Hz or zero if the value could not be changed
due to a parameter error or PLL lock failure.
PinoutSet();
Void PinoutSet(void)
This function enables the GPIO modules and configures the device pins for USB operation.
Used to configures PL6 as DP and PL7 as DM.
USBStackModeSet ( );
Void USBStackModeSet(uint32_t ui32Index, tUSBMode iUSBMode, tUSBModeCallback
pfnCallback)
Allows dual mode application to switch to device modes.
Param ui32Index = 0, specifies the USB controller whose mode of operation is to be set.
Param iUSBMode = 0, indicates the mode that the application wishes to operate = 0, for device mode
Param pfnCallback is a pointer to a function which the USB library will call each time the mode is
changed to indicate the new operating mode.
USBDHIDCompositeInit( );
void * USBDHIDCompositeInit(uint32_t ui32Index, tUSBDHIDDevice
*psHIDKbDevice, tCompositeEntry *psCompEntry)
Initializes HID device operation.
Param ui32Index = 0, is the index of the USB controller which is to be initialized for HID device
operation.
Param psHIDKbDevice = descriptors, points to a structure containing device, configuration, interface
and endpoint descriptors customizing the operation of the HID device.
Param psCompEntry=0, is the composite device entry to initialize when creating a composite device.
Returns zero on failure or a non-zero instance value that should be used with the remaining USB HID
APIs.
USBDCDConfigGetInterfaceEndpoint( );
tEndpointDescriptor *USBDCDConfigGetInterfaceEndpoint(const tConfigHeader *psConfig, uint32_t
ui32InterfaceNumber, uint32_t ui32Index)
Return a pointer to the n-th endpoint descriptor in a particular interface within a configuration
descriptor.
Param psConfig= pointer to descriptor, points to the header structure for the configuration descriptor
that is to be searched.
Param ui32InterfaceNumber = ptr to device information, is the interface number whose endpoint is to
be found.
Param ui32Index = 0 for device descriptor and 1 for other descriptor, is the zero based index of the
endpoint that is to be found within the appropriate alternate setting for the interface.
Returns a pointer to the requested endpoint descriptor if found or NULL otherwise.
USBDCDDeviceInfoInit( );
Void USBDCDDeviceInfoInit(uint32_t ui32Index, tDeviceInfo *psDeviceInfo)
Initialize an instance of the tDeviceInfo structure.
Param ui32Index = 0, is the index of the USB controller which is to be initialized.
Param psDeviceInfo = pointer to descriptors.
MAP_SysCtlPeripheralReset();
Void SysCtlPeripheralReset(uint32_t ui32Peripheral).
Performs a software reset of a peripheral.
Param ui32Peripheral = 0xf0002800 for USB 0, is the peripheral to reset.
USBDevLPMDisable( );
Void USBDevLPMDisable(uint32_t ui32Base)
Disables the USB controller from responding to LPM suspend requests.
Param ui32Base = 0x40050000 for USB 0, specifies the USB module base address.
USBLibDMAInit( );
tUSBDMAInstance *USBLibDMAInit(uint32_t ui32Index)
This function is used to initialize the DMA interface for a USB instance.
param ui32Index = 0, is the index of the USB controller for this instance.
Returns a pointer to use with USBLibDMA APIs.
USBDevConnect( );
Void USBDevConnect(uint32_t ui32Base)
Connects the USB controller to the bus in device mode.
Param ui32Base = 0x40050000 for USB 0, specifies the USB module base address.
IntEnable( );
IntEnable(uint32_t ui32Interrupt)
The specified interrupt is enabled in the interrupt controller.
Param ui32Interrupt = MemManage, bus fault, usage fault, or general interrupt to be enabled.
4.2
4.3
When data arrives from the host, the device will receive an interrupt from the host. The
interrupt handler must perform the following steps on reception:
1. Read the USB Receive Interrupt Status register USBRXIS.
2. Identify the interrupt.
3. Call the appropriate handler (i.e. Read Data handler).
4. Read handler checks whether the RX Packet Ready bit is set.
5. Check the number of byte available in the buffer.
6. Read the data to buffer USB FIFO endpoint.
7. Clear the RX Packet Ready bit.
8. Give the read data to the application.
4.3
When the device side application needs to send data to the host, the device must follow the below steps.
1. Application calls the write data API and passes the data buffer to the CDC (communication
device class) layer.
2. Write data API checks the amount of data to be sent
3. Copy data to the TX buffer
4. Set the TX Packet Ready bit
5. After the transmit operation completes, the host sends a TX completion interrupt
6. TX Packet Ready bit will be cleared automatically
7. After receiving the TX Complete Interrupt, the ISR sends an event to the application indicating
the completion of data transfer
4.4
Registers configuration
The set of registers that are to be configured for the correct operation of the system is as described
below:
Note: only registers that need change are indicated below. All other registers that do not need any
change from default values are not described. The detailed bit allocation of each register can be known
from the user manual of the controller and are not described.
Register 1: USBFADDR
USB Device Functional Address contains the 7-bit address of the Device part of the transaction.
Base 0x4005.0000
Offset 0x000
Type RW
Reset value: 0x00
Initialization value: Function Address of Device as received through SET_ADDRESS.
When the USB controller is being used in Device mode, this register must be written with the address
received through a SET_ADDRESS command, which is then used for decoding the function address in
subsequent token packets.
Register 2: USBPOWER
USBPOWER is an 8-bit register used for controlling SUSPEND and RESUME signaling.
Base 0x4005.0000
Offset 0x001
Type RW,
Reset value: 0x20
Initialization value: 0x00
Full speed mode is enabled by disabling high speed mode.
Register 22: USBTXFIFOSZ
USB Transmit Dynamic FIFO Sizing is an 8-bit registers that allow the selected TX/RX endpoint
FIFOs to be dynamically sized.
Base 0x4005.0000
Offset 0x062
Type RW,
Reset value: 0x00
Initialization value: 0x03
Max Packet Size selected as 64 bytes.
Note: USBRXFIFOSZ configuration is same as above.
Register 82 to 88: USBTXMAXP1 to USBTXMAXP7
The USB Maximum Transmit Data Endpoint is a 16-bit register that defines the maximum amount of
data that can be transferred through the transmit endpoint in a single operation.
Base 0x4005.0000
Offset 0x110
Type RW,
Reset 0x0000
Initialization value: Maximum Payload in bytes per transaction.
Register 165: USBDMACTL0 to USBDMACTL7
USB DMA Control register provides the DMA transfer control for each channel. The enabling, transfer
direction, transfer mode, the DMA burst modes are all controlled by this register.
Reset value: 0x00
Initialization value: 0x08
DMA mode and DMA interrupt mode is enabled.