From 2e5ed8704449c37adf8414807b38576f704933e1 Mon Sep 17 00:00:00 2001 From: ogatatsu Date: Sat, 3 Oct 2020 17:00:53 +0900 Subject: [PATCH 1/2] fix to not use mutex in ISR --- cores/nRF5/Uart.cpp | 11 ++++++++++- cores/nRF5/Uart.h | 1 + 2 files changed, 11 insertions(+), 1 deletion(-) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index f87f7a99..c88746bd 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -44,6 +44,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 0; _mutex = NULL; + _end_tx_sem = NULL; _begun = false; } @@ -58,6 +59,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_hwFlow = 1; _mutex = NULL; + _end_tx_sem = NULL; _begun = false; } @@ -142,6 +144,7 @@ void Uart::begin(unsigned long baudrate, uint16_t config) NVIC_EnableIRQ(IRQn); _mutex = xSemaphoreCreateMutex(); + _end_tx_sem = xSemaphoreCreateBinary(); _begun = true; } @@ -165,7 +168,9 @@ void Uart::end() rxBuffer.clear(); vSemaphoreDelete(_mutex); + vSemaphoreDelete(_end_tx_sem); _mutex = NULL; + _end_tx_sem = NULL; _begun = false; } @@ -192,7 +197,7 @@ void Uart::IrqHandler() if (nrfUart->EVENTS_ENDTX) { nrfUart->EVENTS_ENDTX = 0x0UL; - xSemaphoreGiveFromISR(_mutex, NULL); + xSemaphoreGiveFromISR(_end_tx_sem, NULL); } } @@ -235,6 +240,10 @@ size_t Uart::write(const uint8_t *buffer, size_t size) nrfUart->TASKS_STARTTX = 0x1UL; sent += txSize; + xSemaphoreTake(_end_tx_sem, portMAX_DELAY); + + xSemaphoreGive(_mutex); + } while (sent < size); return sent; diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index a0003411..bcd92db3 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -71,6 +71,7 @@ class Uart : public HardwareSerial // Adafruit SemaphoreHandle_t _mutex; + SemaphoreHandle_t _end_tx_sem; }; From 60a4c7784c7fc7a4156e4b16c6ce0b2233599b45 Mon Sep 17 00:00:00 2001 From: ogatatsu Date: Tue, 6 Oct 2020 21:20:12 +0900 Subject: [PATCH 2/2] optimize by deleting mutex --- cores/nRF5/Uart.cpp | 16 ++++------------ cores/nRF5/Uart.h | 1 - 2 files changed, 4 insertions(+), 13 deletions(-) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index c88746bd..5dd05dfb 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -43,7 +43,6 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_pinTX = g_ADigitalPinMap[_pinTX]; uc_hwFlow = 0; - _mutex = NULL; _end_tx_sem = NULL; _begun = false; } @@ -58,7 +57,6 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p uc_pinRTS = g_ADigitalPinMap[_pinRTS]; uc_hwFlow = 1; - _mutex = NULL; _end_tx_sem = NULL; _begun = false; } @@ -143,8 +141,8 @@ void Uart::begin(unsigned long baudrate, uint16_t config) NVIC_SetPriority(IRQn, 3); NVIC_EnableIRQ(IRQn); - _mutex = xSemaphoreCreateMutex(); _end_tx_sem = xSemaphoreCreateBinary(); + xSemaphoreGive(_end_tx_sem); _begun = true; } @@ -167,9 +165,7 @@ void Uart::end() rxBuffer.clear(); - vSemaphoreDelete(_mutex); vSemaphoreDelete(_end_tx_sem); - _mutex = NULL; _end_tx_sem = NULL; _begun = false; } @@ -177,8 +173,8 @@ void Uart::end() void Uart::flush() { if ( _begun ) { - xSemaphoreTake(_mutex, portMAX_DELAY); - xSemaphoreGive(_mutex); + xSemaphoreTake(_end_tx_sem, portMAX_DELAY); + xSemaphoreGive(_end_tx_sem); } } @@ -232,7 +228,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size) size_t remaining = size - sent; size_t txSize = min(remaining, (size_t)SERIAL_BUFFER_SIZE); - xSemaphoreTake(_mutex, portMAX_DELAY); + xSemaphoreTake(_end_tx_sem, portMAX_DELAY); memcpy(txBuffer, buffer + sent, txSize); @@ -240,10 +236,6 @@ size_t Uart::write(const uint8_t *buffer, size_t size) nrfUart->TASKS_STARTTX = 0x1UL; sent += txSize; - xSemaphoreTake(_end_tx_sem, portMAX_DELAY); - - xSemaphoreGive(_mutex); - } while (sent < size); return sent; diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index bcd92db3..9aa78fce 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -70,7 +70,6 @@ class Uart : public HardwareSerial bool _begun; // Adafruit - SemaphoreHandle_t _mutex; SemaphoreHandle_t _end_tx_sem; };