Skip to content

Commit 0f522e0

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 0f522e0

File tree

2 files changed

+41
-0
lines changed

2 files changed

+41
-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: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,19 @@ 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+
#else
154164
eeprom_buffer_fill();
155165
return eeprom_buffered_read_byte(pos);
166+
#endif /* _EEPROM_BASE */
156167
}
157168

158169
/**
@@ -163,8 +174,20 @@ uint8_t eeprom_read_byte(const uint32_t pos)
163174
*/
164175
void eeprom_write_byte(uint32_t pos, uint8_t value)
165176
{
177+
#if defined(DATA_EEPROM_BASE)
178+
/* with actual EEPROM, pos is a relative address */
179+
if (pos <= (DATA_EEPROM_END - DATA_EEPROM_BASE)) {
180+
if (HAL_FLASHEx_DATAEEPROM_Unlock() == HAL_OK) {
181+
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, (pos + DATA_EEPROM_BASE), (uint32_t)value);
182+
HAL_FLASHEx_DATAEEPROM_Lock();
183+
}
184+
}
185+
/* align content of the buffered eeprom */
186+
eeprom_buffer[pos] = (uint8_t)value;
187+
#else
166188
eeprom_buffered_write_byte(pos, value);
167189
eeprom_buffer_flush();
190+
#endif /* _EEPROM_BASE */
168191
}
169192

170193
/**

0 commit comments

Comments
 (0)