Software/STM32CubeIDE

STM32CubeIDE Getting Started

Requirements, opening and building a BSP example in STM32CubeIDE, uploading it over USB DFU, programming external flash, and debug access.

intermediate10 min read

A Neuro N6 project in STM32CubeIDE is built from the board support package repository. Every example carries ready-made CubeIDE projects that reach the BSP, the HAL and the middleware by relative path, so an example builds from a checkout of the repository with nothing else installed beyond STM32CubeIDE. The built image is uploaded over USB through the board's boot loader.

Requirements

ItemDetail
STM32CubeIDE 2.2.0Brings what the example projects use: GNU Tools for STM32 14.3.rel1, make, and STM32CubeProgrammer's signing tool, which every build runs to produce the _Trusted.bin image the boot loader accepts. No separate STM32CubeProgrammer installation is needed
Neuro N6 with FSBL v2.5.0 or laterBoards ship with v2.6.2, the published boot loader, which the repository carries as FSBL/Neuro-N6-FSBL-v2.6.2-Trusted.bin. python harness/n6fsbl.py reports the boot loader on a board
Radio firmware in external flashFor the Wi-Fi and BLE examples, at 0x91E00000
Network weights in external flashFor the neural network examples, from 0x90300000
Python 3 with pyserialFor the tools in harness/, which are written for Windows with Git Bash
A short repository path on WindowsA build writes paths up to 153 characters below the repository folder and Windows limits a path to 260, so the folder's own path is kept under about 100 characters. Spaces are allowed

The repository is cloned from ohmlab-ltd/NeuroN6-BSP.

Opening an example

Each example folder holds its own code in Core/ and its CubeIDE projects in STM32CubeIDE/:

ProjectBuilds
AppliThe application. The example's Core/ is compiled with the BSP, the HAL and any middleware, linked in from the repository's shared folders rather than copied
FSBLThe first-stage boot loader, from the source in the shared FSBL/ folder. The board already runs a boot loader, so uploading an example needs only Appli, and the image this project builds, <Example>_FSBL_Trusted.bin, is not written to a board in use
Appli_FreeRTOSThe same Core/ under FreeRTOS. Present for BSPTest and Camera_JpegStream_USB

The projects are opened with File > Import > General > Existing Projects into Workspace, selecting the example's STM32CubeIDE folder, which finds all its projects. Copy projects into workspace is left unticked, because the projects reach the repository by relative path.

Building

The Debug configuration is built. The project compiles for the STM32N657I0HxQ with USE_HAL_DRIVER and STM32N657xx, links with Common/Linker/STM32N657I0HXQ_LRUN.ld, and its post-build step runs STM32_SigningTool_CLI to wrap the binary in the image header. The image to upload is:

Examples/<Example>/STM32CubeIDE/Appli/Debug/<Example>_Appli_Trusted.bin

The standalone examples compile the whole BSP, and the linker drops the modules an example does not call.

The same build runs headless from Git Bash. harness/examples/cubebuild.sh builds every project an example has, from clean, in a temporary workspace, using the STM32CubeIDE installation named by STM32CUBEIDE_DIR (default C:/ST/STM32CubeIDE_2.2.0):

bash
bash harness/examples/cubebuild.sh Blink        # every project of one example
bash harness/examples/cubebuild.sh Blink Appli  # one project
bash harness/examples/cubebuild.sh --all        # every example

The script prints each image it builds and its size, or the errors, and its exit code is the number of projects that failed. Because of their networks' licence, --all skips the two YOLOv8 examples, which carry a RESTRICTED-LICENCE file, unless NEURON6_BUILD_RESTRICTED=1 is set, as the compile harness harness/compile.sh does. Naming one of them to cubebuild.sh, or importing it into STM32CubeIDE, builds it like any other example.

Uploading over USB DFU

The boot loader presents a DFU interface over the USB-C connector. A running application enumerates as USB device 1209:8008 and the boot loader as 1209:8009. The uploader in harness/uploader/ writes an image to the application slot at 0x90100000:

bash
python harness/uploader/neuro-n6-uploader-cli.py --bin Examples/Blink/STM32CubeIDE/Appli/Debug/Blink_Appli_Trusted.bin

The uploader finds the board by its USB identity, sends a running application to the boot loader with the 1200 baud touch, writes the image and starts it. If the boot loader is already enumerated it writes at once. Every session first returns a bootloader left in the middle of an upload to idle, so an upload broken off part way does not affect the next session.

OptionEffect
--portThe board's serial port, that of its application or of its bootloader. With more than one board connected the uploader refuses to run without it
--addressFlash address in hex, 0x90100000 by default. Addresses below the application slot are refused without --fsbl
--no-rebootSkips the reboot step, for a board already in its boot loader
--fsblWrites a boot loader image to 0x90000000, and accepts only a boot loader image

A boot loader image, such as the <Example>_FSBL_Trusted.bin each example's FSBL project builds, is refused for an application slot.

