I am having an issue with my STM32F446 where I am experiencing a hardfault right after calling HAL_FLASH_Program to write data to internal flash, when I check the CFSR registerI see that the UNDEFINSTR flag is set to high.
I am using the following code to write data to flash memory:
#include "stm32f4xx_hal.h"
#include "stm32f4xx_hal_flash.h"
#define DATA_SIZE 4
int main(void)
{
uint32_t data[DATA_SIZE] = {0x12345678, 0x90ABCDEF, 0xDEADBEEF, 0xCAFEBABE};
uint32_t address = 0x08000040; // Starting address of sector 0
HAL_Init();
HAL_FLASH_Unlock(); // Unlock flash
// Erase sector 0
FLASH_Erase_Sector(FLASH_SECTOR_0, FLASH_VOLTAGE_RANGE_3);
// Write data to flash
for (int i = 0; i < DATA_SIZE; i++) {
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data[i]) != HAL_OK) {
// Handle write error
}
address += sizeof(data[i]);
}
HAL_FLASH_Lock(); // Lock flash
while (1) {
// Main loop
}
}
Could you please help me understand why I am experiencing a hardfault and what is causing the
UNDEFINSTR
flag to be set to high in the
CFSR
register?
Thank you in advance for your help.
Best regards,
