USBD implements EasyDMA for accessing memory without CPU involvement.
Each endpoint has an associated set of registers, tasks, and events. EasyDMA and traffic on the USB bus are closely related. Several events provide insight on USB bus activity, with a number of tasks allowing an automated response to the traffic.
Registers
Enabling endpoints is controlled through the EPINEN and EPOUTEN registers.
The following registers define the memory address of the buffer for a specific IN or OUT endpoint:
- EPIN[n].PTR, (n=0..7)
- EPOUT[n].PTR, (n=0..7)
- ISOIN.PTR
- ISOOUT.PTR
The following registers define the amount of bytes to be sent on the USB bus for next transaction:
- EPIN[n].MAXCNT, (n=0..7)
- ISOIN.MAXCNT
The following registers define the length of the buffer (in bytes) for next transfer of incoming data:
- EPOUT[n].MAXCNT, (n=1..7)
- ISOOUT.MAXCNT
Since the USB host decides how many bytes are sent over the USB bus, the MAXCNT value can be copied from register SIZE.EPOUT[n] (n=1..7) or register SIZE.ISOOUT.
Register EPOUT[0].MAXCNT defines the length of
the OUT buffer (in bytes) for the control endpoint 0.
Register SIZE.EPOUT[0] shall indicate the same value
as MaxPacketSize from the USB device descriptor or wLength
from the SETUP command, whichever is the least.
The .AMOUNT registers indicate how many bytes actually have been transferred over EasyDMA during the last transfer.
Stalling bulk/interrupt endpoints is controlled through the EPSTALL register.
EasyDMA will not copy the SETUP data to memory (it will only transfer data from the Data stage). The following are separate registers in USBD that have setup data.
The EVENTCAUSE register provides details on what caused a given USBEVENT event, for instance if a CRC error is detected during a transaction, or if bus activity stops or resumes.
Tasks
Tasks STARTEPIN[n], STARTEPOUT[n] (n=0..7), STARTISOIN, and STARTISOOUT capture the values for .PTR and .MAXCNT registers. For IN endpoints, a transaction over the USB bus is automatically triggered when the EasyDMA transfer is complete. For OUT endpoints, it is up to software to allow the next transaction over the USB bus. See the examples in Control transfers, Bulk and interrupt transactions, and Isochronous transactions.
For the control endpoint 0, OUT transactions are allowed through the EP0RCVOUT task. The EP0STATUS task allows a Status stage to be initiated, and the EP0STALL task allows stalling further traffic (Data or Status stage) on the control endpoint.
Events
The STARTED event confirms that the values of the .PTR and .MAXCNT registers of the endpoints flagged in register EPSTATUS have been captured. Those can then be modified by software for the next transfer.
Events ENDEPIN[n], ENDEPOUT[n] (n=0..7), ENDISOIN, and ENDISOOUT events indicate that the entire buffer has been consumed. The buffer can be accessed safely by the software.
Only a single EasyDMA transfer can take place in USBD at any time. Software must ensure that tasks STARTEPIN[n] (n=0..7), STARTISOIN , STARTEPOUT[n] (n=0..7), or STARTISOOUT are not triggered before events ENDEPIN[n] (n=0..7), ENDISOIN, ENDEPOUT[n] (n=0..7), or ENDISOOUT are received from an on-going transfer.
The EPDATA event indicates that a successful (acknowledged) data transaction has occurred on the data endpoint(s) flagged in register EPDATASTATUS. A successful (acknowledged) data transaction on endpoint 0 is signalled by the EP0DATADONE event.
At any time a USBEVENT event may be sent, with details provided in EVENTCAUSE register.
The EP0SETUP event indicates that a SETUP token has been received on the control endpoint 0, and that the setup data is available in the setup data registers.