PKP
VS1000 P
ROGRAMMER
’
S
G
UIDE
VSMPG
20.3
Receiving Packets from PC (EP0OUT, EP1OUT, ... , EP3OUT)
The USB hardware handles all necessary token (ACK, NAK, IN, OUT, SETUP, STALL)
sending and receiving. The software sees only the data packet contents plus some state
information about the sent tokens.
20.3.1
Reception
All received packets for all endpoints arrive to the same 2 KiB (1 KiW) ring buffer mem-
ory in X address space. This maximizes memory usage efficiency, but leads to one
important side-effect: The USB specification dictates that an incoming SETUP transfer
to the control endpoint must be the first packet to be processed at all times.
For instance the PC might issue a SETUP control request to the control endpoint before
the software has had time to process a data packet that has previously arrived to a data
endpoint. In such a case, the software should ignore the pending data and handle the
SETUP packet instead.
For achieving this functionality, the hardware can test the USB_STF_SETUP bit at the
USB_CONTROL register. If it is “1”, all packets until the last received SETUP packet
need to be truncated and the last SETUP packet processed. A reasonably fast USB
implementation should be able to achieve this without problems, but delays of several
milliseconds (such as for sending debug messages etc) can cause problems with this
clause, which result in “random hang-ups” of the USB communication with the PC. If care
is taken to process the packets in the correct order, most (if not all) USB transactions
can perfectly well cope with delays of several seconds. In practice the PC waits patiently
for several seconds if the data you send is “correct,” e.g. what the PC expects, but very
quickly responds to any unexpected data by issuing a bus reset.
Software can detect a received packet by scanning the USB_RDPTR and USB_WRPTR
registers. When their values differ, there is a packet ready for processing in the input
buffer.
USB_RDPTR points now to a header word. The actual packet data words are in the
buffer memory after the header word. The packet header word has the following struc-
ture:
Packet header word bits
Name
Bits
Description
crc-err
15
1=CRC error detected
setup
14
1=SETUP packet, 0=DATA packet
endpoint
13:10
Endpoint to which the packet is addressed to
pktlength
9:0
Length of packet in bytes
This is immediately followed by (pk1)/2 data words, MSB first.
A quick routine can access the contents directly in the buffer memory, or choose to copy
the packet contents to another location in memory. In either case, the software should
Rev. 0.20
2011-10-04
Page