[PROJECTS][NUCLEO-F103RB][EEPROM] Replace hard-coded page end address by a variable in EE_VerifyPageFullyErased()

Fixes issue raised in #11.
This commit is contained in:
Ali Labbene 2022-01-21 17:46:47 +01:00
parent c750eab699
commit 4b5805238d
2 changed files with 34 additions and 32 deletions

View File

@ -1,8 +1,8 @@
/**
******************************************************************************
* @file EEPROM_Emulation/inc/eeprom.h
* @file EEPROM_Emulation/inc/eeprom.h
* @author MCD Application Team
* @brief This file contains all the functions prototypes for the EEPROM
* @brief This file contains all the functions prototypes for the EEPROM
* emulation firmware library.
******************************************************************************
* @attention

View File

@ -1,6 +1,6 @@
/**
******************************************************************************
* @file EEPROM_Emulation/src/eeprom.c
* @file EEPROM_Emulation/src/eeprom.c
* @author MCD Application Team
* @brief This file provides all the EEPROM emulation firmware functions.
******************************************************************************
@ -19,7 +19,7 @@
/** @addtogroup EEPROM_Emulation
* @{
*/
*/
/* Includes ------------------------------------------------------------------*/
#include "eeprom.h"
@ -70,7 +70,7 @@ uint16_t EE_Init(void)
s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
s_eraseinit.PageAddress = PAGE0_ID;
s_eraseinit.NbPages = 1;
/* Check for invalid header states and repair if necessary */
switch (pagestatus0)
{
@ -92,7 +92,7 @@ uint16_t EE_Init(void)
{
/* Erase Page0 */
if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
{
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
@ -159,7 +159,7 @@ uint16_t EE_Init(void)
s_eraseinit.NbPages = 1;
/* Erase Page1 */
if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
{
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
@ -175,7 +175,7 @@ uint16_t EE_Init(void)
s_eraseinit.NbPages = 1;
/* Erase Page1 */
if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
{
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
@ -221,7 +221,7 @@ uint16_t EE_Init(void)
s_eraseinit.NbPages = 1;
/* Erase Page1 */
if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
{
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
@ -257,7 +257,7 @@ uint16_t EE_Init(void)
}
}
/* Mark Page1 as valid */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE);
/* If program operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
@ -268,7 +268,7 @@ uint16_t EE_Init(void)
s_eraseinit.NbPages = 1;
/* Erase Page0 */
if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
{
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
@ -305,11 +305,15 @@ uint16_t EE_Init(void)
*/
uint16_t EE_VerifyPageFullyErased(uint32_t Address)
{
uint32_t readstatus = 1;
uint32_t endAddress;
uint32_t readstatus = 1;
uint16_t addressvalue = 0x5555;
/* Compute page end-address */
endAddress = (uint32_t)(Address + (PAGE_SIZE - 4U));
/* Check each active page address starting from end */
while (Address <= PAGE0_END_ADDRESS)
while (Address <= endAddress)
{
/* Get the current location content to be compared with virtual address */
addressvalue = (*(__IO uint16_t*)Address);
@ -317,7 +321,7 @@ uint16_t EE_VerifyPageFullyErased(uint32_t Address)
/* Compare the read address with the virtual address */
if (addressvalue != ERASED)
{
/* In case variable value is read, reset readstatus flag */
readstatus = 0;
@ -326,7 +330,7 @@ uint16_t EE_VerifyPageFullyErased(uint32_t Address)
/* Next address location */
Address = Address + 4;
}
/* Return readstatus value: (0: Page not erased, 1: Page erased) */
return readstatus;
}
@ -436,7 +440,7 @@ static HAL_StatusTypeDef EE_Format(void)
/* Erase Page0 */
if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS))
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
@ -444,7 +448,7 @@ static HAL_StatusTypeDef EE_Format(void)
}
}
/* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE);
/* If program operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
@ -454,15 +458,15 @@ static HAL_StatusTypeDef EE_Format(void)
s_eraseinit.PageAddress = PAGE1_ID;
/* Erase Page1 */
if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS))
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
{
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
return flashstatus;
}
}
return HAL_OK;
}
@ -555,7 +559,7 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
/* Get valid Page for write operation */
validpage = EE_FindValidPage(WRITE_IN_VALID_PAGE);
/* Check if there is no valid page */
if (validpage == NO_VALID_PAGE)
{
@ -575,14 +579,14 @@ static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Da
if ((*(__IO uint32_t*)address) == 0xFFFFFFFF)
{
/* Set variable data */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, Data);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address, Data);
/* If program operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
return flashstatus;
}
/* Set variable virtual address */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address + 2, VirtAddress);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, address + 2, VirtAddress);
/* Return program operation status */
return flashstatus;
}
@ -643,13 +647,13 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
}
/* Set the new Page status to RECEIVE_DATA status */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, RECEIVE_DATA);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, RECEIVE_DATA);
/* If program operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
return flashstatus;
}
/* Write the variable passed as parameter in the new active page */
eepromstatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data);
/* If program operation was failed, a Flash error code is returned */
@ -682,9 +686,9 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
s_eraseinit.TypeErase = FLASH_TYPEERASE_PAGES;
s_eraseinit.PageAddress = oldpageid;
s_eraseinit.NbPages = 1;
/* Erase the old Page: Set old Page status to ERASED status */
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
flashstatus = HAL_FLASHEx_Erase(&s_eraseinit, &page_error);
/* If erase operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
@ -692,14 +696,12 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
}
/* Set new Page status to VALID_PAGE status */
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, VALID_PAGE);
flashstatus = HAL_FLASH_Program(FLASH_TYPEPROGRAM_HALFWORD, newpageaddress, VALID_PAGE);
/* If program operation was failed, a Flash error code is returned */
if (flashstatus != HAL_OK)
{
return flashstatus;
}
/* Return last operation flash status */
return flashstatus;
@ -707,6 +709,6 @@ static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data)
/**
* @}
*/
*/
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/