[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 committed by Ali LABBENE
parent c750eab699
commit 156741e067
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 * @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. * emulation firmware library.
****************************************************************************** ******************************************************************************
* @attention * @attention

View File

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