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,47 +88,20 @@ 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
97
static uint8_t eeprom_buffer [E2END ] = {0 };
140
98
141
- /**
142
- * @}
143
- */
144
-
145
- /** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146
- * @{
147
- */
148
-
149
- /**
150
- * @}
151
- */
152
-
153
99
/**
154
100
* @brief Function reads a byte from emulated eeprom (flash)
155
101
* @param pos : address to read
156
102
* @retval byte : data read from eeprom
157
103
*/
158
- uint8_t eeprom_read_byte (const uint16_t pos )
159
- {
104
+ uint8_t eeprom_read_byte (const uint16_t pos ) {
160
105
eeprom_buffer_fill ();
161
106
return eeprom_buffered_read_byte (pos );
162
107
}
@@ -167,8 +112,7 @@ uint8_t eeprom_read_byte(const uint16_t pos)
167
112
* @param value : value to write
168
113
* @retval none
169
114
*/
170
- void eeprom_write_byte (uint16_t pos , uint8_t value )
171
- {
115
+ void eeprom_write_byte (uint16_t pos , uint8_t value ) {
172
116
eeprom_buffered_write_byte (pos , value );
173
117
eeprom_buffer_flush ();
174
118
}
@@ -178,8 +122,7 @@ void eeprom_write_byte(uint16_t pos, uint8_t value)
178
122
* @param pos : address to read
179
123
* @retval byte : data read from eeprom
180
124
*/
181
- uint8_t eeprom_buffered_read_byte (const uint16_t pos )
182
- {
125
+ uint8_t eeprom_buffered_read_byte (const uint16_t pos ) {
183
126
return eeprom_buffer [pos ];
184
127
}
185
128
@@ -189,8 +132,7 @@ uint8_t eeprom_buffered_read_byte(const uint16_t pos)
189
132
* @param value : value to write
190
133
* @retval none
191
134
*/
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 ) {
194
136
eeprom_buffer [pos ] = value ;
195
137
}
196
138
@@ -199,8 +141,7 @@ void eeprom_buffered_write_byte(uint16_t pos, uint8_t value)
199
141
* @param none
200
142
* @retval none
201
143
*/
202
- void eeprom_buffer_fill (void )
203
- {
144
+ void eeprom_buffer_fill (void ) {
204
145
memcpy (eeprom_buffer , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
205
146
}
206
147
@@ -209,8 +150,7 @@ void eeprom_buffer_fill(void)
209
150
* @param none
210
151
* @retval none
211
152
*/
212
- void eeprom_buffer_flush (void )
213
- {
153
+ void eeprom_buffer_flush (void ) {
214
154
FLASH_EraseInitTypeDef EraseInitStruct ;
215
155
uint32_t offset = 0 ;
216
156
uint32_t address = FLASH_BASE_ADDRESS ;
@@ -220,12 +160,12 @@ void eeprom_buffer_flush(void)
220
160
uint32_t pageError = 0 ;
221
161
uint64_t data = 0 ;
222
162
223
- // ERASING page
163
+ /* ERASING page */
224
164
EraseInitStruct .TypeErase = FLASH_TYPEERASE_PAGES ;
225
165
#ifdef STM32L4xx
226
166
EraseInitStruct .Banks = FLASH_BANK_NUMBER ;
227
167
EraseInitStruct .Page = FLASH_PAGE_NUMBER ;
228
- #else // STM32F4xx
168
+ #else
229
169
#ifdef STM32F1xx
230
170
EraseInitStruct .Banks = FLASH_BANK_1 ;
231
171
#endif
@@ -276,16 +216,15 @@ void eeprom_buffer_flush(void)
276
216
uint32_t SectorError = 0 ;
277
217
uint32_t data = 0 ;
278
218
279
- // ERASING page
219
+ /* ERASING page */
280
220
EraseInitStruct .TypeErase = FLASH_TYPEERASE_SECTORS ;
281
221
EraseInitStruct .VoltageRange = FLASH_VOLTAGE_RANGE_3 ;
282
222
EraseInitStruct .Sector = FLASH_DATA_SECTOR ;
283
223
EraseInitStruct .NbSectors = 1 ;
284
224
285
225
HAL_FLASH_Unlock ();
286
226
287
- if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK )
288
- {
227
+ if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK ) {
289
228
while (address < address_end ) {
290
229
memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
291
230
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
@@ -300,18 +239,6 @@ void eeprom_buffer_flush(void)
300
239
#endif
301
240
}
302
241
303
-
304
- /**
305
- * @}
306
- */
307
-
308
- /**
309
- * @}
310
- */
311
-
312
- /**
313
- * @}
314
- */
315
242
#ifdef __cplusplus
316
243
}
317
244
#endif
0 commit comments