python harness/n6flash.py <image>_Trusted.bin runs the same uploader for a board already in its boot loader, writes only the application slot and refuses a boot loader image. It then waits for the application to enumerate. It refuses to run while more than one board is connected.

The board enters its boot loader on any of the following:

  • the 1200 baud touch: the console port opened at 1200 baud and DTR dropped, which neuron6_com handles in every application that runs the console;
  • BSP_COM_RebootToBootloader() from the application, which every example calls when it receives b on the console;
  • two presses of reset with both presses within the same second of the real-time clock.

The RGB LED breathes red while the board is in its boot loader.

Programming external flash

The Wi-Fi and BLE examples need the MAYA-W476 firmware at 0x91E00000, and each neural network example needs its network's weights in the weights pool. harness/n6weights.py compares a flash region with its manifest by the CRC32 the boot loader reports, and with --write programs it first and checks it again. Whatever the manifest gives, --write writes only inside the weights pool, 0x90300000 to 0x91C7FFFF, or the radio firmware region from 0x91E00000. The tool leaves the board in its bootloader after a check and resets it after a write, and refuses to run while more than one board is connected.

bash
python harness/n6weights.py Middlewares/NeuroN6/MayaW476/firmware/firmware.json --write
python harness/n6weights.py Middlewares/NeuroN6/Models/Nia416/model.json --write

BSP_WIFI_IsFirmwarePresent() reports whether the radio firmware is in place.

Updating the boot loader

The repository carries the image of the published boot loader, v2.6.2, as FSBL/Neuro-N6-FSBL-v2.6.2-Trusted.bin. The uploader writes it with --fsbl, which accepts only a boot loader image:

bash
python harness/uploader/neuro-n6-uploader-cli.py --bin FSBL/Neuro-N6-FSBL-v2.6.2-Trusted.bin --fsbl

python harness/n6fsbl.py then reports which boot loader the board runs. The FSBL/ source builds against the repository's newer HAL, so its output is not identical to the published image; boards take the published image.

A wrong image at 0x90000000, or power lost while it is written, leaves a board that does not start and that USB cannot reach. So does a reset after a boot loader upload abandoned part way, although the bootloader goes on answering until then; such an upload is completed or started again before any reset. A board in this state is recovered over the debug connector (see below).

Example structure

Each standalone example's main.c holds a setup() and a loop() above a block of board support scaffolding that is identical in every example. The scaffolding keeps the board reachable over USB:

  • main() enables the caches, configures the MPU (with the non-cacheable .axisram_nc region and the PSRAM window), calls HAL_Init(), arms the independent watchdog with BSP_WDG_InitEWI() before anything else, and starts the console with BSP_COM_Init();
  • a reset caused by the watchdog goes to the boot loader;
  • the watchdog's early warning, BSP_WDG_EWI_Callback(), sends the board to the boot loader;
  • delay() calls BSP_COM_Task(), refreshes the watchdog and turns a b from the host into a reboot to the boot loader, and loop() is serviced the same way between calls.

print() is BSP_COM_Printf(), which drops output when no host has the port open. millis() is the HAL tick. The fault handlers in Common/Src/stm32n6xx_it.c record the cause in backup register 7 and call BSP_FaultCallback(), whose weak default reboots to the boot loader; an application that must recover by itself defines it.

Adding the BSP to a project

