8000 stmhal: L4: Modify flash.c and storage.c to support L4 MCU. · micropython/micropython@067fb2d · GitHub
[go: up one dir, main page]

Skip to content

Commit 067fb2d

Browse files
Tobias Badertscherdpgeorge
Tobias Badertscher
authored andcommitted
stmhal: L4: Modify flash.c and storage.c to support L4 MCU.
The way to lookup the flash sector now uses a much simpler table for all MCUs.
1 parent dda1a41 commit 067fb2d

File tree

2 files changed

+133
-87
lines changed

2 files changed

+133
-87
lines changed

stmhal/flash.c

Lines changed: 125 additions & 87 deletions
Original file line numberDiff line numberDiff line change
@@ -30,105 +30,102 @@
3030
#include "mpconfigport.h"
3131
#include "py/misc.h"
3232

33-
#if defined(MCU_SERIES_F7)
33+
typedef struct {
34+
uint32_t base_address;
35+
uint32_t sector_size;
36+
uint32_t sector_count;
37+
} flash_layout_t;
38+
39+
#if defined(MCU_SERIES_F4)
40+
41+
static const flash_layout_t flash_layout[] = {
42+
{ 0x08000000, 0x04000, 4 },
43+
{ 0x08010000, 0x10000, 1 },
44+
{ 0x08020000, 0x20000, 3 },
45+
#if defined(FLASH_SECTOR_8)
46+
{ 0x08080000, 0x20000, 4 },
47+
#endif
48+
#if defined(FLASH_SECTOR_12)
49+
{ 0x08100000, 0x04000, 4 },
50+
{ 0x08110000, 0x10000, 1 },
51+
{ 0x08120000, 0x20000, 7 },
52+
#endif
53+
};
54+
55+
#elif defined(MCU_SERIES_F7)
3456

3557
// FLASH_FLAG_PGSERR (Programming Sequence Error) was renamed to
3658
// FLASH_FLAG_ERSERR (Erasing Sequence Error) in STM32F7
3759
#define FLASH_FLAG_PGSERR FLASH_FLAG_ERSERR
3860

39-
/* Base address of the Flash sectors */
40-
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 32 Kbytes */
41-
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08008000) /* Base @ of Sector 1, 32 Kbytes */
42-
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08010000) /* Base @ of Sector 2, 32 Kbytes */
43-
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x08018000) /* Base @ of Sector 3, 32 Kbytes */
44-
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08020000) /* Base @ of Sector 4, 128 Kbytes */
45-
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08040000) /* Base @ of Sector 5, 256 Kbytes */
46-
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08080000) /* Base @ of Sector 6, 256 Kbytes */
47-
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x080C0000) /* Base @ of Sector 7, 256 Kbytes */
48-
#define ADDR_FLASH_END ((uint32_t)0x08100000) /* 1 Mbytes total */
61+
static const flash_layout_t flash_layout[] = {
62+
{ 0x08000000, 0x08000, 4 },
63+
{ 0x08020000, 0x20000, 1 },
64+
{ 0x08040000, 0x40000, 3 },
65+
};
4966

50-
#else
67+
#elif defined(MCU_SERIES_L4)
68+
69+
static const flash_layout_t flash_layout[] = {
70+
{ (uint32_t)FLASH_BASE, (uint32_t)FLASH_PAGE_SIZE, 512 },
71+
};
5172

