Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make help in lbm_lib describes wrong way to select radio #103

Open
djObsidian opened this issue Mar 5, 2025 · 2 comments
Open

Make help in lbm_lib describes wrong way to select radio #103

djObsidian opened this issue Mar 5, 2025 · 2 comments

Comments

@djObsidian
Copy link

djObsidian commented Mar 5, 2025

Update:

This appears to be an issue with makefile of lbm_lib. make help provides wrong instruction for specifying radio chip. Possible correct way is described in third comment in this issue.

Description:

I'm working on integrating the LoRa Basics Modem (LBM) with an LLCC68 (can be threatend as SX1262) chip on an STM32L071 platform via STM32CubeIDE. My implementation of sx126x_hal.c, ral_sx126x_bsp.c and smtc_modem_hal.c are basically identical to such provided ine example, but written with HAL instead of LL. Initially i patched example to run with my board, this boils down to just different pinout and using software random instead of TRNG. And it works fine, it sends packets, sucsessfully joins network and sends counter. However my attempt to recreate the same in CubeIDE fails at sending join request for unknown reason. In order to debug this I added some trace prints:

  1. In main_periodical_uplink():
smtc_modem_init( &modem_event_callback );
SMTC_HAL_TRACE_INFO( "Modem started! \n" );
SMTC_HAL_TRACE_INFO( "Periodical uplink (%d sec) example is starting \n", PERIODICAL_UPLINK_DELAY_S );
while( 1 )
    {
        // Modem process launch
        sleep_time_ms = smtc_modem_run_engine( );
        smtc_modem_status_mask_t status_mask = 0;
        smtc_modem_get_status( STACK_ID, &status_mask );
        SMTC_HAL_TRACE_INFO("Status: %d \n", status_mask);
        SMTC_HAL_TRACE_INFO("Modem engine sleep time: %d ms \n", MIN( sleep_time_ms, WATCHDOG_RELOAD_PERIOD_MS ));

        // Atomically check sleep conditions (button was not pressed and no modem flags pending)
        hal_mcu_disable_irq( );
        if( ( user_button_is_press == false ) && ( smtc_modem_is_irq_flag_pending( ) == false ) )
        {
            hal_watchdog_reload( );
            hal_mcu_set_sleep_for_ms( MIN( sleep_time_ms, WATCHDOG_RELOAD_PERIOD_MS ) );
            if (sleep_time_ms > 0) {
                SMTC_HAL_TRACE_INFO("Sleep ended normaly!\n");
            }
        }
        hal_watchdog_reload( );
        hal_mcu_enable_irq( );
    }
}
  1. In sx126x_hal_write and sx126x_hal_read:
if (command_length > 0 ) SMTC_HAL_TRACE_ARRAY("W-C: ",command, command_length);
if (data_length > 0 ) SMTC_HAL_TRACE_ARRAY("W-D: ",data, data_length);
if (command_length > 0 ) SMTC_HAL_TRACE_ARRAY("WR-C: ",command, command_length);
if (data_length > 0 ) SMTC_HAL_TRACE_ARRAY("WR-D: ",data, data_length);
  1. In smtc_modem_hal_start_timer with ms and calculated ticks and timer IRQ callback with text TCB!

Log form patched example:

