You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

STM32WB LoRaWAN待机后无需重新入网的MIB函数使用求助

STM32WB LoRaWAN: Fixing Re-Join After Standby Wake-Up with MIB Functions

I’ve dealt with exactly this issue on STM32WB-based LoRaWAN devices—standby mode clears the LoRaMAC stack’s network state, forcing a re-join every time you wake up. The key is properly using MIB functions to persist network activation state and session parameters in the backup SRAM (which retains data during standby), then restoring them on wake-up.

First, Let’s Fix Your Current Code’s Critical Issues

Your existing code tries to read the MIB_NETWORK_ACTIVATION state, but there’s a major flaw: you’re calling LoRaMacMibGetRequestConfirm before initializing the LoRaMAC stack. MIB functions rely on the stack being initialized to access parameter storage. Additionally, you aren’t persisting MIB parameters to backup memory before entering standby, so the state gets lost entirely when the device sleeps.

Step-by-Step Correct Implementation

Here’s how to properly use MIB functions to retain network state across standby cycles:

1. Enable Backup SRAM Retention First

Configure the backup domain to keep SRAM powered during standby mode. Add this early in your initialization (run once on power-up):

// Enable access to backup domain and SRAM retention
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
LL_PWR_EnableBkUpSRAM();

2. Initialize LoRaMAC Stack Before Accessing MIB

You must initialize the LoRaWAN stack first, then check the network activation state. Adjust your initialization section like this:

/* USER CODE BEGIN 2 */
// Configure backup SRAM retention (run once on initial power-up)
__HAL_RCC_PWR_CLK_ENABLE();
HAL_PWR_EnableBkUpAccess();
LL_PWR_EnableBkUpSRAM();

/* Reduce system clock and set power scaling */
SystemClock_Decrease();
HAL_PWREx_ControlVoltageScaling(PWR_REGULATOR_VOLTAGE_SCALE2);
HAL_PWREx_EnableLowPowerRunMode();
float iaq;

// Initialize LoRaMAC stack first (critical for MIB access)
lora_init();

LoraFlagStatus join_status = LORA_RESET;
MibRequestConfirm_t mibReq;
mibReq.Type = MIB_NETWORK_ACTIVATION;

// Check existing network activation state
if (LoRaMacMibGetRequestConfirm(&mibReq) == LORAMAC_STATUS_OK) {
  if (mibReq.Param.NetworkActivation == ACTIVATION_TYPE_NONE) {
    // No active network - perform OTAA join
    join_status = lora_join();
    // Save activation state to MIB after successful join
    if (join_status == LORA_SET) {
      MibRequestConfirm_t saveMibReq;
      saveMibReq.Type = MIB_NETWORK_ACTIVATION;
      saveMibReq.Param.NetworkActivation = ACTIVATION_TYPE_OTAA; // Use ACTIVATION_TYPE_ABP if applicable
      LoRaMacMibSetRequestConfirm(&saveMibReq);
    }
  } else {
    // Network already activated - skip join
    join_status = LORA_SET;
  }
} else {
  // MIB read failed - fall back to joining
  join_status = lora_join();
}
/* USER CODE END 2 */

3. Persist Session Parameters Before Entering Standby

Before entering standby, ensure critical LoRaWAN session parameters (like device address, keys) are saved to backup SRAM. You can explicitly save key MIB parameters with a helper function:

// Declare backup SRAM variables (persist through standby)
__attribute__((section(".bckp_sram"))) ActivationType_t saved_network_activation;

void SaveLoRaWANState(void) {
  MibRequestConfirm_t mibReq;
  // Save network activation state to backup SRAM
  mibReq.Type = MIB_NETWORK_ACTIVATION;
  if (LoRaMacMibGetRequestConfirm(&mibReq) == LORAMAC_STATUS_OK) {
    saved_network_activation = mibReq.Param.NetworkActivation;
  }
  // Add more parameters to save if needed: DEVICE_ADDRESS, NWK_SKEY, APP_SKEY
}

// Call this right before entering standby:
SaveLoRaWANState();

4. Restore State on Wake-Up

When waking from standby, reinitialize the stack and restore the saved MIB state to skip re-joining:

// In your wake-up handling code:
if( (__HAL_PWR_GET_FLAG(PWR_FLAG_SB) != RESET) && (__HAL_PWR_GET_FLAG(PWR_FLAG_C2SB) != RESET) ) {
  /* Clear standby flags */
  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_SB);
  __HAL_PWR_CLEAR_FLAG(PWR_FLAG_C2SB);

  // Re-initialize LoRaMAC stack
  lora_init();

  // Restore saved network activation state
  MibRequestConfirm_t restoreMibReq;
  restoreMibReq.Type = MIB_NETWORK_ACTIVATION;
  restoreMibReq.Param.NetworkActivation = saved_network_activation;
  LoRaMacMibSetRequestConfirm(&restoreMibReq);

  // Mark as already joined - skip join process
  join_status = LORA_SET;
}

Key STM32WB-Specific Notes

  • Dual-CPU Sync: Both CPU1 (application) and CPU2 (RF stack) must enter standby. Your existing code sets CPU2 to shutdown mode, which is correct—ensure the RF stack is suspended before entering standby.
  • Linker Script Configuration: Make sure your linker script reserves the .bckp_sram section in the backup SRAM region (check your STM32WB reference manual for address ranges).
  • SDK Consistency: Different STM32WB LoRaWAN SDK versions may have minor MIB function variations—stick to the functions provided in your SDK’s LoRaMacMib.h header.

By following these steps, your device will retain its network activation state across standby wake-ups and avoid unnecessary re-joins.

内容的提问来源于stack exchange,提问作者Sidka

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.08 08:47:48