Skip to content

Commit

Permalink
Everyone uses EnterBindingModeSafely() now
Browse files Browse the repository at this point in the history
  • Loading branch information
CapnBry committed Jan 27, 2024
1 parent 46720d6 commit 8b0e7c2
Show file tree
Hide file tree
Showing 6 changed files with 92 additions and 93 deletions.
1 change: 1 addition & 0 deletions src/include/common.h
Original file line number Diff line number Diff line change
Expand Up @@ -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
3 changes: 1 addition & 2 deletions src/lib/LUA/tx_devLUA.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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)
{
Expand Down
3 changes: 1 addition & 2 deletions src/lib/SCREEN/menu.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ extern Thermal thermal;

extern FiniteStateMachine state_machine;

extern void EnterBindingMode();
extern bool RxWiFiReadyToSend;
extern bool TxBackpackWiFiReadyToSend;
extern bool VRxBackpackWiFiReadyToSend;
Expand Down Expand Up @@ -435,7 +434,7 @@ static void executeBind(bool init)
{
if (init)
{
EnterBindingMode();
EnterBindingModeSafely();
display->displayBindStatus();
return;
}
Expand Down
3 changes: 1 addition & 2 deletions src/src/rx-serial/SerialCRSF.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -133,7 +132,7 @@ void SerialCRSF::processBytes(uint8_t *bytes, uint16_t size)
}
if (telemetry.ShouldCallEnterBind())
{
EnterBindingMode();
EnterBindingModeSafely();
}
if (telemetry.ShouldCallUpdateModelMatch())
{
Expand Down
164 changes: 79 additions & 85 deletions src/src/rx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -1537,7 +1610,6 @@ static void EnterBindingModeSafely()

EnterBindingMode();
}
#endif /* HAS_BUTTON */

static void checkSendLinkStatsToFc(uint32_t now)
{
Expand Down Expand Up @@ -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);
}
11 changes: 9 additions & 2 deletions src/src/tx_main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -939,7 +939,7 @@ void SendUIDOverMSP()
MspSender.SetDataToTransmit(MSPDataPackage, 5);
}

void EnterBindingMode()
static void EnterBindingMode()
{
if (InBindingMode)
return;
Expand Down Expand Up @@ -970,7 +970,7 @@ void EnterBindingMode()
DBGLN("Entered binding mode at freq = %d", Radio.currFreq);
}

void ExitBindingMode()
static void ExitBindingMode()
{
if (!InBindingMode)
return;
Expand All @@ -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)
Expand Down

0 comments on commit 8b0e7c2

Please sign in to comment.