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; };