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.hpp
Go to the documentation of this file.
1#ifndef SBL_USB_CDC_HPP_
2#define SBL_USB_CDC_HPP_
3
4/**
5 * @file cdc.hpp
6 * @brief USB CDC (Virtual COM Port) interface
7 *
8 * Provides serial communication over USB CDC ACM class.
9 * Shows up as a COM port on the host - works with any terminal.
10 *
11 * Usage:
12 * @code
13 * #include <sbl/usb/cdc.hpp>
14 *
15 * sbl::usb::CdcSerial::puts("Hello USB!\n");
16 *
17 * if (sbl::usb::CdcSerial::available()) {
18 * char c = sbl::usb::CdcSerial::read_byte();
19 * }
20 * @endcode
21 *
22 * For logging integration:
23 * @code
24 * using Logger = sbl::log::Logger<sbl::usb::CdcOutput, Timer>;
25 * @endcode
26 */
27
28#include <cstddef>
29#include <cstdint>
30
31namespace sbl::usb {
32
33/**
34 * @brief USB CDC (Virtual COM Port) interface
35 *
36 * Static class providing serial I/O over USB CDC.
37 * All operations are non-blocking unless noted.
38 *
39 * @context read(), available() and connected() only read TinyUSB's CDC
40 * state and do not call tud_task(). write(), puts() and write_byte() are
41 * not ISR-safe: they may call tud_task(). flush() is not ISR-safe: it
42 * blocks until TX completes or times out.
43 */
44class CdcSerial {
45public:
46 /**
47 * @brief Write data to USB serial (non-blocking)
48 *
49 * May call tud_task() internally to flush.
50 *
51 * @context Not ISR-safe.
52 *
53 * @param data Pointer to data buffer
54 * @param len Number of bytes to write
55 * @return Number of bytes written (may be less than len if buffer full)
56 */
57 static size_t write(const uint8_t* data, size_t len);
58
59 /**
60 * @brief Write null-terminated string
61 * @param str String to write
62 * @return Number of bytes written
63 */
64 static size_t puts(const char* str);
65
66 /**
67 * @brief Write single byte
68 * @param byte Byte to write
69 * @return true if byte was written, false if buffer full
70 */
71 static bool write_byte(uint8_t byte);
72
73 /**
74 * @brief Read data from USB serial (non-blocking)
75 * @param data Buffer to read into
76 * @param max_len Maximum bytes to read
77 * @return Number of bytes read
78 */
79 static size_t read(uint8_t* data, size_t max_len);
80
81 /**
82 * @brief Read single byte
83 * @return Byte read, or -1 if none available
84 */
85 static int read_byte();
86
87 /**
88 * @brief Get number of bytes available to read
89 * @return Bytes available in receive buffer
90 */
91 static size_t available();
92
93 /**
94 * @brief Check if host has terminal connected (DTR set)
95 *
96 * Returns true when a host terminal has opened the port and set DTR.
97 * This is more reliable than just checking tud_ready() which only
98 * indicates USB enumeration, not that a terminal is actually listening.
99 *
100 * @return true if host terminal is connected
101 */
102 static bool connected();
103
104 /**
105 * @brief Check if a terminal just connected (DTR rising edge)
106 *
107 * Returns true exactly once per DTR low->high transition. Use this
108 * to send boot banners or one-time status messages when a terminal
109 * opens the port.
110 *
111 * @return true once per new connection
112 */
113 static bool just_connected();
114
115 /**
116 * @brief Flush TX buffer
117 *
118 * Blocks until all pending data is sent or timeout.
119 */
120 static void flush();
121};
122
123/**
124 * @brief Logger output sink for sbl::log integration
125 *
126 * Usage:
127 * @code
128 * #define SBL_LOG_LEVEL SBL_LOG_LEVEL_DEBUG
129 * #define SBL_LOG_OUTPUT sbl::usb::CdcOutput
130 * #include <sbl/common/log/macros.hpp>
131 *
132 * SBL_LOG_INFO("Hello from USB!");
133 * @endcode
134 */
135struct CdcOutput {
136 static void write(const char* str) { CdcSerial::puts(str); }
137};
138
139} // namespace sbl::usb
140
141#endif // SBL_USB_CDC_HPP_
USB CDC (Virtual COM Port) interface.
Definition cdc.hpp:44
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
Logger output sink for sbl::log integration.
Definition cdc.hpp:135