Skip to content

Commit e9dbcd3

Browse files
Nils Hasenbanckfpistm
Nils Hasenbanck
authored andcommitted
[EEPROM emulation] Rework for buffered access
The current EEPROM emulation didn't had any buffered access to the data on the flash. Every access to a byte would trigger a whole page read / write. For writes it would mean, that the whole page would be erased. For backward compability we will keep the current functions as they are, but will add functions to fill/flush the buffer and read from it. The buffer size is static and is one page in size. Fixes: #296 Signed-off-by: Nils Hasenbanck <nils.hasenbanck@trendig.com>
1 parent 27bf43b commit e9dbcd3

File tree

2 files changed

+46
-27
lines changed

2 files changed

+46
-27
lines changed

cores/arduino/stm32/stm32_eeprom.c

+39-25
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ static inline uint32_t get_flash_end(void) {
136136
/** @addtogroup STM32F4xx_System_Private_Variables
137137
* @{
138138
*/
139-
static uint8_t tmpEE[E2END] = {0};
139+
static uint8_t eeprom_buffer[E2END] = {0};
140140

141141
/**
142142
* @}
@@ -145,58 +145,72 @@ static uint8_t tmpEE[E2END] = {0};
145145
/** @addtogroup STM32F4xx_System_Private_FunctionPrototypes
146146
* @{
147147
*/
148-
void get_data_from_flash(void);
149-
void set_data_to_flash(void);
150148

151149
/**
152150
* @}
153151
*/
154152

155153
/**
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
158156
* @retval byte : data read from eeprom
159157
*/
160-
uint8_t eeprom_read_byte(const uint16_t __p)
158+
uint8_t eeprom_read_byte(const uint16_t pos)
161159
{
162-
uint8_t byte = 0;
160+
eeprom_buffer_fill();
161+
return eeprom_buffered_read_byte(pos);
162+
}
163163

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+
}
166175

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];
168184
}
169185

170186
/**
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
174190
* @retval none
175191
*/
176-
void eeprom_write_byte(uint16_t __p, uint8_t __value)
192+
void eeprom_buffered_write_byte(uint16_t pos, uint8_t value)
177193
{
178-
tmpEE[__p] = __value;
179-
set_data_to_flash();
194+
eeprom_buffer[pos] = value;
180195
}
181196

182197
/**
183-
* @brief The function read into the flash.
198+
* @brief This function copies the data from flash into the buffer
184199
* @param none
185200
* @retval none
186201
*/
187-
void get_data_from_flash(void)
202+
void eeprom_buffer_fill(void)
188203
{
189-
memcpy(tmpEE, (uint8_t*)(FLASH_BASE_ADDRESS), E2END);
204+
memcpy(eeprom_buffer, (uint8_t*)(FLASH_BASE_ADDRESS), E2END);
190205
}
191206

192207
/**
193-
* @brief The function write into the flash.
208+
* @brief This function writes the buffer content into the flash
194209
* @param none
195210
* @retval none
196211
*/
197-
void set_data_to_flash(void)
212+
void eeprom_buffer_flush(void)
198213
{
199-
//copy in flash
200214
FLASH_EraseInitTypeDef EraseInitStruct;
201215
uint32_t offset = 0;
202216
uint32_t address = FLASH_BASE_ADDRESS;
@@ -240,12 +254,12 @@ void set_data_to_flash(void)
240254
if(HAL_FLASHEx_Erase(&EraseInitStruct, &pageError) == HAL_OK) {
241255
while(address < address_end) {
242256
#if defined(STM32L0xx) || defined(STM32L1xx)
243-
memcpy(&data, tmpEE + offset, sizeof(uint32_t));
257+
memcpy(&data, eeprom_buffer + offset, sizeof(uint32_t));
244258
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data) == HAL_OK) {
245259
address += 4;
246260
offset += 4;
247261
#else
248-
data = *((uint64_t*)(((uint8_t*)tmpEE + offset)));
262+
data = *((uint64_t*)(((uint8_t*)eeprom_buffer + offset)));
249263

250264
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data) == HAL_OK) {
251265
address += 8;
@@ -273,7 +287,7 @@ void set_data_to_flash(void)
273287
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) == HAL_OK)
274288
{
275289
while(address < address_end) {
276-
memcpy(&data, tmpEE + offset, sizeof(uint32_t));
290+
memcpy(&data, eeprom_buffer + offset, sizeof(uint32_t));
277291
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data) == HAL_OK) {
278292
address += 4;
279293
offset += 4;

cores/arduino/stm32/stm32_eeprom.h

+7-2
Original file line numberDiff line numberDiff line change
@@ -58,8 +58,13 @@
5858
/* Exported macro ------------------------------------------------------------*/
5959
/* Exported functions ------------------------------------------------------- */
6060

61-
uint8_t eeprom_read_byte(const uint16_t __p);
62-
void eeprom_write_byte(uint16_t __p, uint8_t __value);
61+
uint8_t eeprom_read_byte(const uint16_t pos);
62+
void eeprom_write_byte(uint16_t pos, uint8_t value);
63+
64+
void eeprom_buffer_fill();
65+
void eeprom_buffer_flush();
66+
uint8_t eeprom_buffered_read_byte(const uint16_t pos);
67+
void eeprom_buffered_write_byte(uint16_t pos, uint8_t value);
6368

6469
#ifdef __cplusplus
6570
}

0 commit comments

Comments
 (0)