100% found this document useful (3 votes)
76 views35 pages

How To Use USB-based Applications With PIC Microcontrollers

The Universal Serial Bus (USB) interface is commonly used to connect devices like cameras, printers and MP3 players to computers. USB supports up to 127 devices connected via a cable and comes in several versions with different speeds. USB uses a four-wire cable to transmit data using encoding techniques. It allows for power delivery to connected devices and different connector types.

Uploaded by

Srinivas Perala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
100% found this document useful (3 votes)
76 views35 pages

How To Use USB-based Applications With PIC Microcontrollers

The Universal Serial Bus (USB) interface is commonly used to connect devices like cameras, printers and MP3 players to computers. USB supports up to 127 devices connected via a cable and comes in several versions with different speeds. USB uses a four-wire cable to transmit data using encoding techniques. It allows for power delivery to connected devices and different connector types.

Uploaded by

Srinivas Perala
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 35

The Universal Serial Bus (USB) is one of the most common

interfaces used in electronic consumer products today, including


PCs, cameras, GPS devices, MP3 players, modems, printers, and
scanners,
The USB was originally developed by Compaq, Microsoft,
Intel, and NEC, and later by Hewlett-Packard, Lucent, and
Philips as well
how to use
USB-based applications
with PIC microcontrollers
The USB is a high-speed serial interface that can also provide
power to devices connected to it
A USB bus supports up to 127 devices connected through a
four-wire serial cable of up to three or even five meters in length

The USB bus specification comes in 3 versions:
1) USB 1.1 11Mbps
2) USB 2.0 480 Mbps
3) USB 3.0 5 Gbit/s

The maximum power available to an external device is limited to
about 100mA at 5 V.


USB is a four-wire interface implemented using a four-core
shielded cable
Two types of connectors are specified and used: Type A and
Type B.

USB signals are bi-phase, and signals are sent from the host
computer using the NRZI (non-return to zero inverted) data
encoding technique.

In this technique, the signal level is inverted for each change to a logic 0. The signal level
for a logic 1 is not changed.

A 0 bit is stuffed after every six consecutive ones in the data stream to make the data
dynamic
Upstream End Transceiver
Transceivers

At each end of the data link between
host and device is a transceiver circuit.
The transceivers are similar, differing
mainly in the associated resistors

By upstream, we mean the end nearer to
the host. The upstream end has two 15K
pull-down resistors.


Each line can be driven low individually,
or a differential data signal can be
applied. The maximum 'high' level is
3.3V.
When receiving, individual receivers
on each line are able to detect single
ended signals, so that the so-called
Single Ended Zero (SE0) condition,
where both lines are low, can be
detected. There is also a differential
receiver for reliable reception of data.
Speed Identification
At the device end of the link a 1.5 kohm
resistor pulls one of the lines up to a 3.3V
supply derived from VBUS.

This is on D- for a low speed device, and
on D+ for a full speed device.
(A high speed device will initially present
itself as a full speed device with the pull-
up resistor on D+.)

The host can determine the required
speed by observing which line is pulled
high.
Line States

Given that there are just 2 data lines to use, it is
surprising just how many different conditions are
signaled using them:

Detached
When no device is plugged in, the host will see
both data lines low, as its 15 kohm resistors are
pulling each data line low


Attached
When the device is plugged in to the host, the host
will see either D+ or D- go to a '1' level, and will
know that a device has been plugged in.
The '1' level will be on D- for a low speed device,
and D+ for a full (or high) speed device.

Idle

The state of the data lines when the pulled up line is high, and the other line is
low, is called the idle state. This is the state of the lines before and after a packet
is sent
J, K and SEO States

To make it easier to talk about the states of the data lines, some special
terminology is used.

The 'J State' is the same polarity as the idle state (the line with the pull-up
resistor is high, and the other line is low), but is being driven to that state by
either host or device.

The K state is just the opposite polarity to the J state.

The Single Ended Zero (SE0) is when both lines are being pulled low.

The J and K terms are used because for Full Speed and Low Speed links they are
actually of opposite polarity
Single Ended One (SE1)

This is the illegal condition where both lines are high. It should never occur on
a properly functioning link.
Reset
When the host wants to start communicating with a
device it will start by applying a 'Reset' condition
which sets the device to its default unconfigured state.

The Reset condition involves the host pulling down
both data lines to low levels (SE0) for at least 10 ms.
The device may recognise the reset condition after 2.5
us.

This 'Reset' should not be confused with a micro-
controller power-on type reset. It is a USB protocol
reset to ensure that the device USB signaling starts
from a known state.
EOP signal

The End of Packet (EOP) is an SE0 state for 2
bit times, followed by a J state for 1 bit time
Suspend
One of the features of USB which is an essential part of today's emphasis of
'green' products is its ability to power down an unused device.

It does this by suspending the device, which is achieved by not sending
anything to the device for 3 ms.

Normally a SOF packet (at full speed) or a Keep Alive signal (at low speed) is
sent by the host every 1 ms, and this is what keeps the device awake.

A suspended device may draw no more than 0.5 mA from Vbus.

A suspended device must recognise the resume signal, and also the reset
signal.
Resume

When the host wants to wake the device up after a suspend, it does so by
reversing the polarity of the signal on the data lines for at least 20ms. The
signal is completed with a low speed end of packet signal.

