STM32CubeIDE Getting Started
Requirements, opening and building a BSP example in STM32CubeIDE, uploading it over USB DFU, programming external flash, and debug access.
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
| Item | Detail |
|---|---|
| STM32CubeIDE 2.2.0 | Brings 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 later | Boards 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 flash | For the Wi-Fi and BLE examples, at 0x91E00000 |
| Network weights in external flash | For the neural network examples, from 0x90300000 |
Python 3 with pyserial | For the tools in harness/, which are written for Windows with Git Bash |
| A short repository path on Windows | A 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/:
| Project | Builds |
|---|---|
Appli | The 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 |
FSBL | The 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_FreeRTOS | The 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.binThe 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 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 exampleThe 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:
python harness/uploader/neuro-n6-uploader-cli.py --bin Examples/Blink/STM32CubeIDE/Appli/Debug/Blink_Appli_Trusted.binThe 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.
| Option | Effect |
|---|---|
--port | The 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 |
--address | Flash address in hex, 0x90100000 by default. Addresses below the application slot are refused without --fsbl |
--no-reboot | Skips the reboot step, for a board already in its boot loader |
--fsbl | Writes 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_comhandles in every application that runs the console; BSP_COM_RebootToBootloader()from the application, which every example calls when it receivesbon 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.
python harness/n6weights.py Middlewares/NeuroN6/MayaW476/firmware/firmware.json --write
python harness/n6weights.py Middlewares/NeuroN6/Models/Nia416/model.json --writeBSP_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:
python harness/uploader/neuro-n6-uploader-cli.py --bin FSBL/Neuro-N6-FSBL-v2.6.2-Trusted.bin --fsblpython 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_ncregion and the PSRAM window), callsHAL_Init(), arms the independent watchdog withBSP_WDG_InitEWI()before anything else, and starts the console withBSP_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()callsBSP_COM_Task(), refreshes the watchdog and turns abfrom the host into a reboot to the boot loader, andloop()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.
- The repository root and each
Components/*directory used are added to the include paths, and their.cfiles to the build. neuron6_conf_template.his copied into the project asneuron6_conf.hand edited there. The BSP does not compile until the copy exists.- The HAL modules the compiled BSP modules use are enabled in
stm32n6xx_hal_conf.h.harness/stm32n6xx_hal_conf.henables all of them. - A non-cacheable region is reserved in the linker script and MPU set-up, and
BSP_THERMAL_DMA_NODE_SECTIONnames it (.axisram_ncin the repository's linker scripts). The thermal camera module places its DMA descriptor there. - 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_IRQHandlerandBSP_CAMERA_CSI_IRQHandler,BSP_AUDIO_OUT_IRQHandlerandBSP_AUDIO_IN_IRQHandler(their GPDMA channels),BSP_THERMAL_DMA_IRQHandlerandBSP_THERMAL_SPI_IRQHandler,BSP_TONE_IRQHandler(TIM3),BSP_WIFI_IRQHandler(SDMMC1),BSP_BT_IRQHandler(USART6) andBSP_WDG_IRQHandler(IWDG, when the early warning is used). - The HAL in
Drivers/is used, or the changes indocs/HAL-PATCHES.mdare applied to a stock HAL. - Most modules configure their pins, clocks and DMA inline and leave
HAL_<PPP>_MspInitto the application; an application's own is then called for the BSP's handles as well and checks the handle's instance.neuron6_cameraandneuron6_comtake the hook (HAL_DCMIPP_MspInit,HAL_PCD_MspInit). Those two andneuron6_bushand the wiring back throughBSP_CAMERA_RegisterMspCallbacks,BSP_COM_RegisterMspCallbacks,BSP_I2C1_RegisterMspCallbacks,BSP_I2C2_RegisterMspCallbacksandBSP_SPI2_RegisterMspCallbacks, which exist when the HAL is built with the matchingUSE_HAL_<PPP>_REGISTER_CALLBACKSset to 1 and are called before the firstInit. - 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.
| Setting | Default | Effect |
|---|---|---|
BSP_USE_FREERTOS | 0 | 1 runs the modules under FreeRTOS |
USE_BSP_COM_FEATURE | 1 | The USB CDC console |
BSP_USB_UVC | Not defined | 1 makes the board enumerate as a USB video camera and a console |
USE_BSP_WDG_EWI | 1 | The watchdog's early warning interrupt |
USE_BSP_BT_FLOW_CONTROL | 1 | Hardware flow control on the BLE link, which takes PD3 |
BSP_THERMAL_AUTO_RESYNC | 1 | BSP_THERMAL_GetFrame() restarts a stalled thermal stream by itself |
BSP_ISOLATION_APPLICATION_OWNED | 0 | 1 leaves the RIF configuration to the application |
USE_BSP_CPU_CACHE_MAINTENANCE | 1 | D-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_CHANNEL | 0, 1, 2 | GPDMA1 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:
- compile the kernel under
Middlewares/FreeRTOS/Sourcewith the Cortex-M55 non-secure port andheap_4, and add its include paths andUtilities/freertos; - use the board's
FreeRTOSConfig.hfromUtilities/freertos: 1 kHz tick, 56 priorities, 96 KB heap, stack overflow checking, newlib reentrancy,configMAX_SYSCALL_INTERRUPT_PRIORITY5; - compile
neuron6_rtos.cand setBSP_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_NONEchanges it toOPT_OS_FREERTOS; - run the HAL tick on TIM16 with
stm32n6xx_hal_timebase_tim.cfromUtilities/freertos, withHAL_TIM_MODULE_ENABLED; - remove
SVC_Handler,PendSV_HandlerandSysTick_Handlerfromstm32n6xx_it.c, which the port defines, and addTIM16_IRQHandler; - call
BSP_RTOS_StartUsbTask(0, 0)afterBSP_COM_Init()and beforevTaskStartScheduler(); that task then services the USB stack, and nothing else callsBSP_COM_Task(); - 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.