Kleidos API Search

In development. It changes without notice.

Topic

Serial Transport

The serial line of a debug build, over a UART or the chip's USB.

It carries the console (Serial Debug Console) and the output of the diagnostics; on a computer it is the terminal.

See also
Logging, Serial Debug Console

Files

file src/platform/peripherals/serial_line_wait.h
How long a console writer waits for another task's line.
file src/platform/peripherals/serial_transport.cpp
Debug serial / console transport implementation.
file src/platform/peripherals/serial_transport.h
Debug serial / console transport facade.

Classes

class kleidos::platform::serial::LineWait
One writer's wait for the console's writer lock. More about kleidos::platform::serial::LineWait
class kleidos::platform::serial::LineGuard
Hold the console so several writes land as one run. More about kleidos::platform::serial::LineGuard
struct kleidos::platform::serial::DropStats
Bytes this transport accepted and then did not send, and why. More about kleidos::platform::serial::DropStats

Functions

constexpr uint32_tkleidos::platform::serial::lineAirtimeMs (std::size_t bytes, uint32_t baud)
Time bytes take on a UART at baud in 8N1 framing, rounded up.

Variables

constexpr uint32_tkleidos::platform::serial::kLineStallPatienceMs
constexpr uint32_tkleidos::platform::serial::kUartBitsPerByte
Bits one byte takes on a UART line in 8N1 framing: start, eight data, stop.

Lifecycle and connection

voidkleidos::platform::serial::begin (uint32_t baudRate)
Initialize the serial transport.
voidkleidos::platform::serial::setTimeout (uint32_t timeoutMs)
Set the read timeout.
boolkleidos::platform::serial::isConnected ()
True when a host is attached.
boolkleidos::platform::serial::waitForConnection (uint32_t timeoutMs, uint32_t pollMs=10)
Block until a host connects or the timeout expires.
voidkleidos::platform::serial::setDebugOutput (bool enabled)
Route ESP logs to this transport.

Input

intkleidos::platform::serial::available ()
Bytes available to read.
intkleidos::platform::serial::read ()
Read one byte.

Output

size_tkleidos::platform::serial::write (const uint8_t *data, size_t length)
Write raw bytes to the transport.
boolkleidos::platform::serial::atLineStart ()
Whether the last byte written through this transport ended a line.
DropStatskleidos::platform::serial::dropStats ()
size_tkleidos::platform::serial::write (const char *data, size_t length)
Write raw characters to the transport.
size_tkleidos::platform::serial::print (const char *text)
Write a NUL-terminated string.
size_tkleidos::platform::serial::println ()
Write a line terminator.
size_tkleidos::platform::serial::println (const char *text)
Write a string followed by a newline.
intkleidos::platform::serial::printf (const char *format,...)
printf-style formatted output.
voidkleidos::platform::serial::flush ()
Flush buffered output.

Function Documentation

atLineStart() #

bool kleidos::platform::serial::atLineStart()
nodiscard

Whether the last byte written through this transport ended a line.

True before anything was written. A caller that must start a line of its own (under a LineGuard, so the answer cannot change under it) writes a newline first when this is false, which is the case while another task's line is waiting for its own newline.

Return values
ValueMeaning
trueThe console is at the start of a line.
falseThe last byte written was not a newline.

available() #

int kleidos::platform::serial::available()

Bytes available to read.

Returns
Number of bytes available; negative on error.
Return values
ValueMeaning
0No data is currently buffered.
1At least one byte is buffered.
-1Backend read failed.

begin() #

void kleidos::platform::serial::begin(uint32_t baudRate)

Initialize the serial transport.

Parameters
NameMeaning
baudRateBaud rate for UART targets; ignored for USB-JTAG.

dropStats() #

DropStats kleidos::platform::serial::dropStats()
nodiscard
Returns
The running drop tally. Never resets; a caller compares snapshots.

isConnected() #

bool kleidos::platform::serial::isConnected()

True when a host is attached.

Return values
ValueMeaning
trueUSB/serial host is currently connected.
falseNo host detected.

lineAirtimeMs() #

uint32_t kleidos::platform::serial::lineAirtimeMs(std::size_t bytes, uint32_t baud)
constexpr

Time bytes take on a UART at baud in 8N1 framing, rounded up.

Parameters
NameDirectionMeaning
bytesinBytes to send.
baudinLine speed in bits per second (0 yields 0).
Returns
Milliseconds on the wire.

print() #

size_t kleidos::platform::serial::print(const char * text)

Write a NUL-terminated string.

Parameters
NameMeaning
textString to write; must not be nullptr.
Returns
Number of bytes written.

printf() #

int kleidos::platform::serial::printf(const char * format, ...)

printf-style formatted output.

Parameters
NameMeaning
formatprintf format string.
Returns
Number of characters written, or negative on error.

println() [1/2] #

size_t kleidos::platform::serial::println()

Write a line terminator.

Returns
Number of bytes written.

println() [2/2] #

size_t kleidos::platform::serial::println(const char * text)

Write a string followed by a newline.

Parameters
NameMeaning
textString to write; must not be nullptr.
Returns
Number of bytes written (including the newline).

read() #

int kleidos::platform::serial::read()

Read one byte.

Returns
Byte value (0-255), or negative when no data is available.
Return values
ValueMeaning
0..255The byte that was read.
-1No byte available (EOF / closed stream / native build).

setDebugOutput() #

void kleidos::platform::serial::setDebugOutput(bool enabled)

Route ESP logs to this transport.

Parameters
NameMeaning
enabledTrue to forward ESP_LOG output to this serial transport.

setTimeout() #

void kleidos::platform::serial::setTimeout(uint32_t timeoutMs)

Set the read timeout.

Parameters
NameMeaning
timeoutMsRead timeout in milliseconds.

waitForConnection() #

bool kleidos::platform::serial::waitForConnection(uint32_t timeoutMs, uint32_t pollMs = 10)

Block until a host connects or the timeout expires.

Parameters
NameMeaning
timeoutMsMaximum time to wait in milliseconds.
pollMsPolling interval in milliseconds.
Return values
ValueMeaning
trueA host connected within timeoutMs.
falseTimeout elapsed before a host connected.

write() [1/2] #

size_t kleidos::platform::serial::write(const char * data, size_t length)

Write raw characters to the transport.

Parameters
NameMeaning
dataPointer to the characters to write.
lengthNumber of characters to write.
Returns
Number of characters actually written.

write() [2/2] #

size_t kleidos::platform::serial::write(const uint8_t * data, size_t length)

Write raw bytes to the transport.

One call lands as one contiguous run on the wire: a write from another task waits for it rather than landing inside it, on both backends. Each backend feeds its driver in pieces no larger than half the driver's ring and holds the console's writer lock across them, so the run stays whole because of that lock: the USB-Serial-JTAG driver takes a run all at once or not at all and refuses outright any run longer than its ring.

Parameters
NameMeaning
dataPointer to the bytes to write.
lengthNumber of bytes to write.
Returns
Number of bytes actually written.

Variable Documentation

kLineStallPatienceMs #

uint32_t kleidos::platform::serial::kLineStallPatienceMs
inlineconstexpr

How long a waiter tolerates a holder that places no byte at all before it writes anyway. Longer than the longest stall a holder sits through while it still means to finish its line: the video take gives a line up after half a second without progress, and asserts that it stays below this.