Sound Byte Libs 1ee2ca6
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 */
33class CdcSerial {
34public:
35 /**
36 * @brief Write data to USB serial (non-blocking)
37 * @param data Pointer to data buffer
38 * @param len Number of bytes to write
39 * @return Number of bytes written (may be less than len if buffer full)
40 */
41 static size_t write(const uint8_t* data, size_t len);
42
43 /**
44 * @brief Write null-terminated string
45 * @param str String to write
46 * @return Number of bytes written
47 */
48 static size_t puts(const char* str);
49
50 /**
51 * @brief Write single byte
52 * @param byte Byte to write
53 * @return true if byte was written, false if buffer full
54 */
55 static bool write_byte(uint8_t byte);
56
57 /**
58 * @brief Read data from USB serial (non-blocking)
59 * @param data Buffer to read into
60 * @param max_len Maximum bytes to read
61 * @return Number of bytes read
62 */
63 static size_t read(uint8_t* data, size_t max_len);
64
65 /**
66 * @brief Read single byte
67 * @return Byte read, or -1 if none available
68 */
69 static int read_byte();
70
71 /**
72 * @brief Get number of bytes available to read
73 * @return Bytes available in receive buffer
74 */
75 static size_t available();
76
77 /**
78 * @brief Check if host has terminal connected (DTR set)
79 * @return true if host terminal is connected
80 */
81 static bool connected();
82
83 /**
84 * @brief Flush TX buffer
85 *
86 * Blocks until all pending data is sent or timeout.
87 */
88 static void flush();
89};
90
91/**
92 * @brief Logger output sink for sbl::log integration
93 *
94 * Usage:
95 * using Logger = sbl::log::Logger<sbl::usb::CdcOutput, MyTimer>;
96 *
97 * #define SBL_LOG_LEVEL SBL_LOG_LEVEL_DEBUG
98 * #define SblLogger_ Logger
99 * #include <sbl/log/macros.hpp>
100 *
101 * SBL_LOG_INFO("Hello from USB!");
102 */
103struct CdcOutput {
104 static void write(const char* str) {
105 CdcSerial::puts(str);
106 }
107};
108
109} // namespace sbl::usb
USB CDC (Virtual COM Port) interface.
Definition cdc.hpp:33
static size_t write(const uint8_t *data, size_t len)
Write data to USB serial (non-blocking)
Definition cdc.cpp:13
static size_t puts(const char *str)
Write null-terminated string.
Definition cdc.cpp:44
static void flush()
Flush TX buffer.
Definition cdc.cpp:87
static size_t available()
Get number of bytes available to read.
Definition cdc.cpp:79
static size_t read(uint8_t *data, size_t max_len)
Read data from USB serial (non-blocking)
Definition cdc.cpp:65
static bool write_byte(uint8_t byte)
Write single byte.
Definition cdc.cpp:49
static bool connected()
Check if host has terminal connected (DTR set)
Definition cdc.cpp:83
static int read_byte()
Read single byte.
Definition cdc.cpp:72
Logger output sink for sbl::log integration.
Definition cdc.hpp:103
static void write(const char *str)
Definition cdc.hpp:104