USB Communications Device Class API. This class is used to enable communication between the host and the device.
This class is often used to enable serial communication and can be used to flash the firmware on the ESP32 without the external USB to Serial chip.
Event handling functions.
void onEvent(esp_event_handler_t callback);
void onEvent(arduino_usb_cdc_event_t event, esp_event_handler_t callback);
Where event
can be:
- ARDUINO_USB_CDC_ANY_EVENT
- ARDUINO_USB_CDC_CONNECTED_EVENT
- ARDUINO_USB_CDC_DISCONNECTED_EVENT
- ARDUINO_USB_CDC_LINE_STATE_EVENT
- ARDUINO_USB_CDC_LINE_CODING_EVENT
- ARDUINO_USB_CDC_RX_EVENT
- ARDUINO_USB_CDC_TX_EVENT
- ARDUINO_USB_CDC_RX_OVERFLOW_EVENT
- ARDUINO_USB_CDC_MAX_EVENT
The setRxBufferSize
function is used to set the size of the RX buffer.
size_t setRxBufferSize(size_t size);
This function is used to define the time to reach the timeout for the TX.
void setTxTimeoutMs(uint32_t timeout);
This function is used to start the peripheral using the default CDC configuration.
void begin(unsigned long baud);
Where:
baud
is the baud rate.
This function will finish the peripheral as CDC and release all the allocated resources. After calling end
you need to use begin
again in order to initialize the USB CDC driver again.
void end();
This function will return if there are messages in the queue.
int available(void);
The return is the number of bytes available to read.
This function will return if the hardware is available to write data.
int availableForWrite(void);
This function is used to peek
messages from the queue.
int peek(void);
This function is used to read the bytes available.
size_t read(uint8_t *buffer, size_t size);
Where:
buffer
is the pointer to the buffer to be read.size
is the number of bytes to be read.
This function is used to write the message.
size_t write(const uint8_t *buffer, size_t size);
Where:
buffer
is the pointer to the buffer to be written.size
is the number of bytes to be written.
This function is used to flush the data.
void flush(void);
This function is used to get the baudRate
.
uint32_t baudRate();
This function will enable the debug output, usually from the UART0, to the USB CDC.
void setDebugOutput(bool);
This function enables the device to reboot by the DTR as RTS signals.
void enableReboot(bool enable);
This function will return if the reboot is enabled.
bool rebootEnabled(void);
Here is an example of how to use the USB CDC.
.. literalinclude:: ../../../libraries/USB/examples/USBSerial/USBSerial.ino :language: arduino