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-XF | STM32L476RG |
|---|---|
| CLK | PA5 |
| MISO | PA6 |
| MOSI | PA7 |
| CS | PA8 |
| DIO8 | PA1 |
| RESET | PA0 |
| BUSY | PB3 |
Example
- STM32 example programs are located in the
examples/main_examplesdirectory 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 versionporting_test_radio_irq(): Verifies TX/RX interrupt triggeringporting_test_get_time(): Checks HAL time‑getting functionporting_test_timer_irq(): Verifies timer interruptsporting_test_stop_timer(): Verifies timer stop functionalityporting_test_disable_enable_irq(): Verifies interrupt disable/enable logicporting_test_random(): Verifies random number generation and distributionporting_test_config_rx_radio()/porting_test_config_tx_radio(): Checks RX/TX configuration timeporting_test_sleep_ms(): Checks MCU sleep timeporting_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 peripheralssmtc_rac_init(): Initializes the Radio Abstraction Layer
- User button:
user_button_tstores button state and press durationuser_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 transactionsperiodic_uplink_init(): Initializes periodic uplink
- Main loop:
- Reload watchdog:
hal_watchdog_reload() - Run radio transactions:
smtc_rac_run_engine() - Process button events:
ping_pong_on_button_press() - MCU sleep: determine if there are pending transactions, then call
hal_mcu_set_sleep_for_ms(SLEEP_DELAY)
- Reload watchdog:
- Core logic:
ping_pong_init(): Sets transaction context, payload, and modulation parametersping_pong_tx()/ping_pong_rx(): Sends/receives PING or PONGpre_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:

main_periodical_uplink
Example Description
- Based on STM32L476RG + Core2021-XF, implements:
- LoRaWAN OTAA join
- Periodic uplink
- Immediate uplink triggered by a user button
- Uses
example_options.hto provide LoRaWAN credentials:USER_LORAWAN_DEVICE_EUIUSER_LORAWAN_JOIN_EUIUSER_LORAWAN_GEN_APP_KEYUSER_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 peripheralssmtc_rac_init()andsmtc_modem_init()initialize the modem, with callbackmodem_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 JoinJOINED: successful join, starts the first periodic uplinkALARM: periodic uplink triggerTXDONE: uplink transmission completedDOWNDATA: 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()andsmtc_rac_run_engine()to process transactions - MCU sleeps until the next transaction or watchdog reload
Operation Result
- After power‑on, the serial port prints:
