Merge pull request #9335 from iNavFlight/MrD_Add-odometer-to-OSD
[inav.git] / src / main / config / config_streamer_stm32f4.c
blob4f48831bc8892e557739319cc8dde98ed275cc1f
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 "config/config_streamer.h"
23 #if defined(STM32F4) && !defined(CONFIG_IN_RAM) && !defined(CONFIG_IN_EXTERNAL_FLASH)
26 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
27 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
28 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
29 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
30 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
31 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
32 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
33 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
34 Sector 8 0x08080000 - 0x0809FFFF 128 Kbytes
35 Sector 9 0x080A0000 - 0x080BFFFF 128 Kbytes
36 Sector 10 0x080C0000 - 0x080DFFFF 128 Kbytes
37 Sector 11 0x080E0000 - 0x080FFFFF 128 Kbytes
39 #define FLASH_PAGE_SIZE ((uint32_t)0x4000)
40 static uint32_t getFLASHSectorForEEPROM(uint32_t address)
42 if (address <= 0x08003FFF)
43 return FLASH_Sector_0;
44 if (address <= 0x08007FFF)
45 return FLASH_Sector_1;
46 if (address <= 0x0800BFFF)
47 return FLASH_Sector_2;
48 if (address <= 0x0800FFFF)
49 return FLASH_Sector_3;
50 if (address <= 0x0801FFFF)
51 return FLASH_Sector_4;
52 if (address <= 0x0803FFFF)
53 return FLASH_Sector_5;
54 if (address <= 0x0805FFFF)
55 return FLASH_Sector_6;
56 if (address <= 0x0807FFFF)
57 return FLASH_Sector_7;
58 if (address <= 0x0809FFFF)
59 return FLASH_Sector_8;
60 if (address <= 0x080DFFFF)
61 return FLASH_Sector_9;
62 if (address <= 0x080BFFFF)
63 return FLASH_Sector_10;
64 if (address <= 0x080FFFFF)
65 return FLASH_Sector_11;
67 // Not good
68 while (1) {
69 failureMode(FAILURE_FLASH_WRITE_FAILED);
73 void config_streamer_impl_unlock(void)
75 FLASH_Unlock();
76 FLASH_ClearFlag(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR | FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
79 void config_streamer_impl_lock(void)
81 FLASH_Lock();
84 int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
86 if (c->err != 0) {
87 return c->err;
90 if (c->address % FLASH_PAGE_SIZE == 0) {
91 const FLASH_Status status = FLASH_EraseSector(getFLASHSectorForEEPROM(c->address), VoltageRange_3);
92 if (status != FLASH_COMPLETE) {
93 return -1;
97 const FLASH_Status status = FLASH_ProgramWord(c->address, *buffer);
98 if (status != FLASH_COMPLETE) {
99 return -2;
102 c->address += CONFIG_STREAMER_BUFFER_SIZE;
103 return 0;
106 #endif