Skip to content

Commit 1f13b6d

Browse files
authored
Merge pull request #52 from YJ98/STM32F407_add_littlefs
Stm32 f407 add littlefs
2 parents 054f6db + addfbef commit 1f13b6d

File tree

3 files changed

+98
-3
lines changed

3 files changed

+98
-3
lines changed

bsp/stm32/stm32f407-atk-explorer/board/Kconfig

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,17 @@ menu "Onboard Peripheral Drivers"
9191
range 0 24000000
9292
depends on BSP_USING_SDCARD
9393
default 1000000
94+
95+
config BSP_USING_SPI_FLASH_LITTLEFS
96+
bool "Enable LITTLEFS"
97+
select RT_USING_DFS
98+
select RT_USING_DFS_ROMFS
99+
select RT_USING_MTD_NOR
100+
select BSP_USING_SPI_FLASH
101+
select BSP_USING_FS
102+
select RT_USING_SYSTEM_WORKQUEUE
103+
default n
104+
94105
endmenu
95106

96107
endmenu

bsp/stm32/stm32f407-atk-explorer/board/ports/drv_filesystem.c

Lines changed: 67 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@
1616
#include <dfs_romfs.h>
1717
#include <dfs_fs.h>
1818
#include <dfs_posix.h>
19+
#include <fal.h>
1920

2021
#if DFS_FILESYSTEMS_MAX < 4
2122
#error "Please define DFS_FILESYSTEMS_MAX more than 4"
@@ -75,12 +76,73 @@ static int onboard_sdcard_mount(void)
7576
}
7677
#endif
7778

79+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
80+
81+
#define FS_PARTITION_NAME "filesystem"
82+
83+
static void spiflash_mount(void *parameter)
84+
{
85+
struct rt_device *mtd_dev = RT_NULL;
86+
fal_init();
87+
mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
88+
if (!mtd_dev)
89+
{
90+
LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
91+
}
92+
while (1)
93+
{
94+
rt_thread_mdelay(500);
95+
if(rt_device_find(FS_PARTITION_NAME) != RT_NULL)
96+
{
97+
if (dfs_mount(FS_PARTITION_NAME, "/flash", "lfs", 0, 0) == RT_EOK)
98+
{
99+
LOG_I("spi flash mount to '/flash'");
100+
break;
101+
}
102+
else
103+
{
104+
LOG_W("spi flash mount to '/flash' failed!");
105+
}
106+
}
107+
}
108+
}
109+
110+
static int onboard_spiflash_mount(void)
111+
{
112+
rt_thread_t tid;
113+
114+
if (dfs_mount(FS_PARTITION_NAME, "/flash", "lfs", 0, 0) == RT_EOK)
115+
{
116+
LOG_I("spi flash mount to '/flash'");
117+
}
118+
else
119+
{
120+
tid = rt_thread_create("spiflash_mount", spiflash_mount, RT_NULL,
121+
1024, RT_THREAD_PRIORITY_MAX - 3, 20);
122+
if (tid != RT_NULL)
123+
{
124+
rt_thread_startup(tid);
125+
}
126+
else
127+
{
128+
LOG_E("create spiflash_mount thread err!");
129+
}
130+
}
131+
132+
return RT_EOK;
133+
}
134+
#endif
135+
136+
78137
static const struct romfs_dirent _romfs_root[] =
79138
{
80139
#ifdef BSP_USING_SDCARD
81140
{ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
82141
#endif
83-
// {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
142+
143+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
144+
{ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
145+
#endif
84146
};
85147

86148
const struct romfs_dirent romfs_root =
@@ -98,6 +160,10 @@ static int filesystem_mount(void)
98160
onboard_sdcard_mount();
99161
#endif
100162

163+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
164+
onboard_spiflash_mount();
165+
#endif
166+
101167
return RT_EOK;
102168
}
103169
INIT_APP_EXPORT(filesystem_mount);

bsp/stm32/stm32f407-atk-explorer/board/ports/fal_cfg.h

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,35 +14,53 @@
1414
#include <rtthread.h>
1515
#include <board.h>
1616

17+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
18+
extern struct fal_flash_dev nor_flash0;
19+
#else
1720
#define FLASH_SIZE_GRANULARITY_16K (4 * 16 * 1024)
1821
#define FLASH_SIZE_GRANULARITY_64K (64 * 1024)
1922
#define FLASH_SIZE_GRANULARITY_128K (7 * 128 * 1024)
20-
2123
#define STM32_FLASH_START_ADRESS_16K STM32_FLASH_START_ADRESS
2224
#define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K + FLASH_SIZE_GRANULARITY_16K)
2325
#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K + FLASH_SIZE_GRANULARITY_64K)
2426

2527
extern const struct fal_flash_dev stm32_onchip_flash_16k;
2628
extern const struct fal_flash_dev stm32_onchip_flash_64k;
2729
extern const struct fal_flash_dev stm32_onchip_flash_128k;
30+
#endif
31+
2832

2933
/* flash device table */
34+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
35+
#define FAL_FLASH_DEV_TABLE \
36+
{ \
37+
&nor_flash0, \
38+
}
39+
#else
3040
#define FAL_FLASH_DEV_TABLE \
3141
{ \
3242
&stm32_onchip_flash_16k, \
3343
&stm32_onchip_flash_64k, \
3444
&stm32_onchip_flash_128k, \
3545
}
46+
#endif
47+
3648
/* ====================== Partition Configuration ========================== */
3749
#ifdef FAL_PART_HAS_TABLE_CFG
3850

3951
/* partition table */
52+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
53+
#define FAL_PART_TABLE \
54+
{ \
55+
{FAL_PART_MAGIC_WROD, "filesystem",FAL_USING_NOR_FLASH_DEV_NAME, 0 , 16 * 1024 * 1024, 0}, \
56+
}
57+
#else
4058
#define FAL_PART_TABLE \
4159
{ \
4260
{FAL_PART_MAGIC_WROD, "bootloader", "onchip_flash_16k", 0 , FLASH_SIZE_GRANULARITY_16K , 0}, \
4361
{FAL_PART_MAGIC_WROD, "param", "onchip_flash_64k", 0 , FLASH_SIZE_GRANULARITY_64K , 0}, \
4462
{FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0 , FLASH_SIZE_GRANULARITY_128K, 0}, \
4563
}
46-
64+
#endif
4765
#endif /* FAL_PART_HAS_TABLE_CFG */
4866
#endif /* _FAL_CFG_H_ */

0 commit comments

Comments
 (0)