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/>.
20 #include "drivers/system.h"
21 #include "config/config_streamer.h"
23 #if defined(STM32H7) && !defined(CONFIG_IN_RAM) && !defined(CONFIG_IN_EXTERNAL_FLASH)
25 #if defined(STM32H743xx)
26 /* Sectors 0-7 of 128K each */
27 #define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
28 static uint32_t getFLASHSectorForEEPROM(uint32_t address
)
30 if (address
<= 0x0801FFFF)
31 return FLASH_SECTOR_0
;
32 if (address
<= 0x0803FFFF)
33 return FLASH_SECTOR_1
;
34 if (address
<= 0x0805FFFF)
35 return FLASH_SECTOR_2
;
36 if (address
<= 0x0807FFFF)
37 return FLASH_SECTOR_3
;
38 if (address
<= 0x0809FFFF)
39 return FLASH_SECTOR_4
;
40 if (address
<= 0x080BFFFF)
41 return FLASH_SECTOR_5
;
42 if (address
<= 0x080DFFFF)
43 return FLASH_SECTOR_6
;
44 if (address
<= 0x080FFFFF)
45 return FLASH_SECTOR_7
;
48 failureMode(FAILURE_FLASH_WRITE_FAILED
);
51 #elif defined(STM32H750xx)
52 # error "STM32750xx 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"
54 # error "Unsupported CPU!"
57 void config_streamer_impl_unlock(void)
62 void config_streamer_impl_lock(void)
67 int config_streamer_impl_write_word(config_streamer_t
*c
, config_streamer_buffer_align_type_t
*buffer
)
73 if (c
->address
% FLASH_PAGE_SIZE
== 0) {
74 FLASH_EraseInitTypeDef EraseInitStruct
= {
75 .TypeErase
= FLASH_TYPEERASE_SECTORS
,
76 .VoltageRange
= FLASH_VOLTAGE_RANGE_3
, // 2.7-3.6V
80 EraseInitStruct
.Sector
= getFLASHSectorForEEPROM(c
->address
);
83 const HAL_StatusTypeDef status
= HAL_FLASHEx_Erase(&EraseInitStruct
, &SECTORError
);
84 if (status
!= HAL_OK
) {
89 // On H7 HAL_FLASH_Program takes data address, not the raw word value
90 const HAL_StatusTypeDef status
= HAL_FLASH_Program(FLASH_TYPEPROGRAM_FLASHWORD
, c
->address
, (uint32_t)buffer
);
91 if (status
!= HAL_OK
) {
95 c
->address
+= CONFIG_STREAMER_BUFFER_SIZE
;