From 3c5dd1604e94974c5c9a32be89271d5c2944917f Mon Sep 17 00:00:00 2001 From: Martin Budden Date: Wed, 16 Nov 2016 09:11:15 +0000 Subject: [PATCH] Ensured there is sufficient time to send a telemetry frame --- src/main/rx/crsf.c | 14 +++++++++----- 1 file changed, 9 insertions(+), 5 deletions(-) diff --git a/src/main/rx/crsf.c b/src/main/rx/crsf.c index b8c623ca7..8bf33c87f 100644 --- a/src/main/rx/crsf.c +++ b/src/main/rx/crsf.c @@ -41,7 +41,8 @@ #include "telemetry/telemetry.h" -#define CRSF_TIME_NEEDED_PER_FRAME_US 1500 //!! this needs checking +#define CRSF_TIME_NEEDED_PER_FRAME_US 1000 +#define CRSF_TIME_BETWEEN_FRAMES_US 4000 // a frame is sent by the transmitter every 4 milliseconds #define CRSF_DIGITAL_CHANNEL_MIN 172 #define CRSF_DIGITAL_CHANNEL_MAX 1811 @@ -72,6 +73,8 @@ static uint8_t telemetryBufLen = 0; * Assume a max payload of 32 bytes (needs confirming with TBS), so max frame size of 36 bytes * A 36 byte frame can be transmitted in 771 microseconds. * + * CRSF_TIME_NEEDED_PER_FRAME_US is set conservatively at 1000 microseconds + * * Every frame has the structure: * < Type> < CRC> * @@ -204,10 +207,11 @@ void crsfRxSendTelemetryData(void) { // if there is telemetry data to write if (telemetryBufLen > 0) { - // check that we are not currently receiving data - const uint32_t now = micros(); - if (now > crsfFrameStartAt + CRSF_TIME_NEEDED_PER_FRAME_US) { - // any incoming frames will be complete, so it is OK to write to shared serial port + // check that we are not currently receiving data (ie in the middle of an RX frame) + // and that there is time to send the telemetry frame before the next RX frame arrives + const uint32_t timeSinceStartOfFrame = micros() - crsfFrameStartAt; + if ((timeSinceStartOfFrame > CRSF_TIME_NEEDED_PER_FRAME_US) + && (timeSinceStartOfFrame < CRSF_TIME_BETWEEN_FRAMES_US - CRSF_TIME_NEEDED_PER_FRAME_US)) { serialWriteBuf(serialPort, telemetryBuf, telemetryBufLen); telemetryBufLen = 0; // reset telemetry buffer }