cancel
Showing results for 
Search instead for 
Did you mean: 

Hardfault Exception is triggered after HAL_FLASH_Lock() function

GergoKato
Associate II

Hi, 

I want to write into flash on an STM32WB15 mcu. I have this code, where i write two 32 bit variable into the flash. 

 

void Write_Flash_Data(uint32_t new_value1, uint32_t new_value2) {
    HAL_StatusTypeDef status;

    HAL_FLASH_Unlock();  // Unlock Flash memory for writing

    // Erase the Flash page before writing
    FLASH_EraseInitTypeDef EraseInitStruct;
    uint32_t PageError;

    EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
    EraseInitStruct.Page = (uint32_t)((uint32_t)flash_data - 0x08000000) / FLASH_PAGE_SIZE;
    EraseInitStruct.NbPages = 1;

    // Check for errors during erase
    status = HAL_FLASHEx_Erase(&EraseInitStruct, &PageError);

    if (status != HAL_OK) {
        // Handle erase error
        HAL_FLASH_Lock();  // Lock the Flash to avoid other operations
        return;
    }

    // Write new values to Flash (64-bit write)
    uint64_t data = ((uint64_t)new_value2 << 32) | new_value1;

    status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, (uint32_t)flash_data, data);
    if (status != HAL_OK) {
        // Handle programming error
        HAL_FLASH_Lock();  // Lock the Flash
        return;
    }
    HAL_FLASH_Lock();  // Lock the Flash memory after writing
}

 

The function is called in the main while(1) loop

 

		__disable_irq();
		Write_Flash_Data((uint32_t)km,(uint32_t)km);
		__enable_irq();

		SPEED_PrevTimeCount_tm_u32 = __HAL_TIM_GET_COUNTER(&htim2);


		HAL_Delay(100);

 

The adress of the flash data is allocated in the .ld file:

GergoKato_1-1726337220369.png

Aand used:

 

__attribute__((section(".my_flash_data"))) uint32_t flash_data[2];

 

After i run the code, it runs into a hardfault exception. I was able to debug it.

I can see in the Memory, that the write was succesfull:

GergoKato_0-1726337034396.png

After the HAL_Flash_Lock(); returns, the program goes into hardfault exception interrupt, the strangest thing is, that it does not happen at the same time between tries. Sometimes it goes right into hardfault, sometimes it executes some instruction, and then goes into hardfault. 

 

Does someone have any idea what can be the problem, or how to start to solve this issue?

If i did not wrote enough information, please ask me to provide them!

Many thanks!

 

3 REPLIES 3
Pavel A.
Evangelist III

Why do you use __disable_irq() around the write?

When the hardfault occurs, have you tried to analyze it (what is the original exception reason) ?

I wa trying to solve the problem, and in an other forum they suggested that the issue is maybe that an interrupt interrupts the flash write, causing an error. But it did not solved the issue. 

 

The Fault analizer states this:

GergoKato_0-1726566084982.png

 

GergoKato
Associate II

I finally was able to detecte where is the Haurdfault comed from:

After writing, the main loop calls  MX_APPE_Process(); the tries to start a task, but the index is pointing to a place where there is no task:

GergoKato_0-1726664028495.png

GergoKato_2-1726664061661.png

After calling that, the program goes to HardFault