@@ -136,7 +136,7 @@ static inline uint32_t get_flash_end(void) {
136
136
/** @addtogroup STM32F4xx_System_Private_Variables
137
137
* @{
138
138
*/
139
- static uint8_t tmpEE [E2END ] = {0 };
139
+ static uint8_t eeprom_buffer [E2END ] = {0 };
140
140
141
141
/**
142
142
* @}
@@ -145,58 +145,72 @@ static uint8_t tmpEE[E2END] = {0};
145
145
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146
146
* @{
147
147
*/
148
- void get_data_from_flash (void );
149
- void set_data_to_flash (void );
150
148
151
149
/**
152
150
* @}
153
151
*/
154
152
155
153
/**
156
- * @brief Function read a byte from eeprom
157
- * @param __p : address to read
154
+ * @brief Function reads a byte from emulated eeprom (flash)
155
+ * @param pos : address to read
158
156
* @retval byte : data read from eeprom
159
157
*/
160
- uint8_t eeprom_read_byte (const uint16_t __p )
158
+ uint8_t eeprom_read_byte (const uint16_t pos )
161
159
{
162
- uint8_t byte = 0 ;
160
+ eeprom_buffer_fill ();
161
+ return eeprom_buffered_read_byte (pos );
162
+ }
163
163
164
- get_data_from_flash ();
165
- byte = tmpEE [__p ];
164
+ /**
165
+ * @brief Function writes a byte to emulated eeprom (flash)
166
+ * @param pos : address to write
167
+ * @param value : value to write
168
+ * @retval none
169
+ */
170
+ void eeprom_write_byte (uint16_t pos , uint8_t value )
171
+ {
172
+ eeprom_buffered_write_byte (pos , value );
173
+ eeprom_buffer_flush ();
174
+ }
166
175
167
- return byte ;
176
+ /**
177
+ * @brief Function reads a byte from the eeprom buffer
178
+ * @param pos : address to read
179
+ * @retval byte : data read from eeprom
180
+ */
181
+ uint8_t eeprom_buffered_read_byte (const uint16_t pos )
182
+ {
183
+ return eeprom_buffer [pos ];
168
184
}
169
185
170
186
/**
171
- * @brief Function write a byte to eeprom
172
- * @param __p : address to write
173
- * @param __value : value to write
187
+ * @brief Function writes a byte to the eeprom buffer
188
+ * @param pos : address to write
189
+ * @param value : value to write
174
190
* @retval none
175
191
*/
176
- void eeprom_write_byte (uint16_t __p , uint8_t __value )
192
+ void eeprom_buffered_write_byte (uint16_t pos , uint8_t value )
177
193
{
178
- tmpEE [__p ] = __value ;
179
- set_data_to_flash ();
194
+ eeprom_buffer [pos ] = value ;
180
195
}
181
196
182
197
/**
183
- * @brief The function read into the flash.
198
+ * @brief This function copies the data from flash into the buffer
184
199
* @param none
185
200
* @retval none
186
201
*/
187
- void get_data_from_flash (void )
202
+ void eeprom_buffer_fill (void )
188
203
{
189
- memcpy (tmpEE , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
204
+ memcpy (eeprom_buffer , (uint8_t * )(FLASH_BASE_ADDRESS ), E2END );
190
205
}
191
206
192
207
/**
193
- * @brief The function write into the flash.
208
+ * @brief This function writes the buffer content into the flash
194
209
* @param none
195
210
* @retval none
196
211
*/
197
- void set_data_to_flash (void )
212
+ void eeprom_buffer_flush (void )
198
213
{
199
- //copy in flash
200
214
FLASH_EraseInitTypeDef EraseInitStruct ;
201
215
uint32_t offset = 0 ;
202
216
uint32_t address = FLASH_BASE_ADDRESS ;
@@ -240,12 +254,12 @@ void set_data_to_flash(void)
240
254
if (HAL_FLASHEx_Erase (& EraseInitStruct , & pageError ) == HAL_OK ) {
241
255
while (address < address_end ) {
242
256
#if defined(STM32L0xx ) || defined(STM32L1xx )
243
- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
257
+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
244
258
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
245
259
address += 4 ;
246
260
offset += 4 ;
247
261
#else
248
- data = * ((uint64_t * )(((uint8_t * )tmpEE + offset )));
262
+ data = * ((uint64_t * )(((uint8_t * )eeprom_buffer + offset )));
249
263
250
264
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_DOUBLEWORD , address , data ) == HAL_OK ) {
251
265
address += 8 ;
@@ -273,7 +287,7 @@ void set_data_to_flash(void)
273
287
if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK )
274
288
{
275
289
while (address < address_end ) {
276
- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
290
+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
277
291
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
278
292
address += 4 ;
279
293
offset += 4 ;
0 commit comments