Hardfault error while writing to EEPROM(Flash) in LoRa-E5 module

Hi,
I’m working on LoRa E5 module for storing values in EEPROM. I want to use the last page of flash for storing configuration values of lora.

I have used EE library from GITHUB which uses HAL functions with unlock and lock as below.

// For writing into Flash
HAL_FLASH_Unlock();
HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD,EEPROM_Address, doubleWord);
HAL_FLASH_Lock();

// For erasing the sector
HAL_FLASH_Unlock();
flashErase.TypeErase = FLASH_TYPEERASE_PAGES;
flashErase.Page = eeHandle.PageSectorNumber;
flashErase.NbPages = 1;
HAL_FLASHEx_Erase(&flashErase, &error)
HAL_FLASH_Lock();

I followed 2 sequences for writing into flash:

  1. Erasing the page 127(which I use for writing the values), writing the values to it / reading the values from it any number of times worked perfectly.
  2. Read the values from page 127 and then erasing the page and writing to it. I get hardfault error everytime.

if I erase the page 1st and write to it, then there is no use of storing in it. So, If I read the flash, I’m not able to write to it.
I have also moved all the functions used for writing into flash to RAM by adding attribute((section(“.RamFunc”))) in function definition so that there won’t be stalling of CPU. I have checked in disassembly while debugging that all the related code is running from RAM.

I get hardfault error while HAL_FLASH_Unlock() after erasing the page. Is there any other configuration I need to do for using flash as eeprom.
I want to do the procedure as follows:
Read EEPROM → Erase&update the new values in Flash.