39
39
extern "C" {
40
40
#endif
41
41
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
49
52
#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 )
54
63
#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
55
84
#elif defined (STM32F3xx )
56
85
static inline uint32_t get_flash_end (void ) {
57
86
uint32_t size ;
58
-
59
87
switch ((* ((uint16_t * )FLASH_SIZE_DATA_REGISTER ))) {
60
88
case 0x200U :
61
89
size = 0x0807FFFFU ;
@@ -76,23 +104,34 @@ static inline uint32_t get_flash_end(void) {
76
104
size = 0x08003FFFU ;
77
105
break ;
78
106
}
79
-
80
107
return size ;
81
108
}
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()
86
110
#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))
89
128
#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"
95
133
#endif
134
+ #endif /* FLASH_BASE_ADDRESS */
96
135
97
136
static uint8_t eeprom_buffer [E2END ] = {0 };
98
137
@@ -101,7 +140,7 @@ static uint8_t eeprom_buffer[E2END] = {0};
101
140
* @param pos : address to read
102
141
* @retval byte : data read from eeprom
103
142
*/
104
- uint8_t eeprom_read_byte (const uint16_t pos ) {
143
+ uint8_t eeprom_read_byte (const uint32_t pos ) {
105
144
eeprom_buffer_fill ();
106
145
return eeprom_buffered_read_byte (pos );
107
146
}
@@ -112,7 +151,7 @@ uint8_t eeprom_read_byte(const uint16_t pos) {
112
151
* @param value : value to write
113
152
* @retval none
114
153
*/
115
- void eeprom_write_byte (uint16_t pos , uint8_t value ) {
154
+ void eeprom_write_byte (uint32_t pos , uint8_t value ) {
116
155
eeprom_buffered_write_byte (pos , value );
117
156
eeprom_buffer_flush ();
118
157
}
@@ -122,7 +161,7 @@ void eeprom_write_byte(uint16_t pos, uint8_t value) {
122
161
* @param pos : address to read
123
162
* @retval byte : data read from eeprom
124
163
*/
125
- uint8_t eeprom_buffered_read_byte (const uint16_t pos ) {
164
+ uint8_t eeprom_buffered_read_byte (const uint32_t pos ) {
126
165
return eeprom_buffer [pos ];
127
166
}
128
167
@@ -132,7 +171,7 @@ uint8_t eeprom_buffered_read_byte(const uint16_t pos) {
132
171
* @param value : value to write
133
172
* @retval none
134
173
*/
135
- void eeprom_buffered_write_byte (uint16_t pos , uint8_t value ) {
174
+ void eeprom_buffered_write_byte (uint32_t pos , uint8_t value ) {
136
175
eeprom_buffer [pos ] = value ;
137
176
}
138
177
@@ -162,13 +201,12 @@ void eeprom_buffer_flush(void) {
162
201
163
202
/* ERASING page */
164
203
EraseInitStruct .TypeErase = FLASH_TYPEERASE_PAGES ;
165
- #ifdef STM32L4xx
204
+ #if defined( STM32L4xx ) || defined( STM32F1xx )
166
205
EraseInitStruct .Banks = FLASH_BANK_NUMBER ;
206
+ #endif
207
+ #ifdef STM32L4xx
167
208
EraseInitStruct .Page = FLASH_PAGE_NUMBER ;
168
209
#else
169
- #ifdef STM32F1xx
170
- EraseInitStruct .Banks = FLASH_BANK_1 ;
171
- #endif
172
210
EraseInitStruct .PageAddress = FLASH_BASE_ADDRESS ;
173
211
#endif
174
212
EraseInitStruct .NbPages = 1 ;
@@ -199,7 +237,7 @@ void eeprom_buffer_flush(void) {
199
237
address += 4 ;
200
238
offset += 4 ;
201
239
#else
202
- data = * ((uint64_t * )((( uint8_t * )eeprom_buffer + offset ) ));
240
+ data = * ((uint64_t * )((uint8_t * )eeprom_buffer + offset ));
203
241
204
242
if (HAL_FLASH_Program (FLASH_TYPEPROGRAM_DOUBLEWORD , address , data ) == HAL_OK ) {
205
243
address += 8 ;
0 commit comments