USB Device using STM32

 What is a universal synchronous bus USB?

USB was designed to standardize the connection of peripherals to personal computers, both to communicate with and to supply electric power. It has largely replaced interfaces such as serial ports and parallel ports and has become commonplace on a wide range of devices.

Examples of peripherals that are connected via USB include computer keyboards and mice, video cameras, printers, portable media players, mobile (portable) digital telephones, disk drives, and network adapters.

USB connectors have been increasingly replacing other types as charging cables of portable devices.

USB 1.x:

Released in January 1996, USB 1.0 specified signalling rates of 1.5 Mbit/s (Low Bandwidth or Low Speed) and 12 Mbit/s (Full Speed).


USB 2.0:

The USB2.0 supports three speeds:

  • Low speed (LS): supports the transfer rate of 1.5 Mb/s. This speed is mainly dedicated to interactive devices (such as mouse, keyboards)
  • Full speed (FS): supports the transfer rate of 12 Mb/s. This speed is mainly dedicated to phone and audio devices (such as microphones, speakers)
  • High speed (HS): supports the transfer rate of 480 Mb/s. This speed is mainly dedicated to video and storage devices (such as printers, cameras).

USB Connectors:
Available receptacles for each connector
ConnectorsUSB 1.0
1996
USB 1.1
1998
USB 2.0
2001
USB 2.0
Revised
USB 3.0
2008
USB 3.1
2014
USB 3.2
2017
USB4
2019
Data rate1.5 Mbit/s
(Low Speed)
12 Mbit/s
(Full Speed)
1.5 Mbit/s
(Low Speed)
12 Mbit/s
(Full Speed)
480 Mbit/s
(High Speed)
5 Gbit/s
(SuperSpeed)
10 Gbit/s
(SuperSpeed+)
20 Gbit/s
(SuperSpeed+)
40 Gbit/s
(SuperSpeed+, Thunderbolt 3 and 4)
StandardAType A
USB Type-A receptacle.svg
Type A
USB 3.0 Type-A receptacle blue.svg
Deprecated
BType B
USB Type-B receptacle.svg
Type B
USB 3.0 Type-B receptacle blue.svg
Deprecated
CN/AType C (enlarged)
USB Type-C Receptacle Pinout.svg
MiniAN/AMini A
USB Mini-A receptacle.svg
Deprecated
BMini B
USB Mini-B receptacle.svg
ABN/AMini AB
USB Mini-AB receptacle.svg
MicroAN/AMicro A
Micro A
USB 3.0 Micro-A.svg
Deprecated
BN/AMicro B
Micro B
USB 3.0 Micro-B receptacle.svg
Deprecated
ABN/AMicro AB
USB Micro-AB receptacle.svg
Micro AB
USB micro AB SuperSpeed.png
Deprecated
ConnectorsUSB 1.0
1996
USB 1.1
1998
USB 2.0
2001
USB 2.0
Revised
USB 3.0
2011
USB 3.1
2014
USB 3.2
2017
USB4
2019


USB Topology:


USB physical interconnect is characterized by a tired star topology. 
Each star has a hub at the center with one upstream connection directly or indirectly with the host and one or more downstream connection(s) with function or other hubs.
A maximum of 127 devices (functions or hubs) can be connected to one host (root hub) with a maximum of 5 hubs connected in series.

Detecting a Device has been Connected: 

A USB interface consists of 4 wires. Power, Ground, Data Plus (USBDP) and Data Minus (USBDM). 
A USB host port with no devices connected uses 15kohm resistors to connect both USB DP and USB DM to GND. 
When a USB device (sometimes referred to as a slave) is plugged into a USB host there is a change on these USB data lines. It is this change that the USB host uses to detect a device has been connected. This change is also used to identify the speed of device attached.

Determining the Device Speed :

A low-speed USB device (1.5Mbps) uses a 1k5 pull-up resistor to VCC on the USB DM signal line.






A full-speed USB device (12Mbps) uses a 1k5 pull-up resistor to VCC on the USB DP signal line. 
A high-speed USB device (480Mbps) will initially appear as a full-speed device to the host.
The first thing the USB host does is to attempt to send /receive packets at high speed to the USB device. This is known as J and K chirp and if communication is successful it will be assumed that the USB device is a high-speed device. 
If this initial communication fails then the USB host assumes that the device is a full speed device. 
This means a high-speed device has a 1k5 pull-up resistor on USB DP that can be switched in / out of the circuit.
A J state is defined as a differential signal on USBDP and USB DM >= +300mV. 
A K state is defined as a differential signal on USBDP and USB DM >= -300mV.

Bit stuffing:

After six consecutive 1's in data USB hardware inserts '0' as the 7th bit to ensure that no DC signal in the differential line.


NRZI
  • Toggle the voltage value for 0 
  •  Keep the voltage value for 1




USB signal names with signal levels

USB signal names with signal levels
USB Enumeration: 