52-
/* Base address of the Flash sectors */
53-
#define ADDR_FLASH_SECTOR_0 ((uint32_t)0x08000000) /* Base @ of Sector 0, 16 Kbytes */
54-
#define ADDR_FLASH_SECTOR_1 ((uint32_t)0x08004000) /* Base @ of Sector 1, 16 Kbytes */
55-
#define ADDR_FLASH_SECTOR_2 ((uint32_t)0x08008000) /* Base @ of Sector 2, 16 Kbytes */
56-
#define ADDR_FLASH_SECTOR_3 ((uint32_t)0x0800C000) /* Base @ of Sector 3, 16 Kbytes */
57-
#define ADDR_FLASH_SECTOR_4 ((uint32_t)0x08010000) /* Base @ of Sector 4, 64 Kbytes */
58-
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
59-
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
60-
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
61-
#if !defined(FLASH_SECTOR_8)
62-
#define ADDR_FLASH_END ((uint32_t)0x08080000) /* 512 Kbytes total */
63-
#else
64-
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
65-
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
66-
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
67-
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
68-
#if !defined(FLASH_SECTOR_12)
69-
#define ADDR_FLASH_END ((uint32_t)0x08100000) /* 1 Mbytes total */
7073
#else
71-
#define ADDR_FLASH_SECTOR_12 ((uint32_t)0x08100000) /* Base @ of Sector 12, 16 Kbytes */
72-
#define ADDR_FLASH_SECTOR_13 ((uint32_t)0x08104000) /* Base @ of Sector 13, 16 Kbytes */
73-
#define ADDR_FLASH_SECTOR_14 ((uint32_t)0x08108000) /* Base @ of Sector 14, 16 Kbytes */
74-
#define ADDR_FLASH_SECTOR_15 ((uint32_t)0x0810C000) /* Base @ of Sector 15, 16 Kbytes */
75-
#define ADDR_FLASH_SECTOR_16 ((uint32_t)0x08110000) /* Base @ of Sector 16, 64 Kbytes */
76-
#define ADDR_FLASH_SECTOR_17 ((uint32_t)0x08120000) /* Base @ of Sector 17, 128 Kbytes */
77-
#define ADDR_FLASH_SECTOR_18 ((uint32_t)0x08140000) /* Base @ of Sector 18, 128 Kbytes */
78-
#define ADDR_FLASH_SECTOR_19 ((uint32_t)0x08160000) /* Base @ of Sector 19, 128 Kbytes */
79-
#define ADDR_FLASH_SECTOR_20 ((uint32_t)0x08180000) /* Base @ of Sector 20, 128 Kbytes */
80-
#define ADDR_FLASH_SECTOR_21 ((uint32_t)0x081A0000) /* Base @ of Sector 21, 128 Kbytes */
81-
#define ADDR_FLASH_SECTOR_22 ((uint32_t)0x081C0000) /* Base @ of Sector 22, 128 Kbytes */
82-
#define ADDR_FLASH_SECTOR_23 ((uint32_t)0x081E0000) /* Base @ of Sector 23, 128 Kbytes */
83-
#define ADDR_FLASH_END ((uint32_t)0x08200000) /* 2 Mbytes total */
84-
#endif
74+
#error Unsupported processor
8575
#endif
8676

