Skip to main content

Working with STM32

  • This chapter demonstrates examples from the official LoRa USP library
  • For related documentation, please visit the official GitHub: USP Documentation

Hardware Connection

Connect according to the table below:

Core2021-XFSTM32L476RG
CLKPA5
MISOPA6
MOSIPA7
CSPA8
DIO8PA1
RESETPA0
BUSYPB3

Example

  • STM32 example programs are located in the examples/main_examples directory of Lora-net/usp.
  • Testing requires two Core2021-XF modules: one for transmission and one for reception.
  • The following describes several example programs, including usage and operational effects.

main_porting_tests

Example Description

  • Based on STM32L476RG + Core2021-XF, implements porting functionality verification examples
  • Used to test the following:
    • SPI interface functionality
    • Interrupt IRQ response
    • Timers and low‑power modes
    • Random number generation
    • Radio configuration time
    • MCU sleep time
    • Flash emulation storage (optional)
  • Tests cover module initialization, TX/RX configuration, and MCU‑related hardware interfaces
  • Test results are printed via the serial port

Code Analysis

  • Hardware abstraction layers:
    • smtc_modem_hal.h, smtc_hal_mcu.h, smtc_hal_gpio.h, smtc_hal_watchdog.h
    • IRQ callback functions: radio_tx_irq_callback, radio_rx_irq_callback, timer_irq_callback
  • LoRa/Radio parameters:
    • ralf_params_lora_t rx_lora_param / tx_lora_param: simulated RX/TX configurations for porting tests
    • Fixed frequency, symbol rate, bandwidth, sync word, and transmit power
  • Main test functions:
    • porting_test_spi(): Verifies SPI port and chip firmware version
    • porting_test_radio_irq(): Verifies TX/RX interrupt triggering
    • porting_test_get_time(): Checks HAL time‑getting function
    • porting_test_timer_irq(): Verifies timer interrupts
    • porting_test_stop_timer(): Verifies timer stop functionality
    • porting_test_disable_enable_irq(): Verifies interrupt disable/enable logic
    • porting_test_random(): Verifies random number generation and distribution
    • porting_test_config_rx_radio() / porting_test_config_tx_radio(): Checks RX/TX configuration time
    • porting_test_sleep_ms(): Checks MCU sleep time
    • porting_test_timer_irq_low_power(): Verifies timer interrupts in low‑power mode
  • Flash tests (optional): porting_test_flash() / test_context_store_restore()
  • main_porting_tests():
    • Initializes MCU peripherals
    • Disables IRQs to avoid initialization conflicts
    • Runs porting tests in a loop and optionally performs Flash tests based on ENABLE_TEST_FLASH
    • After completion, enters an infinite loop and refreshes the watchdog

Operation Result

  • After power‑on, the serial port prints:


ping_pong_example

Example Description

  • Based on STM32L476RG + Core2021-XF, implements a Ping‑Pong + Periodic Uplink communication example
  • Supports Manager mode triggered by a user button:
    • The Manager initiates a PING
    • The Subordinate automatically responds with a PONG
    • Callbacks are triggered automatically after each transmission or reception
  • Supports periodic uplink (sends a fixed payload "LoRa" every 10 seconds by default)
  • LED indicators for TX/RX status
  • Uses a watchdog to prevent MCU lock‑up
  • Automatically enters low‑power sleep to save energy

Code Analysis

  • MCU initialization:
    • hal_mcu_init(): Initializes MCU peripherals
    • smtc_rac_init(): Initializes the Radio Abstraction Layer
  • User button:
    • user_button_t stores button state and press duration
    • user_button_callback(): Debounces the button and triggers Manager mode
  • LED initialization: SMTC_LED_TX / SMTC_LED_RX
  • Application initialization:
    • ping_pong_init(): Initializes Ping‑Pong transactions
    • periodic_uplink_init(): Initializes periodic uplink
  • Main loop:
    1. Reload watchdog: hal_watchdog_reload()
    2. Run radio transactions: smtc_rac_run_engine()
    3. Process button events: ping_pong_on_button_press()
    4. MCU sleep: determine if there are pending transactions, then call hal_mcu_set_sleep_for_ms(SLEEP_DELAY)
  • Core logic:
    • ping_pong_init(): Sets transaction context, payload, and modulation parameters
    • ping_pong_tx() / ping_pong_rx(): Sends/receives PING or PONG
    • pre_ping_pong_callback() / post_ping_pong_callback():
      • LED control
      • Handles transaction completion status (TX_DONE, RX_PACKET, RX_TIMEOUT, CRC_ERROR, TASK_ABORTED)
      • Automatically retries or restarts
  • Periodic uplink logic: schedules the next transmission via a callback function

Operation Result

  • After power‑on, the serial port prints:


Example Description

  • Based on STM32L476RG + Core2021-XF, implements:
    • LoRaWAN OTAA join
    • Periodic uplink
    • Immediate uplink triggered by a user button
  • Uses example_options.h to provide LoRaWAN credentials:
    • USER_LORAWAN_DEVICE_EUI
    • USER_LORAWAN_JOIN_EUI
    • USER_LORAWAN_GEN_APP_KEY
    • USER_LORAWAN_APP_KEY
  • Region configuration: MODEM_EXAMPLE_REGION (EU868 or WW_2G4)
  • Watchdog and low‑power sleep protect the MCU
  • Serial port prints events, uplink/downlink data, and debug information

Code Analysis

  • Initialization:
    • hal_mcu_init(), hal_gpio_init_in() configure peripherals
    • smtc_rac_init() and smtc_modem_init() initialize the modem, with callback modem_event_callback()
  • User button:
    • user_button_callback() handles button events and triggers an immediate uplink
  • Periodic uplink:
    • periodical_uplink_init() initializes the transaction
    • After each transmission, post_periodic_callback() schedules the next uplink
  • Event callback:
    • modem_event_callback() handles various modem events:
      • RESET: sets credentials and region, initiates Join
      • JOINED: successful join, starts the first periodic uplink
      • ALARM: periodic uplink trigger
      • TXDONE: uplink transmission completed
      • DOWNDATA: downlink data received
      • Other events (LINK_CHECK, CLASS_B_PING_SLOT_INFO, etc.) print debug information
  • Uplink data:
    • 32-bit uplink counter
    • Optional port for transmission (default 101/102)
  • Main loop:
    • Checks the button for immediate uplink
    • Calls smtc_modem_run_engine() and smtc_rac_run_engine() to process transactions
    • MCU sleeps until the next transaction or watchdog reload

Operation Result

  • After power‑on, the serial port prints: