From 220a03f04376622cf3348e217520f4cb7a9c9a50 Mon Sep 17 00:00:00 2001 From: Theo Arends <11044339+arendst@users.noreply.github.com> Date: Sun, 27 Nov 2022 12:11:48 +0100 Subject: [PATCH] Add LD2410 debugging info --- .../TasmotaSerial-3.5.0/src/TasmotaSerial.cpp | 19 ++++++++++++++++ .../TasmotaSerial-3.5.0/src/TasmotaSerial.h | 4 +++- tasmota/tasmota.ino | 2 +- .../tasmota_xsns_sensor/xsns_102_ld2410.ino | 22 ++++++++++++++----- 4 files changed, 40 insertions(+), 7 deletions(-) diff --git a/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.cpp b/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.cpp index 82fce4934..ef72dce1c 100644 --- a/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.cpp +++ b/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.cpp @@ -48,6 +48,7 @@ TasmotaSerial::TasmotaSerial(int receive_pin, int transmit_pin, int hardware_fal m_valid = false; m_hardserial = false; m_hardswap = false; + m_overflow = false; m_stop_bits = 1; m_nwmode = nwmode; serial_buffer_size = buffer_size; @@ -267,6 +268,21 @@ bool TasmotaSerial::hardwareSerial(void) { #endif // ESP32 } +bool TasmotaSerial::overflow(void) { + if (m_hardserial) { +#ifdef ESP8266 + return Serial.hasOverrun(); // Returns then clear overrun flag +#endif // ESP8266 +#ifdef ESP32 + return false; // Not implemented +#endif // ESP32 + } else { + bool res = m_overflow; + m_overflow = false; + return res; + } +} + void TasmotaSerial::flush(void) { if (m_hardserial) { #ifdef ESP8266 @@ -305,6 +321,7 @@ int TasmotaSerial::read(void) { #endif // ESP32 } else { if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) return -1; +// m_overflow = false; uint32_t ch = m_buffer[m_out_pos]; m_out_pos = (m_out_pos +1) % serial_buffer_size; return ch; @@ -321,6 +338,7 @@ size_t TasmotaSerial::read(char* buffer, size_t size) { #endif // ESP32 } else { if ((-1 == m_rx_pin) || (m_in_pos == m_out_pos)) { return 0; } +// m_overflow = false; size_t count = 0; for( ; size && (m_in_pos == m_out_pos) ; --size, ++count) { *buffer++ = m_buffer[m_out_pos]; @@ -427,6 +445,7 @@ void IRAM_ATTR TasmotaSerial::rxRead(void) { m_in_pos = next; } else { // Buffer overrun - exit and fix Hardware Watchdog in case of high speed flooding + m_overflow = true; break; } diff --git a/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.h b/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.h index 759433d66..e62d75316 100644 --- a/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.h +++ b/lib/default/TasmotaSerial-3.5.0/src/TasmotaSerial.h @@ -60,7 +60,8 @@ class TasmotaSerial : public Stream { #ifdef ESP32 uint32_t getUart(void) const { return m_uart; } #endif - bool isValid() { return m_valid; } + bool isValid(void) { return m_valid; } + bool overflow(void); using Print::write; @@ -89,6 +90,7 @@ class TasmotaSerial : public Stream { bool m_nwmode; bool m_hardserial; bool m_hardswap; + bool m_overflow; bool m_high_speed = false; bool m_very_high_speed = false; // above 100000 bauds uint8_t *m_buffer = nullptr; diff --git a/tasmota/tasmota.ino b/tasmota/tasmota.ino index 137bd80c2..a90a1c069 100644 --- a/tasmota/tasmota.ino +++ b/tasmota/tasmota.ino @@ -683,7 +683,7 @@ void SleepDelay(uint32_t mseconds) { if (!TasmotaGlobal.backlog_nodelay && mseconds) { uint32_t wait = millis() + mseconds; while (!TimeReached(wait) && !Serial.available()) { // We need to service serial buffer ASAP as otherwise we get uart buffer overrun - XdrvCall(FUNC_SLEEP_LOOP); // Main purpose is reacting ASAP on serial data availability or interrupt handling (ADE7880) + XdrvXsnsCall(FUNC_SLEEP_LOOP); // Main purpose is reacting ASAP on serial data availability or interrupt handling (ADE7880) delay(1); } } else { diff --git a/tasmota/tasmota_xsns_sensor/xsns_102_ld2410.ino b/tasmota/tasmota_xsns_sensor/xsns_102_ld2410.ino index 535246108..6975efda5 100644 --- a/tasmota/tasmota_xsns_sensor/xsns_102_ld2410.ino +++ b/tasmota/tasmota_xsns_sensor/xsns_102_ld2410.ino @@ -21,7 +21,7 @@ * https://drive.google.com/drive/folders/1p4dhbEJA3YubyIjIIC7wwVsSo8x29Fq-?spm=a2g0o.detail.1000023.17.93465697yFwVxH * * Internal info: - * - After a LD2410 serial command a response takes 50mS + * - After a LD2410 serial command a response takes about 10mS * - After a LD2410 restart it takes at least 1000mS before commands are allowed \*********************************************************************************************/ @@ -40,6 +40,8 @@ #define LD2410_CMND_SET_BAUDRATE 0xA1 #define LD2410_CMND_FACTORY_RESET 0xA2 #define LD2410_CMND_REBOOT 0xA3 +#define LD2410_CMND_SET_BLUETOOTH 0xA4 +#define LD2410_CMND_GET_BLUETOOTH_MAC 0xA5 const uint8_t LD2410_config_header[4] = {0xFD, 0xFC, 0xFB, 0xFA}; const uint8_t LD2410_config_footer[4] = {0x04, 0x03, 0x02, 0x01}; @@ -151,6 +153,11 @@ bool Ld2410Match(const uint8_t *header, uint32_t offset) { } void Ld2410Input(void) { + + if (LD2410Serial->overflow()) { + AddLog(LOG_LEVEL_DEBUG, PSTR("LD2: Serial buffer overrun")); + } + while (LD2410Serial->available()) { yield(); // Fix watchdogs @@ -178,6 +185,11 @@ void Ld2410Input(void) { if (target_header) { // F4F3F2F1 if (Ld2410Match(LD2410_target_footer, len -4)) { // F8F7F6F5 Ld1410HandleTargetData(); + + // Break to test Hardware Watchdog due to buffer overrun + LD2410.byte_counter = 0; + break; + } } else if (config_header) { // FDFCFBFA @@ -271,8 +283,8 @@ void Ld2410Every100MSecond(void) { case 56: Ld2410SendCommand(LD2410_CMND_REBOOT); // Wait at least 1 second break; - case 46: - LD2410.step = 7; + case 51: + LD2410.step = 12; AddLog(LOG_LEVEL_DEBUG, PSTR("LD2: Settings factory reset")); break; @@ -311,7 +323,7 @@ void Ld2410Every100MSecond(void) { LD2410Serial->begin(57600); break; */ - // case 7: Init + // case 12: Init case 5: Ld2410SetConfigMode(); // Stop running mode break; @@ -365,7 +377,7 @@ void Ld2410Detect(void) { if (LD2410Serial->hardwareSerial()) { ClaimSerial(); } LD2410.retry = 4; - LD2410.step = 7; + LD2410.step = 12; } } }