From 7c794c7fcbaafa3ae0e5c4c7c3e5565ab6323e28 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 19:37:29 +0300 Subject: [PATCH 01/15] RXEN signal fix - fixed a typo in the comment - reversed polarity of the RXEN signal (should be high for RX) - MCU pin connected to the RXEN line is no longer hi-z --- src/r5f1026a/platform.c | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index 6cebaac..064c7a5 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -118,7 +118,7 @@ void uart_init(void) { PMC1_bit.no2 = 0; // P12 (TxD0) is digital I/O PM1_bit.no1 = 1; // P11 (RxD0) is input - PMC1_bit.no1 = 0; // P12 (RxD0) is digital I/O + PMC1_bit.no1 = 0; // P11 (RxD0) is digital I/O SS0 |= 0x03U; // Serial channel start operation @@ -381,7 +381,7 @@ void platform_init(void) { P1_bit.no3 = 1; // P13 (PDN) is high PM1_bit.no3 = 0; // P13 (PDN) is output - PMC1_bit.no1 = 0; // P10 (RXEN) is digital I/O + PMC1_bit.no0 = 0; // P10 (RXEN) is digital I/O P1_bit.no0 = 0; // P10 (RXEN) is low PM1_bit.no0 = 0; // P10 (RXEN) is output @@ -467,16 +467,16 @@ bool platform_poke(uint8_t reg, uint16_t val) { return false; } - // TX requested so disable RXEN + // TX requested so disable RXEN (LNA supply off) if (reg == 0x30 && (val & TX)) { - P1_bit.no0 = 1; // P10 (RXEN) is high + P1_bit.no0 = 0; // P10 (RXEN) is low } i2c_write(I2C_ADDR_XCVR, reg, val); - // RX requested so enable RXEN + // RX requested so enable RXEN (LNA supply on) if (reg == 0x30 && (val & RX)) { - P1_bit.no0 = 0; // P10 (RXEN) is low + P1_bit.no0 = 1; // P10 (RXEN) is high } return true; From c6803dae1a383886a1ab0283eb91b83d09741e37 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski <44336093+sp5wwp@users.noreply.github.com> Date: Thu, 8 Oct 2026 19:43:37 +0300 Subject: [PATCH 02/15] Add workflow_dispatch trigger to main.yaml --- .github/workflows/main.yaml | 1 + 1 file changed, 1 insertion(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index e209a0d..b981ef0 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -1,6 +1,7 @@ name: Build sa8x8-fw on: + workflow_dispatch: push: paths: - src/** From 0b5ce8d1f3680a7999f6bb21b4d9fabeeaa7ada7 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 19:46:55 +0300 Subject: [PATCH 03/15] Initialize the ring buffer before platform_init() --- src/sa8x8.c | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/src/sa8x8.c b/src/sa8x8.c index 411b9d6..9cc94b7 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -171,13 +171,14 @@ uint16_t a2i(const char *s, uint8_t *pos) { * Application entrypoint */ int main(void) { - // Perform platform specific initialization - platform_init(); - - // Create ring buffer for received UART data + // Create ring buffer for received UART data before platform_init() + // enables the UART receive interrupt, which writes into it struct item buf[16]; ring_init(&rx, buf, 16); + // Perform platform specific initialization + platform_init(); + // Create command buffer and offset pointer char cmd[32]; char *c; From 3e5e228b2e54c1afbb8032e28eb25707e618e828 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 19:54:56 +0300 Subject: [PATCH 04/15] Proper wait for the UART TX to complete --- src/r5f1026a/platform.c | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index 064c7a5..e99ce6d 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -408,8 +408,9 @@ void platform_sleep(void) { } void platform_turbo(void) { - while (SSR00L & 0x20U) - ; // Wait for any pending transmit data + while (SSR00L & 0x60U) + ; // Wait until the transmit buffer is empty (BFF00, bit 5) and the + // last character has finished shifting out (TSF00, bit 6) delay(1000); From a5ae3ee115f9608aedcc6ac3e3e8e1c3497e3297 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 19:58:14 +0300 Subject: [PATCH 05/15] AT+TURBO is not implemented on the R5R0C002 --- src/common.h | 1 + src/r5f1026a/platform.c | 5 +++++ src/r5r0c002/platform.c | 5 +++++ src/sa8x8.c | 7 +++++++ 4 files changed, 18 insertions(+) diff --git a/src/common.h b/src/common.h index eb235ab..44ffedc 100644 --- a/src/common.h +++ b/src/common.h @@ -119,6 +119,7 @@ bool platform_peek(uint8_t reg, uint16_t *val); bool platform_poke(uint8_t reg, uint16_t val); void platform_amp(bool enabled); void platform_audio(bool enabled); +extern const bool PLATFORM_TURBO; /* * UART peripheral control diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index e99ce6d..0fc5b3d 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -23,6 +23,11 @@ */ const uint8_t I2C_ADDR_XCVR = 0xE2U; +/* + * High speed UART mode (AT+TURBO) is supported + */ +const bool PLATFORM_TURBO = true; + /* * Successfully do nothing */ diff --git a/src/r5r0c002/platform.c b/src/r5r0c002/platform.c index 29f1dcd..693ad73 100644 --- a/src/r5r0c002/platform.c +++ b/src/r5r0c002/platform.c @@ -23,6 +23,11 @@ */ const uint8_t I2C_ADDR_XCVR = 0xE2U; +/* + * High speed UART mode (AT+TURBO) is not implemented + */ +const bool PLATFORM_TURBO = false; + /* * Successfully do nothing */ diff --git a/src/sa8x8.c b/src/sa8x8.c index 9cc94b7..233e63f 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -236,6 +236,13 @@ int main(void) { // AT+TURBO: Enter high speed UART mode if (eq(&cmd[2], (char *)CMD_TURBO, sizeof(CMD_TURBO))) { + // Refuse if the platform cannot change its baud rate, so the host + // does not switch speed while the module stays at the old one + if (!PLATFORM_TURBO) { + uart_puts(ERR); + continue; + } + uart_puts(OK); platform_turbo(); continue; From d1aa1323e6e0d97d21ae11d0909dea4fea43d8af Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski <44336093+sp5wwp@users.noreply.github.com> Date: Thu, 8 Oct 2026 20:07:45 +0300 Subject: [PATCH 06/15] Update GitHub Actions to use newer action versions --- .github/workflows/main.yaml | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index b981ef0..241b095 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -21,7 +21,7 @@ jobs: steps: - name: Checkout - uses: actions/checkout@v3 + uses: actions/checkout@v7 - name: Setup uses: eWaterCycle/setup-apptainer@v2 @@ -36,7 +36,7 @@ jobs: done - name: Upload - uses: actions/upload-artifact@v4 + uses: actions/upload-artifact@v7 with: name: sa8x8-fw-srecs path: | From 600abc6d226d41461fa37c15d50f0084586a5c16 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski <44336093+sp5wwp@users.noreply.github.com> Date: Thu, 8 Oct 2026 20:08:55 +0300 Subject: [PATCH 07/15] Change Apptainer setup to manual installation Replaced setup action with manual installation of Apptainer. --- .github/workflows/main.yaml | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 241b095..901cabd 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -24,9 +24,9 @@ jobs: uses: actions/checkout@v7 - name: Setup - uses: eWaterCycle/setup-apptainer@v2 - with: - apptainer-version: 1.3.6 + run: | + wget -q https://github.com/apptainer/apptainer/releases/download/v1.3.6/apptainer_1.3.6_amd64.deb + sudo apt-get install -y ./apptainer_1.3.6_amd64.deb - name: Compile run: | From 3493788e3893eab6515306be3328f27bfc60cef8 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 20:36:42 +0300 Subject: [PATCH 08/15] Make the ring items volatile --- src/common.h | 14 +++++++++++--- 1 file changed, 11 insertions(+), 3 deletions(-) diff --git a/src/common.h b/src/common.h index 44ffedc..f1bb337 100644 --- a/src/common.h +++ b/src/common.h @@ -64,20 +64,28 @@ typedef struct { /* * A single ring buffer item + * + * The character is written by the UART receive interrupt and read by the + * main loop, so it is volatile to keep the compiler from caching it. */ struct item { - char c; + volatile char c; struct item *next; }; /* * A generic ring buffer structure + * + * The put and get pointers are shared between the UART receive interrupt + * (producer) and the main loop (consumer). They are volatile so that the + * busy-wait in uart_gets() rereads them on every iteration, even if the + * compiler inlines the ring functions (for example with LTO). */ struct ring { uint16_t size; struct item *buffer; - struct item *put; - struct item *get; + struct item *volatile put; + struct item *volatile get; }; /* From 936caf000ddbe9d8ec10c8a9616343d040a32d67 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 20:42:21 +0300 Subject: [PATCH 09/15] Take I2C ACK into account --- src/common.h | 4 +-- src/r5f1026a/platform.c | 61 +++++++++++++++++++++++++++-------------- src/r5r0c002/platform.c | 61 +++++++++++++++++++++++++++-------------- src/sa8x8.c | 8 ++++-- 4 files changed, 88 insertions(+), 46 deletions(-) diff --git a/src/common.h b/src/common.h index f1bb337..4948f11 100644 --- a/src/common.h +++ b/src/common.h @@ -140,8 +140,8 @@ int uart_gets(char *s, int size); * I2C peripheral control */ void i2c_init(void); -void i2c_write(uint8_t addr, uint8_t reg, uint16_t val); -uint16_t i2c_read(uint8_t addr, uint8_t reg); +bool i2c_write(uint8_t addr, uint8_t reg, uint16_t val); +bool i2c_read(uint8_t addr, uint8_t reg, uint16_t *val); extern const uint8_t I2C_ADDR_XCVR; /* diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index 0fc5b3d..572b547 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -245,7 +245,7 @@ static inline void _i2c_stop() { /* * Low-level I2C bus write transaction */ -static void _i2c_write(uint8_t val) { +static bool _i2c_write(uint8_t val) { _i2c_sdio_output(); for (uint8_t i = 0; i < 8; i++) { @@ -273,12 +273,17 @@ static void _i2c_write(uint8_t val) { _i2c_sclk_high(); delay(2); + // Slave acknowledges by pulling SDIO low during the ninth clock + bool ack = !_i2c_sdio_value(); + _i2c_sclk_low(); delay(1); _i2c_sdio_high(); _i2c_sdio_output(); delay(6); + + return ack; } /* @@ -340,32 +345,44 @@ void i2c_init(void) { /* * High-level I2C bus write transaction */ -void i2c_write(uint8_t addr, uint8_t reg, uint16_t val) { +bool i2c_write(uint8_t addr, uint8_t reg, uint16_t val) { uint8_t hi = (uint8_t)((val >> 8) & 0xFF); uint8_t lo = val & 0xFF; + // Abort at the first byte the slave does not acknowledge _i2c_start(); - _i2c_write(addr); - _i2c_write(reg); - _i2c_write(hi); - _i2c_write(lo); + bool ack = _i2c_write(addr) && _i2c_write(reg) && _i2c_write(hi) && + _i2c_write(lo); _i2c_stop(); + + return ack; } /* * High-level I2C bus read transaction */ -uint16_t i2c_read(uint8_t addr, uint8_t reg) { +bool i2c_read(uint8_t addr, uint8_t reg, uint16_t *val) { + // Address the register, abort if the slave does not acknowledge _i2c_start(); - _i2c_write(addr); - _i2c_write(reg); + if (!(_i2c_write(addr) && _i2c_write(reg))) { + _i2c_stop(); + return false; + } + + // Repeated start in read mode, abort if the slave does not acknowledge _i2c_start(); - _i2c_write(addr | 0x01); + if (!_i2c_write(addr | 0x01)) { + _i2c_stop(); + return false; + } + uint8_t hi = _i2c_read(true); uint8_t lo = _i2c_read(false); _i2c_stop(); - return ((uint16_t)hi << 8) | lo; + *val = ((uint16_t)hi << 8) | lo; + + return true; } void platform_init(void) { @@ -445,14 +462,16 @@ void platform_refresh(struct platform_state *state) { if (ptt && !state->ptt) { // Enable PTT if requested and not enabled previously - platform_peek(0x30, &val); - val = (val | TX) & ~RX; - platform_poke(0x30, val); + if (platform_peek(0x30, &val)) { + val = (val | TX) & ~RX; + platform_poke(0x30, val); + } } else if (!ptt && state->ptt) { // Disable PTT if requested and enabled previously - platform_peek(0x30, &val); - val = (val & ~TX) | RX; - platform_poke(0x30, val); + if (platform_peek(0x30, &val)) { + val = (val & ~TX) | RX; + platform_poke(0x30, val); + } } state->ptt = ptt; // Push to talk from external GPIO @@ -462,9 +481,7 @@ void platform_refresh(struct platform_state *state) { } bool platform_peek(uint8_t reg, uint16_t *val) { - *val = i2c_read(I2C_ADDR_XCVR, reg); - - return true; + return i2c_read(I2C_ADDR_XCVR, reg, val); } bool platform_poke(uint8_t reg, uint16_t val) { @@ -478,7 +495,9 @@ bool platform_poke(uint8_t reg, uint16_t val) { P1_bit.no0 = 0; // P10 (RXEN) is low } - i2c_write(I2C_ADDR_XCVR, reg, val); + if (!i2c_write(I2C_ADDR_XCVR, reg, val)) { + return false; + } // RX requested so enable RXEN (LNA supply on) if (reg == 0x30 && (val & RX)) { diff --git a/src/r5r0c002/platform.c b/src/r5r0c002/platform.c index 693ad73..7f785d1 100644 --- a/src/r5r0c002/platform.c +++ b/src/r5r0c002/platform.c @@ -161,7 +161,7 @@ static inline void _i2c_stop() { /* * Low-level I2C bus write transaction */ -static void _i2c_write(uint8_t val) { +static bool _i2c_write(uint8_t val) { _i2c_sdio_output(); for (uint8_t i = 0; i < 8; i++) { @@ -189,12 +189,17 @@ static void _i2c_write(uint8_t val) { _i2c_sclk_high(); delay(2); + // Slave acknowledges by pulling SDIO low during the ninth clock + bool ack = !_i2c_sdio_value(); + _i2c_sclk_low(); delay(1); _i2c_sdio_high(); _i2c_sdio_output(); delay(6); + + return ack; } /* @@ -249,32 +254,44 @@ void i2c_init(void) { /* * High-level I2C bus write transaction */ -void i2c_write(uint8_t addr, uint8_t reg, uint16_t val) { +bool i2c_write(uint8_t addr, uint8_t reg, uint16_t val) { uint8_t hi = (uint8_t)((val >> 8) & 0xFF); uint8_t lo = val & 0xFF; + // Abort at the first byte the slave does not acknowledge _i2c_start(); - _i2c_write(addr); - _i2c_write(reg); - _i2c_write(hi); - _i2c_write(lo); + bool ack = _i2c_write(addr) && _i2c_write(reg) && _i2c_write(hi) && + _i2c_write(lo); _i2c_stop(); + + return ack; } /* * High-level I2C bus read transaction */ -uint16_t i2c_read(uint8_t addr, uint8_t reg) { +bool i2c_read(uint8_t addr, uint8_t reg, uint16_t *val) { + // Address the register, abort if the slave does not acknowledge _i2c_start(); - _i2c_write(addr); - _i2c_write(reg); + if (!(_i2c_write(addr) && _i2c_write(reg))) { + _i2c_stop(); + return false; + } + + // Repeated start in read mode, abort if the slave does not acknowledge _i2c_start(); - _i2c_write(addr | 0x01); + if (!_i2c_write(addr | 0x01)) { + _i2c_stop(); + return false; + } + uint8_t hi = _i2c_read(true); uint8_t lo = _i2c_read(false); _i2c_stop(); - return ((uint16_t)hi << 8) | lo; + *val = ((uint16_t)hi << 8) | lo; + + return true; } void platform_init(void) { @@ -330,14 +347,16 @@ void platform_refresh(struct platform_state *state) { if (ptt && !state->ptt) { // Enable PTT if requested and not enabled previously - platform_peek(0x30, &val); - val = (val | TX) & ~RX; - platform_poke(0x30, val); + if (platform_peek(0x30, &val)) { + val = (val | TX) & ~RX; + platform_poke(0x30, val); + } } else if (!ptt && state->ptt) { // Disable PTT if requested and enabled previously - platform_peek(0x30, &val); - val = (val & ~TX) | RX; - platform_poke(0x30, val); + if (platform_peek(0x30, &val)) { + val = (val & ~TX) | RX; + platform_poke(0x30, val); + } } state->ptt = ptt; // Push to talk from external GPIO @@ -347,9 +366,7 @@ void platform_refresh(struct platform_state *state) { } bool platform_peek(uint8_t reg, uint16_t *val) { - *val = i2c_read(I2C_ADDR_XCVR, reg); - - return true; + return i2c_read(I2C_ADDR_XCVR, reg, val); } bool platform_poke(uint8_t reg, uint16_t val) { @@ -363,7 +380,9 @@ bool platform_poke(uint8_t reg, uint16_t val) { p3_7 = 1; // P3_7 (RXEN) is high } - i2c_write(I2C_ADDR_XCVR, reg, val); + if (!i2c_write(I2C_ADDR_XCVR, reg, val)) { + return false; + } // RX requested so enable RXEN if (reg == 0x30 && (val & RX)) { diff --git a/src/sa8x8.c b/src/sa8x8.c index 233e63f..7ddcda3 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -261,8 +261,12 @@ int main(void) { continue; } - // Read requested register - uint16_t val = i2c_read(I2C_ADDR_XCVR, (uint8_t)reg); + // Read requested register, error if the transceiver does not respond + uint16_t val; + if (!platform_peek((uint8_t)reg, &val)) { + uart_puts(ERR); + continue; + } // Send register contents as response uart_puts(i2a(val)); From fbf7213d6a211199a7bf71470f2dd4b8f08f71ef Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 20:49:57 +0300 Subject: [PATCH 10/15] Added AT+TOT timeout command - the timeout is 180 seconds by default --- README.md | 1 + src/common.h | 4 ++ src/r5f1026a/interrupts.c | 5 +- src/r5f1026a/platform.c | 97 ++++++++++++++++++++++++++++++++++++++- src/r5r0c002/platform.c | 7 +++ src/sa8x8.c | 26 +++++++++++ 6 files changed, 137 insertions(+), 3 deletions(-) diff --git a/README.md b/README.md index 6500903..fc8d561 100644 --- a/README.md +++ b/README.md @@ -21,6 +21,7 @@ The following commands are available in this firmware: | AT+POKE=_REG_,_VAL_ | Set transceiver register contents | | AT+AMP=_0_/_1_ | Set power amplifier state | | AT+AUDIO=_0_/_1_ | Set external audio amplifier state | +| AT+TOT=_SEC_ | Set transmit timeout, 0 disables | ## Supported Modules diff --git a/src/common.h b/src/common.h index 4948f11..4b92347 100644 --- a/src/common.h +++ b/src/common.h @@ -26,6 +26,8 @@ #define TX (1U << 6) #define RX (1U << 5) +#define TOT_DEFAULT 180U // Default transmit timeout in seconds, 0 disables + /* * Bit-level access for uint8_t */ @@ -128,6 +130,7 @@ bool platform_poke(uint8_t reg, uint16_t val); void platform_amp(bool enabled); void platform_audio(bool enabled); extern const bool PLATFORM_TURBO; +bool platform_tot(uint16_t seconds); /* * UART peripheral control @@ -148,6 +151,7 @@ extern const uint8_t I2C_ADDR_XCVR; * Time management */ void delay(uint16_t n); +extern volatile uint16_t uptime; #endif diff --git a/src/r5f1026a/interrupts.c b/src/r5f1026a/interrupts.c index bd4b385..39f6fc2 100644 --- a/src/r5f1026a/interrupts.c +++ b/src/r5f1026a/interrupts.c @@ -36,7 +36,10 @@ void INT_SRE0(void) {} void INT_TM01H(void) {} void INT_TM03H(void) {} void INT_IICA0(void) {} -void INT_TM00(void) {} +void INT_TM00(void) { + uptime++; + TMIF00 = 0U; +} void INT_TM01(void) {} void INT_TM02(void) {} void INT_TM03(void) {} diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index 572b547..07c84ce 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -28,6 +28,20 @@ const uint8_t I2C_ADDR_XCVR = 0xE2U; */ const bool PLATFORM_TURBO = true; +/* + * Seconds since boot, incremented by the TAU0 channel 0 interrupt + */ +volatile uint16_t uptime = 0; + +/* + * Transmit timeout state + */ +static uint16_t tot_limit = TOT_DEFAULT; // Timeout in seconds, 0 disables +static bool tx_active = false; // Transmitter currently keyed +static uint16_t tx_start = 0; // Uptime when transmitter was keyed + +static void _tot_check(void); + /* * Successfully do nothing */ @@ -160,8 +174,10 @@ int uart_gets(char *s, int size) { char c; while (len < size - 1) { - while (!ring_get(&rx, &c)) - ; + // Keep enforcing the transmit timeout while waiting for a full command + while (!ring_get(&rx, &c)) { + _tot_check(); + } if (c == '\r' || c == '\n') { break; } @@ -385,6 +401,37 @@ bool i2c_read(uint8_t addr, uint8_t reg, uint16_t *val) { return true; } +/* + * Start a 1 Hz tick on timer array unit 0 channel 0 + */ +static void timer_init(void) { + TAU0EN = 1; // Release and enable clock to timer array unit 0 + delay(1); // Delay recommended by hardware manual + + TPS0 = 0x000FU; // Set operation clock: + //* <03:00>PRS00=1111: CK00 is fCLK / 2**15 = 450 Hz + + TT0 = 0x0001U; // Stop timer channel 0 + + TMMK00 = 1U; // Disable INTTM00 interrupt + TMIF00 = 0U; // Clear INTTM00 interrupt flag + TMPR100 = 1U; // Set INTTM00 lowest priority + TMPR000 = 1U; // Set INTTM00 lowest priority + + TMR00 = 0x0000U; // Set operation mode: + //* <15:14>CKS00=00: Operation clock CK00 set by TPS0 + //* <10:08>STS00=000: Software trigger start only + //* <03:01>MD00=000: Interval timer mode + //* <00>MD000=0: No interrupt when counting starts + + TDR00 = 449U; // 450 Hz / (449 + 1) = 1 Hz + + TOE0 &= (uint16_t)~0x0001U; // No timer output on TO00 + + TMMK00 = 0U; // Enable INTTM00 interrupt + TS0 = 0x0001U; // Start timer channel 0 +} + void platform_init(void) { asm("di"); // Disable interrupts @@ -422,6 +469,8 @@ void platform_init(void) { PM2_bit.no3 = 1; // P23 (GPIO6) is input: squelch indicator // P137 (GPIO7) is input: vox indicator + timer_init(); // Start 1 Hz tick for the transmit timeout + asm("ei"); // Enable interrupts } @@ -460,6 +509,10 @@ void platform_refresh(struct platform_state *state) { bool ptt = !P2_bit.no2; uint16_t val; + // End the transmission if it has exceeded the timeout. A key held through + // the timeout must be released and pressed again before transmitting. + _tot_check(); + if (ptt && !state->ptt) { // Enable PTT if requested and not enabled previously if (platform_peek(0x30, &val)) { @@ -499,6 +552,18 @@ bool platform_poke(uint8_t reg, uint16_t val) { return false; } + // Track transmitter state for the transmit timeout + if (reg == 0x30) { + if (val & TX) { + if (!tx_active) { + tx_start = uptime; // Timeout counts from the moment TX is keyed + } + tx_active = true; + } else { + tx_active = false; + } + } + // RX requested so enable RXEN (LNA supply on) if (reg == 0x30 && (val & RX)) { P1_bit.no0 = 1; // P10 (RXEN) is high @@ -526,3 +591,31 @@ void platform_audio(bool enabled) { P2_bit.no0 = 1; // P20 (SQ) is high } } + +bool platform_tot(uint16_t seconds) { + tot_limit = seconds; + + return true; +} + +/* + * Return to receive if the transmitter has been keyed longer than the timeout + */ +static void _tot_check(void) { + uint16_t val; + + if (!tx_active || tot_limit == 0) { + return; + } + + // Unsigned subtraction stays correct across uptime wraparound + if ((uint16_t)(uptime - tx_start) < tot_limit) { + return; + } + + // Retried on the next call if the transceiver does not respond + if (platform_peek(0x30, &val)) { + val = (val & ~TX) | RX; + platform_poke(0x30, val); + } +} diff --git a/src/r5r0c002/platform.c b/src/r5r0c002/platform.c index 7f785d1..05c477f 100644 --- a/src/r5r0c002/platform.c +++ b/src/r5r0c002/platform.c @@ -411,3 +411,10 @@ void platform_audio(bool enabled) { p1_0 = 1; // P1_0 (SQ) is high } } + +bool platform_tot(uint16_t seconds) { + // Not implemented + (void)seconds; + + return false; +} diff --git a/src/sa8x8.c b/src/sa8x8.c index 7ddcda3..d78dd6c 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -25,6 +25,7 @@ const char CMD_PEEK[] = "+PEEK="; const char CMD_POKE[] = "+POKE="; const char CMD_AMP[] = "+AMP="; const char CMD_AUDIO[] = "+AUDIO="; +const char CMD_TOT[] = "+TOT="; const char VERSION[] = "sa8x8-fw/" GIT_INFO "" CRLF; const char MODEL[] = MODULE_MODEL "" CRLF; @@ -353,6 +354,31 @@ int main(void) { continue; } + // AT+TOT=: Set transmit timeout in seconds, 0 disables + if (eq(&cmd[2], (char *)CMD_TOT, sizeof(CMD_TOT) - 1)) { + uint8_t i = sizeof(CMD_TOT) + 1; + + // Parse timeout + uint16_t seconds = a2i(cmd, &i); + + // Error if missing terminator + if (!(cmd[i] == '\0')) { + uart_puts(ERR); + continue; + } + + // Error if the platform has no transmit timeout support + if (!platform_tot(seconds)) { + uart_puts(ERR); + continue; + } + + // Send command valid response + uart_puts(OK); + + continue; + } + // All other requests are errors uart_puts(ERR); } From 5a64303dd528d0c09bed462c853ce11a57446b51 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 20:58:19 +0300 Subject: [PATCH 11/15] Fixed how a2i parses strings - parsing starts at `s[*pos]` and stops at the first non-digit character - the value has to fit in 16 bits --- src/common.h | 2 +- src/sa8x8.c | 72 ++++++++++++++++++++++++++++++++-------------------- 2 files changed, 45 insertions(+), 29 deletions(-) diff --git a/src/common.h b/src/common.h index 4b92347..6c60074 100644 --- a/src/common.h +++ b/src/common.h @@ -116,7 +116,7 @@ extern struct ring rx; char up(char c); int eq(const char *s1, const char *s2, uint16_t len); const char *i2a(uint16_t n); -uint16_t a2i(const char *s, uint8_t *pos); +bool a2i(const char *s, uint8_t *pos, uint16_t *n); /* * Platform specific diff --git a/src/sa8x8.c b/src/sa8x8.c index d78dd6c..ec8119d 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -147,25 +147,35 @@ const char *i2a(uint16_t n) { } /* - * Convert supplied ASCII character sequence to a number + * Convert supplied ASCII decimal digit sequence to a number + * + * Parsing starts at s[*pos] and stops at the first non-digit character, + * whose index is stored back into *pos. Fails if there is no digit at all + * or if the value does not fit in 16 bits. */ -uint16_t a2i(const char *s, uint8_t *pos) { - uint16_t n = 0; +bool a2i(const char *s, uint8_t *pos, uint16_t *n) { + uint16_t val = 0; uint8_t i = *pos; - while ((s[i] >= '0' && s[i] <= '9') || s[i] == ' ') { - if (s[i] == ' ') { - i++; - continue; + if (!(s[i] >= '0' && s[i] <= '9')) { + return false; + } + + while (s[i] >= '0' && s[i] <= '9') { + uint16_t d = (uint16_t)(s[i++] - '0'); + + // Reject anything above 65535 before it can wrap around + if (val > 6553U || (val == 6553U && d > 5U)) { + return false; } - n *= 10; - n += (uint16_t)(s[i++] - '0'); + val = (uint16_t)(val * 10U + d); } *pos = i; + *n = val; - return n; + return true; } /* @@ -254,10 +264,11 @@ int main(void) { uint8_t i = sizeof(CMD_PEEK) + 1; // Parse I2C register - uint16_t reg = a2i(cmd, &i); + uint16_t reg; - // Error if register larger than single byte or missing terminator - if (reg > 255 || !(cmd[i] == '\0')) { + // Error if no valid number, register larger than single byte or + // missing terminator + if (!a2i(cmd, &i, ®) || reg > 255 || !(cmd[i] == '\0')) { uart_puts(ERR); continue; } @@ -281,19 +292,20 @@ int main(void) { uint8_t i = sizeof(CMD_POKE) + 1; // Parse I2C register - uint16_t reg = a2i(cmd, &i); + uint16_t reg; - // Error response if register larger than single byte - if (reg > 255 || !(cmd[i++] == ',')) { + // Error if no valid number, register larger than single byte or + // missing separator + if (!a2i(cmd, &i, ®) || reg > 255 || !(cmd[i++] == ',')) { uart_puts(ERR); continue; } // Parse I2C value - uint16_t val = a2i(cmd, &i); + uint16_t val; - // Error if missing terminator - if (!(cmd[i] == '\0')) { + // Error if no valid number or missing terminator + if (!a2i(cmd, &i, &val) || !(cmd[i] == '\0')) { uart_puts(ERR); continue; } @@ -315,14 +327,16 @@ int main(void) { uint8_t i = sizeof(CMD_AMP) + 1; // Parse state - bool enabled = (bool) a2i(cmd, &i); + uint16_t state; - // Error if missing terminator - if (!(cmd[i] == '\0')) { + // Error if not 0 or 1, or missing terminator + if (!a2i(cmd, &i, &state) || state > 1 || !(cmd[i] == '\0')) { uart_puts(ERR); continue; } + bool enabled = (state == 1); + // Set requested amplifier state platform_amp(enabled); @@ -337,14 +351,16 @@ int main(void) { uint8_t i = sizeof(CMD_AUDIO) + 1; // Parse state - bool enabled = (bool) a2i(cmd, &i); + uint16_t state; - // Error if missing terminator - if (!(cmd[i] == '\0')) { + // Error if not 0 or 1, or missing terminator + if (!a2i(cmd, &i, &state) || state > 1 || !(cmd[i] == '\0')) { uart_puts(ERR); continue; } + bool enabled = (state == 1); + // Set requested amplifier state platform_audio(enabled); @@ -359,10 +375,10 @@ int main(void) { uint8_t i = sizeof(CMD_TOT) + 1; // Parse timeout - uint16_t seconds = a2i(cmd, &i); + uint16_t seconds; - // Error if missing terminator - if (!(cmd[i] == '\0')) { + // Error if no valid number or missing terminator + if (!a2i(cmd, &i, &seconds) || !(cmd[i] == '\0')) { uart_puts(ERR); continue; } From 6dd6ab0c4cd9cbb2e3ed13fb47ce2073739beb80 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 21:06:36 +0300 Subject: [PATCH 12/15] Main loop never waits for input --- src/common.h | 5 ++--- src/r5f1026a/platform.c | 22 ---------------------- src/r5r0c002/platform.c | 20 -------------------- src/sa8x8.c | 34 ++++++++++++++++++++++++++++------ 4 files changed, 30 insertions(+), 51 deletions(-) diff --git a/src/common.h b/src/common.h index 6c60074..b04cabe 100644 --- a/src/common.h +++ b/src/common.h @@ -80,8 +80,8 @@ struct item { * * The put and get pointers are shared between the UART receive interrupt * (producer) and the main loop (consumer). They are volatile so that the - * busy-wait in uart_gets() rereads them on every iteration, even if the - * compiler inlines the ring functions (for example with LTO). + * main loop rereads them on every iteration, even if the compiler inlines + * the ring functions (for example with LTO). */ struct ring { uint16_t size; @@ -137,7 +137,6 @@ bool platform_tot(uint16_t seconds); */ void uart_init(void); int uart_puts(const char *s); -int uart_gets(char *s, int size); /* * I2C peripheral control diff --git a/src/r5f1026a/platform.c b/src/r5f1026a/platform.c index 07c84ce..30520e7 100644 --- a/src/r5f1026a/platform.c +++ b/src/r5f1026a/platform.c @@ -166,28 +166,6 @@ int uart_puts(const char *s) { return len; } -/* - * Receive a string through UART0 via ring buffer - */ -int uart_gets(char *s, int size) { - int len = 0; - char c; - - while (len < size - 1) { - // Keep enforcing the transmit timeout while waiting for a full command - while (!ring_get(&rx, &c)) { - _tot_check(); - } - if (c == '\r' || c == '\n') { - break; - } - s[len++] = c; - } - s[len] = '\0'; - - return len; -} - static inline void _i2c_sdio_input(void) { PM4_bit.no1 = 1; // P41 (SDIO) is input } diff --git a/src/r5r0c002/platform.c b/src/r5r0c002/platform.c index 05c477f..d286f7e 100644 --- a/src/r5r0c002/platform.c +++ b/src/r5r0c002/platform.c @@ -68,26 +68,6 @@ int uart_puts(const char *s) { return len; } -/* - * Receive a string through UART0 via ring buffer - */ -int uart_gets(char *s, int size) { - int len = 0; - char c; - - while (len < size - 1) { - while (!ring_get(&rx, &c)) - ; - if (c == '\r' || c == '\n') { - break; - } - s[len++] = c; - } - s[len] = '\0'; - - return len; -} - static inline void _i2c_sdio_input(void) { pd1_7 = 0; // P1_7 (SDIO) is input } diff --git a/src/sa8x8.c b/src/sa8x8.c index ec8119d..7f40159 100644 --- a/src/sa8x8.c +++ b/src/sa8x8.c @@ -190,24 +190,46 @@ int main(void) { // Perform platform specific initialization platform_init(); - // Create command buffer and offset pointer + // Command buffer, filled one character at a time as they arrive char cmd[32]; + uint8_t len = 0; + bool overflow = false; + char ch; char *c; // Internal state struct platform_state state = {0}; while (1) { - // Update internal state + // Update internal state, on every iteration so that PTT and the + // transmit timeout are handled even while a command is half received platform_refresh(&state); - // Skip iteration if no incoming command - if (ring_empty(&rx)) { + // Skip iteration if no incoming character + if (!ring_get(&rx, &ch)) { continue; } - // Read command from UART - uart_gets(cmd, 32); + // Accumulate characters until end of line + if (ch != '\r' && ch != '\n') { + if (len < sizeof(cmd) - 1) { + cmd[len++] = ch; + } else { + overflow = true; // Discard the rest of an overlong line + } + continue; + } + + // End of line: terminate command and reset buffer for the next one + cmd[len] = '\0'; + len = 0; + + // Reject a line that did not fit in the command buffer + if (overflow) { + overflow = false; + uart_puts(ERR); + continue; + } // Make command case-insensitive c = cmd; From ff8e824cd58106e04a5a52623798e49474fe9487 Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Thu, 8 Oct 2026 21:10:39 +0300 Subject: [PATCH 13/15] Python API updates, fixes, and cleanups --- api/python/sa8x8.py | 62 ++++++++++++++++++++++++++++++++++----------- 1 file changed, 47 insertions(+), 15 deletions(-) diff --git a/api/python/sa8x8.py b/api/python/sa8x8.py index 3b53041..2846765 100644 --- a/api/python/sa8x8.py +++ b/api/python/sa8x8.py @@ -27,12 +27,16 @@ class AT1846S_FUNC(IntEnum): FUNC_TX = 2 +class SA8x8Error(Exception): + """Module replied ERR, gave an unexpected reply or did not reply.""" + + class AT1846S: - def i2c_read(): - raise NotImplemented("i2c_read not implemented") + def i2c_read(self, reg): + raise NotImplementedError("i2c_read not implemented") - def i2c_write(): - raise NotImplemented("i2c_write not implemented") + def i2c_write(self, reg, val): + raise NotImplementedError("i2c_write not implemented") def initialize(self): self.i2c_write(0x30, 0x0001) # Soft reset @@ -213,7 +217,7 @@ def mode(self, mode): self.reload() def frequency(self, freq): - val = int((freq / 1000) * 16) + val = (int(freq) * 16) // 1000 # Units of 1/16 kHz, exact integer math fHi = (val >> 16) & 0xFFFF fLo = val & 0xFFFF @@ -235,27 +239,55 @@ def __init__(self, ser): self.xcvr.i2c_read = lambda reg: self.peek(reg) self.xcvr.i2c_write = lambda reg, val: self.poke(reg, val) + def command(self, cmd): + """Send one AT command and return its reply line without CR/LF.""" + self.ser.write(f'{cmd}\r\n'.encode('ascii')) + line = self.ser.readline() + + # readline() returns a partial or empty line when the timeout expires + if not line.endswith(b'\n'): + raise SA8x8Error(f'{cmd}: no reply') + + res = line.strip(b'\r\n').decode('ascii') + if res == 'ERR': + raise SA8x8Error(f'{cmd}: ERR') + + return res + + def expect_ok(self, cmd): + res = self.command(cmd) + if res != 'OK': + raise SA8x8Error(f'{cmd}: unexpected reply {res!r}') + @property def version(self): - self.ser.write(b'AT+VERSION\r\n') - return self.ser.readline().strip(b'\r\n').decode('utf-8') + return self.command('AT+VERSION') def peek(self, reg): - msg = f'AT+PEEK={reg}\r\n' - self.ser.write(msg.encode('utf-8')) - val = int(self.ser.readline().strip(b'\r\n').decode('utf-8')) - return val + res = self.command(f'AT+PEEK={reg}') + try: + return int(res) + except ValueError: + raise SA8x8Error(f'AT+PEEK={reg}: unexpected reply {res!r}') from None def poke(self, reg, val): - msg = f'AT+POKE={reg},{val}\r\n' - self.ser.write(msg.encode('utf-8')) - res = self.ser.readline().strip(b'\r\n').decode('utf-8') - return True if res == b'OK' else False + self.expect_ok(f'AT+POKE={reg},{val}') + return True + + def amp(self, enabled): + self.expect_ok(f'AT+AMP={int(bool(enabled))}') + + def audio(self, enabled): + self.expect_ok(f'AT+AUDIO={int(bool(enabled))}') + + def tot(self, seconds): + self.expect_ok(f'AT+TOT={int(seconds)}') def main(): with serial.Serial() as ser: ser.baudrate = 9600 + ser.timeout = 1 # Seconds; a lost reply raises instead of hanging ser.port = '/dev/ttyUSB0' ser.open() From 42935139bcb61d8d8d14f6dc5f330acd6594824f Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Sun, 25 Oct 2026 13:46:31 +0200 Subject: [PATCH 14/15] Rename a file --- src/r5f1026a/{start.s => start.S} | 0 1 file changed, 0 insertions(+), 0 deletions(-) rename src/r5f1026a/{start.s => start.S} (100%) diff --git a/src/r5f1026a/start.s b/src/r5f1026a/start.S similarity index 100% rename from src/r5f1026a/start.s rename to src/r5f1026a/start.S From 1ccbdffcd677fb3060ef7ef156758e57f100a56b Mon Sep 17 00:00:00 2001 From: Wojciech Kaczmarski Date: Sun, 25 Oct 2026 13:47:44 +0200 Subject: [PATCH 15/15] Fix CI tag fetching --- .github/workflows/main.yaml | 2 ++ .github/workflows/release.yaml | 2 ++ 2 files changed, 4 insertions(+) diff --git a/.github/workflows/main.yaml b/.github/workflows/main.yaml index 901cabd..8c7cabf 100644 --- a/.github/workflows/main.yaml +++ b/.github/workflows/main.yaml @@ -22,6 +22,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v7 + with: + fetch-depth: 0 # tags are needed for the firmware version string - name: Setup run: | diff --git a/.github/workflows/release.yaml b/.github/workflows/release.yaml index b1703b7..5f5f39a 100644 --- a/.github/workflows/release.yaml +++ b/.github/workflows/release.yaml @@ -20,6 +20,8 @@ jobs: steps: - name: Checkout uses: actions/checkout@v3 + with: + fetch-depth: 0 # tags are needed for the firmware version string - name: Setup uses: eWaterCycle/setup-apptainer@v2