What causes HardFault_Handler errors and inaccurate predictions in STM32 air quality monitoring Sys?

I'm developing an air quality monitoring system on an STM32F746 using TinyML. The system uses a temperature, humidity, and gas sensor to predict the Air Quality Index (AQI) via a model I trained and deployed. While the code works, I sometimes get inaccurate predictions, and occasionally, the STM32 throws a HardFault_Handler error. Here’s the simplified inference code:
#include "main.h"
#include "stm32f7xx_hal.h"
#include "tinyml_model.h"

float input_data[3];
float output_data[1];

float read_temperature() { return 25.0; }
float read_humidity() { return 60.0; }
float read_gas_level() { return 150.0; }

int main(void) {
HAL_Init();
SystemClock_Config();

while (1) {
input_data[0] = read_temperature();
input_data[1] = read_humidity();
input_data[2] = read_gas_level();

if (predict_air_quality(input_data, output_data) != 0) {
printf("Inference error\n");
} else {
printf("Predicted AQI: %.2f\n", output_data[0]);
}

HAL_Delay(1000);
}
}
#include "main.h"
#include "stm32f7xx_hal.h"
#include "tinyml_model.h"

float input_data[3];
float output_data[1];

float read_temperature() { return 25.0; }
float read_humidity() { return 60.0; }
float read_gas_level() { return 150.0; }

int main(void) {
HAL_Init();
SystemClock_Config();

while (1) {
input_data[0] = read_temperature();
input_data[1] = read_humidity();
input_data[2] = read_gas_level();

if (predict_air_quality(input_data, output_data) != 0) {
printf("Inference error\n");
} else {
printf("Predicted AQI: %.2f\n", output_data[0]);
}

HAL_Delay(1000);
}
}
Could the hard fault be caused by memory issues or model size? How can I resolve the crash and improve prediction consistency?
2 Replies
ZacckOsiemo
ZacckOsiemo3mo ago
How to debug a HardFault on an Arm Cortex®-M STM32
Introduction For a firmware developer targeting and STM32 MCU based on a Cortex® M, they need to keep an eye on memory access, hardware availability, clock, and power to avoid running into issues that can be hard to follow and determine their root cause. Nevertheless, we can all at the end run into...
From An unknown user
From An unknown user
From An unknown user
From An unknown user
Marvee Amasi
Marvee Amasi3mo ago
Your STM32 may optimize away changes to variables when there is inference. Declaring your input_data and output_data arrays as volatile ensures that their values are properly updated during inference volatile float input_data[3]; volatile float output_data[1];
Want results from more Discord servers?
Add your server