USB Enumeration is the process of detecting, identifying and loading drivers for a USB device.
It is the procedure ensuring the control of the device status changes and the real-time management of any device attachment and detachment. During this step, there is a combination of hardware and software negotiation allowing the host to decode the device nature. At the device software level, this procedure ensures the correct reception and decoding of the host request and then device state modification accordingly.

Device status:

From being completely detached until being completely recognized by the USB Host and ensuring its function, the USB device goes through consecutive stages:

  • Attached: it is the stage when the device is physically connected to the USB host but not yet powered. This stage is mainly ensured by hardware.
  • Powered: it is the second stage and is corresponding to a device that was attached to USB Host and just powered. This stage is mainly ensured by hardware.
  • Default: this stage is reached when the attached device is powered then reset by the host. This stage is assigned to the device by its software each time the device is newly attached then powered then reset or an old attached device received a reset. At this stage, the USB Device operates with convenient speed (selected by hardware during reset) and has the default address which is the address number 0.
  • Addressed: after going through all the previous stages, the USB device reaches this state by receiving its unique address (different from 0) from the host. This stage is reached after the correct process of the host request by the device software.
  • Configured: the device reaches this stage after receiving the convenient request from the host with a non-zero configuration number. This stage is reached after the correct process of the host request by the device software.
  • Suspended: the device must enter this stage if there is no data on the traffic for a known period that depends on the speed. In fact, the host forces the device to enter this state electrically depending on its speed. When detecting this electrical indication, the USB device software must change its state into suspended.

Power

Generally, the host (root hub) provides power for functions direct connection. Some hubs may supply power for directly connected downstream functions. For the functions, there are two types:

  • Bus powered functions: these devices rely totally on the bus power coming from the upstream hub.
  • Self powered functions: these devices are capable of providing their own power independently from the bus.
Types of Data transfer:

The USB communication is based on four main transfer types:

  • Control transfer: used for the configuring of the newly attached device.
  • Bulk transfer: used for large amounts of data transmission or reception.
  • Interrupt transfer: used for limited data transmission with minimal latency.
  • Isochronous transfer: used for data transfer with real-time requirements.

Endpoints:

It is a source or destination data buffer that must be implemented on the device side. Each amount of data that is received from or sent to the host will be placed into an endpoint. Each endpoint is uniquely characterized by a number and direction which means that for a given number, there is a unique pair of endpoints of the same type and number but each one deals with the data of only one direction:

  • IN Endpoint for data that will be transferred into the host.
  • OUT Endpoint for data that will be transferred from the host.

A device has always more than one endpoint pair identified by their numbers:

  • Endpoint zero: it is a pair of OUT and IN endpoints dedicated for control data transfers which means it is a control endpoint. It is required to establish the first communication transfers between the host and the device while the other endpoints are not yet configured.
  • Other endpoints: will be configured after negotiation between the host and the device. Each endpoint is independent from the other and can handle a different transfer type.





  •  Each device/host has multiple endpoints 
  •  The endpoint is a virtual communication channel between device and host 
  •  Each endpoint can have a different type of USB transaction 
    •  SETUP 
    •  BULK 
    •  Interrupt 
    • Isochronous 
  •  Endpoint 0 is reserved for enumeration & configuration of the USB device
  •  Max. 16 Endpoints (bidirectional), depends on HW

Packet Structure


SYNC:

A USB packet begins with an 8-bit synchronization sequence, 00000001₂. That is, after the initial idle state J, the data lines toggle KJKJKJKK. The final 1 bit (repeated K state) marks the end of the sync pattern and the beginning of the USB frame. For high-bandwidth USB, the packet begins with a 32-bit synchronization sequence.

PID:

The Packet identifier has 8bits width, the first four bits can identify the packet type and the last 4 bits is a mirror of the first 4 bits.

Address:

7 bit address that specfies the address of device.

Endpoint:
4 -bits 

CRC 
5 bits - checksum of the packet.

USB introduction
The USB peripheral implements an interface between a full-speed USB 2.0 bus and the APB1 bus. 
USB suspend/resume are supported which allows to stop the device clocks for low-power consumption.

USB main features

  • USB specification version 2.0 full-speed compliant 
  • Configurable number of endpoints from 1 to 8 
  • Cyclic redundancy check (CRC) generation/checking, Non-return-to-zero Inverted (NRZI) encoding/decoding and bit-stuffing 
  • Isochronous transfers support 
  • Double-buffered bulk/isochronous endpoint support 
  • USB Suspend/Resume operations 
  • Frame locked clock pulse generation 
In low, medium, high and XL-density devices, the USB and CAN share a dedicated 512-byte SRAM memory for data transmission and reception, and so they cannot be used concurrently (the shared RAM is accessed through CAN and USB exclusively). The USB and CAN can be used in the same application but not at the same time. 

Functional description:











 

Comments

Popular posts from this blog

Diodes