INFO: Modem Initialization
11:08:09:768 -> WR-C:  - (4 bytes):
11:08:09:781 ->  1D 02 9F 00
11:08:09:786 -> WR-D:  - (9 bytes):
11:08:09:786 ->  00 29 02 40 A7 1A 9D D8 4D
11:08:09:789 -> W-C:  - (3 bytes):
11:08:09:789 ->  0D 02 9F
11:08:09:794 -> W-D:  - (9 bytes):
11:08:09:794 ->  03 08 AC 08 89 07 36 D8 4D
11:08:09:796 -> W-C:  - (2 bytes):
11:08:09:796 ->  96 01
11:08:09:799 -> W-C:  - (2 bytes):
11:08:09:799 ->  9D 01
11:08:09:802 -> W-C:  - (3 bytes):
11:08:09:802 ->  0D 08 AC
11:08:09:804 -> W-D:  - (1 bytes):
11:08:09:804 ->  94
11:08:09:806 -> W-C:  - (2 bytes):
11:08:09:806 ->  84 04
11:08:09:813 -> INFO: Use soft secure element for cryptographic functionalities
11:08:09:813 -> stack_id 0
11:08:09:814 ->  DevNonce = 72
11:08:09:822 ->  JoinNonce = 0xff ff ff, NetID = 0xff ff ff
11:08:09:822 ->  Region = EU868
11:08:09:826 -> LoRaWAN Certification is disabled on stack 0
11:08:09:829 -> INFO: Modem event callback
11:08:09:829 -> Modem event callback
11:08:09:836 -> INFO: Event received: RESET
11:08:09:836 -> INFO: smtc_modem_join_network
11:08:09:842 ->  Start a new join sequence in 2 seconds on stack 0
11:08:09:848 -> INFO: Modem started! 
11:08:09:848 -> INFO: Periodical uplink (60 sec) example is starting 
11:08:09:854 -> INFO: Status: 0 
11:08:09:857 -> INFO: Modem engine sleep time: 2000 ms 
11:08:09:861 -> INFO: Sleep ended normaly!
11:08:11:866 -> DevEUI - (8 bytes):
11:08:11:871 ->  CA F6 FD 91 E4 B9 F0 58
11:08:11:876 -> JoinEUI - (8 bytes):
11:08:11:876 ->  58 77 82 C6 90 3E A7 B5
11:08:11:878 -> DevNonce 0x4b, stack_id 0
11:08:11:882 -> INFO: Status: 64 
11:08:11:882 -> INFO: Modem engine sleep time: 0 ms 
11:08:11:886 -> 
11:08:11:890 ->   *************************************
11:08:11:890 ->   * Send Payload  for stack_id = 0
11:08:11:897 ->   *************************************
11:08:11:905 ->   Tx  LoRa at 7719 ms: freq:868300000, SF10, BW125, len 23 bytes 14 dBm, fcnt_up 0, toa = 371
11:08:11:910 -> INFO: Starting timer with 5569 ms!
11:08:11:910 -> INFO: Ticks 11405
11:08:11:912 -> INFO: Status: 64 
11:08:11:919 -> INFO: Modem engine sleep time: 20000 ms 
11:08:11:923 -> INFO: Sleep ended normaly!
11:08:17:494 -> INFO: TCB!
11:08:17:494 -> W-C:  - (2 bytes):
11:08:17:496 ->  9F 00
11:08:17:499 -> W-C:  - (2 bytes):
11:08:17:499 ->  A0 00
11:08:17:501 -> W-C:  - (2 bytes):
11:08:17:501 ->  8A 01
11:08:17:505 -> W-C:  - (5 bytes):
11:08:17:505 ->  86 36 44 CC CD
11:08:17:508 -> WR-C:  - (4 bytes):
11:08:17:508 ->  1D 08 D8 00
11:08:17:511 -> WR-D:  - (1 bytes):
11:08:17:511 ->  C8
11:08:17:514 -> W-C:  - (3 bytes):
11:08:17:514 ->  0D 08 D8
11:08:17:516 -> W-D:  - (1 bytes):
11:08:17:516 ->  DE
11:08:17:519 -> W-C:  - (5 bytes):
11:08:17:519 ->  95 04 07 00 01
11:08:17:522 -> W-C:  - (3 bytes):
11:08:17:522 ->  8E 0E 02
11:08:17:526 -> W-C:  - (5 bytes):
11:08:17:526 ->  8B 0A 04 01 00
11:08:17:529 -> WR-C:  - (4 bytes):
11:08:17:529 ->  1D 08 89 00
11:08:17:531 -> WR-D:  - (1 bytes):
11:08:17:531 ->  04
11:08:17:538 -> W-C:  - (3 bytes):
11:08:17:538 ->  0D 08 89
11:08:17:539 -> W-D:  - (1 bytes):
11:08:17:539 ->  04
11:08:17:540 -> W-C:  - (7 bytes):
11:08:17:540 ->  8C 00 08 00 17 01 00
11:08:17:544 -> WR-C:  - (4 bytes):
11:08:17:544 ->  1D 07 36 00
11:08:17:546 -> WR-D:  - (1 bytes):
11:08:17:546 ->  0D
11:08:17:549 -> W-C:  - (3 bytes):
11:08:17:549 ->  0D 07 36
11:08:17:551 -> W-D:  - (1 bytes):
11:08:17:551 ->  0D
11:08:17:554 -> WR-C:  - (4 bytes):
11:08:17:554 ->  1D 07 40 00
11:08:17:557 -> WR-D:  - (2 bytes):
11:08:17:557 ->  14 24
11:08:17:560 -> W-C:  - (3 bytes):
11:08:17:560 ->  0D 07 40
11:08:17:563 -> W-D:  - (2 bytes):
11:08:17:563 ->  34 44
11:08:17:567 -> W-C:  - (9 bytes):
11:08:17:567 ->  08 43 FF 00 01 00 00 00 00
11:08:17:570 -> W-C:  - (3 bytes):
11:08:17:570 ->  8F 00 00
11:08:17:572 -> W-C:  - (2 bytes):
11:08:17:572 ->  0E 00
11:08:17:582 -> W-D:  - (23 bytes):
11:08:17:582 ->  00 B5 A7 3E 90 C6 82 77 58 58 F0 B9 E4 91 FD F6
11:08:17:582 ->  CA 4B 00 D6 6F 23 53
11:08:17:585 -> W-C:  - (4 bytes):
11:08:17:585 ->  83 00 00 00
11:08:17:588 -> INFO: Status: 64 
11:08:17:588 -> INFO: Modem engine sleep time: 20000 ms 
11:08:17:593 -> INFO: Sleep ended normaly!
11:08:17:962 -> WR-C:  - (2 bytes):
11:08:17:965 ->  12 00
11:08:17:967 -> WR-D:  - (2 bytes):
11:08:17:967 ->  00 01
11:08:17:970 -> W-C:  - (3 bytes):
11:08:17:970 ->  02 00 01
11:08:17:973 -> W-C:  - (2 bytes):
11:08:17:973 ->  84 04
11:08:17:981 -> 
11:08:17:981 ->   *************************************
11:08:17:981 ->   *  TX DONE
11:08:17:981 ->   *************************************

