/**
******************************************************************************
* @file EEPROM_Emul/Core/eeprom_emul.c
* @author MCD Application Team
* @brief This file provides all the EEPROM emulation firmware functions.
@verbatim
==============================================================================
##### How to use this driver #####
==============================================================================
[..]
This driver provides functions to initialize EEPROM emulation, to read and
write EEPROM variables, and to cleanup FLASH pages used by EEPROM emulation.
(#) EEPROM emulation initialization functions:
(++) Format the FLASH pages used by EEPROM emulation using EE_Format().
This function is optionally used, it can be called the very first
time EEPROM emulation is used, to prepare FLASH pages for EEPROM
emulation with empty EEPROM variables. It can also be called at
any time, to flush all EEPROM variables.
(++) Initialize EEPROM emulation, and restore the FLASH pages used by
EEPROM emulation to a known good state in case of power loss
using EE_Init(). It must be performed at system start up.
(#) EEPROM variables access functions:
(++) Write EEPROM variable using EE_WriteVariableXbits() functions
A Clean Up request can be raised as return parameter in case
FLASH pages used by EEPROM emulation, are full.
(++) Read EEPROM variable using EE_ReadVariableXbits() functions
(#) Clean up functions of FLASH pages, used by EEPROM emulation:
(++) There Two modes of erasing:
(+++) Polling mode using EE_CleanUp() function
(+++) Interrupt mode using EE_CleanUp_IT() function
(++) Callback function called when the clean up operation in interrupt
mode, is finished: EE_EndOfCleanup_UserCallback()
@endverbatim
******************************************************************************
* @attention
*
* <h2><center>© Copyright (c) 2020 STMicroelectronics.
* All rights reserved.</center></h2>
*
* This software component is licensed by ST under BSD 3-Clause license,
* the "License"; You may not use this file except in compliance with the
* License. You may obtain a copy of the License at:
* opensource.org/licenses/BSD-3-Clause
*
******************************************************************************
*/
/* Includes ------------------------------------------------------------------*/
#include "eeprom_emul.h"
/** @defgroup EEPROM_Emulation EEPROM_Emulation
* @{
*/
/* Private define -----------------------------------------------------------*/
#ifdef DUALCORE_FLASH_SHARING
#define HSEM_PROCESS_1 12U /* Number taken randomly to identify the process locking a semaphore in the driver context */
#endif
/* Private typedef -----------------------------------------------------------*/
/** @defgroup EEPROM_Private_Structures EEPROM Private Structures
* @{
*/
/**
* @brief EE Find Type structure definition.
*/
/* Type of find requested :
READ --> page in active state
WRITE --> page in receive state or active state
ERASE --> page in erased state */
typedef enum {
FIND_READ_PAGE,
FIND_WRITE_PAGE,
FIND_ERASE_PAGE
} EE_Find_type;
/**
* @brief EE State Type structure definition.
*/
/* Type of state requested :
ERASED --> page is erased
RECEIVE --> page used during data transfer when no more space available in the system
ACTIVE --> page contains valid data and is not full
VALID --> page contains valid data and is full
ERASING --> page used during transfer, should be erased after transfer
INVALID --> page invalid state */
typedef enum {
STATE_PAGE_ERASED,
STATE_PAGE_RECEIVE,
STATE_PAGE_ACTIVE,
STATE_PAGE_VALID,
STATE_PAGE_ERASING,
STATE_PAGE_INVALID
} EE_State_type;
/**
* @brief EE Transfer Type structure definition.
*/
/* Definition of the different type of page transfer
NORMAL -> copy data page source to page destination
RECOVER -> resume page transfer that has been interrupted */
typedef enum {
EE_TRANSFER_NORMAL,
EE_TRANSFER_RECOVER
} EE_Transfer_type;
/**
* @brief EE State Reliability structure definition.
*/
/* Reliability of page state:
RELIABLE -> header of page is not corrupted, state is reliable
CORRUPTED -> header of page is corrupted, state is corrupted */
typedef enum {
STATE_RELIABLE,
STATE_CORRUPTED
} EE_State_Reliability;
/**
* @}
*/
/* Private variables ---------------------------------------------------------*/
/** @defgroup EEPROM_Private_Variables EEPROM Private Variables
* @{
*/
/* Global variables used to store eeprom status */
uint16_t uhNbWrittenElements = 0U; /*!< Nb of elements written in valid and active pages */
uint8_t ubCurrentActivePage = 0U; /*!< Current active page (can be active or receive state) */
uint32_t uwAddressNextWrite = PAGE_HEADER_SIZE; /*!< Initialize write position just after page header */
/* During the cleanup phase in EE_Init, AddressRead is the address being read */
__IO uint32_t AddressRead = 0;
/* Flag equal to 1 when the cleanup phase is in progress, 0 if not */
__IO uint8_t CleanupPhase = 0;
/**
* @}
*/
/* Private function prototypes -----------------------------------------------*/
/** @defgroup EEPROM_Private_Functions EEPROM Private Functions
* @{
*/
static EE_Status ReadVariable(uint16_t VirtAddress, EE_DATA_TYPE* pData);
static EE_Status WriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data);
static EE_Status VerifyPageFullyErased(uint32_t Address, uint32_t PageSize);
static uint32_t FindPage(EE_Find_type Operation);
static EE_Status PagesTransfer(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Transfer_type type);
#ifdef DUALCORE_FLASH_SHARING
static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data, EE_Write_type Write_type);
#else
static EE_Status VerifyPagesFullWriteVariable(uint16_t VirtAddress, EE_DATA_TYPE Data);
#endif
static EE_Status SetPageState(uint32_t Page, EE_State_type State);
static EE_State_type GetPageState(uint32_t Address);
void ConfigureCrc(void);
uint16_t CalculateCrc(EE_DATA_TYPE Data, uint16_t VirtAddress);
/**
* @}
*/
/* Exported functions -------------------------------------------------------*/
/** @addtogroup EEPROM_Exported_Functions
* @{
*/
/**
* @brief Restore the pages to a known good state in case of power loss.
* If a page is in RECEIVE state, resume transfer.
* Then if some pages are ERASING state, erase these pages.
* @param EraseType: Type of erase to apply on page requiring to be erased.
* This parameter can be one of the following values:
* @arg @ref EE_FORCED_ERASE pages to erase are erased unconditionnally
* @arg @ref EE_CONDITIONAL_ERASE pages to erase are erased only if not fully erased
* @retval EE_Status
* - EE_OK in case of success
* - EE error code in case of error
*/
EE_Status EE_Init(EE_Erase_type EraseType)
{
EE_State_type pagestatus = STATE_PAGE_INVALID;
uint32_t page = 0U, pageaddress = 0U, varidx = 0U,
nbactivepage = 0U, nbactivereceivepage = 0U, nbvalidpage = 0U,
lastvalidpage = 0U, firstvalidpage = 0U,
recoverytransfer = 0U;
EE_ELEMENT_TYPE addressvalue = 0U;
EE_State_Reliability pagestate = STATE_RELIABLE;
EE_Status status = EE_OK;
/* Check if the configuration is 128-bits bank or 2*64-