1
1
/**
2
2
******************************************************************************
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
9
5
******************************************************************************
10
6
* @attention
11
7
*
35
31
*
36
32
******************************************************************************
37
33
*/
38
- /** @addtogroup CMSIS
39
- * @{
40
- */
41
34
42
- /** @addtogroup stm32f4xx_system
43
- * @{
44
- */
45
-
46
- /** @addtogroup STM32F4xx_System_Private_Includes
47
- * @{
48
- */
49
35
#include "stm32_eeprom.h"
50
36
#include <string.h>
51
37
52
38
#ifdef __cplusplus
53
39
extern "C" {
54
40
#endif
55
41
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 */
72
44
#if defined (STM32F0xx ) || defined (STM32F1xx ) || defined(STM32L1xx )
73
45
#if defined (FLASH_BANK2_END )
74
46
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK2_END + 1) - FLASH_PAGE_SIZE))
75
47
#elif defined (FLASH_BANK1_END )
76
48
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK1_END + 1) - FLASH_PAGE_SIZE))
77
49
#else
78
50
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_END + 1) - FLASH_PAGE_SIZE))
79
- #endif // FLASH_BANK2_END
51
+ #endif /* FLASH_BANK2_END */
80
52
#elif defined (STM32F2xx ) || defined (STM32F4xx ) || defined (STM32F7xx )
81
53
#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_END + 1) - FLASH_PAGE_SIZE)
82
54
#define FLASH_DATA_SECTOR ((uint32_t)(FLASH_SECTOR_TOTAL - 1))
@@ -116,87 +88,69 @@ static inline uint32_t get_flash_end(void) {
116
88
#define FLASH_BANK_NUMBER FLASH_BANK_1
117
89
#else
118
90
#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 */
121
93
#define FLASH_PAGE_NUMBER ((uint32_t)((FLASH_SIZE / FLASH_PAGE_SIZE) - 1))
122
94
#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_BASE + (FLASH_PAGE_NUMBER * FLASH_PAGE_SIZE)))
123
95
#endif
124
- /**
125
- * @}
126
- */
127
-
128
- /** @addtogroup STM32F4xx_System_Private_Macros
129
- * @{
130
- */
131
-
132
- /**
133
- * @}
134
- */
135
96
136
- /** @addtogroup STM32F4xx_System_Private_Variables
137
- * @{
138
- */
139
- static uint8_t tmpEE [E2END ] = {0 };
97
+ static uint8_t eeprom_buffer [E2END ] = {0 };
140
98
141
99
/**
142
- * @}
143
- */
144
-
145
- /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146
- * @{
100
+ * @brief Function reads a byte from emulated eeprom (flash)
101
+ * @param pos : address to read
102
+ * @retval byte : data read from eeprom
147
103
*/
148
- void get_data_from_flash (void );
149
- void set_data_to_flash (void );
104
+ uint8_t eeprom_read_byte (const uint16_t pos ) {
105
+ eeprom_buffer_fill ();
106
+ return eeprom_buffered_read_byte (pos );
107
+ }
150
108
151
109
/**
152
- * @}
110
+ * @brief Function writes a byte to emulated eeprom (flash)
111
+ * @param pos : address to write
112
+ * @param value : value to write
113
+ * @retval none
153
114
*/
115
+ void eeprom_write_byte (uint16_t pos , uint8_t value ) {
116
+ eeprom_buffered_write_byte (pos , value );
117
+ eeprom_buffer_flush ();
118
+ }
154
119
155
120
/**
156
- * @brief Function read a byte from eeprom
157
- * @param __p : address to read
121
+ * @brief Function reads a byte from the eeprom buffer
122
+ * @param pos : address to read
158
123
* @retval byte : data read from eeprom
159
124
*/
160
- uint8_t eeprom_read_byte (const uint16_t __p )
161
- {
162
- uint8_t byte = 0 ;
163
-
164
- get_data_from_flash ();
165
- byte = tmpEE [__p ];
166
-
167
- return byte ;
125
+ uint8_t eeprom_buffered_read_byte (const uint16_t pos ) {
126
+ return eeprom_buffer [pos ];
168
127
}
169
128
170
129
/**
171
- * @brief Function write a byte to eeprom
172
- * @param __p : address to write
173
- * @param __value : value to write
130
+ * @brief Function writes a byte to the eeprom buffer
131
+ * @param pos : address to write
132
+ * @param value : value to write
174
133
* @retval none
175
134
*/
176
- void eeprom_write_byte (uint16_t __p , uint8_t __value )
177
- {
178
- tmpEE [__p ] = __value ;
179
- set_data_to_flash ();
135
+ void eeprom_buffered_write_byte (uint16_t pos , uint8_t value ) {
136
+ eeprom_buffer [pos ] = value ;
180
137
}
181
138
182
139
/**
183
- * @brief The function read into the flash.
140
+ * @brief This function copies the data from flash into the buffer
184
141
* @param none
185
142
* @retval none
186
143
*/
187
- void get_data_from_flash (void )
188
- {
189
- memcpy (tmpEE , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
144
+ void eeprom_buffer_fill (void ) {
145
+ memcpy (eeprom_buffer , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
190
146
}
191
147
192
148
/**
193
- * @brief The function write into the flash.
149
+ * @brief This function writes the buffer content into the flash
194
150
* @param none
195
151
* @retval none
196
152
*/
197
- void set_data_to_flash (void )
198
- {
199
- //copy in flash
153
+ void eeprom_buffer_flush (void ) {
200
154
FLASH_EraseInitTypeDef EraseInitStruct ;
201
155
uint32_t offset = 0 ;
202
156
uint32_t address = FLASH_BASE_ADDRESS ;
@@ -206,12 +160,12 @@ void set_data_to_flash(void)
206
160
uint32_t pageError = 0 ;
207
161
uint64_t data = 0 ;
208
162
209
- // ERASING page
163
+ /* ERASING page */
210
164
EraseInitStruct .TypeErase = FLASH_TYPEERASE_PAGES ;
211
165
#ifdef STM32L4xx
212
166
EraseInitStruct .Banks = FLASH_BANK_NUMBER ;
213
167
EraseInitStruct .Page = FLASH_PAGE_NUMBER ;
214
- #else // STM32F4xx
168
+ #else
215
169
#ifdef STM32F1xx
216
170
EraseInitStruct .Banks = FLASH_BANK_1 ;
217
171
#endif
@@ -240,12 +194,12 @@ void set_data_to_flash(void)
240
194
if (HAL_FLASHEx_Erase (& EraseInitStruct , & pageError ) == HAL_OK ) {
241
195
while (address < address_end ) {
242
196
#if defined(STM32L0xx ) || defined(STM32L1xx )
243
- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
197
+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
244
198
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
245
199
address += 4 ;
246
200
offset += 4 ;
247
201
#else
248
- data = * ((uint64_t * )(((uint8_t * )tmpEE + offset )));
202
+ data = * ((uint64_t * )(((uint8_t * )eeprom_buffer + offset )));
249
203
250
204
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_DOUBLEWORD , address , data ) == HAL_OK ) {
251
205
address += 8 ;
@@ -262,18 +216,17 @@ void set_data_to_flash(void)
262
216
uint32_t SectorError = 0 ;
263
217
uint32_t data = 0 ;
264
218
265
- // ERASING page
219
+ /* ERASING page */
266
220
EraseInitStruct .TypeErase = FLASH_TYPEERASE_SECTORS ;
267
221
EraseInitStruct .VoltageRange = FLASH_VOLTAGE_RANGE_3 ;
268
222
EraseInitStruct .Sector = FLASH_DATA_SECTOR ;
269
223
EraseInitStruct .NbSectors = 1 ;
270
224
271
225
HAL_FLASH_Unlock ();
272
226
273
- if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK )
274
- {
227
+ if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK ) {
275
228
while (address < address_end ) {
276
- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
229
+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
277
230
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
278
231
address += 4 ;
279
232
offset += 4 ;
@@ -286,18 +239,6 @@ void set_data_to_flash(void)
286
239
#endif
287
240
}
288
241
289
-
290
- /**
291
- * @}
292
- */
293
-
294
- /**
295
- * @}
296
- */
297
-
298
- /**
299
- * @}
300
- */
301
242
#ifdef __cplusplus
302
243
}
303
244
#endif
0 commit comments