TS serial: do BT setup only after 30sec timeout (#199)

This will avoid BT module restart after MCU restart. This will keep
BT connection between MCU reboots.

(cherry picked from commit 12443d73a0fa2a1fd0de0dcae3231473016b9efb)

Co-authored-by: Andrey Gusakov <dron0gus@gmail.com>
This commit is contained in:
rusefillc 2023-02-15 13:17:54 -05:00 committed by GitHub
parent 2f79f62399
commit 2bb430d32b
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 49 additions and 11 deletions

View File

@ -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);
}
}

View File

@ -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;

View File

@ -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);
}