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