A project of its own uses the BSP as follows.

  1. The repository root and each Components/* directory used are added to the include paths, and their .c files to the build.
  2. neuron6_conf_template.h is copied into the project as neuron6_conf.h and edited there. The BSP does not compile until the copy exists.
  3. The HAL modules the compiled BSP modules use are enabled in stm32n6xx_hal_conf.h. harness/stm32n6xx_hal_conf.h enables all of them.
  4. A non-cacheable region is reserved in the linker script and MPU set-up, and BSP_THERMAL_DMA_NODE_SECTION names it (.axisram_nc in the repository's linker scripts). The thermal camera module places its DMA descriptor there.
  5. The interrupt handlers the BSP exports are called from stm32n6xx_it.c: BSP_PB_IRQHandler (EXTI8), BSP_IO_IRQHandler (EXTI0 to EXTI15 for header pins), BSP_COM_IRQHandler (USB1_OTG_HS), BSP_CAMERA_DCMIPP_IRQHandler and BSP_CAMERA_CSI_IRQHandler, BSP_AUDIO_OUT_IRQHandler and BSP_AUDIO_IN_IRQHandler (their GPDMA channels), BSP_THERMAL_DMA_IRQHandler and BSP_THERMAL_SPI_IRQHandler, BSP_TONE_IRQHandler (TIM3), BSP_WIFI_IRQHandler (SDMMC1), BSP_BT_IRQHandler (USART6) and BSP_WDG_IRQHandler (IWDG, when the early warning is used).
  6. The HAL in Drivers/ is used, or the changes in docs/HAL-PATCHES.md are applied to a stock HAL.
  7. Most modules configure their pins, clocks and DMA inline and leave HAL_<PPP>_MspInit to the application; an application's own is then called for the BSP's handles as well and checks the handle's instance. neuron6_camera and neuron6_com take the hook (HAL_DCMIPP_MspInit, HAL_PCD_MspInit). Those two and neuron6_bus hand the wiring back through BSP_CAMERA_RegisterMspCallbacks, BSP_COM_RegisterMspCallbacks, BSP_I2C1_RegisterMspCallbacks, BSP_I2C2_RegisterMspCallbacks and BSP_SPI2_RegisterMspCallbacks, which exist when the HAL is built with the matching USE_HAL_<PPP>_REGISTER_CALLBACKS set to 1 and are called before the first Init.
  8. Isolation is left to the BSP unless the application owns it (see Resource isolation).

Configuration

neuron6_conf.h holds the interrupt priorities and the feature switches.

SettingDefaultEffect
BSP_USE_FREERTOS01 runs the modules under FreeRTOS
USE_BSP_COM_FEATURE1The USB CDC console
BSP_USB_UVCNot defined1 makes the board enumerate as a USB video camera and a console
USE_BSP_WDG_EWI1The watchdog's early warning interrupt
USE_BSP_BT_FLOW_CONTROL1Hardware flow control on the BLE link, which takes PD3
BSP_THERMAL_AUTO_RESYNC1BSP_THERMAL_GetFrame() restarts a stalled thermal stream by itself
BSP_ISOLATION_APPLICATION_OWNED01 leaves the RIF configuration to the application
USE_BSP_CPU_CACHE_MAINTENANCE1D-cache clean and invalidate around every DMA transfer the BSP owns
BSP_THERMAL_DMA_NODE_SECTION".axisram_nc"Non-cacheable section for the thermal DMA descriptor
BSP_AUDIO_IN_DMA_CHANNEL, BSP_THERMAL_DMA_CHANNEL, BSP_AUDIO_OUT_DMA_CHANNEL0, 1, 2GPDMA1 channels, one per module

The camera, thermal camera, console, NPU and Wi-Fi interrupts sit at priority 5, audio and BLE at 6, the header pins and the tone generator at 8 and the button at 14. The watchdog's early warning sits at 0 so that it preempts whatever stopped the refresh. Each is a BSP_*_IT_PRIORITY setting in neuron6_conf.h.

Running under FreeRTOS

With BSP_USE_FREERTOS set to 1 the same modules run under FreeRTOS Kernel V10.6.2. Utilities/freertos/README.md lists the steps a project takes:

  1. compile the kernel under Middlewares/FreeRTOS/Source with the Cortex-M55 non-secure port and heap_4, and add its include paths and Utilities/freertos;
  2. use the board's FreeRTOSConfig.h from Utilities/freertos: 1 kHz tick, 56 priorities, 96 KB heap, stack overflow checking, newlib reentrancy, configMAX_SYSCALL_INTERRUPT_PRIORITY 5;
  3. compile neuron6_rtos.c and set BSP_USE_FREERTOS, which also selects TinyUSB's FreeRTOS abstraction and the video encoder's event-group wait; a build that passes -DCFG_TUSB_OS=OPT_OS_NONE changes it to OPT_OS_FREERTOS;
  4. run the HAL tick on TIM16 with stm32n6xx_hal_timebase_tim.c from Utilities/freertos, with HAL_TIM_MODULE_ENABLED;
  5. remove SVC_Handler, PendSV_Handler and SysTick_Handler from stm32n6xx_it.c, which the port defines, and add TIM16_IRQHandler;
  6. call BSP_RTOS_StartUsbTask(0, 0) after BSP_COM_Init() and before vTaskStartScheduler(); that task then services the USB stack, and nothing else calls BSP_COM_Task();
  7. start the scheduler after the peripherals are up.

Under the kernel the bus, ADC and radio lock hooks become recursive mutexes, HAL_Delay yields, and a stack overflow or failed configASSERT reaches BSP_RTOS_FatalHook(). Every BSP interrupt except the watchdog's early warning sits at priority 5 or below (numerically 5 to 15), so its callbacks may call the kernel's *FromISR functions; the early warning, at 0, has a callback that does not use the kernel.

Debug access

The board's six-pin debug connector carries 3V3, GND, SWDIO (PA13), SWCLK (PA14), NRST and BOOT1 (PA6, the schematic net LCD_HSYNC); BOOT0 is not connected. An ST-LINK on this connector gives serial wire debug access to the STM32N657. Asserting BOOT1 during reset selects the STM32N6 development boot, in which the boot ROM reopens debug access and idles; STM32CubeProgrammer with the board's external memory loader then programs the external flash, which also recovers a board whose boot loader has been erased. The supported route for application images is the USB DFU upload described above. See Board Tour and Pinout.