Skip to content

Commit 9f0f424

Browse files
authored
Merge pull request #587 from ogatatsu/uart
fix to not use mutex in ISR
2 parents 9d6f75d + 60a4c77 commit 9f0f424

File tree

2 files changed

+11
-10
lines changed

2 files changed

+11
-10
lines changed

cores/nRF5/Uart.cpp

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p
4343
uc_pinTX = g_ADigitalPinMap[_pinTX];
4444
uc_hwFlow = 0;
4545

46-
_mutex = NULL;
46+
_end_tx_sem = NULL;
4747
_begun = false;
4848
}
4949

@@ -57,7 +57,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p
5757
uc_pinRTS = g_ADigitalPinMap[_pinRTS];
5858
uc_hwFlow = 1;
5959

60-
_mutex = NULL;
60+
_end_tx_sem = NULL;
6161
_begun = false;
6262
}
6363

@@ -141,7 +141,8 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
141141
NVIC_SetPriority(IRQn, 3);
142142
NVIC_EnableIRQ(IRQn);
143143

144-
_mutex = xSemaphoreCreateMutex();
144+
_end_tx_sem = xSemaphoreCreateBinary();
145+
xSemaphoreGive(_end_tx_sem);
145146
_begun = true;
146147
}
147148

@@ -164,16 +165,16 @@ void Uart::end()
164165

165166
rxBuffer.clear();
166167

167-
vSemaphoreDelete(_mutex);
168-
_mutex = NULL;
168+
vSemaphoreDelete(_end_tx_sem);
169+
_end_tx_sem = NULL;
169170
_begun = false;
170171
}
171172

172173
void Uart::flush()
173174
{
174175
if ( _begun ) {
175-
xSemaphoreTake(_mutex, portMAX_DELAY);
176-
xSemaphoreGive(_mutex);
176+
xSemaphoreTake(_end_tx_sem, portMAX_DELAY);
177+
xSemaphoreGive(_end_tx_sem);
177178
}
178179
}
179180

@@ -192,7 +193,7 @@ void Uart::IrqHandler()
192193
if (nrfUart->EVENTS_ENDTX)
193194
{
194195
nrfUart->EVENTS_ENDTX = 0x0UL;
195-
xSemaphoreGiveFromISR(_mutex, NULL);
196+
xSemaphoreGiveFromISR(_end_tx_sem, NULL);
196197
}
197198
}
198199

@@ -227,7 +228,7 @@ size_t Uart::write(const uint8_t *buffer, size_t size)
227228
size_t remaining = size - sent;
228229
size_t txSize = min(remaining, (size_t)SERIAL_BUFFER_SIZE);
229230

230-
xSemaphoreTake(_mutex, portMAX_DELAY);
231+
xSemaphoreTake(_end_tx_sem, portMAX_DELAY);
231232

232233
memcpy(txBuffer, buffer + sent, txSize);
233234

cores/nRF5/Uart.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ class Uart : public HardwareSerial
7070
bool _begun;
7171

7272
// Adafruit
73-
SemaphoreHandle_t _mutex;
73+
SemaphoreHandle_t _end_tx_sem;
7474
};
7575

7676

0 commit comments

Comments
 (0)