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,87 +88,69 @@ 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- */
139- static uint8_t tmpEE [E2END ] = {0 };
97+ static uint8_t eeprom_buffer [E2END ] = {0 };
14098
14199/**
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
147103 */
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+ }
150108
151109/**
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
153114 */
115+ void eeprom_write_byte (uint16_t pos , uint8_t value ) {
116+ eeprom_buffered_write_byte (pos , value );
117+ eeprom_buffer_flush ();
118+ }
154119
155120/**
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
158123 * @retval byte : data read from eeprom
159124 */
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 ];
168127}
169128
170129/**
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
174133 * @retval none
175134 */
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 ;
180137}
181138
182139/**
183- * @brief The function read into the flash.
140+ * @brief This function copies the data from flash into the buffer
184141 * @param none
185142 * @retval none
186143 */
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 );
190146}
191147
192148/**
193- * @brief The function write into the flash.
149+ * @brief This function writes the buffer content into the flash
194150 * @param none
195151 * @retval none
196152 */
197- void set_data_to_flash (void )
198- {
199- //copy in flash
153+ void eeprom_buffer_flush (void ) {
200154 FLASH_EraseInitTypeDef EraseInitStruct ;
201155 uint32_t offset = 0 ;
202156 uint32_t address = FLASH_BASE_ADDRESS ;
@@ -206,12 +160,12 @@ void set_data_to_flash(void)
206160 uint32_t pageError = 0 ;
207161 uint64_t data = 0 ;
208162
209- // ERASING page
163+ /* ERASING page */
210164 EraseInitStruct .TypeErase = FLASH_TYPEERASE_PAGES ;
211165#ifdef STM32L4xx
212166 EraseInitStruct .Banks = FLASH_BANK_NUMBER ;
213167 EraseInitStruct .Page = FLASH_PAGE_NUMBER ;
214- #else // STM32F4xx
168+ #else
215169#ifdef STM32F1xx
216170 EraseInitStruct .Banks = FLASH_BANK_1 ;
217171#endif
@@ -240,12 +194,12 @@ void set_data_to_flash(void)
240194 if (HAL_FLASHEx_Erase (& EraseInitStruct , & pageError ) == HAL_OK ) {
241195 while (address < address_end ) {
242196#if defined(STM32L0xx ) || defined(STM32L1xx )
243- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
197+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
244198 if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
245199 address += 4 ;
246200 offset += 4 ;
247201#else
248- data = * ((uint64_t * )(((uint8_t * )tmpEE + offset )));
202+ data = * ((uint64_t * )(((uint8_t * )eeprom_buffer + offset )));
249203
250204 if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_DOUBLEWORD , address , data ) == HAL_OK ) {
251205 address += 8 ;
@@ -262,18 +216,17 @@ void set_data_to_flash(void)
262216 uint32_t SectorError = 0 ;
263217 uint32_t data = 0 ;
264218
265- // ERASING page
219+ /* ERASING page */
266220 EraseInitStruct .TypeErase = FLASH_TYPEERASE_SECTORS ;
267221 EraseInitStruct .VoltageRange = FLASH_VOLTAGE_RANGE_3 ;
268222 EraseInitStruct .Sector = FLASH_DATA_SECTOR ;
269223 EraseInitStruct .NbSectors = 1 ;
270224
271225 HAL_FLASH_Unlock ();
272226
273- if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK )
274- {
227+ if (HAL_FLASHEx_Erase (& EraseInitStruct , & SectorError ) == HAL_OK ) {
275228 while (address < address_end ) {
276- memcpy (& data , tmpEE + offset , sizeof (uint32_t ));
229+ memcpy (& data , eeprom_buffer + offset , sizeof (uint32_t ));
277230 if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_WORD , address , data ) == HAL_OK ) {
278231 address += 4 ;
279232 offset += 4 ;
@@ -286,18 +239,6 @@ void set_data_to_flash(void)
286239#endif
287240}
288241
289-
290- /**
291- * @}
292- */
293-
294- /**
295- * @}
296- */
297-
298- /**
299- * @}
300- */
301242#ifdef __cplusplus
302243}
303244#endif
0 commit comments