From b1336100ccd2c5c1ba03cc3700dbebb8c8e005d9 Mon Sep 17 00:00:00 2001 From: hathach Date: Tue, 4 Apr 2017 12:39:02 +0700 Subject: [PATCH] add mutex for uart to prevent write conflict --- cores/nRF5/Uart.cpp | 10 ++++++++++ cores/nRF5/Uart.h | 4 ++++ 2 files changed, 14 insertions(+) diff --git a/cores/nRF5/Uart.cpp b/cores/nRF5/Uart.cpp index 3f1c0121..5a1225f8 100644 --- a/cores/nRF5/Uart.cpp +++ b/cores/nRF5/Uart.cpp @@ -29,6 +29,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi uc_pinTX = g_ADigitalPinMap[_pinTX]; uc_hwFlow = 0; + _mutex = NULL; + _begun = false; } @@ -42,6 +44,8 @@ Uart::Uart(NRF_UART_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _pi uc_pinRTS = g_ADigitalPinMap[_pinRTS]; uc_hwFlow = 1; + _mutex = NULL; + _begun = false; } @@ -152,6 +156,8 @@ void Uart::begin(unsigned long baudrate, uint16_t /*config*/) NVIC_SetPriority(IRQn, 3); NVIC_EnableIRQ(IRQn); + _mutex = xSemaphoreCreateMutex(); + _begun = true; } @@ -206,12 +212,16 @@ int Uart::read() size_t Uart::write(const uint8_t data) { + xSemaphoreTake(_mutex, portMAX_DELAY); + nrfUart->TXD = data; while(!nrfUart->EVENTS_TXDRDY); nrfUart->EVENTS_TXDRDY = 0x0UL; + xSemaphoreGive(_mutex); + return 1; } diff --git a/cores/nRF5/Uart.h b/cores/nRF5/Uart.h index d3c56976..7330925c 100644 --- a/cores/nRF5/Uart.h +++ b/cores/nRF5/Uart.h @@ -23,6 +23,7 @@ #include "HardwareSerial.h" #include "RingBuffer.h" +#include "rtos.h" #include @@ -58,6 +59,9 @@ class Uart : public HardwareSerial uint8_t uc_hwFlow; bool _begun; + + // Adafruit + SemaphoreHandle_t _mutex; }; #ifdef __cplusplus