Hi,
This is my code now. It is working fine. this can retain the stored value after reset and power cycle.
thanks
Mathan
static FLASH_EraseInitTypeDef EraseInitStruct;
uint32_t FirstSector , NbOfSectors , BankNumber ,EndSector ;
uint32_t Address , SectorError, temp_value ;
uint32_t DRV_Get_Sector(uint32_t Address);
uint32_t DRV_Get_Bank(uint32_t Addr);
/******************************************************************************
* FUNCTION NAME: DRV_Write_Flash
*
* DESCRIPTION:
* write the data in particular internal flash memory location
*
* PARAMETERS: uint32_t address
* uint8_t *data
*
* RETURN VALUE: ErrorStatus SUCCESS
*
* NOTES:
*
*
******************************************************************************/
void DRV_Write_Flash(uint32_t address , uint8_t *data)
{
volatile uint32_t data_to_FLASH[(strlen((char*)data)/4) + (int)((strlen((char*)data) % 4) != 0)];
memset((uint8_t*)data_to_FLASH, 0, strlen((char*)data_to_FLASH));
strcpy((char*)data_to_FLASH, (char*)data);
volatile uint32_t data_length = (strlen((char*)data) / 4)
+ (int)((strlen((char*)data) % 4) != 0);
/* Unlock the Flash to enable the flash control register access *************/
HAL_FLASH_Unlock();
/* Erase the user Flash area
Get the 1st sector to erase */
FirstSector = DRV_Get_Sector(address);
/* Get the number of sector to erase from 1st sector*/
NbOfSectors = DRV_Get_Sector(address) - FirstSector + 1;
/* Get the bank */
BankNumber = DRV_Get_Bank(address);
/* Fill EraseInit structure*/
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
EraseInitStruct.Banks = BankNumber;
EraseInitStruct.Sector = FirstSector;
EraseInitStruct.NbSectors = NbOfSectors;
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK)
{
/*
Error occurred while sector erase.
User can add here some code to deal with this error.
SectorError will contain the faulty sector and then to know the code error on this sector,
user can call function 'HAL_FLASH_GetError()'
*/
/* Infinite loop */
while (1)
{
Error_Handler();
}
}
/* Program the user Flash area word by word*/
volatile uint32_t index=0;
while (index < data_length)
{
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_QUADWORD, address, (uint32_t)data) == HAL_OK)
{
address = address + 16; /* increment to next quad word*/
index++;
}
else
{
/* Error occurred while writing data in Flash memory.
User can add here some code to deal with this error */
while (1)
{
Error_Handler();
}
}
}
HAL_FLASH_Lock();
}
void DRV_Read_Flash(uint32_t address , uint8_t* data)
{
*data = *(__IO uint32_t*)address;
}
/**
* @brief Gets the sector of a given address
* @PAram Addr: Address of the FLASH Memory
* @retval The sector of a given address
*/
uint32_t DRV_Get_Sector(uint32_t Address)
{
uint32_t sector = 0;
if((Address >= FLASH_BASE) && (Address < FLASH_BASE + FLASH_BANK_SIZE))
{
sector = (Address & ~FLASH_BASE) / FLASH_SECTOR_SIZE;
}
else if ((Address >= FLASH_BASE + FLASH_BANK_SIZE) && (Address < FLASH_BASE + FLASH_SIZE))
{
sector = ((Address & ~FLASH_BASE) - FLASH_BANK_SIZE) / FLASH_SECTOR_SIZE;
}
else
{
sector = 0xFFFFFFFF; /* Address out of range */
}
return sector;
}
/**
* @brief Gets the bank of a given address
* @PAram Addr: Address of the FLASH Memory
* @retval The bank of a given address
*/
uint32_t DRV_Get_Bank(uint32_t Addr)
{
uint32_t bank = 0;
if (READ_BIT(FLASH->OPTSR_CUR, FLASH_OPTSR_SWAP_BANK) == 0)
{
/* No Bank swap */
if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
{
bank = FLASH_BANK_1;
}
else
{
bank = FLASH_BANK_2;
}
}
else
{
/* Bank swap */
if (Addr < (FLASH_BASE + FLASH_BANK_SIZE))
{
bank = FLASH_BANK_2;
}
else
{
bank = FLASH_BANK_1;
}
}
return bank;
}