feat: Automatic soft-serial fallback for module ports (#3823)
This commit is contained in:
@@ -44,7 +44,7 @@ static void* afhds2Init(uint8_t module)
|
||||
// serial port setup
|
||||
resetPulsesAFHDS2();
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART,
|
||||
&afhds2SerialInitParams);
|
||||
&afhds2SerialInitParams, false);
|
||||
|
||||
// mixer setup
|
||||
mixerSchedulerSetPeriod(module, AFHDS2_PERIOD);
|
||||
|
||||
@@ -1110,13 +1110,13 @@ static void* initModule(uint8_t module)
|
||||
params.baudrate = AFHDS3_UART_BAUDRATE;
|
||||
params.polarity =
|
||||
module == INTERNAL_MODULE ? ETX_Pol_Normal : ETX_Pol_Inverted;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
|
||||
#if defined(CONFIGURABLE_MODULE_PORT)
|
||||
if (!mod_st && module == EXTERNAL_MODULE) {
|
||||
// Try Connect using aux serial mod
|
||||
params.polarity = ETX_Pol_Normal;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -1125,7 +1125,7 @@ static void* initModule(uint8_t module)
|
||||
params.baudrate = AFHDS3_SOFTSERIAL_BAUDRATE;
|
||||
params.direction = ETX_Dir_TX;
|
||||
period = AFHDS3_SOFTSERIAL_COMMAND_TIMEOUT * 1000 /* us */;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, ¶ms, false);
|
||||
// TODO: telemetry RX ???
|
||||
}
|
||||
|
||||
|
||||
@@ -226,7 +226,7 @@ static void* crossfireInit(uint8_t module)
|
||||
if (module == INTERNAL_MODULE) {
|
||||
|
||||
params.baudrate = INT_CROSSFIRE_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -234,7 +234,7 @@ static void* crossfireInit(uint8_t module)
|
||||
if (module == EXTERNAL_MODULE) {
|
||||
|
||||
params.baudrate = EXT_CROSSFIRE_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, ¶ms, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -203,19 +203,12 @@ static etx_module_state_t* dsmInit(uint8_t module, uint32_t baudrate,
|
||||
|
||||
etx_serial_init params(dsmUartParams);
|
||||
params.baudrate = baudrate;
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
if (!mod_st) {
|
||||
// inverted soft-serial fallback
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, ¶ms);
|
||||
if (!mod_st) return nullptr;
|
||||
}
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, true);
|
||||
if (!mod_st) return nullptr;
|
||||
|
||||
if (telemetry) {
|
||||
params.direction = ETX_Dir_RX;
|
||||
if (!modulePortInitSerial(module, ETX_MOD_PORT_SPORT, ¶ms)) {
|
||||
// inverted soft-serial fallback
|
||||
modulePortInitSerial(module, ETX_MOD_PORT_SPORT_INV, ¶ms);
|
||||
}
|
||||
modulePortInitSerial(module, ETX_MOD_PORT_SPORT, ¶ms, true);
|
||||
}
|
||||
|
||||
mixerSchedulerSetPeriod(module, period);
|
||||
|
||||
@@ -163,7 +163,7 @@ static const etx_serial_init ghostSerialParams = {
|
||||
|
||||
static void* ghostInit(uint8_t module)
|
||||
{
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ghostSerialParams);
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ghostSerialParams, false);
|
||||
if (!mod_st) return nullptr;
|
||||
|
||||
mixerSchedulerSetPeriod(module, GHOST_PERIOD);
|
||||
|
||||
@@ -210,7 +210,7 @@ static void* multiInit(uint8_t module)
|
||||
// serial port setup
|
||||
// TODO: error handling
|
||||
cfg.direction = ETX_Dir_TX_RX;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -219,19 +219,14 @@ static void* multiInit(uint8_t module)
|
||||
// serial port setup
|
||||
cfg.direction = ETX_Dir_TX;
|
||||
cfg.polarity = ETX_Pol_Inverted;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg);
|
||||
|
||||
if (!mod_st) {
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &cfg);
|
||||
}
|
||||
|
||||
// no TX port: bail out
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &cfg, true);
|
||||
if (!mod_st) return nullptr;
|
||||
|
||||
// Init S.PORT RX channel
|
||||
cfg.direction = ETX_Dir_RX;
|
||||
cfg.polarity = ETX_Pol_Normal;
|
||||
modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &cfg);
|
||||
modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &cfg, false);
|
||||
}
|
||||
#endif
|
||||
|
||||
|
||||
@@ -111,12 +111,7 @@ static etx_serial_init ppmMLinkSerialParams = {
|
||||
static bool ppmInitMLinkTelemetry(uint8_t module)
|
||||
{
|
||||
// Try S.PORT hardware USART (requires HW inverters)
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ppmMLinkSerialParams) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
// fall-back to softserial
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT_INV, &ppmMLinkSerialParams) != nullptr) {
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ppmMLinkSerialParams, true) != nullptr) {
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -285,13 +285,13 @@ static void* pxx1Init(uint8_t module)
|
||||
if (module == INTERNAL_MODULE) {
|
||||
|
||||
txCfg.baudrate = _pxx1_internal_baudrate;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg, false);
|
||||
|
||||
if (!mod_st) {
|
||||
// assume that radios that don't have an internal UART
|
||||
// will have a module that uses legacy PXX1 (PWM)
|
||||
txCfg.encoding = ETX_Encoding_PXX1_PWM;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg, false);
|
||||
}
|
||||
|
||||
if (!mod_st) return nullptr;
|
||||
@@ -305,14 +305,14 @@ static void* pxx1Init(uint8_t module)
|
||||
|
||||
case MODULE_TYPE_R9M_LITE_PXX1: {
|
||||
txCfg.baudrate = EXTMODULE_PXX1_SERIAL_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg, false);
|
||||
if (!mod_st) return nullptr;
|
||||
} break;
|
||||
|
||||
case MODULE_TYPE_XJT_PXX1:
|
||||
case MODULE_TYPE_R9M_PXX1: {
|
||||
txCfg.encoding = ETX_Encoding_PXX1_PWM;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &txCfg, false);
|
||||
if (!mod_st) return nullptr;
|
||||
} break;
|
||||
|
||||
@@ -328,7 +328,7 @@ static void* pxx1Init(uint8_t module)
|
||||
rxCfg.direction = ETX_Dir_TX_RX;
|
||||
|
||||
// TODO: handle init errors properly
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &rxCfg) != nullptr) {
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &rxCfg, false) != nullptr) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
|
||||
@@ -640,7 +640,7 @@ static void* pxx2Init(uint8_t module)
|
||||
if (module == INTERNAL_MODULE) {
|
||||
|
||||
params.baudrate = PXX2_HIGHSPEED_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
if (!mod_st) return nullptr;
|
||||
resetAccessAuthenticationCount();
|
||||
}
|
||||
@@ -665,7 +665,7 @@ static void* pxx2Init(uint8_t module)
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms);
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
if (!mod_st) return nullptr;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,6 +21,7 @@
|
||||
|
||||
#include "sbus.h"
|
||||
#include "hal/module_port.h"
|
||||
#include "hal/serial_driver.h"
|
||||
#include "mixer_scheduler.h"
|
||||
|
||||
#include "opentx.h"
|
||||
@@ -96,7 +97,7 @@ const etx_serial_init sbusUartParams = {
|
||||
.baudrate = SBUS_BAUDRATE,
|
||||
.encoding = ETX_Encoding_8E2,
|
||||
.direction = ETX_Dir_TX,
|
||||
.polarity = ETX_Pol_Normal,
|
||||
.polarity = ETX_Pol_Inverted,
|
||||
};
|
||||
|
||||
static void* sbusInit(uint8_t module)
|
||||
@@ -106,13 +107,8 @@ static void* sbusInit(uint8_t module)
|
||||
if (module == INTERNAL_MODULE) return nullptr;
|
||||
#endif
|
||||
|
||||
// auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &sbusUartParams);
|
||||
// TODO: check if inverter is there, or mandate it somehow...
|
||||
// if (!mod_st) {
|
||||
// -> SOFT_INV has 'setPolarity'
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SOFT_INV, &sbusUartParams);
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &sbusUartParams, true);
|
||||
if (!mod_st) return nullptr;
|
||||
// }
|
||||
|
||||
mixerSchedulerSetPeriod(module, SBUS_PERIOD(module));
|
||||
return (void*)mod_st;
|
||||
@@ -139,9 +135,12 @@ static void sbusSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_
|
||||
auto drv = modulePortGetSerialDrv(mod_st->tx);
|
||||
auto drv_ctx = modulePortGetCtx(mod_st->tx);
|
||||
|
||||
// TODO: check if the port has an inverter and use it instead
|
||||
if(drv->setPolarity)
|
||||
auto port = mod_st->tx.port;
|
||||
if (port && port->set_inverted) {
|
||||
port->set_inverted(GET_SBUS_POLARITY(module));
|
||||
} else if (drv->setPolarity) {
|
||||
drv->setPolarity(drv_ctx, GET_SBUS_POLARITY(module));
|
||||
}
|
||||
|
||||
drv->sendBuffer(drv_ctx, buffer, p_data - buffer);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user