WiP: refactor serial ports

This commit is contained in:
Raphael Coeffic
2022-03-20 16:22:12 +01:00
parent 87a3d2f9e7
commit f733810c79
27 changed files with 601 additions and 199 deletions
+10
View File
@@ -21,6 +21,16 @@
#include "afhds2.h"
etx_serial_init afhds2SerialInitParams = {
.baudrate = INTMODULE_USART_AFHDS2_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
afhds2::afhds2() {
// TODO Auto-generated constructor stub
+2
View File
@@ -56,4 +56,6 @@ public:
virtual ~afhds2();
};
extern etx_serial_init afhds2SerialInitParams;
#endif /* PULSES_AFHDS2_H_ */
+116 -5
View File
@@ -20,6 +20,11 @@
*/
#include "opentx.h"
#include "mixer_scheduler.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
#endif
#define CROSSFIRE_CH_BITS 11
#define CROSSFIRE_CENTER 0x3E0
@@ -71,7 +76,8 @@ uint8_t createCrossfireChannelsFrame(uint8_t * frame, int16_t * pulses)
}
static void setupPulsesCrossfire(uint8_t idx, CrossfirePulsesData* p_data,
uint8_t endpoint)
uint8_t endpoint, int16_t* channels,
uint8_t nChannels)
{
#if defined(LUA)
if (outputTelemetryBuffer.destination == endpoint) {
@@ -88,18 +94,123 @@ static void setupPulsesCrossfire(uint8_t idx, CrossfirePulsesData* p_data,
} else {
p_data->length = createCrossfireChannelsFrame(
p_data->pulses,
&channelOutputs[g_model.moduleData[idx].channelsStart]);
channels /*TODO: nChannels*/);
}
}
}
void setupPulsesCrossfire(uint8_t idx)
static void setupPulsesCrossfire(uint8_t idx, int16_t* channels, uint8_t nChannels)
{
if (idx == INTERNAL_MODULE) {
auto* p_data = &intmodulePulsesData.crossfire;
setupPulsesCrossfire(idx, p_data, 0);
setupPulsesCrossfire(idx, p_data, 0, channels, nChannels);
} else if (telemetryProtocol == PROTOCOL_TELEMETRY_CROSSFIRE) {
auto* p_data = &extmodulePulsesData.crossfire;
setupPulsesCrossfire(idx, p_data, TELEMETRY_ENDPOINT_SPORT);
setupPulsesCrossfire(idx, p_data, TELEMETRY_ENDPOINT_SPORT, channels, nChannels);
}
}
#if defined(INTERNAL_MODULE_CRSF)
static void intmoduleCRSF_rx(uint8_t data)
{
intmoduleFifo.push(data);
#if !defined(SIMU)
// wakeup mixer when rx buffer is quarter full (16 bytes)
if (intmoduleFifo.size() >= 16) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(mixerTaskId.rtos_handle, 0, eNoAction,
&xHigherPriorityTaskWoken);
// might effect a context switch on ISR exit
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
#endif
}
#endif
static const etx_serial_init intmoduleCrossfireInitParams = {
.baudrate = 0,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleCRSF_rx,
.on_error = nullptr,
};
static void* crossfireInit(uint8_t module)
{
if (module == INTERNAL_MODULE) {
#if defined(INTERNAL_MODULE_CRSF)
// serial port setup
etx_serial_init params(intmoduleCrossfireInitParams);
params.baudrate = CROSSFIRE_BAUDRATE;
// wakeup mixer when rx buffer is quarter full (16 bytes)
params.on_receive = intmoduleCRSF_rx;
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&params);
#endif
} else {
// TODO: setup S.PORT
// - for now, the init is still done via telemetryWakeup()....
}
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, CROSSFIRE_PERIOD);
return (void*)(uint32_t)module;
}
static void crossfireDeInit(void* context)
{
uint32_t module = (uint32_t)context;
if (module == INTERNAL_MODULE) {
#if defined(INTERNAL_MODULE_CRSF)
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
intmoduleFifo.clear();
#endif
} else {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
}
}
static void crossfireSetupPulses(void* context, int16_t* channels, uint8_t nChannels)
{
uint32_t module = (uint32_t)context;
ModuleSyncStatus& status = getModuleSyncStatus(module);
if (status.isValid()) {
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
} else {
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
}
setupPulsesCrossfire(module, channels, nChannels);
}
static void crossfireSendPulses(void* context)
{
uint32_t module = (uint32_t)context;
if (module == INTERNAL_MODULE) {
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.crossfire.pulses,
intmodulePulsesData.crossfire.length);
} else {
sportSendBuffer(extmodulePulsesData.crossfire.pulses,
extmodulePulsesData.crossfire.length);
}
}
#include "hal/module_driver.h"
etx_hal_module_driver_t CrossfireModuleDriver = {
.protocol = PROTOCOL_CHANNELS_CROSSFIRE,
.init = crossfireInit,
.deinit = crossfireDeInit,
.setupPulses = crossfireSetupPulses,
.sendPulses = crossfireSendPulses
};
+26
View File
@@ -0,0 +1,26 @@
/*
* Copyright (C) EdgeTx
*
* Based on code named
* opentx - https://github.com/opentx/opentx
* th9x - http://code.google.com/p/th9x
* er9x - http://code.google.com/p/er9x
* gruvin9x - http://code.google.com/p/gruvin9x
*
* License GPLv2: http://www.gnu.org/licenses/gpl-2.0.html
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License version 2 as
* published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*/
#pragma once
#include "hal/module_driver.h"
extern etx_hal_module_driver_t CrossfireModuleDriver;
+14
View File
@@ -50,6 +50,20 @@ static void sendConfig(uint8_t moduleIdx);
static void sendDSM(uint8_t moduleIdx);
#endif
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
etx_serial_init multiSerialInitParams = {
.baudrate = MULTIMODULE_BAUDRATE,
.parity = ETX_Parity_Even,
.stop_bits = ETX_StopBits_Two,
.word_length = ETX_WordLength_9,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
#endif
void multiPatchCustom(uint8_t moduleIdx)
{
if (g_model.moduleData[moduleIdx].multi.customProto) {
+3
View File
@@ -23,6 +23,7 @@
#define _MULTI_PULSES_H_
#include "pulses_common.h"
#include "hal/serial_driver.h"
void multiPatchCustom(uint8_t moduleIdx);
void convertEtxProtocolToMulti(int *protocol, int *subprotocol);
@@ -43,4 +44,6 @@ class UartMultiPulses: public DataBuffer<uint8_t, 64>
}
};
extern etx_serial_init multiSerialInitParams;
#endif
+40 -82
View File
@@ -33,6 +33,10 @@
#include "intmodule_pulses_driver.h"
#endif
#if defined(CROSSFIRE)
#include "pulses/crossfire.h"
#endif
uint8_t s_pulses_paused = 0;
ModuleState moduleState[NUM_MODULES];
InternalModulePulsesData intmodulePulsesData __DMA;
@@ -304,7 +308,8 @@ void enablePulsesExternalModule(uint8_t protocol)
#if defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
EXTERNAL_MODULE_ON();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, CROSSFIRE_PERIOD);
if (CrossfireModuleDriver.init)
CrossfireModuleDriver.init(EXTERNAL_MODULE);
break;
#endif
@@ -409,15 +414,14 @@ bool setupPulsesExternalModule(uint8_t protocol)
#if defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
{
ModuleSyncStatus& status = getModuleSyncStatus(EXTERNAL_MODULE);
if (status.isValid())
mixerSchedulerSetPeriod(EXTERNAL_MODULE, status.getAdjustedRefreshRate());
else
mixerSchedulerSetPeriod(EXTERNAL_MODULE, CROSSFIRE_PERIOD);
setupPulsesCrossfire(EXTERNAL_MODULE);
if (CrossfireModuleDriver.setupPulses) {
int16_t* channels =
&channelOutputs[g_model.moduleData[EXTERNAL_MODULE].channelsStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
CrossfireModuleDriver.setupPulses((void*)EXTERNAL_MODULE, channels,
nChannels);
}
return true;
}
#endif
#if defined(GHOST)
@@ -466,25 +470,6 @@ bool setupPulsesExternalModule(uint8_t protocol)
#if defined(HARDWARE_INTERNAL_MODULE)
#if defined(INTERNAL_MODULE_CRSF)
static void intmoduleCRSF_rx(uint8_t data)
{
intmoduleFifo.push(data);
#if !defined(SIMU)
// wakeup mixer when rx buffer is quarter full (16 bytes)
if (intmoduleFifo.size() >= 16) {
BaseType_t xHigherPriorityTaskWoken = pdFALSE;
xTaskNotifyFromISR(mixerTaskId.rtos_handle, 0, eNoAction,
&xHigherPriorityTaskWoken);
// might effect a context switch on ISR exit
portYIELD_FROM_ISR(xHigherPriorityTaskWoken);
}
#endif
}
#endif
static void enablePulsesInternalModule(uint8_t protocol)
{
// start new protocol hardware here
@@ -512,12 +497,8 @@ static void enablePulsesInternalModule(uint8_t protocol)
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED: {
etx_serial_init params;
params.baudrate = PXX2_HIGHSPEED_BAUDRATE;
params.rx_enable = true;
intmoduleFifo.clear();
intmoduleSerialStart(&params);
IntmoduleSerialDriver.init(&pxx2SerialInitParams)
resetAccessAuthenticationCount();
#if defined(INTMODULE_HEARTBEAT)
@@ -529,19 +510,11 @@ static void enablePulsesInternalModule(uint8_t protocol)
#endif
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE: {
case PROTOCOL_CHANNELS_MULTIMODULE:
// serial port setup
etx_serial_init params;
params.baudrate = MULTIMODULE_BAUDRATE;
params.rx_enable = true;
params.parity = USART_Parity_Even;
params.stop_bits = USART_StopBits_2;
params.word_length = USART_WordLength_9b;
intmodulePulsesData.multi.initFrame();
intmoduleFifo.clear();
intmoduleSerialStart(&params);
IntmoduleSerialDriver.init(&multiSerialInitParams);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
@@ -555,26 +528,16 @@ static void enablePulsesInternalModule(uint8_t protocol)
MultiRfProtocols::instance(INTERNAL_MODULE)->triggerScan();
TRACE("counter = %d", moduleState[INTERNAL_MODULE].counter);
#endif
} break;
break;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE: {
// serial port setup
etx_serial_init params;
params.baudrate = CROSSFIRE_BAUDRATE;
params.rx_enable = true;
// wakeup mixer when rx buffer is quarter full (16 bytes)
params.on_receive = intmoduleCRSF_rx;
intmoduleFifo.clear();
intmoduleSerialStart(&params);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, CROSSFIRE_PERIOD);
} break;
case PROTOCOL_CHANNELS_CROSSFIRE:
if (CrossfireModuleDriver.init) {
// TODO: save context
CrossfireModuleDriver.init(INTERNAL_MODULE);
}
break;
#endif
#if defined(INTERNAL_MODULE_PPM)
@@ -585,20 +548,15 @@ static void enablePulsesInternalModule(uint8_t protocol)
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A: {
case PROTOCOL_CHANNELS_AFHDS2A:
// serial port setup
etx_serial_init params;
params.baudrate = INTMODULE_USART_AFHDS2_BAUDRATE;
params.rx_enable = true;
resetPulsesAFHDS2();
intmoduleFifo.clear();
intmoduleSerialStart(&params);
IntmoduleSerialDriver.init(&afhds2SerialInitParams)
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, AFHDS2_PERIOD);
} break;
break;
#endif
default:
@@ -645,15 +603,14 @@ bool setupPulsesInternalModule(uint8_t protocol)
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
{
ModuleSyncStatus& status = getModuleSyncStatus(INTERNAL_MODULE);
if (status.isValid())
mixerSchedulerSetPeriod(INTERNAL_MODULE, status.getAdjustedRefreshRate());
else
mixerSchedulerSetPeriod(INTERNAL_MODULE, CROSSFIRE_PERIOD);
setupPulsesCrossfire(INTERNAL_MODULE);
if (CrossfireModuleDriver.setupPulses) {
int16_t* channels =
&channelOutputs[g_model.moduleData[INTERNAL_MODULE].channelsStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
CrossfireModuleDriver.setupPulses((void*)INTERNAL_MODULE, channels,
nChannels);
}
return true;
}
#endif
#if defined(INTERNAL_MODULE_MULTI)
@@ -711,8 +668,8 @@ void intmoduleSendNextFrame()
#if defined(PXX1)
#if defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
intmoduleSendBuffer(intmodulePulsesData.pxx_uart.getData(),
intmodulePulsesData.pxx_uart.getSize());
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.pxx_uart.getData(),
intmodulePulsesData.pxx_uart.getSize());
break;
#else
case PROTOCOL_CHANNELS_PXX1_PULSES:
@@ -724,15 +681,16 @@ void intmoduleSendNextFrame()
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE:
intmoduleSendBuffer(intmodulePulsesData.multi.getData(),
intmodulePulsesData.multi.getSize());
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.multi.getData(),
intmodulePulsesData.multi.getSize());
break;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
intmoduleSendBuffer(intmodulePulsesData.crossfire.pulses,
intmodulePulsesData.crossfire.length);
if (CrossfireModuleDriver.sendPulses) {
CrossfireModuleDriver.sendPulses((void*)INTERNAL_MODULE);
}
break;
#endif
+1 -19
View File
@@ -32,6 +32,7 @@
#include "afhds2.h"
#include "modules_helpers.h"
#include "ff.h"
#include "hal/module_driver.h"
#if defined(DSM2)
@@ -276,25 +277,6 @@ inline void startPulses()
#endif
}
enum ChannelsProtocols {
PROTOCOL_CHANNELS_UNINITIALIZED,
PROTOCOL_CHANNELS_NONE,
PROTOCOL_CHANNELS_PPM,
PROTOCOL_CHANNELS_PXX1_PULSES,
PROTOCOL_CHANNELS_PXX1_SERIAL,
PROTOCOL_CHANNELS_DSM2_LP45,
PROTOCOL_CHANNELS_DSM2_DSM2,
PROTOCOL_CHANNELS_DSM2_DSMX,
PROTOCOL_CHANNELS_CROSSFIRE,
PROTOCOL_CHANNELS_MULTIMODULE,
PROTOCOL_CHANNELS_SBUS,
PROTOCOL_CHANNELS_PXX2_LOWSPEED,
PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
PROTOCOL_CHANNELS_AFHDS2A,
PROTOCOL_CHANNELS_AFHDS3,
PROTOCOL_CHANNELS_GHOST
};
inline void stopPulses()
{
s_pulses_paused = true;
+14
View File
@@ -25,6 +25,20 @@
#include "io/frsky_firmware_update.h"
#include "libopenui/src/libopenui_file.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
const etx_serial_init pxx2SerialInitParams = {
.baudrate = PXX2_HIGHSPEED_BAUDRATE,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = intmoduleFifoReceive,
.on_error = intmoduleFifoError,
};
#endif
bool isPXX2PowerAvailable(const PXX2HardwareInformation& info, int value)
{
uint8_t modelId = info.modelID;
+3
View File
@@ -26,6 +26,7 @@
#include "io/frsky_pxx2.h"
#include "popups.h"
#include "./pxx.h"
#include "hal/serial_driver.h"
#define PXX2_TYPE_C_MODULE 0x01
#define PXX2_TYPE_ID_REGISTER 0x01
@@ -575,4 +576,6 @@ class Pxx2OtaUpdate {
const char * nextStep(uint8_t step, const char * rxName, uint32_t address, const uint8_t * buffer);
};
extern const etx_serial_init pxx2SerialInitParams;
#endif