1 /**
2   ******************************************************************************
3   * @file    EEPROM_Emul/Core/eeprom_emul.h
4   * @author  MCD Application Team
5   * @brief   This file contains all the functions prototypes for the EEPROM
6   *          emulation firmware library.
7   ******************************************************************************
8   * @attention
9   *
10   * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics International N.V.
11   * All rights reserved.</center></h2>
12   *
13   * Redistribution and use in source and binary forms, with or without
14   * modification, are permitted, provided that the following conditions are met:
15   *
16   * 1. Redistribution of source code must retain the above copyright notice,
17   *    this list of conditions and the following disclaimer.
18   * 2. Redistributions in binary form must reproduce the above copyright notice,
19   *    this list of conditions and the following disclaimer in the documentation
20   *    and/or other materials provided with the distribution.
21   * 3. Neither the name of STMicroelectronics nor the names of other
22   *    contributors to this software may be used to endorse or promote products
23   *    derived from this software without specific written permission.
24   * 4. This software, including modifications and/or derivative works of this
25   *    software, must execute solely and exclusively on microcontroller or
26   *    microprocessor devices manufactured by or for STMicroelectronics.
27   * 5. Redistribution and use of this software other than as permitted under
28   *    this license is void and will automatically terminate your rights under
29   *    this license.
30   *
31   * THIS SOFTWARE IS PROVIDED BY STMICROELECTRONICS AND CONTRIBUTORS "AS IS"
32   * AND ANY EXPRESS, IMPLIED OR STATUTORY WARRANTIES, INCLUDING, BUT NOT
33   * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY, FITNESS FOR A
34   * PARTICULAR PURPOSE AND NON-INFRINGEMENT OF THIRD PARTY INTELLECTUAL PROPERTY
35   * RIGHTS ARE DISCLAIMED TO THE FULLEST EXTENT PERMITTED BY LAW. IN NO EVENT
36   * SHALL STMICROELECTRONICS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
37   * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
38   * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
39   * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
40   * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
41   * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
42   * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
43   *
44   ******************************************************************************
45   */
46 
47 /* Define to prevent recursive inclusion -------------------------------------*/
48 #ifndef __EEPROM_EMUL_H
49 #define __EEPROM_EMUL_H
50 
51 /* Includes ------------------------------------------------------------------*/
52 #include "stm32l4xx_hal.h"
53 #include "eeprom_emul_conf.h"
54 #include "eeprom_emul_types.h"
55 #include "flash_interface.h"
56 #include "stm32l4xx_ll_crc.h"
57 #include "stm32l4xx_ll_bus.h"
58 #if defined(RECOVERY_TEST)
59 #include "stm32l4xx_ll_rtc.h"
60 #endif
61 
62 /** @addtogroup EEPROM_Emulation
63   * @{
64   */
65 
66 /* Private constants ---------------------------------------------------------*/
67 /** @defgroup EEPROM_Private_Constants EEPROM Private Constants
68   * @{
69   */
70 
71 /** @defgroup Private_Other_Constants Private Other Constants
72   * @{
73   */
74 
75 /* Page definitions */
76 #define PAGE_SIZE               FLASH_PAGE_SIZE                                  /*!< Page size */
77 #define PAGE_HEADER_SIZE        EE_ELEMENT_SIZE * 4U                             /*!< Page Header is 4 elements to save page state */
78 #define NB_MAX_ELEMENTS_BY_PAGE ((PAGE_SIZE - PAGE_HEADER_SIZE) / EE_ELEMENT_SIZE) /*!< Max number of elements by page */
79 #define PAGES_NUMBER            (((((NB_OF_VARIABLES + NB_MAX_ELEMENTS_BY_PAGE) / NB_MAX_ELEMENTS_BY_PAGE) * 2U) * CYCLES_NUMBER) + GUARD_PAGES_NUMBER)
80                                                                                  /*!< Number of consecutives pages used by the application */
81 #define NB_MAX_WRITTEN_ELEMENTS ((NB_MAX_ELEMENTS_BY_PAGE * PAGES_NUMBER) / 2U)  /*!< Max number of elements written before triggering pages transfer */
82 #define START_PAGE              PAGE(START_PAGE_ADDRESS)                         /*!< Page index of the 1st page used for EEPROM emul, in the bank */
83 #define END_EEPROM_ADDRESS      (START_PAGE_ADDRESS + (PAGES_NUMBER * FLASH_PAGE_SIZE) - 1) /*!< Last address of EEPROM emulation flash pages */
84 
85 /* No page define */
86 #define EE_NO_PAGE_FOUND        ((uint32_t)0xFFFFFFFFU)
87 
88 /**
89   * @}
90   */
91 
92 /**
93   * @}
94   */
95 
96 /* Private macro -------------------------------------------------------------*/
97 /** @defgroup EEPROM_Private_Macros EEPROM Private Macros
98   * @{
99   */
100 
101 /** @defgroup Macros_Pages Macros to manipulate pages
102   * @{
103   */
104 
105 /* Macros to manipulate pages */
106 #define PAGE_ADDRESS(__PAGE__)   (uint32_t)(FLASH_BASE + (__PAGE__) * PAGE_SIZE + ((START_PAGE_ADDRESS - FLASH_BASE) / BANK_SIZE) * BANK_SIZE) /*!< Get page address from page index */
107 #define PAGE(__ADDRESS__)        (uint32_t)((((__ADDRESS__) - FLASH_BASE) % BANK_SIZE) / FLASH_PAGE_SIZE) /*!< Get page index from page address */
108 #define PREVIOUS_PAGE(__PAGE__)  (uint32_t)((((__PAGE__) - START_PAGE - 1U + PAGES_NUMBER) % PAGES_NUMBER) + START_PAGE) /*!< Get page index of previous page, among circular page list */
109 #define FOLLOWING_PAGE(__PAGE__) (uint32_t)((((__PAGE__) - START_PAGE + 1U) % PAGES_NUMBER) + START_PAGE) /*!< Get page index of following page, among circular page list */
110 
111 /**
112   * @}
113   */
114 
115 /** @defgroup Macros_Elements Macros to manipulate elements
116   * @{
117   */
118 
119 /* Macros to manipulate elements */
120 #define EE_VIRTUALADDRESS_VALUE(__ELEMENT__)            (EE_VIRTUALADDRESS_TYPE)((__ELEMENT__) & EE_MASK_VIRTUALADDRESS) /*!< Get virtual address value from element value */
121 #define EE_DATA_VALUE(__ELEMENT__)                      (EE_DATA_TYPE)(((__ELEMENT__) & EE_MASK_DATA) >> EE_DATA_SHIFT) /*!< Get Data value from element value */
122 #define EE_CRC_VALUE(__ELEMENT__)                       (EE_CRC_TYPE)(((__ELEMENT__) & EE_MASK_CRC) >> EE_CRC_SHIFT) /*!< Get Crc value from element value */
123 #define EE_ELEMENT_VALUE(__VIRTADDR__,__DATA__,__CRC__) (((EE_ELEMENT_TYPE)(__DATA__) << EE_DATA_SHIFT) | (__CRC__) << EE_CRC_SHIFT | (__VIRTADDR__)) /*!< Get element value from virtual addr, data and crc values */
124 
125 /**
126   * @}
127   */
128 
129 /**
130   * @}
131   */
132 
133 /* Exported constants --------------------------------------------------------*/
134 /* Exported types ------------------------------------------------------------*/
135 /* Exported macro ------------------------------------------------------------*/
136 /* Exported functions ------------------------------------------------------- */
137 /** @defgroup EEPROM_Exported_Functions EEPROM Exported Functions
138   * @{
139   */
140 EE_Status EE_Format(EE_Erase_type EraseType);
141 EE_Status EE_Init(uint16_t* VirtAddTab, EE_Erase_type EraseType);
142 #if defined(EE_ACCESS_32BITS)
143 EE_Status EE_ReadVariable32bits(uint16_t VirtAddress, uint32_t* pData);
144 EE_Status EE_WriteVariable32bits(uint16_t VirtAddress, uint32_t Data);
145 #endif
146 EE_Status EE_ReadVariable16bits(uint16_t VirtAddress, uint16_t* pData);
147 EE_Status EE_WriteVariable16bits(uint16_t VirtAddress, uint16_t Data);
148 EE_Status EE_ReadVariable8bits(uint16_t VirtAddress, uint8_t* pData);
149 EE_Status EE_WriteVariable8bits(uint16_t VirtAddress, uint8_t Data);
150 EE_Status EE_CleanUp(void);
151 EE_Status EE_CleanUp_IT(void);
152 EE_Status EE_DeleteCorruptedFlashAddress(uint32_t Address);
153 void EE_EndOfCleanup_UserCallback(void);
154 
155 /**
156   * @}
157   */
158 
159 /**
160   * @}
161   */
162 
163 #endif /* __EEPROM_EMUL_H */
164 
165 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
166