Skip to content

Commit 9c22f15

Browse files
authored
Merge pull request stm32duino#338 from fpistm/eeprom
[EEPROM] Allow FLASH definition to be redefined
2 parents f07cd11 + 757fd88 commit 9c22f15

File tree

2 files changed

+84
-41
lines changed

2 files changed

+84
-41
lines changed

cores/arduino/stm32/stm32_eeprom.c

+71-33
Original file line numberDiff line numberDiff line change
@@ -39,23 +39,51 @@
3939
extern "C" {
4040
#endif
4141

42-
/* Use the last page of the flash to store data in order to prevent overwritting
43-
program data */
44-
#if defined (STM32F0xx) || defined (STM32F1xx) || defined(STM32L1xx)
45-
#if defined (FLASH_BANK2_END)
46-
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK2_END + 1) - FLASH_PAGE_SIZE))
47-
#elif defined (FLASH_BANK1_END)
48-
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_BANK1_END + 1) - FLASH_PAGE_SIZE))
42+
/* Be able to change FLASH_BANK_NUMBER to use if relevant */
43+
#if !defined(FLASH_BANK_NUMBER) &&\
44+
(defined(STM32F0xx) || defined(STM32F1xx) ||\
45+
defined(STM32L1xx) || defined(STM32L4xx))
46+
/* Fo STM32F0xx, FLASH_BANK_1 is not defined only FLASH_BANK1_END is defined */
47+
#if defined(STM32F0xx)
48+
#define FLASH_BANK_1 1U
49+
#endif
50+
#if defined(FLASH_BANK_2)
51+
#define FLASH_BANK_NUMBER FLASH_BANK_2
4952
#else
50-
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_END + 1) - FLASH_PAGE_SIZE))
51-
#endif /* FLASH_BANK2_END */
52-
#elif defined (STM32F2xx) || defined (STM32F4xx) || defined (STM32F7xx)
53-
#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_END + 1) - FLASH_PAGE_SIZE)
53+
#define FLASH_BANK_NUMBER FLASH_BANK_1
54+
#endif /* FLASH_BANK_2 */
55+
#ifndef FLASH_BANK_NUMBER
56+
#error "FLASH_BANK_NUMBER could not be defined"
57+
#endif
58+
#endif /* !FLASH_BANK_NUMBER */
59+
60+
/* Be able to change FLASH_DATA_SECTOR to use if relevant */
61+
#if defined(STM32F2xx) || defined(STM32F4xx) || defined(STM32F7xx)
62+
#if !defined(FLASH_DATA_SECTOR)
5463
#define FLASH_DATA_SECTOR ((uint32_t)(FLASH_SECTOR_TOTAL - 1))
64+
#else
65+
#ifndef FLASH_BASE_ADDRESS
66+
#error "FLASH_BASE_ADDRESS have to be defined when FLASH_DATA_SECTOR is defined"
67+
#endif
68+
#endif /* !FLASH_DATA_SECTOR */
69+
#endif /* STM32F2xx || STM32F4xx || STM32F7xx */
70+
71+
/* Be able to change FLASH_PAGE_NUMBER to use if relevant */
72+
#if !defined(FLASH_PAGE_NUMBER) && defined (STM32L4xx)
73+
#define FLASH_PAGE_NUMBER ((uint32_t)((FLASH_SIZE / FLASH_PAGE_SIZE) - 1))
74+
#endif /* !FLASH_PAGE_NUMBER */
75+
76+
/* Be able to change FLASH_END to use */
77+
#if !defined(FLASH_END) && !defined(STM32L0xx)
78+
#if defined (STM32F0xx) || defined (STM32F1xx) || defined(STM32L1xx)
79+
#if defined (FLASH_BANK2_END) && (FLASH_BANK_NUMBER == FLASH_BANK_2)
80+
#define FLASH_END FLASH_BANK2_END
81+
#elif defined (FLASH_BANK1_END) && (FLASH_BANK_NUMBER == FLASH_BANK_1)
82+
#define FLASH_END FLASH_BANK1_END
83+
#endif
5584
#elif defined (STM32F3xx)
5685
static inline uint32_t get_flash_end(void) {
5786
uint32_t size;
58-
5987
switch((*((uint16_t *)FLASH_SIZE_DATA_REGISTER))) {
6088
case 0x200U:
6189
size = 0x0807FFFFU;
@@ -76,23 +104,34 @@ static inline uint32_t get_flash_end(void) {
76104
size = 0x08003FFFU;
77105
break;
78106
}
79-
80107
return size;
81108
}
82-
#define FLASH_END_ADDR get_flash_end()
83-
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_END_ADDR + 1) - FLASH_PAGE_SIZE))
84-
#elif defined (STM32L0xx)
85-
#define FLASH_BASE_ADDRESS ((uint32_t)(DATA_EEPROM_BASE))
109+
#define FLASH_END get_flash_end()
86110
#elif defined (STM32L4xx)
87-
#ifndef FLASH_BANK_2
88-
#define FLASH_BANK_NUMBER FLASH_BANK_1
111+
/* If FLASH_PAGE_NUMBER is defined by user, this is not really end of the flash */
112+
#define FLASH_END ((uint32_t)(FLASH_BASE + (((FLASH_PAGE_NUMBER +1) * FLASH_PAGE_SIZE))-1))
113+
#endif
114+
#ifndef FLASH_END
115+
#error "FLASH_END could not be defined"
116+
#endif
117+
#endif /* FLASH_END */
118+
119+
/* Be able to change FLASH_BASE_ADDRESS to use */
120+
#ifndef FLASH_BASE_ADDRESS
121+
/*
122+
* By default, Use the last page of the flash to store data
123+
* in order to prevent overwritting
124+
* program data
125+
*/
126+
#if defined(STM32L0xx)
127+
#define FLASH_BASE_ADDRESS ((uint32_t)(DATA_EEPROM_BASE))
89128
#else
90-
#define FLASH_BANK_NUMBER FLASH_BANK_2
91-
#endif /* FLASH_BANK_2 */
92-
/* Flash base address */
93-
#define FLASH_PAGE_NUMBER ((uint32_t)((FLASH_SIZE / FLASH_PAGE_SIZE) - 1))
94-
#define FLASH_BASE_ADDRESS ((uint32_t)(FLASH_BASE + (FLASH_PAGE_NUMBER * FLASH_PAGE_SIZE)))
129+
#define FLASH_BASE_ADDRESS ((uint32_t)((FLASH_END + 1) - FLASH_PAGE_SIZE))
130+
#endif
131+
#ifndef FLASH_BASE_ADDRESS
132+
#error "FLASH_BASE_ADDRESS could not be defined"
95133
#endif
134+
#endif /* FLASH_BASE_ADDRESS */
96135

