Use these checks when your program needs to show connection state or react to a reset, pause or incoming settings. Read events in one place in the main loop, then update your LED, display or application state there.
Use foreground events to update an LED, screen or application state. USB_GetEvents() safely copies the pending Word mask to USB_LastEvents, then clears the pending mask. It leaves USB_Result unchanged. Copy the snapshot if another part of your program may collect events too; preferably give one main-loop routine responsibility for dispatching them.
|
Event |
Mask |
Meaning and user action |
|
USB_EV_RESET |
1 |
Host reset; discard assumptions about earlier transfers. |
|
USB_EV_CONFIG |
2 |
Host configured or unconfigured; check USB_Configured. |
|
USB_EV_SUSPEND |
4 |
Transfers paused; update your indication or application power policy. |
|
USB_EV_RESUME |
8 |
Activity resumed; check current state before sending. |
|
USB_EV_RX |
16 |
Receive activity; use the relevant class API to obtain data. |
|
USB_EV_TX |
32 |
Transmit activity; query readiness if needed. |
|
USB_EV_FEATURE |
64 |
HID feature data accepted; inspect USB_Feature. |
|
USB_EV_LINE |
128 |
CDC line settings accepted; inspect USB_LineCoding. |
|
USB_EV_DTR_RTS |
256 |
CDC control lines changed; inspect USB_LineState. |
|
USB_EV_BREAK |
512 |
CDC break requested; inspect USB_BreakMs. |
|
USB_EV_ERROR |
1024 |
Hardware error recorded; inspect USB_ErrorFlags. |
|
USB_EV_DETACH |
2048 |
Library detached the USB connection. |
Several events can be reported together. Test each event bit separately. Repeated events combine into one bit; this list does not tell you their order or how many times each happened.
Dim wEvents As Word
Dim wEventTest As Word
USB_GetEvents()
wEvents = USB_LastEvents
wEventTest = wEvents & USB_EV_CONFIG
If wEventTest <> 0 Then
If USB_Configured = 1 Then
' Your configured action: update screen, LED or application state.
Else
' Your unconfigured action.
EndIf
EndIf
wEventTest = wEvents & USB_EV_DETACH
If wEventTest <> 0 Then
' Your detached action.
EndIf
wEventTest = wEvents & USB_EV_SUSPEND
If wEventTest <> 0 Then
' Your suspended action.
EndIf
Use the same pattern for the other masks. Check current configured/suspended state before operations, because events describe previous activity. With VBUS sensing disabled, physical cable removal may be observed as inactivity/suspend rather than an immediate distinct detach.
USB_Detach() deliberately disconnects in software, clears configuration and queued transfers, and returns USB_OK. Cable power is unaffected. USB_Attach() allows attachment again and returns USB_OK when that request is accepted. If sensed VBUS is absent, attachment waits for power. Wait for configuration afterward. These calls preserve the interrupt-enable choice; they do not require repeated USB_Init() calls.
USB_ClearErrors() clears USB_ErrorFlags after you have recorded it. It leaves USB_Result unchanged and does not consume events or repair the underlying cause.
Read the status variables to see what is happening. Use the library commands to change USB state instead of writing to these variables. The HID, CDC, MSC and browser topics explain their extra data and commands.
|
Name |
Type |
Purpose |
|
USB_Result |
Byte |
Last result-producing command's outcome. |
|
USB_Configured, USB_Suspended |
Bit each |
Current USB state; neither alone means a PC application is ready. |
|
Byte |
Byte count from the last read. |
|
|
USB_Events, USB_LastEvents |
Word each |
Pending and collected masks; consume through USB_GetEvents(). |
|
USB_ErrorFlags |
Byte on PIC18; Word on Positron16 |
Accumulated hardware error bits; consult the target's USB error register definitions. |
|
USB_Frame |
Word |
Sampled USB frame number, 0–2047; wraps and stops during suspend. |
|
Byte arrays |
Application packet data in HID/CDC; class-owned in MSC/RNDIS. |
|
|
USB_Feature[] |
Byte array |
Optional HID feature report, sized by USB_HID_FEATURE_SIZE. |
|
USB_LineCoding[7] |
Byte array |
CDC baud in bytes 0–3, least significant first; stop format, parity and data bits in bytes 4–6. |
|
USB_LineState |
Byte |
Host-requested CDC DTR in bit 0 and RTS in bit 1. |
|
USB_BreakMs |
Word |
CDC requested break: 0 stops, 65535 is indefinite, others request milliseconds. |
|
Word |
Status requested through USB_CDC_Notify(); separate from DTR/RTS. |
|
|
USB_IRQ_PENDING |
Byte, Positron16 |
Compatibility state; direct ISR operation needs no polling of it. |
Multi-byte state or shared feature arrays can change during an interrupt. When a consistent snapshot matters, briefly save and mask the selected device's USB interrupt source, copy the data, then restore its previous enable state. Do not run service commands during that copy. Use the same protection while changing feature data that the ISR may read. USB_GetEvents() already protects its snapshot.
USB_RAM_USED and Positron16 USB_RAM_ALLOC are compile-time allocation constants, not free-memory counters. The optional USB_ExtraRAM[] is application Byte storage when the tool enables it. See memory planning for allocation and safe use. Do not use private library variables as application interfaces.
Created with the Personal Edition of HelpNDoc: Save time and frustration with HelpNDoc's WinHelp HLP to CHM conversion feature