From e0afe42010efb0509e3108fa21121320d6e35b52 Mon Sep 17 00:00:00 2001 From: Hans Christian Olaussen <41271048+klutvott123@users.noreply.github.com> Date: Wed, 27 Apr 2022 22:10:55 +0200 Subject: [PATCH] CRSF use persistent object to retain baudrate Use persistent object to store negotiated CRSF baudrate in RTC backup register instead of .noinit. --- src/main/drivers/persistent.h | 1 + src/main/rx/crsf.c | 4 +++- src/main/telemetry/crsf.c | 10 ++++------ 3 files changed, 8 insertions(+), 7 deletions(-) diff --git a/src/main/drivers/persistent.h b/src/main/drivers/persistent.h index 2b7ba80b1..8dddfe175 100644 --- a/src/main/drivers/persistent.h +++ b/src/main/drivers/persistent.h @@ -37,6 +37,7 @@ typedef enum { #endif PERSISTENT_OBJECT_RTC_HIGH, // high 32 bits of rtcTime_t PERSISTENT_OBJECT_RTC_LOW, // low 32 bits of rtcTime_t + PERSISTENT_OBJECT_SERIALRX_BAUD, // serial rx baudrate PERSISTENT_OBJECT_COUNT, #ifdef USE_SPRACING_PERSISTENT_RTC_WORKAROUND // On SPRACING H7 firmware use this alternate location for all reset reasons interpreted by this firmware diff --git a/src/main/rx/crsf.c b/src/main/rx/crsf.c index 981eb6e21..d8d9236e5 100644 --- a/src/main/rx/crsf.c +++ b/src/main/rx/crsf.c @@ -38,6 +38,7 @@ #include "pg/rx.h" +#include "drivers/persistent.h" #include "drivers/serial.h" #include "drivers/serial_uart.h" #include "drivers/system.h" @@ -639,7 +640,7 @@ bool crsfRxInit(const rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState) uint32_t crsfBaudrate = CRSF_BAUDRATE; #if defined(USE_CRSF_V3) - crsfBaudrate = (isMPUSoftReset() && rxConfig->crsf_use_negotiated_baud) ? getCrsfCachedBaudrate() : CRSF_BAUDRATE; + crsfBaudrate = rxConfig->crsf_use_negotiated_baud ? getCrsfCachedBaudrate() : CRSF_BAUDRATE; #endif serialPort = openSerialPort(portConfig->identifier, @@ -667,6 +668,7 @@ bool crsfRxInit(const rxConfig_t *rxConfig, rxRuntimeState_t *rxRuntimeState) void crsfRxUpdateBaudrate(uint32_t baudrate) { serialSetBaudRate(serialPort, baudrate); + persistentObjectWrite(PERSISTENT_OBJECT_SERIALRX_BAUD, baudrate); } bool crsfRxUseNegotiatedBaud(void) diff --git a/src/main/telemetry/crsf.c b/src/main/telemetry/crsf.c index 2fbb3a242..88868ce9c 100644 --- a/src/main/telemetry/crsf.c +++ b/src/main/telemetry/crsf.c @@ -43,6 +43,7 @@ #include "common/utils.h" #include "drivers/nvic.h" +#include "drivers/persistent.h" #include "fc/rc_modes.h" #include "fc/runtime_config.h" @@ -103,10 +104,9 @@ typedef struct { static crsfSpeedControl_s crsfSpeed = {0}; -uint32_t crsfCachedBaudrate __attribute__ ((section (".noinit"))); // Used for retaining negotiated baudrate after soft reset - uint32_t getCrsfCachedBaudrate(void) { + uint32_t crsfCachedBaudrate = persistentObjectRead(PERSISTENT_OBJECT_SERIALRX_BAUD); // check if valid first. return default baudrate if not for (unsigned i = 0; i < BAUD_COUNT; i++) { if (crsfCachedBaudrate == baudRates[i] && baudRates[i] >= CRSF_BAUDRATE) { @@ -133,8 +133,7 @@ void setCrsfDefaultSpeed(void) crsfSpeed.confirmationTime = 0; crsfSpeed.index = BAUD_COUNT; isCrsfV3Running = false; - crsfCachedBaudrate = getCrsfDesiredSpeed(); - crsfRxUpdateBaudrate(crsfCachedBaudrate); + crsfRxUpdateBaudrate(getCrsfDesiredSpeed()); } bool crsfBaudNegotiationInProgress(void) @@ -470,8 +469,7 @@ void speedNegotiationProcess(timeUs_t currentTimeUs) } else if (crsfSpeed.isNewSpeedValid) { if (cmpTimeUs(currentTimeUs, crsfSpeed.confirmationTime) >= 4000) { // delay 4ms before applying the new baudrate - crsfCachedBaudrate = getCrsfDesiredSpeed(); - crsfRxUpdateBaudrate(crsfCachedBaudrate); + crsfRxUpdateBaudrate(getCrsfDesiredSpeed()); crsfSpeed.isNewSpeedValid = false; isCrsfV3Running = true; } -- 2.11.4.GIT