1
0
mirror of https://github.com/bdring/Grbl_Esp32.git synced 2025-08-28 16:49:54 +02:00

Added Mutex Area to Protect Serial Buffer

Added Mutex areas to protect wrting and read the serial buffer at the same time.

Thanks @misan
This commit is contained in:
bdring
2018-08-01 10:01:57 -05:00
parent 25cd4a03ac
commit c7f317aca6
2 changed files with 8 additions and 2 deletions

View File

@@ -27,6 +27,7 @@
#include <EEPROM.h>
#include <driver/rmt.h>
#include <esp_task_wdt.h>
#include <freertos/task.h>
// Define the Grbl system include files. NOTE: Do not alter organization.
#include "config.h"
@@ -57,3 +58,4 @@

View File

@@ -23,6 +23,8 @@
#define RX_RING_BUFFER (RX_BUFFER_SIZE+1)
#define TX_RING_BUFFER (TX_BUFFER_SIZE+1)
portMUX_TYPE myMutex = portMUX_INITIALIZER_UNLOCKED;
uint8_t serial_rx_buffer[RX_RING_BUFFER];
uint8_t serial_rx_buffer_head = 0;
volatile uint8_t serial_rx_buffer_tail = 0;
@@ -106,6 +108,7 @@ void serialCheckTask(void *pvParameters)
}
// Throw away any unfound extended-ASCII character by not passing it to the serial buffer.
} else { // Write character to buffer
vTaskEnterCritical(&myMutex);
next_head = serial_rx_buffer_head + 1;
if (next_head == RX_RING_BUFFER) { next_head = 0; }
@@ -114,6 +117,7 @@ void serialCheckTask(void *pvParameters)
serial_rx_buffer[serial_rx_buffer_head] = data;
serial_rx_buffer_head = next_head;
}
vTaskExitCritical(&myMutex);
}
} // switch data
@@ -138,14 +142,14 @@ uint8_t serial_read()
if (serial_rx_buffer_head == tail) {
return SERIAL_NO_DATA;
} else {
vTaskEnterCritical(&myMutex); // make sure buffer is not modified while reading by newly read chars from the serial when we are here
uint8_t data = serial_rx_buffer[tail];
tail++;
if (tail == RX_RING_BUFFER) { tail = 0; }
serial_rx_buffer_tail = tail;
vTaskExitCritical(&myMutex);
return data;
}
}