Skip to content

Commit fcc33ea

Browse files
[Silabs] Fix race condition in the uart driver use by the matter shell
1 parent ad19dee commit fcc33ea

File tree

1 file changed

+10
-0
lines changed

1 file changed

+10
-0
lines changed

examples/platform/silabs/uart.cpp

+10
Original file line numberDiff line numberDiff line change
@@ -219,13 +219,16 @@ static bool InitFifo(Fifo_t * fifo, uint8_t * pDataBuffer, uint16_t bufferSize)
219219
static uint16_t AvailableDataCount(Fifo_t * fifo)
220220
{
221221
uint16_t size = 0;
222+
CORE_DECLARE_IRQ_STATE;
222223

224+
CORE_ENTER_ATOMIC();
223225
// if equal there is no data return 0 directly
224226
if (fifo->Tail != fifo->Head)
225227
{
226228
// determine if a wrap around occurred to get the right data size avalaible.
227229
size = (fifo->Tail < fifo->Head) ? (fifo->MaxSize - fifo->Head + fifo->Tail) : (fifo->Tail - fifo->Head);
228230
}
231+
CORE_EXIT_ATOMIC();
229232

230233
return size;
231234
}
@@ -249,10 +252,12 @@ static void WriteToFifo(Fifo_t * fifo, uint8_t * pDataToWrite, uint16_t SizeToWr
249252
VerifyOrDie(fifo != nullptr);
250253
VerifyOrDie(pDataToWrite != nullptr);
251254
VerifyOrDie(SizeToWrite <= fifo->MaxSize);
255+
CORE_DECLARE_IRQ_STATE;
252256

253257
// Overwrite is not allowed
254258
if (RemainingSpace(fifo) >= SizeToWrite)
255259
{
260+
CORE_ENTER_ATOMIC();
256261
uint16_t nBytesBeforWrap = (fifo->MaxSize - fifo->Tail);
257262
if (SizeToWrite > nBytesBeforWrap)
258263
{
@@ -267,6 +272,7 @@ static void WriteToFifo(Fifo_t * fifo, uint8_t * pDataToWrite, uint16_t SizeToWr
267272
}
268273

269274
fifo->Tail = (fifo->Tail + SizeToWrite) % fifo->MaxSize; // increment tail with wraparound
275+
CORE_EXIT_ATOMIC();
270276
}
271277
}
272278

@@ -280,8 +286,11 @@ static uint16_t RetrieveFromFifo(Fifo_t * fifo, uint8_t * pData, uint16_t SizeTo
280286
VerifyOrDie(fifo != nullptr);
281287
VerifyOrDie(pData != nullptr);
282288
VerifyOrDie(SizeToRead <= fifo->MaxSize);
289+
CORE_DECLARE_IRQ_STATE;
283290

284291
uint16_t ReadSize = std::min(SizeToRead, AvailableDataCount(fifo));
292+
293+
CORE_ENTER_ATOMIC();
285294
uint16_t nBytesBeforWrap = (fifo->MaxSize - fifo->Head);
286295

287296
if (ReadSize > nBytesBeforWrap)
@@ -295,6 +304,7 @@ static uint16_t RetrieveFromFifo(Fifo_t * fifo, uint8_t * pData, uint16_t SizeTo
295304
}
296305

297306
fifo->Head = (fifo->Head + ReadSize) % fifo->MaxSize; // increment tail with wraparound
307+
CORE_EXIT_ATOMIC();
298308

299309
return ReadSize;
300310
}

0 commit comments

Comments
 (0)