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:
+135
-24
@@ -20,10 +20,28 @@
|
||||
*/
|
||||
|
||||
#include "opentx.h"
|
||||
#include "ghost.h"
|
||||
#include "telemetry/ghost.h"
|
||||
#include "telemetry/ghost_menu.h"
|
||||
#include "hal/module_port.h"
|
||||
#include "mixer_scheduler.h"
|
||||
|
||||
uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
|
||||
static uint8_t getGhostModuleAddr()
|
||||
{
|
||||
// TODO: check if asymmetric baudrate is really needed
|
||||
// as it introduces a lot of clutter
|
||||
//
|
||||
// #if SPORT_MAX_BAUDRATE < 400000
|
||||
// return g_model.moduleData[EXTERNAL_MODULE].ghost.telemetryBaudrate ==
|
||||
// GHST_TELEMETRY_RATE_400K
|
||||
// ? GHST_ADDR_MODULE_SYM
|
||||
// : GHST_ADDR_MODULE_ASYM;
|
||||
// #else
|
||||
return GHST_ADDR_MODULE_SYM;
|
||||
// #endif
|
||||
}
|
||||
|
||||
static uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
|
||||
{
|
||||
uint8_t * buf = frame;
|
||||
|
||||
@@ -46,7 +64,7 @@ uint8_t createGhostMenuControlFrame(uint8_t * frame, int16_t * pulses)
|
||||
}
|
||||
|
||||
// Range for pulses (channels output) is [-1024:+1024]
|
||||
uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bits)
|
||||
static uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bits)
|
||||
{
|
||||
static uint8_t lastGhostFrameId = 0;
|
||||
uint8_t ghostUpper4Offset = 0;
|
||||
@@ -88,10 +106,16 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
|
||||
uint8_t bitsavailable = 0;
|
||||
for (int i = 0; i < 4; i++) {
|
||||
uint32_t value;
|
||||
if(raw12bits) {
|
||||
value = limit(0, (1024 + (pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER)) << 1, 0xFFF);
|
||||
if (raw12bits) {
|
||||
value = limit(
|
||||
0, (1024 + (pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER)) << 1,
|
||||
0xFFF);
|
||||
} else {
|
||||
value = limit(0, GHST_RC_CTR_VAL_12BIT + (((pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER) << 3) / 5), 2 * GHST_RC_CTR_VAL_12BIT);
|
||||
value = limit(
|
||||
0,
|
||||
GHST_RC_CTR_VAL_12BIT +
|
||||
(((pulses[i] + 2 * PPM_CH_CENTER(i) - 2 * PPM_CENTER) << 3) / 5),
|
||||
2 * GHST_RC_CTR_VAL_12BIT);
|
||||
}
|
||||
bits |= value << bitsavailable;
|
||||
bitsavailable += GHST_CH_BITS_12;
|
||||
@@ -106,10 +130,20 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
|
||||
for (int i = 4; i < 8; ++i) {
|
||||
uint8_t channelIndex = i + ghostUpper4Offset;
|
||||
uint8_t value;
|
||||
if(raw12bits) {
|
||||
value = limit(0, 128 + ((pulses[channelIndex] + 2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >> 3), 0xFF);
|
||||
if (raw12bits) {
|
||||
value = limit(0,
|
||||
128 + ((pulses[channelIndex] +
|
||||
2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >>
|
||||
3),
|
||||
0xFF);
|
||||
} else {
|
||||
value = limit(0, GHST_RC_CTR_VAL_8BIT + (((pulses[channelIndex] + 2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >> 1) / 5), 2 * GHST_RC_CTR_VAL_8BIT);
|
||||
value = limit(0,
|
||||
GHST_RC_CTR_VAL_8BIT +
|
||||
(((pulses[channelIndex] +
|
||||
2 * PPM_CH_CENTER(channelIndex) - 2 * PPM_CENTER) >>
|
||||
1) /
|
||||
5),
|
||||
2 * GHST_RC_CTR_VAL_8BIT);
|
||||
}
|
||||
*buf++ = value;
|
||||
}
|
||||
@@ -120,25 +154,102 @@ uint8_t createGhostChannelsFrame(uint8_t * frame, int16_t * pulses, bool raw12bi
|
||||
return buf - frame;
|
||||
}
|
||||
|
||||
void setupPulsesGhost()
|
||||
static const etx_serial_init ghostSerialParams = {
|
||||
.baudrate = GHOST_BAUDRATE,
|
||||
.encoding = ETX_Encoding_8N1,
|
||||
.direction = ETX_Dir_TX_RX,
|
||||
.polarity = ETX_Pol_Normal,
|
||||
};
|
||||
|
||||
static void* ghostInit(uint8_t module)
|
||||
{
|
||||
if (telemetryProtocol == PROTOCOL_TELEMETRY_GHOST) {
|
||||
auto mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &ghostSerialParams);
|
||||
if (!mod_st) return nullptr;
|
||||
|
||||
auto &module = g_model.moduleData[EXTERNAL_MODULE];
|
||||
auto *p_data = &extmodulePulsesData.ghost;
|
||||
mixerSchedulerSetPeriod(module, GHOST_PERIOD);
|
||||
return mod_st;
|
||||
}
|
||||
|
||||
#if defined(LUA)
|
||||
if (outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT) {
|
||||
memcpy(p_data->pulses, outputTelemetryBuffer.data, outputTelemetryBuffer.size);
|
||||
p_data->length = outputTelemetryBuffer.size;
|
||||
outputTelemetryBuffer.reset();
|
||||
} else
|
||||
#endif
|
||||
if (moduleState[EXTERNAL_MODULE].counter == GHST_MENU_CONTROL) {
|
||||
p_data->length = createGhostMenuControlFrame(p_data->pulses, &channelOutputs[module.channelsStart]);
|
||||
} else {
|
||||
p_data->length = createGhostChannelsFrame(p_data->pulses, &channelOutputs[module.channelsStart], module.ghost.raw12bits);
|
||||
static void ghostDeInit(void* ctx)
|
||||
{
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
modulePortDeInit(mod_st);
|
||||
}
|
||||
|
||||
static void ghostSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
|
||||
{
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
auto module = modulePortGetModule(mod_st);
|
||||
|
||||
ModuleSyncStatus& status = getModuleSyncStatus(module);
|
||||
if (status.isValid()) {
|
||||
mixerSchedulerSetPeriod(module, status.getAdjustedRefreshRate());
|
||||
} else {
|
||||
mixerSchedulerSetPeriod(module, GHOST_PERIOD);
|
||||
}
|
||||
|
||||
const auto& mod_cfg = g_model.moduleData[module].ghost;
|
||||
auto p_data = buffer;
|
||||
|
||||
#if defined(LUA)
|
||||
if (outputTelemetryBuffer.destination == TELEMETRY_ENDPOINT_SPORT) {
|
||||
auto len = outputTelemetryBuffer.size;
|
||||
auto f_data = outputTelemetryBuffer.data;
|
||||
while (len >= 12) { // type(1B) + payload(10B) + crc(1B)
|
||||
// insert address byte
|
||||
*p_data++ = getGhostModuleAddr();
|
||||
// and length
|
||||
*p_data++ = 12;
|
||||
memcpy(p_data, f_data, 12);
|
||||
p_data += 12; f_data += 12;
|
||||
len -= 12;
|
||||
}
|
||||
outputTelemetryBuffer.reset();
|
||||
} else
|
||||
#endif
|
||||
if (moduleState[module].counter == GHST_MENU_CONTROL) {
|
||||
p_data += createGhostMenuControlFrame(p_data, channels);
|
||||
} else {
|
||||
p_data += createGhostChannelsFrame(p_data, channels, mod_cfg.raw12bits);
|
||||
}
|
||||
|
||||
moduleState[module].counter = GHST_FRAME_CHANNEL;
|
||||
|
||||
auto drv = modulePortGetSerialDrv(mod_st->tx);
|
||||
auto drv_ctx = modulePortGetCtx(mod_st->tx);
|
||||
drv->sendBuffer(drv_ctx, buffer, p_data - buffer);
|
||||
}
|
||||
|
||||
static void ghostProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
|
||||
{
|
||||
if (*len == 0 && data != GHST_ADDR_RADIO) {
|
||||
TRACE("[GH] address 0x%02X error", data);
|
||||
return;
|
||||
}
|
||||
|
||||
if (*len < TELEMETRY_RX_PACKET_SIZE) {
|
||||
buffer[(*len)++] = data;
|
||||
}
|
||||
else {
|
||||
TRACE("[GH] array size %d error", *len);
|
||||
*len = 0;
|
||||
}
|
||||
|
||||
if (*len > 4) {
|
||||
uint8_t length = buffer[1];
|
||||
if (length + 2 == *len) {
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
auto module = modulePortGetModule(mod_st);
|
||||
processGhostTelemetryFrame(module, buffer, *len);
|
||||
*len = 0;
|
||||
}
|
||||
moduleState[EXTERNAL_MODULE].counter = GHST_FRAME_CHANNEL;
|
||||
}
|
||||
}
|
||||
|
||||
const etx_proto_driver_t GhostDriver = {
|
||||
.protocol = PROTOCOL_CHANNELS_GHOST,
|
||||
.init = ghostInit,
|
||||
.deinit = ghostDeInit,
|
||||
.sendPulses = ghostSendPulses,
|
||||
.processData = ghostProcessData,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user