In the previous post, we talked about the General USB Architecture, that as you have seen is not easy and Highly Hierarchical, you can read that as highly Layerized.
Before talking about the foundamentals of USB Coding, is important to spent some words about Usb Protocols (not deeply handled in the previous post).
USB protocols are different from the other common interfaces, are highly layerized, but we will work only with High Level Layers, Low Level are controlled by USB Controller.
Each USB Transaction consists of a:
- Token Packet (Header defining what it expects to follow)
- Optional Data Packet, (Containing the payload)
- Status Packet (Used to acknowledge transactions and to provide a means of error correction)
The first packet ( Token Packet ) is generated by the Host Controller and will cointain vital informations about the data transaction such as Device Address, Endpoints. Second packet strictly linked to the first, cointains the Payload and is called Data Packet, and finally a Status Packet is sent, works as an Handshaking Packet.
.:: Anatomy of an USB Packet ::.
USB’s data is formatted in LSB, and foundamentally USB packet is consist of various fields:
- 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 igh speed and is used to synchronise the clock of the receiver with that of the transmitter. The last two bits indicatehere the PID fields starts.)
- PID (his field is used to identify the type of packet that is being sent), Values of PID can identify 4 kind of packets: Token, Data, Handshake, Special.
- 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.)
- ENDP ( The Endpoint, already defined in the previous post)
- CRC ( A classical Anti Corruption Check)
- EOP (End Of Packet)
This is the basical composition of an USB Packet, and as said 4 types of packets, each packet have more kind of Sub-Packets:
- Token Packets
- Data Packets
- Handshake Packets
- Start of Frame Packets (often called SOF Packet)
As you have seen, USB protocol is complicated to code directly, but fortunately we have a series of Functions that manages from low level to transation level, and only things we need to know are the error codes. Each function will have a series of buffers, typically 8 bytes long, each buffer will belong to an endpoint – EPx In, EPx
Out (we have the following numeration EP0 In/Out, EP1 In/Out, etc) , these Endpoints can be described as sources or
sinks of data, so if we send a packet to our EP1 device we will deal with EP1 In and EP1 Out. In all cases we have to ensure the support for EP0, because it receives all foundamental data about Device Control/Status.
Effective data transfer is done by Pipes, that are Logical Connections between Host and Endpoints and have some Control Parameters as Bandwidth Allocation, Transfer Type.
We can have two kind of pipes:
- Stream Pipes: Can be sent any type of data down a stream pipe and can retrieve the data out the other end, and can support Bulk, Isochronous and Interrupt Transfer Types.
- Message Pipes: Data is transferred in the desired direction, and is a Bidirectional Channel.
Here finishes the Architectural part, in the next part we will talk about USB Coding, and finally about Usb Forensics..
Many thanks goes to BeyondLogic Corp. for the great USB’s Arch Summary.
See you to the next post.. :)