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
30#if PROTOCORE_HAS_BUS
31
32/** @brief Read timeout in milliseconds, for a driver that takes whatever has arrived. */
33#ifndef PROTOCORE_UART_TIMEOUT_MS
34#define PROTOCORE_UART_TIMEOUT_MS 20u
35#endif
36
38
39/** @brief Bring up @p unit at @p baud, 8N1, on @p rx_pin / @p tx_pin (-1 = the unit's default). */
40PROTOCORE_INLINE proto_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. */
46PROTOCORE_INLINE proto_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 */
55PROTOCORE_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. */
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. */
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
75PROTOCORE_INLINE proto_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 PROTO_TRUE;
82}
83
84PROTOCORE_INLINE proto_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 PROTO_FALSE;
90}
91
92PROTOCORE_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
102{
103 (void)unit;
104 return 0u;
105}
106
108{
109 return protocore_uart_read(unit, out, 1, PROTOCORE_UART_TIMEOUT_MS) == 1u;
110}
111
113
114#endif // PROTOCORE_HAS_BUS
115
116#endif // PROTOCORE_UART_H
#define PROTOCORE_INLINE
Linkage for a leaf primitive whose body is cheaper than the call that reaches it.
The platform contract: what the library asks of a target, in the library's own words.
#define PROTO_FALSE
the false value
Definition types.h:68
#define PROTOCORE_BEGIN_DECLS
Give a header's declarations C linkage, so their symbol names carry no parameter types.
Definition types.h:96
_Bool proto_bool
The truth value.
Definition types.h:64
#define PROTO_TRUE
the true value, spelled so a caller never writes a bare 1
Definition types.h:67
#define PROTOCORE_END_DECLS
Definition types.h:97
PROTOCORE_INLINE size_t protocore_uart_available(uint8_t unit)
Definition uart.h:101
PROTOCORE_INLINE proto_bool protocore_uart_begin(uint8_t unit, uint32_t baud, int rx_pin, int tx_pin)
Definition uart.h:75
PROTOCORE_INLINE proto_bool protocore_uart_read_byte(uint8_t unit, uint8_t *out)
Definition uart.h:107
PROTOCORE_INLINE size_t protocore_uart_read(uint8_t unit, uint8_t *buf, size_t len, uint32_t ms)
Definition uart.h:92
PROTOCORE_INLINE proto_bool protocore_uart_write(uint8_t unit, const uint8_t *buf, size_t len)
Definition uart.h:84