perf: Process CRSF telemetry by frame (#4038)
Co-authored-by: 3djc <lesitewebdejc@hotmail.com> Co-authored-by: Peter Feerick <peter.feerick@gmail.com>
This commit is contained in:
co-authored by
3djc
Peter Feerick
parent
eaabb2ce97
commit
c93695eeaa
@@ -93,4 +93,6 @@ const etx_proto_driver_t Afhds2InternalDriver = {
|
||||
.deinit = afhds2DeInit,
|
||||
.sendPulses = afhds2SendPulses,
|
||||
.processData = afhds2ProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -1165,6 +1165,8 @@ etx_proto_driver_t ProtoDriver = {
|
||||
.deinit = deinitModule,
|
||||
.sendPulses = sendPulses,
|
||||
.processData = processTelemetryData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
} // namespace afhds3
|
||||
|
||||
+145
-60
@@ -19,6 +19,11 @@
|
||||
* GNU General Public License for more details.
|
||||
*/
|
||||
|
||||
#if !defined(SIMU)
|
||||
#include "stm32_exti_driver.h"
|
||||
#include "stm32_hal_ll.h"
|
||||
#endif
|
||||
|
||||
#include "opentx.h"
|
||||
#include "mixer_scheduler.h"
|
||||
#include "hal/module_driver.h"
|
||||
@@ -35,6 +40,7 @@
|
||||
#define CROSSFIRE_CENTER_CH_OFFSET(ch) (0)
|
||||
#endif
|
||||
|
||||
#define MIN_FRAME_LEN 3
|
||||
|
||||
uint8_t createCrossfireModelIDFrame(uint8_t moduleIdx, uint8_t * frame)
|
||||
{
|
||||
@@ -112,8 +118,8 @@ static void crossfireSetupMixerScheduler(uint8_t module)
|
||||
static bool _checkFrameCRC(uint8_t* rxBuffer)
|
||||
{
|
||||
uint8_t len = rxBuffer[1];
|
||||
uint8_t crc = crc8(&rxBuffer[2], len-1);
|
||||
return (crc == rxBuffer[len+1]);
|
||||
uint8_t crc = crc8(&rxBuffer[2], len - 1);
|
||||
return (crc == rxBuffer[len + 1]);
|
||||
}
|
||||
|
||||
static void crossfireSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, uint8_t nChannels)
|
||||
@@ -136,77 +142,90 @@ static void crossfireSendPulses(void* ctx, uint8_t* buffer, int16_t* channels, u
|
||||
|
||||
static bool _lenIsSane(uint8_t len)
|
||||
{
|
||||
// packet len must be at least 3 bytes (type+payload+crc) and 2 bytes < MAX (hdr+len)
|
||||
return (len > 2 && len < TELEMETRY_RX_PACKET_SIZE-1);
|
||||
// packet len must be at least 3 bytes (type + payload + crc)
|
||||
// and 2 bytes < MAX (hdr + len)
|
||||
return (len > 2 && len < TELEMETRY_RX_PACKET_SIZE - 1);
|
||||
}
|
||||
|
||||
static void _seekStart(uint8_t* buffer, uint8_t* len)
|
||||
static void crossfireProcessFrame(void* ctx, uint8_t* frame, uint8_t frame_len,
|
||||
uint8_t* buf, uint8_t* p_len)
|
||||
{
|
||||
// Bad telemetry packets frequently are just truncated packets, with the start
|
||||
// of a new packet contained in the data. This causes multiple packet drops as
|
||||
// the parser tries to resync.
|
||||
// Search through the rxBuffer for a sync byte, shift the contents if found
|
||||
// and reduce rxBufferCount
|
||||
for (uint8_t idx = 1; idx < *len; ++idx) {
|
||||
uint8_t data = buffer[idx];
|
||||
if (data == RADIO_ADDRESS || data == UART_SYNC) {
|
||||
uint8_t remain = *len - idx;
|
||||
// If there's at least 2 bytes, check the length for validity too
|
||||
if (remain > 1 && !_lenIsSane(buffer[idx + 1])) continue;
|
||||
if (frame_len < MIN_FRAME_LEN) return;
|
||||
|
||||
// TRACE("Found 0x%02x with %u remain", data, remain);
|
||||
// copy the data to the front of the buffer
|
||||
for (uint8_t src = idx; src < *len; ++src) {
|
||||
buffer[src - idx] = buffer[src];
|
||||
// De-Fragmentation:
|
||||
// It is assumed here that a continuation chunk
|
||||
// will not belong to multiple CRSF frames.
|
||||
|
||||
uint8_t& len = *p_len;
|
||||
if (len > 0) {
|
||||
uint8_t unfrag_len = buf[1] + 2;
|
||||
uint8_t defrag_len = len + frame_len;
|
||||
|
||||
if (defrag_len <= unfrag_len && defrag_len <= TELEMETRY_RX_PACKET_SIZE) {
|
||||
// If we're not going to overshoot
|
||||
// the intended frame length,
|
||||
// let's reassemble it
|
||||
memcpy(buf + len, frame, frame_len);
|
||||
len = defrag_len;
|
||||
frame_len = 0;
|
||||
|
||||
// frame complete?
|
||||
if (defrag_len < unfrag_len) {
|
||||
TRACE("[XF] frag cont frame (%d < %d)", defrag_len, unfrag_len);
|
||||
return;
|
||||
} else {
|
||||
TRACE("[XF] frame complete");
|
||||
}
|
||||
} else {
|
||||
TRACE("[XF] overshoot (%d > %d)", defrag_len, unfrag_len);
|
||||
}
|
||||
}
|
||||
|
||||
*len = remain;
|
||||
if (frame_len > 0) {
|
||||
memcpy(buf, frame, frame_len);
|
||||
len = frame_len;
|
||||
|
||||
uint8_t unfrag_len = buf[1] + 2;
|
||||
if (!_lenIsSane(unfrag_len)) {
|
||||
TRACE("[XF] pkt len error (%d)", unfrag_len);
|
||||
len = 0;
|
||||
return;
|
||||
} // if found sync
|
||||
}
|
||||
|
||||
if (len < unfrag_len) {
|
||||
TRACE("[XF] frag frame (%d < %d)", len, unfrag_len);
|
||||
return;
|
||||
}
|
||||
}
|
||||
|
||||
// Not found, clear the buffer
|
||||
*len = 0;
|
||||
}
|
||||
uint8_t* p_buf = buf;
|
||||
while (len >= MIN_FRAME_LEN) {
|
||||
uint8_t pkt_len = p_buf[1] + 2;
|
||||
if (pkt_len > len) {
|
||||
TRACE("[XF] length error (%d > %d)", pkt_len, len);
|
||||
len = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
static void crossfireProcessData(void* ctx, uint8_t data, uint8_t* buffer, uint8_t* len)
|
||||
{
|
||||
if (*len == 0 && data != RADIO_ADDRESS && data != UART_SYNC) {
|
||||
TRACE("[XF] address 0x%02X error", data);
|
||||
return;
|
||||
}
|
||||
|
||||
if (*len == 1 && !_lenIsSane(data)) {
|
||||
TRACE("[XF] length 0x%02X error", data);
|
||||
*len = 0;
|
||||
return;
|
||||
}
|
||||
|
||||
if (*len < TELEMETRY_RX_PACKET_SIZE) {
|
||||
buffer[(*len)++] = data;
|
||||
}
|
||||
else {
|
||||
TRACE("[XF] array size %d error", *len);
|
||||
*len = 0;
|
||||
}
|
||||
|
||||
// rxBuffer[1] holds the packet length-2, check if the whole packet was received
|
||||
while (*len > 4 && (buffer[1]+2) == *len) {
|
||||
if (_checkFrameCRC(buffer)) {
|
||||
#if defined(BLUETOOTH) // TODO: generic telemetry mirror to BT
|
||||
if (p_buf[0] != RADIO_ADDRESS && p_buf[0] != UART_SYNC) {
|
||||
TRACE("[XF] address 0x%02X error", p_buf[0]);
|
||||
} else if (!_checkFrameCRC(p_buf)) {
|
||||
TRACE("[XF] CRC error ");
|
||||
} else {
|
||||
#if defined(BLUETOOTH)
|
||||
// TODO: generic telemetry mirror to BT
|
||||
if (g_eeGeneral.bluetoothMode == BLUETOOTH_TELEMETRY &&
|
||||
bluetooth.state == BLUETOOTH_STATE_CONNECTED) {
|
||||
bluetooth.write(buffer, *len);
|
||||
bluetooth.write(p_buf, pkt_len);
|
||||
}
|
||||
#endif
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
processCrossfireTelemetryFrame(modulePortGetModule(mod_st));
|
||||
*len = 0;
|
||||
}
|
||||
else {
|
||||
TRACE("[XF] CRC error ");
|
||||
_seekStart(buffer, len); // adjusts len
|
||||
auto module = modulePortGetModule(mod_st);
|
||||
processCrossfireTelemetryFrame(module, p_buf, pkt_len);
|
||||
}
|
||||
|
||||
p_buf += pkt_len;
|
||||
len -= pkt_len;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -217,6 +236,31 @@ static const etx_serial_init crsfSerialParams = {
|
||||
.polarity = ETX_Pol_Normal,
|
||||
};
|
||||
|
||||
#if !defined(SIMU)
|
||||
|
||||
#if defined(INTERNAL_MODULE_CRSF)
|
||||
static void _crsf_intmodule_frame_received(void*)
|
||||
{
|
||||
telemetryFrameTrigger_ISR(INTERNAL_MODULE, &CrossfireDriver);
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(HARDWARE_EXTERNAL_MODULE)
|
||||
static void _crsf_extmodule_frame_received()
|
||||
{
|
||||
telemetryFrameTrigger_ISR(EXTERNAL_MODULE, &CrossfireDriver);
|
||||
}
|
||||
|
||||
// proxy trigger to avoid calling
|
||||
// FreeRTOS methods from ISR with prio 0
|
||||
static void _soft_irq_trigger(void*)
|
||||
{
|
||||
EXTI->SWIER = TELEMETRY_RX_FRAME_EXTI_LINE;
|
||||
}
|
||||
#endif
|
||||
|
||||
#endif // !SIMU
|
||||
|
||||
static void* crossfireInit(uint8_t module)
|
||||
{
|
||||
etx_module_state_t* mod_st = nullptr;
|
||||
@@ -224,17 +268,45 @@ static void* crossfireInit(uint8_t module)
|
||||
|
||||
#if defined(INTERNAL_MODULE_CRSF)
|
||||
if (module == INTERNAL_MODULE) {
|
||||
|
||||
params.baudrate = INT_CROSSFIRE_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_UART, ¶ms, false);
|
||||
|
||||
if (mod_st) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
|
||||
auto& rx_count = getTelemetryRxBufferCount(INTERNAL_MODULE);
|
||||
rx_count = 0;
|
||||
|
||||
#if !defined(SIMU)
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
drv->setIdleCb(ctx, _crsf_intmodule_frame_received, 0);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
#if defined(HARDWARE_EXTERNAL_MODULE)
|
||||
if (module == EXTERNAL_MODULE) {
|
||||
|
||||
params.baudrate = EXT_CROSSFIRE_BAUDRATE;
|
||||
mod_st = modulePortInitSerial(module, ETX_MOD_PORT_SPORT, ¶ms, false);
|
||||
|
||||
if (mod_st) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
|
||||
auto& rx_count = getTelemetryRxBufferCount(EXTERNAL_MODULE);
|
||||
rx_count = 0;
|
||||
|
||||
#if !defined(SIMU)
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
drv->setIdleCb(ctx, _soft_irq_trigger, 0);
|
||||
stm32_exti_enable(TELEMETRY_RX_FRAME_EXTI_LINE, 0,
|
||||
_crsf_extmodule_frame_received);
|
||||
}
|
||||
#endif
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
@@ -248,6 +320,17 @@ static void* crossfireInit(uint8_t module)
|
||||
static void crossfireDeInit(void* ctx)
|
||||
{
|
||||
auto mod_st = (etx_module_state_t*)ctx;
|
||||
|
||||
#if !defined(SIMU)
|
||||
if (mod_st && (modulePortGetModule(mod_st) == EXTERNAL_MODULE)) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
stm32_exti_disable(TELEMETRY_RX_FRAME_EXTI_LINE);
|
||||
}
|
||||
}
|
||||
#endif
|
||||
|
||||
modulePortDeInit(mod_st);
|
||||
}
|
||||
|
||||
@@ -256,5 +339,7 @@ const etx_proto_driver_t CrossfireDriver = {
|
||||
.init = crossfireInit,
|
||||
.deinit = crossfireDeInit,
|
||||
.sendPulses = crossfireSendPulses,
|
||||
.processData = crossfireProcessData,
|
||||
.processData = nullptr,
|
||||
.processFrame = crossfireProcessFrame,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -296,6 +296,7 @@ const etx_proto_driver_t DSM2Driver = {
|
||||
.deinit = dsmDeInit,
|
||||
.sendPulses = dsm2SendPulses,
|
||||
.processData = nullptr,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = dsm2ConfigChange,
|
||||
};
|
||||
|
||||
@@ -305,5 +306,6 @@ const etx_proto_driver_t DSMPDriver = {
|
||||
.deinit = dsmDeInit,
|
||||
.sendPulses = dsmpSendPulses,
|
||||
.processData = dsmpProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -252,4 +252,6 @@ const etx_proto_driver_t GhostDriver = {
|
||||
.deinit = ghostDeInit,
|
||||
.sendPulses = ghostSendPulses,
|
||||
.processData = ghostProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -286,6 +286,8 @@ const etx_proto_driver_t MultiDriver = {
|
||||
.deinit = multiDeInit,
|
||||
.sendPulses = multiSendPulses,
|
||||
.processData = multiProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
static void sendChannels(uint8_t*& p_buf, uint8_t module)
|
||||
|
||||
@@ -219,5 +219,6 @@ const etx_proto_driver_t PpmDriver = {
|
||||
.deinit = ppmDeInit,
|
||||
.sendPulses = ppmSendPulses,
|
||||
.processData = ppmProcessTelemetryData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = ppmOnConfigChange,
|
||||
};
|
||||
|
||||
+13
-14
@@ -257,26 +257,26 @@ static const etx_serial_init pxx1SerialCfg = {
|
||||
.polarity = ETX_Pol_Normal,
|
||||
};
|
||||
|
||||
static const etx_serial_driver_t* _sport_drv;
|
||||
static void* _sport_ctx;
|
||||
|
||||
static void pxx1SportSensorPolling()
|
||||
static void pxx1SportSensorPolling(void* param)
|
||||
{
|
||||
if (outputTelemetryBuffer.destination != TELEMETRY_ENDPOINT_SPORT)
|
||||
return;
|
||||
|
||||
auto mod_st = (etx_module_state_t*)param;
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
|
||||
// Match sensor polling from the module
|
||||
// -> detect <0x7E [Physical ID]> as the last sequence
|
||||
uint8_t b;
|
||||
if (_sport_drv->getLastByte(_sport_ctx, 2, &b) <= 0 || b != START_STOP)
|
||||
return;
|
||||
if (_sport_drv->getLastByte(_sport_ctx, 1, &b) <= 0 ||
|
||||
if (drv->getBufferedBytes(ctx) != 2 ||
|
||||
drv->getLastByte(ctx, 2, &b) <= 0 || b != START_STOP ||
|
||||
drv->getLastByte(ctx, 1, &b) <= 0 ||
|
||||
b != outputTelemetryBuffer.sport.physicalId)
|
||||
return;
|
||||
|
||||
_sport_drv->sendBuffer(_sport_ctx,
|
||||
outputTelemetryBuffer.data + 1,
|
||||
outputTelemetryBuffer.size - 1);
|
||||
drv->sendBuffer(ctx, outputTelemetryBuffer.data + 1,
|
||||
outputTelemetryBuffer.size - 1);
|
||||
|
||||
outputTelemetryBuffer.reset();
|
||||
}
|
||||
@@ -333,14 +333,11 @@ static void* pxx1Init(uint8_t module)
|
||||
rxCfg.baudrate = FRSKY_SPORT_BAUDRATE;
|
||||
rxCfg.direction = ETX_Dir_TX_RX;
|
||||
|
||||
// TODO: handle init errors properly
|
||||
if (modulePortInitSerial(module, ETX_MOD_PORT_SPORT, &rxCfg, false) != nullptr) {
|
||||
auto drv = modulePortGetSerialDrv(mod_st->rx);
|
||||
auto ctx = modulePortGetCtx(mod_st->rx);
|
||||
if (drv && ctx && drv->setIdleCb) {
|
||||
_sport_drv = drv;
|
||||
_sport_ctx = ctx;
|
||||
drv->setIdleCb(ctx, pxx1SportSensorPolling);
|
||||
drv->setIdleCb(ctx, pxx1SportSensorPolling, mod_st);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -417,4 +414,6 @@ const etx_proto_driver_t Pxx1Driver = {
|
||||
.deinit = pxx1DeInit,
|
||||
.sendPulses = pxx1SendPulses,
|
||||
.processData = pxx1ProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -841,4 +841,6 @@ const etx_proto_driver_t Pxx2Driver = {
|
||||
.deinit = pxx2DeInit,
|
||||
.sendPulses = pxx2SendPulses,
|
||||
.processData = pxx2ProcessData,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
@@ -154,4 +154,6 @@ const etx_proto_driver_t SBusDriver = {
|
||||
.deinit = sbusDeInit,
|
||||
.sendPulses = sbusSendPulses,
|
||||
.processData = nullptr,
|
||||
.processFrame = nullptr,
|
||||
.onConfigChange = nullptr,
|
||||
};
|
||||
|
||||
Reference in New Issue
Block a user