diff --git a/firmware/console/binary/tunerstudio.cpp b/firmware/console/binary/tunerstudio.cpp index 7a11025..ebab8b9 100644 --- a/firmware/console/binary/tunerstudio.cpp +++ b/firmware/console/binary/tunerstudio.cpp @@ -92,6 +92,7 @@ static void printErrorCounters() { /* TunerStudio repeats connection attempts at ~1Hz rate. * So first char receive timeout should be less than 1s */ #define TS_COMMUNICATION_TIMEOUT TIME_MS2I(500) //0.5 Sec +#define TS_BT_COMMUNICATION_TIMEOUT TIME_MS2I(30 * 1000) // 30 Sec void tunerStudioDebug(TsChannelBase* tsChannel, const char *msg) { (void)tsChannel; @@ -426,6 +427,8 @@ static int tsProcessOne(TsChannelBase* tsChannel) { } void TunerstudioThread::ThreadTask() { + bool btInitAttempted = false; + sysinterval_t btTimeout = 0; auto channel = setupChannel(); // No channel configured for this thread, cancel. @@ -437,7 +440,17 @@ void TunerstudioThread::ThreadTask() { while (true) { if (tsProcessOne(channel) == 0) { //onDataArrived(true); + btTimeout = 0; } else { + btTimeout += TS_COMMUNICATION_TIMEOUT; + + if ((btTimeout >= TS_BT_COMMUNICATION_TIMEOUT) && + (btInitAttempted == false)) { + // Try to init BT module + channel->reStart(); + // Try this only once + btInitAttempted = true; + } //onDataArrived(false); } } diff --git a/firmware/console/binary/tunerstudio_io.h b/firmware/console/binary/tunerstudio_io.h index 23aea55..4b7f046 100644 --- a/firmware/console/binary/tunerstudio_io.h +++ b/firmware/console/binary/tunerstudio_io.h @@ -49,6 +49,7 @@ public: virtual void flush() { } virtual bool isConfigured() const { return true; } virtual bool isReady() const { return true; } + virtual int reStart() { } virtual void stop() { } // Base functions that use the above virtual implementation @@ -96,7 +97,10 @@ class SerialTsChannel : public SerialTsChannelBase { public: SerialTsChannel(SerialDriver& driver) : SerialTsChannelBase("Serial"), m_driver(&driver) { } - int start(uint32_t baud) override; + int start(uint32_t newBaud) override; + // Close and open serial ports with specified baud rate + // Also will do BT module setup if BT is enabled + int reStart() override; void stop() override; void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override; @@ -107,6 +111,7 @@ private: int bt_wait_ok(void); int bt_disconnect(void); + uint32_t baud; SerialDriver* const m_driver; }; #endif // HAL_USE_SERIAL @@ -117,7 +122,8 @@ class UartTsChannel : public SerialTsChannelBase { public: UartTsChannel(UARTDriver& driver) : SerialTsChannelBase("UART"), m_driver(&driver) { } - int start(uint32_t baud) override; + int start(uint32_t newBaud) override; + int reStart() override; void stop() override; void write(const uint8_t* buffer, size_t size, bool isEndOfPacket) override; diff --git a/firmware/console/binary/tunerstudio_io_serial.cpp b/firmware/console/binary/tunerstudio_io_serial.cpp index a3cc326..6e812ab 100644 --- a/firmware/console/binary/tunerstudio_io_serial.cpp +++ b/firmware/console/binary/tunerstudio_io_serial.cpp @@ -61,7 +61,23 @@ int SerialTsChannel::bt_disconnect(void) return bt_wait_ok(); } -int SerialTsChannel::start(uint32_t baud) { +int SerialTsChannel::start(uint32_t newBaud) { + SerialConfig cfg = { + .speed = newBaud, + .cr1 = 0, + .cr2 = USART_CR2_STOP1_BITS | USART_CR2_LINEN, + .cr3 = 0 + }; + + baud = newBaud; + + sdStart(m_driver, &cfg); + + return 0; +} + +/* this will also try to reinit BT module */ +int SerialTsChannel::reStart() { int ret = 0; SerialConfig cfg = { .speed = baud, @@ -70,6 +86,9 @@ int SerialTsChannel::start(uint32_t baud) { .cr3 = 0 }; + /* Stop first */ + sdStop(m_driver); + if (BT_SERIAL_OVER_JDY33) { /* try BT setup */ int retry = 3; @@ -200,12 +219,6 @@ int SerialTsChannel::start(uint32_t baud) { sdStart(m_driver, &cfg); } - if (ret < 0) { - /* set requested baudrate and wait for direct uart connection */ - cfg.speed = baud; - sdStart(m_driver, &cfg); - } - return ret; } @@ -223,14 +236,14 @@ size_t SerialTsChannel::readTimeout(uint8_t* buffer, size_t size, int timeout) { #endif // HAL_USE_SERIAL #if (HAL_USE_UART == TRUE) && (UART_USE_WAIT == TRUE) -int UartTsChannel::start(uint32_t baud) { +int UartTsChannel::start(uint32_t newBaud) { m_config.txend1_cb = NULL; m_config.txend2_cb = NULL; m_config.rxend_cb = NULL; m_config.rxchar_cb = NULL; m_config.rxerr_cb = NULL; m_config.timeout_cb = NULL; - m_config.speed = baud; + m_config.speed = newBaud; m_config.cr1 = 0; m_config.cr2 = 0/*USART_CR2_STOP1_BITS*/ | USART_CR2_LINEN; m_config.cr3 = 0; @@ -240,6 +253,12 @@ int UartTsChannel::start(uint32_t baud) { return 0; } +int UartTsChannel::reStart() { + stop(); + /* TODO: add BT setup? */ + start(baud); +} + void UartTsChannel::stop() { uartStop(m_driver); }