Merge pull request #4919 from YJ98/STM32F407_add_littlefs

【修改】STM32F4探索者,移植 littlefs 文件系统
This commit is contained in:
guo 2021-08-08 20:19:28 +08:00 committed by GitHub
commit 85d7748266
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 91 additions and 8 deletions

View File

@ -76,7 +76,7 @@ menu "Onboard Peripheral Drivers"
bool bool
default n default n
config BSP_USING_SDCARD config BSP_USING_SDCARD_FATFS
bool "Enable SDCARD (FATFS)" bool "Enable SDCARD (FATFS)"
select BSP_USING_SDIO select BSP_USING_SDIO
select RT_USING_DFS select RT_USING_DFS
@ -91,6 +91,20 @@ menu "Onboard Peripheral Drivers"
range 0 24000000 range 0 24000000
depends on BSP_USING_SDCARD depends on BSP_USING_SDCARD
default 1000000 default 1000000
config BSP_USING_SPI_FLASH_LITTLEFS
bool "Enable SPI-FLASH (LittleFS)"
select RT_USING_DFS
select RT_USING_DFS_ROMFS
select RT_USING_MTD_NOR
select BSP_USING_SPI_FLASH
select BSP_USING_FS
select PKG_USING_FAL
select FAL_USING_SFUD_PORT
select PKG_USING_LITTLEFS
select RT_USING_SYSTEM_WORKQUEUE
default n
endmenu endmenu
endmenu endmenu

View File

@ -28,7 +28,7 @@
#define DBG_LVL DBG_INFO #define DBG_LVL DBG_INFO
#include <rtdbg.h> #include <rtdbg.h>
#ifdef BSP_USING_SDCARD #ifdef BSP_USING_SDCARD_FATFS
static void sd_mount(void *parameter) static void sd_mount(void *parameter)
{ {
while (1) while (1)
@ -75,12 +75,52 @@ static int onboard_sdcard_mount(void)
} }
#endif #endif
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
#include <fal.h>
#define FS_PARTITION_NAME "spiflash0"
static int onboard_spiflash_mount(void)
{
struct rt_device *mtd_dev = RT_NULL;
fal_init();
mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
if (!mtd_dev)
{
LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
}
if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
{
LOG_I("spi flash mount to '/spiflash'");
}
else
{
dfs_mkfs("lfs", FS_PARTITION_NAME);
if (dfs_mount(FS_PARTITION_NAME, "/spiflash", "lfs", 0, 0) == RT_EOK)
{
LOG_I("spi flash mount to '/spiflash'");
}
else
{
LOG_E("spi flash failed to mount to '/spiflash'");
}
}
return RT_EOK;
}
#endif
static const struct romfs_dirent _romfs_root[] = static const struct romfs_dirent _romfs_root[] =
{ {
#ifdef BSP_USING_SDCARD #ifdef BSP_USING_SDCARD_FATFS
{ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}, {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0},
#endif #endif
// {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
{ROMFS_DIRENT_DIR, "spiflash", RT_NULL, 0},
#endif
}; };
const struct romfs_dirent romfs_root = const struct romfs_dirent romfs_root =
@ -94,10 +134,14 @@ static int filesystem_mount(void)
{ {
LOG_E("rom mount to '/' failed!"); LOG_E("rom mount to '/' failed!");
} }
#ifdef BSP_USING_SDCARD #ifdef BSP_USING_SDCARD_FATFS
onboard_sdcard_mount(); onboard_sdcard_mount();
#endif #endif
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
onboard_spiflash_mount();
#endif
return RT_EOK; return RT_EOK;
} }
INIT_APP_EXPORT(filesystem_mount); INIT_APP_EXPORT(filesystem_mount);

View File

