Embedded sharing~IO related 5~USB

Here is just a summary of IO control related and this kind of bus, etc. ~ Continue to update the fifth part whaosoft  aiot  http://143ai.com

This time supports USB communication protocol

0 Basic concepts 

    A [transmission] (control, batch, interrupt, etc.): consists of multiple [transactions];

    A [transaction] (IN, OUT, SETUP): consists of more than one [Packet].

    USB data is transferred between [host software] and [USB device-specific endpoint]. The association between [host software] and [USB device-specific endpoints] is called [pipes]. A USB device can have multiple pipes.

1 pack

    Packet (Packet) is the basic unit of information transmission in the USB system, and all data is transmitted on the bus after being packaged. Data transmission on the USB bus is in units of packets, and packets can only be transmitted within frames. The frame period of the high-speed USB bus is 125us, and the frame period of the full-speed and low-speed USB buses is 1ms. The start of the frame is indicated by a specific packet (SOF packet), and the end of the frame is EOF. EOF is not a packet, but a level state, and data transmission is not allowed during EOF.

    Note: Although the frame period of the high-speed USB bus and the full-speed/low-speed USB bus are different, the frame number in the SOF packet increases at the same speed, because in the high-speed USB system, the frame number in the SOF packet is actually obtained from the high value of the counter. 11 bits, the lowest three bits are not used as the micro-frame number, so the increase period of the frame number is also 1mS

  • What is the situation on the USB bus?

 A packet is the smallest unit of data transmission on the USB bus and cannot be interrupted or disturbed, otherwise an error will occur. Several data packets form a transaction transmission, and a transaction transmission cannot be interrupted. Several packets belonging to a transaction transmission must be continuous and cannot be completed across frames. A transfer consists of one or more transaction transfers, which can be completed across frames.

    The USB packet consists of five parts, namely the synchronization field (SYNC), the packet identifier field (PID), the data field, the cyclic redundancy check field (CRC) and the end of packet field (EOP). The basic format of the packet is as follows:

 PID type (ie packet type)

 Token Packets

  This format applies to IN, OUT, SETUP, PING.

    PID data transfer direction:

    IN Device->Host

    OUT Host->Device

    SETUP Host->Device

    PING Device->Host

Start-of-Frame(SOF) Packets

    The SOF packet is sent by the Host to the Device.

  1. For the full-speed bus, it is sent every 1.00 ms ±0.0005 ms;

  2. For the high-speed bus, it is sent every 125 μs ±0.0625 μs;

    The composition of the SOF package is shown in the figure below: 

Data Packets

 There are four types of data packets: DATA0, DATA1, DATA2, and MDATA, and they are distinguished by PID. DATA0 and DATA1 are defined to support data toggle synchronization.

 Handshake Packets

 

  • ACK: For IN transactions, it will be issued by the host; for OUT, SETUP and PING transactions, it will be issued by the device.

  • NAK: In the data phase, for IN transactions, it will be sent by the device; in the handshake phase, for OUT and PING transactions, it will also be sent by the device; the host never sends NAK packets.

Transaction

    The process of receiving or sending data information on the USB is called transaction processing (Transaction), that is: The delivery of service to an endpoint. A transaction is composed of a system packet, and which packets are composed of it depends on the specific transaction. May consist of the following packages:

  • a token packet

  • optional data pcket

  • optional handshake packet

  • optional special packet

 Input (IN) transactions

    Input transaction processing: Indicates the process in which the USB host receives a data packet from a USB device on the bus.

  • [Normal] input transactions

  • Input transaction processing when [device is busy]

  • [Device error] input transaction processing

Output (OUT) transaction processing

    Output transaction processing: Indicates the process in which the USB host outputs a data packet to a certain USB device on the bus for reception.

  • [Normal] output transactions

  • [When the device is busy] output transaction processing

  • [device error] output transaction processing

SETUP transaction processing

  • [Normal] setup transactions

  • [When the device is busy] set transaction processing

Control Transfer

Control transmission consists of 2 to 3 stages:

Each phase consists of one or more (data phase) transaction transfers (Transaction).

The control data is used by the USB system software to configure the device (during enumeration), and other driver software can choose to use control transfer to realize specific functions, and the data transfer cannot be lost.

 build phase

    The host obtains configuration information from the USB device and sets configuration values ​​of the device. The data exchange in the establishment phase includes the SETUP token packet, followed by the DATA0 data packet and the ACK handshake packet. Its function is to perform a set (concept ambiguous) data exchange, and define the content of this control transmission (ie: the number of data packets IN or OUT in the Data Stage, and the sending direction, which have been set in the Setup Stage) .

  • 【Equipment error】setting transaction processing

    USB transfer type

        In USB transmission, four transmission types are defined:

  • Control Transfer

  • Interrupt Transfer

  • Bulk Transfer

  • Synchronous transmission (Isochronous)

  • Build phase (Setup)

  • Data phase (no data control does not have this phase) (DATA)

  • Status

 

