cancel
Showing results for 
Search instead for 
Did you mean: 

STM32H7 - Write float value into flash memory permanently

yaxsomo
Associate III

Hello Everyone,

 

I'm trying to write a float value into my STM32H743VIT6 flash memory, bit it jumps straight to the HardFaultHandler and I cannot understand why...

 

Capture d’écran 2025-02-15 à 01.42.36.png

 

Here's the two functions:  

 

HAL_StatusTypeDef Store_AltitudeMax(float altitude)
{
    HAL_StatusTypeDef status;
    uint32_t FirstSector, NbOfSectors, SectorError;
    FLASH_EraseInitTypeDef EraseInitStruct;

    // Ensure data is written in a full 32-byte Flash word
    uint64_t FlashWord[4] = { 0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF,
                              0xFFFFFFFFFFFFFFFF, 0xFFFFFFFFFFFFFFFF };

    memcpy(&FlashWord[0], &altitude, sizeof(float));

    // Unlock Flash
    status = HAL_FLASH_Unlock();
    if (status != HAL_OK)
    {
        return status;
    }

    // Get the sector of ALTITUDE_MAX_ADDRESS
    FirstSector = GetSector(ALTITUDE_MAX_ADDRESS);
    NbOfSectors = 1;  // Only erase 1 sector

    // Configure Erase Structure
    EraseInitStruct.TypeErase     = FLASH_TYPEERASE_SECTORS;
    EraseInitStruct.VoltageRange  = VOLTAGE_RANGE;
    EraseInitStruct.Banks         = FLASH_BANK;
    EraseInitStruct.Sector        = FirstSector;
    EraseInitStruct.NbSectors     = NbOfSectors;

    // Erase Flash Sector
    if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
    {
        HAL_FLASH_Lock();
        return HAL_ERROR;
    }

    // Program the Flash word (32 bytes)
    status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, ALTITUDE_MAX_ADDRESS, (uint32_t)FlashWord);

    // Lock Flash after programming
    HAL_FLASH_Lock();
    return status;
}

float Retrieve_AltitudeMax(void)
{
    float altitude = 0.0f;
    uint64_t FlashWord[4];

    // Read from Flash (direct memory access)
    memcpy(FlashWord, (uint64_t*)ALTITUDE_MAX_ADDRESS, sizeof(FlashWord));

    // Extract stored float value
    memcpy(&altitude, &FlashWord[0], sizeof(float));

    return altitude;
}

 

 

And the test snippet : 

 

  HAL_StatusTypeDef ret = Store_AltitudeMax(768);

  if(ret != HAL_OK) {
	  printf("Something went wrong with the storage of the variable! \r\n");
  } else {
	  HAL_Delay(3000);

	  float value = Retrieve_AltitudeMax();

	  printf("Altitude retrieved: %d \r\n", (int)value);
  }

 

 

Can anyone tell me what's going on here ?

1 ACCEPTED SOLUTION

Accepted Solutions

The reference manual is the best source of information. It will have the memory map and flash layout. The datasheet is secondary.

STM32H742, STM32H743/753 and STM32H750 Value line advanced Arm®-based 32-bit MCUs - Reference manual

If you feel a post has answered your question, please click "Accept as Solution".

View solution in original post

5 REPLIES 5
TDK
Guru

What is the value of ALTITUDE_MAX_ADDRESS? It needs to be 256-bit aligned.

Step through the code, which line does it go to a hard fault?

/**
  * @brief  Program a flash word at a specified address
  * @PAram  TypeProgram Indicate the way to program at a specified address.
  *         This parameter can be a value of @ref FLASH_Type_Program
  * @PAram  FlashAddress specifies the address to be programmed.
  *         This parameter shall be aligned to the Flash word:
  *          - 256 bits for STM32H74x/5X devices (8x 32bits words)
  *          - 128 bits for STM32H7Ax/BX devices (4x 32bits words)
  *          - 256 bits for STM32H72x/3X devices (8x 32bits words)
  * @PAram  DataAddress specifies the address of data to be programmed.
  *         This parameter shall be 32-bit aligned
  *
  * @retval HAL_StatusTypeDef HAL Status
  */
HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t FlashAddress, uint32_t DataAddress)

 

If you feel a post has answered your question, please click "Accept as Solution".
yaxsomo
Associate III

Hello TDK,

Finally I found the solution.

Actually, the Both functions seems good.

the 3 defines that i created during the issue were the following : 

 

 

#define ALTITUDE_MAX_ADDRESS  0x081E0000U 
#define FLASH_BANK            FLASH_BANK_1
#define VOLTAGE_RANGE         FLASH_VOLTAGE_RANGE_3

 

 

The HardFaultHandler issue occurred during the Retrieve_AltitudeMax function.

I just changed the FLASH_BANK value to FLASH_BANK_2 and it solved the issue. Can you tell me if the two functions are optimized or if i can do better? 

Maybe it's not a smart question, but why this change solved the issue ? And why on this particular H7 model, there's not really explicit informations on the flash addresses on the datasheet? Does this apply on all H7 MCUs?

The reference manual is the best source of information. It will have the memory map and flash layout. The datasheet is secondary.

STM32H742, STM32H743/753 and STM32H750 Value line advanced Arm®-based 32-bit MCUs - Reference manual

If you feel a post has answered your question, please click "Accept as Solution".
yaxsomo
Associate III

That's why I couldn't find the information on the datasheet!! 

Thank you so much!

I just changed the FLASH_BANK value to FLASH_BANK_2 and it solved the issue.

Of course. Because the address 0x081E0000 belongs to bank2.

And why on this particular H7 model, there's not really explicit informations on the flash addresses on the datasheet?

??? The addresses of all banks and sectors are documented, either in DS or RM. And they are not hard to calculate.