From 2b69ca95be5054d668b5eb283d2c97cf82b1247d Mon Sep 17 00:00:00 2001 From: kurte Date: Wed, 14 Aug 2024 07:25:15 -0700 Subject: [PATCH] Fix T3.x SerialEventX when format is passed in. Before there were two different begin Methods that were wrappers to the c begin and format functions. The code that checked for SerialEvent being defined in a sketch was only in the begin method where format was not passed in. Changed these to have only one begin method, with an optional format descriptor If format is not specified I pass in: (uint32_t)-1 which the begin method checks for if it is -1 format is not called otherwise it is. Note: with T4.x the default was always 0 and the format code was always done, but did not want to this here on the off chance there are T3.x sketches out there where maybe the first call SerialX.begin(baud, some-not-default-format)... and later call SerialX.begin(baud), and expected the format not to change. --- teensy3/HardwareSerial.h | 37 ++++++------------------------------- teensy3/HardwareSerial1.cpp | 3 ++- teensy3/HardwareSerial2.cpp | 3 ++- teensy3/HardwareSerial3.cpp | 3 ++- teensy3/HardwareSerial4.cpp | 3 ++- teensy3/HardwareSerial5.cpp | 3 ++- teensy3/HardwareSerial6.cpp | 3 ++- 7 files changed, 18 insertions(+), 37 deletions(-) diff --git a/teensy3/HardwareSerial.h b/teensy3/HardwareSerial.h index afa16d7a5..f6dd5703a 100644 --- a/teensy3/HardwareSerial.h +++ b/teensy3/HardwareSerial.h @@ -282,10 +282,7 @@ class HardwareSerial : public Stream #else //(__MK64FX512__) || defined(__MK66FX1M0__) enum {CNT_HARDWARE_SERIAL = 3}; #endif - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial_begin(BAUD2DIV(baud)); - serial_format(format); } + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial_end(); } virtual void transmitterEnable(uint8_t pin) { serial_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial_set_rx(pin); } @@ -336,10 +333,7 @@ class HardwareSerial2 : public HardwareSerial { public: constexpr HardwareSerial2(void (* const se)()) : HardwareSerial(se) {} - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial2_begin(BAUD2DIV2(baud)); - serial2_format(format); } + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial2_end(); } virtual void transmitterEnable(uint8_t pin) { serial2_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial2_set_rx(pin); } @@ -375,10 +369,7 @@ class HardwareSerial3 : public HardwareSerial { public: constexpr HardwareSerial3(void (* const se)()) : HardwareSerial(se) {} - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial3_begin(BAUD2DIV3(baud)); - serial3_format(format); } + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial3_end(); } virtual void transmitterEnable(uint8_t pin) { serial3_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial3_set_rx(pin); } @@ -414,10 +405,7 @@ class HardwareSerial4 : public HardwareSerial { public: constexpr HardwareSerial4(void (* const se)()) : HardwareSerial(se) {} - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial4_begin(BAUD2DIV3(baud)); - serial4_format(format); } + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial4_end(); } virtual void transmitterEnable(uint8_t pin) { serial4_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial4_set_rx(pin); } @@ -453,10 +441,7 @@ class HardwareSerial5 : public HardwareSerial { public: constexpr HardwareSerial5(void (* const se)()) : HardwareSerial(se) {} - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial5_begin(BAUD2DIV3(baud)); - serial5_format(format); } + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial5_end(); } virtual void transmitterEnable(uint8_t pin) { serial5_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial5_set_rx(pin); } @@ -492,17 +477,7 @@ class HardwareSerial6 : public HardwareSerial { public: constexpr HardwareSerial6(void (* const se)()) : HardwareSerial(se) {} -#if defined(__MK66FX1M0__) // For LPUART just pass baud straight in. - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial6_begin(baud); - serial6_format(format); } -#else - virtual void begin(uint32_t baud); - virtual void begin(uint32_t baud, uint32_t format) { - serial6_begin(BAUD2DIV3(baud)); - serial6_format(format); } -#endif + virtual void begin(uint32_t baud, uint32_t format=(uint32_t)-1); virtual void end(void) { serial6_end(); } virtual void transmitterEnable(uint8_t pin) { serial6_set_transmit_pin(pin); } virtual void setRX(uint8_t pin) { serial6_set_rx(pin); } diff --git a/teensy3/HardwareSerial1.cpp b/teensy3/HardwareSerial1.cpp index fd625ad57..9a13d2d5f 100644 --- a/teensy3/HardwareSerial1.cpp +++ b/teensy3/HardwareSerial1.cpp @@ -36,8 +36,9 @@ uint8_t _serialEvent1_default __attribute__((weak)) PROGMEM = 0 ; HardwareSerial Serial1(&serialEvent1); -void HardwareSerial::begin(uint32_t baud) { +void HardwareSerial::begin(uint32_t baud, uint32_t format) { serial_begin(BAUD2DIV(baud)); + if (format != (uint32_t)-1) serial_format(format); if (!_serialEvent1_default) addToSerialEventsList(); } diff --git a/teensy3/HardwareSerial2.cpp b/teensy3/HardwareSerial2.cpp index 8e10cc352..f2738860c 100644 --- a/teensy3/HardwareSerial2.cpp +++ b/teensy3/HardwareSerial2.cpp @@ -34,7 +34,8 @@ HardwareSerial2 Serial2(&serialEvent2); uint8_t _serialEvent2_default __attribute__((weak)) PROGMEM = 0 ; -void HardwareSerial2::begin(uint32_t baud) { +void HardwareSerial2::begin(uint32_t baud, uint32_t format) { serial2_begin(BAUD2DIV2(baud)); + if (format != (uint32_t)-1) serial2_format(format); if (!_serialEvent2_default) addToSerialEventsList(); } diff --git a/teensy3/HardwareSerial3.cpp b/teensy3/HardwareSerial3.cpp index 176736aa9..722af351f 100644 --- a/teensy3/HardwareSerial3.cpp +++ b/teensy3/HardwareSerial3.cpp @@ -35,7 +35,8 @@ HardwareSerial3 Serial3(&serialEvent3); uint8_t _serialEvent3_default __attribute__((weak)) PROGMEM = 0 ; -void HardwareSerial3::begin(uint32_t baud) { +void HardwareSerial3::begin(uint32_t baud, uint32_t format) { serial3_begin(BAUD2DIV3(baud)); + if (format != (uint32_t)-1) serial3_format(format); if (!_serialEvent3_default) addToSerialEventsList(); } diff --git a/teensy3/HardwareSerial4.cpp b/teensy3/HardwareSerial4.cpp index b25c87fba..f3884bf8a 100644 --- a/teensy3/HardwareSerial4.cpp +++ b/teensy3/HardwareSerial4.cpp @@ -37,8 +37,9 @@ HardwareSerial4 Serial4(&serialEvent4); uint8_t _serialEvent4_default __attribute__((weak)) PROGMEM = 0 ; -void HardwareSerial4::begin(uint32_t baud) { +void HardwareSerial4::begin(uint32_t baud, uint32_t format) { serial4_begin(BAUD2DIV3(baud)); + if (format != (uint32_t)-1) serial4_format(format); if (!_serialEvent4_default) addToSerialEventsList(); } diff --git a/teensy3/HardwareSerial5.cpp b/teensy3/HardwareSerial5.cpp index 15b7473de..d61aa83dd 100644 --- a/teensy3/HardwareSerial5.cpp +++ b/teensy3/HardwareSerial5.cpp @@ -37,8 +37,9 @@ HardwareSerial5 Serial5(&serialEvent5); uint8_t _serialEvent5_default __attribute__((weak)) PROGMEM = 0 ; -void HardwareSerial5::begin(uint32_t baud) { +void HardwareSerial5::begin(uint32_t baud, uint32_t format) { serial5_begin(BAUD2DIV3(baud)); + if (format != (uint32_t)-1) serial5_format(format); if (!_serialEvent5_default) addToSerialEventsList(); } diff --git a/teensy3/HardwareSerial6.cpp b/teensy3/HardwareSerial6.cpp index 3d80baf71..5364c3bae 100644 --- a/teensy3/HardwareSerial6.cpp +++ b/teensy3/HardwareSerial6.cpp @@ -37,12 +37,13 @@ HardwareSerial6 Serial6(&serialEvent6); uint8_t _serialEvent6_default __attribute__((weak)) PROGMEM = 0 ; -void HardwareSerial6::begin(uint32_t baud) { +void HardwareSerial6::begin(uint32_t baud, uint32_t format) { #if defined(__MK66FX1M0__) // For LPUART just pass baud straight in. serial6_begin(baud); #else serial6_begin(BAUD2DIV3(baud)); #endif + if (format != (uint32_t)-1) serial6_format(format); if (!_serialEvent6_default) addToSerialEventsList(); }