Log from my example:

INFO: Modem Initialization
12:27:18:314 -> WR-C:  - (4 bytes):
12:27:18:328 ->  1D 02 9F 00
12:27:18:333 -> WR-D:  - (9 bytes):
12:27:18:333 ->  00 29 0A 40 87 1A 9D D8 4D
12:27:18:335 -> W-C:  - (3 bytes):
12:27:18:335 ->  0D 02 9F
12:27:18:340 -> W-D:  - (9 bytes):
12:27:18:340 ->  03 08 AC 08 89 07 36 D8 4D
12:27:18:343 -> W-C:  - (2 bytes):
12:27:18:343 ->  96 01
12:27:18:345 -> W-C:  - (2 bytes):
12:27:18:345 ->  9D 01
12:27:18:348 -> W-C:  - (3 bytes):
12:27:18:348 ->  0D 08 AC
12:27:18:350 -> W-D:  - (1 bytes):
12:27:18:350 ->  94
12:27:18:353 -> W-C:  - (2 bytes):
12:27:18:353 ->  84 04
12:27:18:361 -> INFO: Use soft secure element for cryptographic functionalities
12:27:18:361 -> stack_id 0
12:27:18:363 ->  DevNonce = 84
12:27:18:368 ->  JoinNonce = 0xff ff ff, NetID = 0xff ff ff
12:27:18:368 ->  Region = EU868
12:27:18:375 -> LoRaWAN Certification is disabled on stack 0
12:27:18:378 -> INFO: Modem event callback
12:27:18:378 -> Modem event callback
12:27:18:384 -> INFO: Event received: RESET
12:27:18:384 -> INFO: smtc_modem_join_network
12:27:18:390 ->  Start a new join sequence now on stack 0
12:27:18:396 -> INFO: Modem started! 
12:27:18:396 -> INFO: Periodical uplink (60 sec) example is starting 
12:27:18:402 -> DevEUI - (8 bytes):
12:27:18:408 ->  CA F6 FD 91 E4 B9 F0 58
12:27:18:412 -> JoinEUI - (8 bytes):
12:27:18:412 ->  58 77 82 C6 90 3E A7 B5
12:27:18:415 -> DevNonce 0x55, stack_id 0
12:27:18:418 -> INFO: Status: 64 
12:27:18:418 -> INFO: Modem engine sleep time: 0 ms 
12:27:18:426 -> 
12:27:18:427 ->   *************************************
12:27:18:427 ->   * Send Payload  for stack_id = 0
12:27:18:433 ->   *************************************
12:27:18:442 ->   Tx  LoRa at 33101 ms: freq:868100000, SF9, BW125, len 23 bytes 14 dBm, fcnt_up 0, toa = 206
12:27:18:446 -> INFO: Starting timer with 5610 ms!
12:27:18:446 -> INFO: Ticks 11489
12:27:18:449 -> INFO: lptim ticks after start: 0 
12:27:18:457 -> INFO: Status: 64 
12:27:18:459 -> INFO: Modem engine sleep time: 20000 ms 
12:27:18:464 -> INFO: TCB!
12:27:24:066 -> INFO: Sleep ended normaly!
12:27:24:069 -> INFO: Starting timer with 2 ms!
12:27:24:073 -> INFO: Ticks 4
12:27:24:076 -> INFO: lptim ticks after start: 0 
12:27:24:083 -> INFO: Status: 64 
12:27:24:086 -> INFO: Modem engine sleep time: 20000 ms 
12:27:24:091 -> INFO: Starting timer with 2 ms!
12:27:24:095 -> INFO: Ticks 4
12:27:24:100 -> INFO: lptim ticks after start: 0 
12:27:24:105 -> INFO: Status: 64

