Skip to content

Commit d2007e1

Browse files
authored
fix: add Mutex verification
1 parent 0c60c73 commit d2007e1

File tree

1 file changed

+6
-3
lines changed

1 file changed

+6
-3
lines changed

Diff for: libraries/ESP32/examples/Serial/onReceiveExample/onReceiveExample.ino

+6-3
Original file line numberDiff line numberDiff line change
@@ -70,7 +70,7 @@ SemaphoreHandle_t uart_buffer_Mutex = NULL;
7070
// task created when onReceive() is used
7171
void UART0_RX_CB() {
7272
// take the mutex, waits forever until loop() finishes its processing
73-
if (xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
73+
if (uart_buffer_Mutex != NULL && xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
7474
uint32_t now = millis(); // tracks timeout
7575
while ((millis() - now) < communicationTimeout_ms) {
7676
if (UART0.available()) {
@@ -88,7 +88,10 @@ void UART0_RX_CB() {
8888
void setup() {
8989
// creates a mutex object to control access to uart_buffer
9090
uart_buffer_Mutex = xSemaphoreCreateMutex();
91-
91+
if(uart_buffer_Mutex == NULL) {
92+
log_e("Error creating Mutex. Sketch will fail.");
93+
delay(1000);
94+
}
9295
UART0.begin(115200);
9396
UART0.onReceive(UART0_RX_CB); // sets the callback function
9497
UART0.println("Send data to UART0 in order to activate the RX callback");
@@ -98,7 +101,7 @@ uint32_t counter = 0;
98101
void loop() {
99102
if (uart_buffer.length() > 0) {
100103
// signals that the onReceive function shall not change uart_buffer while processing
101-
if (xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
104+
if (uart_buffer_Mutex != NULL && xSemaphoreTake(uart_buffer_Mutex, portMAX_DELAY)) {
102105
// process the received data from UART0 - example, just print it beside a counter
103106
UART0.print("[");
104107
UART0.print(counter++);

0 commit comments

Comments
 (0)