Skip to content

Commit dea7f9f

Browse files
committed
stm32L0xx or stm32L1xx soc series has an embedded EEPROM
This EEPROM area is mapped in the non volatile memory and accessed with dedicated HAL functions In this case, the EEPROM area is defined from DATA_EEPROM_BASE to DATA_EEPROM_END Signed-off-by: Francois Ramu <francois.ramu@st.com>
1 parent 59dda5c commit dea7f9f

File tree

2 files changed

+62
-0
lines changed

2 files changed

+62
-0
lines changed

cores/arduino/stm32/stm32_eeprom.h

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -80,7 +80,25 @@ extern "C" {
8080
*/
8181
#define FLASH_PAGE_SIZE ((uint32_t)(16*1024)) /* 16kB page */
8282
#endif
83+
84+
#if defined(DATA_EEPROM_BASE) || defined(FLASH_EEPROM_BASE)
85+
86+
#if defined (DATA_EEPROM_END)
87+
#define E2END (DATA_EEPROM_END - DATA_EEPROM_BASE)
88+
#elif defined (DATA_EEPROM_BANK2_END)
89+
/* assuming two contiguous banks */
90+
#define DATA_EEPROM_END DATA_EEPROM_BANK2_END
91+
#define E2END (DATA_EEPROM_BANK2_END - DATA_EEPROM_BASE)
92+
#elif defined (FLASH_EEPROM_END)
93+
#define DATA_EEPROM_BASE FLASH_EEPROM_BASE
94+
#define DATA_EEPROM_END FLASH_EEPROM_END
95+
#define E2END (DATA_EEPROM_END - DATA_EEPROM_BASE)
96+
#endif /* __EEPROM_END */
97+
98+
#else /* _EEPROM_BASE */
8399
#define E2END (FLASH_PAGE_SIZE - 1)
100+
#endif /* _EEPROM_BASE */
101+
84102
#endif
85103

86104
/* Exported macro ------------------------------------------------------------*/

libraries/SrcWrapper/src/stm32/stm32_eeprom.c

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,20 @@ static uint8_t eeprom_buffer[E2END + 1] __attribute__((aligned(8))) = {0};
151151
*/
152152
uint8_t eeprom_read_byte(const uint32_t pos)
153153
{
154+
#if defined(DATA_EEPROM_BASE)
155+
__IO uint8_t data = 0;
156+
if (pos <= (DATA_EEPROM_END - DATA_EEPROM_BASE)) {
157+
/* with actual EEPROM, pos is a relative address */
158+
data = *(__IO uint8_t *)(DATA_EEPROM_BASE + pos);
159+
/* align content of the buffered eeprom */
160+
eeprom_buffer[pos] = (uint8_t)data;
161+
162+
return (uint8_t)data;
163+
}
164+
#else
154165
eeprom_buffer_fill();
155166
return eeprom_buffered_read_byte(pos);
167+
#endif /* _EEPROM_BASE */
156168
}
157169

158170
/**
@@ -163,8 +175,20 @@ uint8_t eeprom_read_byte(const uint32_t pos)
163175
*/
164176
void eeprom_write_byte(uint32_t pos, uint8_t value)
165177
{
178+
#if defined(DATA_EEPROM_BASE)
179+
/* with actual EEPROM, pos is a relative address */
180+
if (pos <= (DATA_EEPROM_END - DATA_EEPROM_BASE)) {
181+
if (HAL_FLASHEx_DATAEEPROM_Unlock() == HAL_OK) {
182+
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, (pos + DATA_EEPROM_BASE), (uint32_t)value);
183+
HAL_FLASHEx_DATAEEPROM_Lock();
184+
}
185+
}
186+
/* align content of the buffered eeprom */
187+
eeprom_buffer[pos] = (uint8_t)value;
188+
#else
166189
eeprom_buffered_write_byte(pos, value);
167190
eeprom_buffer_flush();
191+
#endif /* _EEPROM_BASE */
168192
}
169193

170194
/**
@@ -195,7 +219,11 @@ void eeprom_buffered_write_byte(uint32_t pos, uint8_t value)
195219
*/
196220
void eeprom_buffer_fill(void)
197221
{
222+
#if defined(DATA_EEPROM_BASE)
223+
memcpy(eeprom_buffer, (uint8_t *)(DATA_EEPROM_BASE), E2END + 1);
224+
#else
198225
memcpy(eeprom_buffer, (uint8_t *)(FLASH_BASE_ADDRESS), E2END + 1);
226+
#endif /* STM32L0xx */
199227
}
200228

201229
#if defined(EEPROM_RETRAM_MODE)
@@ -210,6 +238,22 @@ void eeprom_buffer_flush(void)
210238
memcpy((uint8_t *)(FLASH_BASE_ADDRESS), eeprom_buffer, E2END + 1);
211239
}
212240

241+
#elif defined(DATA_EEPROM_BASE)
242+
/**
243+
* @brief This function writes the buffer content into the eeprom on L0
244+
* @param none
245+
* @retval none
246+
*/
247+
void eeprom_buffer_flush(void)
248+
{
249+
HAL_FLASHEx_DATAEEPROM_Unlock();
250+
251+
for (uint32_t i = 0 ; i < E2END ; i++) {
252+
/* Program byte (8-bit) at a specified address.*/
253+
*(__IO uint8_t *)(i + DATA_EEPROM_BASE) = (uint8_t) eeprom_buffer[i];
254+
}
255+
HAL_FLASHEx_DATAEEPROM_Lock();
256+
}
213257
#else /* defined(EEPROM_RETRAM_MODE) */
214258

215259
/**

0 commit comments

Comments
 (0)