Expected behavior:

After initialization modem should send join packed to transceiver over SPI

Investigation:

  • In both cases, hal_mcu_set_sleep_for_ms behaves differently than intuitively expected. After Send Payload message you can see Modem engine sleep time: 20000 ms. However, in example the mcu wakes up almost instantly and right after that timer callback fires. In my implementation mcu also doesn't stays in sleep for 20 seconds, but calback fires during sleep and does nothing exept for sending info trace about that.
  • It seems like during sleep in example MCU receives wake up event (from lptim?) and thus sleep is terminated. However interrupt is tirggered only after wakeup, as you can see TCB! only after Sleep ended normaly!
  • It seems like in my case timer interrupt is triggered during sleep but doesn't wake up the MCU from sleep.... even though the uart (i'm not using lpuart) is on and working, TCB! is being sent. However why sx126x_hal write and read functions aren't called remains unclear.
  • It doesn't seems to be a hardware configuration issue. I double checked clock settings, enabled interrupts in my implementation and example, they are identical.

Additional info and questions:

  • What is the purpose of hal_mcu_disable_irq( ); and hal_mcu_enable_irq( ); before and after sleep in example? I see in the docs

The current design of the LoRa Basics Modem has only been tested in the case where the provided callback is executed in an interrupt context, with interrupts disabled.

But doesn't modem handles it by itself with smtc_modem_hal_disable_modem_irq?

  • Does the lpm_enter_sleep_mode function put the MCU in SLEEP or STOP mode?
  • My implementation of hal_mcu_set_sleep_for_ms and modem_hal_start_timer are as follows:
