diff --git a/radio/src/bluetooth.cpp b/radio/src/bluetooth.cpp index 90cb0e714..6f6f21474 100644 --- a/radio/src/bluetooth.cpp +++ b/radio/src/bluetooth.cpp @@ -64,6 +64,9 @@ char * bluetoothReadline() while (1) { if (!btRxFifo.pop(byte)) { +#if defined(PCBX9E) // X9E BT module can get unresponsive + TRACE("NO RESPONSE FROM BT MODULE"); +#endif return NULL; } TRACE_NOCRLF("%02X ", byte); @@ -218,6 +221,65 @@ void bluetoothReceiveTrainer() } } +#if defined(PCBX9E) && defined(DEBUG) +void bluetoothWakeup(void) +{ + if (!g_eeGeneral.bluetoothMode) { + if (bluetoothState != BLUETOOTH_INIT) { + bluetoothDone(); + bluetoothState = BLUETOOTH_INIT; + } + } + else { + static tmr10ms_t waitEnd = 0; + if (bluetoothState != BLUETOOTH_STATE_IDLE) { + + if (bluetoothState == BLUETOOTH_INIT) { + bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE); + char command[32]; + char * cur = strAppend(command, BLUETOOTH_COMMAND_NAME); + uint8_t len = ZLEN(g_eeGeneral.bluetoothName); + if (len > 0) { + for (int i = 0; i < len; i++) { + *cur++ = idx2char(g_eeGeneral.bluetoothName[i]); + } + } + else { + cur = strAppend(cur, "Taranis-X9E"); + } + strAppend(cur, "\r\n"); + bluetoothWriteString(command); + bluetoothState = BLUETOOTH_WAIT_TTM; + waitEnd = get_tmr10ms() + 25; // 250ms + } + else if (bluetoothState == BLUETOOTH_WAIT_TTM) { + if (get_tmr10ms() > waitEnd) { + char * line = bluetoothReadline(); + if (strncmp(line, "OK+", 3)) { + bluetoothState = BLUETOOTH_STATE_IDLE; + } + else { + bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE); + const char btMessage[] = "TTM:BPS-115200"; + bluetoothWriteString(btMessage); + bluetoothState = BLUETOOTH_WAIT_BAUDRATE_CHANGE; + waitEnd = get_tmr10ms() + 250; // 2.5s + } + } + } + else if (bluetoothState == BLUETOOTH_WAIT_BAUDRATE_CHANGE) { + if (get_tmr10ms() > waitEnd) { + bluetoothState = BLUETOOTH_INIT; + } + } + } else if (IS_BLUETOOTH_TRAINER()){ + bluetoothState = BLUETOOTH_STATE_CONNECTED; + bluetoothWriteWakeup(); + bluetoothSendTrainer(); + } + } +} +#else //PCBX9E void bluetoothWakeup() { tmr10ms_t now = get_tmr10ms(); @@ -267,11 +329,7 @@ void bluetoothWakeup() } strAppend(cur, "\r\n"); bluetoothWriteString(command); -#if defined(PCBX9E) - bluetoothState = BLUETOOTH_STATE_CONNECTED; -#else bluetoothState = BLUETOOTH_STATE_NAME_SENT; -#endif } else if (bluetoothState == BLUETOOTH_STATE_NAME_SENT && !strncmp(line, "OK+", 3)) { bluetoothWriteString("AT+TXPW3\r\n"); @@ -317,3 +375,4 @@ void bluetoothWakeup() } } } +#endif diff --git a/radio/src/bluetooth.h b/radio/src/bluetooth.h index f490e6a26..e0597e635 100644 --- a/radio/src/bluetooth.h +++ b/radio/src/bluetooth.h @@ -19,6 +19,11 @@ */ enum BluetoothStates { +#if defined(PCBX9E) && defined(DEBUG) + BLUETOOTH_INIT, + BLUETOOTH_WAIT_TTM, + BLUETOOTH_WAIT_BAUDRATE_CHANGE, +#endif BLUETOOTH_STATE_OFF, BLUETOOTH_STATE_NAME_SENT, BLUETOOTH_STATE_POWER_SENT, @@ -38,6 +43,7 @@ enum BluetoothStates { extern volatile uint8_t bluetoothState; extern char bluetoothFriend[LEN_BLUETOOTH_FRIEND+1]; +char * bluetoothReadline(); void bluetoothWriteString(const char * command); void bluetoothForwardTelemetry(uint8_t data); void bluetoothWakeup(); diff --git a/radio/src/cli.cpp b/radio/src/cli.cpp index 32e912026..2653f3699 100644 --- a/radio/src/cli.cpp +++ b/radio/src/cli.cpp @@ -1205,11 +1205,14 @@ int cliBlueTooth(const char ** argv) char command[32]; strAppend(strAppend(command, argv[1]), "\r\n"); bluetoothWriteString(command); + char * line = bluetoothReadline(); + serialPrint(" 0) { if (baudrate > 0) { bluetoothInit(baudrate); - serialPrint("BT baudrate set to %d", baudrate); + char * line = bluetoothReadline(); + serialPrint("