Update cloud build defines (#14080)
[betaflight.git] / src / main / config / config_streamer.c
blobfc5ab25696162287d626f5be6ecc7cbeda68e68f
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/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];
34 #else
35 uint8_t eepromData[EEPROM_SIZE];
36 #endif
37 #endif
39 #if !defined(FLASH_PAGE_SIZE)
40 #error "Flash page size not defined for target."
41 #endif
43 void config_streamer_init(config_streamer_t *c)
45 memset(c, 0, sizeof(*c));
48 void config_streamer_start(config_streamer_t *c, uintptr_t base, int size)
50 // base must start at FLASH_PAGE_SIZE boundary when using embedded flash.
51 c->address = base;
52 c->size = size;
53 if (!c->unlocked) {
54 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
55 // NOP
56 #elif defined(CONFIG_IN_FLASH) || defined(CONFIG_IN_FILE)
57 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
58 HAL_FLASH_Unlock();
59 #elif defined(APM32F4)
60 DAL_FLASH_Unlock();
61 #elif defined(AT32F4)
62 flash_unlock();
63 #else
64 FLASH_Unlock();
65 #endif
66 #endif
67 c->unlocked = true;
70 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_FILE) || defined(CONFIG_IN_EXTERNAL_FLASH)
71 // NOP
72 #elif defined(CONFIG_IN_FLASH)
73 #if defined(STM32F4)
74 FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
75 #elif defined(STM32F7)
76 // NOP
77 #elif defined(STM32H7)
78 // NOP
79 #elif defined(STM32G4)
80 // NOP
81 #elif defined(AT32F4)
82 flash_flag_clear(FLASH_ODF_FLAG | FLASH_PRGMERR_FLAG | FLASH_EPPERR_FLAG);
83 #elif defined(APM32F4)
84 __DAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
85 #elif defined(UNIT_TEST) || defined(SIMULATOR_BUILD)
86 // NOP
87 #else
88 # error "Unsupported CPU"
89 #endif
90 #endif
91 c->err = 0;
94 #if defined(CONFIG_IN_RAM) || defined(CONFIG_IN_EXTERNAL_FLASH) || defined(CONFIG_IN_SDCARD)
95 // No flash sector method required.
96 #elif defined(CONFIG_IN_FLASH)
97 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
99 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
100 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
101 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
102 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
103 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
104 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
105 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
106 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
108 F7X5XI device with 2M flash
109 Sector 8 0x08100000 - 0x0813FFFF 256 Kbytes
110 Sector 9 0x08140000 - 0x0817FFFF 256 Kbytes
111 Sector 10 0x08180000 - 0x081BFFFF 256 Kbytes
112 Sector 11 0x081C0000 - 0x081FFFFF 256 Kbytes
115 static uint32_t getFLASHSectorForEEPROM(void)
117 if ((uint32_t)&__config_start <= 0x08007FFF)
118 return FLASH_SECTOR_0;
119 if ((uint32_t)&__config_start <= 0x0800FFFF)
120 return FLASH_SECTOR_1;
121 if ((uint32_t)&__config_start <= 0x08017FFF)
122 return FLASH_SECTOR_2;
123 if ((uint32_t)&__config_start <= 0x0801FFFF)
124 return FLASH_SECTOR_3;
125 if ((uint32_t)&__config_start <= 0x0803FFFF)
126 return FLASH_SECTOR_4;
127 if ((uint32_t)&__config_start <= 0x0807FFFF)
128 return FLASH_SECTOR_5;
129 if ((uint32_t)&__config_start <= 0x080BFFFF)
130 return FLASH_SECTOR_6;
131 if ((uint32_t)&__config_start <= 0x080FFFFF)
132 return FLASH_SECTOR_7;
133 #if defined(STM32F765xx)
134 if ((uint32_t)&__config_start <= 0x0813FFFF)
135 return FLASH_SECTOR_8;
136 if ((uint32_t)&__config_start <= 0x0817FFFF)
137 return FLASH_SECTOR_9;
138 if ((uint32_t)&__config_start <= 0x081BFFFF)
139 return FLASH_SECTOR_10;
140 if ((uint32_t)&__config_start <= 0x081FFFFF)
141 return FLASH_SECTOR_11;
142 #endif
144 // Not good
145 while (1) {
146 failureMode(FAILURE_CONFIG_STORE_FAILURE);
150 #elif defined(STM32F722xx)
152 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
153 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
154 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
155 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
156 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
157 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
158 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
159 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
162 static uint32_t getFLASHSectorForEEPROM(void)
164 if ((uint32_t)&__config_start <= 0x08003FFF)
165 return FLASH_SECTOR_0;
166 if ((uint32_t)&__config_start <= 0x08007FFF)
167 return FLASH_SECTOR_1;
168 if ((uint32_t)&__config_start <= 0x0800BFFF)
169 return FLASH_SECTOR_2;
170 if ((uint32_t)&__config_start <= 0x0800FFFF)
171 return FLASH_SECTOR_3;
172 if ((uint32_t)&__config_start <= 0x0801FFFF)
173 return FLASH_SECTOR_4;
174 if ((uint32_t)&__config_start <= 0x0803FFFF)
175 return FLASH_SECTOR_5;
176 if ((uint32_t)&__config_start <= 0x0805FFFF)
177 return FLASH_SECTOR_6;
178 if ((uint32_t)&__config_start <= 0x0807FFFF)
179 return FLASH_SECTOR_7;
181 // Not good
182 while (1) {
183 failureMode(FAILURE_CONFIG_STORE_FAILURE);
187 #elif defined(STM32F4)
189 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
190 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
191 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
192 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
193 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
194 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
195 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
196 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
197 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
198 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
199 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
200 Sector 11 0x080E0000 - 0x080FFFFF 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;
221 if ((uint32_t)&__config_start <= 0x0809FFFF)
222 return FLASH_Sector_8;
223 if ((uint32_t)&__config_start <= 0x080DFFFF)
224 return FLASH_Sector_9;
225 if ((uint32_t)&__config_start <= 0x080BFFFF)
226 return FLASH_Sector_10;
227 if ((uint32_t)&__config_start <= 0x080FFFFF)
228 return FLASH_Sector_11;
230 // Not good
231 while (1) {
232 failureMode(FAILURE_CONFIG_STORE_FAILURE);
236 #elif defined(STM32H743xx) || defined(STM32G4) || defined(STM32H7A3xx) || defined(STM32H7A3xxQ) || defined(STM32H723xx) || defined(STM32H725xx)
238 MCUs with uniform array of equal size sectors, handled in two banks having contiguous address.
239 (Devices with non-contiguous flash layout is not currently useful anyways.)
241 H743
242 2 bank * 8 sector/bank * 128K/sector (2MB)
243 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
244 Bank 2 0x08100000 - 0x081FFFFF 128KB * 8
246 H743
247 1 bank * 8 sector/bank * 128K/sector (1MB)
248 Bank 1 0x08000000 - 0x080FFFFF 128KB * 8
250 H7A3
251 2 bank * 128 sector/bank * 8KB/sector (2MB)
252 Bank 1 0x08000000 - 0x080FFFFF 8KB * 128
253 Bank 2 0x08100000 - 0x081FFFFF 8KB * 128
255 G473/474 in dual bank mode
256 2 bank * 128 sector/bank * 2KB/sector (512KB)
257 Bank 1 0x08000000 - 0x0803FFFF 2KB * 128
258 Bank 2 0x08040000 - 0x0807FFFF 2KB * 128
260 Note that FLASH_BANK_SIZE constant used in the following code changes depending on
261 bank operation mode. The code assumes dual bank operation, in which case the
262 FLASH_BANK_SIZE constant is set to one half of the available flash size in HAL.
265 #if defined(STM32H743xx) || defined(STM32H723xx) || defined(STM32H725xx)
266 #define FLASH_PAGE_PER_BANK 8
267 #elif defined(STM32H7A3xx) || defined(STM32H7A3xxQ)
268 #define FLASH_PAGE_PER_BANK 128
269 #elif defined(STM32G4)
270 #define FLASH_PAGE_PER_BANK 128
271 // These are not defined in CMSIS like H7
272 #define FLASH_BANK1_BASE FLASH_BASE
273 #define FLASH_BANK2_BASE (FLASH_BANK1_BASE + FLASH_BANK_SIZE)
274 #endif
276 static void getFLASHSectorForEEPROM(uint32_t address, uint32_t *bank, uint32_t *sector)
278 #if defined(FLASH_BANK2_BASE)
279 if (address >= FLASH_BANK1_BASE && address < FLASH_BANK2_BASE) {
280 *bank = FLASH_BANK_1;
281 } else if (address >= FLASH_BANK2_BASE && address < FLASH_BANK2_BASE + FLASH_BANK_SIZE) {
282 *bank = FLASH_BANK_2;
283 address -= FLASH_BANK_SIZE;
285 #else
286 if (address >= FLASH_BANK1_BASE && address < FLASH_BANK1_BASE + FLASH_BANK_SIZE) {
287 *bank = FLASH_BANK_1;
289 #endif
290 else {
291 // Not good
292 while (1) {
293 failureMode(FAILURE_CONFIG_STORE_FAILURE);
297 address -= FLASH_BANK1_BASE;
298 *sector = address / FLASH_PAGE_SIZE;
300 #elif defined(STM32H750xx)
302 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
303 space by using a smaller function.
305 Bank 1
306 Sector 0 0x08000000 - 0x0801FFFF 128 Kbytes
310 static void getFLASHSectorForEEPROM(uint32_t *bank, uint32_t *sector)
313 uint32_t start = (uint32_t)&__config_start;
315 if (start == FLASH_BANK1_BASE) {
316 *sector = FLASH_SECTOR_0;
317 *bank = FLASH_BANK_1;
318 } else {
319 // Not good
320 while (1) {
321 failureMode(FAILURE_CONFIG_STORE_FAILURE);
325 #elif defined(APM32F4)
327 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
328 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
329 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
330 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
331 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
332 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
333 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
334 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
335 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
336 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
337 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
338 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
341 static uint32_t getFLASHSectorForEEPROM(void)
343 if ((uint32_t)&__config_start <= 0x08003FFF)
344 return FLASH_SECTOR_0;
345 if ((uint32_t)&__config_start <= 0x08007FFF)
346 return FLASH_SECTOR_1;
347 if ((uint32_t)&__config_start <= 0x0800BFFF)
348 return FLASH_SECTOR_2;
349 if ((uint32_t)&__config_start <= 0x0800FFFF)
350 return FLASH_SECTOR_3;
351 if ((uint32_t)&__config_start <= 0x0801FFFF)
352 return FLASH_SECTOR_4;
353 if ((uint32_t)&__config_start <= 0x0803FFFF)
354 return FLASH_SECTOR_5;
355 if ((uint32_t)&__config_start <= 0x0805FFFF)
356 return FLASH_SECTOR_6;
357 if ((uint32_t)&__config_start <= 0x0807FFFF)
358 return FLASH_SECTOR_7;
359 if ((uint32_t)&__config_start <= 0x0809FFFF)
360 return FLASH_SECTOR_8;
361 if ((uint32_t)&__config_start <= 0x080DFFFF)
362 return FLASH_SECTOR_9;
363 if ((uint32_t)&__config_start <= 0x080BFFFF)
364 return FLASH_SECTOR_10;
365 if ((uint32_t)&__config_start <= 0x080FFFFF)
366 return FLASH_SECTOR_11;
368 // Not good
369 while (1) {
370 failureMode(FAILURE_CONFIG_STORE_FAILURE);
374 #endif
375 #endif // CONFIG_IN_FLASH
377 // FIXME the return values are currently magic numbers
378 static int write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
380 if (c->err != 0) {
381 return c->err;
383 #if defined(CONFIG_IN_EXTERNAL_FLASH)
385 uint32_t dataOffset = (uint32_t)(c->address - (uintptr_t)&eepromData[0]);
387 const flashPartition_t *flashPartition = flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG);
388 const flashGeometry_t *flashGeometry = flashGetGeometry();
390 uint32_t flashStartAddress = flashPartition->startSector * flashGeometry->sectorSize;
391 uint32_t flashOverflowAddress = ((flashPartition->endSector + 1) * flashGeometry->sectorSize); // +1 to sector for inclusive
393 uint32_t flashAddress = flashStartAddress + dataOffset;
394 if (flashAddress + CONFIG_STREAMER_BUFFER_SIZE > flashOverflowAddress) {
395 return -3; // address is past end of partition
398 uint32_t flashSectorSize = flashGeometry->sectorSize;
399 uint32_t flashPageSize = flashGeometry->pageSize;
400 const uint8_t *buffers[1];
401 uint32_t bufferSizes[1];
403 bool onPageBoundary = (flashAddress % flashPageSize == 0);
404 if (onPageBoundary) {
406 bool firstPage = (flashAddress == flashStartAddress);
407 if (!firstPage) {
408 flashPageProgramFinish();
411 if (flashAddress % flashSectorSize == 0) {
412 flashEraseSector(flashAddress);
415 flashPageProgramBegin(flashAddress, NULL);
418 buffers[0] = (uint8_t *)buffer;
419 bufferSizes[0] = CONFIG_STREAMER_BUFFER_SIZE;
421 flashPageProgramContinue(buffers, bufferSizes, 1);
423 #elif defined(CONFIG_IN_RAM) || defined(CONFIG_IN_SDCARD) || defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
424 if (c->address == (uintptr_t)&eepromData[0]) {
425 memset(eepromData, 0, sizeof(eepromData));
428 uint64_t *dest_addr = (uint64_t *)c->address;
429 uint64_t *src_addr = (uint64_t*)buffer;
430 uint8_t row_index = CONFIG_STREAMER_BUFFER_SIZE / sizeof(uint64_t);
431 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE % sizeof(uint64_t) == 0, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
432 /* copy the 256 bits flash word */
435 *dest_addr++ = *src_addr++;
436 } while (--row_index != 0);
438 #elif defined(CONFIG_IN_FILE)
440 if (c->address % FLASH_PAGE_SIZE == 0) {
441 const FLASH_Status status = FLASH_ErasePage(c->address);
442 if (status != FLASH_COMPLETE) {
443 return -1;
446 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t), "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
447 const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
448 if (status != FLASH_COMPLETE) {
449 return -2;
452 #elif defined(CONFIG_IN_FLASH)
454 #if defined(STM32H7)
455 if (c->address % FLASH_PAGE_SIZE == 0) {
456 FLASH_EraseInitTypeDef EraseInitStruct = {
457 .TypeErase = FLASH_TYPEERASE_SECTORS,
458 #if !(defined(STM32H7A3xx) || defined(STM32H7A3xxQ))
459 .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
460 #endif
461 .NbSectors = 1
463 getFLASHSectorForEEPROM(c->address, &EraseInitStruct.Banks, &EraseInitStruct.Sector);
464 uint32_t SECTORError;
465 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
466 if (status != HAL_OK) {
467 return -1;
471 // For H7
472 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t DataAddress);
473 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * FLASH_NB_32BITWORD_IN_FLASHWORD, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
474 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD, c->address, (uint64_t)(uint32_t)buffer);
475 if (status != HAL_OK) {
476 return -2;
478 #elif defined(STM32F7)
479 if (c->address % FLASH_PAGE_SIZE == 0) {
480 FLASH_EraseInitTypeDef EraseInitStruct = {
481 .TypeErase = FLASH_TYPEERASE_SECTORS,
482 .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
483 .NbSectors = 1
485 EraseInitStruct.Sector = getFLASHSectorForEEPROM();
486 uint32_t SECTORError;
487 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
488 if (status != HAL_OK) {
489 return -1;
493 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
494 // For F7
495 // HAL_StatusTypeDef HAL_FLASH_Program(uint32_t TypeProgram, uint32_t Address, uint64_t Data);
496 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer);
497 if (status != HAL_OK) {
498 return -2;
500 #elif defined(STM32G4)
501 if (c->address % FLASH_PAGE_SIZE == 0) {
503 FLASH_EraseInitTypeDef EraseInitStruct = {
504 .TypeErase = FLASH_TYPEERASE_PAGES,
505 .NbPages = 1
507 getFLASHSectorForEEPROM(c->address, &EraseInitStruct.Banks, &EraseInitStruct.Page);
508 uint32_t SECTORError;
509 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
510 if (status != HAL_OK) {
511 return -1;
515 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 2, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
516 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, c->address, (uint64_t)*buffer);
517 if (status != HAL_OK) {
518 return -2;
520 #elif defined(AT32F4)
521 if (c->address % FLASH_PAGE_SIZE == 0) {
522 const flash_status_type status = flash_sector_erase(c->address);
523 if (status != FLASH_OPERATE_DONE) {
524 return -1;
528 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
529 const flash_status_type status = flash_word_program(c->address, (uint32_t)*buffer);
530 if (status != FLASH_OPERATE_DONE) {
531 return -2;
533 #elif defined(APM32F4)
534 if (c->address % FLASH_PAGE_SIZE == 0) {
535 FLASH_EraseInitTypeDef EraseInitStruct = {
536 .TypeErase = FLASH_TYPEERASE_SECTORS,
537 .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
538 .NbSectors = 1
540 EraseInitStruct.Sector = getFLASHSectorForEEPROM();
541 uint32_t SECTORError;
542 const DAL_StatusTypeDef status = DAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
543 if (status != DAL_OK) {
544 return -1;
548 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
549 const DAL_StatusTypeDef status = DAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer);
550 if (status != DAL_OK) {
551 return -2;
553 #else // !STM32H7 && !STM32F7 && !STM32G4
554 if (c->address % FLASH_PAGE_SIZE == 0) {
555 const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(), VoltageRange_3); //0x08080000 to 0x080A0000
556 if (status != FLASH_COMPLETE) {
557 return -1;
561 STATIC_ASSERT(CONFIG_STREAMER_BUFFER_SIZE == sizeof(uint32_t) * 1, "CONFIG_STREAMER_BUFFER_SIZE does not match written size");
562 const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
563 if (status != FLASH_COMPLETE) {
564 return -2;
566 #endif
567 #endif
568 c->address += CONFIG_STREAMER_BUFFER_SIZE;
569 return 0;
572 int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size)
574 for (const uint8_t *pat = p; pat != (uint8_t*)p + size; pat++) {
575 c->buffer.b[c->at++] = *pat;
577 if (c->at == sizeof(c->buffer)) {
578 c->err = write_word(c, &c->buffer.w);
579 c->at = 0;
582 return c->err;
585 int config_streamer_status(config_streamer_t *c)
587 return c->err;
590 int config_streamer_flush(config_streamer_t *c)
592 if (c->at != 0) {
593 memset(c->buffer.b + c->at, 0, sizeof(c->buffer) - c->at);
594 c->err = write_word(c, &c->buffer.w);
595 c->at = 0;
597 return c->err;
600 int config_streamer_finish(config_streamer_t *c)
602 if (c->unlocked) {
603 #if defined(CONFIG_IN_SDCARD)
604 saveEEPROMToSDCard();
605 #elif defined(CONFIG_IN_EXTERNAL_FLASH)
606 flashFlush();
607 #elif defined(CONFIG_IN_MEMORY_MAPPED_FLASH)
608 saveEEPROMToMemoryMappedFlash();
609 #elif defined(CONFIG_IN_RAM)
610 // NOP
611 #elif defined(CONFIG_IN_FILE)
612 FLASH_Lock();
613 #elif defined(CONFIG_IN_FLASH)
614 #if defined(STM32F7) || defined(STM32H7) || defined(STM32G4)
615 HAL_FLASH_Lock();
616 #elif defined(AT32F4)
617 flash_lock();
618 #elif defined(APM32F4)
619 DAL_FLASH_Lock();
620 #else
621 FLASH_Lock();
622 #endif
623 #endif
624 c->unlocked = false;
626 return c->err;