Skip to content

Commit 85d7748

Browse files
authored
Merge pull request #4919 from YJ98/STM32F407_add_littlefs
【修改】STM32F4探索者,移植 littlefs 文件系统
2 parents 1c61d6e + ad25b22 commit 85d7748

File tree

4 files changed

+91
-8
lines changed

4 files changed

+91
-8
lines changed

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

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -76,7 +76,7 @@ menu "Onboard Peripheral Drivers"
7676
bool
7777
default n
7878

79-
config BSP_USING_SDCARD
79+
config BSP_USING_SDCARD_FATFS
8080
bool "Enable SDCARD (FATFS)"
8181
select BSP_USING_SDIO
8282
select RT_USING_DFS
@@ -91,6 +91,20 @@ 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 SPI-FLASH (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 PKG_USING_FAL
103+
select FAL_USING_SFUD_PORT
104+
select PKG_USING_LITTLEFS
105+
select RT_USING_SYSTEM_WORKQUEUE
106+
default n
107+
94108
endmenu
95109

96110
endmenu

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

Lines changed: 48 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#define DBG_LVL DBG_INFO
2929
#include <rtdbg.h>
3030

31-
#ifdef BSP_USING_SDCARD
31+
#ifdef BSP_USING_SDCARD_FATFS
3232
static void sd_mount(void *parameter)
3333
{
3434
while (1)
@@ -75,12 +75,52 @@ static int onboard_sdcard_mount(void)
7575
}
7676
#endif
7777

78+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
79+
#include <fal.h>
80+
#define FS_PARTITION_NAME "spiflash0"
81+
82+
static int onboard_spiflash_mount(void)
83+
{
84+
struct rt_device *mtd_dev = RT_NULL;
85+
86+
fal_init();
87+
88+
mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
89+
if (!mtd_dev)
90+
{
91+
LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
92+
}
93+
94+
if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
95+
{
96+
LOG_I("spi flash mount to '/spiflash'");
97+
}
98+
else
99+
{
100+
dfs_mkfs("lfs", FS_PARTITION_NAME);
101+
if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
102+
{
103+
LOG_I("spi flash mount to '/spiflash'");
104+
}
105+
else
106+
{
107+
LOG_E("spi flash failed to mount to '/spiflash'");
108+
}
109+
}
110+
111+
return RT_EOK;
112+
}
113+
#endif
114+
78115
static const struct romfs_dirent _romfs_root[] =
79116
{
80-
#ifdef BSP_USING_SDCARD
117+
#ifdef BSP_USING_SDCARD_FATFS
81118
{ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
82119
#endif
83-
// {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
120+
121+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
122+
{ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
123+
#endif
84124
};
85125

86126
const struct romfs_dirent romfs_root =
@@ -94,10 +134,14 @@ static int filesystem_mount(void)
94134
{
95135
LOG_E("rom mount to '/' failed!");
96136
}
97-
#ifdef BSP_USING_SDCARD
137+
#ifdef BSP_USING_SDCARD_FATFS
98138
onboard_sdcard_mount();
99139
#endif
100140

141+
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
142+
onboard_spiflash_mount();
143+
#endif
144+
101145
return RT_EOK;
102146
}
103147
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, "spiflash0", 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_ */

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

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,12 +14,19 @@
1414
#include "drv_spi.h"
1515

1616
#if defined(BSP_USING_SPI_FLASH)
17+
18+
#ifdef FAL_USING_NOR_FLASH_DEV_NAME
19+
#define _SPI_FLASH_NAME FAL_USING_NOR_FLASH_DEV_NAME
20+
#else
21+
#define _SPI_FLASH_NAME "W25Q128"
22+
#endif
23+
1724
static int rt_hw_spi_flash_init(void)
1825
{
1926
__HAL_RCC_GPIOB_CLK_ENABLE();
2027
rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14);
2128

22-
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10"))
29+
if (RT_NULL == rt_sfud_flash_probe(_SPI_FLASH_NAME, "spi10"))
2330
{
2431
return -RT_ERROR;
2532
};

0 commit comments

Comments
 (0)