11/**
22 ******************************************************************************
3- * @file eeprom.c
4- * @author WI6LABS
5- * @version V1.0.0
6- * @date 01-August-2016
7- * @brief provide emulated eeprom from flash
8- *
3+ * @file stm32_eeprom.c
4+ * @brief Provides emulated eeprom from flash
95 ******************************************************************************
106 * @attention
117 *
3531 *
3632 ******************************************************************************
3733 */
38- /** @addtogroup CMSIS
39- * @{
40- */
4134
42- /** @addtogroup stm32f4xx_system
43- * @{
44- */
45-
46- /** @addtogroup STM32F4xx_System_Private_Includes
47- * @{
48- */
4935#include "stm32_eeprom.h"
5036#include <string.h>
5137
5238#ifdef __cplusplus
5339 extern "C" {
5440#endif
5541
56- /**
57- * @}
58- */
59-
60- /** @addtogroup STM32F4xx_System_Private_TypesDefinitions
61- * @{
62- */
63-
64- /**
65- * @}
66- */
67-
68- /** @addtogroup STM32F4xx_System_Private_Defines
69- * @{
70- */
71- // We use the last page of the flash to store data (to prevent code overwritten).
42+ /* Use the last page of the flash to store data in order to prevent overwritting
43+ program data */
7244#if defined (STM32F0xx ) || defined (STM32F1xx ) || defined(STM32L1xx )
7345#if defined (FLASH_BANK2_END )
7446#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK2_END + 1) - FLASH_PAGE_SIZE))
7547#elif defined (FLASH_BANK1_END )
7648#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK1_END + 1) - FLASH_PAGE_SIZE))
7749#else
7850#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_END + 1) - FLASH_PAGE_SIZE))
79- #endif // FLASH_BANK2_END
51+ #endif /* FLASH_BANK2_END */
8052#elif defined (STM32F2xx ) || defined (STM32F4xx ) || defined (STM32F7xx )
8153#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_END + 1) - FLASH_PAGE_SIZE)
8254#define FLASH_DATA_SECTOR ((uint32_t)(FLASH_SECTOR_TOTAL - 1))
@@ -116,47 +88,20 @@ static inline uint32_t get_flash_end(void) {
11688#define FLASH_BANK_NUMBER FLASH_BANK_1
11789#else
11890#define FLASH_BANK_NUMBER FLASH_BANK_2
119- #endif // FLASH_BANK_2
120- // Flash base address
91+ #endif /* FLASH_BANK_2 */
92+ /* Flash base address */
12193#define FLASH_PAGE_NUMBER ((uint32_t)((FLASH_SIZE / FLASH_PAGE_SIZE) - 1))
12294#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_BASE + (FLASH_PAGE_NUMBER * FLASH_PAGE_SIZE)))
12395#endif
124- /**
125- * @}
126- */
127-
128- /** @addtogroup STM32F4xx_System_Private_Macros
129- * @{
130- */
131-
132- /**
133- * @}
134- */
13596
136- /** @addtogroup STM32F4xx_System_Private_Variables
137- * @{
138- */
13997static uint8_t eeprom_buffer [E2END ] = {0 };
14098
141- /**
142- * @}
143- */
144-
145- /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146- * @{
147- */
148-
149- /**
150- * @}
151- */
152-
15399/**
154100 * @brief Function reads a byte from emulated eeprom (flash)
155101 * @param pos : address to read
156102 * @retval byte : data read from eeprom
157103 */
158- uint8_t eeprom_read_byte (const uint16_t pos )
159- {
104+ uint8_t eeprom_read_byte (const uint16_t pos ) {
160105 eeprom_buffer_fill ();
161106 return eeprom_buffered_read_byte (pos );
162107}
@@ -167,8 +112,7 @@ uint8_t eeprom_read_byte(const uint16_t pos)
167112 * @param value : value to write
168113 * @retval none
169114 */
170- void eeprom_write_byte (uint16_t pos , uint8_t value )
171- {
115+ void eeprom_write_byte (uint16_t pos , uint8_t value ) {
172116 eeprom_buffered_write_byte (pos , value );
173117 eeprom_buffer_flush ();
174118}
@@ -178,8 +122,7 @@ void eeprom_write_byte(uint16_t pos, uint8_t value)
178122 * @param pos : address to read
179123 * @retval byte : data read from eeprom
180124 */
181- uint8_t eeprom_buffered_read_byte (const uint16_t pos )
182- {
125+ uint8_t eeprom_buffered_read_byte (const uint16_t pos ) {
183126 return eeprom_buffer [pos ];
184127}
185128
@@ -189,8 +132,7 @@ uint8_t eeprom_buffered_read_byte(const uint16_t pos)
189132 * @param value : value to write
190133 * @retval none
191134 */
192- void eeprom_buffered_write_byte (uint16_t pos , uint8_t value )
193- {
135+ void eeprom_buffered_write_byte (uint16_t pos , uint8_t value ) {
194136 eeprom_buffer [pos ] = value ;
195137}
196138
@@ -199,8 +141,7 @@ void eeprom_buffered_write_byte(uint16_t pos, uint8_t value)
199141 * @param none
200142 * @retval none
201143 */
202- void eeprom_buffer_fill (void )
203- {
144+ void eeprom_buffer_fill (void ) {
204145 memcpy (eeprom_buffer , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
205146}
206147
@@ -209,8 +150,7 @@ void eeprom_buffer_fill(void)
209150 * @param none
210151 * @retval none
211152 */
212- void eeprom_buffer_flush (void )
213- {
153+ void eeprom_buffer_flush (void ) {
214154 FLASH_EraseInitTypeDef EraseInitStruct ;
215155 uint32_t offset = 0 ;
216156 uint32_t address = FLASH_BASE_ADDRESS ;
@@ -220,12 +160,12 @@ void eeprom_buffer_flush(void)
220160 uint32_t pageError = 0 ;
221161 uint64_t data = 0 ;
222162
223- // ERASING page
163+ /* ERASING page */
224164 EraseInitStruct .TypeErase = FLASH_TYPEERASE_PAGES ;
225165#ifdef STM32L4xx
226166 EraseInitStruct .Banks = FLASH_BANK_NUMBER ;
227167 EraseInitStruct .Page = FLASH_PAGE_NUMBER ;
228- #else // STM32F4xx
168+ #else
229169#ifdef STM32F1xx
230170 EraseInitStruct .Banks = FLASH_BANK_1 ;
231171#endif
@@ -276,16 +216,15 @@ void eeprom_buffer_flush(void)
276216 uint32_t SectorError = 0 ;
277217 uint32_t data = 0 ;
278218
279- // ERASING page
219+ /* ERASING page */
280220 EraseInitStruct .TypeErase = FLASH_TYPEERASE_SECTORS ;
281221 EraseInitStruct .VoltageRange = FLASH_VOLTAGE_RANGE_3 ;
282222 EraseInitStruct .Sector = FLASH_DATA_SECTOR ;
283223 EraseInitStruct .NbSectors = 1 ;
284224
285225 HAL_FLASH_Unlock ();
286226
287- if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK )
288- {
227+ if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK ) {
289228 while (address < address_end ) {
290229 memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
291230 if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
@@ -300,18 +239,6 @@ void eeprom_buffer_flush(void)
300239#endif
301240}
302241
303-
304- /**
305- * @}
306- */
307-
308- /**
309- * @}
310- */
311-
312- /**
313- * @}
314- */
315242#ifdef __cplusplus
316243}
317244#endif
0 commit comments