Skip to content

Commit ff94ae5

Browse files
committed
fix: library: EEPROM: use correct flash size
FLASH_SIZE defined by the CMSIS Device Peripheral Access Layer Header File is the maximum one of a product line. So depending of the mcu this is not the correct one. As an example, L432KC have 256 KB while L432Kb has 128 KB. Using LL_GetFlashSize() allows to get the correct FLASH size. Fixes stm32duino#1316 Signed-off-by: Frederic Pillon <[email protected]>
1 parent 782e8c6 commit ff94ae5

File tree

1 file changed

+4
-30
lines changed

1 file changed

+4
-30
lines changed

Diff for: libraries/EEPROM/src/utility/stm32_eeprom.c

+4-30
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@
1717
*/
1818

1919
#include "stm32_eeprom.h"
20+
#include "stm32yyxx_ll_utils.h"
2021
#include <string.h>
2122

2223
#ifdef __cplusplus
@@ -47,8 +48,8 @@ extern "C" {
4748
#endif /* FLASH_SECTOR_TOTAL */
4849

4950
/* Be able to change FLASH_PAGE_NUMBER to use if relevant */
50-
#if !defined(FLASH_PAGE_NUMBER) && defined (FLASH_SIZE) && defined(FLASH_PAGE_SIZE)
51-
#define FLASH_PAGE_NUMBER ((uint32_t)((FLASH_SIZE / FLASH_PAGE_SIZE) - 1))
51+
#if !defined(FLASH_PAGE_NUMBER) && defined(FLASH_PAGE_SIZE)
52+
#define FLASH_PAGE_NUMBER ((uint32_t)(((LL_GetFlashSize() * 1024) / FLASH_PAGE_SIZE) - 1))
5253
#endif /* !FLASH_PAGE_NUMBER */
5354

5455
/* Be able to change FLASH_END to use */
@@ -61,33 +62,6 @@ extern "C" {
6162
#define FLASH_END FLASH_BANK2_END
6263
#elif defined (FLASH_BANK1_END) && (FLASH_BANK_NUMBER == FLASH_BANK_1)
6364
#define FLASH_END FLASH_BANK1_END
64-
#elif !defined (FLASH_PAGE_NUMBER)
65-
static inline uint32_t get_flash_end(void)
66-
{
67-
uint32_t size;
68-
switch ((*((uint16_t *)FLASH_SIZE_DATA_REGISTER))) {
69-
case 0x200U:
70-
size = 0x0807FFFFU;
71-
break;
72-
case 0x100U:
73-
size = 0x0803FFFFU;
74-
break;
75-
case 0x80U:
76-
size = 0x0801FFFFU;
77-
break;
78-
case 0x40U:
79-
size = 0x0800FFFFU;
80-
break;
81-
case 0x20U:
82-
size = 0x08007FFFU;
83-
break;
84-
default:
85-
size = 0x08003FFFU;
86-
break;
87-
}
88-
return size;
89-
}
90-
#define FLASH_END get_flash_end()
9165
#elif defined(FLASH_BASE) && defined(FLASH_PAGE_NUMBER) && defined (FLASH_PAGE_SIZE)
9266
/* If FLASH_PAGE_NUMBER is defined by user, this is not really end of the flash */
9367
#define FLASH_END ((uint32_t)(FLASH_BASE + (((FLASH_PAGE_NUMBER +1) * FLASH_PAGE_SIZE))-1))
@@ -227,7 +201,7 @@ void eeprom_buffer_flush(void)
227201
#if defined(FLASH_BANK_NUMBER)
228202
EraseInitStruct.Banks = FLASH_BANK_NUMBER;
229203
#endif /* FLASH_BANK_NUMBER */
230-
#if defined (FLASH_PAGE_NUMBER)
204+
#if defined (FLASH_PAGE_NUMBER) && defined(FLASH_SIZE)
231205
EraseInitStruct.Page = FLASH_PAGE_NUMBER;
232206
#else
233207
EraseInitStruct.PageAddress = FLASH_BASE_ADDRESS;

0 commit comments

Comments
 (0)