Skip to content

Commit

Permalink
feat(ACCESS): configure AUX serial port as "External module" (#2562)
Browse files Browse the repository at this point in the history
This allows configuring the external access mod on runtime rather than by compilation option.
  • Loading branch information
raphaelcoeffic committed Oct 15, 2022
1 parent 29db5d6 commit f7d25b4
Show file tree
Hide file tree
Showing 17 changed files with 296 additions and 104 deletions.
1 change: 1 addition & 0 deletions radio/src/dataconstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,7 @@ enum UartModes {
UART_MODE_GPS,
UART_MODE_DEBUG,
UART_MODE_SPACEMOUSE,
UART_MODE_EXT_MODULE,
UART_MODE_COUNT SKIP,
UART_MODE_MAX SKIP = UART_MODE_COUNT-1
};
Expand Down
34 changes: 31 additions & 3 deletions radio/src/gui/gui_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@

#include "opentx.h"

#if defined(PXX2)
#include "extmodule_serial_driver.h"
#endif

#if defined(PCBFRSKY) || defined(PCBFLYSKY)
uint8_t switchToMix(uint8_t source)
{
Expand Down Expand Up @@ -431,6 +435,15 @@ bool isSerialModeAvailable(uint8_t port_nr, int mode)
return false;
#endif

#if !defined(AUX_SERIAL_DMA_TX) || defined(EXTMODULE_USART)
if (mode == UART_MODE_EXT_MODULE)
return false;
#else // defined(AUX_SERIAL_DMA_TX) && !defined(EXTMODULE_USART)
// UART_MODE_EXT_MODULE is only supported on AUX1, as AUX2 has no TX DMA
if (mode == UART_MODE_EXT_MODULE && port_nr != SP_AUX1)
return false;
#endif

#if !defined(LUA)
if (mode == UART_MODE_LUA)
return false;
Expand Down Expand Up @@ -803,6 +816,7 @@ bool isInternalModuleAvailable(int moduleType)
}
#endif

#if defined(HARDWARE_EXTERNAL_MODULE)
bool isExternalModuleAvailable(int moduleType)
{

Expand Down Expand Up @@ -835,11 +849,17 @@ bool isExternalModuleAvailable(int moduleType)
return false; // doesn't exist for now


#if !defined(PXX2) || !defined(EXTMODULE_USART)
if (moduleType == MODULE_TYPE_XJT_LITE_PXX2 || moduleType == MODULE_TYPE_R9M_PXX2 || moduleType == MODULE_TYPE_R9M_LITE_PXX2 || moduleType == MODULE_TYPE_R9M_LITE_PRO_PXX2) {
if (moduleType == MODULE_TYPE_XJT_LITE_PXX2 ||
moduleType == MODULE_TYPE_R9M_PXX2 ||
moduleType == MODULE_TYPE_R9M_LITE_PXX2 ||
moduleType == MODULE_TYPE_R9M_LITE_PRO_PXX2) {

#if defined(PXX2)
return extmoduleGetSerialPort() != nullptr;
#else
return false;
}
#endif
}

#if !defined(CROSSFIRE)
if (moduleType == MODULE_TYPE_CROSSFIRE)
Expand Down Expand Up @@ -887,6 +907,14 @@ bool isExternalModuleAvailable(int moduleType)
return true;
}

#else // !defined(HARDWARE_EXTERNAL_MODULE)

bool isExternalModuleAvailable(int moduleType)
{
return false;
}
#endif

bool isRfProtocolAvailable(int protocol)
{
#if defined(CROSSFIRE)
Expand Down
4 changes: 3 additions & 1 deletion radio/src/pulses/afhds3.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -864,7 +864,9 @@ void applyModelConfig(uint8_t module)
static void* initExternal(uint8_t module)
{
#if defined(AFHDS3_EXT_UART)
const etx_serial_driver_t* drv = &ExtmoduleSerialDriver;
auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;

uint16_t period = AFHDS3_UART_COMMAND_TIMEOUT * 1000 /* us */;
#else
const etx_serial_driver_t* drv = nullptr;
Expand Down
4 changes: 3 additions & 1 deletion radio/src/pulses/pulses.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -207,9 +207,11 @@ uint8_t getModuleType(uint8_t module)
}
#endif

#if defined(HARDWARE_EXTERNAL_MODULE)
if (module == EXTERNAL_MODULE && isExternalModuleAvailable(type)) {
return type;
}
#endif

return MODULE_TYPE_NONE;
}
Expand Down Expand Up @@ -579,7 +581,7 @@ void enablePulsesExternalModule(uint8_t protocol)
break;
#endif

#if defined(PXX2) && defined(EXTMODULE_USART)
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
externalModuleContext = Pxx2ExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx2ExternalDriver;
Expand Down
16 changes: 12 additions & 4 deletions radio/src/pulses/pxx1.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -295,7 +295,10 @@ static const etx_serial_init pxx1ExtSerialInit = {

static void* pxx1InitExternal(uint8_t module)
{
void* uart_ctx = ExtmoduleSerialDriver.init(&pxx1ExtSerialInit);
auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;

void* uart_ctx = drv->init(&pxx1ExtSerialInit);

mixerSchedulerSetPeriod(EXTERNAL_MODULE, EXTMODULE_PXX1_SERIAL_PERIOD);
EXTERNAL_MODULE_ON();
Expand All @@ -307,7 +310,9 @@ static void pxx1DeInitExternal(void* context)
{
EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
ExtmoduleSerialDriver.deinit(context);

auto drv = extmoduleGetSerialPort();
if (drv) drv->deinit(context);
}

static void pxx1SetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
Expand All @@ -323,8 +328,11 @@ static void pxx1SetupPulsesExternal(void* context, int16_t* channels, uint8_t nC

static void pxx1SendPulsesExternal(void* context)
{
ExtmoduleSerialDriver.sendBuffer(context, extmodulePulsesData.pxx_uart.getData(),
extmodulePulsesData.pxx_uart.getSize());
auto drv = extmoduleGetSerialPort();
if (!drv) return;

drv->sendBuffer(context, extmodulePulsesData.pxx_uart.getData(),
extmodulePulsesData.pxx_uart.getSize());
}

const etx_module_driver_t Pxx1ExternalSerialDriver = {
Expand Down
16 changes: 8 additions & 8 deletions radio/src/pulses/pxx2.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -812,6 +812,7 @@ static void pxx2ProcessData(void* context, uint8_t data, uint8_t* buffer, uint8_


#include "hal/module_driver.h"
#include "extmodule_serial_driver.h"

const etx_module_driver_t Pxx2InternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
Expand All @@ -823,9 +824,6 @@ const etx_module_driver_t Pxx2InternalDriver = {
.processData = pxx2ProcessData,
};

#if defined(EXTMODULE_USART)
#include "extmodule_serial_driver.h"

static void* pxx2InitExternal(uint8_t module, uint32_t baudrate)
{
etx_serial_init params(pxx2SerialInitParams);
Expand All @@ -837,9 +835,10 @@ static void* pxx2InitExternal(uint8_t module, uint32_t baudrate)
telemetryProtocol = PROTOCOL_TELEMETRY_FRSKY_SPORT;

auto state = &pxx2State[module];
state->init(module, &extmodulePulsesData.pxx2, &ExtmoduleSerialDriver,
ExtmoduleSerialDriver.init(&params));

auto drv = extmoduleGetSerialPort();
if (!drv) return nullptr;

state->init(module, &extmodulePulsesData.pxx2, drv, drv->init(&params));
return state;
}

Expand All @@ -855,9 +854,11 @@ static void* pxx2InitExtHighSpeed(uint8_t module)

static void pxx2DeInitExternal(void* context)
{
auto state = (PXX2State*)context;
if (state) state->deinit();

EXTERNAL_MODULE_OFF();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
ExtmoduleSerialDriver.deinit(context);
telemetryProtocol = 0xFF;
}

Expand Down Expand Up @@ -897,4 +898,3 @@ const etx_module_driver_t Pxx2LowSpeedExternalDriver = {
.getByte = pxx2GetByte,
.processData = pxx2ProcessData,
};
#endif
29 changes: 24 additions & 5 deletions radio/src/serial.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,10 @@
#include "telemetry/crossfire.h"
#endif

#if defined(AUX_SERIAL_DMA_TX) && !defined(EXTMODULE_USART)
#include "extmodule_serial_driver.h"
#endif

#define PRINTF_BUFFER_SIZE 128

static void (*dbg_serial_putc)(void*, uint8_t) = nullptr;
Expand Down Expand Up @@ -212,11 +216,19 @@ static void serialSetCallBacks(int mode, void* ctx, const etx_serial_port_t* por
gpsSetSerialDriver(ctx, drv);
break;
#endif

#if defined(SPACEMOUSE)
case UART_MODE_SPACEMOUSE:
spacemouseSetSerialDriver(ctx, drv);
break;
#endif

#if defined(AUX_SERIAL_DMA_TX) && !defined(EXTMODULE_USART)
case UART_MODE_EXT_MODULE:
extmoduleSetSerialPort(drv);
break;
#endif

#endif
}
}
Expand Down Expand Up @@ -279,6 +291,12 @@ static void serialSetupPort(int mode, etx_serial_init& params)
params.rx_enable = true;
break;
#endif

#if defined(AUX_SERIAL_DMA_TX) && !defined(EXTMODULE_USART)
case UART_MODE_EXT_MODULE:
params.rx_enable = true;
break;
#endif
#endif
}
}
Expand Down Expand Up @@ -330,10 +348,10 @@ void serialInit(uint8_t port_nr, int mode)

if (state->port) {
auto drv = state->port->uart;
if (drv && drv->deinit) {
if (drv && drv->deinit && state->usart_ctx) {
drv->deinit(state->usart_ctx);
}
if (state->mode != 0) {
if (state->mode != UART_MODE_NONE) {
// Clear callbacks
serialSetCallBacks(state->mode, nullptr, nullptr);
}
Expand All @@ -356,11 +374,12 @@ void serialInit(uint8_t port_nr, int mode)
serialSetPowerState(port_nr);
#endif

if (params.baudrate != 0) {
state->mode = mode;
state->mode = mode;

if (mode != UART_MODE_NONE) {
state->port = port;

if (port) {
if (port && params.baudrate != 0) {
if (port->uart && port->uart->init)
state->usart_ctx = port->uart->init(&params);
}
Expand Down
9 changes: 0 additions & 9 deletions radio/src/storage/storage_common.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -114,15 +114,6 @@ void postModelLoad(bool alarms)
}
#endif

#if defined(HARDWARE_INTERNAL_MODULE)
if (!isInternalModuleAvailable(g_model.moduleData[INTERNAL_MODULE].type)) {
memclear(&g_model.moduleData[INTERNAL_MODULE], sizeof(ModuleData));
}
#endif

if (!isExternalModuleAvailable(g_model.moduleData[EXTERNAL_MODULE].type)) {
memclear(&g_model.moduleData[EXTERNAL_MODULE], sizeof(ModuleData));
}
#if defined(MULTIMODULE) && defined(MULTI_PROTOLIST)
MultiRfProtocols::removeInstance(EXTERNAL_MODULE);
#endif
Expand Down
1 change: 1 addition & 0 deletions radio/src/storage/yaml/yaml_datastructs_funcs.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1881,6 +1881,7 @@ static const struct YamlIdStr enum_UartModes[] = {
{ UART_MODE_GPS, "GPS" },
{ UART_MODE_DEBUG, "DEBUG" },
{ UART_MODE_SPACEMOUSE, "SPACEMOUSE" },
{ UART_MODE_EXT_MODULE, "EXT_MODULE" },
{ 0, NULL }
};

Expand Down
31 changes: 23 additions & 8 deletions radio/src/targets/common/arm/stm32/aux_serial_driver.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@
#define AUX_SERIAL_TX_BUFFER 512

#if defined(SDRAM)
#define AUX_SERIAL_RX_BUFFER 128
#define AUX_SERIAL_RX_BUFFER 512
#else
#define AUX_SERIAL_RX_BUFFER 32
#endif
Expand All @@ -53,26 +53,34 @@ static void* aux_serial_init(const SerialState* st, const etx_serial_init* param
stm32_usart_init(st->usart, params);

if (params->rx_enable && st->usart->rxDMA) {
st->rxFifo->clear();
stm32_usart_init_rx_dma(st->usart, st->rxFifo->buffer(), st->rxFifo->size());
st->rxFifo->clear();
}

return (void*)st;
}

static void aux_serial_putc(void* ctx, uint8_t c)
{
// When sending single bytes,
// send is based on IRQ, so that this
// only enables the corresponding IRQ

auto st = (const SerialState*)ctx;
if (st->txFifo->isFull()) return;
st->txFifo->push(c);

// Send is based on IRQ, so that this
// only enables the corresponding IRQ
stm32_usart_send_buffer(st->usart, &c, 1);
stm32_usart_enable_tx_irq(st->usart);
}

static void aux_serial_send_buffer(void* ctx, const uint8_t* data, uint8_t size)
{
auto st = (const SerialState*)ctx;
if (st->usart->txDMA) {
stm32_usart_send_buffer(st->usart, data, size);
return;
}

while(size > 0) {
aux_serial_putc(ctx, *data++);
size--;
Expand Down Expand Up @@ -112,15 +120,22 @@ static const LL_GPIO_InitTypeDef auxUSARTPinInit = {
.Alternate = AUX_SERIAL_GPIO_AF_LL,
};

#if !defined(AUX_SERIAL_DMA_TX)
#define AUX_SERIAL_DMA_TX nullptr
#define AUX_SERIAL_DMA_Stream_TX nullptr
#define AUX_SERIAL_DMA_Stream_TX_LL 0
#define AUX_SERIAL_DMA_Channel_TX 0
#endif

static const stm32_usart_t auxUSART = {
.USARTx = AUX_SERIAL_USART,
.GPIOx = AUX_SERIAL_GPIO,
.pinInit = &auxUSARTPinInit,
.IRQn = AUX_SERIAL_USART_IRQn,
.IRQ_Prio = 7, // TODO: define constant
.txDMA = nullptr,
.txDMA_Stream = 0,
.txDMA_Channel = 0,
.txDMA = AUX_SERIAL_DMA_TX,
.txDMA_Stream = AUX_SERIAL_DMA_Stream_TX_LL,
.txDMA_Channel = AUX_SERIAL_DMA_Channel_TX,
.rxDMA = AUX_SERIAL_DMA_RX,
.rxDMA_Stream = AUX_SERIAL_DMA_Stream_RX_LL,
.rxDMA_Channel = AUX_SERIAL_DMA_Channel_RX,
Expand Down
Loading

0 comments on commit f7d25b4

Please sign in to comment.