Send and receive HID or CDC bytes

USB Library ›› Device ›› Keep USB running ››
… visits
Parent Previous Next

For HID or CDC, the basic pattern is simple: fill TXReport, send it, and check the result. To receive, call USB_Read(), check the result, then use only the bytes counted by USB_RxLength.


Use HID for complete reports with a fixed layout, or CDC for a stream such as lines of text. This page is the detailed reference for the shared packet commands.

Send bytes to the computer

For HID and CDC, put outgoing bytes in TXReport and read incoming bytes from RXReport. The library already declares both Byte arrays, sized by USB_TX_SIZE and USB_RX_SIZE; do not declare them again. MSC and browser mode use these arrays internally, so use their file or named-value commands instead.


USB_CanWrite() asks whether the next data transmit buffer is available. It returns USB_OK, USB_BUSY, USB_OFFLINE or USB_HALTED. It neither copies data nor reserves the buffer. You may skip this preliminary check and call USB_Write() directly.


USB_Write(length) copies bytes from TXReport, starting at index 0, into an available USB buffer. length is a Byte. HID requires exactly USB_TX_SIZE; CDC accepts 0 through USB_TX_SIZE. Results are USB_OK, USB_BUSY, USB_OFFLINE, USB_BAD_LENGTH or USB_HALTED.


On USB_OK, the packet was copied and queued, so you may reuse TXReport. This does not confirm that a PC application processed it. On USB_BUSY, retain the unsent report and retry later. For HID, initialize the complete fixed-size report. For CDC, pass the actual byte count to avoid sending unwanted padding.


USB_WriteWait(length, timeout) retries that write while the result is USB_BUSY. The Byte length has the same rules. The Word timeout is 0–65535 nominal milliseconds. Any other result ends the call immediately.

' HID data example: a complete report with command 100 in its first byte.

Clear TXReport

TXReport[0] = 100

USB_WriteWait(USB_TX_SIZE, 10)

If USB_Result = USB_OK Then

    ' Queued successfully; application acknowledgement is a separate matter.

EndIf

USB_TxIdle() returns USB_OK when all data transmit buffers are free, or USB_BUSY while any remain queued. It can also return USB_OFFLINE or USB_HALTED. With ping-pong enabled, USB_CanWrite() may succeed while USB_TxIdle() remains busy: one slot is free and another is occupied. This check excludes CDC notifications; automatic HID idle reports can generate further activity.


USB_Flush(timeout) waits for USB_TxIdle() using the same Word timeout convention. Use it when queued data must finish its USB transfers before the next action. It does not erase buffers, flush MSC storage or acknowledge application processing.


Both wait commands attempt their operation immediately. Timeout 0 still allows one attempt; a busy result becomes USB_TIMEOUT. Each permitted wait interval makes 40 calls to USB_Service(), with a 25 µs delay after each, then retries. Thus the interval contains 1 ms of delays plus servicing and loop overhead. It is not a precise elapsed-time deadline. These commands are foreground-only, including in ISR builds.


A timed-out flush does not cancel already queued packets; they may complete later. A successful write-wait means queued, not drained. Use application acknowledgements when duplicate execution matters.

Receive bytes from the computer

USB_Available() checks without consuming a packet. USB_OK means a packet can be read; alternatives are USB_NO_DATA, USB_OFFLINE or USB_HALTED. It does not fill RXReport or update a useful receive length. Calling USB_Read() directly is usually simpler.


USB_Read() takes the next packet. On USB_OK, use RXReport[0] through RXReport[USB_RxLength - 1]. It releases the hardware receive slot so another packet can arrive. HID output reports received through control requests also use this interface.

Read the actual length. USB_RX_SIZE is the space available; USB_RxLength is the number of bytes just received. Do not process bytes past that length. A CDC packet can be empty, so check for zero before subtracting 1. A failed read sets the length to zero and reports USB_NO_DATA, USB_OFFLINE, USB_HALTED or USB_BAD_LENGTH.

Dim bLength As Byte

Dim bIndex As Byte

Dim bReceived As Byte


' Put this in the HID/CDC main loop after the required servicing.

USB_Read()

If USB_Result = USB_OK Then

    bLength = USB_RxLength

    If bLength > 0 Then

        For bIndex = 0 To bLength - 1

            bReceived = RXReport[bIndex]

            ' Process bReceived, or copy it into your own Byte array.

        Next

    EndIf

EndIf

Use or copy the received bytes before the next successful read replaces them. Keep this work short. CDC sends a stream of bytes: one PC write can arrive in more than one firmware read. Use a line ending, a length field or another rule to mark a complete message.

See also

Created with the Personal Edition of HelpNDoc: Effortlessly Publish Your Word Document as an eBook