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(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD))
40 #error "STM32750xx 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)
74 # define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
76 # elif defined(STM32G4)
77 # define FLASH_PAGE_SIZE ((uint32_t)0x800) // 2K page
79 # elif defined(SIMULATOR_BUILD)
80 # define FLASH_PAGE_SIZE (0x400)
82 # error "Flash page size not defined for target."
86 void config_streamer_init(config_streamer_t
*c
)
88 memset(c
, 0, sizeof(*c
));
91 void config_streamer_start(config_streamer_t
*c
, uintptr_t base
, int size
)
93 // base must start at FLASH_PAGE_SIZE boundary when using embedded flash.
97 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
99 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
100 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
109 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
111 #elif defined(CONFIG_IN_FLASH)
112 #if defined(STM32F10X)
113 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPRTERR
);
114 #elif defined(STM32F303)
115 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_PGERR
| FLASH_FLAG_WRPERR
);
116 #elif defined(STM32F4)
117 FLASH_ClearFlag(FLASH_FLAG_EOP
| FLASH_FLAG_OPERR
| FLASH_FLAG_WRPERR
| FLASH_FLAG_PGAERR
| FLASH_FLAG_PGPERR
| FLASH_FLAG_PGSERR
);
118 #elif defined(STM32F7)
120 #elif defined(STM32H7)
122 #elif defined(STM32G4)
124 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
127 # error "Unsupported CPU"
133 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
134 // No flash sector method required.
135 #elif defined(CONFIG_IN_FLASH)
136 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
138 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
139 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
140 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
141 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
142 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
143 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
144 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
145 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
147 F7X5XI device with 2M flash
148 Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes
149 Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes
150 Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes
151 Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes
154 static uint32_t getFLASHSectorForEEPROM(void)
156 if ((uint32_t)&__config_start
<= 0x08007FFF)
157 return FLASH_SECTOR_0
;
158 if ((uint32_t)&__config_start
<= 0x0800FFFF)
159 return FLASH_SECTOR_1
;
160 if ((uint32_t)&__config_start
<= 0x08017FFF)
161 return FLASH_SECTOR_2
;
162 if ((uint32_t)&__config_start
<= 0x0801FFFF)
163 return FLASH_SECTOR_3
;
164 if ((uint32_t)&__config_start
<= 0x0803FFFF)
165 return FLASH_SECTOR_4
;
166 if ((uint32_t)&__config_start
<= 0x0807FFFF)
167 return FLASH_SECTOR_5
;
168 if ((uint32_t)&__config_start
<= 0x080BFFFF)
169 return FLASH_SECTOR_6
;
170 if ((uint32_t)&__config_start
<= 0x080FFFFF)
171 return FLASH_SECTOR_7
;
172 #if defined(STM32F765xx)
173 if ((uint32_t)&__config_start
<= 0x0813FFFF)
174 return FLASH_SECTOR_8
;
175 if ((uint32_t)&__config_start
<= 0x0817FFFF)
176 return FLASH_SECTOR_9
;
177 if ((uint32_t)&__config_start
<= 0x081BFFFF)
178 return FLASH_SECTOR_10
;
179 if ((uint32_t)&__config_start
<= 0x081FFFFF)
180 return FLASH_SECTOR_11
;
185 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
189 #elif defined(STM32F722xx)
191 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
192 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
193 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
194 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
195 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
196 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
197 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
198 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
201 static uint32_t getFLASHSectorForEEPROM(void)
203 if ((uint32_t)&__config_start
<= 0x08003FFF)
204 return FLASH_SECTOR_0
;
205 if ((uint32_t)&__config_start
<= 0x08007FFF)
206 return FLASH_SECTOR_1
;
207 if ((uint32_t)&__config_start
<= 0x0800BFFF)
208 return FLASH_SECTOR_2
;
209 if ((uint32_t)&__config_start
<= 0x0800FFFF)
210 return FLASH_SECTOR_3
;
211 if ((uint32_t)&__config_start
<= 0x0801FFFF)
212 return FLASH_SECTOR_4
;
213 if ((uint32_t)&__config_start
<= 0x0803FFFF)
214 return FLASH_SECTOR_5
;
215 if ((uint32_t)&__config_start
<= 0x0805FFFF)
216 return FLASH_SECTOR_6
;
217 if ((uint32_t)&__config_start
<= 0x0807FFFF)
218 return FLASH_SECTOR_7
;
222 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
226 #elif defined(STM32F4)
228 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
229 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
230 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
231 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
232 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
233 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
234 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
235 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
236 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
237 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
238 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
239 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
242 static uint32_t getFLASHSectorForEEPROM(void)
244 if ((uint32_t)&__config_start
<= 0x08003FFF)
245 return FLASH_Sector_0
;
246 if ((uint32_t)&__config_start
<= 0x08007FFF)
247 return FLASH_Sector_1
;
248 if ((uint32_t)&__config_start
<= 0x0800BFFF)
249 return FLASH_Sector_2
;
250 if ((uint32_t)&__config_start
<= 0x0800FFFF)
251 return FLASH_Sector_3
;
252 if ((uint32_t)&__config_start
<= 0x0801FFFF)
253 return FLASH_Sector_4
;
254 if ((uint32_t)&__config_start
<= 0x0803FFFF)
255 return FLASH_Sector_5
;
256 if ((uint32_t)&__config_start
<= 0x0805FFFF)
257 return FLASH_Sector_6
;
258 if ((uint32_t)&__config_start
<= 0x0807FFFF)
259 return FLASH_Sector_7
;
260 if ((uint32_t)&__config_start
<= 0x0809FFFF)
261 return FLASH_Sector_8
;
262 if ((uint32_t)&__config_start
<= 0x080DFFFF)
263 return FLASH_Sector_9
;
264 if ((uint32_t)&__config_start
<= 0x080BFFFF)
265 return FLASH_Sector_10
;
266 if ((uint32_t)&__config_start
<= 0x080FFFFF)
267 return FLASH_Sector_11
;
271 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
275 #elif defined(STM32H743xx) || defined(STM32G4)
277 MCUs with uniform array of equal size sectors, handled in two banks having contiguous address.
278 (Devices with non-contiguous flash layout is not currently useful anyways.)
281 2 bank * 8 sector/bank * 128K/sector (2MB)
282 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
283 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
285 G473/474 in dual bank mode
286 2 bank * 128 sector/bank * 2KB/sector (512KB)
287 Bank 1 0x08000000 - 0x0803FFFF 2KB * 128
288 Bank 2 0x08040000 - 0x0807FFFF 2KB * 128
290 Note that FLASH_BANK_SIZE constant used in the following code changes depending on
291 bank operation mode. The code assumes dual bank operation, in which case the
292 FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL.
295 #if defined(STM32H743xx)
296 #define FLASH_PAGE_PER_BANK 8
297 #elif defined(STM32G4)
298 #define FLASH_PAGE_PER_BANK 128
299 // These are not defined in CMSIS like H7
300 #define FLASH_BANK1_BASE FLASH_BASE
301 #define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE)
304 static void getFLASHSectorForEEPROM(uint32_t address
, uint32_t *bank
, uint32_t *sector
)
306 if (address
>= FLASH_BANK1_BASE
&& address
< FLASH_BANK2_BASE
) {
307 *bank
= FLASH_BANK_1
;
308 } else if (address
>= FLASH_BANK2_BASE
&& address
< FLASH_BANK2_BASE
+ FLASH_BANK_SIZE
) {
309 *bank
= FLASH_BANK_2
;
310 address
-= FLASH_BANK_SIZE
;
314 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
318 address
-= FLASH_BANK1_BASE
;
319 *sector
= address
/ FLASH_PAGE_SIZE
;
321 #elif defined(STM32H750xx)
323 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
324 space by using a smaller function.
327 Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes
331 static void getFLASHSectorForEEPROM(uint32_t *bank
, uint32_t *sector
)
334 uint32_t start
= (uint32_t)&__config_start
;
336 if (start
== FLASH_BANK1_BASE
) {
337 *sector
= FLASH_SECTOR_0
;
338 *bank
= FLASH_BANK_1
;
342 failureMode(FAILURE_CONFIG_STORE_FAILURE
);
347 #endif // CONFIG_IN_FLASH
349 // FIXME the return values are currently magic numbers
350 static int write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
355 #if defined(CONFIG_IN_EXTERNAL_FLASH)
357 uint32_t dataOffset
= (uint32_t)(c
->address
- (uintptr_t)&eepromData
[0]);
359 const flashPartition_t
*flashPartition
= flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG
);
360 const flashGeometry_t
*flashGeometry
= flashGetGeometry();
362 uint32_t flashStartAddress
= flashPartition
->startSector
* flashGeometry
->sectorSize
;
363 uint32_t flashOverflowAddress
= ((flashPartition
->endSector
+ 1) * flashGeometry
->sectorSize
); // +1 to sector for inclusive
365 uint32_t flashAddress
= flashStartAddress
+ dataOffset
;
366 if (flashAddress
+ CONFIG_STREAMER_BUFFER_SIZE
> flashOverflowAddress
) {
367 return -3; // address is past end of partition
370 uint32_t flashSectorSize
= flashGeometry
->sectorSize
;
371 uint32_t flashPageSize
= flashGeometry
->pageSize
;
373 bool onPageBoundary
= (flashAddress
% flashPageSize
== 0);
374 if (onPageBoundary
) {
376 bool firstPage
= (flashAddress
== flashStartAddress
);
378 flashPageProgramFinish();
381 if (flashAddress
% flashSectorSize
== 0) {
382 flashEraseSector(flashAddress
);
385 flashPageProgramBegin(flashAddress
);
388 flashPageProgramContinue((uint8_t *)buffer
, CONFIG_STREAMER_BUFFER_SIZE
);
390 #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD)
391 if (c
->address
== (uintptr_t)&eepromData
[0]) {
392 memset(eepromData
, 0, sizeof(eepromData
));
395 uint64_t *dest_addr
= (uint64_t *)c
->address
;
396 uint64_t *src_addr
= (uint64_t*)buffer
;
397 uint8_t row_index
= 4;
398 /* copy the 256 bits flash word */
401 *dest_addr
++ = *src_addr
++;
402 } while (--row_index
!= 0);
404 #elif defined(CONFIG_IN_FILE)
406 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
407 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
408 if (status
!= FLASH_COMPLETE
) {
412 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
413 if (status
!= FLASH_COMPLETE
) {
417 #elif defined(CONFIG_IN_FLASH)
420 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
421 FLASH_EraseInitTypeDef EraseInitStruct
= {
422 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
423 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
426 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Sector
);
427 uint32_t SECTORError
;
428 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
429 if (status
!= HAL_OK
) {
435 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress);
436 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD
, c
->address
, (uint64_t)(uint32_t)buffer
);
437 if (status
!= HAL_OK
) {
440 #elif defined(STM32F7)
441 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
442 FLASH_EraseInitTypeDef EraseInitStruct
= {
443 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
444 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
447 EraseInitStruct
.Sector
= getFLASHSectorForEEPROM();
448 uint32_t SECTORError
;
449 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
450 if (status
!= HAL_OK
) {
456 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
457 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD
, c
->address
, (uint64_t)*buffer
);
458 if (status
!= HAL_OK
) {
461 #elif defined(STM32G4)
462 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
464 FLASH_EraseInitTypeDef EraseInitStruct
= {
465 .TypeErase
= FLASH_TYPEERASE_PAGES
,
468 getFLASHSectorForEEPROM(c
->address
, &EraseInitStruct
.Banks
, &EraseInitStruct
.Page
);
469 uint32_t SECTORError
;
470 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
471 if (status
!= HAL_OK
) {
476 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD
, c
->address
, (uint64_t)*buffer
);
477 if (status
!= HAL_OK
) {
480 #else // !STM32H7 && !STM32F7 && !STM32G4
481 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
483 const FLASH_Status status
= FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3
); //0x08080000 to 0x080A0000
484 #else // STM32F3, STM32F1
485 const FLASH_Status status
= FLASH_ErasePage(c
->address
);
487 if (status
!= FLASH_COMPLETE
) {
491 const FLASH_Status status
= FLASH_ProgramWord(c
->address
, *buffer
);
492 if (status
!= FLASH_COMPLETE
) {
497 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;
501 int config_streamer_write(config_streamer_t
*c
, const uint8_t *p
, uint32_t size
)
503 for (const uint8_t *pat
= p
; pat
!= (uint8_t*)p
+ size
; pat
++) {
504 c
->buffer
.b
[c
->at
++] = *pat
;
506 if (c
->at
== sizeof(c
->buffer
)) {
507 c
->err
= write_word(c
, &c
->buffer
.w
);
514 int config_streamer_status(config_streamer_t
*c
)
519 int config_streamer_flush(config_streamer_t
*c
)
522 memset(c
->buffer
.b
+ c
->at
, 0, sizeof(c
->buffer
) - c
->at
);
523 c
->err
= write_word(c
, &c
->buffer
.w
);
529 int config_streamer_finish(config_streamer_t
*c
)
532 #if defined(CONFIG_IN_SDCARD)
533 bool saveEEPROMToSDCard(void); // XXX forward declaration to avoid circular dependency between config_streamer / config_eeprom
534 saveEEPROMToSDCard();
535 // TODO overwrite the data in the file on the SD card.
536 #elif defined(CONFIG_IN_EXTERNAL_FLASH)
538 #elif defined(CONFIG_IN_RAM)
540 #elif defined(CONFIG_IN_FILE)
542 #elif defined(CONFIG_IN_FLASH)
543 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)