External serial driver moved to a separate driver

This commit is contained in:
Raphael Coeffic
2022-03-20 16:22:12 +01:00
parent 803b472c85
commit ffefd0f6dd
17 changed files with 885 additions and 618 deletions
+75 -67
View File
@@ -21,6 +21,7 @@
#include "opentx.h"
#include "mixer_scheduler.h"
#include "hal/module_driver.h"
#if defined(INTMODULE_USART)
#include "intmodule_serial_driver.h"
@@ -99,14 +100,13 @@ static void setupPulsesCrossfire(uint8_t idx, CrossfirePulsesData* p_data,
}
}
static void setupPulsesCrossfire(uint8_t idx, int16_t* channels, uint8_t nChannels)
static void crossfireSetupMixerScheduler(uint8_t module)
{
if (idx == INTERNAL_MODULE) {
auto* p_data = &intmodulePulsesData.crossfire;
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, channels, nChannels);
ModuleSyncStatus& status = getModuleSyncStatus(module);
if (status.isValid()) {
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
} else {
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
}
}
@@ -137,83 +137,91 @@ static const etx_serial_init intmoduleCrossfireInitParams = {
.on_receive = intmoduleCRSF_rx,
.on_error = nullptr,
};
#endif
static void* crossfireInit(uint8_t module)
static void* crossfireInitInternal(uint8_t module)
{
if (module == INTERNAL_MODULE) {
#if defined(INTERNAL_MODULE_CRSF)
// serial port setup
etx_serial_init params(intmoduleCrossfireInitParams);
params.baudrate = CROSSFIRE_BAUDRATE;
(void)module;
// wakeup mixer when rx buffer is quarter full (16 bytes)
params.on_receive = intmoduleCRSF_rx;
// serial port setup
etx_serial_init params(intmoduleCrossfireInitParams);
params.baudrate = CROSSFIRE_BAUDRATE;
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&params);
#endif
} else {
// TODO: setup S.PORT
// - for now, the init is still done via telemetryWakeup()....
telemetryInit(PROTOCOL_TELEMETRY_CROSSFIRE);
}
// wakeup mixer when rx buffer is quarter full (16 bytes)
params.on_receive = intmoduleCRSF_rx;
// mixer setup
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
return (void*)(ulong)module;
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&params);
return nullptr;
}
static void crossfireDeInit(void* context)
static void crossfireDeInitInternal(void* context)
{
ulong module = (ulong)context;
(void)context;
if (module == INTERNAL_MODULE) {
#if defined(INTERNAL_MODULE_CRSF)
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
intmoduleFifo.clear();
#endif
} else {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
}
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
intmoduleFifo.clear();
}
static void crossfireSetupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void crossfireSetupPulsesInternal(void* context, int16_t* channels, uint8_t nChannels)
{
ulong module = (ulong)context;
ModuleSyncStatus& status = getModuleSyncStatus(module);
if (status.isValid()) {
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
} else {
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
}
setupPulsesCrossfire(module, channels, nChannels);
auto* p_data = &intmodulePulsesData.crossfire;
crossfireSetupMixerScheduler(INTERNAL_MODULE);
setupPulsesCrossfire(INTERNAL_MODULE, p_data, 0, channels, nChannels);
}
static void crossfireSendPulses(void* context)
static void crossfireSendPulsesInternal(void* context)
{
ulong module = (ulong)context;
(void)context;
if (module == INTERNAL_MODULE) {
#if defined(INTERNAL_MODULE_CRSF)
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.crossfire.pulses,
intmodulePulsesData.crossfire.length);
#endif
} else {
sportSendBuffer(extmodulePulsesData.crossfire.pulses,
extmodulePulsesData.crossfire.length);
}
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.crossfire.pulses,
intmodulePulsesData.crossfire.length);
}
#include "hal/module_driver.h"
etx_module_driver_t CrossfireModuleDriver = {
etx_module_driver_t CrossfireInternalDriver = {
.protocol = PROTOCOL_CHANNELS_CROSSFIRE,
.init = crossfireInit,
.deinit = crossfireDeInit,
.setupPulses = crossfireSetupPulses,
.sendPulses = crossfireSendPulses
.init = crossfireInitInternal,
.deinit = crossfireDeInitInternal,
.setupPulses = crossfireSetupPulsesInternal,
.sendPulses = crossfireSendPulsesInternal,
};
#endif
static void* crossfireInitExternal(uint8_t module)
{
telemetryInit(PROTOCOL_TELEMETRY_CROSSFIRE);
mixerSchedulerSetPeriod(module, CROSSFIRE_PERIOD);
return nullptr;
}
static void crossfireDeInitExternal(void* context)
{
(void)context;
telemetryProtocol = 0xFF;
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
}
static void crossfireSetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
{
auto* p_data = &extmodulePulsesData.crossfire;
crossfireSetupMixerScheduler(EXTERNAL_MODULE);
setupPulsesCrossfire(EXTERNAL_MODULE, p_data, TELEMETRY_ENDPOINT_SPORT,
channels, nChannels);
}
static void crossfireSendPulsesExternal(void* context)
{
(void)context;
sportSendBuffer(extmodulePulsesData.crossfire.pulses,
extmodulePulsesData.crossfire.length);
}
etx_module_driver_t CrossfireExternalDriver = {
.protocol = PROTOCOL_CHANNELS_CROSSFIRE,
.init = crossfireInitExternal,
.deinit = crossfireDeInitExternal,
.setupPulses = crossfireSetupPulsesExternal,
.sendPulses = crossfireSendPulsesExternal
};
+2 -1
View File
@@ -23,4 +23,5 @@
#include "hal/module_driver.h"
extern etx_module_driver_t CrossfireModuleDriver;
extern etx_module_driver_t CrossfireInternalDriver;
extern etx_module_driver_t CrossfireExternalDriver;
+329 -249
View File
@@ -34,6 +34,8 @@
#include "intmodule_pulses_driver.h"
#endif
#include "extmodule_driver.h"
#if defined(CROSSFIRE)
#include "pulses/crossfire.h"
#endif
@@ -277,10 +279,255 @@ uint8_t getRequiredProtocol(uint8_t module)
return protocol;
}
#if defined(HARDWARE_INTERNAL_MODULE)
static void* internalModuleContext = nullptr;
static const etx_module_driver_t* internalModuleDriver = nullptr;
static void enablePulsesInternalModule(uint8_t protocol)
{
// start new protocol hardware here
if (internalModuleDriver) {
internalModuleDriver->deinit(internalModuleContext);
internalModuleContext = nullptr;
internalModuleDriver = nullptr;
} else {
intmoduleStop();
}
switch (protocol) {
#if defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePxx1PulsesStart();
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
break;
#endif
#if defined(PXX1) && defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
intmodulePxx1SerialStart();
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
break;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
internalModuleContext = Pxx2InternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &Pxx2InternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE:
// TODO: use module interface
// serial port setup
intmodulePulsesData.multi.initFrame();
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&multiSerialInitParams);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
// reset status
getMultiModuleStatus(INTERNAL_MODULE).failsafeChecked = false;
getMultiModuleStatus(INTERNAL_MODULE).flags = 0;
#if defined(MULTI_PROTOLIST)
TRACE("enablePulsesInternalModule(): trigger scan");
MultiRfProtocols::instance(INTERNAL_MODULE)->triggerScan();
TRACE("counter = %d", moduleState[INTERNAL_MODULE].counter);
#endif
break;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
internalModuleContext = CrossfireInternalDriver.init(INTERNAL_MODULE);
internalModuleDriver = &CrossfireInternalDriver;
break;
#endif
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmodulePpmStart();
mixerSchedulerSetPeriod(INTERNAL_MODULE, PPM_PERIOD(INTERNAL_MODULE));
break;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
// serial port setup
resetPulsesAFHDS2();
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&afhds2SerialInitParams)
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, AFHDS2_PERIOD);
break;
#endif
default:
// internal module stopped, use default mixer period
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
break;
}
}
bool setupPulsesInternalModule(uint8_t protocol)
{
uint8_t channelStart = g_model.moduleData[INTERNAL_MODULE].channelsStart;
int16_t* channels = &channelOutputs[channelStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
if (internalModuleDriver) {
internalModuleDriver->setupPulses(internalModuleContext,
channels, nChannels);
return true;
}
switch (protocol) {
#if defined(HARDWARE_INTERNAL_MODULE) && defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePulsesData.pxx.setupFrame(INTERNAL_MODULE);
return true;
#endif
#if defined(PXX1) && defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
intmodulePulsesData.pxx_uart.setupFrame(INTERNAL_MODULE);
return true;
#endif
#if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
setupPulsesPPMInternalModule();
return true;
#endif
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE:
setupPulsesMultiInternalModule();
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
return true;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
{
ModuleSyncStatus& status = getModuleSyncStatus(INTERNAL_MODULE);
mixerSchedulerSetPeriod(
INTERNAL_MODULE,
status.isValid() ? status.getAdjustedRefreshRate() : AFHDS2_PERIOD);
status.invalidate();
setupPulsesAFHDS2();
return true;
}
#endif
default:
return false;
}
}
void stopPulsesInternalModule()
{
if (moduleState[INTERNAL_MODULE].protocol != PROTOCOL_CHANNELS_UNINITIALIZED) {
if (internalModuleDriver) {
internalModuleDriver->deinit(internalModuleContext);
internalModuleDriver = nullptr;
internalModuleContext = nullptr;
} else {
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
}
moduleState[INTERNAL_MODULE].protocol = PROTOCOL_CHANNELS_NONE;
}
}
void intmoduleSendNextFrame()
{
if (internalModuleDriver) {
internalModuleDriver->sendPulses(internalModuleContext);
return;
}
switch (moduleState[INTERNAL_MODULE].protocol) {
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmoduleSendNextFramePPM(
intmodulePulsesData.ppm.pulses,
intmodulePulsesData.ppm.ptr - intmodulePulsesData.ppm.pulses);
break;
#endif
#if defined(PXX1)
#if defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.pxx_uart.getData(),
intmodulePulsesData.pxx_uart.getSize());
break;
#else
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmoduleSendNextFramePxx1(intmodulePulsesData.pxx.getData(),
intmodulePulsesData.pxx.getSize());
break;
#endif
#endif
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE:
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.multi.getData(),
intmodulePulsesData.multi.getSize());
break;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A: {
uint8_t* data = (uint8_t*)intmodulePulsesData.flysky.pulses;
uint16_t size = intmodulePulsesData.flysky.ptr - data;
intmoduleSendBuffer(data, size);
} break;
#endif
}
}
bool setupPulsesInternalModule()
{
uint8_t protocol = getRequiredProtocol(INTERNAL_MODULE);
heartbeat |= (HEART_TIMER_PULSES << INTERNAL_MODULE);
if (moduleState[INTERNAL_MODULE].protocol != protocol) {
enablePulsesInternalModule(protocol);
moduleState[INTERNAL_MODULE].protocol = protocol;
return false;
}
else {
return setupPulsesInternalModule(protocol);
}
}
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
static void* externalModuleContext = nullptr;
static const etx_module_driver_t* externalModuleDriver = nullptr;
void enablePulsesExternalModule(uint8_t protocol)
{
// start new protocol hardware here
if (externalModuleDriver) {
externalModuleDriver->deinit(externalModuleContext);
externalModuleDriver = nullptr;
externalModuleContext = nullptr;
} else {
extmoduleStop();
}
switch (protocol) {
#if defined(PXX1)
@@ -301,16 +548,20 @@ void enablePulsesExternalModule(uint8_t protocol)
case PROTOCOL_CHANNELS_DSM2_LP45:
case PROTOCOL_CHANNELS_DSM2_DSM2:
case PROTOCOL_CHANNELS_DSM2_DSMX:
#if defined(PCBSKY9X)
extmoduleSerialStart(DSM2_BAUDRATE, DSM2_PERIOD * 2000, false);
#else
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, DSM2_PERIOD);
#endif
break;
#endif
#if defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
EXTERNAL_MODULE_ON();
if (CrossfireModuleDriver.init)
CrossfireModuleDriver.init(EXTERNAL_MODULE);
externalModuleContext = CrossfireExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &CrossfireExternalDriver;
break;
#endif
@@ -323,18 +574,24 @@ void enablePulsesExternalModule(uint8_t protocol)
#if defined(PXX2) && defined(EXTMODULE_USART)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
Pxx2ModuleDriver.init(EXTERNAL_MODULE);
externalModuleContext = Pxx2ExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx2ExternalDriver;
break;
case PROTOCOL_CHANNELS_PXX2_LOWSPEED:
Pxx2LowSpeedModuleDriver.init(EXTERNAL_MODULE);
externalModuleContext = Pxx2LowSpeedExternalDriver.init(EXTERNAL_MODULE);
externalModuleDriver = &Pxx2LowSpeedExternalDriver;
break;
#endif
#if defined(MULTIMODULE)
case PROTOCOL_CHANNELS_MULTIMODULE:
#if defined(PCBSKY9X)
extmoduleSerialStart(MULTIMODULE_BAUDRATE, MULTIMODULE_PERIOD * 2000, true);
#else
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, MULTIMODULE_PERIOD);
#endif
getMultiModuleStatus(EXTERNAL_MODULE).failsafeChecked = false;
getMultiModuleStatus(EXTERNAL_MODULE).flags = 0;
#if defined(MULTI_PROTOLIST)
@@ -345,8 +602,12 @@ void enablePulsesExternalModule(uint8_t protocol)
#if defined(SBUS)
case PROTOCOL_CHANNELS_SBUS:
#if defined(PCBSKY9X)
extmoduleSerialStart(SBUS_BAUDRATE, SBUS_PERIOD_HALF_US, false);
#else
extmoduleSerialStart();
mixerSchedulerSetPeriod(EXTERNAL_MODULE, SBUS_PERIOD);
#endif
break;
#endif
@@ -374,10 +635,16 @@ void enablePulsesExternalModule(uint8_t protocol)
bool setupPulsesExternalModule(uint8_t protocol)
{
int16_t* channels =
&channelOutputs[g_model.moduleData[EXTERNAL_MODULE].channelsStart];
uint8_t channelStart = g_model.moduleData[EXTERNAL_MODULE].channelsStart;
int16_t* channels = &channelOutputs[channelStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
if (externalModuleDriver) {
externalModuleDriver->setupPulses(externalModuleContext,
channels, nChannels);
return true;
}
switch (protocol) {
#if defined(PXX1)
@@ -392,13 +659,6 @@ bool setupPulsesExternalModule(uint8_t protocol)
return true;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
case PROTOCOL_CHANNELS_PXX2_LOWSPEED:
Pxx2ModuleDriver.setupPulses((void*)EXTERNAL_MODULE, channels, nChannels);
return true;
#endif
#if defined(SBUS)
case PROTOCOL_CHANNELS_SBUS:
setupPulsesSbus();
@@ -415,15 +675,6 @@ bool setupPulsesExternalModule(uint8_t protocol)
return true;
#endif
#if defined(CROSSFIRE)
case PROTOCOL_CHANNELS_CROSSFIRE:
if (CrossfireModuleDriver.setupPulses) {
CrossfireModuleDriver.setupPulses((void*)EXTERNAL_MODULE, channels,
nChannels);
}
return true;
#endif
#if defined(GHOST)
case PROTOCOL_CHANNELS_GHOST:
{
@@ -466,255 +717,85 @@ bool setupPulsesExternalModule(uint8_t protocol)
return false;
}
}
#endif
#if defined(HARDWARE_INTERNAL_MODULE)
static void enablePulsesInternalModule(uint8_t protocol)
void extmoduleSendNextFrame()
{
// start new protocol hardware here
switch (protocol) {
#if defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePxx1PulsesStart();
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
break;
#endif
#if defined(PXX1) && defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
intmodulePxx1SerialStart();
#if defined(INTMODULE_HEARTBEAT)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, INTMODULE_PXX1_SERIAL_PERIOD);
break;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
Pxx2ModuleDriver.init(INTERNAL_MODULE);
break;
#endif
#if defined(INTERNAL_MODULE_MULTI)
case PROTOCOL_CHANNELS_MULTIMODULE:
// serial port setup
intmodulePulsesData.multi.initFrame();
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&multiSerialInitParams);
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
// reset status
getMultiModuleStatus(INTERNAL_MODULE).failsafeChecked = false;
getMultiModuleStatus(INTERNAL_MODULE).flags = 0;
#if defined(MULTI_PROTOLIST)
TRACE("enablePulsesInternalModule(): trigger scan");
MultiRfProtocols::instance(INTERNAL_MODULE)->triggerScan();
TRACE("counter = %d", moduleState[INTERNAL_MODULE].counter);
#endif
break;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
if (CrossfireModuleDriver.init) {
// TODO: save context
CrossfireModuleDriver.init(INTERNAL_MODULE);
}
break;
#endif
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmodulePpmStart();
mixerSchedulerSetPeriod(INTERNAL_MODULE, PPM_PERIOD(INTERNAL_MODULE));
break;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
// serial port setup
resetPulsesAFHDS2();
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&afhds2SerialInitParams)
// mixer setup
mixerSchedulerSetPeriod(INTERNAL_MODULE, AFHDS2_PERIOD);
break;
#endif
default:
// internal module stopped, use default mixer period
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
break;
if (externalModuleDriver) {
externalModuleDriver->sendPulses(externalModuleContext);
return;
}
}
bool setupPulsesInternalModule(uint8_t protocol)
{
int16_t* channels =
&channelOutputs[g_model.moduleData[INTERNAL_MODULE].channelsStart];
uint8_t nChannels = 16; // TODO: MAX_CHANNELS - channelsStart
switch (moduleState[EXTERNAL_MODULE].protocol) {
switch (protocol) {
#if defined(HARDWARE_INTERNAL_MODULE) && defined(PXX1) && !defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmodulePulsesData.pxx.setupFrame(INTERNAL_MODULE);
return true;
#endif
#if defined(PXX1) && defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
intmodulePulsesData.pxx_uart.setupFrame(INTERNAL_MODULE);
return true;
#endif
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
Pxx2ModuleDriver.setupPulses((void*)INTERNAL_MODULE, channels, nChannels);
return true;
#endif
#if defined(PCBTARANIS) && defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
setupPulsesPPMInternalModule();
return true;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
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)
case PROTOCOL_CHANNELS_MULTIMODULE:
setupPulsesMultiInternalModule();
mixerSchedulerSetPeriod(INTERNAL_MODULE, MULTIMODULE_PERIOD);
return true;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A:
{
ModuleSyncStatus& status = getModuleSyncStatus(INTERNAL_MODULE);
mixerSchedulerSetPeriod(
INTERNAL_MODULE,
status.isValid() ? status.getAdjustedRefreshRate() : AFHDS2_PERIOD);
status.invalidate();
setupPulsesAFHDS2();
return true;
}
#endif
default:
return false;
}
}
void stopPulsesInternalModule()
{
if (moduleState[INTERNAL_MODULE].protocol != PROTOCOL_CHANNELS_UNINITIALIZED) {
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
intmoduleStop();
moduleState[INTERNAL_MODULE].protocol = PROTOCOL_CHANNELS_NONE;
}
}
void intmoduleSendNextFrame()
{
switch (moduleState[INTERNAL_MODULE].protocol) {
#if defined(PXX2)
case PROTOCOL_CHANNELS_PXX2_HIGHSPEED:
Pxx2ModuleDriver.sendPulses((void*)INTERNAL_MODULE);
break;
#endif
#if defined(INTERNAL_MODULE_PPM)
case PROTOCOL_CHANNELS_PPM:
intmoduleSendNextFramePPM(
intmodulePulsesData.ppm.pulses,
intmodulePulsesData.ppm.ptr - intmodulePulsesData.ppm.pulses);
break;
#endif
// Note: this is called directly from the EXTMODULE_TIMER_IRQHandler()
// periodically (the only non-synchronous pulse driver
//
// case PROTOCOL_CHANNELS_PPM:
// extmoduleSendNextFramePpm(
// extmodulePulsesData.ppm.pulses,
// extmodulePulsesData.ppm.ptr - extmodulePulsesData.ppm.pulses,
// *(extmodulePulsesData.ppm.ptr - 1));
// break;
#if defined(PXX1)
#if defined(INTMODULE_USART)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.pxx_uart.getData(),
intmodulePulsesData.pxx_uart.getSize());
break;
#else
case PROTOCOL_CHANNELS_PXX1_PULSES:
intmoduleSendNextFramePxx1(intmodulePulsesData.pxx.getData(),
intmodulePulsesData.pxx.getSize());
extmoduleSendNextFramePxx1(extmodulePulsesData.pxx.getData(),
extmodulePulsesData.pxx.getSize());
break;
#endif
#if defined(PXX1) && defined(HARDWARE_EXTERNAL_MODULE_SIZE_SML)
case PROTOCOL_CHANNELS_PXX1_SERIAL:
extmoduleSendBuffer(extmodulePulsesData.pxx_uart.getData(),
extmodulePulsesData.pxx_uart.getSize());
break;
#endif
#if defined(INTERNAL_MODULE_MULTI)
#if defined(AFHDS3)
case PROTOCOL_CHANNELS_AFHDS3:
#if defined(EXTMODULE_USART) && defined(EXTMODULE_TX_INVERT_GPIO)
extmoduleSendBuffer(extmodulePulsesData.afhds3.getData(),
extmodulePulsesData.afhds3.getSize());
#else
extmoduleSendNextFrameAFHDS3(extmodulePulsesData.afhds3.getData(),
extmodulePulsesData.afhds3.getSize());
#endif
break;
#endif
#if defined(DSM2)
case PROTOCOL_CHANNELS_SBUS:
case PROTOCOL_CHANNELS_DSM2_LP45:
case PROTOCOL_CHANNELS_DSM2_DSM2:
case PROTOCOL_CHANNELS_DSM2_DSMX:
case PROTOCOL_CHANNELS_MULTIMODULE:
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.multi.getData(),
intmodulePulsesData.multi.getSize());
extmoduleSendNextFrameSoftSerial100kbit(
extmodulePulsesData.dsm2.pulses,
extmodulePulsesData.dsm2.ptr - extmodulePulsesData.dsm2.pulses);
break;
#endif
#if defined(INTERNAL_MODULE_CRSF)
case PROTOCOL_CHANNELS_CROSSFIRE:
if (CrossfireModuleDriver.sendPulses) {
CrossfireModuleDriver.sendPulses((void*)INTERNAL_MODULE);
}
#if defined(GHOST)
case PROTOCOL_CHANNELS_GHOST:
sportSendBuffer(extmodulePulsesData.ghost.pulses,
extmodulePulsesData.ghost.length);
break;
#endif
#if defined(AFHDS2)
case PROTOCOL_CHANNELS_AFHDS2A: {
uint8_t* data = (uint8_t*)intmodulePulsesData.flysky.pulses;
uint16_t size = intmodulePulsesData.flysky.ptr - data;
intmoduleSendBuffer(data, size);
} break;
#endif
}
}
bool setupPulsesInternalModule()
{
uint8_t protocol = getRequiredProtocol(INTERNAL_MODULE);
heartbeat |= (HEART_TIMER_PULSES << INTERNAL_MODULE);
if (moduleState[INTERNAL_MODULE].protocol != protocol) {
intmoduleStop();
enablePulsesInternalModule(protocol);
moduleState[INTERNAL_MODULE].protocol = protocol;
return false;
}
else {
return setupPulsesInternalModule(protocol);
}
}
#endif
#if defined(HARDWARE_EXTERNAL_MODULE)
void stopPulsesExternalModule()
{
if (moduleState[EXTERNAL_MODULE].protocol != PROTOCOL_CHANNELS_UNINITIALIZED) {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
if (moduleState[EXTERNAL_MODULE].protocol !=
PROTOCOL_CHANNELS_UNINITIALIZED) {
if (externalModuleDriver) {
externalModuleDriver->deinit(externalModuleContext);
externalModuleDriver = nullptr;
externalModuleContext = nullptr;
} else {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
}
moduleState[EXTERNAL_MODULE].protocol = PROTOCOL_CHANNELS_NONE;
}
}
@@ -726,7 +807,6 @@ bool setupPulsesExternalModule()
heartbeat |= (HEART_TIMER_PULSES << EXTERNAL_MODULE);
if (moduleState[EXTERNAL_MODULE].protocol != protocol) {
extmoduleStop();
enablePulsesExternalModule(protocol);
moduleState[EXTERNAL_MODULE].protocol = protocol;
return false;
+1
View File
@@ -231,6 +231,7 @@ void intmoduleSendNextFrame();
#if defined(HARDWARE_EXTERNAL_MODULE)
bool setupPulsesExternalModule();
void stopPulsesExternalModule();
void extmoduleSendNextFrame();
#endif
void setupPulsesDSM2();
void setupPulsesCrossfire(uint8_t idx);
+97 -69
View File
@@ -647,50 +647,87 @@ void Pxx2OtaUpdate::flashFirmware(const char * filename, ProgressHandler progres
resumePulses();
}
static void* pxx2Init(uint8_t module)
static void* pxx2InitInternal(uint8_t moduleType)
{
if (module == INTERNAL_MODULE) {
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&pxx2SerialInitParams);
resetAccessAuthenticationCount();
intmoduleFifo.clear();
IntmoduleSerialDriver.init(&pxx2SerialInitParams);
resetAccessAuthenticationCount();
#if defined(INTMODULE_HEARTBEAT)
// use backup trigger (1 ms later)
init_intmodule_heartbeat();
// use backup trigger (1 ms later)
init_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, PXX2_PERIOD);
return nullptr;
}
static void pxx2DeInitInternal(void* context)
{
IntmoduleSerialDriver.deinit();
#if defined(INTMODULE_HEARTBEAT)
stop_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
}
static bool pxx2InternalSendNextFrame = true;
static void pxx2SetupPulsesInternal(void* context, int16_t* channels, uint8_t nChannels)
{
(void)context;
pxx2InternalSendNextFrame =
intmodulePulsesData.pxx2.setupFrame(INTERNAL_MODULE, channels, nChannels);
if (moduleState[INTERNAL_MODULE].mode == MODULE_MODE_SPECTRUM_ANALYSER ||
moduleState[INTERNAL_MODULE].mode == MODULE_MODE_POWER_METER) {
mixerSchedulerSetPeriod(INTERNAL_MODULE, PXX2_TOOLS_PERIOD);
} else {
mixerSchedulerSetPeriod(INTERNAL_MODULE, PXX2_PERIOD);
} else {
#if defined(EXTMODULE_USART)
extmoduleInvertedSerialStart(PXX2_HIGHSPEED_BAUDRATE);
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PXX2_NO_HEARTBEAT_PERIOD);
#else
return nullptr;
#endif
}
return (void*)(uint32_t)module;
}
static void pxx2DeInit(void* context)
static void pxx2SendPulsesInternal(void* context)
{
uint32_t module = (uint32_t)context;
(void)context;
if (module == INTERNAL_MODULE) {
IntmoduleSerialDriver.deinit();
#if defined(INTMODULE_HEARTBEAT)
stop_intmodule_heartbeat();
#endif
mixerSchedulerSetPeriod(INTERNAL_MODULE, 0);
} else {
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
if (pxx2InternalSendNextFrame) {
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.pxx2.getData(),
intmodulePulsesData.pxx2.getSize());
}
}
#include "hal/module_driver.h"
const etx_module_driver_t Pxx2InternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
.init = pxx2InitInternal,
.deinit = pxx2DeInitInternal,
.setupPulses = pxx2SetupPulsesInternal,
.sendPulses = pxx2SendPulsesInternal,
};
#if defined(EXTMODULE_USART)
#include "extmodule_serial_driver.h"
static const etx_serial_init pxx2ExtSerialInitParams = {
.baudrate = 0,
.parity = ETX_Parity_None,
.stop_bits = ETX_StopBits_One,
.word_length = ETX_WordLength_8,
.rx_enable = true,
.on_receive = extmoduleFifoReceive,
.on_error = extmoduleFifoError,
};
static void* pxx2InitLowSpeed(uint8_t module)
{
if (module == EXTERNAL_MODULE) {
extmoduleInvertedSerialStart(PXX2_LOWSPEED_BAUDRATE);
etx_serial_init params(pxx2ExtSerialInitParams);
params.baudrate = PXX2_LOWSPEED_BAUDRATE;
ExtmoduleSerialDriver.init(&params);
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PXX2_NO_HEARTBEAT_PERIOD);
return (void*)EXTERNAL_MODULE;
}
@@ -698,59 +735,50 @@ static void* pxx2InitLowSpeed(uint8_t module)
return nullptr;
}
static bool pxx2InternalSendNextFrame = true;
static void pxx2SetupPulses(void* context, int16_t* channels, uint8_t nChannels)
static void* pxx2InitExternal(uint8_t moduleType)
{
uint32_t module = (uint32_t)context;
etx_serial_init params(pxx2ExtSerialInitParams);
params.baudrate = PXX2_HIGHSPEED_BAUDRATE;
ExtmoduleSerialDriver.init(&params);
mixerSchedulerSetPeriod(EXTERNAL_MODULE, PXX2_NO_HEARTBEAT_PERIOD);
if (module == INTERNAL_MODULE) {
pxx2InternalSendNextFrame = intmodulePulsesData.pxx2.setupFrame(
INTERNAL_MODULE, channels, nChannels);
if (moduleState[INTERNAL_MODULE].mode == MODULE_MODE_SPECTRUM_ANALYSER ||
moduleState[INTERNAL_MODULE].mode == MODULE_MODE_POWER_METER) {
mixerSchedulerSetPeriod(INTERNAL_MODULE, PXX2_TOOLS_PERIOD);
} else {
mixerSchedulerSetPeriod(INTERNAL_MODULE, PXX2_PERIOD);
}
} else {
extmodulePulsesData.pxx2.setupFrame(EXTERNAL_MODULE, channels, nChannels);
}
return nullptr;
}
static void pxx2SendPulses(void* context)
static void pxx2DeInitExternal(void* context)
{
uint32_t module = (uint32_t)context;
(void)context;
if (module == INTERNAL_MODULE) {
if (pxx2InternalSendNextFrame) {
IntmoduleSerialDriver.sendBuffer(intmodulePulsesData.pxx2.getData(),
intmodulePulsesData.pxx2.getSize());
}
} else {
#if defined(EXTMODULE_USART)
extmoduleSendBuffer(extmodulePulsesData.pxx2.getData(),
extmodulePulsesData.pxx2.getSize());
#endif
}
mixerSchedulerSetPeriod(EXTERNAL_MODULE, 0);
extmoduleStop();
}
#include "hal/module_driver.h"
static void pxx2SetupPulsesExternal(void* context, int16_t* channels, uint8_t nChannels)
{
(void)context;
extmodulePulsesData.pxx2.setupFrame(EXTERNAL_MODULE, channels, nChannels);
}
const etx_module_driver_t Pxx2ModuleDriver = {
static void pxx2SendPulsesExternal(void* context)
{
(void)context;
ExtmoduleSerialDriver.sendBuffer(extmodulePulsesData.pxx2.getData(),
extmodulePulsesData.pxx2.getSize());
}
const etx_module_driver_t Pxx2ExternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_HIGHSPEED,
.init = pxx2Init,
.deinit = pxx2DeInit,
.setupPulses = pxx2SetupPulses,
.sendPulses = pxx2SendPulses,
.init = pxx2InitExternal,
.deinit = pxx2DeInitExternal,
.setupPulses = pxx2SetupPulsesExternal,
.sendPulses = pxx2SendPulsesExternal,
};
const etx_module_driver_t Pxx2LowSpeedModuleDriver = {
const etx_module_driver_t Pxx2LowSpeedExternalDriver = {
.protocol = PROTOCOL_CHANNELS_PXX2_LOWSPEED,
.init = pxx2InitLowSpeed,
.deinit = pxx2DeInit,
.setupPulses = pxx2SetupPulses,
.sendPulses = pxx2SendPulses,
.deinit = pxx2DeInitExternal,
.setupPulses = pxx2SetupPulsesExternal,
.sendPulses = pxx2SendPulsesExternal,
};
#endif
+4 -2
View File
@@ -579,7 +579,9 @@ class Pxx2OtaUpdate {
};
extern const etx_serial_init pxx2SerialInitParams;
extern const etx_module_driver_t Pxx2ModuleDriver;
extern const etx_module_driver_t Pxx2LowSpeedModuleDriver;
extern const etx_module_driver_t Pxx2InternalDriver;
extern const etx_module_driver_t Pxx2ExternalDriver;
extern const etx_module_driver_t Pxx2LowSpeedExternalDriver;
#endif