void hal_mcu_set_sleep_for_ms(int32_t milliseconds) {
    if (milliseconds <= 0) {
        return;
    }
    uint32_t delay_ms_2_tick = milliseconds * 2048 / 1000;
    HAL_SuspendTick();
    HAL_RTCEx_SetWakeUpTimer_IT(&hrtc, delay_ms_2_tick, RTC_WAKEUPCLOCK_RTCCLK_DIV16);
    HAL_PWR_EnterSLEEPMode(PWR_LOWPOWERREGULATOR_ON, PWR_SLEEPENTRY_WFI);
    HAL_RTCEx_DeactivateWakeUpTimer(&hrtc);
    HAL_ResumeTick();
}

void modem_hal_start_timer(uint32_t milliseconds, const hal_lp_timer_irq_t* tmr_irq) {
    HAL_LPTIM_TimeOut_Stop_IT(hal_ctx->lp_timer);
    SMTC_HAL_TRACE_INFO("Starting timer with %d ms!\n", milliseconds);
    uint32_t ticks = (milliseconds * 2048) / 1000; 
    if (ticks > 0xFFFF) {
        ticks = 0xFFFF; 
    }
    SMTC_HAL_TRACE_INFO("Ticks %d\n", ticks);
    lptim_tmr_irq = *tmr_irq;
    HAL_LPTIM_TimeOut_Start_IT(hal_ctx->lp_timer, 0xFFFF, ticks);
    __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_IT(); 
    __HAL_LPTIM_WAKEUPTIMER_EXTI_ENABLE_EVENT();
    SMTC_HAL_TRACE_INFO("lptim ticks after start: %d \n", HAL_LPTIM_ReadCounter(hal_ctx->lp_timer));
}

At this point I'm not sure if the problem is the way I'm using the lbm_lib library or if I've made a mistake somewhere with the STM32 HAL. I would appreciate any help or advice on this problem.

@djObsidian
Copy link
Author

On a follow-up note: adding disable_irq() and enable_irq() to my code matches the behavior with what is in the example, but sx126x_hal is still not called

14:44:04:820 -> INFO: Starting timer with 5610 ms!
14:44:04:820 -> INFO: Ticks 11489
14:44:04:823 -> INFO: lptim ticks after start: 0 
14:44:04:831 -> INFO: Status: 64 
14:44:04:833 -> INFO: Modem engine sleep time: 20000 ms 
14:44:04:838 -> INFO: Sleep ended normaly!
14:44:10:444 -> INFO: TCB!
14:44:10:444 -> INFO: Starting timer with 2 ms!
14:44:10:448 -> INFO: Ticks 4

@djObsidian
Copy link
Author

Oh, it was actually wrong make command. According to make help in lbm_lib the correct way to specify radio is

   ----------------------------- Compilation ----------------------------------
|   * make basic_modem_<TARGET> MCU_FLAGS=xxx : build basic_modem on a given target with chosen mcu flags
|   *                                           MCU_FLAGS are mandatory. Ex for stm32l4:
|   *                                           MCU_FLAGS="-mcpu=cortex-m4 -mthumb -mabi=aapcs -mfpu=fpv4-sp-d16 -mfloat-abi=hard"

So my make command was following:
make basic_modem_sx1262 MCU_FLAGS="-mcpu=cortex-m0plus -mabi=aapcs -mthumb" VERBOSE=yes REGION=EU_868
but looking at logs produced by make example I see radio specified in a different way:
make -C ../lbm_lib/. basic_modem RADIO=sx1262 MODEM_TRACE=yes MCU_FLAGS="-mcpu=cortex-m0plus -mabi=aapcs -mthumb" EXTRAFLAGS=""-DRP_MARGIN_DELAY=10"" OPT="-Og" CRYPTO=SOFT NB_OF_STACK=1 LBM_CSMA=no USE_CSMA_BY_DEFAULT=no VERBOSE=no -j
And yes, passing radio with RADIO parameter makes it work fine, while passing it as part of targert doesn't and leads to behavior described above.

@djObsidian djObsidian changed the title [Help wanted] Porting periodical_uplink to STM32 HAL, no join TX Make help in lbm_lib describes wrong way to select radio Mar 5, 2025
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant