Skip to content

Commit

Permalink
feat(radio): discover SBUS trainer availability
Browse files Browse the repository at this point in the history
  • Loading branch information
raphaelcoeffic committed Oct 30, 2023
1 parent 745bb8d commit 649be2e
Show file tree
Hide file tree
Showing 10 changed files with 184 additions and 133 deletions.
65 changes: 64 additions & 1 deletion radio/src/boards/generic_stm32/module_ports.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -175,6 +175,24 @@ static const stm32_usart_t extmoduleUSART = {

DEFINE_STM32_SERIAL_PORT(ExternalModule, extmoduleUSART, INTMODULE_FIFO_SIZE, 0);

#elif defined(TRAINER_MODULE_SBUS_USART)

static const stm32_usart_t sbus_trainer_USART = {
.USARTx = TRAINER_MODULE_SBUS_USART,
.GPIOx = TRAINER_MODULE_SBUS_GPIO,
.GPIO_Pin = TRAINER_MODULE_SBUS_GPIO_PIN,
.IRQn = (IRQn_Type)-1,
.IRQ_Prio = 0,
.txDMA = nullptr,
.txDMA_Stream = 0,
.txDMA_Channel = 0,
.rxDMA = TRAINER_MODULE_SBUS_DMA,
.rxDMA_Stream = TRAINER_MODULE_SBUS_DMA_STREAM_LL,
.rxDMA_Channel = TRAINER_MODULE_SBUS_DMA_CHANNEL,
};

DEFINE_STM32_SERIAL_PORT(SbusTrainer, sbus_trainer_USART, 32, 0);

#endif

static stm32_pulse_dma_tc_cb_t _ext_timer_DMA_TC_Callback;
Expand Down Expand Up @@ -221,7 +239,30 @@ extern "C" void EXTMODULE_TIMER_IRQHandler()
}

DEFINE_STM32_SOFTSERIAL_PORT(ExternalModule, extmoduleTimer);
#endif

#if defined(TRAINER_MODULE_CPPM)

// static_assert(__IS_TRAINER_TIMER_IN_CHANNEL_SUPPORTED(TRAINER_MODULE_CPPM_TIMER_Channel),
// "Unsupported trainer timer input channel");

static const stm32_pulse_timer_t trainerModuleTimer = {
.GPIOx = TRAINER_MODULE_CPPM_GPIO,
.GPIO_Pin = TRAINER_MODULE_CPPM_GPIO_PIN,
.GPIO_Alternate = TRAINER_MODULE_CPPM_GPIO_AF,
.TIMx = TRAINER_MODULE_CPPM_TIMER,
.TIM_Freq = TRAINER_MODULE_CPPM_FREQ,
.TIM_Channel = TRAINER_MODULE_CPPM_TIMER_Channel,
.TIM_IRQn = TRAINER_MODULE_CPPM_TIMER_IRQn,
.DMAx = nullptr,
.DMA_Stream = 0,
.DMA_Channel = 0,
.DMA_IRQn = (IRQn_Type)0,
.DMA_TC_CallbackPtr = nullptr,
};

#endif // TRAINER_MODULE_CPPM

#endif // HARDWARE_EXTERNAL_MODULE

