Blackbox device type 'file' (SITL) considered working when file handler is available
[inav.git] / src / main / config / config_streamer_extflash.c
blobcb576ccc26ccdbb8ae5e245cc0835854b7b3d22f
1 /*
2 * This file is part of Cleanflight.
4 * Cleanflight is free software: you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation, either version 3 of the License, or
7 * (at your option) any later version.
9 * Cleanflight is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
18 #include <string.h>
19 #include "platform.h"
20 #include "drivers/system.h"
21 #include "drivers/flash.h"
22 #include "config/config_streamer.h"
24 #if defined(CONFIG_IN_EXTERNAL_FLASH)
26 static bool streamerLocked = true;
28 void config_streamer_impl_unlock(void)
30 const flashGeometry_t *flashGeometry = flashGetGeometry();
31 if (flashGeometry->pageSize == CONFIG_STREAMER_BUFFER_SIZE) {
32 // streamer needs to buffer exactly one flash page
33 streamerLocked = false;
37 void config_streamer_impl_lock(void)
39 streamerLocked = true;
42 int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
44 if (streamerLocked) {
45 return -1;
48 uint32_t dataOffset = (uint32_t)(c->address - (uintptr_t)&eepromData[0]);
50 const flashPartition_t *flashPartition = flashPartitionFindByType(FLASH_PARTITION_TYPE_CONFIG);
51 const flashGeometry_t *flashGeometry = flashGetGeometry();
53 uint32_t flashStartAddress = flashPartition->startSector * flashGeometry->sectorSize;
54 uint32_t flashOverflowAddress = ((flashPartition->endSector + 1) * flashGeometry->sectorSize); // +1 to sector for inclusive
56 uint32_t flashAddress = flashStartAddress + dataOffset;
57 if (flashAddress + CONFIG_STREAMER_BUFFER_SIZE > flashOverflowAddress) {
58 return -2; // address is past end of partition
61 uint32_t flashSectorSize = flashGeometry->sectorSize;
63 if (flashAddress % flashSectorSize == 0) {
64 flashEraseSector(flashAddress);
67 if (flashPageProgram(flashAddress, (uint8_t *)buffer, CONFIG_STREAMER_BUFFER_SIZE) == flashAddress) {
68 // returned same address: programming failed
69 return -3;
72 c->address += CONFIG_STREAMER_BUFFER_SIZE;
74 return 0;
77 #endif