Skip to main content
Visitor II
May 30, 2024
Solved

How to write into flash memory using HAL_FLASH_PROGRAM with FLASH_TYPEPROGRAM_QUADWORD

  • May 30, 2024
  • 2 replies
  • 11809 views

Hello,

I am new to STM32 and I am trying to write into FLASH memory using some reference code I found in internet. Here is my write function:

uint32_t Flash_Write_Data (uint32_t StartPageAddress, uint32_t *Data, uint16_t numberofwords)

{

 

static FLASH_EraseInitTypeDef EraseInitStruct;

uint32_t PAGEError;

int sofar=0;

 

/* Unlock the Flash to enable the flash control register access *************/

HAL_FLASH_Unlock();

 

/* Erase the user Flash area*/

 

uint32_t StartPage = GetPage(StartPageAddress);

uint32_t EndPageAdress = StartPageAddress + numberofwords*32;

uint32_t EndPage = GetPage(EndPageAdress);

 

/* Fill EraseInit structure*/

EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;

EraseInitStruct.Page = page_indx;

EraseInitStruct.NbPages = ((EndPage - StartPage)/FLASH_PAGE_SIZE) +1;

 

if (HAL_FLASHEx_Erase(&EraseInitStruct, &PAGEError) != HAL_OK)

{

/*Error occurred while page erase.*/

return HAL_FLASH_GetError ();

}

 

/* Program the user Flash area word by word*/

 

while (sofar<numberofwords)

{

 

if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, StartPageAddress, Data[sofar]) == HAL_OK)

{

StartPageAddress += 32; // use StartPageAddress += 2 for half word and 8 for double word

sofar++;

}

else

{

/* Error occurred while writing data in Flash memory*/

return HAL_FLASH_GetError ();

}

}

 

/* Lock the Flash to disable the flash control register access (recommended

to protect the FLASH memory against possible unwanted operation) *********/

HAL_FLASH_Lock();

 

return 0;

}

-----------------------

Function call for write in main.c:

char *data = "hello stm32";

uint32_t Rx_Data[30];

 

Flash_Write_Data(0x083FE000 , (uint32_t *)data, 2);

float number = 123;

Flash_Write_NUM(0x083FC000, number);

-----------------------

When I tried to write the data into flash memory my application just writes 0's in the memory (I checked using STM32 programmer)

and when I tried to write a number into the flash instead of char data, I get hardfault handler issue.

 

What am I doing wrong here? Can anyone shed some light please?

 

Regards

RiverDI user

    This topic has been closed for replies.
    Best answer by EXUE.2

    can you find the '\Projects\STM32U5x9J-DK\Examples\FLASH\FLASH_EraseProgram\...'?

    2 replies

    ST Employee
    June 3, 2024

    which MCU are you used? since your write flash address is start from the internal 0x083FE000, your MCU flash size is bigger than 3M?

    Visitor II
    June 24, 2024

    Hello, 

    Yes, my Flash is 4 MB and which has 512 pages of 8KB each.

    My MCU is: STM32U5A9

    I am trying to write into flash using my above code, after I write into flash I am getting hard fault handler error. Once I restart the device, I could see the data written into the flash, but my problem is my app is getting into "hard-fault" handler when I write into flash.

    Could you point out what is missing in my code?

    Thanks & Regards

    ST Employee
    June 24, 2024

    there is no obvious problem for your code, but there are some rules to write flash, like flash write align 4 bytes, flash latency configuration must correct.... I suggest you to download 'STM32CubeMX' tool to execute the flash operate demo to find the cause.

    STM32CubeMX link: https://www.st.com/en/development-tools/stm32cubemx.html#get-software

    Visitor II
    August 19, 2024

    Hello EXUE.2,

    I was able to write the data into FLASH by using the reference example you have shared. Thank you!