#define TELEMETRY_USART_IRQ_PRIORITY 0
#define TELEMETRY_DMA_IRQ_PRIORITY 0
Expand Down Expand Up @@ -454,6 +495,28 @@ static const etx_module_port_t _external_ports[] = {
.set_inverted = nullptr,
#endif
},
#if !defined(EXTMODULE_USART) && defined(TRAINER_MODULE_SBUS_USART)
// RX on HEARTBEAT
{
.port = ETX_MOD_PORT_UART,
.type = ETX_MOD_TYPE_SERIAL,
.dir_flags = ETX_MOD_DIR_RX,
.drv = { .serial = &STM32SerialDriver },
.hw_def = REF_STM32_SERIAL_PORT(SbusTrainer),
.set_inverted = nullptr,
},
#endif
#if defined(TRAINER_MODULE_CPPM)
// Timer input on HEARTBEAT
{
.port = ETX_MOD_PORT_TIMER,
.type = ETX_MOD_TYPE_TIMER,
.dir_flags = ETX_MOD_DIR_RX,
.drv = { .timer = nullptr },
.hw_def = (void*)&trainerModuleTimer,
.set_inverted = nullptr,
},
#endif
// TX/RX half-duplex on S.PORT
{
.port = ETX_MOD_PORT_SPORT,
Expand Down
51 changes: 30 additions & 21 deletions radio/src/gui/gui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -768,10 +768,11 @@ bool isPxx2IsrmChannelsCountAllowed(int channels)

bool isTrainerUsingModuleBay()
{
#if defined(PCBTARANIS)
if (TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE <= g_model.trainerData.mode && g_model.trainerData.mode <= TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE)
if (g_model.trainerData.mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE ||
g_model.trainerData.mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE) {
return true;
#endif
}

return false;
}

Expand Down Expand Up @@ -935,7 +936,8 @@ bool isExternalModuleAvailable(int moduleType)

#if defined(PXX2)
return modulePortFind(EXTERNAL_MODULE, ETX_MOD_TYPE_SERIAL,
ETX_MOD_PORT_UART, ETX_Pol_Normal);
ETX_MOD_PORT_UART, ETX_Pol_Normal,
ETX_MOD_DIR_TX_RX | ETX_MOD_FULL_DUPLEX);
#else
return false;
#endif
Expand Down Expand Up @@ -1068,26 +1070,33 @@ bool isTrainerModeAvailable(int mode)
return false;
#endif

#if !defined(TRAINER_MODULE_CPPM)
if (mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE)
return false;
#endif
if (mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE ||
mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE) {

#if !defined(TRAINER_MODULE_SBUS)
if (mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE)
return false;
#endif
// no external module or is enabled
if (!modulePortGetModuleDescription(EXTERNAL_MODULE) ||
IS_EXTERNAL_MODULE_ENABLED()) {
return false;
}

#if defined(TRAINER_MODULE_CPPM) || defined(TRAINER_MODULE_SBUS)
if (IS_EXTERNAL_MODULE_ENABLED() &&
(mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE ||
mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE))
return false;
#endif
if (mode == TRAINER_MODE_MASTER_CPPM_EXTERNAL_MODULE) {
auto port = modulePortFind(EXTERNAL_MODULE, ETX_MOD_TYPE_TIMER,
ETX_MOD_PORT_TIMER, ETX_Pol_Normal,
ETX_MOD_DIR_RX);
return port != nullptr;
}

if (mode == TRAINER_MODE_MASTER_SBUS_EXTERNAL_MODULE) {
auto port = modulePortFind(EXTERNAL_MODULE, ETX_MOD_TYPE_SERIAL,
ETX_MOD_PORT_UART, ETX_Pol_Normal,
ETX_MOD_DIR_RX);
return port != nullptr;
}
}

