Sound Byte Libs 0.5.1-340-gab5b79a
C++ firmware library for audio applications on 32-bit ARM Cortex-M processors
Loading...
Searching...
No Matches
cdc.cpp
Go to the documentation of this file.
1/**
2 * @file cdc.cpp
3 * @brief USB CDC serial implementation over TinyUSB
4 *
5 * Wraps TinyUSB CDC functions with SBL API.
6 */
7
8#include "cdc.hpp"
9#include "tusb.h"
10#include <cstring>
11
12// Connection tracking for just_connected()
13// Fires on: (1) first USB enumeration (boot), (2) DTR rising edge (terminal open)
14static bool last_ready = false;
15static volatile bool dtr_rose = false;
16
17extern "C" void tud_cdc_line_state_cb(uint8_t itf, bool dtr, bool rts) {
18 (void)itf;
19 (void)rts;
20 if (dtr) {
21 dtr_rose = true;
22 }
23}
24
25namespace sbl::usb {
26
27size_t CdcSerial::write(const uint8_t* data, size_t len) {
28 if (!tud_ready()) {
29 return 0;
30 }
31
32 size_t written = 0;
33 while (written < len) {
34 size_t avail = tud_cdc_write_available();
35 if (avail == 0) {
36 // Buffer full, flush and retry
37 tud_cdc_write_flush();
38 tud_task(); // Process USB to make room
39 avail = tud_cdc_write_available();
40 if (avail == 0) {
41 break; // Still full, return the bytes written so far
42 }
43 }
44
45 size_t to_write = (len - written) < avail ? (len - written) : avail;
46 size_t n = tud_cdc_write(data + written, to_write);
47 written += n;
48
49 if (n == 0) {
50 break; // Write failed
51 }
52 }
53
54 tud_cdc_write_flush();
55 return written;
56}
57
58size_t CdcSerial::puts(const char* str) {
59 if (!str) {
60 return 0;
61 }
62 return write(reinterpret_cast<const uint8_t*>(str), strlen(str));
63}
64
65bool CdcSerial::write_byte(uint8_t byte) {
66 if (!tud_ready()) {
67 return false;
68 }
69
70 if (tud_cdc_write_available() == 0) {
71 tud_cdc_write_flush();
72 tud_task();
73 if (tud_cdc_write_available() == 0) {
74 return false;
75 }
76 }
77
78 return tud_cdc_write_char(byte) == 1;
79}
80
81size_t CdcSerial::read(uint8_t* data, size_t max_len) {
82 if (!tud_cdc_available()) {
83 return 0;
84 }
85 return tud_cdc_read(data, max_len);
86}
87
89 if (!tud_cdc_available()) {
90 return -1;
91 }
92 return tud_cdc_read_char();
93}
94
95size_t CdcSerial::available() { return tud_cdc_available(); }
96
97bool CdcSerial::connected() { return tud_cdc_connected(); }
98
100 bool ready = tud_ready();
101
102 // Boot: detect first USB enumeration
103 if (ready && !last_ready) {
104 last_ready = true;
105 return true;
106 }
107 last_ready = ready;
108
109 // Reconnect: detect DTR rising edge (terminal open)
110 if (dtr_rose) {
111 dtr_rose = false;
112 return true;
113 }
114
115 return false;
116}
117
119 tud_cdc_write_flush();
120
121 // Wait for TX complete with timeout
122 for (int i = 0; i < 1000; i++) {
123 tud_task();
124 if (tud_cdc_write_available() == CFG_TUD_CDC_TX_BUFSIZE) {
125 break; // Buffer empty, all sent
126 }
127 // Small delay; a spin keeps this free of the system timer
128 for (volatile int j = 0; j < 1000; j++) {}
129 }
130}
131
132} // namespace sbl::usb
USB CDC (Virtual COM Port) interface.
static size_t write(const uint8_t *data, size_t len)
Write data to USB serial (non-blocking)
Definition cdc.cpp:27
static size_t puts(const char *str)
Write null-terminated string.
Definition cdc.cpp:58
static void flush()
Flush TX buffer.
Definition cdc.cpp:118
static size_t available()
Get number of bytes available to read.
Definition cdc.cpp:95
static size_t read(uint8_t *data, size_t max_len)
Read data from USB serial (non-blocking)
Definition cdc.cpp:81
static bool write_byte(uint8_t byte)
Write single byte.
Definition cdc.cpp:65
static bool connected()
Check if host has terminal connected (DTR set)
Definition cdc.cpp:97
static bool just_connected()
Check if a terminal just connected (DTR rising edge)
Definition cdc.cpp:99
static int read_byte()
Read single byte.
Definition cdc.cpp:88
USB device classes.
Definition cdc.cpp:25
bool ready()
Check if USB is enumerated and ready.