Skip to content

Commit cea3660

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 cea3660

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-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: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,17 @@ 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+
return (uint8_t)data;
160+
}
161+
#else
154162
eeprom_buffer_fill();
155163
return eeprom_buffered_read_byte(pos);
164+
#endif /* _EEPROM_BASE */
156165
}
157166

158167
/**
@@ -163,8 +172,18 @@ uint8_t eeprom_read_byte(const uint32_t pos)
163172
*/
164173
void eeprom_write_byte(uint32_t pos, uint8_t value)
165174
{
175+
#if defined(DATA_EEPROM_BASE)
176+
/* with actual EEPROM, pos is a relative address */
177+
if (pos <= (DATA_EEPROM_END - DATA_EEPROM_BASE)) {
178+
if (HAL_FLASHEx_DATAEEPROM_Unlock() == HAL_OK) {
179+
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, (pos + DATA_EEPROM_BASE), (uint32_t)value);
180+
HAL_FLASHEx_DATAEEPROM_Lock();
181+
}
182+
}
183+
#else
166184
eeprom_buffered_write_byte(pos, value);
167185
eeprom_buffer_flush();
186+
#endif /* _EEPROM_BASE */
168187
}
169188

170189
/**
@@ -195,7 +214,17 @@ void eeprom_buffered_write_byte(uint32_t pos, uint8_t value)
195214
*/
196215
void eeprom_buffer_fill(void)
197216
{
217+
#if defined(DATA_EEPROM_BASE) || defined(FLASH_EEPROM_BASE)
218+
uint32_t i;
219+
__IO uint32_t data;
220+
221+
for (i = DATA_EEPROM_BASE ; i <= DATA_EEPROM_END ; i++) {
222+
data = *(__IO uint32_t *)(DATA_EEPROM_BASE + i);
223+
eeprom_buffer[i] = (uint8_t)data;
224+
}
225+
#else
198226
memcpy(eeprom_buffer, (uint8_t *)(FLASH_BASE_ADDRESS), E2END + 1);
227+
#endif /* STM32L0xx */
199228
}
200229

201230
#if defined(EEPROM_RETRAM_MODE)
@@ -210,6 +239,22 @@ void eeprom_buffer_flush(void)
210239
memcpy((uint8_t *)(FLASH_BASE_ADDRESS), eeprom_buffer, E2END + 1);
211240
}
212241

242+
#elif defined(DATA_EEPROM_BASE) || defined(FLASH_EEPROM_BASE)
243+
/**
244+
* @brief This function writes the buffer content into the eeprom on L0
245+
* @param none
246+
* @retval none
247+
*/
248+
void eeprom_buffer_flush(void)
249+
{
250+
uint32_t i;
251+
252+
for (i = DATA_EEPROM_BASE ; i <= DATA_EEPROM_END ; i++) {
253+
HAL_FLASHEx_DATAEEPROM_Unlock();
254+
HAL_FLASHEx_DATAEEPROM_Program(FLASH_TYPEPROGRAMDATA_BYTE, i, (uint32_t)eeprom_buffer[i]);
255+
HAL_FLASHEx_DATAEEPROM_Lock();
256+
}
257+
}
213258
#else /* defined(EEPROM_RETRAM_MODE) */
214259

215260
/**

0 commit comments

Comments
 (0)