Skip to content

Commit 76d5bf9

Browse files
Nils Hasenbanckfpistm
authored andcommitted
[EEPROM emulation] Clean up code
Clean up also the old references to the original author/version/date, since we already refactored this code quite well. The copyright notice of course is still intact. Signed-off-by: Nils Hasenbanck <[email protected]>
1 parent ca36657 commit 76d5bf9

File tree

2 files changed

+27
-100
lines changed

2 files changed

+27
-100
lines changed

cores/arduino/stm32/stm32_eeprom.c

Lines changed: 23 additions & 93 deletions
Original file line numberDiff line numberDiff line change
@@ -1,11 +1,7 @@
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
*
@@ -35,48 +31,24 @@
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,76 +88,48 @@ 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-
*/
13997
static uint8_t tmpEE[E2END] = {0};
14098

141-
/**
142-
* @}
143-
*/
144-
145-
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146-
* @{
147-
*/
14899
void get_data_from_flash(void);
149100
void set_data_to_flash(void);
150101

151-
/**
152-
* @}
153-
*/
154-
155102
/**
156103
* @brief Function read a byte from eeprom
157-
* @param __p : address to read
104+
* @param pos : address to read
158105
* @retval byte : data read from eeprom
159106
*/
160-
uint8_t eeprom_read_byte(const uint16_t __p)
161-
{
107+
uint8_t eeprom_read_byte(const uint16_t pos) {
162108
uint8_t byte = 0;
163109

164110
get_data_from_flash();
165-
byte = tmpEE[__p];
111+
byte = tmpEE[pos];
166112

167113
return byte;
168114
}
169115

170116
/**
171-
* @brief Function write a byte to eeprom
172-
* @param __p : address to write
173-
* @param __value : value to write
117+
* @brief Function writes a byte to emulated eeprom (flash)
118+
* @param pos : address to write
119+
* @param value : value to write
174120
* @retval none
175121
*/
176-
void eeprom_write_byte(uint16_t __p, uint8_t __value)
177-
{
178-
tmpEE[__p] = __value;
122+
void eeprom_write_byte(uint16_t pos, uint8_t value) {
123+
tmpEE[pos] = value;
179124
set_data_to_flash();
180125
}
181126

182127
/**
183-
* @brief The function read into the flash.
128+
* @brief Function reads a byte from emulated eeprom (flash)
184129
* @param none
185130
* @retval none
186131
*/
187-
void get_data_from_flash(void)
188-
{
132+
void get_data_from_flash(void) {
189133
memcpy(tmpEE, (uint8_t*)(FLASH_BASE_ADDRESS), E2END);
190134
}
191135

@@ -194,9 +138,8 @@ void get_data_from_flash(void)
194138
* @param none
195139
* @retval none
196140
*/
197-
void set_data_to_flash(void)
198-
{
199-
//copy in flash
141+
void set_data_to_flash(void) {
142+
/* Copy in flash */
200143
FLASH_EraseInitTypeDef EraseInitStruct;
201144
uint32_t offset = 0;
202145
uint32_t address = FLASH_BASE_ADDRESS;
@@ -206,12 +149,12 @@ void set_data_to_flash(void)
206149
uint32_t pageError = 0;
207150
uint64_t data = 0;
208151

209-
// ERASING page
152+
/* ERASING page */
210153
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
211154
#ifdef STM32L4xx
212155
EraseInitStruct.Banks = FLASH_BANK_NUMBER;
213156
EraseInitStruct.Page = FLASH_PAGE_NUMBER;
214-
#else // STM32F4xx
157+
#else /* STM32F4xx */
215158
#ifdef STM32F1xx
216159
EraseInitStruct.Banks = FLASH_BANK_1;
217160
#endif
@@ -262,16 +205,15 @@ void set_data_to_flash(void)
262205
uint32_t SectorError = 0;
263206
uint32_t data = 0;
264207

265-
// ERASING page
208+
/* ERASING page */
266209
EraseInitStruct.TypeErase = FLASH_TYPEERASE_SECTORS;
267210
EraseInitStruct.VoltageRange = FLASH_VOLTAGE_RANGE_3;
268211
EraseInitStruct.Sector = FLASH_DATA_SECTOR;
269212
EraseInitStruct.NbSectors = 1;
270213

271214
HAL_FLASH_Unlock();
272215

273-
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) == HAL_OK)
274-
{
216+
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) == HAL_OK) {
275217
while(address < address_end) {
276218
memcpy(&data, tmpEE + offset, sizeof(uint32_t));
277219
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data) == HAL_OK) {
@@ -286,18 +228,6 @@ void set_data_to_flash(void)
286228
#endif
287229
}
288230

289-
290-
/**
291-
* @}
292-
*/
293-
294-
/**
295-
* @}
296-
*/
297-
298-
/**
299-
* @}
300-
*/
301231
#ifdef __cplusplus
302232
}
303233
#endif

cores/arduino/stm32/stm32_eeprom.h

Lines changed: 4 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,6 @@
11
/**
22
******************************************************************************
33
* @file stm32_eeprom.h
4-
* @author WI6LABS
5-
* @version V1.0.0
6-
* @date 01-August-2016
74
* @brief Header for eeprom module
85
******************************************************************************
96
* @attention
@@ -50,16 +47,16 @@
5047
/* Exported constants --------------------------------------------------------*/
5148

5249
#if defined (STM32F2xx) || defined (STM32F4xx) || defined (STM32F7xx)
53-
//FLASH_SECTOR_SIZE
54-
#define FLASH_PAGE_SIZE ((uint32_t)(16*1024)) //16kB page
50+
/* FLASH_SECTOR_SIZE */
51+
#define FLASH_PAGE_SIZE ((uint32_t)(16*1024)) /* 16kB page */
5552
#endif
5653
#define E2END FLASH_PAGE_SIZE
5754

5855
/* Exported macro ------------------------------------------------------------*/
5956
/* Exported functions ------------------------------------------------------- */
6057

61-
uint8_t eeprom_read_byte(const uint16_t __p);
62-
void eeprom_write_byte(uint16_t __p, uint8_t __value);
58+
uint8_t eeprom_read_byte(const uint16_t pos);
59+
void eeprom_write_byte(uint16_t pos, uint8_t value);
6360

6461
#ifdef __cplusplus
6562
}

0 commit comments

Comments
 (0)