Skip to content

Commit 7f6e43e

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: stm32duino#296 Signed-off-by: Nils Hasenbanck <[email protected]>
1 parent bba41b0 commit 7f6e43e

File tree

2 files changed

+38
-22
lines changed

2 files changed

+38
-22
lines changed

cores/arduino/stm32/stm32_eeprom.c

Lines changed: 33 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -94,23 +94,16 @@ static inline uint32_t get_flash_end(void) {
9494
#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_BASE + (FLASH_PAGE_NUMBER * FLASH_PAGE_SIZE)))
9595
#endif
9696

97-
static uint8_t tmpEE[E2END] = {0};
98-
99-
void get_data_from_flash(void);
100-
void set_data_to_flash(void);
97+
static uint8_t eeprom_buffer[E2END] = {0};
10198

10299
/**
103-
* @brief Function read a byte from eeprom
100+
* @brief Function reads a byte from emulated eeprom (flash)
104101
* @param pos : address to read
105102
* @retval byte : data read from eeprom
106103
*/
107104
uint8_t eeprom_read_byte(const uint16_t pos) {
108-
uint8_t byte = 0;
109-
110-
get_data_from_flash();
111-
byte = tmpEE[pos];
112-
113-
return byte;
105+
eeprom_buffer_fill();
106+
return eeprom_buffered_read_byte(pos);
114107
}
115108

116109
/**
@@ -120,26 +113,44 @@ uint8_t eeprom_read_byte(const uint16_t pos) {
120113
* @retval none
121114
*/
122115
void eeprom_write_byte(uint16_t pos, uint8_t value) {
123-
tmpEE[pos] = value;
124-
set_data_to_flash();
116+
eeprom_buffered_write_byte(pos, value);
117+
eeprom_buffer_flush();
125118
}
126119

127120
/**
128-
* @brief Function reads a byte from emulated eeprom (flash)
121+
* @brief Function reads a byte from the eeprom buffer
122+
* @param pos : address to read
123+
* @retval byte : data read from eeprom
124+
*/
125+
uint8_t eeprom_buffered_read_byte(const uint16_t pos) {
126+
return eeprom_buffer[pos];
127+
}
128+
129+
/**
130+
* @brief Function writes a byte to the eeprom buffer
131+
* @param pos : address to write
132+
* @param value : value to write
133+
* @retval none
134+
*/
135+
void eeprom_buffered_write_byte(uint16_t pos, uint8_t value) {
136+
eeprom_buffer[pos] = value;
137+
}
138+
139+
/**
140+
* @brief This function copies the data from flash into the buffer
129141
* @param none
130142
* @retval none
131143
*/
132-
void get_data_from_flash(void) {
133-
memcpy(tmpEE, (uint8_t*)(FLASH_BASE_ADDRESS), E2END);
144+
void eeprom_buffer_fill(void) {
145+
memcpy(eeprom_buffer, (uint8_t*)(FLASH_BASE_ADDRESS), E2END);
134146
}
135147

136148
/**
137-
* @brief The function write into the flash.
149+
* @brief This function writes the buffer content into the flash
138150
* @param none
139151
* @retval none
140152
*/
141-
void set_data_to_flash(void) {
142-
/* Copy in flash */
153+
void eeprom_buffer_flush(void) {
143154
FLASH_EraseInitTypeDef EraseInitStruct;
144155
uint32_t offset = 0;
145156
uint32_t address = FLASH_BASE_ADDRESS;
@@ -183,12 +194,12 @@ void set_data_to_flash(void) {
183194
if(HAL_FLASHEx_Erase(&EraseInitStruct, &pageError) == HAL_OK) {
184195
while(address < address_end) {
185196
#if defined(STM32L0xx) || defined(STM32L1xx)
186-
memcpy(&data, tmpEE + offset, sizeof(uint32_t));
197+
memcpy(&data, eeprom_buffer + offset, sizeof(uint32_t));
187198
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data) == HAL_OK) {
188199
address += 4;
189200
offset += 4;
190201
#else
191-
data = *((uint64_t*)(((uint8_t*)tmpEE + offset)));
202+
data = *((uint64_t*)(((uint8_t*)eeprom_buffer + offset)));
192203

193204
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data) == HAL_OK) {
194205
address += 8;
@@ -215,7 +226,7 @@ void set_data_to_flash(void) {
215226

216227
if(HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) == HAL_OK) {
217228
while(address < address_end) {
218-
memcpy(&data, tmpEE + offset, sizeof(uint32_t));
229+
memcpy(&data, eeprom_buffer + offset, sizeof(uint32_t));
219230
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, address, data) == HAL_OK) {
220231
address += 4;
221232
offset += 4;

cores/arduino/stm32/stm32_eeprom.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -58,6 +58,11 @@
5858
uint8_t eeprom_read_byte(const uint16_t pos);
5959
void eeprom_write_byte(uint16_t pos, uint8_t value);
6060

61+
void eeprom_buffer_fill();
62+
void eeprom_buffer_flush();
63+
uint8_t eeprom_buffered_read_byte(const uint16_t pos);
64+
void eeprom_buffered_write_byte(uint16_t pos, uint8_t value);
65+
6166
#ifdef __cplusplus
6267
}
6368
#endif

0 commit comments

Comments
 (0)