asdf
Dependencies: mbed
Fork of 00_eeprom_emulation_f401 by
eeprom/eeprom.cpp@1:bc0ee9ad46aa, 2016-09-27 (annotated)
- Committer:
- mega64
- Date:
- Tue Sep 27 20:36:44 2016 +0000
- Revision:
- 1:bc0ee9ad46aa
- Parent:
- 0:1756c3542c95
bug fix in EE_VerifyPageFullyErased
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
mega64 | 0:1756c3542c95 | 1 | /** |
mega64 | 0:1756c3542c95 | 2 | ****************************************************************************** |
mega64 | 0:1756c3542c95 | 3 | * @file EEPROM/EEPROM_Emulation/src/eeprom.c |
mega64 | 0:1756c3542c95 | 4 | * @author MCD Application Team |
mega64 | 0:1756c3542c95 | 5 | * @version V1.0.1 |
mega64 | 0:1756c3542c95 | 6 | * @date 29-January-2016 |
mega64 | 0:1756c3542c95 | 7 | * @brief This file provides all the EEPROM emulation firmware functions. |
mega64 | 0:1756c3542c95 | 8 | ****************************************************************************** |
mega64 | 0:1756c3542c95 | 9 | * @attention |
mega64 | 0:1756c3542c95 | 10 | * |
mega64 | 0:1756c3542c95 | 11 | * <h2><center>© COPYRIGHT(c) 2016 STMicroelectronics</center></h2> |
mega64 | 0:1756c3542c95 | 12 | * |
mega64 | 0:1756c3542c95 | 13 | * Redistribution and use in source and binary forms, with or without modification, |
mega64 | 0:1756c3542c95 | 14 | * are permitted provided that the following conditions are met: |
mega64 | 0:1756c3542c95 | 15 | * 1. Redistributions of source code must retain the above copyright notice, |
mega64 | 0:1756c3542c95 | 16 | * this list of conditions and the following disclaimer. |
mega64 | 0:1756c3542c95 | 17 | * 2. Redistributions in binary form must reproduce the above copyright notice, |
mega64 | 0:1756c3542c95 | 18 | * this list of conditions and the following disclaimer in the documentation |
mega64 | 0:1756c3542c95 | 19 | * and/or other materials provided with the distribution. |
mega64 | 0:1756c3542c95 | 20 | * 3. Neither the name of STMicroelectronics nor the names of its contributors |
mega64 | 0:1756c3542c95 | 21 | * may be used to endorse or promote products derived from this software |
mega64 | 0:1756c3542c95 | 22 | * without specific prior written permission. |
mega64 | 0:1756c3542c95 | 23 | * |
mega64 | 0:1756c3542c95 | 24 | * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
mega64 | 0:1756c3542c95 | 25 | * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
mega64 | 0:1756c3542c95 | 26 | * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE |
mega64 | 0:1756c3542c95 | 27 | * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE |
mega64 | 0:1756c3542c95 | 28 | * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL |
mega64 | 0:1756c3542c95 | 29 | * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR |
mega64 | 0:1756c3542c95 | 30 | * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER |
mega64 | 0:1756c3542c95 | 31 | * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, |
mega64 | 0:1756c3542c95 | 32 | * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE |
mega64 | 0:1756c3542c95 | 33 | * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. |
mega64 | 0:1756c3542c95 | 34 | * |
mega64 | 0:1756c3542c95 | 35 | ****************************************************************************** |
mega64 | 0:1756c3542c95 | 36 | */ |
mega64 | 0:1756c3542c95 | 37 | |
mega64 | 0:1756c3542c95 | 38 | /** @addtogroup EEPROM_Emulation |
mega64 | 0:1756c3542c95 | 39 | * @{ |
mega64 | 0:1756c3542c95 | 40 | */ |
mega64 | 0:1756c3542c95 | 41 | |
mega64 | 0:1756c3542c95 | 42 | /* Includes ------------------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 43 | #include "eeprom.h" |
mega64 | 0:1756c3542c95 | 44 | |
mega64 | 0:1756c3542c95 | 45 | /* Private typedef -----------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 46 | /* Private define ------------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 47 | /* Private macro -------------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 48 | /* Private variables ---------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 49 | |
mega64 | 1:bc0ee9ad46aa | 50 | /* Dummy variables to protect eeprom pages if code size is bigger than 32kb (F401) |
mega64 | 0:1756c3542c95 | 51 | needed in Mbed online compiler to avoid conflict with linker (N.S.) */ |
mega64 | 1:bc0ee9ad46aa | 52 | const uint8_t Eeprom_area0[PAGE_SIZE] __attribute__((at(PAGE0_BASE_ADDRESS),used))={ [0 ... (PAGE_SIZE-1)] = 0xFF }; |
mega64 | 1:bc0ee9ad46aa | 53 | const uint8_t Eeprom_area1[PAGE_SIZE] __attribute__((at(PAGE1_BASE_ADDRESS),used))={ [0 ... (PAGE_SIZE-1)] = 0xFF }; |
mega64 | 0:1756c3542c95 | 54 | |
mega64 | 0:1756c3542c95 | 55 | |
mega64 | 0:1756c3542c95 | 56 | /* Global variable used to store variable value in read sequence */ |
mega64 | 0:1756c3542c95 | 57 | uint16_t DataVar = 0; |
mega64 | 0:1756c3542c95 | 58 | |
mega64 | 0:1756c3542c95 | 59 | /* Virtual address defined by the user: 0xFFFF value is prohibited */ |
mega64 | 0:1756c3542c95 | 60 | extern uint16_t VirtAddVarTab[NB_OF_VAR]; |
mega64 | 0:1756c3542c95 | 61 | |
mega64 | 0:1756c3542c95 | 62 | /* Private function prototypes -----------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 63 | /* Private functions ---------------------------------------------------------*/ |
mega64 | 0:1756c3542c95 | 64 | static HAL_StatusTypeDef EE_Format(void); |
mega64 | 0:1756c3542c95 | 65 | static uint16_t EE_FindValidPage(uint8_t Operation); |
mega64 | 0:1756c3542c95 | 66 | static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data); |
mega64 | 0:1756c3542c95 | 67 | static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data); |
mega64 | 0:1756c3542c95 | 68 | static uint16_t EE_VerifyPageFullyErased(uint32_t Address); |
mega64 | 0:1756c3542c95 | 69 | |
mega64 | 0:1756c3542c95 | 70 | /** |
mega64 | 0:1756c3542c95 | 71 | * @brief Restore the pages to a known good state in case of page's status |
mega64 | 0:1756c3542c95 | 72 | * corruption after a power loss. |
mega64 | 0:1756c3542c95 | 73 | * @param None. |
mega64 | 0:1756c3542c95 | 74 | * @retval - Flash error code: on write Flash error |
mega64 | 0:1756c3542c95 | 75 | * - FLASH_COMPLETE: on success |
mega64 | 0:1756c3542c95 | 76 | */ |
mega64 | 0:1756c3542c95 | 77 | uint16_t EE_Init(void) |
mega64 | 0:1756c3542c95 | 78 | { |
mega64 | 0:1756c3542c95 | 79 | uint16_t PageStatus0 = 6, PageStatus1 = 6; |
mega64 | 0:1756c3542c95 | 80 | uint16_t VarIdx = 0; |
mega64 | 0:1756c3542c95 | 81 | uint16_t EepromStatus = 0, ReadStatus = 0; |
mega64 | 0:1756c3542c95 | 82 | int16_t x = -1; |
mega64 | 0:1756c3542c95 | 83 | HAL_StatusTypeDef FlashStatus; |
mega64 | 0:1756c3542c95 | 84 | uint32_t SectorError = 0; |
mega64 | 0:1756c3542c95 | 85 | FLASH_EraseInitTypeDef pEraseInit; |
mega64 | 0:1756c3542c95 | 86 | |
mega64 | 0:1756c3542c95 | 87 | |
mega64 | 0:1756c3542c95 | 88 | /* Get Page0 status */ |
mega64 | 0:1756c3542c95 | 89 | PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); |
mega64 | 0:1756c3542c95 | 90 | /* Get Page1 status */ |
mega64 | 0:1756c3542c95 | 91 | PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); |
mega64 | 0:1756c3542c95 | 92 | |
mega64 | 0:1756c3542c95 | 93 | pEraseInit.TypeErase = TYPEERASE_SECTORS; |
mega64 | 0:1756c3542c95 | 94 | pEraseInit.Sector = PAGE0_ID; |
mega64 | 0:1756c3542c95 | 95 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 96 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 97 | |
mega64 | 0:1756c3542c95 | 98 | /* Check for invalid header states and repair if necessary */ |
mega64 | 0:1756c3542c95 | 99 | switch (PageStatus0) |
mega64 | 0:1756c3542c95 | 100 | { |
mega64 | 0:1756c3542c95 | 101 | case ERASED: |
mega64 | 0:1756c3542c95 | 102 | if (PageStatus1 == VALID_PAGE) /* Page0 erased, Page1 valid */ |
mega64 | 0:1756c3542c95 | 103 | { |
mega64 | 0:1756c3542c95 | 104 | /* Erase Page0 */ |
mega64 | 0:1756c3542c95 | 105 | if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 106 | { |
mega64 | 0:1756c3542c95 | 107 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 108 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 109 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 110 | { |
mega64 | 0:1756c3542c95 | 111 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 112 | } |
mega64 | 0:1756c3542c95 | 113 | } |
mega64 | 0:1756c3542c95 | 114 | } |
mega64 | 0:1756c3542c95 | 115 | else if (PageStatus1 == RECEIVE_DATA) /* Page0 erased, Page1 receive */ |
mega64 | 0:1756c3542c95 | 116 | { |
mega64 | 0:1756c3542c95 | 117 | /* Erase Page0 */ |
mega64 | 0:1756c3542c95 | 118 | if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 119 | { |
mega64 | 0:1756c3542c95 | 120 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 121 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 122 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 123 | { |
mega64 | 0:1756c3542c95 | 124 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 125 | } |
mega64 | 0:1756c3542c95 | 126 | } |
mega64 | 0:1756c3542c95 | 127 | /* Mark Page1 as valid */ |
mega64 | 0:1756c3542c95 | 128 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 129 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 130 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 131 | { |
mega64 | 0:1756c3542c95 | 132 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 133 | } |
mega64 | 0:1756c3542c95 | 134 | } |
mega64 | 0:1756c3542c95 | 135 | else /* First EEPROM access (Page0&1 are erased) or invalid state -> format EEPROM */ |
mega64 | 0:1756c3542c95 | 136 | { |
mega64 | 0:1756c3542c95 | 137 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
mega64 | 0:1756c3542c95 | 138 | FlashStatus = EE_Format(); |
mega64 | 0:1756c3542c95 | 139 | /* If erase/program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 140 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 141 | { |
mega64 | 0:1756c3542c95 | 142 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 143 | } |
mega64 | 0:1756c3542c95 | 144 | } |
mega64 | 0:1756c3542c95 | 145 | break; |
mega64 | 0:1756c3542c95 | 146 | |
mega64 | 0:1756c3542c95 | 147 | case RECEIVE_DATA: |
mega64 | 0:1756c3542c95 | 148 | if (PageStatus1 == VALID_PAGE) /* Page0 receive, Page1 valid */ |
mega64 | 0:1756c3542c95 | 149 | { |
mega64 | 0:1756c3542c95 | 150 | /* Transfer data from Page1 to Page0 */ |
mega64 | 0:1756c3542c95 | 151 | for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) |
mega64 | 0:1756c3542c95 | 152 | { |
mega64 | 0:1756c3542c95 | 153 | if (( *(__IO uint16_t*)(PAGE0_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) |
mega64 | 0:1756c3542c95 | 154 | { |
mega64 | 0:1756c3542c95 | 155 | x = VarIdx; |
mega64 | 0:1756c3542c95 | 156 | } |
mega64 | 0:1756c3542c95 | 157 | if (VarIdx != x) |
mega64 | 0:1756c3542c95 | 158 | { |
mega64 | 0:1756c3542c95 | 159 | /* Read the last variables' updates */ |
mega64 | 0:1756c3542c95 | 160 | ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); |
mega64 | 0:1756c3542c95 | 161 | /* In case variable corresponding to the virtual address was found */ |
mega64 | 0:1756c3542c95 | 162 | if (ReadStatus != 0x1) |
mega64 | 0:1756c3542c95 | 163 | { |
mega64 | 0:1756c3542c95 | 164 | /* Transfer the variable to the Page0 */ |
mega64 | 0:1756c3542c95 | 165 | EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); |
mega64 | 0:1756c3542c95 | 166 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 167 | if (EepromStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 168 | { |
mega64 | 0:1756c3542c95 | 169 | return EepromStatus; |
mega64 | 0:1756c3542c95 | 170 | } |
mega64 | 0:1756c3542c95 | 171 | } |
mega64 | 0:1756c3542c95 | 172 | } |
mega64 | 0:1756c3542c95 | 173 | } |
mega64 | 0:1756c3542c95 | 174 | /* Mark Page0 as valid */ |
mega64 | 0:1756c3542c95 | 175 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 176 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 177 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 178 | { |
mega64 | 0:1756c3542c95 | 179 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 180 | } |
mega64 | 0:1756c3542c95 | 181 | pEraseInit.Sector = PAGE1_ID; |
mega64 | 0:1756c3542c95 | 182 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 183 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 184 | /* Erase Page1 */ |
mega64 | 0:1756c3542c95 | 185 | if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 186 | { |
mega64 | 0:1756c3542c95 | 187 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 188 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 189 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 190 | { |
mega64 | 0:1756c3542c95 | 191 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 192 | } |
mega64 | 0:1756c3542c95 | 193 | } |
mega64 | 0:1756c3542c95 | 194 | } |
mega64 | 0:1756c3542c95 | 195 | else if (PageStatus1 == ERASED) /* Page0 receive, Page1 erased */ |
mega64 | 0:1756c3542c95 | 196 | { |
mega64 | 0:1756c3542c95 | 197 | pEraseInit.Sector = PAGE1_ID; |
mega64 | 0:1756c3542c95 | 198 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 199 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 200 | /* Erase Page1 */ |
mega64 | 0:1756c3542c95 | 201 | if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 202 | { |
mega64 | 0:1756c3542c95 | 203 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 204 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 205 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 206 | { |
mega64 | 0:1756c3542c95 | 207 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 208 | } |
mega64 | 0:1756c3542c95 | 209 | } |
mega64 | 0:1756c3542c95 | 210 | /* Mark Page0 as valid */ |
mega64 | 0:1756c3542c95 | 211 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 212 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 213 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 214 | { |
mega64 | 0:1756c3542c95 | 215 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 216 | } |
mega64 | 0:1756c3542c95 | 217 | } |
mega64 | 0:1756c3542c95 | 218 | else /* Invalid state -> format eeprom */ |
mega64 | 0:1756c3542c95 | 219 | { |
mega64 | 0:1756c3542c95 | 220 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
mega64 | 0:1756c3542c95 | 221 | FlashStatus = EE_Format(); |
mega64 | 0:1756c3542c95 | 222 | /* If erase/program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 223 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 224 | { |
mega64 | 0:1756c3542c95 | 225 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 226 | } |
mega64 | 0:1756c3542c95 | 227 | } |
mega64 | 0:1756c3542c95 | 228 | break; |
mega64 | 0:1756c3542c95 | 229 | |
mega64 | 0:1756c3542c95 | 230 | case VALID_PAGE: |
mega64 | 0:1756c3542c95 | 231 | if (PageStatus1 == VALID_PAGE) /* Invalid state -> format eeprom */ |
mega64 | 0:1756c3542c95 | 232 | { |
mega64 | 0:1756c3542c95 | 233 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
mega64 | 0:1756c3542c95 | 234 | FlashStatus = EE_Format(); |
mega64 | 0:1756c3542c95 | 235 | /* If erase/program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 236 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 237 | { |
mega64 | 0:1756c3542c95 | 238 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 239 | } |
mega64 | 0:1756c3542c95 | 240 | } |
mega64 | 0:1756c3542c95 | 241 | else if (PageStatus1 == ERASED) /* Page0 valid, Page1 erased */ |
mega64 | 0:1756c3542c95 | 242 | { |
mega64 | 0:1756c3542c95 | 243 | pEraseInit.Sector = PAGE1_ID; |
mega64 | 0:1756c3542c95 | 244 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 245 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 246 | /* Erase Page1 */ |
mega64 | 0:1756c3542c95 | 247 | if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 248 | { |
mega64 | 0:1756c3542c95 | 249 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 250 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 251 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 252 | { |
mega64 | 0:1756c3542c95 | 253 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 254 | } |
mega64 | 0:1756c3542c95 | 255 | } |
mega64 | 0:1756c3542c95 | 256 | } |
mega64 | 0:1756c3542c95 | 257 | else /* Page0 valid, Page1 receive */ |
mega64 | 0:1756c3542c95 | 258 | { |
mega64 | 0:1756c3542c95 | 259 | /* Transfer data from Page0 to Page1 */ |
mega64 | 0:1756c3542c95 | 260 | for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) |
mega64 | 0:1756c3542c95 | 261 | { |
mega64 | 0:1756c3542c95 | 262 | if ((*(__IO uint16_t*)(PAGE1_BASE_ADDRESS + 6)) == VirtAddVarTab[VarIdx]) |
mega64 | 0:1756c3542c95 | 263 | { |
mega64 | 0:1756c3542c95 | 264 | x = VarIdx; |
mega64 | 0:1756c3542c95 | 265 | } |
mega64 | 0:1756c3542c95 | 266 | if (VarIdx != x) |
mega64 | 0:1756c3542c95 | 267 | { |
mega64 | 0:1756c3542c95 | 268 | /* Read the last variables' updates */ |
mega64 | 0:1756c3542c95 | 269 | ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); |
mega64 | 0:1756c3542c95 | 270 | /* In case variable corresponding to the virtual address was found */ |
mega64 | 0:1756c3542c95 | 271 | if (ReadStatus != 0x1) |
mega64 | 0:1756c3542c95 | 272 | { |
mega64 | 0:1756c3542c95 | 273 | /* Transfer the variable to the Page1 */ |
mega64 | 0:1756c3542c95 | 274 | EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); |
mega64 | 0:1756c3542c95 | 275 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 276 | if (EepromStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 277 | { |
mega64 | 0:1756c3542c95 | 278 | return EepromStatus; |
mega64 | 0:1756c3542c95 | 279 | } |
mega64 | 0:1756c3542c95 | 280 | } |
mega64 | 0:1756c3542c95 | 281 | } |
mega64 | 0:1756c3542c95 | 282 | } |
mega64 | 0:1756c3542c95 | 283 | /* Mark Page1 as valid */ |
mega64 | 0:1756c3542c95 | 284 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE1_BASE_ADDRESS, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 285 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 286 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 287 | { |
mega64 | 0:1756c3542c95 | 288 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 289 | } |
mega64 | 0:1756c3542c95 | 290 | pEraseInit.Sector = PAGE0_ID; |
mega64 | 0:1756c3542c95 | 291 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 292 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 293 | /* Erase Page0 */ |
mega64 | 0:1756c3542c95 | 294 | if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 295 | { |
mega64 | 0:1756c3542c95 | 296 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 297 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 298 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 299 | { |
mega64 | 0:1756c3542c95 | 300 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 301 | } |
mega64 | 0:1756c3542c95 | 302 | } |
mega64 | 0:1756c3542c95 | 303 | } |
mega64 | 0:1756c3542c95 | 304 | break; |
mega64 | 0:1756c3542c95 | 305 | |
mega64 | 0:1756c3542c95 | 306 | default: /* Any other state -> format eeprom */ |
mega64 | 0:1756c3542c95 | 307 | /* Erase both Page0 and Page1 and set Page0 as valid page */ |
mega64 | 0:1756c3542c95 | 308 | FlashStatus = EE_Format(); |
mega64 | 0:1756c3542c95 | 309 | /* If erase/program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 310 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 311 | { |
mega64 | 0:1756c3542c95 | 312 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 313 | } |
mega64 | 0:1756c3542c95 | 314 | break; |
mega64 | 0:1756c3542c95 | 315 | } |
mega64 | 0:1756c3542c95 | 316 | |
mega64 | 0:1756c3542c95 | 317 | return HAL_OK; |
mega64 | 0:1756c3542c95 | 318 | } |
mega64 | 0:1756c3542c95 | 319 | |
mega64 | 0:1756c3542c95 | 320 | /** |
mega64 | 0:1756c3542c95 | 321 | * @brief Verify if specified page is fully erased. |
mega64 | 0:1756c3542c95 | 322 | * @param Address: page address |
mega64 | 0:1756c3542c95 | 323 | * This parameter can be one of the following values: |
mega64 | 0:1756c3542c95 | 324 | * @arg PAGE0_BASE_ADDRESS: Page0 base address |
mega64 | 0:1756c3542c95 | 325 | * @arg PAGE1_BASE_ADDRESS: Page1 base address |
mega64 | 0:1756c3542c95 | 326 | * @retval page fully erased status: |
mega64 | 0:1756c3542c95 | 327 | * - 0: if Page not erased |
mega64 | 0:1756c3542c95 | 328 | * - 1: if Page erased |
mega64 | 0:1756c3542c95 | 329 | */ |
mega64 | 0:1756c3542c95 | 330 | uint16_t EE_VerifyPageFullyErased(uint32_t Address) |
mega64 | 0:1756c3542c95 | 331 | { |
mega64 | 1:bc0ee9ad46aa | 332 | uint32_t readstatus = 1; |
mega64 | 1:bc0ee9ad46aa | 333 | uint16_t addressvalue = 0x5555; |
mega64 | 1:bc0ee9ad46aa | 334 | uint32_t end_address; |
mega64 | 1:bc0ee9ad46aa | 335 | |
mega64 | 1:bc0ee9ad46aa | 336 | if (PAGE0_BASE_ADDRESS==Address) |
mega64 | 1:bc0ee9ad46aa | 337 | { |
mega64 | 1:bc0ee9ad46aa | 338 | end_address = PAGE0_END_ADDRESS; |
mega64 | 1:bc0ee9ad46aa | 339 | } |
mega64 | 1:bc0ee9ad46aa | 340 | else |
mega64 | 1:bc0ee9ad46aa | 341 | { |
mega64 | 1:bc0ee9ad46aa | 342 | end_address = PAGE1_END_ADDRESS; |
mega64 | 1:bc0ee9ad46aa | 343 | }; |
mega64 | 1:bc0ee9ad46aa | 344 | |
mega64 | 0:1756c3542c95 | 345 | |
mega64 | 0:1756c3542c95 | 346 | /* Check each active page address starting from end */ |
mega64 | 1:bc0ee9ad46aa | 347 | while (Address <= end_address) |
mega64 | 0:1756c3542c95 | 348 | { |
mega64 | 0:1756c3542c95 | 349 | /* Get the current location content to be compared with virtual address */ |
mega64 | 1:bc0ee9ad46aa | 350 | addressvalue = (*(__IO uint16_t*)Address); |
mega64 | 0:1756c3542c95 | 351 | |
mega64 | 0:1756c3542c95 | 352 | /* Compare the read address with the virtual address */ |
mega64 | 1:bc0ee9ad46aa | 353 | if (addressvalue != ERASED) |
mega64 | 0:1756c3542c95 | 354 | { |
mega64 | 0:1756c3542c95 | 355 | |
mega64 | 1:bc0ee9ad46aa | 356 | /* In case variable value is read, reset readstatus flag */ |
mega64 | 1:bc0ee9ad46aa | 357 | readstatus = 0; |
mega64 | 0:1756c3542c95 | 358 | |
mega64 | 0:1756c3542c95 | 359 | break; |
mega64 | 0:1756c3542c95 | 360 | } |
mega64 | 0:1756c3542c95 | 361 | /* Next address location */ |
mega64 | 0:1756c3542c95 | 362 | Address = Address + 4; |
mega64 | 0:1756c3542c95 | 363 | } |
mega64 | 0:1756c3542c95 | 364 | |
mega64 | 1:bc0ee9ad46aa | 365 | /* Return readstatus value: (0: Page not erased, 1: Page erased) */ |
mega64 | 1:bc0ee9ad46aa | 366 | return readstatus; |
mega64 | 0:1756c3542c95 | 367 | } |
mega64 | 0:1756c3542c95 | 368 | |
mega64 | 0:1756c3542c95 | 369 | /** |
mega64 | 0:1756c3542c95 | 370 | * @brief Returns the last stored variable data, if found, which correspond to |
mega64 | 0:1756c3542c95 | 371 | * the passed virtual address |
mega64 | 0:1756c3542c95 | 372 | * @param VirtAddress: Variable virtual address |
mega64 | 0:1756c3542c95 | 373 | * @param Data: Global variable contains the read variable value |
mega64 | 0:1756c3542c95 | 374 | * @retval Success or error status: |
mega64 | 0:1756c3542c95 | 375 | * - 0: if variable was found |
mega64 | 0:1756c3542c95 | 376 | * - 1: if the variable was not found |
mega64 | 0:1756c3542c95 | 377 | * - NO_VALID_PAGE: if no valid page was found. |
mega64 | 0:1756c3542c95 | 378 | */ |
mega64 | 0:1756c3542c95 | 379 | uint16_t EE_ReadVariable(uint16_t VirtAddress, uint16_t* Data) |
mega64 | 0:1756c3542c95 | 380 | { |
mega64 | 0:1756c3542c95 | 381 | uint16_t ValidPage = PAGE0; |
mega64 | 0:1756c3542c95 | 382 | uint16_t AddressValue = 0x5555, ReadStatus = 1; |
mega64 | 0:1756c3542c95 | 383 | uint32_t Address = EEPROM_START_ADDRESS, PageStartAddress = EEPROM_START_ADDRESS; |
mega64 | 0:1756c3542c95 | 384 | |
mega64 | 0:1756c3542c95 | 385 | /* Get active Page for read operation */ |
mega64 | 0:1756c3542c95 | 386 | ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE); |
mega64 | 0:1756c3542c95 | 387 | |
mega64 | 0:1756c3542c95 | 388 | /* Check if there is no valid page */ |
mega64 | 0:1756c3542c95 | 389 | if (ValidPage == NO_VALID_PAGE) |
mega64 | 0:1756c3542c95 | 390 | { |
mega64 | 0:1756c3542c95 | 391 | return NO_VALID_PAGE; |
mega64 | 0:1756c3542c95 | 392 | } |
mega64 | 0:1756c3542c95 | 393 | |
mega64 | 0:1756c3542c95 | 394 | /* Get the valid Page start Address */ |
mega64 | 0:1756c3542c95 | 395 | PageStartAddress = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE)); |
mega64 | 0:1756c3542c95 | 396 | |
mega64 | 0:1756c3542c95 | 397 | /* Get the valid Page end Address */ |
mega64 | 0:1756c3542c95 | 398 | Address = (uint32_t)((EEPROM_START_ADDRESS - 2) + (uint32_t)((1 + ValidPage) * PAGE_SIZE)); |
mega64 | 0:1756c3542c95 | 399 | |
mega64 | 0:1756c3542c95 | 400 | /* Check each active page address starting from end */ |
mega64 | 0:1756c3542c95 | 401 | while (Address > (PageStartAddress + 2)) |
mega64 | 0:1756c3542c95 | 402 | { |
mega64 | 0:1756c3542c95 | 403 | /* Get the current location content to be compared with virtual address */ |
mega64 | 0:1756c3542c95 | 404 | AddressValue = (*(__IO uint16_t*)Address); |
mega64 | 0:1756c3542c95 | 405 | |
mega64 | 0:1756c3542c95 | 406 | /* Compare the read address with the virtual address */ |
mega64 | 0:1756c3542c95 | 407 | if (AddressValue == VirtAddress) |
mega64 | 0:1756c3542c95 | 408 | { |
mega64 | 0:1756c3542c95 | 409 | /* Get content of Address-2 which is variable value */ |
mega64 | 0:1756c3542c95 | 410 | *Data = (*(__IO uint16_t*)(Address - 2)); |
mega64 | 0:1756c3542c95 | 411 | |
mega64 | 0:1756c3542c95 | 412 | /* In case variable value is read, reset ReadStatus flag */ |
mega64 | 0:1756c3542c95 | 413 | ReadStatus = 0; |
mega64 | 0:1756c3542c95 | 414 | |
mega64 | 0:1756c3542c95 | 415 | break; |
mega64 | 0:1756c3542c95 | 416 | } |
mega64 | 0:1756c3542c95 | 417 | else |
mega64 | 0:1756c3542c95 | 418 | { |
mega64 | 0:1756c3542c95 | 419 | /* Next address location */ |
mega64 | 0:1756c3542c95 | 420 | Address = Address - 4; |
mega64 | 0:1756c3542c95 | 421 | } |
mega64 | 0:1756c3542c95 | 422 | } |
mega64 | 0:1756c3542c95 | 423 | |
mega64 | 0:1756c3542c95 | 424 | /* Return ReadStatus value: (0: variable exist, 1: variable doesn't exist) */ |
mega64 | 0:1756c3542c95 | 425 | return ReadStatus; |
mega64 | 0:1756c3542c95 | 426 | } |
mega64 | 0:1756c3542c95 | 427 | |
mega64 | 0:1756c3542c95 | 428 | /** |
mega64 | 0:1756c3542c95 | 429 | * @brief Writes/upadtes variable data in EEPROM. |
mega64 | 0:1756c3542c95 | 430 | * @param VirtAddress: Variable virtual address |
mega64 | 0:1756c3542c95 | 431 | * @param Data: 16 bit data to be written |
mega64 | 0:1756c3542c95 | 432 | * @retval Success or error status: |
mega64 | 0:1756c3542c95 | 433 | * - FLASH_COMPLETE: on success |
mega64 | 0:1756c3542c95 | 434 | * - PAGE_FULL: if valid page is full |
mega64 | 0:1756c3542c95 | 435 | * - NO_VALID_PAGE: if no valid page was found |
mega64 | 0:1756c3542c95 | 436 | * - Flash error code: on write Flash error |
mega64 | 0:1756c3542c95 | 437 | */ |
mega64 | 0:1756c3542c95 | 438 | uint16_t EE_WriteVariable(uint16_t VirtAddress, uint16_t Data) |
mega64 | 0:1756c3542c95 | 439 | { |
mega64 | 0:1756c3542c95 | 440 | uint16_t Status = 0; |
mega64 | 0:1756c3542c95 | 441 | |
mega64 | 0:1756c3542c95 | 442 | /* Write the variable virtual address and value in the EEPROM */ |
mega64 | 0:1756c3542c95 | 443 | Status = EE_VerifyPageFullWriteVariable(VirtAddress, Data); |
mega64 | 0:1756c3542c95 | 444 | |
mega64 | 0:1756c3542c95 | 445 | /* In case the EEPROM active page is full */ |
mega64 | 0:1756c3542c95 | 446 | if (Status == PAGE_FULL) |
mega64 | 0:1756c3542c95 | 447 | { |
mega64 | 0:1756c3542c95 | 448 | /* Perform Page transfer */ |
mega64 | 0:1756c3542c95 | 449 | Status = EE_PageTransfer(VirtAddress, Data); |
mega64 | 0:1756c3542c95 | 450 | } |
mega64 | 0:1756c3542c95 | 451 | |
mega64 | 0:1756c3542c95 | 452 | /* Return last operation status */ |
mega64 | 0:1756c3542c95 | 453 | return Status; |
mega64 | 0:1756c3542c95 | 454 | } |
mega64 | 0:1756c3542c95 | 455 | |
mega64 | 0:1756c3542c95 | 456 | /** |
mega64 | 0:1756c3542c95 | 457 | * @brief Erases PAGE and PAGE1 and writes VALID_PAGE header to PAGE |
mega64 | 0:1756c3542c95 | 458 | * @param None |
mega64 | 0:1756c3542c95 | 459 | * @retval Status of the last operation (Flash write or erase) done during |
mega64 | 0:1756c3542c95 | 460 | * EEPROM formating |
mega64 | 0:1756c3542c95 | 461 | */ |
mega64 | 0:1756c3542c95 | 462 | static HAL_StatusTypeDef EE_Format(void) |
mega64 | 0:1756c3542c95 | 463 | { |
mega64 | 0:1756c3542c95 | 464 | HAL_StatusTypeDef FlashStatus = HAL_OK; |
mega64 | 0:1756c3542c95 | 465 | uint32_t SectorError = 0; |
mega64 | 0:1756c3542c95 | 466 | FLASH_EraseInitTypeDef pEraseInit; |
mega64 | 0:1756c3542c95 | 467 | |
mega64 | 0:1756c3542c95 | 468 | pEraseInit.TypeErase = FLASH_TYPEERASE_SECTORS; |
mega64 | 0:1756c3542c95 | 469 | pEraseInit.Sector = PAGE0_ID; |
mega64 | 0:1756c3542c95 | 470 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 471 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 472 | /* Erase Page0 */ |
mega64 | 0:1756c3542c95 | 473 | if(!EE_VerifyPageFullyErased(PAGE0_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 474 | { |
mega64 | 0:1756c3542c95 | 475 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 476 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 477 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 478 | { |
mega64 | 0:1756c3542c95 | 479 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 480 | } |
mega64 | 0:1756c3542c95 | 481 | } |
mega64 | 0:1756c3542c95 | 482 | /* Set Page0 as valid page: Write VALID_PAGE at Page0 base address */ |
mega64 | 0:1756c3542c95 | 483 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, PAGE0_BASE_ADDRESS, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 484 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 485 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 486 | { |
mega64 | 0:1756c3542c95 | 487 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 488 | } |
mega64 | 0:1756c3542c95 | 489 | |
mega64 | 0:1756c3542c95 | 490 | pEraseInit.Sector = PAGE1_ID; |
mega64 | 0:1756c3542c95 | 491 | /* Erase Page1 */ |
mega64 | 0:1756c3542c95 | 492 | if(!EE_VerifyPageFullyErased(PAGE1_BASE_ADDRESS)) |
mega64 | 0:1756c3542c95 | 493 | { |
mega64 | 0:1756c3542c95 | 494 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 495 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 496 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 497 | { |
mega64 | 0:1756c3542c95 | 498 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 499 | } |
mega64 | 0:1756c3542c95 | 500 | } |
mega64 | 0:1756c3542c95 | 501 | |
mega64 | 0:1756c3542c95 | 502 | return HAL_OK; |
mega64 | 0:1756c3542c95 | 503 | } |
mega64 | 0:1756c3542c95 | 504 | |
mega64 | 0:1756c3542c95 | 505 | /** |
mega64 | 0:1756c3542c95 | 506 | * @brief Find valid Page for write or read operation |
mega64 | 0:1756c3542c95 | 507 | * @param Operation: operation to achieve on the valid page. |
mega64 | 0:1756c3542c95 | 508 | * This parameter can be one of the following values: |
mega64 | 0:1756c3542c95 | 509 | * @arg READ_FROM_VALID_PAGE: read operation from valid page |
mega64 | 0:1756c3542c95 | 510 | * @arg WRITE_IN_VALID_PAGE: write operation from valid page |
mega64 | 0:1756c3542c95 | 511 | * @retval Valid page number (PAGE or PAGE1) or NO_VALID_PAGE in case |
mega64 | 0:1756c3542c95 | 512 | * of no valid page was found |
mega64 | 0:1756c3542c95 | 513 | */ |
mega64 | 0:1756c3542c95 | 514 | static uint16_t EE_FindValidPage(uint8_t Operation) |
mega64 | 0:1756c3542c95 | 515 | { |
mega64 | 0:1756c3542c95 | 516 | uint16_t PageStatus0 = 6, PageStatus1 = 6; |
mega64 | 0:1756c3542c95 | 517 | |
mega64 | 0:1756c3542c95 | 518 | /* Get Page0 actual status */ |
mega64 | 0:1756c3542c95 | 519 | PageStatus0 = (*(__IO uint16_t*)PAGE0_BASE_ADDRESS); |
mega64 | 0:1756c3542c95 | 520 | |
mega64 | 0:1756c3542c95 | 521 | /* Get Page1 actual status */ |
mega64 | 0:1756c3542c95 | 522 | PageStatus1 = (*(__IO uint16_t*)PAGE1_BASE_ADDRESS); |
mega64 | 0:1756c3542c95 | 523 | |
mega64 | 0:1756c3542c95 | 524 | /* Write or read operation */ |
mega64 | 0:1756c3542c95 | 525 | switch (Operation) |
mega64 | 0:1756c3542c95 | 526 | { |
mega64 | 0:1756c3542c95 | 527 | case WRITE_IN_VALID_PAGE: /* ---- Write operation ---- */ |
mega64 | 0:1756c3542c95 | 528 | if (PageStatus1 == VALID_PAGE) |
mega64 | 0:1756c3542c95 | 529 | { |
mega64 | 0:1756c3542c95 | 530 | /* Page0 receiving data */ |
mega64 | 0:1756c3542c95 | 531 | if (PageStatus0 == RECEIVE_DATA) |
mega64 | 0:1756c3542c95 | 532 | { |
mega64 | 0:1756c3542c95 | 533 | return PAGE0; /* Page0 valid */ |
mega64 | 0:1756c3542c95 | 534 | } |
mega64 | 0:1756c3542c95 | 535 | else |
mega64 | 0:1756c3542c95 | 536 | { |
mega64 | 0:1756c3542c95 | 537 | return PAGE1; /* Page1 valid */ |
mega64 | 0:1756c3542c95 | 538 | } |
mega64 | 0:1756c3542c95 | 539 | } |
mega64 | 0:1756c3542c95 | 540 | else if (PageStatus0 == VALID_PAGE) |
mega64 | 0:1756c3542c95 | 541 | { |
mega64 | 0:1756c3542c95 | 542 | /* Page1 receiving data */ |
mega64 | 0:1756c3542c95 | 543 | if (PageStatus1 == RECEIVE_DATA) |
mega64 | 0:1756c3542c95 | 544 | { |
mega64 | 0:1756c3542c95 | 545 | return PAGE1; /* Page1 valid */ |
mega64 | 0:1756c3542c95 | 546 | } |
mega64 | 0:1756c3542c95 | 547 | else |
mega64 | 0:1756c3542c95 | 548 | { |
mega64 | 0:1756c3542c95 | 549 | return PAGE0; /* Page0 valid */ |
mega64 | 0:1756c3542c95 | 550 | } |
mega64 | 0:1756c3542c95 | 551 | } |
mega64 | 0:1756c3542c95 | 552 | else |
mega64 | 0:1756c3542c95 | 553 | { |
mega64 | 0:1756c3542c95 | 554 | return NO_VALID_PAGE; /* No valid Page */ |
mega64 | 0:1756c3542c95 | 555 | } |
mega64 | 0:1756c3542c95 | 556 | |
mega64 | 0:1756c3542c95 | 557 | case READ_FROM_VALID_PAGE: /* ---- Read operation ---- */ |
mega64 | 0:1756c3542c95 | 558 | if (PageStatus0 == VALID_PAGE) |
mega64 | 0:1756c3542c95 | 559 | { |
mega64 | 0:1756c3542c95 | 560 | return PAGE0; /* Page0 valid */ |
mega64 | 0:1756c3542c95 | 561 | } |
mega64 | 0:1756c3542c95 | 562 | else if (PageStatus1 == VALID_PAGE) |
mega64 | 0:1756c3542c95 | 563 | { |
mega64 | 0:1756c3542c95 | 564 | return PAGE1; /* Page1 valid */ |
mega64 | 0:1756c3542c95 | 565 | } |
mega64 | 0:1756c3542c95 | 566 | else |
mega64 | 0:1756c3542c95 | 567 | { |
mega64 | 0:1756c3542c95 | 568 | return NO_VALID_PAGE ; /* No valid Page */ |
mega64 | 0:1756c3542c95 | 569 | } |
mega64 | 0:1756c3542c95 | 570 | |
mega64 | 0:1756c3542c95 | 571 | default: |
mega64 | 0:1756c3542c95 | 572 | return PAGE0; /* Page0 valid */ |
mega64 | 0:1756c3542c95 | 573 | } |
mega64 | 0:1756c3542c95 | 574 | } |
mega64 | 0:1756c3542c95 | 575 | |
mega64 | 0:1756c3542c95 | 576 | /** |
mega64 | 0:1756c3542c95 | 577 | * @brief Verify if active page is full and Writes variable in EEPROM. |
mega64 | 0:1756c3542c95 | 578 | * @param VirtAddress: 16 bit virtual address of the variable |
mega64 | 0:1756c3542c95 | 579 | * @param Data: 16 bit data to be written as variable value |
mega64 | 0:1756c3542c95 | 580 | * @retval Success or error status: |
mega64 | 0:1756c3542c95 | 581 | * - FLASH_COMPLETE: on success |
mega64 | 0:1756c3542c95 | 582 | * - PAGE_FULL: if valid page is full |
mega64 | 0:1756c3542c95 | 583 | * - NO_VALID_PAGE: if no valid page was found |
mega64 | 0:1756c3542c95 | 584 | * - Flash error code: on write Flash error |
mega64 | 0:1756c3542c95 | 585 | */ |
mega64 | 0:1756c3542c95 | 586 | static uint16_t EE_VerifyPageFullWriteVariable(uint16_t VirtAddress, uint16_t Data) |
mega64 | 0:1756c3542c95 | 587 | { |
mega64 | 0:1756c3542c95 | 588 | HAL_StatusTypeDef FlashStatus = HAL_OK; |
mega64 | 0:1756c3542c95 | 589 | uint16_t ValidPage = PAGE0; |
mega64 | 0:1756c3542c95 | 590 | uint32_t Address = EEPROM_START_ADDRESS, PageEndAddress = EEPROM_START_ADDRESS+PAGE_SIZE; |
mega64 | 0:1756c3542c95 | 591 | |
mega64 | 0:1756c3542c95 | 592 | /* Get valid Page for write operation */ |
mega64 | 0:1756c3542c95 | 593 | ValidPage = EE_FindValidPage(WRITE_IN_VALID_PAGE); |
mega64 | 0:1756c3542c95 | 594 | |
mega64 | 0:1756c3542c95 | 595 | /* Check if there is no valid page */ |
mega64 | 0:1756c3542c95 | 596 | if (ValidPage == NO_VALID_PAGE) |
mega64 | 0:1756c3542c95 | 597 | { |
mega64 | 0:1756c3542c95 | 598 | return NO_VALID_PAGE; |
mega64 | 0:1756c3542c95 | 599 | } |
mega64 | 0:1756c3542c95 | 600 | |
mega64 | 0:1756c3542c95 | 601 | /* Get the valid Page start Address */ |
mega64 | 0:1756c3542c95 | 602 | Address = (uint32_t)(EEPROM_START_ADDRESS + (uint32_t)(ValidPage * PAGE_SIZE)); |
mega64 | 0:1756c3542c95 | 603 | |
mega64 | 0:1756c3542c95 | 604 | /* Get the valid Page end Address */ |
mega64 | 0:1756c3542c95 | 605 | PageEndAddress = (uint32_t)((EEPROM_START_ADDRESS - 1) + (uint32_t)((ValidPage + 1) * PAGE_SIZE)); |
mega64 | 0:1756c3542c95 | 606 | |
mega64 | 0:1756c3542c95 | 607 | /* Check each active page address starting from begining */ |
mega64 | 0:1756c3542c95 | 608 | while (Address < PageEndAddress) |
mega64 | 0:1756c3542c95 | 609 | { |
mega64 | 0:1756c3542c95 | 610 | /* Verify if Address and Address+2 contents are 0xFFFFFFFF */ |
mega64 | 0:1756c3542c95 | 611 | if ((*(__IO uint32_t*)Address) == 0xFFFFFFFF) |
mega64 | 0:1756c3542c95 | 612 | { |
mega64 | 0:1756c3542c95 | 613 | /* Set variable data */ |
mega64 | 0:1756c3542c95 | 614 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address, Data); |
mega64 | 0:1756c3542c95 | 615 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 616 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 617 | { |
mega64 | 0:1756c3542c95 | 618 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 619 | } |
mega64 | 0:1756c3542c95 | 620 | /* Set variable virtual address */ |
mega64 | 0:1756c3542c95 | 621 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, Address + 2, VirtAddress); |
mega64 | 0:1756c3542c95 | 622 | /* Return program operation status */ |
mega64 | 0:1756c3542c95 | 623 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 624 | } |
mega64 | 0:1756c3542c95 | 625 | else |
mega64 | 0:1756c3542c95 | 626 | { |
mega64 | 0:1756c3542c95 | 627 | /* Next address location */ |
mega64 | 0:1756c3542c95 | 628 | Address = Address + 4; |
mega64 | 0:1756c3542c95 | 629 | } |
mega64 | 0:1756c3542c95 | 630 | } |
mega64 | 0:1756c3542c95 | 631 | |
mega64 | 0:1756c3542c95 | 632 | /* Return PAGE_FULL in case the valid page is full */ |
mega64 | 0:1756c3542c95 | 633 | return PAGE_FULL; |
mega64 | 0:1756c3542c95 | 634 | } |
mega64 | 0:1756c3542c95 | 635 | |
mega64 | 0:1756c3542c95 | 636 | /** |
mega64 | 0:1756c3542c95 | 637 | * @brief Transfers last updated variables data from the full Page to |
mega64 | 0:1756c3542c95 | 638 | * an empty one. |
mega64 | 0:1756c3542c95 | 639 | * @param VirtAddress: 16 bit virtual address of the variable |
mega64 | 0:1756c3542c95 | 640 | * @param Data: 16 bit data to be written as variable value |
mega64 | 0:1756c3542c95 | 641 | * @retval Success or error status: |
mega64 | 0:1756c3542c95 | 642 | * - FLASH_COMPLETE: on success |
mega64 | 0:1756c3542c95 | 643 | * - PAGE_FULL: if valid page is full |
mega64 | 0:1756c3542c95 | 644 | * - NO_VALID_PAGE: if no valid page was found |
mega64 | 0:1756c3542c95 | 645 | * - Flash error code: on write Flash error |
mega64 | 0:1756c3542c95 | 646 | */ |
mega64 | 0:1756c3542c95 | 647 | static uint16_t EE_PageTransfer(uint16_t VirtAddress, uint16_t Data) |
mega64 | 0:1756c3542c95 | 648 | { |
mega64 | 0:1756c3542c95 | 649 | HAL_StatusTypeDef FlashStatus = HAL_OK; |
mega64 | 0:1756c3542c95 | 650 | uint32_t NewPageAddress = EEPROM_START_ADDRESS; |
mega64 | 0:1756c3542c95 | 651 | uint16_t OldPageId=0; |
mega64 | 0:1756c3542c95 | 652 | uint16_t ValidPage = PAGE0, VarIdx = 0; |
mega64 | 0:1756c3542c95 | 653 | uint16_t EepromStatus = 0, ReadStatus = 0; |
mega64 | 0:1756c3542c95 | 654 | uint32_t SectorError = 0; |
mega64 | 0:1756c3542c95 | 655 | FLASH_EraseInitTypeDef pEraseInit; |
mega64 | 0:1756c3542c95 | 656 | |
mega64 | 0:1756c3542c95 | 657 | /* Get active Page for read operation */ |
mega64 | 0:1756c3542c95 | 658 | ValidPage = EE_FindValidPage(READ_FROM_VALID_PAGE); |
mega64 | 0:1756c3542c95 | 659 | |
mega64 | 0:1756c3542c95 | 660 | if (ValidPage == PAGE1) /* Page1 valid */ |
mega64 | 0:1756c3542c95 | 661 | { |
mega64 | 0:1756c3542c95 | 662 | /* New page address where variable will be moved to */ |
mega64 | 0:1756c3542c95 | 663 | NewPageAddress = PAGE0_BASE_ADDRESS; |
mega64 | 0:1756c3542c95 | 664 | |
mega64 | 0:1756c3542c95 | 665 | /* Old page ID where variable will be taken from */ |
mega64 | 0:1756c3542c95 | 666 | OldPageId = PAGE1_ID; |
mega64 | 0:1756c3542c95 | 667 | } |
mega64 | 0:1756c3542c95 | 668 | else if (ValidPage == PAGE0) /* Page0 valid */ |
mega64 | 0:1756c3542c95 | 669 | { |
mega64 | 0:1756c3542c95 | 670 | /* New page address where variable will be moved to */ |
mega64 | 0:1756c3542c95 | 671 | NewPageAddress = PAGE1_BASE_ADDRESS; |
mega64 | 0:1756c3542c95 | 672 | |
mega64 | 0:1756c3542c95 | 673 | /* Old page ID where variable will be taken from */ |
mega64 | 0:1756c3542c95 | 674 | OldPageId = PAGE0_ID; |
mega64 | 0:1756c3542c95 | 675 | } |
mega64 | 0:1756c3542c95 | 676 | else |
mega64 | 0:1756c3542c95 | 677 | { |
mega64 | 0:1756c3542c95 | 678 | return NO_VALID_PAGE; /* No valid Page */ |
mega64 | 0:1756c3542c95 | 679 | } |
mega64 | 0:1756c3542c95 | 680 | |
mega64 | 0:1756c3542c95 | 681 | /* Set the new Page status to RECEIVE_DATA status */ |
mega64 | 0:1756c3542c95 | 682 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, RECEIVE_DATA); |
mega64 | 0:1756c3542c95 | 683 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 684 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 685 | { |
mega64 | 0:1756c3542c95 | 686 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 687 | } |
mega64 | 0:1756c3542c95 | 688 | |
mega64 | 0:1756c3542c95 | 689 | /* Write the variable passed as parameter in the new active page */ |
mega64 | 0:1756c3542c95 | 690 | EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddress, Data); |
mega64 | 0:1756c3542c95 | 691 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 692 | if (EepromStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 693 | { |
mega64 | 0:1756c3542c95 | 694 | return EepromStatus; |
mega64 | 0:1756c3542c95 | 695 | } |
mega64 | 0:1756c3542c95 | 696 | |
mega64 | 0:1756c3542c95 | 697 | /* Transfer process: transfer variables from old to the new active page */ |
mega64 | 0:1756c3542c95 | 698 | for (VarIdx = 0; VarIdx < NB_OF_VAR; VarIdx++) |
mega64 | 0:1756c3542c95 | 699 | { |
mega64 | 0:1756c3542c95 | 700 | if (VirtAddVarTab[VarIdx] != VirtAddress) /* Check each variable except the one passed as parameter */ |
mega64 | 0:1756c3542c95 | 701 | { |
mega64 | 0:1756c3542c95 | 702 | /* Read the other last variable updates */ |
mega64 | 0:1756c3542c95 | 703 | ReadStatus = EE_ReadVariable(VirtAddVarTab[VarIdx], &DataVar); |
mega64 | 0:1756c3542c95 | 704 | /* In case variable corresponding to the virtual address was found */ |
mega64 | 0:1756c3542c95 | 705 | if (ReadStatus != 0x1) |
mega64 | 0:1756c3542c95 | 706 | { |
mega64 | 0:1756c3542c95 | 707 | /* Transfer the variable to the new active page */ |
mega64 | 0:1756c3542c95 | 708 | EepromStatus = EE_VerifyPageFullWriteVariable(VirtAddVarTab[VarIdx], DataVar); |
mega64 | 0:1756c3542c95 | 709 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 710 | if (EepromStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 711 | { |
mega64 | 0:1756c3542c95 | 712 | return EepromStatus; |
mega64 | 0:1756c3542c95 | 713 | } |
mega64 | 0:1756c3542c95 | 714 | } |
mega64 | 0:1756c3542c95 | 715 | } |
mega64 | 0:1756c3542c95 | 716 | } |
mega64 | 0:1756c3542c95 | 717 | |
mega64 | 0:1756c3542c95 | 718 | pEraseInit.TypeErase = TYPEERASE_SECTORS; |
mega64 | 0:1756c3542c95 | 719 | pEraseInit.Sector = OldPageId; |
mega64 | 0:1756c3542c95 | 720 | pEraseInit.NbSectors = 1; |
mega64 | 0:1756c3542c95 | 721 | pEraseInit.VoltageRange = VOLTAGE_RANGE; |
mega64 | 0:1756c3542c95 | 722 | |
mega64 | 0:1756c3542c95 | 723 | /* Erase the old Page: Set old Page status to ERASED status */ |
mega64 | 0:1756c3542c95 | 724 | FlashStatus = HAL_FLASHEx_Erase(&pEraseInit, &SectorError); |
mega64 | 0:1756c3542c95 | 725 | /* If erase operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 726 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 727 | { |
mega64 | 0:1756c3542c95 | 728 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 729 | } |
mega64 | 0:1756c3542c95 | 730 | |
mega64 | 0:1756c3542c95 | 731 | /* Set new Page status to VALID_PAGE status */ |
mega64 | 0:1756c3542c95 | 732 | FlashStatus = HAL_FLASH_Program(TYPEPROGRAM_HALFWORD, NewPageAddress, VALID_PAGE); |
mega64 | 0:1756c3542c95 | 733 | /* If program operation was failed, a Flash error code is returned */ |
mega64 | 0:1756c3542c95 | 734 | if (FlashStatus != HAL_OK) |
mega64 | 0:1756c3542c95 | 735 | { |
mega64 | 0:1756c3542c95 | 736 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 737 | } |
mega64 | 0:1756c3542c95 | 738 | |
mega64 | 0:1756c3542c95 | 739 | /* Return last operation flash status */ |
mega64 | 0:1756c3542c95 | 740 | return FlashStatus; |
mega64 | 0:1756c3542c95 | 741 | } |
mega64 | 0:1756c3542c95 | 742 | |
mega64 | 0:1756c3542c95 | 743 | /** |
mega64 | 0:1756c3542c95 | 744 | * @} |
mega64 | 0:1756c3542c95 | 745 | */ |
mega64 | 0:1756c3542c95 | 746 | |
mega64 | 0:1756c3542c95 | 747 | /******************* (C) COPYRIGHT 2011 STMicroelectronics *****END OF FILE****/ |