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_MEMORY_MAPPED_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(STM32F40_41xxx)
46 # define FLASH_PAGE_SIZE ((uint32_t)0x4000) // 16K sectors
47 # elif defined (STM32F411xE)
48 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
49 # elif defined(STM32F427_437xx)
50 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
51 # elif defined (STM32F446xx)
52 # define FLASH_PAGE_SIZE ((uint32_t)0x4000)
54 #elif defined(STM32F722xx)
55 # define FLASH_PAGE_SIZE ((uint32_t)0x4000) // 16K sectors
56 # elif defined(STM32F745xx)
57 # define FLASH_PAGE_SIZE ((uint32_t)0x8000) // 32K sectors
58 # elif defined(STM32F746xx)
59 # define FLASH_PAGE_SIZE ((uint32_t)0x8000)
60 # elif defined(STM32F765xx)
61 # define FLASH_PAGE_SIZE ((uint32_t)0x8000)
62 # elif defined(UNIT_TEST)
63 # define FLASH_PAGE_SIZE (0x400)
65 # elif defined(STM32H743xx) || defined(STM32H750xx) || defined(STM32H723xx) || defined(STM32H725xx) || defined(STM32H730xx)
66 # define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
67 # elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
68 # define FLASH_PAGE_SIZE ((uint32_t)0x2000) // 8K sectors
70 # elif defined(STM32G4)
71 # define FLASH_PAGE_SIZE ((uint32_t)0x800) // 2K page
73 # elif defined(SIMULATOR_BUILD)
74 # define FLASH_PAGE_SIZE (0x400)
76 # error "Flash page size not defined for target."
80 void config_streamer_init(config_streamer_t
*c
)
82 memset(c
, 0, sizeof(*c
));
85 void config_streamer_start(config_streamer_t
*c
, uintptr_t base
, int size
)
87 // base must start at FLASH_PAGE_SIZE boundary when using embedded flash.
91 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
93 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
94 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
103 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
105 #elif defined(CONFIG_IN_FLASH)
107 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
108 #elif defined(STM32F7)
110 #elif defined(STM32H7)
112 #elif defined(STM32G4)
114 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
117 # error "Unsupported CPU"
123 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
124 // No flash sector method required.
125 #elif defined(CONFIG_IN_FLASH)
126 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
128 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
129 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
130 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
131 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
132 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
133 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
134 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
135 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
137 F7X5XI device with 2M flash
138 Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes
139 Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes
140 Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes
141 Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes
144 static uint32_t getFLASHSectorForEEPROM(void)
146 if ((uint32_t)&__config_start
<= 0x08007FFF)
147 return FLASH_SECTOR_0
;
148 if ((uint32_t)&__config_start
<= 0x0800FFFF)
149 return FLASH_SECTOR_1
;
150 if ((uint32_t)&__config_start
<= 0x08017FFF)
151 return FLASH_SECTOR_2
;
152 if ((uint32_t)&__config_start
<= 0x0801FFFF)
153 return FLASH_SECTOR_3
;
154 if ((uint32_t)&__config_start
<= 0x0803FFFF)
155 return FLASH_SECTOR_4
;
156 if ((uint32_t)&__config_start
<= 0x0807FFFF)
157 return FLASH_SECTOR_5
;
158 if ((uint32_t)&__config_start
<= 0x080BFFFF)
159 return FLASH_SECTOR_6
;
160 if ((uint32_t)&__config_start
<= 0x080FFFFF)
161 return FLASH_SECTOR_7
;
162 #if defined(STM32F765xx)
163 if ((uint32_t)&__config_start
<= 0x0813FFFF)
164 return FLASH_SECTOR_8
;
165 if ((uint32_t)&__config_start
<= 0x0817FFFF)
166 return FLASH_SECTOR_9
;
167 if ((uint32_t)&__config_start
<= 0x081BFFFF)
168 return FLASH_SECTOR_10
;
169 if ((uint32_t)&__config_start
<= 0x081FFFFF)
170 return FLASH_SECTOR_11
;
175 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
179 #elif defined(STM32F722xx)
181 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
182 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
183 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
184 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
185 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
186 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
187 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
188 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
191 static uint32_t getFLASHSectorForEEPROM(void)
193 if ((uint32_t)&__config_start
<= 0x08003FFF)
194 return FLASH_SECTOR_0
;
195 if ((uint32_t)&__config_start
<= 0x08007FFF)
196 return FLASH_SECTOR_1
;
197 if ((uint32_t)&__config_start
<= 0x0800BFFF)
198 return FLASH_SECTOR_2
;
199 if ((uint32_t)&__config_start
<= 0x0800FFFF)
200 return FLASH_SECTOR_3
;
201 if ((uint32_t)&__config_start
<= 0x0801FFFF)
202 return FLASH_SECTOR_4
;
203 if ((uint32_t)&__config_start
<= 0x0803FFFF)
204 return FLASH_SECTOR_5
;
205 if ((uint32_t)&__config_start
<= 0x0805FFFF)
206 return FLASH_SECTOR_6
;
207 if ((uint32_t)&__config_start
<= 0x0807FFFF)
208 return FLASH_SECTOR_7
;
212 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
216 #elif defined(STM32F4)
218 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
219 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
220 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
221 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
222 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
223 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
224 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
225 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
226 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
227 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
228 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
229 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
232 static uint32_t getFLASHSectorForEEPROM(void)
234 if ((uint32_t)&__config_start
<= 0x08003FFF)
235 return FLASH_Sector_0
;
236 if ((uint32_t)&__config_start
<= 0x08007FFF)
237 return FLASH_Sector_1
;
238 if ((uint32_t)&__config_start
<= 0x0800BFFF)
239 return FLASH_Sector_2
;
240 if ((uint32_t)&__config_start
<= 0x0800FFFF)
241 return FLASH_Sector_3
;
242 if ((uint32_t)&__config_start
<= 0x0801FFFF)
243 return FLASH_Sector_4
;
244 if ((uint32_t)&__config_start
<= 0x0803FFFF)
245 return FLASH_Sector_5
;
246 if ((uint32_t)&__config_start
<= 0x0805FFFF)
247 return FLASH_Sector_6
;
248 if ((uint32_t)&__config_start
<= 0x0807FFFF)
249 return FLASH_Sector_7
;
250 if ((uint32_t)&__config_start
<= 0x0809FFFF)
251 return FLASH_Sector_8
;
252 if ((uint32_t)&__config_start
<= 0x080DFFFF)
253 return FLASH_Sector_9
;
254 if ((uint32_t)&__config_start
<= 0x080BFFFF)
255 return FLASH_Sector_10
;
256 if ((uint32_t)&__config_start
<= 0x080FFFFF)
257 return FLASH_Sector_11
;
261 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
265 #elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx)
267 MCUs with uniform array of equal size sectors, handled in two banks having contiguous address.
268 (Devices with non-contiguous flash layout is not currently useful anyways.)
271 2 bank * 8 sector/bank * 128K/sector (2MB)
272 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
273 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
276 1 bank * 8 sector/bank * 128K/sector (1MB)
277 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
280 2 bank * 128 sector/bank * 8KB/sector (2MB)
281 Bank 1 0x08000000 - 0x080FFFFF 8KB * 128
282 Bank 2 0x08100000 - 0x081FFFFF 8KB * 128
284 G473/474 in dual bank mode
285 2 bank * 128 sector/bank * 2KB/sector (512KB)
286 Bank 1 0x08000000 - 0x0803FFFF 2KB * 128
287 Bank 2 0x08040000 - 0x0807FFFF 2KB * 128
289 Note that FLASH_BANK_SIZE constant used in the following code changes depending on
290 bank operation mode. The code assumes dual bank operation, in which case the
291 FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL.
294 #if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx)
295 #define FLASH_PAGE_PER_BANK 8
296 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
297 #define FLASH_PAGE_PER_BANK 128
298 #elif defined(STM32G4)
299 #define FLASH_PAGE_PER_BANK 128
300 // These are not defined in CMSIS like H7
301 #define FLASH_BANK1_BASE FLASH_BASE
302 #define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE)
305 static void getFLASHSectorForEEPROM(uint32_t address
, uint32_t *bank
, uint32_t *sector
)
307 #if defined(FLASH_BANK2_BASE)
308 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK2_BASE
) {
309 *bank
= FLASH_BANK_1
;
310 } else if (address
>= FLASH_BANK2_BASE
&& address
< FLASH_BANK2_BASE
+ FLASH_BANK_SIZE
) {
311 *bank
= FLASH_BANK_2
;
312 address
-= FLASH_BANK_SIZE
;
315 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK1_BASE
+ FLASH_BANK_SIZE
) {
316 *bank
= FLASH_BANK_1
;
322 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
326 address
-= FLASH_BANK1_BASE
;
327 *sector
= address
/ FLASH_PAGE_SIZE
;
329 #elif defined(STM32H750xx)
331 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
332 space by using a smaller function.
335 Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes
339 static void getFLASHSectorForEEPROM(uint32_t *bank
, uint32_t *sector
)
342 uint32_t start
= (uint32_t)&__config_start
;
344 if (start
== FLASH_BANK1_BASE
) {
345 *sector
= FLASH_SECTOR_0
;
346 *bank
= FLASH_BANK_1
;
350 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
355 #endif // CONFIG_IN_FLASH
357 // FIXME the return values are currently magic numbers
358 static int write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
363 #if defined(CONFIG_IN_EXTERNAL_FLASH)
365 uint32_t dataOffset
= (uint32_t)(c
->address
- (uintptr_t)&eepromData
[0]);
367 const flashPartition_t
*flashPartition
= flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG
);
368 const flashGeometry_t
*flashGeometry
= flashGetGeometry();
370 uint32_t flashStartAddress
= flashPartition
->startSector
* flashGeometry
->sectorSize
;
371 uint32_t flashOverflowAddress
= ((flashPartition
->endSector
+ 1) * flashGeometry
->sectorSize
); // +1 to sector for inclusive
373 uint32_t flashAddress
= flashStartAddress
+ dataOffset
;
374 if (flashAddress
+ CONFIG_STREAMER_BUFFER_SIZE
> flashOverflowAddress
) {
375 return -3; // address is past end of partition
378 uint32_t flashSectorSize
= flashGeometry
->sectorSize
;
379 uint32_t flashPageSize
= flashGeometry
->pageSize
;
380 const uint8_t *buffers
[1];
381 uint32_t bufferSizes
[1];
383 bool onPageBoundary
= (flashAddress
% flashPageSize
== 0);
384 if (onPageBoundary
) {
386 bool firstPage
= (flashAddress
== flashStartAddress
);
388 flashPageProgramFinish();
391 if (flashAddress
% flashSectorSize
== 0) {
392 flashEraseSector(flashAddress
);
395 flashPageProgramBegin(flashAddress
, NULL
);
398 buffers
[0] = (uint8_t *)buffer
;
399 bufferSizes
[0] = CONFIG_STREAMER_BUFFER_SIZE
;
401 flashPageProgramContinue(buffers
, bufferSizes
, 1);
403 #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
404 if (c
->address
== (uintptr_t)&eepromData
[0]) {
405 memset(eepromData
, 0, sizeof(eepromData
));
408 uint64_t *dest_addr
= (uint64_t *)c
->address
;
409 uint64_t *src_addr
= (uint64_t*)buffer
;
410 uint8_t row_index
= 4;
411 /* copy the 256 bits flash word */
414 *dest_addr
++ = *src_addr
++;
415 } while (--row_index
!= 0);
417 #elif defined(CONFIG_IN_FILE)
419 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
420 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
421 if (status
!= FLASH_COMPLETE
) {
425 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
426 if (status
!= FLASH_COMPLETE
) {
430 #elif defined(CONFIG_IN_FLASH)
433 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
434 FLASH_EraseInitTypeDef EraseInitStruct
= {
435 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
436 #if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ))
437 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
441 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Sector
);
442 uint32_t SECTORError
;
443 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
444 if (status
!= HAL_OK
) {
450 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress);
451 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD
, c
->address
, (uint64_t)(uint32_t)buffer
);
452 if (status
!= HAL_OK
) {
455 #elif defined(STM32F7)
456 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
457 FLASH_EraseInitTypeDef EraseInitStruct
= {
458 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
459 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
462 EraseInitStruct
.Sector
= getFLASHSectorForEEPROM();
463 uint32_t SECTORError
;
464 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
465 if (status
!= HAL_OK
) {
471 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
472 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD
, c
->address
, (uint64_t)*buffer
);
473 if (status
!= HAL_OK
) {
476 #elif defined(STM32G4)
477 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
479 FLASH_EraseInitTypeDef EraseInitStruct
= {
480 .TypeErase
= FLASH_TYPEERASE_PAGES
,
483 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Page
);
484 uint32_t SECTORError
;
485 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
486 if (status
!= HAL_OK
) {
491 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD
, c
->address
, (uint64_t)*buffer
);
492 if (status
!= HAL_OK
) {
495 #else // !STM32H7 && !STM32F7 && !STM32G4
496 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
497 const FLASH_Status status
= FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3
); //0x08080000 to 0x080A0000
498 if (status
!= FLASH_COMPLETE
) {
502 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
503 if (status
!= FLASH_COMPLETE
) {
508 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;
512 int config_streamer_write(config_streamer_t
*c
, const uint8_t *p
, uint32_t size
)
514 for (const uint8_t *pat
= p
; pat
!= (uint8_t*)p
+ size
; pat
++) {
515 c
->buffer
.b
[c
->at
++] = *pat
;
517 if (c
->at
== sizeof(c
->buffer
)) {
518 c
->err
= write_word(c
, &c
->buffer
.w
);
525 int config_streamer_status(config_streamer_t
*c
)
530 int config_streamer_flush(config_streamer_t
*c
)
533 memset(c
->buffer
.b
+ c
->at
, 0, sizeof(c
->buffer
) - c
->at
);
534 c
->err
= write_word(c
, &c
->buffer
.w
);
540 int config_streamer_finish(config_streamer_t
*c
)
543 #if defined(CONFIG_IN_SDCARD)
544 bool saveEEPROMToSDCard(void); // forward declaration to avoid circular dependency between config_streamer / config_eeprom
545 saveEEPROMToSDCard();
546 #elif defined(CONFIG_IN_EXTERNAL_FLASH)
548 #elif defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
549 void saveEEPROMToMemoryMappedFlash(void); // forward declaration to avoid circular dependency between config_streamer / config_eeprom
550 saveEEPROMToMemoryMappedFlash();
551 #elif defined(CONFIG_IN_RAM)
553 #elif defined(CONFIG_IN_FILE)
555 #elif defined(CONFIG_IN_FLASH)
556 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)