ProtoCore v1.0.16
Deterministic, zero-heap network stack for embedded targets
Loading...
Searching...
No Matches
uart.h
Go to the documentation of this file.
1// ProtoCore v1.0.16 - Copyright (C) 2026 Douglas Quigg (dstroy0) <dquigg123@gmail.com>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4/**
5 * @file uart.h
6 * @brief The one owner of the UART units for the peripheral drivers.
7 *
8 * The third sibling of i2c.h and spi.h, for the parts that speak a serial stream rather than a
9 * bus: the mmWave presence radars (LD2410, HMMD) and the interface bridge's UART targets. A unit
10 * is a controller index, 0 upward. Which pins it runs on is a board fact, so the caller names
11 * them and -1 leaves a pin at the unit's default.
12 *
13 * A driver reads what has arrived rather than blocking on a byte count: ::protocore_uart_available
14 * reports the depth of the driver's receive ring, and ::protocore_uart_read takes what is there up to
15 * its timeout. Nothing here waits without a bound (SRC_LAW rule 5).
16 *
17 * The bodies compile wherever the platform states a bus.
18 *
19 * @author Douglas Quigg (dstroy0)
20 * @date 2026
21 */
22
23#ifndef PROTOCORE_UART_H
24#define PROTOCORE_UART_H
25
27
28#include "protocore_config.h"
29
30EMBED_BEGIN_DECLS
31
32#if PROTOCORE_HAS_BUS
33
34/** @brief Read timeout in milliseconds, for a driver that takes whatever arrived. */
35#ifndef PROTOCORE_UART_TIMEOUT_MS
36#define PROTOCORE_UART_TIMEOUT_MS 20u
37#endif
38
39/** @brief Bring up @p unit at @p baud, 8N1, on @p rx_pin / @p tx_pin (-1 = the unit's default). */
40EMBED_INLINE embed_bool protocore_uart_begin(uint8_t unit, uint32_t baud, int rx_pin, int tx_pin)
41{
42 return protocore_platform_uart_begin(unit, baud, rx_pin, tx_pin) != 0;
43}
44
45/** @brief Write @p len bytes to @p unit. @return true if the driver took all of them. */
46EMBED_INLINE embed_bool protocore_uart_write(uint8_t unit, const uint8_t *buf, size_t len)
47{
48 return protocore_platform_uart_write(unit, buf, (int)len) == (int)len;
49}
50
51/**
52 * @brief Take up to @p len bytes from @p unit's receive ring, waiting at most @p ms for them.
53 * @return how many bytes were written into @p buf.
54 */
55EMBED_INLINE size_t protocore_uart_read(uint8_t unit, uint8_t *buf, size_t len, uint32_t ms)
56{
57 int n = protocore_platform_uart_read(unit, buf, (uint32_t)len, ms);
58 return n > 0 ? (size_t)n : 0u;
59}
60
61/** @brief Bytes sitting in @p unit's receive ring. */
62EMBED_INLINE size_t protocore_uart_available(uint8_t unit)
63{
64 return (size_t)protocore_platform_uart_available(unit);
65}
66
67/** @brief Take one byte from @p unit. @return true if one was there. */
68EMBED_INLINE embed_bool protocore_uart_read_byte(uint8_t unit, uint8_t *out)
69{
70 return protocore_uart_read(unit, out, 1, PROTOCORE_UART_TIMEOUT_MS) == 1u;
71}
72
73#else // no bus seam on this build
74
75EMBED_INLINE embed_bool protocore_uart_begin(uint8_t unit, uint32_t baud, int rx_pin, int tx_pin)
76{
77 (void)unit;
78 (void)baud;
79 (void)rx_pin;
80 (void)tx_pin;
81 return EMBED_TRUE;
82}
83
84EMBED_INLINE embed_bool protocore_uart_write(uint8_t unit, const uint8_t *buf, size_t len)
85{
86 (void)unit;
87 (void)buf;
88 (void)len;
89 return EMBED_FALSE;
90}
91
92EMBED_INLINE size_t protocore_uart_read(uint8_t unit, uint8_t *buf, size_t len, uint32_t ms)
93{
94 (void)unit;
95 (void)buf;
96 (void)len;
97 (void)ms;
98 return 0u;
99}
100
101EMBED_INLINE size_t protocore_uart_available(uint8_t unit)
102{
103 (void)unit;
104 return 0u;
105}
106
107EMBED_INLINE embed_bool protocore_uart_read_byte(uint8_t unit, uint8_t *out)
108{
109 return protocore_uart_read(unit, out, 1, PROTOCORE_UART_TIMEOUT_MS) == 1u;
110}
111
112#endif // PROTOCORE_HAS_BUS
113
114EMBED_END_DECLS
115
116#endif // PROTOCORE_UART_H
The platform contract: what the library asks of a target, in the library's own words.
EMBED_INLINE embed_bool protocore_uart_read_byte(uint8_t unit, uint8_t *out)
Definition uart.h:107
EMBED_INLINE embed_bool protocore_uart_write(uint8_t unit, const uint8_t *buf, size_t len)
Definition uart.h:84
EMBED_INLINE size_t protocore_uart_read(uint8_t unit, uint8_t *buf, size_t len, uint32_t ms)
Definition uart.h:92
EMBED_INLINE size_t protocore_uart_available(uint8_t unit)
Definition uart.h:101
EMBED_BEGIN_DECLS EMBED_INLINE embed_bool protocore_uart_begin(uint8_t unit, uint32_t baud, int rx_pin, int tx_pin)
Definition uart.h:75