97136
static uint8_t eeprom_buffer[E2END] = {0};
98137

@@ -101,7 +140,7 @@ static uint8_t eeprom_buffer[E2END] = {0};
101140
* @param pos : address to read
102141
* @retval byte : data read from eeprom
103142
*/
104-
uint8_t eeprom_read_byte(const uint16_t pos) {
143+
uint8_t eeprom_read_byte(const uint32_t pos) {
105144
eeprom_buffer_fill();
106145
return eeprom_buffered_read_byte(pos);
107146
}
@@ -112,7 +151,7 @@ uint8_t eeprom_read_byte(const uint16_t pos) {
112151
* @param value : value to write
113152
* @retval none
114153
*/
115-
void eeprom_write_byte(uint16_t pos, uint8_t value) {
154+
void eeprom_write_byte(uint32_t pos, uint8_t value) {
116155
eeprom_buffered_write_byte(pos, value);
117156
eeprom_buffer_flush();
118157
}
@@ -122,7 +161,7 @@ void eeprom_write_byte(uint16_t pos, uint8_t value) {
122161
* @param pos : address to read
123162
* @retval byte : data read from eeprom
124163
*/
125-
uint8_t eeprom_buffered_read_byte(const uint16_t pos) {
164+
uint8_t eeprom_buffered_read_byte(const uint32_t pos) {
126165
return eeprom_buffer[pos];
127166
}
128167

@@ -132,7 +171,7 @@ uint8_t eeprom_buffered_read_byte(const uint16_t pos) {
132171
* @param value : value to write
133172
* @retval none
134173
*/
135-
void eeprom_buffered_write_byte(uint16_t pos, uint8_t value) {
174+
void eeprom_buffered_write_byte(uint32_t pos, uint8_t value) {
136175
eeprom_buffer[pos] = value;
137176
}
138177

@@ -162,13 +201,12 @@ void eeprom_buffer_flush(void) {
162201

163202
/* ERASING page */
164203
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
165-
#ifdef STM32L4xx
204+
#if defined(STM32L4xx) || defined(STM32F1xx)
166205
EraseInitStruct.Banks = FLASH_BANK_NUMBER;
206+
#endif
207+
#ifdef STM32L4xx
167208
EraseInitStruct.Page = FLASH_PAGE_NUMBER;
168209
#else
169-
#ifdef STM32F1xx
170-
EraseInitStruct.Banks = FLASH_BANK_1;
171-
#endif
172210
EraseInitStruct.PageAddress = FLASH_BASE_ADDRESS;
173211
#endif
174212
EraseInitStruct.NbPages = 1;
@@ -199,7 +237,7 @@ void eeprom_buffer_flush(void) {
199237
address += 4;
200238
offset += 4;
201239
#else
202-
data = *((uint64_t*)(((uint8_t*)eeprom_buffer + offset)));
240+
data = *((uint64_t*)((uint8_t*)eeprom_buffer + offset));
203241

204242
if(HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, address, data) == HAL_OK) {
205243
address += 8;

cores/arduino/stm32/stm32_eeprom.h

+13-8
Original file line numberDiff line numberDiff line change
@@ -37,31 +37,36 @@
3737
#define __STM32_EEPROM_H
3838

3939
/* Includes ------------------------------------------------------------------*/
40-
#include "stm32_def.h"
40+
#include "variant.h"
4141

4242
#ifdef __cplusplus
4343
extern "C" {
4444
#endif
4545

4646
/* Exported types ------------------------------------------------------------*/
4747
/* Exported constants --------------------------------------------------------*/
48-
49-
#if defined (STM32F2xx) || defined (STM32F4xx) || defined (STM32F7xx)
50-
/* FLASH_SECTOR_SIZE */
48+
#ifndef FLASH_PAGE_SIZE
49+
/*
50+
* FLASH_PAGE_SIZE is not defined for STM32F2xx, STM32F4xx and STM32F7xx
51+
* Could be redefined in variant.h or using build_opt.h
52+
* Warning: This is not the sector size, only the size used for EEPROM
53+
* emulation. Anyway, all the sector size will be erased.
54+
* So pay attention to not use this sector for other stuff.
55+
*/
5156
#define FLASH_PAGE_SIZE ((uint32_t)(16*1024)) /* 16kB page */
5257
#endif
5358
#define E2END FLASH_PAGE_SIZE
5459

5560
/* Exported macro ------------------------------------------------------------*/
5661
/* Exported functions ------------------------------------------------------- */
5762

58-
uint8_t eeprom_read_byte(const uint16_t pos);
59-
void eeprom_write_byte(uint16_t pos, uint8_t value);
63+
uint8_t eeprom_read_byte(const uint32_t pos);
64+
void eeprom_write_byte(uint32_t pos, uint8_t value);
6065

6166
void eeprom_buffer_fill();
6267
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);
68+
uint8_t eeprom_buffered_read_byte(const uint32_t pos);
69+
void eeprom_buffered_write_byte(uint32_t pos, uint8_t value);
6570

6671
#ifdef __cplusplus
6772
}

0 commit comments

Comments
 (0)