diff --git a/CHANGELOG.md b/CHANGELOG.md index a0ca350..ce0b7ab 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -1,5 +1,22 @@ # Changes +## Unreleased + +- The modem's UART is listened to from boot, not only while the library + is open. With the power pins wired, `rb9704 power on` or a modem left + talking after `rb9704 end` sent its boot lines to nobody; they + overflowed the M5Stamp-C3's 128-byte UART FIFO, after which `begin` + heard only repeated fragments of old lines and failed with `-EIO` (1 in + 6 after a power cycle, none at all after a pause), and a second `begin` + crashed the board in the library's line parser. Bytes heard while the + library is closed are now thrown away, and when it opens part-way + through a line the rest of that line is too. On the Stamp: 8 of 8 power + cycles, and 12 of 12 with 0 to 5 s between power on and `begin`. +- The fake modem's `raw` and `before` write any bytes, now or just ahead + of its next reply. `test_a_boot_burst_while_closed` sends a 680-byte + boot burst before `begin`: the overflow above on a board through the + rig, a check that it does no harm on native_sim. + ## 0.2.0, 2026-10-25 - The shell sample over WiFi: `overlay-wifi.conf` puts the shell on diff --git a/README.md b/README.md index b9cf019..3d5dc25 100644 --- a/README.md +++ b/README.md @@ -62,11 +62,19 @@ The library's source shows several things a caller has to work around; asks the modem to abandon it. `rbBegin` empties the queue but not the count of it, which would make every send after a stuck one queue without transmitting; `rb9704_begin` zeroes the count. +- **A line with no status code reads past the library's buffer**, and on + a board it crashed. A modem talks before `rb9704_begin` opens the + library: its boot lines, once its power pins bring it up. The module + listens on the UART from boot and throws those bytes away, so a small + hardware FIFO never overflows into fragments of old lines, and when the + library opens part-way through a line the rest of that line is thrown + away too. Others are for upstream to fix, and are listed in `docs/JSPR.md` for that: `setState` waits for a misspelt target, a line with no status code reads -past its buffer, MT segment data is cut at 1446 characters, and the -Python binding never releases the GIL. +past its buffer (the module keeps the causes it knows of away from it), +MT segment data is cut at 1446 characters, and the Python binding never +releases the GIL. ## Using it diff --git a/docs/JSPR.md b/docs/JSPR.md index 4ca9f9f..b0386ba 100644 --- a/docs/JSPR.md +++ b/docs/JSPR.md @@ -723,6 +723,10 @@ Everything else is ignored. the end of the line through the zeroed buffer, then `memmove` with a negative length cast to `size_t` (`src/jspr.c:89-113`). Expect a crash. Never send banners, prompts or blank-ish lines of 9 bytes or more. + On an M5Stamp-C3 it did crash (2026-10-25), when the UART's FIFO had + overflowed and handed over fragments of old lines. rockblock9704-zephyr + now keeps the FIFO empty from boot and gives the library no line it + opened in the middle of (`src/rb9704_serial.c`). 3. **`resultCodeIndexStart` is not reset** after a line is discarded for the wrong target (`src/jspr.c:48,133-136`): if that line had leading junk, the next line is read from the same offset. Do not send leading junk. diff --git a/docs/boards/stamp_c3.md b/docs/boards/stamp_c3.md index de44407..d3fc7aa 100644 --- a/docs/boards/stamp_c3.md +++ b/docs/boards/stamp_c3.md @@ -172,5 +172,27 @@ has no I_BTD: tie GPIO7 to 3.3 V for those runs. | WiFi, the stored network, telnet | works (2026-10-24) | | a real 9704 on TX and RX, powered from the Stamp's 5V: `begin`, `info`, `signal` | works (2026-10-25) | | the same with the 9704's USB-C plugged in | fails: the FTDI holds the modem's RX | -| the LED and button | built, not yet run | -| P_EN, I_EN, I_BTD | not yet wired | +| P_EN, I_EN, I_BTD: `begin` powers it up in about 2 s, `power off`, `begin` again | works (2026-10-25), 8 of 8 cycles, and 12 of 12 with up to 5 s between `power on` and `begin` | +| under open sky: the constellation visible and 3 to 5 bars within seconds of `begin` | works (2026-10-25) | +| staying up while active, on the Stamp's 5V | fails: see below | +| provisioning, and a message sent | not yet | +| the LED and button | the button's short press works; the busy colour reads orange, not white, at this brightness | + +### Open: the modem stops answering after a few minutes + +On 2026-10-25, powered from the Stamp's 5V, the modem stopped answering +2 to 5 minutes after each `begin`, four times: once indoors, three times +under open sky. Every request then gets `-EIO`, its signal reports stop, +and it stays silent until the control lines are cycled; left alone with +P_EN and I_EN high it was still silent after 9 minutes. Indoors the RDY +LED went off when it happened. + +The likely cause is the supply: the 9704's input going below the 4.0 V +it needs while it charges, after the Stamp's USB-C, its 5V pin and the +leads. That is not yet measured. What would settle it: the voltage at +the 9704's supply pin over those minutes; the 9704 on a 5 V supply of +its own, GND joined to the Stamp's; and the 9704 on its own USB-C from a +laptop for 30 minutes, which has not been tried for longer than a few +seconds either. Whether the modem goes quiet on its serial port while it +provisions is also not known, and a test that power-cycles it on +silence would cut that short. diff --git a/python/rb9704tools/fakemodem.py b/python/rb9704tools/fakemodem.py index 29a44ca..8258c00 100644 --- a/python/rb9704tools/fakemodem.py +++ b/python/rb9704tools/fakemodem.py @@ -29,6 +29,9 @@ Control is one command per line on stdin, events one per line on stdout: mo-release send the withheld final status segment ask for MO segments of n bytes (default 1446) state the operationalState from now on + raw write text as it is, \r for CR: a line's + tail, junk, a line cut short + before write text just before the next reply quit READY @@ -95,6 +98,7 @@ class FakeModem: self.mo_refuse = False self.mo_hold = False # the next MO's final status waits for mo-release self.held = None # (topic, id, status) withheld + self.before = b"" # written ahead of the next reply self.imei = "300434030000000" self.serial = "123456" self.iccid = "8988169712345678901" @@ -123,6 +127,9 @@ class FakeModem: def request(self, line): self.events("RX " + line) + if self.before: + self.write(self.before) + self.before = b"" parts = line.split(" ", 2) if len(parts) < 2: return @@ -324,6 +331,15 @@ class FakeModem: self.segment = max(1, min(MO_SEGMENT_MAX, int(w[1]))) elif cmd == "state": self.state = w[1] + elif cmd in ("raw", "before"): + text = line.split(" ", 1)[1] if " " in line else "" + data = text.encode().decode("unicode_escape").encode("latin-1") + with self.lock: + if cmd == "raw": + self.write(data) + else: + self.before = data + self.events("%s %s" % (cmd.upper(), text)) elif cmd == "quit": return False elif cmd: diff --git a/python/tests/test_zephyr.py b/python/tests/test_zephyr.py index ba2f5cc..f02d58a 100644 --- a/python/tests/test_zephyr.py +++ b/python/tests/test_zephyr.py @@ -5,6 +5,7 @@ Zephyr, through the shell sample, against the fake modem: on native_sim with --zephyr-exe, on a board with --board-console and --board-modem. """ +import time import zlib import pytest @@ -102,6 +103,37 @@ def test_cancel_a_send_the_modem_sits_on(zephyr): c.out.expect(r"result 0") +def test_a_boot_burst_while_closed(zephyr): + """A modem powered up before begin talks to nobody: its boot lines, + over 600 bytes here, end part-way through one, finished just before + the first reply. On the M5Stamp-C3 such a burst overflowed the UART's + 128-byte FIFO while the module was not listening, and begin then heard + only repeated fragments of old lines; given one, the library read past + its buffer and the board crashed (2026-10-25). The module now empties + the FIFO from boot and skips the rest of a line it opened in. + + On a board through the rig this is that overflow. native_sim has no + FIFO to overflow: there it checks that the burst does no harm.""" + c, f = zephyr + boot = ('\\x11\\x11299 bootInfo {"image_type":"production","version":{"major":1,' + '"minor":5,"patch":14,"build_info":"SX-250613-A3E9C3E3-P1-P3142-1194848-' + '12516979"},"boot_source":"primary"}\\r') + lines = (boot + + '299 simStatus {"card_present":true,"sim_connected":true,' + '"iccid":"8988169712345678901"}\\r' + + '299 operationalState {"state":"inactive","reason":0}\\r' * 4 + + '299 constellationState {"constellation_visible":false,"signal_bars":0}\\r' * 2 + + '299 bootInfo {"image_type":"prod') + f.cmd("raw " + lines) + f.out.expect(r"^RAW ") + time.sleep(0.5) + f.cmd('before uction"}\\r') + f.out.expect(r"^BEFORE ") + begin(zephyr) + c.sh("rb9704 info") + c.out.expect(r"imei\s+300434030000000") + + def test_topic_below_64_is_refused_here(zephyr): c, f = zephyr begin(zephyr) diff --git a/src/rb9704_internal.h b/src/rb9704_internal.h index 282d1f5..71f869f 100644 --- a/src/rb9704_internal.h +++ b/src/rb9704_internal.h @@ -15,6 +15,8 @@ #define RB9704_NODE DT_COMPAT_GET_ANY_STATUS_OKAY(rock7_rockblock_9704) /* rb9704_serial.c */ +/* The receive interrupt, on from boot (rb9704_serial.c says why). */ +int rb9704_serial_start(void); int rb9704_serial_available(void); uint32_t rb9704_serial_dropped(void); void rb9704_serial_spy(bool on); diff --git a/src/rb9704_serial.c b/src/rb9704_serial.c index 0c1c859..9c30e07 100644 --- a/src/rb9704_serial.c +++ b/src/rb9704_serial.c @@ -12,6 +12,17 @@ * bytes one at a time from receiveJspr(). TX is polled: a worst-case 2 kB * command is about 90 ms at 230400, and only the worker thread ever writes. * + * The receive interrupt is on from boot, not only while the library is + * open. A modem powered up by `rb9704 power on`, or left talking after + * rbEnd(), sends lines nobody is reading; with the interrupt off they + * overflowed the UART's hardware FIFO, 128 bytes on the ESP32-C3, and the + * UART then gave back repeated fragments of old lines and never the + * replies (2026-10-25, M5Stamp-C3). Now the ISR always empties the FIFO + * and throws the bytes away while the library is closed. When it opens + * part-way through a line, the rest of that line is thrown away too: a + * line with no status code at its front makes the library read past its + * buffer (docs/JSPR.md, section 8, item 2), and that crashed the board. + * * The spy (rb9704_spy): JSPR is printable JSON lines, so the spy reassembles * the lines in each direction and printk()s them. Do not use the library's * own -DDEBUG for this: its TX debug path truncates the caller's buffer in @@ -24,6 +35,8 @@ #include #include +#include + #include "rb9704_glue.h" #include "rb9704_internal.h" #include "serial.h" /* the library's serialContext */ @@ -38,6 +51,15 @@ static K_SEM_DEFINE(rb9704_rx_sem, 0, 1); static atomic_t rx_dropped; static atomic_t spy_on; +/* Whether the library is open, so received bytes are for it. */ +static atomic_t listening; +/* ISR only: the last byte heard ended a line, or there was none yet. + * Non-printable bytes do not count: the modem sends a few at boot, and the + * library already skips junk in front of a status code. */ +static bool mid_line; +/* Set as the library opens mid-line: drop bytes up to the next CR. */ +static atomic_t skip_to_cr; + /* Per-byte RX timeout. receiveJspr() treats a 0-return mid-line as * end-of-data and discards the partial line, so this must comfortably * exceed any inter-byte gap the modem produces (gaps at 230400 are @@ -99,14 +121,34 @@ static void modem_uart_isr(const struct device *dev, void *user_data) while (uart_irq_update(dev) && uart_irq_rx_ready(dev)) { uint8_t tmp[64]; int n = uart_fifo_read(dev, tmp, sizeof(tmp)); + int keep = 0; if (n <= 0) { break; } - uint32_t put = ring_buf_put(&rb9704_rx_ring, tmp, n); + for (int i = 0; i < n; i++) { + uint8_t c = tmp[i]; + bool skip = !atomic_get(&listening) || atomic_get(&skip_to_cr); - if (put < (uint32_t)n) { - atomic_add(&rx_dropped, n - put); + if (c == '\r') { + mid_line = false; + if (atomic_get(&skip_to_cr)) { + atomic_clear(&skip_to_cr); + } + } else if (c >= 0x20 && c < 0x7f) { + mid_line = true; + } + if (!skip) { + tmp[keep++] = c; + } + } + if (keep == 0) { + continue; + } + uint32_t put = ring_buf_put(&rb9704_rx_ring, tmp, keep); + + if (put < (uint32_t)keep) { + atomic_add(&rx_dropped, keep - put); } k_sem_give(&rb9704_rx_sem); rb9704_worker_rx(); @@ -121,16 +163,19 @@ static bool ser_init(void) printk("rb9704: UART %s not ready\n", modem_uart->name); return false; } + /* The ISR does not touch the ring while not listening, so the reset + * needs no lock. */ ring_buf_reset(&rb9704_rx_ring); k_sem_reset(&rb9704_rx_sem); - uart_irq_callback_user_data_set(modem_uart, modem_uart_isr, NULL); - uart_irq_rx_enable(modem_uart); + atomic_set(&skip_to_cr, mid_line ? 1 : 0); + atomic_set(&listening, 1); return true; } static bool ser_deinit(void) { - uart_irq_rx_disable(modem_uart); + /* The interrupt stays on, emptying the FIFO into nothing. */ + atomic_clear(&listening); return true; } @@ -175,6 +220,16 @@ static int ser_peek(void) /* --- setup + worker-facing helpers -------------------------------------- */ +int rb9704_serial_start(void) +{ + if (!device_is_ready(modem_uart)) { + return -ENODEV; + } + uart_irq_callback_user_data_set(modem_uart, modem_uart_isr, NULL); + uart_irq_rx_enable(modem_uart); + return 0; +} + bool rb9704_serial_set_context(const char *port, uint32_t baud) { ARG_UNUSED(port); /* the UART is the devicetree node's bus */ diff --git a/src/rb9704_worker.c b/src/rb9704_worker.c index 046b0aa..2eb80ec 100644 --- a/src/rb9704_worker.c +++ b/src/rb9704_worker.c @@ -609,6 +609,9 @@ static void worker(void *a, void *b, void *c) LOG_ERR("power pins not ready"); } k_mutex_unlock(&rb9704_st_lock); + if (rb9704_serial_start() != 0) { + LOG_ERR("modem UART not ready"); + } for (;;) { uint32_t ev = k_event_wait(&rb9704_ev, EV_REQ | EV_RX, false, K_FOREVER);