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_streamer.h"
30 #if !defined(CONFIG_IN_FLASH)
31 #if defined(CONFIG_IN_RAM) && defined(PERSISTENT)
32 PERSISTENT
uint8_t eepromData
[EEPROM_SIZE
];
34 uint8_t eepromData
[EEPROM_SIZE
];
39 #if (defined(STM32H750xx) || defined(STM32H730xx)) && !(defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD))
40 #error "The configured MCU only has one flash page which contains the bootloader, no spare flash pages available, use external storage for persistent config or ram for target testing"
42 // @todo this is not strictly correct for F4/F7, where sector sizes are variable
43 #if !defined(FLASH_PAGE_SIZE)
45 # if defined(STM32F10X_MD)
46 # define FLASH_PAGE_SIZE (0x400)
47 # elif defined(STM32F10X_HD)
48 # define FLASH_PAGE_SIZE (0x800)
50 # elif defined(STM32F303xC)
51 # define FLASH_PAGE_SIZE (0x800)
53 # elif defined(STM32F40_41xxx)
54 # define FLASH_PAGE_SIZE ((uint32_t)0x4000) // 16K sectors
55 # elif defined (STM32F411xE)
56 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
57 # elif defined(STM32F427_437xx)
58 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
59 # elif defined (STM32F446xx)
60 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
62 #elif defined(STM32F722xx)
63 # define FLASH_PAGE_SIZE ((uint32_t)0x4000) // 16K sectors
64 # elif defined(STM32F745xx)
65 # define FLASH_PAGE_SIZE ((uint32_t)0x8000) // 32K sectors
66 # elif defined(STM32F746xx)
67 # define FLASH_PAGE_SIZE ((uint32_t)0x8000)
68 # elif defined(STM32F765xx)
69 # define FLASH_PAGE_SIZE ((uint32_t)0x8000)
70 # elif defined(UNIT_TEST)
71 # define FLASH_PAGE_SIZE (0x400)
73 # elif defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
74 # define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
75 # elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
76 # define FLASH_PAGE_SIZE ((uint32_t)0x2000) // 8K sectors
78 # elif defined(STM32G4)
79 # define FLASH_PAGE_SIZE ((uint32_t)0x800) // 2K page
81 # elif defined(SIMULATOR_BUILD)
82 # define FLASH_PAGE_SIZE (0x400)
84 # error "Flash page size not defined for target."
88 void config_streamer_init(config_streamer_t
*c
)
90 memset(c
, 0, sizeof(*c
));
93 void config_streamer_start(config_streamer_t
*c
, uintptr_t base
, int size
)
95 // base must start at FLASH_PAGE_SIZE boundary when using embedded flash.
99 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
101 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
102 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
111 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
113 #elif defined(CONFIG_IN_FLASH)
114 #if defined(STM32F10X)
115 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPRTERR
);
116 #elif defined(STM32F303)
117 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPERR
);
118 #elif defined(STM32F4)
119 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
120 #elif defined(STM32F7)
122 #elif defined(STM32H7)
124 #elif defined(STM32G4)
126 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
129 # error "Unsupported CPU"
135 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
136 // No flash sector method required.
137 #elif defined(CONFIG_IN_FLASH)
138 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
140 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
141 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
142 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
143 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
144 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
145 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
146 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
147 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
149 F7X5XI device with 2M flash
150 Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes
151 Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes
152 Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes
153 Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes
156 static uint32_t getFLASHSectorForEEPROM(void)
158 if ((uint32_t)&__config_start
<= 0x08007FFF)
159 return FLASH_SECTOR_0
;
160 if ((uint32_t)&__config_start
<= 0x0800FFFF)
161 return FLASH_SECTOR_1
;
162 if ((uint32_t)&__config_start
<= 0x08017FFF)
163 return FLASH_SECTOR_2
;
164 if ((uint32_t)&__config_start
<= 0x0801FFFF)
165 return FLASH_SECTOR_3
;
166 if ((uint32_t)&__config_start
<= 0x0803FFFF)
167 return FLASH_SECTOR_4
;
168 if ((uint32_t)&__config_start
<= 0x0807FFFF)
169 return FLASH_SECTOR_5
;
170 if ((uint32_t)&__config_start
<= 0x080BFFFF)
171 return FLASH_SECTOR_6
;
172 if ((uint32_t)&__config_start
<= 0x080FFFFF)
173 return FLASH_SECTOR_7
;
174 #if defined(STM32F765xx)
175 if ((uint32_t)&__config_start
<= 0x0813FFFF)
176 return FLASH_SECTOR_8
;
177 if ((uint32_t)&__config_start
<= 0x0817FFFF)
178 return FLASH_SECTOR_9
;
179 if ((uint32_t)&__config_start
<= 0x081BFFFF)
180 return FLASH_SECTOR_10
;
181 if ((uint32_t)&__config_start
<= 0x081FFFFF)
182 return FLASH_SECTOR_11
;
187 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
191 #elif defined(STM32F722xx)
193 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
194 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
195 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
196 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
197 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
198 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
199 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
200 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
203 static uint32_t getFLASHSectorForEEPROM(void)
205 if ((uint32_t)&__config_start
<= 0x08003FFF)
206 return FLASH_SECTOR_0
;
207 if ((uint32_t)&__config_start
<= 0x08007FFF)
208 return FLASH_SECTOR_1
;
209 if ((uint32_t)&__config_start
<= 0x0800BFFF)
210 return FLASH_SECTOR_2
;
211 if ((uint32_t)&__config_start
<= 0x0800FFFF)
212 return FLASH_SECTOR_3
;
213 if ((uint32_t)&__config_start
<= 0x0801FFFF)
214 return FLASH_SECTOR_4
;
215 if ((uint32_t)&__config_start
<= 0x0803FFFF)
216 return FLASH_SECTOR_5
;
217 if ((uint32_t)&__config_start
<= 0x0805FFFF)
218 return FLASH_SECTOR_6
;
219 if ((uint32_t)&__config_start
<= 0x0807FFFF)
220 return FLASH_SECTOR_7
;
224 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
228 #elif defined(STM32F4)
230 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
231 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
232 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
233 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
234 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
235 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
236 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
237 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
238 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
239 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
240 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
241 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
244 static uint32_t getFLASHSectorForEEPROM(void)
246 if ((uint32_t)&__config_start
<= 0x08003FFF)
247 return FLASH_Sector_0
;
248 if ((uint32_t)&__config_start
<= 0x08007FFF)
249 return FLASH_Sector_1
;
250 if ((uint32_t)&__config_start
<= 0x0800BFFF)
251 return FLASH_Sector_2
;
252 if ((uint32_t)&__config_start
<= 0x0800FFFF)
253 return FLASH_Sector_3
;
254 if ((uint32_t)&__config_start
<= 0x0801FFFF)
255 return FLASH_Sector_4
;
256 if ((uint32_t)&__config_start
<= 0x0803FFFF)
257 return FLASH_Sector_5
;
258 if ((uint32_t)&__config_start
<= 0x0805FFFF)
259 return FLASH_Sector_6
;
260 if ((uint32_t)&__config_start
<= 0x0807FFFF)
261 return FLASH_Sector_7
;
262 if ((uint32_t)&__config_start
<= 0x0809FFFF)
263 return FLASH_Sector_8
;
264 if ((uint32_t)&__config_start
<= 0x080DFFFF)
265 return FLASH_Sector_9
;
266 if ((uint32_t)&__config_start
<= 0x080BFFFF)
267 return FLASH_Sector_10
;
268 if ((uint32_t)&__config_start
<= 0x080FFFFF)
269 return FLASH_Sector_11
;
273 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
277 #elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx)
279 MCUs with uniform array of equal size sectors, handled in two banks having contiguous address.
280 (Devices with non-contiguous flash layout is not currently useful anyways.)
283 2 bank * 8 sector/bank * 128K/sector (2MB)
284 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
285 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
288 1 bank * 8 sector/bank * 128K/sector (1MB)
289 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
292 2 bank * 128 sector/bank * 8KB/sector (2MB)
293 Bank 1 0x08000000 - 0x080FFFFF 8KB * 128
294 Bank 2 0x08100000 - 0x081FFFFF 8KB * 128
296 G473/474 in dual bank mode
297 2 bank * 128 sector/bank * 2KB/sector (512KB)
298 Bank 1 0x08000000 - 0x0803FFFF 2KB * 128
299 Bank 2 0x08040000 - 0x0807FFFF 2KB * 128
301 Note that FLASH_BANK_SIZE constant used in the following code changes depending on
302 bank operation mode. The code assumes dual bank operation, in which case the
303 FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL.
306 #if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx)
307 #define FLASH_PAGE_PER_BANK 8
308 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
309 #define FLASH_PAGE_PER_BANK 128
310 #elif defined(STM32G4)
311 #define FLASH_PAGE_PER_BANK 128
312 // These are not defined in CMSIS like H7
313 #define FLASH_BANK1_BASE FLASH_BASE
314 #define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE)
317 static void getFLASHSectorForEEPROM(uint32_t address
, uint32_t *bank
, uint32_t *sector
)
319 #if defined(FLASH_BANK2_BASE)
320 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK2_BASE
) {
321 *bank
= FLASH_BANK_1
;
322 } else if (address
>= FLASH_BANK2_BASE
&& address
< FLASH_BANK2_BASE
+ FLASH_BANK_SIZE
) {
323 *bank
= FLASH_BANK_2
;
324 address
-= FLASH_BANK_SIZE
;
327 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK1_BASE
+ FLASH_BANK_SIZE
) {
328 *bank
= FLASH_BANK_1
;
334 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
338 address
-= FLASH_BANK1_BASE
;
339 *sector
= address
/ FLASH_PAGE_SIZE
;
341 #elif defined(STM32H750xx)
343 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
344 space by using a smaller function.
347 Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes
351 static void getFLASHSectorForEEPROM(uint32_t *bank
, uint32_t *sector
)
354 uint32_t start
= (uint32_t)&__config_start
;
356 if (start
== FLASH_BANK1_BASE
) {
357 *sector
= FLASH_SECTOR_0
;
358 *bank
= FLASH_BANK_1
;
362 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
367 #endif // CONFIG_IN_FLASH
369 // FIXME the return values are currently magic numbers
370 static int write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
375 #if defined(CONFIG_IN_EXTERNAL_FLASH)
377 uint32_t dataOffset
= (uint32_t)(c
->address
- (uintptr_t)&eepromData
[0]);
379 const flashPartition_t
*flashPartition
= flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG
);
380 const flashGeometry_t
*flashGeometry
= flashGetGeometry();
382 uint32_t flashStartAddress
= flashPartition
->startSector
* flashGeometry
->sectorSize
;
383 uint32_t flashOverflowAddress
= ((flashPartition
->endSector
+ 1) * flashGeometry
->sectorSize
); // +1 to sector for inclusive
385 uint32_t flashAddress
= flashStartAddress
+ dataOffset
;
386 if (flashAddress
+ CONFIG_STREAMER_BUFFER_SIZE
> flashOverflowAddress
) {
387 return -3; // address is past end of partition
390 uint32_t flashSectorSize
= flashGeometry
->sectorSize
;
391 uint32_t flashPageSize
= flashGeometry
->pageSize
;
392 const uint8_t *buffers
[1];
393 uint32_t bufferSizes
[1];
395 bool onPageBoundary
= (flashAddress
% flashPageSize
== 0);
396 if (onPageBoundary
) {
398 bool firstPage
= (flashAddress
== flashStartAddress
);
400 flashPageProgramFinish();
403 if (flashAddress
% flashSectorSize
== 0) {
404 flashEraseSector(flashAddress
);
407 flashPageProgramBegin(flashAddress
, NULL
);
410 buffers
[0] = (uint8_t *)buffer
;
411 bufferSizes
[0] = CONFIG_STREAMER_BUFFER_SIZE
;
413 flashPageProgramContinue(buffers
, bufferSizes
, 1);
415 #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD)
416 if (c
->address
== (uintptr_t)&eepromData
[0]) {
417 memset(eepromData
, 0, sizeof(eepromData
));
420 uint64_t *dest_addr
= (uint64_t *)c
->address
;
421 uint64_t *src_addr
= (uint64_t*)buffer
;
422 uint8_t row_index
= 4;
423 /* copy the 256 bits flash word */
426 *dest_addr
++ = *src_addr
++;
427 } while (--row_index
!= 0);
429 #elif defined(CONFIG_IN_FILE)
431 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
432 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
433 if (status
!= FLASH_COMPLETE
) {
437 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
438 if (status
!= FLASH_COMPLETE
) {
442 #elif defined(CONFIG_IN_FLASH)
445 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
446 FLASH_EraseInitTypeDef EraseInitStruct
= {
447 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
448 #if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ))
449 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
453 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Sector
);
454 uint32_t SECTORError
;
455 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
456 if (status
!= HAL_OK
) {
462 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress);
463 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD
, c
->address
, (uint64_t)(uint32_t)buffer
);
464 if (status
!= HAL_OK
) {
467 #elif defined(STM32F7)
468 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
469 FLASH_EraseInitTypeDef EraseInitStruct
= {
470 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
471 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
474 EraseInitStruct
.Sector
= getFLASHSectorForEEPROM();
475 uint32_t SECTORError
;
476 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
477 if (status
!= HAL_OK
) {
483 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
484 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD
, c
->address
, (uint64_t)*buffer
);
485 if (status
!= HAL_OK
) {
488 #elif defined(STM32G4)
489 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
491 FLASH_EraseInitTypeDef EraseInitStruct
= {
492 .TypeErase
= FLASH_TYPEERASE_PAGES
,
495 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Page
);
496 uint32_t SECTORError
;
497 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
498 if (status
!= HAL_OK
) {
503 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD
, c
->address
, (uint64_t)*buffer
);
504 if (status
!= HAL_OK
) {
507 #else // !STM32H7 && !STM32F7 && !STM32G4
508 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
510 const FLASH_Status status
= FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3
); //0x08080000 to 0x080A0000
511 #else // STM32F3, STM32F1
512 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
514 if (status
!= FLASH_COMPLETE
) {
518 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
519 if (status
!= FLASH_COMPLETE
) {
524 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;
528 int config_streamer_write(config_streamer_t
*c
, const uint8_t *p
, uint32_t size
)
530 for (const uint8_t *pat
= p
; pat
!= (uint8_t*)p
+ size
; pat
++) {
531 c
->buffer
.b
[c
->at
++] = *pat
;
533 if (c
->at
== sizeof(c
->buffer
)) {
534 c
->err
= write_word(c
, &c
->buffer
.w
);
541 int config_streamer_status(config_streamer_t
*c
)
546 int config_streamer_flush(config_streamer_t
*c
)
549 memset(c
->buffer
.b
+ c
->at
, 0, sizeof(c
->buffer
) - c
->at
);
550 c
->err
= write_word(c
, &c
->buffer
.w
);
556 int config_streamer_finish(config_streamer_t
*c
)
559 #if defined(CONFIG_IN_SDCARD)
560 bool saveEEPROMToSDCard(void); // XXX forward declaration to avoid circular dependency between config_streamer / config_eeprom
561 saveEEPROMToSDCard();
562 // TODO overwrite the data in the file on the SD card.
563 #elif defined(CONFIG_IN_EXTERNAL_FLASH)
565 #elif defined(CONFIG_IN_RAM)
567 #elif defined(CONFIG_IN_FILE)
569 #elif defined(CONFIG_IN_FLASH)
570 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)