Skip to content

Commit 6be0bbb

Browse files
committed
stmhal: Add support for flash filesystem on F401 MCUs.
It uses a 16k cache buffer and so the filesystem size is limited.
1 parent 5a11086 commit 6be0bbb

File tree

2 files changed

+25
-0
lines changed

2 files changed

+25
-0
lines changed

stmhal/flash.c

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -37,10 +37,12 @@
3737
#define ADDR_FLASH_SECTOR_5 ((uint32_t)0x08020000) /* Base @ of Sector 5, 128 Kbytes */
3838
#define ADDR_FLASH_SECTOR_6 ((uint32_t)0x08040000) /* Base @ of Sector 6, 128 Kbytes */
3939
#define ADDR_FLASH_SECTOR_7 ((uint32_t)0x08060000) /* Base @ of Sector 7, 128 Kbytes */
40+
#if defined(FLASH_SECTOR_8)
4041
#define ADDR_FLASH_SECTOR_8 ((uint32_t)0x08080000) /* Base @ of Sector 8, 128 Kbytes */
4142
#define ADDR_FLASH_SECTOR_9 ((uint32_t)0x080A0000) /* Base @ of Sector 9, 128 Kbytes */
4243
#define ADDR_FLASH_SECTOR_10 ((uint32_t)0x080C0000) /* Base @ of Sector 10, 128 Kbytes */
4344
#define ADDR_FLASH_SECTOR_11 ((uint32_t)0x080E0000) /* Base @ of Sector 11, 128 Kbytes */
45+
#endif
4446

4547
static const uint32_t flash_info_table[26] = {
4648
ADDR_FLASH_SECTOR_0, FLASH_SECTOR_0,
@@ -51,11 +53,15 @@ static const uint32_t flash_info_table[26] = {
5153
ADDR_FLASH_SECTOR_5, FLASH_SECTOR_5,
5254
ADDR_FLASH_SECTOR_6, FLASH_SECTOR_6,
5355
ADDR_FLASH_SECTOR_7, FLASH_SECTOR_7,
56+
#if defined(FLASH_SECTOR_8)
5457
ADDR_FLASH_SECTOR_8, FLASH_SECTOR_8,
5558
ADDR_FLASH_SECTOR_9, FLASH_SECTOR_9,
5659
ADDR_FLASH_SECTOR_10, FLASH_SECTOR_10,
5760
ADDR_FLASH_SECTOR_11, FLASH_SECTOR_11,
5861
ADDR_FLASH_SECTOR_11 + 0x20000, 0,
62+
#else
63+
ADDR_FLASH_SECTOR_7 + 0x20000, 0,
64+
#endif
5965
};
6066

6167
uint32_t flash_get_sector_info(uint32_t addr, uint32_t *start_addr, uint32_t *size) {

stmhal/storage.c

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,10 +34,26 @@
3434
#include "flash.h"
3535
#include "storage.h"
3636

37+
#if defined(STM32F405xx)
38+
3739
#define CACHE_MEM_START_ADDR (0x10000000) // CCM data RAM, 64k
3840
#define FLASH_PART1_START_BLOCK (0x100)
3941
#define FLASH_PART1_NUM_BLOCKS (224) // 16k+16k+16k+64k=112k
4042
#define FLASH_MEM_START_ADDR (0x08004000) // sector 1, 16k
43+
#define FLASH_SECTOR_SIZE_MAX (0x10000) // 64k max, size of CCM
44+
45+
#elif defined(STM32F401xE)
46+
47+
STATIC byte flash_cache_mem[0x4000] __attribute__((aligned(4))); // 16k
48+
#define CACHE_MEM_START_ADDR (&flash_cache_mem[0])
49+
#define FLASH_PART1_START_BLOCK (0x100)
50+
#define FLASH_PART1_NUM_BLOCKS (128) // 16k+16k+16k+16k(of64k)=64k
51+
#define FLASH_MEM_START_ADDR (0x08004000) // sector 1, 16k
52+
#define FLASH_SECTOR_SIZE_MAX (0x4000) // 16k max due to size of cache buffer
53+
54+
#else
55+
#error "no storage support for this MCU"
56+
#endif
4157

4258
#define FLASH_FLAG_DIRTY (1)
4359
#define FLASH_FLAG_FORCE_WRITE (2)
@@ -62,6 +78,9 @@ static uint8_t *flash_cache_get_addr_for_write(uint32_t flash_addr) {
6278
uint32_t flash_sector_start;
6379
uint32_t flash_sector_size;
6480
uint32_t flash_sector_id = flash_get_sector_info(flash_addr, &flash_sector_start, &flash_sector_size);
81+
if (flash_sector_size > FLASH_SECTOR_SIZE_MAX) {
82+
flash_sector_size = FLASH_SECTOR_SIZE_MAX;
83+
}
6584
if (flash_cache_sector_id != flash_sector_id) {
6685
flash_cache_flush();
6786
memcpy((void*)CACHE_MEM_START_ADDR, (const void*)flash_sector_start, flash_sector_size);

0 commit comments

Comments
 (0)