2025-12-12 7:02 AM
Hello everyone — I’m running an AI image-classification model on the STM32N6570-DK and I’m stuck with a reproducible problem. The first inference completes successfully (EVENT_DONE), but on the second run the code gets stuck inside the runtime (run_epoch) with no event returned; on the third attempt it again stalls and then triggers a HardFault. I followed the ST tutorial here when creating the project:
https://community.st.com/t5/stm32-mcus/how-to-build-an-ai-application-from-scratch-on-the-stm32n6570-dk/ta-p/825591
Below are the full details, steps to reproduce, debug info, things I’ve tried, and the files I’ve attached.
Board: STM32N6570-DK
Model: Image Classification (generated with STM32Cube.AI)
Model and weights: placed in xSPI1 / xSPI2 (QSPI/XSPI) and also copied to internal SRAM for some buffers
SRAM clocks: enabled for SRAM3, SRAM4, SRAM5, SRAM6
Toolchain: STM32Cube IDE - Latest version
CubeMX / Cube.AI version: Latest Version
RTOS: (bare-metal / FreeRTOS) : Bare-metal
Followed ST tutorial linked above to generate the project and AI code.
Integrated the Image Classification model and generated C sources/weights with Cube.AI.
Placed model weights and buffers in external XSPI1/XSPI2 and also set some buffers in internal SRAM.
Enabled clocks for SRAM3/4/5/6 so the SRAM regions are accessible.
Confirmed the input buffer address is readable after SRAM clocks are enabled.
Run the code:
1st inference: completes normally — returns event EVENT_DONE.
2nd inference: code enters run_epoch and blocks (no event).
3rd inference: same block in run_epoch, then Hard Fault occurs.
I have attached:
The generated project (zipped).
A screenshot / image of the HardFault (registers / fault screen).
First ai_run returns as expected.
On subsequent calls, execution enters the AI runtime’s run_epoch (or similar), but the runtime never completes or calls the EVENT_DONE callback.
Eventually a Hard Fault occurs; the first HardFault image shows stacked registers (I’ll attach the screenshot).
There are no obvious warnings during build; project builds clean.
Solved! Go to Solution.
2025-12-17 9:33 AM
Hello.
I tried your project, thanks for sharing it in an easily-useable format :).
The code looks okay (though i noticed some strange configs in the RIF, leading to access violations --just keep that in mind if the code ever breaks again or if it produces bad ouputs).
I managed to reproduce your issue exhibited each time epoch3 is settled for execution.
The real issue is that tensors for Epoch 3 are stored in your external flash memory, but the external flash memory is not accessible from the code.
It is possible to reproduce your issue with the following code in your main.c (declare uint32_t xxx; somewhere).
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CACHEAXI_Init();
MX_RAMCFG_Init();
MX_XSPI1_Init();
MX_XSPI2_Init();
SystemIsolation_Config();
MX_EXTMEM_MANAGER_Init();
MX_X_CUBE_AI_Init();
/* USER CODE BEGIN 2 */
xxx = *(uint32_t*)(0x7104a200); // <<<<<<<<<<-------------- ADD THIS LINE HERE
/* USER CODE END 2 */The address -- ==the address of your access where the hardfault is seen -- occurs when the external flash is read.
This is not an AI-related issue.
QUICKFIX proposal:
in your main.c, you have commented a part of code that can make your project work:
/*
* Comment out the line that says MX_EXTMEM_MANAGER_Init();
* and un-comment the code below will make your project work
*
* (the issue may then be in the extmem manager config... to be reviewed on your side :) )
*/
BSP_XSPI_RAM_Init(0);
BSP_XSPI_RAM_EnableMemoryMappedMode(0);
BSP_XSPI_NOR_Init_t NOR_Init;
NOR_Init.InterfaceMode = BSP_XSPI_NOR_OPI_MODE;
NOR_Init.TransferRate = BSP_XSPI_NOR_DTR_TRANSFER;
BSP_XSPI_NOR_Init(0, &NOR_Init);
BSP_XSPI_NOR_EnableMemoryMappedMode(0);
Cheers.
2025-12-17 9:33 AM
Hello.
I tried your project, thanks for sharing it in an easily-useable format :).
The code looks okay (though i noticed some strange configs in the RIF, leading to access violations --just keep that in mind if the code ever breaks again or if it produces bad ouputs).
I managed to reproduce your issue exhibited each time epoch3 is settled for execution.
The real issue is that tensors for Epoch 3 are stored in your external flash memory, but the external flash memory is not accessible from the code.
It is possible to reproduce your issue with the following code in your main.c (declare uint32_t xxx; somewhere).
/* USER CODE BEGIN SysInit */
/* USER CODE END SysInit */
/* Initialize all configured peripherals */
MX_GPIO_Init();
MX_CACHEAXI_Init();
MX_RAMCFG_Init();
MX_XSPI1_Init();
MX_XSPI2_Init();
SystemIsolation_Config();
MX_EXTMEM_MANAGER_Init();
MX_X_CUBE_AI_Init();
/* USER CODE BEGIN 2 */
xxx = *(uint32_t*)(0x7104a200); // <<<<<<<<<<-------------- ADD THIS LINE HERE
/* USER CODE END 2 */The address -- ==the address of your access where the hardfault is seen -- occurs when the external flash is read.
This is not an AI-related issue.
QUICKFIX proposal:
in your main.c, you have commented a part of code that can make your project work:
/*
* Comment out the line that says MX_EXTMEM_MANAGER_Init();
* and un-comment the code below will make your project work
*
* (the issue may then be in the extmem manager config... to be reviewed on your side :) )
*/
BSP_XSPI_RAM_Init(0);
BSP_XSPI_RAM_EnableMemoryMappedMode(0);
BSP_XSPI_NOR_Init_t NOR_Init;
NOR_Init.InterfaceMode = BSP_XSPI_NOR_OPI_MODE;
NOR_Init.TransferRate = BSP_XSPI_NOR_DTR_TRANSFER;
BSP_XSPI_NOR_Init(0, &NOR_Init);
BSP_XSPI_NOR_EnableMemoryMappedMode(0);
Cheers.