1515 * along with Cleanflight. If not, see <http://www.gnu.org/licenses/>.
1616 */
1717
18- #include <string.h>
19- #include "platform.h"
20- #include "drivers/system.h"
21- #include "config/config_streamer.h"
18+ #include <string.h>
19+ #include "platform.h"
20+ #include "drivers/system.h"
21+ #include "config/config_streamer.h"
22+
23+ #if defined(STM32H7 ) && !defined(CONFIG_IN_RAM ) && !defined(CONFIG_IN_EXTERNAL_FLASH )
2224
23- #if defined(STM32H7 ) && !defined(CONFIG_IN_RAM ) && !defined(CONFIG_IN_EXTERNAL_FLASH )
25+ static uint32_t getFLASHBankForEEPROM (uint32_t address )
26+ {
27+ #ifdef DUAL_BANK
28+ if (address < (FLASH_BASE + FLASH_BANK_SIZE )) {
29+ return FLASH_BANK_1 ;
30+ }
31+
32+ return FLASH_BANK_2 ;
33+ #else
34+ return FLASH_BANK_1 ;
35+ #endif
36+ }
37+
38+ #if defined(STM32H7A3xx )
39+ static uint32_t getFLASHSectorForEEPROM (uint32_t address )
40+ {
41+ uint32_t sector = 0 ;
42+
43+ if (address < (FLASH_BASE + FLASH_BANK_SIZE )) {
44+ sector = (address - FLASH_BASE ) / FLASH_SECTOR_SIZE ;
45+ } else {
46+ sector = (address - (FLASH_BASE + FLASH_BANK_SIZE )) / FLASH_SECTOR_SIZE ;
47+ }
2448
25- #if defined(STM32H743xx )
49+ if (sector > FLASH_SECTOR_TOTAL ) {
50+ failureMode (FAILURE_FLASH_WRITE_FAILED );
51+ }
52+
53+ return sector ;
54+ }
55+ #elif defined(STM32H743xx )
2656/* Sectors 0-7 of 128K each */
27- #define FLASH_PAGE_SIZE ((uint32_t)0x20000) // 128K sectors
2857static uint32_t getFLASHSectorForEEPROM (uint32_t address )
2958{
3059 if (address <= 0x0801FFFF )
@@ -49,9 +78,9 @@ static uint32_t getFLASHSectorForEEPROM(uint32_t address)
4978 }
5079}
5180#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"
81+ #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"
5382#else
54- # error "Unsupported CPU!"
83+ #error "Unsupported CPU!"
5584#endif
5685
5786void config_streamer_impl_unlock (void )
@@ -70,30 +99,31 @@ int config_streamer_impl_write_word(config_streamer_t *c, config_streamer_buffer
7099 return c -> err ;
71100 }
72101
73- if (c -> address % FLASH_PAGE_SIZE == 0 ) {
102+ if (c -> address % FLASH_SECTOR_SIZE == 0 ) {
74103 FLASH_EraseInitTypeDef EraseInitStruct = {
75- .TypeErase = FLASH_TYPEERASE_SECTORS ,
76- .VoltageRange = FLASH_VOLTAGE_RANGE_3 , // 2.7-3.6V
77- .NbSectors = 1 ,
78- .Banks = FLASH_BANK_1
79- };
80- EraseInitStruct .Sector = getFLASHSectorForEEPROM (c -> address );
81-
82- uint32_t SECTORError ;
83- const HAL_StatusTypeDef status = HAL_FLASHEx_Erase (& EraseInitStruct , & SECTORError );
84- if (status != HAL_OK ) {
85- return -1 ;
86- }
87- }
104+ .TypeErase = FLASH_TYPEERASE_SECTORS ,
105+ #ifdef FLASH_VOLTAGE_RANGE_3
106+ .VoltageRange = FLASH_VOLTAGE_RANGE_3 , // 2.7-3.6V
107+ #endif
108+ .NbSectors = 1 };
109+ EraseInitStruct .Banks = getFLASHBankForEEPROM (c -> address );
110+ EraseInitStruct .Sector = getFLASHSectorForEEPROM (c -> address );
88111
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 ) {
92- return -2 ;
93- }
112+ uint32_t SECTORError ;
113+ const HAL_StatusTypeDef status = HAL_FLASHEx_Erase (& EraseInitStruct , & SECTORError );
114+ if (status != HAL_OK ) {
115+ return -1 ;
116+ }
117+ }
94118
95- c -> address += CONFIG_STREAMER_BUFFER_SIZE ;
96- return 0 ;
97- }
119+ // On H7 HAL_FLASH_Program takes data address, not the raw word value
120+ const HAL_StatusTypeDef status = HAL_FLASH_Program (FLASH_TYPEPROGRAM_FLASHWORD , c -> address , (uint32_t )buffer );
121+ if (status != HAL_OK ) {
122+ return -2 ;
123+ }
98124
99- #endif
125+ c -> address += CONFIG_STREAMER_BUFFER_SIZE ;
126+ return 0 ;
127+ }
128+
129+ #endif
0 commit comments