Merge pull request #11494 from haslinghuis/dshot_gpio
[betaflight.git] / src / main / config / config_streamer.c
blob682aa9fb196dd705b1259f32d0010a4429c14548
1 /*
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)
8 * any later version.
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/>.
21 #include <string.h>
23 #include "platform.h"
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];
33 #else
34 uint8_t eepromData[EEPROM_SIZE];
35 #endif
36 #endif
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"
41 #endif
42 // @todo this is not strictly correct for F4/F7, where sector sizes are variable
43 #if !defined(FLASH_PAGE_SIZE)
44 // F1
45 # if defined(STM32F10X_MD)
46 # define FLASH_PAGE_SIZE (0x400)
47 # elif defined(STM32F10X_HD)
48 # define FLASH_PAGE_SIZE (0x800)
49 // F3
50 # elif defined(STM32F303xC)
51 # define FLASH_PAGE_SIZE (0x800)
52 // F4
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)
61 // F7
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)
72 // H7
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
77 // G4
78 # elif defined(STM32G4)
79 # define FLASH_PAGE_SIZE ((uint32_t)0x800) // 2K page
80 // SIMULATOR
81 # elif defined(SIMULATOR_BUILD)
82 # define FLASH_PAGE_SIZE (0x400)
83 # else
84 # error "Flash page size not defined for target."
85 # endif
86 #endif
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.
96 c->address = base;
97 c->size = size;
98 if (!c->unlocked) {
99 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
100 // NOP
101 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
102 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
103 HAL_FLASH_Unlock();
104 #else
105 FLASH_Unlock();
106 #endif
107 #endif
108 c->unlocked = true;
111 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
112 // NOP
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)
121 // NOP
122 #elif defined(STM32H7)
123 // NOP
124 #elif defined(STM32G4)
125 // NOP
126 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
127 // NOP
128 #else
129 # error "Unsupported CPU"
130 #endif
131 #endif
132 c->err = 0;
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;
183 #endif
185 // Not good
186 while (1) {
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;
222 // Not good
223 while (1) {
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;
271 // Not good
272 while (1) {
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.)
282 H743
283 2 bank * 8 sector/bank * 128K/sector (2MB)
284 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
285 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
287 H743
288 1 bank * 8 sector/bank * 128K/sector (1MB)
289 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
291 H7A3
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)
315 #endif
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;
326 #else
327 if (address >= FLASH_BANK1_BASE && address < FLASH_BANK1_BASE + FLASH_BANK_SIZE) {
328 *bank = FLASH_BANK_1;
330 #endif
331 else {
332 // Not good
333 while (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.
346 Bank 1
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;
359 } else {
360 // Not good
361 while (1) {
362 failureMode(FAILURE_CONFIG_STORE_FAILURE);
366 #endif
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)
372 if (c->err != 0) {
373 return c->err;
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);
399 if (!firstPage) {
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) {
434 return -1;
437 const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
438 if (status != FLASH_COMPLETE) {
439 return -2;
442 #elif defined(CONFIG_IN_FLASH)
444 #if defined(STM32H7)
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
450 #endif
451 .NbSectors = 1
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) {
457 return -1;
461 // For H7
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) {
465 return -2;
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
472 .NbSectors = 1
474 EraseInitStruct.Sector = getFLASHSectorForEEPROM();
475 uint32_t SECTORError;
476 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
477 if (status != HAL_OK) {
478 return -1;
482 // For F7
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) {
486 return -2;
488 #elif defined(STM32G4)
489 if (c->address % FLASH_PAGE_SIZE == 0) {
491 FLASH_EraseInitTypeDef EraseInitStruct = {
492 .TypeErase = FLASH_TYPEERASE_PAGES,
493 .NbPages = 1
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) {
499 return -1;
503 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, c->address, (uint64_t)*buffer);
504 if (status != HAL_OK) {
505 return -2;
507 #else // !STM32H7 && !STM32F7 && !STM32G4
508 if (c->address % FLASH_PAGE_SIZE == 0) {
509 #if defined(STM32F4)
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);
513 #endif
514 if (status != FLASH_COMPLETE) {
515 return -1;
518 const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
519 if (status != FLASH_COMPLETE) {
520 return -2;
522 #endif
523 #endif
524 c->address += CONFIG_STREAMER_BUFFER_SIZE;
525 return 0;
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);
535 c->at = 0;
538 return c->err;
541 int config_streamer_status(config_streamer_t *c)
543 return c->err;
546 int config_streamer_flush(config_streamer_t *c)
548 if (c->at != 0) {
549 memset(c->buffer.b + c->at, 0, sizeof(c->buffer) - c->at);
550 c->err = write_word(c, &c->buffer.w);
551 c->at = 0;
553 return c-> err;
556 int config_streamer_finish(config_streamer_t *c)
558 if (c->unlocked) {
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)
564 flashFlush();
565 #elif defined(CONFIG_IN_RAM)
566 // NOP
567 #elif defined(CONFIG_IN_FILE)
568 FLASH_Lock();
569 #elif defined(CONFIG_IN_FLASH)
570 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
571 HAL_FLASH_Lock();
572 #else
573 FLASH_Lock();
574 #endif
575 #endif
576 c->unlocked = false;
578 return c->err;