Skip to content

STM32 Example Code

Note: These are legacy C reference examples. For new development, use the Rust SDK examples with the Rust SDK instead.

C example code for using the Xylolabs API on STM32 microcontrollers.

File Structure

File Description
f411_audio_example.c STM32F411 audio + sensors with XAP codec, LTE-M1 modem
f103_sensor_example.c STM32F103 sensor-only node, ADPCM fallback, LTE-M1 modem
wb55_wireless_example.c STM32WB55 BLE sensor node, XMBP over BLE to gateway

Design Principles

HAL-Based, CubeMX Compatible

All examples use the STM32 HAL library generated by STM32CubeMX. No LL (low-level) register access is required. This makes the code portable across STM32 families with minimal changes to peripheral initialization code.

// HAL usage example
HAL_I2S_Receive_DMA(&hi2s1, (uint16_t *)dma_buf, DMA_BUF_SAMPLES);
HAL_UART_Transmit(&huart1, at_cmd, strlen(at_cmd), HAL_MAX_DELAY);
int16_t adc_raw = HAL_ADC_GetValue(&hadc1);

Static Memory

No malloc/free is used. All buffers are static globals sized at compile time. STM32 targets have limited RAM (F103: 20KB, F411: 128KB, WB55: 256KB); static allocation prevents heap fragmentation in long-running deployments.

XAP Default Codec (F411 and above)

XAP (LC3-based) encoding requires a hardware FPU (Cortex-M4F or better) and at least ~64KB of RAM. The STM32F411 (128KB RAM, Cortex-M4F) meets these requirements and uses XAP as the default codec at 16kHz/64kbps (~2 KB/s encoded, comfortable on LTE-M1).

The STM32F103 (20KB RAM, Cortex-M3, no FPU) is too constrained for XAP. It uses ADPCM for audio (if needed) or sensor-only XMBP streaming.

LTE-M1 via UART AT Commands

STM32 communicates with the cellular network through an external LTE-M1 modem connected via UART. AT commands follow the 3GPP standard with modem-specific extensions (Quectel BG96, SIM7080, etc.).

// AT command sequence for HTTP POST
HAL_UART_Transmit(&huart1, "AT+QHTTPCFG=\"contextid\",1\r\n", ...);
HAL_UART_Transmit(&huart1, "AT+QHTTPPOST=...\r\n", ...);
HAL_UART_Transmit(&huart1, xmbp_buf, packet_len, ...);

Legacy C Build

STM32CubeIDE

  1. Open STM32CubeIDE and create a new STM32 project for your target (F103, F411, WB55)
  2. Configure peripherals in CubeMX (I2S, UART, ADC, IWDG)
  3. Add the example .c file to the Core/Src/ directory
  4. Add sdk/c/common/include/ and sdk/c/stm32/ to include paths
  5. Build with Project > Build All (Ctrl+B)

CMake (arm-none-eabi)

cmake_minimum_required(VERSION 3.22)
set(CMAKE_TOOLCHAIN_FILE arm-none-eabi-toolchain.cmake)
project(xylolabs_stm32_example C ASM)

add_executable(f411_audio
    Core/Src/f411_audio_example.c
    Core/Src/stm32f4xx_hal_msp.c
    Drivers/STM32F4xx_HAL_Driver/Src/stm32f4xx_hal.c
    # ... other HAL sources
)

target_include_directories(f411_audio PRIVATE
    Core/Inc
    Drivers/STM32F4xx_HAL_Driver/Inc
    sdk/c/common/include
    sdk/c/stm32
)

target_compile_definitions(f411_audio PRIVATE STM32F411xE USE_HAL_DRIVER)

Common LTE-M1 Modems

Modem Interface AT Command Set
Quectel BG96/BG95 UART AT 3GPP + Quectel extensions
SIMCom SIM7080G UART AT 3GPP + SIMCom extensions
u-blox SARA-R4/R5 UART AT 3GPP + u-blox extensions
Nordic nRF9160 SPI/UART AT or native SDK

API Documentation Reference

  • API Reference -- XMBP protocol, ingest sessions, audio uploads, API key management, device management