CDC Badge OS
Firmware for the CDC Badge v1.0 hardware security key
Loading...
Searching...
No Matches
usb_cdc.cpp File Reference
#include "usb_badge/usb_cdc.h"
#include "usb_badge/usb_hid.h"
#include "cdc_core/feature_flags.h"
#include "cdc_log.h"
#include "esp_err.h"
#include "esp_idf_version.h"
#include "esp_rom_sys.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "tusb.h"
#include "class/cdc/cdc.h"
#include <cstring>

Go to the source code of this file.

Functions

static void usb_device_task (void *arg)
 TinyUSB device task.
static void usb_shutdown_handler (void)
 Public USB CDC API implementation.
static bool usb_start_stack (void)
bool usb_cdc_init (void)
bool usb_cdc_start (void)
 Starts USB CDC runtime (or triggers re-enumeration in early-debug mode).
bool usb_cdc_ready (void)
 Returns whether USB CDC is connected and ready.
size_t usb_cdc_write (const uint8_t *data, size_t len)
 Writes byte buffer to USB CDC endpoint.
size_t usb_cdc_print (const char *str)
 Writes null-terminated string to USB CDC.
size_t usb_cdc_read (uint8_t *data, size_t len)
 Reads bytes from USB CDC endpoint.
int usb_cdc_getchar (void)
 Reads one character from USB CDC stream.
size_t usb_cdc_available (void)
 Returns number of bytes available for read.
void usb_cdc_flush (void)
 Flushes pending USB CDC writes.

Variables

static const char * TAG = "USB"
static bool g_usb_prepared = false
 Internal USB CDC startup and task state.
static bool g_usb_started = false
static TaskHandle_t g_usb_task = nullptr

Function Documentation

◆ usb_cdc_available()

size_t usb_cdc_available ( void )

Returns number of bytes available for read.

Returns
Pending byte count.

Definition at line 268 of file usb_cdc.cpp.

References g_usb_started.

◆ usb_cdc_flush()

void usb_cdc_flush ( void )

Flushes pending USB CDC writes.

Flush TX buffer (blocking)

Definition at line 276 of file usb_cdc.cpp.

References g_usb_started.

◆ usb_cdc_getchar()

int usb_cdc_getchar ( void )

Reads one character from USB CDC stream.

Returns
Character value or -1 if unavailable.

Definition at line 254 of file usb_cdc.cpp.

References g_usb_started.

◆ usb_cdc_init()

bool usb_cdc_init ( void )

USB CDC Serial Interface

Provides serial console via TinyUSB CDC. Initialize USB subsystem with CDC serial Behavior depends on CONFIG_USB_EARLY_DEBUG:

  • If enabled: Starts USB immediately (for debug logs during boot)
  • If disabled: Only prepares USB, call usb_cdc_start() after modules loaded
    Returns
    true if successful

Definition at line 129 of file usb_cdc.cpp.

References g_usb_prepared, LOG_E, LOG_I, TAG, usb_hid_init(), and usb_start_stack().

Referenced by initCoreServices().

◆ usb_cdc_print()

size_t usb_cdc_print ( const char * str)

Writes null-terminated string to USB CDC.

Parameters
strString to write.
Returns
Number of bytes written.

Definition at line 234 of file usb_cdc.cpp.

References usb_cdc_write().

◆ usb_cdc_read()

size_t usb_cdc_read ( uint8_t * data,
size_t len )

Reads bytes from USB CDC endpoint.

Parameters
dataOutput buffer.
lenMaximum bytes to read.
Returns
Number of bytes read.

Definition at line 245 of file usb_cdc.cpp.

References g_usb_started.

◆ usb_cdc_ready()

bool usb_cdc_ready ( void )

Returns whether USB CDC is connected and ready.

Returns
true if host CDC connection is active.

Definition at line 196 of file usb_cdc.cpp.

References g_usb_started.

◆ usb_cdc_start()

bool usb_cdc_start ( void )

Starts USB CDC runtime (or triggers re-enumeration in early-debug mode).

Returns
true if USB is running after call.

Definition at line 166 of file usb_cdc.cpp.

References g_usb_prepared, g_usb_started, LOG_I, TAG, and usb_start_stack().

Referenced by startApp().

◆ usb_cdc_write()

size_t usb_cdc_write ( const uint8_t * data,
size_t len )

Writes byte buffer to USB CDC endpoint.

Parameters
dataData buffer.
lenNumber of bytes to write.
Returns
Number of bytes written.

Definition at line 206 of file usb_cdc.cpp.

References g_usb_started.

Referenced by host_usb_cdc_write(), and usb_cdc_print().

◆ usb_device_task()

void usb_device_task ( void * arg)
static

TinyUSB device task.

TinyUSB device task loop.

Parameters
argTask parameter (unused).

Definition at line 78 of file usb_cdc.cpp.

Referenced by usb_start_stack().

◆ usb_shutdown_handler()

void usb_shutdown_handler ( void )
static

Public USB CDC API implementation.

Starts the TinyUSB stack and creates the USB device task.

Returns
true if USB stack startup succeeded, otherwise false.

Definition at line 94 of file usb_cdc.cpp.

Referenced by usb_start_stack().

◆ usb_start_stack()

bool usb_start_stack ( void )
static

Definition at line 108 of file usb_cdc.cpp.

References g_usb_started, g_usb_task, LOG_E, TAG, usb_device_task(), and usb_shutdown_handler().

Referenced by usb_cdc_init(), and usb_cdc_start().

Variable Documentation

◆ g_usb_prepared

bool g_usb_prepared = false
static

Internal USB CDC startup and task state.

Definition at line 38 of file usb_cdc.cpp.

Referenced by usb_cdc_init(), and usb_cdc_start().

◆ g_usb_started

bool g_usb_started = false
static

◆ g_usb_task

TaskHandle_t g_usb_task = nullptr
static

Definition at line 40 of file usb_cdc.cpp.

Referenced by usb_start_stack().

◆ TAG

const char* TAG = "USB"
static

USB CDC Serial Implementation

Uses TinyUSB for USB CDC serial console. Based on cdc-badge-os-legacy/components/usb_badge/usb_hid.cpp

Definition at line 32 of file usb_cdc.cpp.