chore: New module/serial API (#3055)
* WiP: new module interface * WiP: softserial timer DMA * WiP: setup() and send() in one step and remove methods specific to internal or external module * fixes and extra port config * horus targets * module ports have moved * T8 support (only internal module) * fix nv14 * stupid GCC.... * disable BT * remove nv14 telem driver * remove old aux serial driver * Move module buffers out of pulses.h * Soft serial fixes and PXX1 support * Fixed PPM module output * Fixed CLI * compilation fixes * Ported bluetooth and flysky gimbal to new serial driver * Re-enable soft-serial RX * fixes and cleanup * nv14 fixes * Fix flysky gimbals * Ghost module * 16 bytes of RAM * Remove getByte from module API * Ghost cleanup * fix: _half_duplex_input() must be called after usart init * WiP * more cleanup * Remove board specific settings from PXX1 / PXX2 drivers * A bit of PXX2 header cleanup * Add support for OVER8 and ONEBIT * Add some support for inverters (NV14 / EL18) * Support NV14 power fixes in generic board * Fix polarity handling * Fix RX DMA * Fix mixer not running * Add soft-serial polarity control * Quick fix for telemetry polling while module port is changed * Fix bootloader DEBUG * Fix MPM telemetry * Fix pulse start * DSM2 soft-serial fallback * DSMP async module restart * Fix CRSF LUA upstream * Remove global variable telemetryProtocol * Use HAL/LL constants * Fix various issue with AFHDS3 and NV14/EL18 internal module * Fixed S.PORT unit tests * fix: update widx after writing at current index * fix: init error condition * Fix: possible infinite loop in _find_port() * Add support for bidir S.PORT init * Add support for "getLastByte" in HW serial driver * PXX1: Add support for S.PORT based on IDLE callback * Fix rebase error * Fix softserial _rx_fifo_push() * Add softserial support for waitForTxCompleted() * Fix MPM flashing (internal / external) * Fixed external MPM * Activate RX DMA on internal module Seems to work with serial passthrough without issues, at least on the L3R PRO. * Fix softserial DMA on stdlcd F4 targets * Add softserial support for 32bit timers (mostly TIM2) * More precise soft-serial * Fix MPM update using inverters & HW uart * Raise S.PORT DMA/USART IRQ prio to achieve faster switching to input * Remove useless delays * Fix flashing external ELRS module * Added a retry mechanism for FrSky S.PORT update * Remove useless enableRx() * External FrSky modules are all flashed via S.PORT * Remove power management unit update This was only used on the first batch of X-lite.
This commit is contained in:
+172
-109
@@ -19,10 +19,21 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
#include "pulses/pxx1.h"
|
||||
#include "mixer_scheduler.h"
|
||||
#include "hal/module_port.h"
|
||||
#include "heartbeat_driver.h"
|
||||
#include "mixer_scheduler.h"
|
||||
|
||||
#include "pxx1_transport.h"
|
||||
#include "pxx1.h"
|
||||
|
||||
#include "opentx.h"
|
||||
|
||||
static uint32_t _pxx1_internal_baudrate = PXX1_DEFAULT_SERIAL_BAUDRATE;
|
||||
|
||||
void pxx1SetInternalBaudrate(uint32_t baudrate)
|
||||
{
|
||||
_pxx1_internal_baudrate = baudrate;
|
||||
}
|
||||
|
||||
template <class PxxTransport>
|
||||
void Pxx1Pulses<PxxTransport>::addFlag1(uint8_t module, uint8_t sendFailsafe)
|
||||
@@ -63,10 +74,12 @@ void Pxx1Pulses<PxxTransport>::addExtraFlags(uint8_t module)
|
||||
extraFlags |= (1 << 6);
|
||||
}
|
||||
|
||||
#if defined(HARDWARE_EXTERNAL_MODULE) && defined(HARDWARE_INTERNAL_MODULE)
|
||||
// Disable S.PORT if internal module is active
|
||||
if (module == EXTERNAL_MODULE && isSportLineUsedByInternalModule()) {
|
||||
extraFlags |= (1 << 5);
|
||||
}
|
||||
#endif
|
||||
|
||||
PxxTransport::addByte(extraFlags);
|
||||
}
|
||||
@@ -175,17 +188,25 @@ void Pxx1Pulses<PxxTransport>::add8ChannelsFrame(uint8_t module, uint8_t sendUpp
|
||||
}
|
||||
|
||||
template <class PxxTransport>
|
||||
void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
|
||||
Pxx1Pulses<PxxTransport>::Pxx1Pulses(uint8_t* buffer)
|
||||
: PxxTransport(buffer)
|
||||
{
|
||||
}
|
||||
|
||||
template <class PxxTransport>
|
||||
void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module, Pxx1Type type)
|
||||
{
|
||||
uint8_t sendUpperChannels = 0;
|
||||
uint8_t sendFailsafe = 0;
|
||||
|
||||
PxxTransport::initFrame(PXX_PULSES_PERIOD);
|
||||
|
||||
#if defined(PXX_FREQUENCY_HIGH)
|
||||
if (moduleState[module].protocol == PROTOCOL_CHANNELS_PXX1_SERIAL) {
|
||||
// Fast serial PXX1 (either X-lite internal or R9M-lite):
|
||||
// - up to 16 channels sent in the same frame
|
||||
//
|
||||
if (type == Pxx1Type::FAST_SERIAL) {
|
||||
if (moduleState[module].counter-- == 0) {
|
||||
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET && g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
|
||||
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET &&
|
||||
g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
|
||||
moduleState[module].counter = 1000;
|
||||
}
|
||||
add8ChannelsFrame(module, 0, sendFailsafe);
|
||||
@@ -194,8 +215,11 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
|
||||
}
|
||||
return;
|
||||
}
|
||||
#endif
|
||||
|
||||
// Slow PXX1:
|
||||
// - if more than 8 channels shall be sent, it happens
|
||||
// in alternating frames
|
||||
//
|
||||
if (moduleState[module].counter & 0x01) {
|
||||
// channelsCount is shifted by 8
|
||||
sendUpperChannels = g_model.moduleData[module].channelsCount;
|
||||
@@ -207,7 +231,8 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
|
||||
}
|
||||
} else {
|
||||
if (moduleState[module].counter == 0) {
|
||||
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET && g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
|
||||
sendFailsafe = (g_model.moduleData[module].failsafeMode != FAILSAFE_NOT_SET &&
|
||||
g_model.moduleData[module].failsafeMode != FAILSAFE_RECEIVER);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -218,129 +243,167 @@ void Pxx1Pulses<PxxTransport>::setupFrame(uint8_t module)
|
||||
}
|
||||
}
|
||||
|
||||
template class Pxx1Pulses<StandardPxx1Transport<PwmPxxBitTransport> >;
|
||||
template class Pxx1Pulses<StandardPxx1Transport<SerialPxxBitTransport> >;
|
||||
template class Pxx1Pulses<StandardPxx1Transport>;
|
||||
template class Pxx1Pulses<UartPxx1Transport>;
|
||||
|
||||
#if defined(INTMODULE_USART) && defined(INTERNAL_MODULE_PXX1)
|
||||
#include "intmodule_serial_driver.h"
|
||||
|
||||
static const etx_serial_init pxx1SerialInit = {
|
||||
.baudrate = INTMODULE_PXX1_SERIAL_BAUDRATE,
|
||||
.parity = ETX_Parity_None,
|
||||
.stop_bits = ETX_StopBits_One,
|
||||
.word_length = ETX_WordLength_8,
|
||||
.rx_enable = false,
|
||||
static const etx_serial_init pxx1SerialCfg = {
|
||||
.baudrate = 0,
|
||||
.encoding = ETX_Encoding_8N1,
|
||||
.direction = ETX_Dir_TX,
|
||||
.polarity = ETX_Pol_Normal,
|
||||
};
|
||||
|
||||
static void* pxx1InitInternal(uint8_t module)
|
||||
static const etx_serial_driver_t* _sport_drv;
|
||||
static void* _sport_ctx;
|
||||
|
||||
static void pxx1SportSensorPolling()
|
||||
{
|
||||
void* uart_ctx = IntmoduleSerialDriver.init(&pxx1SerialInit);
|
||||
if (outputTelemetryBuffer.destination != TELEMETRY_ENDPOINT_SPORT)
|
||||
return;
|
||||
|
||||
#if defined(INTMODULE_HEARTBEAT)
|
||||
init_intmodule_heartbeat();
|
||||
#endif
|
||||
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
|
||||
INTERNAL_MODULE_ON();
|
||||
// Match sensor polling from the module
|
||||
// -> detect <0x7E [Physical ID]> as the last sequence
|
||||
uint8_t b;
|
||||
if (_sport_drv->getLastByte(_sport_ctx, 2, &b) <= 0 || b != START_STOP)
|
||||
return;
|
||||
if (_sport_drv->getLastByte(_sport_ctx, 1, &b) <= 0 ||
|
||||
b != outputTelemetryBuffer.sport.physicalId)
|
||||
return;
|
||||
|
||||
return uart_ctx;
|
||||
_sport_drv->sendBuffer(_sport_ctx,
|
||||
outputTelemetryBuffer.data + 1,
|
||||
outputTelemetryBuffer.size - 1);
|
||||
|
||||
outputTelemetryBuffer.reset();
|
||||
}
|
||||
|
||||
static void pxx1DeInitInternal(void* context)
|
||||
static void* pxx1Init(uint8_t module)
|
||||
{
|
||||
INTERNAL_MODULE_OFF();
|
||||
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
|
||||
#if defined(INTMODULE_HEARTBEAT)
|
||||
stop_intmodule_heartbeat();
|
||||
#endif
|
||||
IntmoduleSerialDriver.deinit(context);
|
||||
etx_module_state_t* mod_st = nullptr;
|
||||
etx_serial_init txCfg(pxx1SerialCfg);
|
||||
|
||||
if (module == INTERNAL_MODULE) {
|
||||
|
||||
txCfg.baudrate = _pxx1_internal_baudrate;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
|
||||
|
||||
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);
|
||||
}
|
||||
|
||||
if (!mod_st) return nullptr;
|
||||
}
|
||||
|
||||
if (module == EXTERNAL_MODULE) {
|
||||
|
||||
// Init driver (timer / serial) based on module type
|
||||
uint8_t type = g_model.moduleData[module].type;
|
||||
switch(type) {
|
||||
|
||||
case MODULE_TYPE_R9M_LITE_PXX1: {
|
||||
txCfg.baudrate = EXTMODULE_PXX1_SERIAL_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, &txCfg);
|
||||
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);
|
||||
if (!mod_st) return nullptr;
|
||||
} break;
|
||||
|
||||
default:
|
||||
// unknown module: bail out!
|
||||
return nullptr;
|
||||
}
|
||||
}
|
||||
|
||||
// Init telemetry RX
|
||||
etx_serial_init rxCfg(pxx1SerialCfg);
|
||||
rxCfg.baudrate = FRSKY_SPORT_BAUDRATE;
|
||||
rxCfg.direction = ETX_Dir_TX_RX;
|
||||
|
||||
// TODO: handle init errors properly
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &rxCfg) != nullptr) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
_sport_drv = drv;
|
||||
_sport_ctx = ctx;
|
||||
drv->setIdleCb(ctx, pxx1SportSensorPolling);
|
||||
}
|
||||
}
|
||||
|
||||
// Store PXX1 type in 'user_data'
|
||||
if (txCfg.encoding == ETX_Encoding_PXX1_PWM ||
|
||||
txCfg.baudrate == PXX1_DEFAULT_SERIAL_BAUDRATE) {
|
||||
|
||||
// Legacy / slow PXX1
|
||||
mixerSchedulerSetPeriod(module, PXX1_DEFAULT_PERIOD);
|
||||
if (txCfg.encoding == ETX_Encoding_PXX1_PWM) {
|
||||
mod_st->user_data = (void*)Pxx1Type::PWM;
|
||||
} else {
|
||||
mod_st->user_data = (void*)Pxx1Type::SLOW_SERIAL;
|
||||
}
|
||||
} else {
|
||||
// Fast PXX1
|
||||
mixerSchedulerSetPeriod(module, PXX1_FAST_PERIOD);
|
||||
mod_st->user_data = (void*)Pxx1Type::FAST_SERIAL;
|
||||
}
|
||||
|
||||
return mod_st;
|
||||
}
|
||||
|
||||
static void pxx1SetupPulsesInternal(void* context, int16_t* channels, uint8_t nChannels)
|
||||
static void pxx1DeInit(void* ctx)
|
||||
{
|
||||
(void)context;
|
||||
|
||||
// TODO:
|
||||
(void)channels;
|
||||
(void)nChannels;
|
||||
intmodulePulsesData.pxx_uart.setupFrame(INTERNAL_MODULE);
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
modulePortDeInit(mod_st);
|
||||
}
|
||||
|
||||
static void pxx1SendPulsesInternal(void* context)
|
||||
static void pxx1SendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
|
||||
{
|
||||
IntmoduleSerialDriver.sendBuffer(context, intmodulePulsesData.pxx_uart.getData(),
|
||||
intmodulePulsesData.pxx_uart.getSize());
|
||||
}
|
||||
|
||||
const etx_module_driver_t Pxx1InternalSerialDriver = {
|
||||
.protocol = PROTOCOL_CHANNELS_PXX1_SERIAL,
|
||||
.init = pxx1InitInternal,
|
||||
.deinit = pxx1DeInitInternal,
|
||||
.setupPulses = pxx1SetupPulsesInternal,
|
||||
.sendPulses = pxx1SendPulsesInternal,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
#if defined(EXTMODULE_USART) && defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
|
||||
#include "extmodule_serial_driver.h"
|
||||
|
||||
static const etx_serial_init pxx1ExtSerialInit = {
|
||||
.baudrate = EXTMODULE_PXX1_SERIAL_BAUDRATE,
|
||||
.parity = ETX_Parity_None,
|
||||
.stop_bits = ETX_StopBits_One,
|
||||
.word_length = ETX_WordLength_8,
|
||||
.rx_enable = false,
|
||||
};
|
||||
|
||||
static void* pxx1InitExternal(uint8_t module)
|
||||
{
|
||||
auto drv = extmoduleGetSerialPort();
|
||||
if (!drv) return nullptr;
|
||||
|
||||
void* uart_ctx = drv->init(&pxx1ExtSerialInit);
|
||||
|
||||
mixerSchedulerSetPeriod(EXTERNAL_MODULE, EXTMODULE_PXX1_SERIAL_PERIOD);
|
||||
EXTERNAL_MODULE_ON();
|
||||
|
||||
return uart_ctx;
|
||||
}
|
||||
|
||||
static void pxx1DeInitExternal(void* context)
|
||||
{
|
||||
EXTERNAL_MODULE_OFF();
|
||||
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
|
||||
|
||||
auto drv = extmoduleGetSerialPort();
|
||||
if (drv) drv->deinit(context);
|
||||
}
|
||||
|
||||
static void pxx1SetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
|
||||
{
|
||||
(void)context;
|
||||
|
||||
// TODO:
|
||||
(void)channels;
|
||||
(void)nChannels;
|
||||
|
||||
extmodulePulsesData.pxx_uart.setupFrame(EXTERNAL_MODULE);
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
auto module = modulePortGetModule(mod_st);
|
||||
auto pxx1_type = (Pxx1Type)(uintptr_t)mod_st->user_data;
|
||||
|
||||
uint32_t frame_len = 0;
|
||||
if (pxx1_type == Pxx1Type::PWM) {
|
||||
PwmPxx1Pulses frame(buffer);
|
||||
frame.setupFrame(module, pxx1_type);
|
||||
frame_len = frame.getSize();
|
||||
} else {
|
||||
UartPxx1Pulses frame(buffer);
|
||||
frame.setupFrame(module, pxx1_type);
|
||||
frame_len = frame.getSize();
|
||||
}
|
||||
|
||||
if (frame_len == 0) return;
|
||||
|
||||
auto drv = modulePortGetSerialDrv(mod_st->tx);
|
||||
auto drv_ctx = modulePortGetCtx(mod_st->tx);
|
||||
drv->sendBuffer(drv_ctx, buffer, frame_len);
|
||||
}
|
||||
|
||||
static void pxx1SendPulsesExternal(void* context)
|
||||
static void pxx1ProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
|
||||
{
|
||||
auto drv = extmoduleGetSerialPort();
|
||||
if (!drv) return;
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
auto module = modulePortGetModule(mod_st);
|
||||
|
||||
drv->sendBuffer(context, extmodulePulsesData.pxx_uart.getData(),
|
||||
extmodulePulsesData.pxx_uart.getSize());
|
||||
processFrskySportTelemetryData(module, data, buffer, *len);
|
||||
}
|
||||
|
||||
const etx_module_driver_t Pxx1ExternalSerialDriver = {
|
||||
.protocol = PROTOCOL_CHANNELS_PXX1_SERIAL,
|
||||
.init = pxx1InitExternal,
|
||||
.deinit = pxx1DeInitExternal,
|
||||
.setupPulses = pxx1SetupPulsesExternal,
|
||||
.sendPulses = pxx1SendPulsesExternal,
|
||||
const etx_proto_driver_t Pxx1Driver = {
|
||||
.protocol = PROTOCOL_CHANNELS_PXX1,
|
||||
.init = pxx1Init,
|
||||
.deinit = pxx1DeInit,
|
||||
.sendPulses = pxx1SendPulses,
|
||||
.processData = pxx1ProcessData,
|
||||
};
|
||||
|
||||
#endif
|
||||
|
||||
Reference in New Issue
Block a user