From 8e2ee36d4411231f46920bda37234d7c3d670289 Mon Sep 17 00:00:00 2001 From: CValdesS Date: Thu, 5 Nov 2026 01:36:58 +0100 Subject: [PATCH 1/3] Uart: don't use the TX semaphore when the port isn't open end() deletes _end_tx_sem and leaves it NULL, and write() takes it without checking, so anything printed after end() hard-faults inside xQueueSemaphoreTake(). flush() already checks _begun. write() now returns 0 when the port isn't open. So does availableForWrite(), which reads the same semaphore. --- cores/nRF5/Uart.cpp | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index 2811a37..8007f9f 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -229,7 +229,8 @@ size_t Uart::write(uint8_t data) size_t Uart::write(const uint8_t *buffer, size_t size) { - if(size == 0) return 0; + // end() deletes the semaphore, and there is none before begin() + if(!_begun || size == 0) return 0; size_t sent = 0; @@ -253,6 +254,8 @@ size_t Uart::write(const uint8_t *buffer, size_t size) } int Uart::availableForWrite(void) { + if(!_begun) return 0; + // UART does not use ring buffer for TX, therefore it is either busy or not UBaseType_t available = uxSemaphoreGetCount(_end_tx_sem); return available ? SERIAL_BUFFER_SIZE : 0; From b9ef514850d2e0d1bd95a169c71916cc3ca65f0d Mon Sep 17 00:00:00 2001 From: CValdesS Date: Sun, 8 Nov 2026 20:21:23 +0100 Subject: [PATCH 2/3] Uart: serialise end() with the calls that use the TX semaphore Checking _begun isn't enough when end() runs in another task: a caller can pass the check and then use _end_tx_sem after end() has deleted it, and a long write() can be caught between two chunks. A mutex, created by the first begin() and never deleted, is now held by begin(), end(), write() for the whole buffer, and flush(). availableForWrite() takes it without waiting and returns 0 if it's held, since TX is busy then. end() waits for a write() or flush() in progress before it stops the UARTE and deletes the semaphore; with flow control held off, that's as long as the write() itself would wait. end() without an open port now returns early instead of running the stop sequence on a disabled UARTE and deleting a NULL semaphore. --- cores/nRF5/Uart.cpp | 54 +++++++++++++++++++++++++++++++++++++++------ cores/nRF5/Uart.h | 4 ++++ 2 files changed, 51 insertions(+), 7 deletions(-) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index 8007f9f..41c25dc 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -40,6 +40,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 0; _end_tx_sem = NULL; + _lock = NULL; _begun = false; } @@ -54,6 +55,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 1; _end_tx_sem = NULL; + _lock = NULL; _begun = false; } @@ -70,8 +72,14 @@ void Uart::begin(unsigned long baudrate) void Uart::begin(unsigned long baudrate, uint16_t config) { + if ( _lock == NULL ) _lock = xSemaphoreCreateMutex(); + xSemaphoreTake(_lock, portMAX_DELAY); + // skip if already begun - if ( _begun ) return; + if ( _begun ) { + xSemaphoreGive(_lock); + return; + } nrfUart->PSEL.TXD = uc_pinTX; nrfUart->PSEL.RXD = uc_pinRX; @@ -144,10 +152,22 @@ void Uart::begin(unsigned long baudrate, uint16_t config) _end_tx_sem = xSemaphoreCreateBinary(); xSemaphoreGive(_end_tx_sem); _begun = true; + + xSemaphoreGive(_lock); } void Uart::end() { + if ( _lock == NULL ) return; + + // Waits for a write() or flush() in progress: none can be holding or waiting on _end_tx_sem below + xSemaphoreTake(_lock, portMAX_DELAY); + + if ( !_begun ) { + xSemaphoreGive(_lock); + return; + } + NVIC_DisableIRQ(IRQn); nrfUart->INTENCLR = UARTE_INTENCLR_DMARXEND_Msk | UARTE_INTENCLR_DMATXEND_Msk; @@ -176,14 +196,20 @@ void Uart::end() vSemaphoreDelete(_end_tx_sem); _end_tx_sem = NULL; _begun = false; + + xSemaphoreGive(_lock); } void Uart::flush() { + if ( _lock == NULL ) return; + + xSemaphoreTake(_lock, portMAX_DELAY); if ( _begun ) { xSemaphoreTake(_end_tx_sem, portMAX_DELAY); xSemaphoreGive(_end_tx_sem); } + xSemaphoreGive(_lock); } void Uart::IrqHandler() @@ -229,8 +255,15 @@ size_t Uart::write(uint8_t data) size_t Uart::write(const uint8_t *buffer, size_t size) { - // end() deletes the semaphore, and there is none before begin() - if(!_begun || size == 0) return 0; + // No lock before the first begin(), and the port can't be open without one + if(_lock == NULL || size == 0) return 0; + + // Held for the whole buffer, so end() can't delete _end_tx_sem between chunks + xSemaphoreTake(_lock, portMAX_DELAY); + if(!_begun) { + xSemaphoreGive(_lock); + return 0; + } size_t sent = 0; @@ -250,15 +283,22 @@ size_t Uart::write(const uint8_t *buffer, size_t size) } while (sent < size); + xSemaphoreGive(_lock); return sent; } int Uart::availableForWrite(void) { - if(!_begun) return 0; + // Don't wait: if another caller holds the lock, TX is busy anyway + if(_lock == NULL || xSemaphoreTake(_lock, 0) != pdTRUE) return 0; + + int available = 0; + if(_begun) { + // UART does not use ring buffer for TX, therefore it is either busy or not + available = uxSemaphoreGetCount(_end_tx_sem) ? SERIAL_BUFFER_SIZE : 0; + } - // UART does not use ring buffer for TX, therefore it is either busy or not - UBaseType_t available = uxSemaphoreGetCount(_end_tx_sem); - return available ? SERIAL_BUFFER_SIZE : 0; + xSemaphoreGive(_lock); + return available; } //------------- Serial1 (UART, nRF54L has no USB CDC) -------------// diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index 1c851f6..100e535 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -72,6 +72,10 @@ class Uart : public HardwareSerial // Adafruit SemaphoreHandle_t _end_tx_sem; + + // Serialises begin()/end() with write()/flush()/availableForWrite(), so end() + // can't delete _end_tx_sem under a caller. Created by the first begin(), never deleted. + SemaphoreHandle_t _lock; }; From aa34298e62309f061d0ee4a4a058874b13b5f004 Mon Sep 17 00:00:00 2001 From: CValdesS Date: Sun, 15 Nov 2026 14:38:19 +0100 Subject: [PATCH 3/3] Uart: create the lock in the constructor, from static storage Two tasks calling begin() for the first time could both see no lock and create a mutex each, and xSemaphoreCreateMutex() can fail and return NULL. The constructor now creates it with xSemaphoreCreateMutexStatic(), before any task can call in, the way Adafruit_LittleFS does, and it can't fail. That removes the NULL checks from end(), flush(), write() and availableForWrite(): before begin() they take the lock, see the port closed and return. --- cores/nRF5/Uart.cpp | 14 ++++---------- cores/nRF5/Uart.h | 4 +++- 2 files changed, 7 insertions(+), 11 deletions(-) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index 41c25dc..fc856c5 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -40,7 +40,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 0; _end_tx_sem = NULL; - _lock = NULL; + _lock = xSemaphoreCreateMutexStatic(&_lockStorage); _begun = false; } @@ -55,7 +55,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 1; _end_tx_sem = NULL; - _lock = NULL; + _lock = xSemaphoreCreateMutexStatic(&_lockStorage); _begun = false; } @@ -72,7 +72,6 @@ void Uart::begin(unsigned long baudrate) void Uart::begin(unsigned long baudrate, uint16_t config) { - if ( _lock == NULL ) _lock = xSemaphoreCreateMutex(); xSemaphoreTake(_lock, portMAX_DELAY); // skip if already begun @@ -158,8 +157,6 @@ void Uart::begin(unsigned long baudrate, uint16_t config) void Uart::end() { - if ( _lock == NULL ) return; - // Waits for a write() or flush() in progress: none can be holding or waiting on _end_tx_sem below xSemaphoreTake(_lock, portMAX_DELAY); @@ -202,8 +199,6 @@ void Uart::end() void Uart::flush() { - if ( _lock == NULL ) return; - xSemaphoreTake(_lock, portMAX_DELAY); if ( _begun ) { xSemaphoreTake(_end_tx_sem, portMAX_DELAY); @@ -255,8 +250,7 @@ size_t Uart::write(uint8_t data) size_t Uart::write(const uint8_t *buffer, size_t size) { - // No lock before the first begin(), and the port can't be open without one - if(_lock == NULL || size == 0) return 0; + if(size == 0) return 0; // Held for the whole buffer, so end() can't delete _end_tx_sem between chunks xSemaphoreTake(_lock, portMAX_DELAY); @@ -289,7 +283,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size) int Uart::availableForWrite(void) { // Don't wait: if another caller holds the lock, TX is busy anyway - if(_lock == NULL || xSemaphoreTake(_lock, 0) != pdTRUE) return 0; + if(xSemaphoreTake(_lock, 0) != pdTRUE) return 0; int available = 0; if(_begun) { diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index 100e535..0daa956 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -74,8 +74,10 @@ class Uart : public HardwareSerial SemaphoreHandle_t _end_tx_sem; // Serialises begin()/end() with write()/flush()/availableForWrite(), so end() - // can't delete _end_tx_sem under a caller. Created by the first begin(), never deleted. + // can't delete _end_tx_sem under a caller. Created by the constructor in + // _lockStorage, before any task can call in, and never deleted. SemaphoreHandle_t _lock; + StaticSemaphore_t _lockStorage; };