#if !defined(MULTIMODULE) || !defined(HARDWARE_INTERNAL_MODULE) || !defined(HARDWARE_EXTERNAL_MODULE)
if (mode == TRAINER_MODE_MULTI)
return false;
#if !defined(MULTIMODULE) || !defined(HARDWARE_INTERNAL_MODULE) || \
!defined(HARDWARE_EXTERNAL_MODULE)
if (mode == TRAINER_MODE_MULTI) return false;
#else
if (mode == TRAINER_MODE_MULTI &&
((!IS_INTERNAL_MODULE_ENABLED() && !IS_EXTERNAL_MODULE_ENABLED()) ||
Expand Down
19 changes: 13 additions & 6 deletions radio/src/hal/module_port.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -106,8 +106,12 @@ static bool _init_timer_driver(etx_module_driver_t* d, const etx_module_port_t*
}

static bool _match_port(const etx_module_port_t* p, uint8_t type, uint8_t port,
uint8_t polarity, bool softserial_fallback)
uint8_t polarity, uint8_t direction, bool softserial_fallback)
{
if ((p->dir_flags & direction) != direction) {
return false;
}

if (p->type == type && p->port == port) {
// either polarity matches or can be set
if (polarity != ETX_Pol_Inverted ||
Expand All @@ -134,6 +138,7 @@ static bool _match_port(const etx_module_port_t* p, uint8_t type, uint8_t port,

static const etx_module_port_t* _find_port(uint8_t module, uint8_t type,
uint8_t port, uint8_t polarity,
uint8_t direction,
bool softserial_fallback)
{
if (module >= MAX_MODULES || module >= _n_modules || !_modules[module])
Expand All @@ -145,7 +150,7 @@ static const etx_module_port_t* _find_port(uint8_t module, uint8_t type,

while(n_ports > 0) {

if (_match_port(p, type, port, polarity, softserial_fallback)) {
if (_match_port(p, type, port, polarity, direction, softserial_fallback)) {
found_port = p;
break;
}
Expand Down Expand Up @@ -173,9 +178,10 @@ const etx_module_t* modulePortGetModuleDescription(uint8_t module)
}

const etx_module_port_t* modulePortFind(uint8_t module, uint8_t type,
uint8_t port, uint8_t polarity)
uint8_t port, uint8_t polarity,
uint8_t direction)
{
return _find_port(module, type, port, polarity, false);
return _find_port(module, type, port, polarity, direction, false);
}

void modulePortSetPower(uint8_t module, uint8_t enable)
Expand Down Expand Up @@ -204,6 +210,7 @@ etx_module_state_t* modulePortInitSerial(uint8_t module, uint8_t port,
{
const etx_module_port_t* found_port = _find_port(module, ETX_MOD_TYPE_SERIAL,
port, params->polarity,
params->direction,
softserial_fallback);
if (!found_port) return nullptr;

Expand Down Expand Up @@ -236,8 +243,8 @@ etx_module_state_t* modulePortInitSerial(uint8_t module, uint8_t port,
etx_module_state_t* modulePortInitTimer(uint8_t module, uint8_t port,
const etx_timer_config_t* cfg)
{
const etx_module_port_t* found_port = _find_port(module, ETX_MOD_TYPE_TIMER,
port, ETX_Pol_Normal, false);
const etx_module_port_t* found_port = _find_port(
module, ETX_MOD_TYPE_TIMER, port, ETX_Pol_Normal, ETX_MOD_DIR_TX, false);
if (!found_port) return nullptr;

auto state = &(_module_states[module]);
Expand Down
7 changes: 4 additions & 3 deletions radio/src/hal/module_port.h
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,8 @@
//

// flags
#define ETX_MOD_DIR_TX (1 << 0)
#define ETX_MOD_DIR_RX (1 << 1)
#define ETX_MOD_DIR_RX (1 << 0)
#define ETX_MOD_DIR_TX (1 << 1)
#define ETX_MOD_DIR_TX_RX (ETX_MOD_DIR_TX | ETX_MOD_DIR_RX)
#define ETX_MOD_FULL_DUPLEX (1 << 2)

Expand Down Expand Up @@ -118,7 +118,8 @@ void modulePortConfigExtra(const etx_module_port_t* port);
const etx_module_t* modulePortGetModuleDescription(uint8_t module);

const etx_module_port_t* modulePortFind(uint8_t module, uint8_t type,
uint8_t port, uint8_t polarity);
uint8_t port, uint8_t polarity,
uint8_t direction);

void modulePortSetPower(uint8_t module, uint8_t enabled);

Expand Down
6 changes: 0 additions & 6 deletions radio/src/hal/trainer_driver.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,3 @@ inline bool is_trainer_connected()
void init_trainer_module_cppm();
void stop_trainer_module_cppm();
#endif

#if defined(TRAINER_MODULE_SBUS)
void init_trainer_module_sbus();
void stop_trainer_module_sbus();
int trainerModuleSbusGetByte(uint8_t* byte);
#endif
94 changes: 4 additions & 90 deletions radio/src/targets/common/arm/stm32/trainer_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,11 @@
*/

#include "stm32_pulse_driver.h"
#include "stm32_serial_driver.h"

#include "hal/trainer_driver.h"
#include "hal/module_port.h"

#include "hal.h"

#include "opentx.h"
Expand Down Expand Up @@ -346,93 +350,3 @@ extern "C" void TRAINER_MODULE_CPPM_TIMER_IRQHandler()
#endif

#endif // TRAINER_MODULE_CPPM

#if defined(TRAINER_MODULE_SBUS)

const etx_serial_init sbusTrainerParams = {
.baudrate = SBUS_BAUDRATE,
.encoding = ETX_Encoding_8E2,
.direction = ETX_Dir_RX,
.polarity = ETX_Pol_Normal,
};

// external module may have a full-duplex USART
#if defined(EXTMODULE_USART) || defined(CONFIGURABLE_MODULE_PORT)

#include "hal/module_port.h"

static etx_module_state_t* sbus_trainer_mod_st = nullptr;

void init_trainer_module_sbus()
{
if (sbus_trainer_mod_st) return;
modulePortSetPower(EXTERNAL_MODULE,true);

sbus_trainer_mod_st = modulePortInitSerial(EXTERNAL_MODULE, ETX_MOD_PORT_UART,
&sbusTrainerParams, false);
}

void stop_trainer_module_sbus()
{
if (!sbus_trainer_mod_st) return;
modulePortDeInit(sbus_trainer_mod_st);
modulePortSetPower(EXTERNAL_MODULE,false);
sbus_trainer_mod_st = nullptr;
}

int trainerModuleSbusGetByte(uint8_t* data)
{
if (!sbus_trainer_mod_st) return 0;

auto serial_driver = modulePortGetSerialDrv(sbus_trainer_mod_st->rx);
auto ctx = modulePortGetCtx(sbus_trainer_mod_st->rx);

if (ctx) {
return serial_driver->getByte(ctx, data);
}

return 0;
}

#elif defined(TRAINER_MODULE_SBUS_USART)
#include "stm32_serial_driver.h"

static const stm32_usart_t sbus_trainer_USART = {
.USARTx = TRAINER_MODULE_SBUS_USART,
.GPIOx = TRAINER_MODULE_SBUS_GPIO,
.GPIO_Pin = TRAINER_MODULE_SBUS_GPIO_PIN,
.IRQn = (IRQn_Type)-1,
.IRQ_Prio = 0,
.txDMA = nullptr,
.txDMA_Stream = 0,
.txDMA_Channel = 0,
.rxDMA = TRAINER_MODULE_SBUS_DMA,
.rxDMA_Stream = TRAINER_MODULE_SBUS_DMA_STREAM_LL,
.rxDMA_Channel = TRAINER_MODULE_SBUS_DMA_CHANNEL,
};

DEFINE_STM32_SERIAL_PORT(SbusTrainer, sbus_trainer_USART, 32, 0);

static void* _sbus_trainer_ctx = nullptr;

void init_trainer_module_sbus()
{
_sbus_trainer_ctx = STM32SerialDriver.init(REF_STM32_SERIAL_PORT(SbusTrainer),
&sbusTrainerParams);
}

void stop_trainer_module_sbus()
{
STM32SerialDriver.deinit(_sbus_trainer_ctx);
_sbus_trainer_ctx = nullptr;
}

int trainerModuleSbusGetByte(uint8_t* data)
{
return STM32SerialDriver.getByte(_sbus_trainer_ctx, data);
}

#else
#error "No available SBUS trainer implementation"
#endif
#endif // TRAINER_MODULE_SBUS
4 changes: 4 additions & 0 deletions radio/src/targets/horus/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -1007,6 +1007,10 @@
#define TRAINER_MODULE_CPPM_TIMER_IRQHandler TIM4_IRQHandler
#define TRAINER_MODULE_CPPM_GPIO_AF LL_GPIO_AF_2

#if defined(EXTMODULE_USART) || defined(CONFIGURABLE_MODULE_PORT)
#define TRAINER_MODULE_SBUS
#endif

// Millisecond timer
#define MS_TIMER TIM14
#define MS_TIMER_IRQn TIM8_TRG_COM_TIM14_IRQn
Expand Down
1 change: 1 addition & 0 deletions radio/src/targets/nv14/hal.h
Original file line number Diff line number Diff line change
Expand Up @@ -531,6 +531,7 @@
#define TRAINER_GPIO_AF LL_GPIO_AF_2
#define TRAINER_TIMER_FREQ (PERI1_FREQUENCY * TIMER_MULT_APB1)

#define TRAINER_MODULE_SBUS

//BLUETOOTH
#define BLUETOOTH_ON_RCC_AHB1Periph RCC_AHB1Periph_GPIOI
Expand Down
Loading

0 comments on commit 649be2e

Please sign in to comment.