87-
#endif // MCU_SERIES_F7
88-
89-
static const uint32_t flash_info_table[] = {
90-
ADDR_FLASH_SECTOR_0, FLASH_SECTOR_0,
91-
ADDR_FLASH_SECTOR_1, FLASH_SECTOR_1,
92-
ADDR_FLASH_SECTOR_2, FLASH_SECTOR_2,
93-
ADDR_FLASH_SECTOR_3, FLASH_SECTOR_3,
94-
ADDR_FLASH_SECTOR_4, FLASH_SECTOR_4,
95-
ADDR_FLASH_SECTOR_5, FLASH_SECTOR_5,
96-
ADDR_FLASH_SECTOR_6, FLASH_SECTOR_6,
97-
ADDR_FLASH_SECTOR_7, FLASH_SECTOR_7,
98-
#if defined(FLASH_SECTOR_8)
99-
ADDR_FLASH_SECTOR_8, FLASH_SECTOR_8,
100-
ADDR_FLASH_SECTOR_9, FLASH_SECTOR_9,
101-
ADDR_FLASH_SECTOR_10, FLASH_SECTOR_10,
102-
ADDR_FLASH_SECTOR_11, FLASH_SECTOR_11,
103-
#endif
104-
#if defined(FLASH_SECTOR_12)
105-
ADDR_FLASH_SECTOR_12, FLASH_SECTOR_12,
106-
ADDR_FLASH_SECTOR_13, FLASH_SECTOR_13,
107-
ADDR_FLASH_SECTOR_14, FLASH_SECTOR_14,
108-
ADDR_FLASH_SECTOR_15, FLASH_SECTOR_15,
109-
ADDR_FLASH_SECTOR_16, FLASH_SECTOR_16,
110-
ADDR_FLASH_SECTOR_17, FLASH_SECTOR_17,
111-
ADDR_FLASH_SECTOR_18, FLASH_SECTOR_18,
112-
ADDR_FLASH_SECTOR_19, FLASH_SECTOR_19,
113-
ADDR_FLASH_SECTOR_20, FLASH_SECTOR_20,
114-
ADDR_FLASH_SECTOR_21, FLASH_SECTOR_21,
115-
ADDR_FLASH_SECTOR_22, FLASH_SECTOR_22,
116-
ADDR_FLASH_SECTOR_23, FLASH_SECTOR_23,
117-
#endif
118-
ADDR_FLASH_END, 0,
119-
};
77+
#if defined(MCU_SERIES_L4)
78+
79+
// get the bank of a given flash address
80+
static uint32_t get_bank(uint32_t addr) {
81+
if (READ_BIT(SYSCFG->MEMRMP, SYSCFG_MEMRMP_FB_MODE) == 0) {
82+
// no bank swap
83+
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
84+
return FLASH_BANK_1;
85+
} else {
86+
return FLASH_BANK_2;
87+
}
88+
} else {
89+
// bank swap
90+
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
91+
return FLASH_BANK_2;
92+
} else {
93+
return FLASH_BANK_1;
94+
}
95+
}
96+
}
97+
98+
// get the page of a given flash address
99+
static uint32_t get_page(uint32_t addr) {
100+
if (addr < (FLASH_BASE + FLASH_BANK_SIZE)) {
101+
// bank 1
102+
return (addr - FLASH_BASE) / FLASH_PAGE_SIZE;
103+
} else {
104+
// bank 2
105+
return (addr - (FLASH_BASE + FLASH_BANK_SIZE)) / FLASH_PAGE_SIZE;
106+
}
107+
}
108+
109+
#endif
12011 E41F 0

