From ab7672b73aae6ad853b03f754a9e84f267575d60 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 5 Aug 2020 09:00:38 -0700 Subject: [PATCH 1/2] Add SerialEvent() callback to loop processing Match the AVR SerialEvent implicit callback. Callback is executed in normal user mode, not IRQ, so standard processing can be uses. Fixes #752 after 5 years. :) --- cores/esp8266/HardwareSerial.cpp | 14 ++++++++++++++ cores/esp8266/HardwareSerial.h | 2 ++ cores/esp8266/core_esp8266_main.cpp | 1 + 3 files changed, 17 insertions(+) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 5f7ea141c0..4b12135b89 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -32,6 +32,14 @@ #include "HardwareSerial.h" #include "Esp.h" + +// SerialEvent functions are weak, so when the user doesn't define them, +// the linker just sets their address to 0 (which is checked below). +// The Serialx_available is just a wrapper around Serialx.available(), +// but we can refer to it weakly so we don't pull in the entire +// HardwareSerial instance if the user doesn't also refer to it. +void serialEvent() __attribute__((weak)); + HardwareSerial::HardwareSerial(int uart_nr) : _uart_nr(uart_nr), _rx_size(256) {} @@ -162,6 +170,12 @@ size_t HardwareSerial::readBytes(char* buffer, size_t size) #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL) HardwareSerial Serial(UART0); + +// Executed at end of loop() processing when > 0 bytes available in the Serial port +void serialEventRun(void) +{ + if (serialEvent && Serial.available()) serialEvent(); +} #endif #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) HardwareSerial Serial1(UART1); diff --git a/cores/esp8266/HardwareSerial.h b/cores/esp8266/HardwareSerial.h index dd12e96d25..6b89f68007 100644 --- a/cores/esp8266/HardwareSerial.h +++ b/cores/esp8266/HardwareSerial.h @@ -207,4 +207,6 @@ class HardwareSerial: public Stream extern HardwareSerial Serial; extern HardwareSerial Serial1; +extern void serialEventRun(void) __attribute__((weak)); + #endif diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 337686199d..625eda3e63 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -195,6 +195,7 @@ static void loop_wrapper() { } loop(); loop_end(); + if (serialEventRun) serialEventRun(); esp_schedule(); } From a5e461411e01aeffb1a422a97fd31b8bf5bfeaf9 Mon Sep 17 00:00:00 2001 From: "Earle F. Philhower, III" Date: Wed, 5 Aug 2020 12:47:21 -0700 Subject: [PATCH 2/2] Fix style --- cores/esp8266/HardwareSerial.cpp | 4 +++- cores/esp8266/core_esp8266_main.cpp | 4 +++- 2 files changed, 6 insertions(+), 2 deletions(-) diff --git a/cores/esp8266/HardwareSerial.cpp b/cores/esp8266/HardwareSerial.cpp index 4b12135b89..26383dd43e 100644 --- a/cores/esp8266/HardwareSerial.cpp +++ b/cores/esp8266/HardwareSerial.cpp @@ -174,7 +174,9 @@ HardwareSerial Serial(UART0); // Executed at end of loop() processing when > 0 bytes available in the Serial port void serialEventRun(void) { - if (serialEvent && Serial.available()) serialEvent(); + if (serialEvent && Serial.available()) { + serialEvent(); + } } #endif #if !defined(NO_GLOBAL_INSTANCES) && !defined(NO_GLOBAL_SERIAL1) diff --git a/cores/esp8266/core_esp8266_main.cpp b/cores/esp8266/core_esp8266_main.cpp index 625eda3e63..21724e2eb4 100644 --- a/cores/esp8266/core_esp8266_main.cpp +++ b/cores/esp8266/core_esp8266_main.cpp @@ -195,7 +195,9 @@ static void loop_wrapper() { } loop(); loop_end(); - if (serialEventRun) serialEventRun(); + if (serialEventRun) { + serialEventRun(); + } esp_schedule(); }