Files
rockblock9704-zephyr/tests/rig/bridge/src/main.c
T
scottp acd4263a06 0.4.0: builds on Zephyr 4.5 as well as 4.4
Zephyr 4.5 made uart_irq_update() return void, and the receive
interrupt used it as its loop condition, so 0.3.0 does not compile on
4.5. It is now called at the top of each pass, as Zephyr's own samples
do.

From 4.5 the ring buffer is header-only, and CONFIG_RING_BUFFER brings
back the deprecated legacy implementation. CONFIG_RB9704 selects it only
when KERNELVERSION is below 4.5. The rig bridge reads its ring with
ring_buf_peek() and ring_buf_get() in place of the claim API, and gates
the option the same way in a Kconfig of its own.

Tested on v4.4.0 and v4.5.0: 37 pass and 2 skip against the fake on
native_sim, and the sample, rigs and bridges build for every board.

Also in this release, from main since 0.3.0: rbtest in the shell sample,
rb9704_send_fill, and the Stamp-C3 overnight run. The README has a
section on the Zephyr versions; the standalone manifest pins v4.5.0;
the Python tools are 0.4.0.
2026-11-26 16:21:03 +11:00

75 lines
2.2 KiB
C

/* SPDX-License-Identifier: MIT
* Copyright (c) 2026 Scott Penrose <scottp@dd.com.au>
*
* A two-way UART bridge, for the test rig: every byte in on `host` goes out
* on `target`, and the other way round. With `host` a board's USB serial
* port and `target` a UART wired to another board's modem pins, the fake
* modem on the host answers that board as a modem would. The aliases are
* the board overlay's. */
#include <zephyr/kernel.h>
#include <zephyr/device.h>
#include <zephyr/drivers/uart.h>
#include <zephyr/sys/ring_buffer.h>
static const struct device *const host = DEVICE_DT_GET(DT_ALIAS(bridge_host));
static const struct device *const target = DEVICE_DT_GET(DT_ALIAS(bridge_target));
RING_BUF_DECLARE(to_target, 4096);
RING_BUF_DECLARE(to_host, 4096);
static void isr(const struct device *dev, void *user)
{
struct ring_buf *out = user; /* bytes read here go into this ring */
const struct device *other = dev == host ? target : host;
struct ring_buf *back = out == &to_target ? &to_host : &to_target;
uint8_t b[64];
/* uart_irq_update() is void since Zephyr 4.5; the STM32, ESP32
* and native drivers returned 1 from it before that. */
while (true) {
uart_irq_update(dev);
if (uart_irq_is_pending(dev) <= 0) {
break;
}
if (uart_irq_rx_ready(dev)) {
int n = uart_fifo_read(dev, b, sizeof(b));
if (n > 0) {
ring_buf_put(out, b, n);
uart_irq_tx_enable(other);
}
}
if (uart_irq_tx_ready(dev)) {
/* Peek, fill, then take what the FIFO took: the claim
* API this used is legacy from Zephyr 4.5, and peek and
* get are in both. */
uint8_t t[64];
uint32_t n = ring_buf_peek(back, t, sizeof(t));
if (n == 0) {
/* The other port's ISR may have put bytes in `back`
* and re-enabled TX since the claim; they then wait
* for its next byte, which with two-way traffic is
* soon. Not worth a lock here. */
uart_irq_tx_disable(dev);
} else {
int w = uart_fifo_fill(dev, t, n);
if (w > 0) {
ring_buf_get(back, t, (uint32_t)w);
}
}
}
}
}
int main(void)
{
uart_irq_callback_user_data_set(host, isr, &to_target);
uart_irq_callback_user_data_set(target, isr, &to_host);
uart_irq_rx_enable(host);
uart_irq_rx_enable(target);
return 0;
}