Your program must give USB regular attention. The main loop is the repeating part of your program; this manual also calls it the foreground. An ISR is an interrupt handler that runs when the hardware asks for attention.
Start with the tool's complete Integration program. The table below tells you which service belongs in the main loop for your chosen USB type.
USB must keep running while the computer recognizes the device and while it is connected. The service handles requests and data, plus resets, pauses and resumes. Start USB once, then keep servicing it; do not keep initializing it again.
Start with the Integration listing generated for your selected device and clock. Keep this order:
USB_Init() starts the selected class and clears its buffers, queued data, events and connection state. It lets USB attach when the power settings allow it. It does not wait for the computer to recognize the device, and it does not enable USB interrupts.
USB_Init()
If USB_Result <> USB_OK Then Stop
On Positron16, USB_BAD_LENGTH here can indicate invalid placement of the USB backing array. Check the compiler memory map and the memory guidance in memory planning. Do not continue with an unsuccessful initialization.
Wait while still servicing USB. Transfers need USB_Configured = 1. This only says the computer selected the configuration; a PC program may still need to open it. If USB_Suspended = 1, normal transfers are paused even if configured remains 1.
|
Class and service choice |
Main program |
USB interrupt handler |
|
HID or CDC, polling |
Call USB_Service() frequently; handle application data. |
None required for USB. |
|
HID or CDC, ISR |
Handle application data; also call USB_Service() for optional VBUS sensing. |
Call USB_ISR(). |
|
MSC, polling |
Call USB_MSCService() frequently and complete its file requests. |
None required for USB. |
|
MSC, ISR |
Still call USB_MSCService() and complete file requests in the foreground. |
Call USB_ISR(). |
|
USB Browser/RNDIS |
Call USB_NetworkService() continuously; update values and consume browser input in the foreground. |
This mode uses polling, not the USB ISR. |
USB_Service() runs the common USB work. It does not produce a fresh USB_Result. In polling mode, call it before configuration as well as afterward. Split long calculations, display updates and waits so service continues. A timer that calls it only once each millisecond is not a substitute for a responsive polling loop.
USB_MSCService() and USB_NetworkService() include common USB servicing. Their class-specific work remains in the main program. See MSC file handling and browser mode for their own service steps. The value left in USB_Result after a network service call is not an overall web-server status.
With USB_VBUS_SENSE = 1, the library samples a GPIO to detect cable power. Even in ISR mode, retain foreground service calls: a change on that GPIO need not generate a USB interrupt. With VBUS sensing disabled, HID/CDC ISR operation does not require an additional USB_IRQ_PENDING test or a periodic foreground service call merely to dispatch USB interrupts.
Created with the Personal Edition of HelpNDoc: Produce electronic books easily