Merge pull request #9335 from iNavFlight/MrD_Add-odometer-to-OSD
[inav.git] / src / main / config / config_streamer_stm32f7.c
blob61b148fce571d45a657555ee8ccbb153ea9af299
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(STM32F7) && !defined(CONFIG_IN_RAM) && !defined(CONFIG_IN_EXTERNAL_FLASH)
25 #if defined(STM32F745xx) || defined(STM32F746xx) || defined(STM32F765xx)
27 Sector 0 0x08000000 - 0x08007FFF 32 Kbytes
28 Sector 1 0x08008000 - 0x0800FFFF 32 Kbytes
29 Sector 2 0x08010000 - 0x08017FFF 32 Kbytes
30 Sector 3 0x08018000 - 0x0801FFFF 32 Kbytes
31 Sector 4 0x08020000 - 0x0803FFFF 128 Kbytes
32 Sector 5 0x08040000 - 0x0807FFFF 256 Kbytes
33 Sector 6 0x08080000 - 0x080BFFFF 256 Kbytes
34 Sector 7 0x080C0000 - 0x080FFFFF 256 Kbytes
36 #define FLASH_PAGE_SIZE ((uint32_t)0x8000)
37 static uint32_t getFLASHSectorForEEPROM(uint32_t address)
39 if (address <= 0x08007FFF)
40 return FLASH_SECTOR_0;
41 if (address <= 0x0800FFFF)
42 return FLASH_SECTOR_1;
43 if (address <= 0x08017FFF)
44 return FLASH_SECTOR_2;
45 if (address <= 0x0801FFFF)
46 return FLASH_SECTOR_3;
47 if (address <= 0x0803FFFF)
48 return FLASH_SECTOR_4;
49 if (address <= 0x0807FFFF)
50 return FLASH_SECTOR_5;
51 if (address <= 0x080BFFFF)
52 return FLASH_SECTOR_6;
53 if (address <= 0x080FFFFF)
54 return FLASH_SECTOR_7;
56 // Not good
57 while (1) {
58 failureMode(FAILURE_FLASH_WRITE_FAILED);
61 #elif defined(STM32F722xx)
63 Sector 0 0x08000000 - 0x08003FFF 16 Kbytes
64 Sector 1 0x08004000 - 0x08007FFF 16 Kbytes
65 Sector 2 0x08008000 - 0x0800BFFF 16 Kbytes
66 Sector 3 0x0800C000 - 0x0800FFFF 16 Kbytes
67 Sector 4 0x08010000 - 0x0801FFFF 64 Kbytes
68 Sector 5 0x08020000 - 0x0803FFFF 128 Kbytes
69 Sector 6 0x08040000 - 0x0805FFFF 128 Kbytes
70 Sector 7 0x08060000 - 0x0807FFFF 128 Kbytes
72 #define FLASH_PAGE_SIZE ((uint32_t)0x4000)
73 static uint32_t getFLASHSectorForEEPROM(uint32_t address)
75 if (address <= 0x08003FFF)
76 return FLASH_SECTOR_0;
77 if (address <= 0x08007FFF)
78 return FLASH_SECTOR_1;
79 if (address <= 0x0800BFFF)
80 return FLASH_SECTOR_2;
81 if (address <= 0x0800FFFF)
82 return FLASH_SECTOR_3;
83 if (address <= 0x0801FFFF)
84 return FLASH_SECTOR_4;
85 if (address <= 0x0803FFFF)
86 return FLASH_SECTOR_5;
87 if (address <= 0x0805FFFF)
88 return FLASH_SECTOR_6;
89 if (address <= 0x0807FFFF)
90 return FLASH_SECTOR_7;
92 // Not good
93 while (1) {
94 failureMode(FAILURE_FLASH_WRITE_FAILED);
97 #else
98 # error "Unsupported CPU!"
99 #endif
101 void config_streamer_impl_unlock(void)
103 HAL_FLASH_Unlock();
106 void config_streamer_impl_lock(void)
108 HAL_FLASH_Lock();
111 int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer_align_type_t *buffer)
113 if (c->err != 0) {
114 return c->err;
117 if (c->address % FLASH_PAGE_SIZE == 0) {
118 FLASH_EraseInitTypeDef EraseInitStruct = {
119 .TypeErase = FLASH_TYPEERASE_SECTORS,
120 .VoltageRange = FLASH_VOLTAGE_RANGE_3, // 2.7-3.6V
121 .NbSectors = 1
123 EraseInitStruct.Sector = getFLASHSectorForEEPROM(c->address);
125 uint32_t SECTORError;
126 const HAL_StatusTypeDef status = HAL_FLASHEx_Erase(&EraseInitStruct, &SECTORError);
127 if (status != HAL_OK){
128 return -1;
132 const HAL_StatusTypeDef status = HAL_FLASH_Program(FLASH_TYPEPROGRAM_WORD, c->address, (uint64_t)*buffer);
133 if (status != HAL_OK) {
134 return -2;
137 c->address += CONFIG_STREAMER_BUFFER_SIZE;
138 return 0;
141 #endif