Inside USB Transfers
51
which must be fast to enable the device to meet the specification’s timing
requirements.
The CRC is applied to the data to be checked. The sender, whether host or
device, performs the calculation and sends the result along with the data. The
receiver performs the identical calculation on the received data. If the results
match, the data has arrived without error and the receiver returns ACK. If the
results don’t match, the receiver sends no handshake. The absence of the
expected handshake tells the sender to retry. Hosts typically try a total of three
times. On giving up, the host can inform the driver that requested the transfer.
The PID field in token packets uses a simpler form of error checking. The
lower four bits in the field are the PID, and the upper four bits are the comple-
ment. The receiver can check the integrity of the PID by complementing the
upper four bits and ensuring that they match the PID. If not, the packet is cor-
rupted and the receiver ignores the contents.
6JG&CVC6QIING
The data-toggle value enables detecting missed or duplicate data packets in con-
trol, bulk, and interrupt transfers. IN and OUT transactions have a data-toggle
value in the data packet’s PID field. DATA0 is a code of 0011, and DATA1 is
1011. In controller chips, a register bit often indicates the data-toggle state, so
the data-toggle value is sometimes called the data-toggle bit. Each endpoint
maintains its own data toggle.
Both the sender and receiver keep track of the data toggle. Host controllers han-
dle data toggles at a low level that is invisible to applications and device drivers.
Some device controller chips handle the data toggles completely in hardware,
while others require some firmware control. If you’re debugging a device where
the correct data is transmitting on the bus but the receiver is ignoring or dis-
carding the data, chances are good that the device isn’t sending or expecting the
correct data-toggle value.
When the host configures a device on power up or attachment, the host and
device each set their data toggles to DATA0 for all except some high-speed iso-
chronous endpoints. On detecting an incoming data packet, the host or device
compares the state of its data toggle with the received data toggle. If the values
match, the receiver toggles its value and returns an ACK handshake packet. The
ACK causes the sender to toggle its value for the next transaction.
The next received packet in the transfer should contain a data toggle of
DATA1, and again the receiver toggles its bit and returns ACK. The data toggle