1 /*
2  * Copyright (c) 2006-2022, RT-Thread Development Team
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  *
6  * Change Logs:
7  * Date           Author       Notes
8  * 2020-08-07     NU-LL        first version
9  */
10 #include <rtthread.h>
11 #include <rtdevice.h>
12 #include <drv_spi.h>
13 #include <dev_spi_flash.h>
14 #include <dev_spi_flash_sfud.h>
15 #include <fal.h>
16 #include <dfs_fs.h>
17 
18 #define DRV_DEBUG
19 #define LOG_TAG                         "drv.spiflash"
20 #include <drv_log.h>
21 
22 #define FS_PARTITION_NAME               "filesystem"
23 
24 #define SPI_CS_GPIO                     GPIOD
25 #define SPI_CS_PIN                      GPIO_PIN_6
26 
rt_hw_spi_flash_with_sfud_init(void)27 static int rt_hw_spi_flash_with_sfud_init(void)
28 {
29     rt_err_t err = RT_EOK;
30     rt_hw_spi_device_attach("spi1", "spi10", GET_PIN(D, 6));
31 
32     /* init W25Q16    , And register as a block device */
33     if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, "spi10"))
34     {
35         LOG_E("Failed to probe flash device "FAL_USING_NOR_FLASH_DEV_NAME);
36         return -RT_ERROR;
37     }
38     return err;
39 }
40 INIT_DEVICE_EXPORT(rt_hw_spi_flash_with_sfud_init);
41 
42 
43 
mnt(void)44 static int mnt(void)
45 {
46     struct rt_device *mtd_dev = RT_NULL;
47 
48     fal_init();
49     mtd_dev = fal_mtd_nor_device_create(FS_PARTITION_NAME);
50     if (!mtd_dev)
51     {
52         LOG_E("Can't create a mtd device on '%s' partition.", FS_PARTITION_NAME);
53     }
54     else
55     {
56         /* mount littlefs */
57         if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == 0)
58         {
59             LOG_I("Filesystem initialized!");
60         }
61         else
62         {
63             dfs_mkfs("lfs", FS_PARTITION_NAME);
64             /* mount littlefs */
65             if (dfs_mount(FS_PARTITION_NAME, "/", "lfs", 0, 0) == 0)
66             {
67                 mkdir("/qspi",0x777);
68                 LOG_I("mkdir /qspi. Filesystem initialized!");
69             }
70             else
71             {
72                 LOG_E("Failed to initialize filesystem!");
73             }
74         }
75     }
76     return RT_EOK;
77 }
78 INIT_ENV_EXPORT(mnt);
79 
80 
81 
82 
83