data stage

    According to the direction of data transmission in the data phase, control transmission can be divided into 3 types:

  1. Control read (read USB descriptor)

  2. Control write (configure USB device)

  3. no data control

    Data transmission phase: used to transmit data between the host and the device.

  • control read

    It is the data USB device descriptor to read data from the device to the host. The process is shown in [Control Read] in the figure below. For each data packet, first, the host will send an IN token packet, indicating that it wants to read data. Then, the device returns the data to the host through the DATA1/DATA0 data packet. Finally, the host will respond in the following ways: when the data has been received correctly, the host sends an ACK token packet; when the host is busy, it sends a NAK handshake packet; when an error occurs, the host sends a STALL handshake packet .

  • control write

    It is to transfer data from the host to the device, and the transferred data is the configuration information of the USB device. The process is shown in the following figure [Control Wirte]. For each data packet, the host will send an OUT token packet, indicating that the data is to be sent. Immediately afterwards, the host transmits the data to the device through the DATA1/DATA0 data packet. Finally, the device will respond in the following ways: when the data has been received correctly, the device sends an ACK token packet; when the device is busy, the device sends a NAK handshake packet; when an error occurs, the device sends a STALL handshake packet .

status stage

    Status stage: It is used to indicate that the entire transmission process has been completely completed.

    The direction of transmission in the state phase must be opposite to that of the data phase, that is, it was originally an IN token packet, and this phase should be an OUT token packet; otherwise, it was originally an OUT token packet, and this phase should be an IN token packet.

    For [Control Read], the host will send an OUT token packet, followed by a 0-length DATA1 packet. At this time, the device will also take corresponding actions, sending ACK handshake packets, NAK handshake packets or STALL handshake packets.

    In contrast, for [control write] transmission, the host will send an IN token packet, and then the device will send a 0-length DATA1 packet indicating the completion of the status phase, and the host will make corresponding actions: send ACK handshake packet, NAK handshake packet or STALL handshake packet.

Bulk Transfer

    It is used to transmit a large amount of data, and the transmission is required to be error-free, but there is no requirement for time. It is suitable for printers, storage devices, etc.

    Bulk transfers are reliable transfers that require a handshake to indicate the result of the transfer. If the amount of data is relatively large, multiple batch transaction transfers will be used to complete the transfer of all data. During the transfer process, the PID of the data packet will be flipped according to the DATA0-DATA1-DATA0-… method to ensure the synchronization between the sending end and the receiving end.

    USB allows less than 3 consecutive transmission errors, and will retry the transmission. If it succeeds, the error count counter will be cleared, otherwise the counter will be accumulated. After more than three times, HOST thinks that the function of the endpoint is wrong (STALL), and abandons the transmission task of the endpoint.

    A batch transfer (Transaction) consists of one or more batch transaction transfers (Transaction).

    Flip synchronization: the sender sends data packets in the order of DATA0-DATA1-DATA0-..., only successful transaction transmission will cause PID flipping, that is to say, the sender will flip the PID and send the next data only after receiving the ACK package, otherwise the transaction transmission will be retried. Similarly, if the receiving end finds that the received data packets are not reversed in this order, for example, two DATA0s are received consecutively, then the receiving end considers the second DATA0 to be a retransmission of the previous DATA0.

    It ensures "accurate" data transmission between the host and device by performing "error detection" and "retransmission" at the hardware level, that is, reliable transmission. It consists of three packages (i.e. IN transactions or OUT transactions):

  1. token

  2. data

  3. handshake

For IN Token (即:IN Transaction)

  • ACK: Indicates that the host has received the data correctly

  • NAK: Indicates that the device cannot return or receive data temporarily (for example: the device is busy)

  • STALL: Instructs the device to stop forever, requiring the intervention of the host software (such as: device error)

For OUT Token (即:OUT Transaction)

    If the received data packet is wrong, such as: CRC error, Device will not send any handshake packet

  • ACK: Device has received the data packet correctly and notified the Host to send the next data packet in order

  • NAK: The Device has correctly received the data packet and notified the Host to retransmit the data due to the temporary condition of the Device (such as the buffer is full)

  • STALL: Indicates that the Device endpoint has stopped, and notifies the Host not to retransmit

Bulk read and write sequence

 That is, it consists of a system IN transaction or OUT transaction.

Interrupt Transfer

    Interrupt transfers consist of IN or OUT transactions.

    Interrupt transfer is the same as bulk transfer except that PING is not supported. The difference between them is only that the endpoints of transaction transmission are different, the maximum supported packet length is different, the priority is different, etc., which are transparent to users.

    When the host schedules an interrupt transfer task, it will initiate an interrupt transfer according to the query interval specified in the corresponding interrupt endpoint descriptor. Interrupt transfers have a higher priority, second only to isochronous transfers.

    Similarly, the interrupt transmission also adopts the mechanism of PID reversal to ensure the data synchronization of the receiving and receiving ends. The figure below is the flow chart of interrupt transmission.

    The interrupt transfer method is always used to query the device to determine whether there is data to transfer. Therefore the direction of the interrupt transfer is always from the USB device to the host.

DATA0 or DATA1 contains interrupt information, not interrupt data.

Isochronous Transfer

  1. It consists of two packages:

  2. token

  3. data

    Synchronous transport does not support "handshake" and "retransmission capability", so it is an unreliable transport.

    Synchronous transport is an unreliable transport, so it does not have a handshake and does not support PID rollover. When the host schedules transaction transfers, synchronous transfers have the highest priority.

    Synchronous transmission is suitable for data that must arrive at a fixed rate or at a specified time, and can tolerate occasional errors. Real-time transmission is generally used for microphones, speakers, UVC Camera and other equipment. Real-time transmission only needs two information packet stages of token and data, and there is no handshake packet, so the data will not be retransmitted when the data is wrongly transmitted.

whaosoft aiot http://143ai.com 

 

Guess you like

Origin blog.csdn.net/qq_29788741/article/details/130673060