add mutex for uart to prevent write conflict
This commit is contained in:
1 parent
aa32c41ff8
commit
b1336100cc
2 files changed
+14
No files matched your search
@@ -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;
|
||||
}
|
||||
|
||||
|
||||
@@ -23,6 +23,7 @@
|
||||
|
||||
#include "HardwareSerial.h"
|
||||
#include "RingBuffer.h"
|
||||
#include "rtos.h"
|
||||
|
||||
#include <cstddef>
|
||||
|
||||
@@ -58,6 +59,9 @@ class Uart : public HardwareSerial
|
||||
uint8_t uc_hwFlow;
|
||||
|
||||
bool _begun;
|
||||
|
||||
// Adafruit
|
||||
SemaphoreHandle_t _mutex;
|
||||
};
|
||||
|
||||
#ifdef __cplusplus
|
||||
|
||||
Reference in new issue
Block a user