2023-03-28 05:38 AM
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,
Solved! Go to Solution.
2023-03-28 06:25 AM
Use FLASH that's not hosting your code.
You might need to make a hole in the F4 memory map to free a smaller block.
You need some bootable code at 0x08000000
2023-03-28 05:53 AM
Hello @MOhamed_Mehery,
Have you tried the example provided on Cube Firmware? Here is a link STM32CubeF4/Projects/STM32F446ZE-Nucleo/Examples/FLASH/FLASH_EraseProgram at master · STMicroelectronics/STM32CubeF4 (github.com)
If still facing same issue, ensure that the data you are trying to write to flash is properly formatted and aligned.
To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.
2023-03-28 05:59 AM
Which memory/address do you run this code from? You are erasing beginning of FLASH - do you remap area 0x0000'0000, and/or relocate VTOR appropriately?
JW
2023-03-28 06:25 AM
Use FLASH that's not hosting your code.
You might need to make a hole in the F4 memory map to free a smaller block.
You need some bootable code at 0x08000000