Skip to content

Commit fba00db

Browse files
authored
Make config_streamer_stm32h7 less hard coded. (#10706)
Use constants defined in STM32 libraries for each mcu for flash sector size and number of sectors. Add DUAL_BANK flash support and only pass voltage levels if MCU supports it.
1 parent 3dd4de5 commit fba00db

File tree

4 files changed

+74
-39
lines changed

4 files changed

+74
-39
lines changed

src/main/config/config_eeprom.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -118,6 +118,12 @@ void initEEPROM(void)
118118
BUILD_BUG_ON(sizeof(configFooter_t) != 2);
119119
BUILD_BUG_ON(sizeof(configRecord_t) != 6);
120120

121+
#ifdef STM32H7A3xx
122+
BUILD_BUG_ON(CONFIG_STREAMER_BUFFER_SIZE != 16);
123+
#elif defined(STM32H743xx)
124+
BUILD_BUG_ON(CONFIG_STREAMER_BUFFER_SIZE != 32);
125+
#endif
126+
121127
#if defined(CONFIG_IN_EXTERNAL_FLASH)
122128
bool eepromLoaded = loadEEPROMFromExternalFlash();
123129
if (!eepromLoaded) {

src/main/config/config_streamer.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ int config_streamer_write(config_streamer_t *c, const uint8_t *p, uint32_t size)
5555
return -1;
5656
}
5757

58-
for (const uint8_t *pat = p; pat != (uint8_t*)p + size; pat++) {
58+
for (const uint8_t *pat = p; pat != (uint8_t *)p + size; pat++) {
5959
c->buffer.b[c->at++] = *pat;
6060

6161
if (c->at == sizeof(c->buffer)) {
@@ -81,7 +81,7 @@ int config_streamer_flush(config_streamer_t *c)
8181
c->err = config_streamer_impl_write_word(c, &c->buffer.w);
8282
c->at = 0;
8383
}
84-
return c-> err;
84+
return c->err;
8585
}
8686

8787
int config_streamer_finish(config_streamer_t *c)
@@ -91,4 +91,4 @@ int config_streamer_finish(config_streamer_t *c)
9191
c->unlocked = false;
9292
}
9393
return c->err;
94-
}
94+
}

src/main/config/config_streamer.h

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -27,15 +27,14 @@
2727

2828
#ifdef CONFIG_IN_EXTERNAL_FLASH
2929
#define CONFIG_STREAMER_BUFFER_SIZE M25P16_PAGESIZE // Must match flash device page size
30-
typedef uint32_t config_streamer_buffer_align_type_t;
3130
#elif defined(STM32H7)
32-
#define CONFIG_STREAMER_BUFFER_SIZE 32 // Flash word = 256-bits
33-
typedef uint64_t config_streamer_buffer_align_type_t;
31+
#define CONFIG_STREAMER_BUFFER_SIZE (FLASH_NB_32BITWORD_IN_FLASHWORD * 4) // Flash word = 256-bits or 128bits, depending on the mcu
3432
#else
3533
#define CONFIG_STREAMER_BUFFER_SIZE 4
36-
typedef uint32_t config_streamer_buffer_align_type_t;
3734
#endif
3835

36+
typedef uint32_t config_streamer_buffer_align_type_t;
37+
3938
typedef struct config_streamer_s {
4039
uintptr_t address;
4140
uintptr_t end;

src/main/config/config_streamer_stm32h7.c

Lines changed: 62 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -15,16 +15,45 @@
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
2857
static 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

5786
void 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

Comments
 (0)