From 8b0e7c24dccc63180725920f647e6cc294348b3d Mon Sep 17 00:00:00 2001 From: Bryan Mayland Date: Fri, 26 Jan 2024 21:50:36 -0500 Subject: [PATCH] Everyone uses EnterBindingModeSafely() now --- src/include/common.h | 1 + src/lib/LUA/tx_devLUA.cpp | 3 +- src/lib/SCREEN/menu.cpp | 3 +- src/src/rx-serial/SerialCRSF.cpp | 3 +- src/src/rx_main.cpp | 164 +++++++++++++++---------------- src/src/tx_main.cpp | 11 ++- 6 files changed, 92 insertions(+), 93 deletions(-) diff --git a/src/include/common.h b/src/include/common.h index db9f50f6ce..04445dcfc6 100644 --- a/src/include/common.h +++ b/src/include/common.h @@ -251,3 +251,4 @@ extern uint32_t ChannelData[CRSF_NUM_CHANNELS]; // Current state of channels, CR uint32_t uidMacSeedGet(); bool isDualRadio(); +void EnterBindingModeSafely(); // defined in rx_main/tx_main \ No newline at end of file diff --git a/src/lib/LUA/tx_devLUA.cpp b/src/lib/LUA/tx_devLUA.cpp index 7d4bc27dd5..4deb51549a 100644 --- a/src/lib/LUA/tx_devLUA.cpp +++ b/src/lib/LUA/tx_devLUA.cpp @@ -276,7 +276,6 @@ extern void VtxTriggerSend(); extern void ResetPower(); extern uint8_t adjustPacketRateForBaud(uint8_t rate); extern void SetSyncSpam(); -extern void EnterBindingMode(); extern bool RxWiFiReadyToSend; #if defined(USE_TX_BACKPACK) extern bool TxBackpackWiFiReadyToSend; @@ -428,7 +427,7 @@ static void luahandSimpleSendCmd(struct luaPropertiesCommon *item, uint8_t arg) if ((void *)item == (void *)&luaBind) { msg = "Binding..."; - EnterBindingMode(); + EnterBindingModeSafely(); } else if ((void *)item == (void *)&luaVtxSend) { diff --git a/src/lib/SCREEN/menu.cpp b/src/lib/SCREEN/menu.cpp index f37fcdec15..583b192f00 100644 --- a/src/lib/SCREEN/menu.cpp +++ b/src/lib/SCREEN/menu.cpp @@ -17,7 +17,6 @@ extern Thermal thermal; extern FiniteStateMachine state_machine; -extern void EnterBindingMode(); extern bool RxWiFiReadyToSend; extern bool TxBackpackWiFiReadyToSend; extern bool VRxBackpackWiFiReadyToSend; @@ -435,7 +434,7 @@ static void executeBind(bool init) { if (init) { - EnterBindingMode(); + EnterBindingModeSafely(); display->displayBindStatus(); return; } diff --git a/src/src/rx-serial/SerialCRSF.cpp b/src/src/rx-serial/SerialCRSF.cpp index aa88c418b7..551471907b 100644 --- a/src/src/rx-serial/SerialCRSF.cpp +++ b/src/src/rx-serial/SerialCRSF.cpp @@ -11,7 +11,6 @@ extern MSP2CROSSFIRE msp2crsf; extern Telemetry telemetry; extern void reset_into_bootloader(); -extern void EnterBindingMode(); extern void UpdateModelMatch(uint8_t model); void SerialCRSF::sendQueuedData(uint32_t maxBytesToSend) @@ -133,7 +132,7 @@ void SerialCRSF::processBytes(uint8_t *bytes, uint16_t size) } if (telemetry.ShouldCallEnterBind()) { - EnterBindingMode(); + EnterBindingModeSafely(); } if (telemetry.ShouldCallUpdateModelMatch()) { diff --git a/src/src/rx_main.cpp b/src/src/rx_main.cpp index 4929c9a2ea..8a4c7078ec 100644 --- a/src/src/rx_main.cpp +++ b/src/src/rx_main.cpp @@ -214,10 +214,6 @@ static uint8_t debugRcvrLinkstatsFhssIdx; bool BindingModeRequest = false; -void reset_into_bootloader(void); -void EnterBindingMode(); -void ExitBindingMode(); -void OnELRSBindMSP(uint8_t* packet); extern void setWifiUpdateMode(); uint8_t getLq() @@ -855,6 +851,22 @@ static void ICACHE_RAM_ATTR ProcessRfPacket_RC(OTA_Packet_s const * const otaPkt } } +void ICACHE_RAM_ATTR OnELRSBindMSP(uint8_t* newUid4) +{ + UID[0] = 0; + UID[1] = 0; + for (int i = 0; i < 4; i++) + { + UID[i + 2] = newUid4[i]; + } + + DBGLN("New UID = %d, %d, %d, %d, %d, %d", UID[0], UID[1], UID[2], UID[3], UID[4], UID[5]); + + // Set new UID in eeprom + // EEPROM commit will happen on the main thread in ExitBindingMode() + config.SetUID(UID); +} + static void ICACHE_RAM_ATTR ProcessRfPacket_MSP(OTA_Packet_s const * const otaPktPtr) { uint8_t packageIndex; @@ -1465,6 +1477,68 @@ static void cycleRfMode(unsigned long now) } // if time to switch RF mode } +static void EnterBindingMode() +{ + if (InBindingMode) + { + DBGLN("Already in binding mode"); + return; + } + + // Binding uses a CRCInit=0, 50Hz, and InvertIQ + OtaCrcInitializer = 0; + InBindingMode = true; + + // Start attempting to bind + // Lock the RF rate and freq while binding + SetRFLinkRate(enumRatetoIndex(RATE_BINDING), true); + Radio.SetFrequencyReg(GetInitialFreq()); + if (geminiMode) + { + Radio.SetFrequencyReg(FHSSgetInitialGeminiFreq(), SX12XX_Radio_2); + } + // If the Radio Params (including InvertIQ) parameter changed, need to restart RX to take effect + Radio.RXnb(); + + DBGLN("Entered binding mode at freq = %d", Radio.currFreq); + devicesTriggerEvent(); +} + +static void ExitBindingMode() +{ + if (!InBindingMode) + { + DBGLN("Not in binding mode"); + return; + } + + MspReceiver.ResetState(); + + // Prevent any new packets from coming in + Radio.SetTxIdleMode(); + // Write the values to eeprom + config.Commit(); + + OtaUpdateCrcInitFromUid(); + FHSSrandomiseFHSSsequence(uidMacSeedGet()); + + #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266) + webserverPreventAutoStart = true; + #endif + + // Force RF cycling to start at the beginning immediately + scanIndex = RATE_MAX; + RFmodeLastCycled = 0; + LockRFmode = false; + LostConnection(false); + + // Do this last as LostConnection() will wait for a tock that never comes + // if we're in binding mode + InBindingMode = false; + DBGLN("Exiting binding mode"); + devicesTriggerEvent(); +} + static void updateBindingMode() { // Exit binding mode if the config has been modified, indicating UID has been set @@ -1505,8 +1579,7 @@ static void updateBindingMode() } } -#if defined(HAS_BUTTON) -static void EnterBindingModeSafely() +void EnterBindingModeSafely() { // Will not enter Binding mode if in the process of a passthrough update // or currently binding @@ -1537,7 +1610,6 @@ static void EnterBindingModeSafely() EnterBindingMode(); } -#endif /* HAS_BUTTON */ static void checkSendLinkStatsToFc(uint32_t now) { @@ -1890,81 +1962,3 @@ void reset_into_bootloader(void) connectionState = serialUpdate; #endif } - -void EnterBindingMode() -{ - if (InBindingMode) - { - DBGLN("Already in binding mode"); - return; - } - - // Binding uses a CRCInit=0, 50Hz, and InvertIQ - OtaCrcInitializer = 0; - InBindingMode = true; - - // Start attempting to bind - // Lock the RF rate and freq while binding - SetRFLinkRate(enumRatetoIndex(RATE_BINDING), true); - Radio.SetFrequencyReg(GetInitialFreq()); - if (geminiMode) - { - Radio.SetFrequencyReg(FHSSgetInitialGeminiFreq(), SX12XX_Radio_2); - } - // If the Radio Params (including InvertIQ) parameter changed, need to restart RX to take effect - Radio.RXnb(); - - DBGLN("Entered binding mode at freq = %d", Radio.currFreq); - devicesTriggerEvent(); -} - -void ExitBindingMode() -{ - if (!InBindingMode) - { - DBGLN("Not in binding mode"); - return; - } - - MspReceiver.ResetState(); - - // Prevent any new packets from coming in - Radio.SetTxIdleMode(); - // Write the values to eeprom - config.Commit(); - - OtaUpdateCrcInitFromUid(); - FHSSrandomiseFHSSsequence(uidMacSeedGet()); - - #if defined(PLATFORM_ESP32) || defined(PLATFORM_ESP8266) - webserverPreventAutoStart = true; - #endif - - // Force RF cycling to start at the beginning immediately - scanIndex = RATE_MAX; - RFmodeLastCycled = 0; - LockRFmode = false; - LostConnection(false); - - // Do this last as LostConnection() will wait for a tock that never comes - // if we're in binding mode - InBindingMode = false; - DBGLN("Exiting binding mode"); - devicesTriggerEvent(); -} - -void ICACHE_RAM_ATTR OnELRSBindMSP(uint8_t* newUid4) -{ - UID[0] = 0; - UID[1] = 0; - for (int i = 0; i < 4; i++) - { - UID[i + 2] = newUid4[i]; - } - - DBGLN("New UID = %d, %d, %d, %d, %d, %d", UID[0], UID[1], UID[2], UID[3], UID[4], UID[5]); - - // Set new UID in eeprom - // EEPROM commit will happen on the main thread in ExitBindingMode() - config.SetUID(UID); -} diff --git a/src/src/tx_main.cpp b/src/src/tx_main.cpp index 7d82c94595..54b848f192 100644 --- a/src/src/tx_main.cpp +++ b/src/src/tx_main.cpp @@ -939,7 +939,7 @@ void SendUIDOverMSP() MspSender.SetDataToTransmit(MSPDataPackage, 5); } -void EnterBindingMode() +static void EnterBindingMode() { if (InBindingMode) return; @@ -970,7 +970,7 @@ void EnterBindingMode() DBGLN("Entered binding mode at freq = %d", Radio.currFreq); } -void ExitBindingMode() +static void ExitBindingMode() { if (!InBindingMode) return; @@ -986,6 +986,13 @@ void ExitBindingMode() DBGLN("Exiting binding mode"); } +void EnterBindingModeSafely() +{ + // TX can always enter binding mode safely as the function handles stopping the transmitter + EnterBindingMode(); +} + + void ProcessMSPPacket(uint32_t now, mspPacket_t *packet) { #if !defined(CRITICAL_FLASH)