@ -14,10 +14,12 @@
#include <rtthread.h> #include <rtthread.h>
#include <board.h> #include <board.h>
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
extern struct fal_flash_dev nor_flash0;
#else
#define FLASH_SIZE_GRANULARITY_16K (4 * 16 * 1024) #define FLASH_SIZE_GRANULARITY_16K (4 * 16 * 1024)
#define FLASH_SIZE_GRANULARITY_64K (64 * 1024) #define FLASH_SIZE_GRANULARITY_64K (64 * 1024)
#define FLASH_SIZE_GRANULARITY_128K (7 * 128 * 1024) #define FLASH_SIZE_GRANULARITY_128K (7 * 128 * 1024)
#define STM32_FLASH_START_ADRESS_16K STM32_FLASH_START_ADRESS #define STM32_FLASH_START_ADRESS_16K STM32_FLASH_START_ADRESS
#define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K + FLASH_SIZE_GRANULARITY_16K) #define STM32_FLASH_START_ADRESS_64K (STM32_FLASH_START_ADRESS_16K + FLASH_SIZE_GRANULARITY_16K)
#define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K + FLASH_SIZE_GRANULARITY_64K) #define STM32_FLASH_START_ADRESS_128K (STM32_FLASH_START_ADRESS_64K + FLASH_SIZE_GRANULARITY_64K)
@ -25,24 +27,40 @@
extern const struct fal_flash_dev stm32_onchip_flash_16k; extern const struct fal_flash_dev stm32_onchip_flash_16k;
extern const struct fal_flash_dev stm32_onchip_flash_64k; extern const struct fal_flash_dev stm32_onchip_flash_64k;
extern const struct fal_flash_dev stm32_onchip_flash_128k; extern const struct fal_flash_dev stm32_onchip_flash_128k;
#endif
/* flash device table */ /* flash device table */
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
#define FAL_FLASH_DEV_TABLE \
{ \
&nor_flash0, \
}
#else
#define FAL_FLASH_DEV_TABLE \ #define FAL_FLASH_DEV_TABLE \
{ \ { \
&stm32_onchip_flash_16k, \ &stm32_onchip_flash_16k, \
&stm32_onchip_flash_64k, \ &stm32_onchip_flash_64k, \
&stm32_onchip_flash_128k, \ &stm32_onchip_flash_128k, \
} }
#endif
/* ====================== Partition Configuration ========================== */ /* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG #ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */ /* partition table */
#ifdef BSP_USING_SPI_FLASH_LITTLEFS
#define FAL_PART_TABLE \
{ \
{FAL_PART_MAGIC_WROD, "spiflash0", FAL_USING_NOR_FLASH_DEV_NAME, 0 , 16 * 1024 * 1024, 0}, \
}
#else
#define FAL_PART_TABLE \ #define FAL_PART_TABLE \
{ \ { \
{FAL_PART_MAGIC_WROD, "bootloader", "onchip_flash_16k", 0 , FLASH_SIZE_GRANULARITY_16K , 0}, \ {FAL_PART_MAGIC_WROD, "bootloader", "onchip_flash_16k", 0 , FLASH_SIZE_GRANULARITY_16K , 0}, \
{FAL_PART_MAGIC_WROD, "param", "onchip_flash_64k", 0 , FLASH_SIZE_GRANULARITY_64K , 0}, \ {FAL_PART_MAGIC_WROD, "param", "onchip_flash_64k", 0 , FLASH_SIZE_GRANULARITY_64K , 0}, \
{FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0 , FLASH_SIZE_GRANULARITY_128K, 0}, \ {FAL_PART_MAGIC_WROD, "app", "onchip_flash_128k", 0 , FLASH_SIZE_GRANULARITY_128K, 0}, \
} }
#endif
#endif /* FAL_PART_HAS_TABLE_CFG */ #endif /* FAL_PART_HAS_TABLE_CFG */
#endif /* _FAL_CFG_H_ */ #endif /* _FAL_CFG_H_ */

View File

@ -14,12 +14,19 @@
#include "drv_spi.h" #include "drv_spi.h"
#if defined(BSP_USING_SPI_FLASH) #if defined(BSP_USING_SPI_FLASH)
#ifdef FAL_USING_NOR_FLASH_DEV_NAME
#define _SPI_FLASH_NAME FAL_USING_NOR_FLASH_DEV_NAME
#else
#define _SPI_FLASH_NAME "W25Q128"
#endif
static int rt_hw_spi_flash_init(void) static int rt_hw_spi_flash_init(void)
{ {
__HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOB_CLK_ENABLE();
rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14); rt_hw_spi_device_attach("spi1", "spi10", GPIOB, GPIO_PIN_14);
if (RT_NULL == rt_sfud_flash_probe("W25Q128", "spi10")) if (RT_NULL == rt_sfud_flash_probe(_SPI_FLASH_NAME, "spi10"))
{ {
return -RT_ERROR; return -RT_ERROR;
}; };