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