Showing results for 
Search instead for 
Did you mean: 

How to write into flash memory using HAL_FLASH_PROGRAM with FLASH_TYPEPROGRAM_QUADWORD



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 *************/



/* 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





/* 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) *********/



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?



RiverDI user

ST Employee

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