2 * This file is part of Cleanflight and Betaflight.
4 * Cleanflight and Betaflight are free software. You can redistribute
5 * this software and/or modify this software under the terms of the
6 * GNU General Public License as published by the Free Software
7 * Foundation, either version 3 of the License, or (at your option)
10 * Cleanflight and Betaflight are distributed in the hope that they
11 * will be useful, but WITHOUT ANY WARRANTY; without even the implied
12 * warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
13 * See the GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this software.
18 * If not, see <http://www.gnu.org/licenses/>.
25 #include "drivers/system.h"
26 #include "drivers/flash.h"
28 #include "config/config_eeprom.h"
29 #include "config/config_streamer.h"
31 #if !defined(CONFIG_IN_FLASH)
32 #if defined(CONFIG_IN_RAM) && defined(PERSISTENT)
33 PERSISTENT
uint8_t eepromData
[EEPROM_SIZE
];
35 uint8_t eepromData
[EEPROM_SIZE
];
40 #if !defined(FLASH_PAGE_SIZE)
41 #error "Flash page size not defined for target."
44 void config_streamer_init(config_streamer_t
*c
)
46 memset(c
, 0, sizeof(*c
));
49 void config_streamer_start(config_streamer_t
*c
, uintptr_t base
, int size
)
51 // base must start at FLASH_PAGE_SIZE boundary when using embedded flash.
55 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
57 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
58 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
69 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
71 #elif defined(CONFIG_IN_FLASH)
73 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
74 #elif defined(STM32F7)
76 #elif defined(STM32H7)
78 #elif defined(STM32G4)
81 flash_flag_clear(FLASH_ODF_FLAG
| FLASH_PRGMERR_FLAG
| FLASH_EPPERR_FLAG
);
82 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
85 # error "Unsupported CPU"
91 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
92 // No flash sector method required.
93 #elif defined(CONFIG_IN_FLASH)
94 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
96 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
97 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
98 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
99 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
100 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
101 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
102 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
103 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
105 F7X5XI device with 2M flash
106 Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes
107 Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes
108 Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes
109 Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes
112 static uint32_t getFLASHSectorForEEPROM(void)
114 if ((uint32_t)&__config_start
<= 0x08007FFF)
115 return FLASH_SECTOR_0
;
116 if ((uint32_t)&__config_start
<= 0x0800FFFF)
117 return FLASH_SECTOR_1
;
118 if ((uint32_t)&__config_start
<= 0x08017FFF)
119 return FLASH_SECTOR_2
;
120 if ((uint32_t)&__config_start
<= 0x0801FFFF)
121 return FLASH_SECTOR_3
;
122 if ((uint32_t)&__config_start
<= 0x0803FFFF)
123 return FLASH_SECTOR_4
;
124 if ((uint32_t)&__config_start
<= 0x0807FFFF)
125 return FLASH_SECTOR_5
;
126 if ((uint32_t)&__config_start
<= 0x080BFFFF)
127 return FLASH_SECTOR_6
;
128 if ((uint32_t)&__config_start
<= 0x080FFFFF)
129 return FLASH_SECTOR_7
;
130 #if defined(STM32F765xx)
131 if ((uint32_t)&__config_start
<= 0x0813FFFF)
132 return FLASH_SECTOR_8
;
133 if ((uint32_t)&__config_start
<= 0x0817FFFF)
134 return FLASH_SECTOR_9
;
135 if ((uint32_t)&__config_start
<= 0x081BFFFF)
136 return FLASH_SECTOR_10
;
137 if ((uint32_t)&__config_start
<= 0x081FFFFF)
138 return FLASH_SECTOR_11
;
143 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
147 #elif defined(STM32F722xx)
149 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
150 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
151 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
152 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
153 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
154 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
155 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
156 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
159 static uint32_t getFLASHSectorForEEPROM(void)
161 if ((uint32_t)&__config_start
<= 0x08003FFF)
162 return FLASH_SECTOR_0
;
163 if ((uint32_t)&__config_start
<= 0x08007FFF)
164 return FLASH_SECTOR_1
;
165 if ((uint32_t)&__config_start
<= 0x0800BFFF)
166 return FLASH_SECTOR_2
;
167 if ((uint32_t)&__config_start
<= 0x0800FFFF)
168 return FLASH_SECTOR_3
;
169 if ((uint32_t)&__config_start
<= 0x0801FFFF)
170 return FLASH_SECTOR_4
;
171 if ((uint32_t)&__config_start
<= 0x0803FFFF)
172 return FLASH_SECTOR_5
;
173 if ((uint32_t)&__config_start
<= 0x0805FFFF)
174 return FLASH_SECTOR_6
;
175 if ((uint32_t)&__config_start
<= 0x0807FFFF)
176 return FLASH_SECTOR_7
;
180 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
184 #elif defined(STM32F4)
186 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
187 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
188 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
189 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
190 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
191 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
192 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
193 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
194 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
195 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
196 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
197 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
200 static uint32_t getFLASHSectorForEEPROM(void)
202 if ((uint32_t)&__config_start
<= 0x08003FFF)
203 return FLASH_Sector_0
;
204 if ((uint32_t)&__config_start
<= 0x08007FFF)
205 return FLASH_Sector_1
;
206 if ((uint32_t)&__config_start
<= 0x0800BFFF)
207 return FLASH_Sector_2
;
208 if ((uint32_t)&__config_start
<= 0x0800FFFF)
209 return FLASH_Sector_3
;
210 if ((uint32_t)&__config_start
<= 0x0801FFFF)
211 return FLASH_Sector_4
;
212 if ((uint32_t)&__config_start
<= 0x0803FFFF)
213 return FLASH_Sector_5
;
214 if ((uint32_t)&__config_start
<= 0x0805FFFF)
215 return FLASH_Sector_6
;
216 if ((uint32_t)&__config_start
<= 0x0807FFFF)
217 return FLASH_Sector_7
;
218 if ((uint32_t)&__config_start
<= 0x0809FFFF)
219 return FLASH_Sector_8
;
220 if ((uint32_t)&__config_start
<= 0x080DFFFF)
221 return FLASH_Sector_9
;
222 if ((uint32_t)&__config_start
<= 0x080BFFFF)
223 return FLASH_Sector_10
;
224 if ((uint32_t)&__config_start
<= 0x080FFFFF)
225 return FLASH_Sector_11
;
229 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
233 #elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx)
235 MCUs with uniform array of equal size sectors, handled in two banks having contiguous address.
236 (Devices with non-contiguous flash layout is not currently useful anyways.)
239 2 bank * 8 sector/bank * 128K/sector (2MB)
240 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
241 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
244 1 bank * 8 sector/bank * 128K/sector (1MB)
245 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
248 2 bank * 128 sector/bank * 8KB/sector (2MB)
249 Bank 1 0x08000000 - 0x080FFFFF 8KB * 128
250 Bank 2 0x08100000 - 0x081FFFFF 8KB * 128
252 G473/474 in dual bank mode
253 2 bank * 128 sector/bank * 2KB/sector (512KB)
254 Bank 1 0x08000000 - 0x0803FFFF 2KB * 128
255 Bank 2 0x08040000 - 0x0807FFFF 2KB * 128
257 Note that FLASH_BANK_SIZE constant used in the following code changes depending on
258 bank operation mode. The code assumes dual bank operation, in which case the
259 FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL.
262 #if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx)
263 #define FLASH_PAGE_PER_BANK 8
264 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
265 #define FLASH_PAGE_PER_BANK 128
266 #elif defined(STM32G4)
267 #define FLASH_PAGE_PER_BANK 128
268 // These are not defined in CMSIS like H7
269 #define FLASH_BANK1_BASE FLASH_BASE
270 #define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE)
273 static void getFLASHSectorForEEPROM(uint32_t address
, uint32_t *bank
, uint32_t *sector
)
275 #if defined(FLASH_BANK2_BASE)
276 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK2_BASE
) {
277 *bank
= FLASH_BANK_1
;
278 } else if (address
>= FLASH_BANK2_BASE
&& address
< FLASH_BANK2_BASE
+ FLASH_BANK_SIZE
) {
279 *bank
= FLASH_BANK_2
;
280 address
-= FLASH_BANK_SIZE
;
283 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK1_BASE
+ FLASH_BANK_SIZE
) {
284 *bank
= FLASH_BANK_1
;
290 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
294 address
-= FLASH_BANK1_BASE
;
295 *sector
= address
/ FLASH_PAGE_SIZE
;
297 #elif defined(STM32H750xx)
299 The memory map supports 2 banks of 8 128k sectors like the H743xx, but there is only one 128K sector so we save some code
300 space by using a smaller function.
303 Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes
307 static void getFLASHSectorForEEPROM(uint32_t *bank
, uint32_t *sector
)
310 uint32_t start
= (uint32_t)&__config_start
;
312 if (start
== FLASH_BANK1_BASE
) {
313 *sector
= FLASH_SECTOR_0
;
314 *bank
= FLASH_BANK_1
;
318 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
323 #endif // CONFIG_IN_FLASH
325 // FIXME the return values are currently magic numbers
326 static int write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
331 #if defined(CONFIG_IN_EXTERNAL_FLASH)
333 uint32_t dataOffset
= (uint32_t)(c
->address
- (uintptr_t)&eepromData
[0]);
335 const flashPartition_t
*flashPartition
= flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG
);
336 const flashGeometry_t
*flashGeometry
= flashGetGeometry();
338 uint32_t flashStartAddress
= flashPartition
->startSector
* flashGeometry
->sectorSize
;
339 uint32_t flashOverflowAddress
= ((flashPartition
->endSector
+ 1) * flashGeometry
->sectorSize
); // +1 to sector for inclusive
341 uint32_t flashAddress
= flashStartAddress
+ dataOffset
;
342 if (flashAddress
+ CONFIG_STREAMER_BUFFER_SIZE
> flashOverflowAddress
) {
343 return -3; // address is past end of partition
346 uint32_t flashSectorSize
= flashGeometry
->sectorSize
;
347 uint32_t flashPageSize
= flashGeometry
->pageSize
;
348 const uint8_t *buffers
[1];
349 uint32_t bufferSizes
[1];
351 bool onPageBoundary
= (flashAddress
% flashPageSize
== 0);
352 if (onPageBoundary
) {
354 bool firstPage
= (flashAddress
== flashStartAddress
);
356 flashPageProgramFinish();
359 if (flashAddress
% flashSectorSize
== 0) {
360 flashEraseSector(flashAddress
);
363 flashPageProgramBegin(flashAddress
, NULL
);
366 buffers
[0] = (uint8_t *)buffer
;
367 bufferSizes
[0] = CONFIG_STREAMER_BUFFER_SIZE
;
369 flashPageProgramContinue(buffers
, bufferSizes
, 1);
371 #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
372 if (c
->address
== (uintptr_t)&eepromData
[0]) {
373 memset(eepromData
, 0, sizeof(eepromData
));
376 uint64_t *dest_addr
= (uint64_t *)c
->address
;
377 uint64_t *src_addr
= (uint64_t*)buffer
;
378 uint8_t row_index
= CONFIG_STREAMER_BUFFER_SIZE
/ sizeof(uint64_t);
379 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
% sizeof(uint64_t) == 0, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
380 /* copy the 256 bits flash word */
383 *dest_addr
++ = *src_addr
++;
384 } while (--row_index
!= 0);
386 #elif defined(CONFIG_IN_FILE)
388 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
389 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
390 if (status
!= FLASH_COMPLETE
) {
394 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
395 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
396 if (status
!= FLASH_COMPLETE
) {
400 #elif defined(CONFIG_IN_FLASH)
403 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
404 FLASH_EraseInitTypeDef EraseInitStruct
= {
405 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
406 #if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ))
407 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
411 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Sector
);
412 uint32_t SECTORError
;
413 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
414 if (status
!= HAL_OK
) {
420 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress);
421 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t) * FLASH_NB_32BITWORD_IN_FLASHWORD
, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
422 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD
, c
->address
, (uint64_t)(uint32_t)buffer
);
423 if (status
!= HAL_OK
) {
426 #elif defined(STM32F7)
427 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
428 FLASH_EraseInitTypeDef EraseInitStruct
= {
429 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
430 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
433 EraseInitStruct
.Sector
= getFLASHSectorForEEPROM();
434 uint32_t SECTORError
;
435 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
436 if (status
!= HAL_OK
) {
441 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
443 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
444 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD
, c
->address
, (uint64_t)*buffer
);
445 if (status
!= HAL_OK
) {
448 #elif defined(STM32G4)
449 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
451 FLASH_EraseInitTypeDef EraseInitStruct
= {
452 .TypeErase
= FLASH_TYPEERASE_PAGES
,
455 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Page
);
456 uint32_t SECTORError
;
457 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
458 if (status
!= HAL_OK
) {
463 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t) * 2, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
464 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD
, c
->address
, (uint64_t)*buffer
);
465 if (status
!= HAL_OK
) {
468 #elif defined(AT32F4)
469 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
470 const flash_status_type status
= flash_sector_erase(c
->address
);
471 if (status
!= FLASH_OPERATE_DONE
) {
476 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
477 const flash_status_type status
= flash_word_program(c
->address
, (uint32_t)*buffer
);
478 if (status
!= FLASH_OPERATE_DONE
) {
481 #else // !STM32H7 && !STM32F7 && !STM32G4
482 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
483 const FLASH_Status status
= FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3
); //0x08080000 to 0x080A0000
484 if (status
!= FLASH_COMPLETE
) {
489 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE
== sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
490 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
491 if (status
!= FLASH_COMPLETE
) {
496 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;
500 int config_streamer_write(config_streamer_t
*c
, const uint8_t *p
, uint32_t size
)
502 for (const uint8_t *pat
= p
; pat
!= (uint8_t*)p
+ size
; pat
++) {
503 c
->buffer
.b
[c
->at
++] = *pat
;
505 if (c
->at
== sizeof(c
->buffer
)) {
506 c
->err
= write_word(c
, &c
->buffer
.w
);
513 int config_streamer_status(config_streamer_t
*c
)
518 int config_streamer_flush(config_streamer_t
*c
)
521 memset(c
->buffer
.b
+ c
->at
, 0, sizeof(c
->buffer
) - c
->at
);
522 c
->err
= write_word(c
, &c
->buffer
.w
);
528 int config_streamer_finish(config_streamer_t
*c
)
531 #if defined(CONFIG_IN_SDCARD)
532 saveEEPROMToSDCard();
533 #elif defined(CONFIG_IN_EXTERNAL_FLASH)
535 #elif defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
536 saveEEPROMToMemoryMappedFlash();
537 #elif defined(CONFIG_IN_RAM)
539 #elif defined(CONFIG_IN_FILE)
541 #elif defined(CONFIG_IN_FLASH)
542 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
544 #elif defined(AT32F4)