121111
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {
122-
if (addr >= flash_info_table[0]) {
123-
for (int i = 0; i < MP_ARRAY_SIZE(flash_info_table) - 2; i += 2) {
124-
if (addr < flash_info_table[i + 2]) {
125-
if (start_addr != NULL) {
126-
*start_addr = flash_info_table[i];
127-
}
128-
if (size != NULL) {
129-
*size = flash_info_table[i + 2] - flash_info_table[i];
112+
if (addr >= flash_layout[0].base_address) {
113+
uint32_t sector_index = 0;
114+
for (int i = 0; i < MP_ARRAY_SIZE(flash_layout); ++i) {
115+
for (int j = 0; j < flash_layout[i].sector_count; ++j) {
116+
uint32_t sector_start_next = flash_layout[i].base_address
117+
+ (j + 1) * flash_layout[i].sector_size;
118+
if (addr < sector_start_next) {
119+
if (start_addr != NULL) {
120+
*start_addr = flash_layout[i].base_address
121+
+ j * flash_layout[i].sector_size;
122+
}
123+
if (size != NULL) {
124+
*size = flash_layout[i].sector_size;
125+
}
126+
return sector_index;
130127
}
131-
return flash_info_table[i + 1];
128+
++sector_index;
132129
}
133130
}
134131
}
@@ -144,16 +141,30 @@ void flash_erase(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32)
144141
// unlock
145142
HAL_FLASH_Unlock();
146143

144+
FLASH_EraseInitTypeDef EraseInitStruct;
145+
146+
#if defined(MCU_SERIES_L4)
147+
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_ALL_ERRORS);
148+
149+
// erase the sector(s)
150+
// The sector returned by flash_get_sector_info can not be used
151+
// as the flash has on each bank 0/1 pages 0..255
152+
EraseInitStruct.TypeErase = FLASH_TYPEERASE_PAGES;
153+
EraseInitStruct.Banks = get_bank(flash_dest);
154+
EraseInitStruct.Page = get_page(flash_dest);
155+
EraseInitStruct.NbPages = get_page(flash_dest + 4 * num_word32 - 1) - EraseInitStruct.Page + 1;;
156+
#else
147157
// Clear pending flags (if any)
148158
__HAL_FLASH_CLEAR_FLAG(FLASH_FLAG_EOP | FLASH_FLAG_OPERR | FLASH_FLAG_WRPERR |
149159
FLASH_FLAG_PGAERR | FLASH_FLAG_PGPERR | FLASH_FLAG_PGSERR);
150160

151161
// erase the sector(s)
152-
FLASH_EraseInitTypeDef EraseInitStruct;
153162
EraseInitStruct.TypeErase = TYPEERASE_SECTORS;
154163
EraseInitStruct.VoltageRange = VOLTAGE_RANGE_3; // voltage range needs to be 2.7V to 3.6V
155164
EraseInitStruct.Sector = flash_get_sector_info(flash_dest, NULL, NULL);
156165
EraseInitStruct.NbSectors = flash_get_sector_info(flash_dest + 4 * num_word32 - 1, NULL, NULL) - EraseInitStruct.Sector + 1;
166+
#endif
167+
157168
uint32_t SectorError = 0;
158169
if (HAL_FLASHEx_Erase(&EraseInitStruct, &SectorError) != HAL_OK) {
159170
// error occurred during sector erase
@@ -192,6 +203,31 @@ void flash_erase_it(uint32_t flash_dest, const uint32_t *src, uint32_t num_word3
192203
*/
193204

194205
void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32) {
206+
#if defined(MCU_SERIES_L4)
207+
208+
// program the flash uint64 by uint64
209+
for (int i = 0; i < num_word32 / 2; i++) {
210+
uint64_t val = *(uint64_t*)src;
211+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) {
212+
// error occurred during flash write
213+
HAL_FLASH_Lock(); // lock the flash
214+
return;
215+
}
216+
flash_dest += 8;
217+
src += 2;
218+
}
219+
if ((num_word32 & 0x01) == 1) {
220+
uint64_t val = *(uint64_t*)flash_dest;
221+
val = (val & 0xffffffff00000000uL) | (*src);
222+
if (HAL_FLASH_Program(FLASH_TYPEPROGRAM_DOUBLEWORD, flash_dest, val) != HAL_OK) {
223+
// error occurred during flash write
224+
HAL_FLASH_Lock(); // lock the flash
225+
return;
226+
}
227+
}
228+
229+
#else
230+
195231
// program the flash word by word
196232
for (int i = 0; i < num_word32; i++) {
197233
if (HAL_FLASH_Program(TYPEPROGRAM_WORD, flash_dest, *src) != HAL_OK) {
@@ -203,6 +239,8 @@ void flash_write(uint32_t flash_dest, const uint32_t *src, uint32_t num_word32)
203239
src += 1;
204240
}
205241

242+
#endif
243+
206244
// lock the flash
207245
HAL_FLASH_Lock();
208246
}

stmhal/storage.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,14 @@ STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k
8484
#define FLASH_MEM_SEG1_START_ADDR (0x08008000) // sector 1
8585
#define FLASH_MEM_SEG1_NUM_BLOCKS (192) // sectors 1,2,3: 32k+32k+32=96k
8686

87+
#elif defined(STM32L476xx)
88+
89+
// The STM32L476 doesn't have CCRAM, so we use the 32K SRAM2 for this.
90+
#define CACHE_MEM_START_ADDR (0x10000000) // SRAM2 data RAM, 32k
91+
#define FLASH_SECTOR_SIZE_MAX (0x00800) // 2k max
92+
#define FLASH_MEM_SEG1_START_ADDR (0x08000800) // sector 1
93+
#define FLASH_MEM_SEG1_NUM_BLOCKS (252) // 1 Block=512 Bytes Reserve 126 kBytes
94+
8795
#else
8896
#error "no storage support for this MCU"
8997
#endif

0 commit comments

Comments
 (0)
0