diff --git a/arduino/beken-72xx/cores/arduino/SerialClass.cpp b/arduino/beken-72xx/cores/arduino/SerialClass.cpp index f260a3a..09e2c49 100644 --- a/arduino/beken-72xx/cores/arduino/SerialClass.cpp +++ b/arduino/beken-72xx/cores/arduino/SerialClass.cpp @@ -24,10 +24,29 @@ SerialClass::SerialClass(uint8_t port) { this->buf = NULL; } +#if LT_AUTO_DOWNLOAD_REBOOT +static uint8_t adrState = 0; +static const uint8_t adrCmd[] = {0x01, 0xE0, 0xFC, 0x01, 0x00}; + +static void adrParse(uint8_t c) { + // parse and respond to link check command (CMD_LinkCheck=0) + adrState = (adrState + 1) * (c == adrCmd[adrState]); + if (adrState == 5) { + LT_I("Auto download mode: rebooting"); + LT.restart(); + } +} +#endif + static void callback(int port, void *param) { RingBuffer *buf = (RingBuffer *)param; int ch; while ((ch = uart_read_byte(port)) != -1) { +#if LT_AUTO_DOWNLOAD_REBOOT && defined(PIN_SERIAL1_RX) + // parse UART protocol commands on UART1 + if (port == UART1_PORT) + adrParse(ch); +#endif buf->store_char(ch); } } diff --git a/arduino/beken-72xx/cores/arduino/main.cpp b/arduino/beken-72xx/cores/arduino/main.cpp index 2a7793c..f4b2be3 100644 --- a/arduino/beken-72xx/cores/arduino/main.cpp +++ b/arduino/beken-72xx/cores/arduino/main.cpp @@ -16,6 +16,10 @@ beken_thread_t mainThread; void initArduino() { // set default UART output port uart_print_port = LT_UART_DEFAULT_PORT - 1; +#if LT_AUTO_DOWNLOAD_REBOOT && defined(PIN_SERIAL1_RX) && defined(PIN_SERIAL1_TX) + // initialize auto-download-reboot parser + Serial1.begin(115200); +#endif } bool startMainTask() { diff --git a/arduino/libretuya/core/LibreTuyaConfig.h b/arduino/libretuya/core/LibreTuyaConfig.h index 0a5a5ea..75d8611 100644 --- a/arduino/libretuya/core/LibreTuyaConfig.h +++ b/arduino/libretuya/core/LibreTuyaConfig.h @@ -86,6 +86,10 @@ #define LT_MICROS_HIGH_RES 1 #endif +#ifndef LT_AUTO_DOWNLOAD_REBOOT +#define LT_AUTO_DOWNLOAD_REBOOT 1 +#endif + // Per-module logging output - applies to all loglevels #ifndef LT_DEBUG_ALL #define LT_DEBUG_ALL 0 diff --git a/arduino/libretuya/core/main.cpp b/arduino/libretuya/core/main.cpp index dd58744..bbad290 100644 --- a/arduino/libretuya/core/main.cpp +++ b/arduino/libretuya/core/main.cpp @@ -52,12 +52,12 @@ void runPeriodicTasks() { int main(void) { // print a startup banner LT_BANNER(); + // initialize C library + __libc_init_array(); // inform about the reset reason LT_I("Reset reason: %u", LT.getResetReason()); // initialize Arduino framework initArduino(); - // initialize C library - __libc_init_array(); // optionally initialize per-variant code initVariant(); // initialize FAL