diff --git a/Grbl_Esp32/src/Grbl.cpp b/Grbl_Esp32/src/Grbl.cpp index 5b901c9a..fec365f9 100644 --- a/Grbl_Esp32/src/Grbl.cpp +++ b/Grbl_Esp32/src/Grbl.cpp @@ -30,6 +30,7 @@ #include "Limits.h" #include "Protocol.h" #include "System.h" +#include "Uart.h" #include "WebUI/WifiConfig.h" #include "WebUI/InputBuffer.h" @@ -42,7 +43,7 @@ extern void make_grbl_commands(); void grbl_init() { try { - client_init(); // Setup serial baud rate and interrupts + uartInit(); // Setup serial port debug_serial("Initializing WiFi..."); WiFi.persistent(false); @@ -65,6 +66,10 @@ void grbl_init() { config->load(config_filename->get()); make_grbl_commands(); + // Setup input polling loop after loading the configuration, + // because the polling may depend on the config + client_init(); + report_machine_type(CLIENT_SERIAL); info_serial("Board: %s", config->_board.c_str()); diff --git a/Grbl_Esp32/src/Serial.cpp b/Grbl_Esp32/src/Serial.cpp index 15aaa01a..0f54aa9a 100644 --- a/Grbl_Esp32/src/Serial.cpp +++ b/Grbl_Esp32/src/Serial.cpp @@ -106,11 +106,7 @@ void client_init() { xTaskCreatePinnedToCore(heapCheckTask, "heapTask", 2000, NULL, 1, NULL, 1); #endif - Uart0.setPins(GPIO_NUM_1, GPIO_NUM_3); // Tx 1, Rx 3 - standard hardware pins - Uart0.begin(BAUD_RATE, Uart::Data::Bits8, Uart::Stop::Bits1, Uart::Parity::None); - client_reset_read_buffer(CLIENT_ALL); - Uart0.write("\r\n"); // create some white space after ESP32 boot info clientCheckTaskHandle = 0; // create a task to check for incoming data @@ -126,7 +122,7 @@ void client_init() { ); } -static uint8_t getClientChar(uint8_t& data) { +static ClientType getClientChar(int& data) { if (client_buffer[CLIENT_SERIAL].availableforwrite() && (data = Uart0.read()) != -1) { return CLIENT_SERIAL; } @@ -150,22 +146,23 @@ static uint8_t getClientChar(uint8_t& data) { // this task runs and checks for data on all interfaces // Realtime stuff is acted upon, then characters are added to the appropriate buffer void clientCheckTask(void* pvParameters) { - uint8_t data = 0; - uint8_t client; // who sent the data + int data = 0; // Must be int so -1 value is possible + ClientType client; // who sent the data while (true) { // run continuously std::atomic_thread_fence(std::memory_order::memory_order_seq_cst); // read fence for settings while ((client = getClientChar(data)) != CLIENT_ALL) { + uint8_t clientByte = uint8_t(data); // Pick off realtime command characters directly from the serial stream. These characters are // not passed into the main buffer, but these set system state flag bits for realtime execution. - if (is_realtime_command(data)) { - execute_realtime_command(static_cast(data), client); + if (is_realtime_command(clientByte)) { + execute_realtime_command(static_cast(clientByte), client); } else { if (config->_sdCard->get_state(false) < SDCard::State::Busy) { vTaskEnterCritical(&myMutex); - client_buffer[client].write(data); + client_buffer[client].write(clientByte); vTaskExitCritical(&myMutex); } else { - if (data == '\r' || data == '\n') { + if (clientByte == '\r' || clientByte == '\n') { grbl_sendf(client, "error %d\r\n", Error::AnotherInterfaceBusy); info_client(client, "SD card job running"); } diff --git a/Grbl_Esp32/src/Uart.cpp b/Grbl_Esp32/src/Uart.cpp index 58b7ae07..69cbd4fb 100644 --- a/Grbl_Esp32/src/Uart.cpp +++ b/Grbl_Esp32/src/Uart.cpp @@ -19,6 +19,7 @@ * UART driver that accesses the ESP32 hardware FIFOs directly. */ +#include "Logging.h" #include "Uart.h" #include @@ -66,7 +67,7 @@ int Uart::read(TickType_t timeout) { } uint8_t c; int res = uart_read_bytes(_uart_num, &c, 1, timeout); - return res != 1 ? -1 : c; + return res == 1 ? c : -1; } int Uart::read() { return read(0); @@ -110,3 +111,9 @@ bool Uart::flushTxTimed(TickType_t ticks) { } Uart Uart0(0); + +void uartInit() { + Uart0.setPins(GPIO_NUM_1, GPIO_NUM_3); // Tx 1, Rx 3 - standard hardware pins + Uart0.begin(BAUD_RATE, Uart::Data::Bits8, Uart::Stop::Bits1, Uart::Parity::None); + Uart0.write("\r\n"); // create some white space after ESP32 boot info +} diff --git a/Grbl_Esp32/src/Uart.h b/Grbl_Esp32/src/Uart.h index 2de5d09c..a98f69e0 100644 --- a/Grbl_Esp32/src/Uart.h +++ b/Grbl_Esp32/src/Uart.h @@ -71,3 +71,5 @@ public: }; extern Uart Uart0; + +extern void uartInit();