FrSky X3 board support added

This commit is contained in:
Bertrand Songis
2019-02-18 15:20:28 +01:00
parent bc5c3587d8
commit 8ec797730e
22 changed files with 270 additions and 60 deletions
+66 -10
View File
@@ -71,32 +71,50 @@ char * bluetoothReadline(bool error_reset)
if (!btRxFifo.pop(byte)) {
#if defined(PCBX9E) && !defined(USEHORUSBT) // X9E BT module can get unresponsive
TRACE("NO RESPONSE FROM BT MODULE");
#endif
return nullptr;
}
TRACE_NOCRLF("%02X ", byte);
#if 0
if (error_reset && byte == 'R' && bluetoothBufferIndex == 4 && memcmp(bluetoothBuffer, "ERRO", 4)) {
#if defined(PCBX9E) // X9E enter BT reset loop if following code is implemented
TRACE("BT Error...");
#else
TRACE("BT Reset...");
bluetoothBufferIndex = 0;
bluetoothDone();
bluetoothState = BLUETOOTH_STATE_OFF;
bluetoothWakeupTime = get_tmr10ms() + 100; /* 1s */
#endif
return NULL;
}
TRACE_NOCRLF("%02X ", byte);
else
#endif
if (byte == '\n') {
if (bluetoothBufferIndex > 2 && bluetoothBuffer[bluetoothBufferIndex-1] == '\r') {
bluetoothBuffer[bluetoothBufferIndex-1] = '\0';
bluetoothBufferIndex = 0;
TRACE("BT< %s", bluetoothBuffer);
if (error_reset && !strcmp((char *)bluetoothBuffer, "ERROR")) {
#if defined(PCBX9E) // X9E enter BT reset loop if following code is implemented
TRACE("BT error...");
#if defined(PCBX9E) // X9E enter BT reset loop if following code is implemented
TRACE("BT Error...");
#else
TRACE("BT Reset...");
bluetoothDone();
bluetoothState = BLUETOOTH_STATE_OFF;
bluetoothWakeupTime = get_tmr10ms() + 100; /* 1s */
#endif
return NULL;
return nullptr;
}
else {
if (!strncmp((char *)bluetoothBuffer, "Central:", 8))
strcpy(bluetoothLocalAddr, (char *)bluetoothBuffer+8);
else if (!strncmp((char *)bluetoothBuffer, "Peripheral:", 11))
strcpy(bluetoothLocalAddr, (char *)bluetoothBuffer+11);
return (char *)bluetoothBuffer;
if (!memcmp(bluetoothBuffer, "Central:", 8))
strcpy(bluetoothLocalAddr, (char *) bluetoothBuffer + 8);
else if (!memcmp(bluetoothBuffer, "Peripheral:", 11))
strcpy(bluetoothLocalAddr, (char *) bluetoothBuffer + 11);
return (char *) bluetoothBuffer;
}
}
else {
@@ -328,44 +346,67 @@ void bluetoothWakeup()
if (now < bluetoothWakeupTime)
return;
if (!usbPlugged()) {
bluetoothWakeupTime += 1000; // 10s
return;
}
bluetoothWakeupTime = now + 5; /* 50ms default */
static uint8_t oldbluetooth = 255;
if (oldbluetooth != bluetoothState) {
TRACE("State %d", bluetoothState);
oldbluetooth = bluetoothState;
}
if (g_eeGeneral.bluetoothMode == BLUETOOTH_OFF || (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && !IS_BLUETOOTH_TRAINER())) {
//TRACE_NOCRLF("A");
if (bluetoothState != BLUETOOTH_STATE_OFF) {
//TRACE_NOCRLF("Z");
bluetoothDone();
bluetoothState = BLUETOOTH_STATE_OFF;
}
bluetoothWakeupTime = now + 10; /* 100ms */
}
else if (bluetoothState == BLUETOOTH_STATE_OFF) {
//TRACE_NOCRLF("B");
bluetoothInit(BLUETOOTH_FACTORY_BAUDRATE);
bluetoothState = BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT;
}
if (bluetoothState != BLUETOOTH_STATE_OFF) {
//TRACE_NOCRLF("C");
bluetoothWriteWakeup();
if (bluetoothIsWriting()) {
//TRACE_NOCRLF("D");
return;
}
}
if (bluetoothState == BLUETOOTH_STATE_FACTORY_BAUDRATE_INIT) {
//TRACE_NOCRLF("F");
bluetoothWriteString("AT+BAUD4\r\n");
bluetoothState = BLUETOOTH_STATE_BAUDRATE_SENT;
bluetoothWakeupTime = now + 10; /* 100ms */
}
else if (bluetoothState == BLUETOOTH_STATE_BAUDRATE_SENT) {
//TRACE_NOCRLF("G");
bluetoothInit(BLUETOOTH_DEFAULT_BAUDRATE);
bluetoothState = BLUETOOTH_STATE_BAUDRATE_INIT;
bluetoothReadline(false);
bluetoothWakeupTime = now + 10; /* 100ms */
}
else if (bluetoothState == BLUETOOTH_STATE_CONNECTED) {
//TRACE_NOCRLF("H");
if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerData.mode == TRAINER_MODE_MASTER_BLUETOOTH) {
//TRACE_NOCRLF("I");
bluetoothReceiveTrainer();
}
else {
//TRACE_NOCRLF("J");
if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerData.mode == TRAINER_MODE_SLAVE_BLUETOOTH) {
TRACE_NOCRLF("S");
bluetoothSendTrainer();
bluetoothWakeupTime = now + 2; /* 20ms */
}
@@ -373,8 +414,13 @@ void bluetoothWakeup()
}
}
else {
//TRACE_NOCRLF("L");
char * line = bluetoothReadline();
if (line) {
TRACE("BT %s", line);
}
if (bluetoothState == BLUETOOTH_STATE_BAUDRATE_INIT) {
//TRACE_NOCRLF("M");
char command[32];
char * cur = strAppend(command, BLUETOOTH_COMMAND_NAME);
uint8_t len = ZLEN(g_eeGeneral.bluetoothName);
@@ -387,7 +433,7 @@ void bluetoothWakeup()
#if defined(PCBHORUS)
cur = strAppend(cur, "Horus");
#else
cur = strAppend(cur, "Taranis");
cur = strAppend(cur, "taranis");
#endif
}
strAppend(cur, "\r\n");
@@ -395,10 +441,12 @@ void bluetoothWakeup()
bluetoothState = BLUETOOTH_STATE_NAME_SENT;
}
else if (bluetoothState == BLUETOOTH_STATE_NAME_SENT && (!strncmp(line, "OK+", 3) || !strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) {
//TRACE_NOCRLF("N");
bluetoothWriteString("AT+TXPW\r\n");
bluetoothState = BLUETOOTH_STATE_POWER_SENT;
}
else if (bluetoothState == BLUETOOTH_STATE_POWER_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) {
//TRACE_NOCRLF("O");
if (g_eeGeneral.bluetoothMode == BLUETOOTH_TRAINER && g_model.trainerData.mode == TRAINER_MODE_MASTER_BLUETOOTH)
bluetoothWriteString("AT+ROLE1\r\n");
else
@@ -406,28 +454,35 @@ void bluetoothWakeup()
bluetoothState = BLUETOOTH_STATE_ROLE_SENT;
}
else if (bluetoothState == BLUETOOTH_STATE_ROLE_SENT && (!strncmp(line, "Central:", 8) || !strncmp(line, "Peripheral:", 11))) {
//TRACE_NOCRLF("P");
bluetoothState = BLUETOOTH_STATE_IDLE;
}
else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_REQUESTED) {
//TRACE_NOCRLF("Q");
bluetoothWriteString("AT+DISC?\r\n");
bluetoothState = BLUETOOTH_STATE_DISCOVER_SENT;
}
else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_SENT && !strcmp(line, "OK+DISCS")) {
//TRACE_NOCRLF("R");
bluetoothState = BLUETOOTH_STATE_DISCOVER_START;
}
else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strncmp(line, "OK+DISC:", 8)) {
//TRACE_NOCRLF("S");
strcpy(bluetoothDistantAddr, &line[8]); // TODO quick & dirty
}
else if (bluetoothState == BLUETOOTH_STATE_DISCOVER_START && !strcmp(line, "OK+DISCE")) {
//TRACE_NOCRLF("T");
bluetoothState = BLUETOOTH_STATE_DISCOVER_END;
}
else if (bluetoothState == BLUETOOTH_STATE_BIND_REQUESTED) {
//TRACE_NOCRLF("U");
char command[32];
strAppend(strAppend(strAppend(command, "AT+CON"), bluetoothDistantAddr), "\r\n");
bluetoothWriteString(command);
bluetoothState = BLUETOOTH_STATE_CONNECT_SENT;
}
else if ((bluetoothState == BLUETOOTH_STATE_IDLE || bluetoothState == BLUETOOTH_STATE_DISCONNECTED || bluetoothState == BLUETOOTH_STATE_CONNECT_SENT) && !strncmp(line, "Connected:", 10)) {
//TRACE_NOCRLF("V");
strcpy(bluetoothDistantAddr, &line[10]); // TODO quick & dirty
bluetoothState = BLUETOOTH_STATE_CONNECTED;
if (g_model.trainerData.mode == TRAINER_MODE_SLAVE_BLUETOOTH) {
@@ -435,6 +490,7 @@ void bluetoothWakeup()
}
}
else if (bluetoothState == BLUETOOTH_STATE_DISCONNECTED && !line) {
//TRACE_NOCRLF("W");
char command[32];
strAppend(strAppend(strAppend(command, "AT+CON"), bluetoothDistantAddr), "\r\n");
bluetoothWriteString(command);