Skip to content

Commit 2e5ed87

Browse files
committed
fix to not use mutex in ISR
1 parent 2316b0f commit 2e5ed87

File tree

2 files changed

+11
-1
lines changed

2 files changed

+11
-1
lines changed

cores/nRF5/Uart.cpp

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p
4444
uc_hwFlow = 0;
4545

4646
_mutex = NULL;
47+
_end_tx_sem = NULL;
4748
_begun = false;
4849
}
4950

@@ -58,6 +59,7 @@ Uart::Uart(NRF_UARTE_Type *_nrfUart, IRQn_Type _IRQn, uint8_t _pinRX, uint8_t _p
5859
uc_hwFlow = 1;
5960

6061
_mutex = NULL;
62+
_end_tx_sem = NULL;
6163
_begun = false;
6264
}
6365

@@ -142,6 +144,7 @@ void Uart::begin(unsigned long baudrate, uint16_t config)
142144
NVIC_EnableIRQ(IRQn);
143145

144146
_mutex = xSemaphoreCreateMutex();
147+
_end_tx_sem = xSemaphoreCreateBinary();
145148
_begun = true;
146149
}
147150

@@ -165,7 +168,9 @@ void Uart::end()
165168
rxBuffer.clear();
166169

167170
vSemaphoreDelete(_mutex);
171+
vSemaphoreDelete(_end_tx_sem);
168172
_mutex = NULL;
173+
_end_tx_sem = NULL;
169174
_begun = false;
170175
}
171176

@@ -192,7 +197,7 @@ void Uart::IrqHandler()
192197
if (nrfUart->EVENTS_ENDTX)
193198
{
194199
nrfUart->EVENTS_ENDTX = 0x0UL;
195-
xSemaphoreGiveFromISR(_mutex, NULL);
200+
xSemaphoreGiveFromISR(_end_tx_sem, NULL);
196201
}
197202
}
198203

@@ -235,6 +240,10 @@ size_t Uart::write(const uint8_t *buffer, size_t size)
235240
nrfUart->TASKS_STARTTX = 0x1UL;
236241
sent += txSize;
237242

243+
xSemaphoreTake(_end_tx_sem, portMAX_DELAY);
244+
245+
xSemaphoreGive(_mutex);
246+
238247
} while (sent < size);
239248

240249
return sent;

cores/nRF5/Uart.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,7 @@ class Uart : public HardwareSerial
7171

7272
// Adafruit
7373
SemaphoreHandle_t _mutex;
74+
SemaphoreHandle_t _end_tx_sem;
7475
};
7576

7677

0 commit comments

Comments
 (0)