1+ /*
2+ * Copyright (c) 2019-2024, RT-Thread Development Team
3+ *
4+ * SPDX-License-Identifier: Apache-2.0
5+ *
6+ * Change Logs:
7+ * Date Author Notes
8+ * 2026-06-02 CoreBoxer add LittleFS on QSPI NOR Flash (IMXRT1180-EVK)
9+ */
10+
11+ #include <rtthread.h>
12+
13+ /* ====================================================================
14+ * QSPI NOR Flash LittleFS (controlled by BSP_USING_QSPI_FLASH_FS)
15+ *
16+ * Dependencies:
17+ * - drv_flash.c (Flash read/write/erase driver via ROM API)
18+ * - FAL framework (partition management)
19+ * - LittleFS component (RT-Thread package)
20+ *
21+ * FAL partition configuration (fal_cfg.h):
22+ * "filesystem" partition → filesystem area of norflash0
23+ *
24+ * Mount procedure:
25+ * 1. Find FAL partition to confirm correct configuration
26+ * 2. Create MTD NOR block device
27+ * 3. Try direct mount (for already formatted filesystem)
28+ * 4. If failed, perform mkfs then remount (first use or format scenario)
29+ * ==================================================================== */
30+ #ifdef BSP_USING_QSPI_FLASH_FS
31+ #include <dfs_fs.h>
32+ #include <fal.h>
33+
34+ #define FS_PARTITION_NAME "filesystem"
35+ #define FS_TYPE_NAME "lfs"
36+ #define FS_MOUNT_POINT "/"
37+
38+ static int _qspi_flash_fs_mount (void )
39+ {
40+ struct fal_mtd_nor_device * mtd_dev ;
41+ int ret ;
42+
43+ /* Find FAL partition to confirm configuration is correct */
44+ if (fal_partition_find (FS_PARTITION_NAME ) == RT_NULL )
45+ {
46+ rt_kprintf ("[qspi_fs] partition '%s' not found, check fal_cfg.h\n" ,
47+ FS_PARTITION_NAME );
48+ return - RT_ERROR ;
49+ }
50+
51+ /* Create MTD NOR block device for filesystem partition */
52+ mtd_dev = (struct fal_mtd_nor_device * )fal_mtd_nor_device_create (FS_PARTITION_NAME );
53+ if (mtd_dev == RT_NULL )
54+ {
55+ rt_kprintf ("[qspi_fs] failed to create MTD NOR device for '%s'\n" ,
56+ FS_PARTITION_NAME );
57+ return - RT_ERROR ;
58+ }
59+
60+ /* Try direct mount (for already formatted flash) */
61+ ret = dfs_mount (FS_PARTITION_NAME , FS_MOUNT_POINT , FS_TYPE_NAME , 0 , 0 );
62+ if (ret == 0 )
63+ {
64+ rt_kprintf ("[qspi_fs] '%s' mounted at '%s'\n" ,
65+ FS_PARTITION_NAME , FS_MOUNT_POINT );
66+ return RT_EOK ;
67+ }
68+
69+ /* Mount failed, indicating flash is not formatted (first use scenario), perform mkfs */
70+ rt_kprintf ("[qspi_fs] mount failed (ret=%d), formatting '%s'...\n" ,
71+ ret , FS_PARTITION_NAME );
72+
73+ ret = dfs_mkfs (FS_TYPE_NAME , FS_PARTITION_NAME );
74+ if (ret != 0 )
75+ {
76+ rt_kprintf ("[qspi_fs] mkfs failed (ret=%d)\n" , ret );
77+ return - RT_ERROR ;
78+ }
79+
80+ /* Remount after successful mkfs */
81+ ret = dfs_mount (FS_PARTITION_NAME , FS_MOUNT_POINT , FS_TYPE_NAME , 0 , 0 );
82+ if (ret != 0 )
83+ {
84+ rt_kprintf ("[qspi_fs] mount failed after mkfs (ret=%d)\n" , ret );
85+ return - RT_ERROR ;
86+ }
87+
88+ rt_kprintf ("[qspi_fs] '%s' formatted and mounted at '%s'\n" ,
89+ FS_PARTITION_NAME , FS_MOUNT_POINT );
90+ return RT_EOK ;
91+ }
92+ INIT_APP_EXPORT (_qspi_flash_fs_mount );
93+ /*
94+ * FAL initialization registered as INIT_COMPONENT_EXPORT:
95+ * after Flash driver (INIT_DEVICE_EXPORT)
96+ * before filesystem mount (INIT_APP_EXPORT)
97+ */
98+ INIT_COMPONENT_EXPORT (fal_init );
99+
100+ #endif /* BSP_USING_QSPI_FLASH_FS */
0 commit comments