It is also possible for a device with its remote wakeup feature set, to initiate
a resume itself. It must have been in the idle state for at least 5ms, and
must apply the wakeup K condition for between 1 and 15 ms. The host
takes over the driving of the resume signal within 1 ms
Keep Alive Signal

This is represented by a Low speed EOP. It is sent at least once every
millisecond on a low speed link, in order to keep the device from suspending.
Packets

The packet could be thought of as the smallest element of data transmission.
Each packet conveys an integral number of bytes at the current transmission
rate. Before and after the packet, the bus is in the idle state.
You need not be concerned with the detail of syncs, bit stuffing, and End Of
Packet conditions, unless you are designing at the silicon level, as the Serial
Interface Engine (SIE) will deal with the details for you. You should just be
aware that the SIE can recognise the start and end of a packet, and that the
packet contains a whole number of bytes.
Serial Interface Engine (SIE)
The complexities and speed of the USB protocol are such that it is not practical to expect
a general purpose micro-controller to be able to implement the protocol using an
instruction-driven basis. Dedicated hardware is required to deal with the time-critical
portions of the specification, and the circuitry grouping which performs this function is
referred to as the Serial Interface Engine (SIE)
In spite of this packets often expect fields of data to cross byte
boundaries. The important rule to remember is that all usb fields
are transmitted least significant bit first.
A packet starts with a sync pattern to allow the receiver bit clock to
synchronise with the data. It is followed, by the data bytes of the
packet, and concluded with an End of Packet (EOP) signal.

The data is actually NRZI encoded, and in order to ensure sufficiently
frequent transitions, a zero is inserted after 6 successive 1's (this is
known as bit stuffing)
Endpoints
Each USB device has a number of endpoints. Each endpoint is a source or
sink of data. A device can have up to 16 OUT and 16 IN endpoints.

OUT always means from host to device.

IN always means from device to host.

Endpoint 0 is a special case which is a combination of endpoint 0 OUT and
endpoint 0 IN, and is used for controlling the device.
Pipe
A logical data connection between the host and a particular endpoint, in which we
ignore the lower level mechanisms for actually achieving the data transfers.
Transactions
Simple transfers of data called 'Transactions' are built up using packets
Packet Formats

The first byte in every packet is a Packet Identifier (PID) byte. This byte needs
to be recognised quickly by the SIE and so is not included in any CRC checks. It
therefore has its own validity check. The PID itself is 4 bits long, and the 4 bits
are repeated in an complemented form.
USB uses two different CRCs, one 5 bits long (CRC5) and one 16 bits long
(CRC16).
There are four different packet formats based on which PID the packet
starts with
The token packet contains two addressing elements:
Address (7 bits)
This device address can address up to 127 devices. Address 0 is reserved for a device
which has not yet had its address set.
Endpoint number (4 bits)
There can be up to 16 possible endpoints in a device in each direction. The direction is
implicit in the PID. OUT and SETUP PIDs will refer to the OUT endpoint, and an IN PID
will refer to the IN endpoint
There are three types of transaction
Transactions
OUT Transaction
A successful OUT transaction comprises two or three sequential packets. If it were
being used in an Isochronous Transfer there would not be a handshake packet from
the device.

On a low or full speed link, the PID shown as DATAx will be either a DATA0 or a
DATA1. An alternating DATA0/DATA1 is used as a part of the error control protocol to
(or from) a particular endpoint.
IN Transaction
A successful IN transaction comprises two or three sequential packets. If it were being
used in an Isochronous Transfer there would not be a handshake packet from the host.
Here again, the DATAx is either a DATA0 or a DATA1
SETUP Transaction
A successful SETUP transaction comprises three sequential packets. This is similar to an
OUT transaction, but the data payload is exactly 8 bytes long, and the SETUP PID in the
token packet informs the device that this is the first transaction in a Control Transfer
Data Flow Types
There are four different ways to transfer data on a USB bus. Each has its own
purposes and characteristics. Each one is built up using one or more
transaction type.
Enumeration
When a device is plugged into a USB bus, it becomes known to the host
through a process called enumeration. The steps of enumeration are:
When a device is plugged in, the host becomes aware of it because one of
the data lines (D or D) becomes logic high.

The host sends a USB reset signal to the device to place the device in a
known state. The reset device responds to address 0.

The host sends a request on address 0 to the device to find out its maximum
packet size using a Get Descriptor command.

The device responds by sending a small portion of the device descriptor.
The host sends a USB reset again.
The host assigns a unique address to the device and sends a Set Address
request to the device. After the request is completed, the device assumes
the new address. At this point the host is free to reset any other newly
plugged-in devices on the bus.

The host sends a Get Device Descriptor request to retrieve the complete
device descriptor, gathering information such as manufacturer, type of
device, and maximum control packet size.

The host sends a Get Configuration Descriptors request to receive the
devices configuration data, such as power requirements and the types and
number of interfaces supported.

The host may request any additional descriptors from the device.
Descriptors

All USB devices have a hierarchy of descriptors that describe various features
of the device: the manufacturer ID, the version of the device, the version of
USB it supports, what the device is, its power requirements, the number and
type of endpoints, and so forth.

The most common USB descriptors are:


Device descriptors
Configuration descriptors
Interface descriptors
HID descriptors
Endpoint descriptors

You might also like