当前位置:   article > 正文

RT-Thread ART-Pi 外部SPI Flash 使用FATFS文件系统_art-pi bootloader

art-pi bootloader

验证环境

  • Win10 64位
  • Keil MDK 5.30
  • ART-Pi 开发板:STM32H750XBH6开发板
  • 工程:最小RT-Thread 系统,基于master分支,版本:4.1.0

需求

  • ART-Pi 内部的Flash比较小,规格书里提示:只有128K,目前作为bootloader,跳转到外部8M SPI Flash上,也就是工程的代码,烧写在8M 外部SPI Flash上,这样的代码空间足够大,可以做很多事情
  • ART-Pi 还有一个16M 外部SPI Flash,除了放置WIFI BLE固件外,还有12M的空间,可以用于挂在文件系统
  • 外部16MB SPI Flash默认的分区表,可以查看:fal_cfg.h
/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG
/* partition table */
#define FAL_PART_TABLE                                                                     \
{                                                                                          \
    {FAL_PART_MAGIC_WORD, "wifi_image", NOR_FLASH_DEV_NAME,           0,     512*1024, 0}, \
    {FAL_PART_MAGIC_WORD, "bt_image",   NOR_FLASH_DEV_NAME,    512*1024,     512*1024, 0}, \
    {FAL_PART_MAGIC_WORD, "download",   NOR_FLASH_DEV_NAME,   1024*1024,  2*1024*1024, 0}, \
    {FAL_PART_MAGIC_WORD, "easyflash",  NOR_FLASH_DEV_NAME, 3*1024*1024,  1*1024*1024, 0}, \
    {FAL_PART_MAGIC_WORD, "filesystem", NOR_FLASH_DEV_NAME, 4*1024*1024, 12*1024*1024, 0}, \
}
#endif /* FAL_PART_HAS_TABLE_CFG */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 开启文件系统
  • 可以使用menuconfig,配置使能外部的16M SPI Flash,使能文件系统(基于Flash的)

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

在这里插入图片描述

修改代码

  • 默认 filesystem.c 开启了 RT_USING_DFS_ROMFS ,发现是 board\Kconfig 默认配置的,这里去掉这个选项,这样文件系统可以写
  • 修改 filesystem.c,默认lfs(littlefs)文件系统,改为elm(fatfs)文件系统
  • 使用:RT_USING_DFS_ROMFS 屏蔽掉 只读分区的挂载操作
  • 把 外部16M SPI Flash分区:"filesystem" 挂载为文件根目录 /
/*
 * Copyright (c) 2006-2022, RT-Thread Development Team
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Change Logs:
 * Date           Author        Notes
 * 2018-12-13     balanceTWK    add sdcard port file
 * 2019-06-11     WillianChan   Add SD card hot plug detection
 */

#include <rtthread.h>

#ifdef BSP_USING_FS
#if DFS_FILESYSTEMS_MAX < 4
    #error "Please define DFS_FILESYSTEMS_MAX more than 4"
#endif
#if DFS_FILESYSTEM_TYPES_MAX < 4
    #error "Please define DFS_FILESYSTEM_TYPES_MAX more than 4"
#endif

#include <dfs_fs.h>
#ifdef BSP_USING_SDCARD_FS
    #include <board.h>
    #include "drv_sdio.h"
#endif
#ifdef BSP_USING_SPI_FLASH_FS
    #include "fal.h"
#endif
#define DBG_TAG "app.filesystem"
#define DBG_LVL DBG_INFO
#include <rtdbg.h>

#ifdef RT_USING_DFS_ROMFS   /* 默认不使用 ROMFS */
#include "dfs_romfs.h"
static const struct romfs_dirent _romfs_root[] =
{
    {ROMFS_DIRENT_DIR, "flash", RT_NULL, 0},
    {ROMFS_DIRENT_DIR, "sdcard", RT_NULL, 0}
};

const struct romfs_dirent romfs_root =
{
    ROMFS_DIRENT_DIR, "/", (rt_uint8_t *)_romfs_root, sizeof(_romfs_root) / sizeof(_romfs_root[0])
};
#endif

#ifdef BSP_USING_SDCARD_FS

/* SD Card hot plug detection pin */
#define SD_CHECK_PIN GET_PIN(D, 5)

static void _sdcard_mount(void)
{
    rt_device_t device;

    device = rt_device_find("sd0");

    if (device == NULL)
    {
        mmcsd_wait_cd_changed(0);
        sdcard_change();
        mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
        device = rt_device_find("sd0");
    }

    if (device != RT_NULL)
    {
        if (dfs_mount("sd0", "/sdcard", "elm", 0, 0) == RT_EOK)
        {
            LOG_I("sd card mount to '/sdcard'");
        }
        else
        {
            LOG_W("sd card mount to '/sdcard' failed!");
        }
    }
}

static void _sdcard_unmount(void)
{
    rt_thread_mdelay(200);
    dfs_unmount("/sdcard");
    LOG_I("Unmount \"/sdcard\"");

    mmcsd_wait_cd_changed(0);
    sdcard_change();
    mmcsd_wait_cd_changed(RT_WAITING_FOREVER);
}

static void sd_mount(void *parameter)
{
    rt_uint8_t re_sd_check_pin = 1;
    rt_thread_mdelay(200);

    if (rt_pin_read(SD_CHECK_PIN))
    {
        _sdcard_mount();
    }

    while (1)
    {
        rt_thread_mdelay(200);

        if (!re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) != 0)
        {
            _sdcard_mount();
        }

        if (re_sd_check_pin && (re_sd_check_pin = rt_pin_read(SD_CHECK_PIN)) == 0)
        {
            _sdcard_unmount();
        }
    }
}

#endif /* BSP_USING_SDCARD_FS */

#define BLK_DEV_NAME  "filesystem"

int mnt_spi_flash_init(void)    /* 外部SPI Flash 分区挂载 */
{
    rt_thread_delay(RT_TICK_PER_SECOND);
    fal_blk_device_create(BLK_DEV_NAME);

    if (dfs_mount(BLK_DEV_NAME, "/", "elm", 0, 0) == 0)
    {
        rt_kprintf(BLK_DEV_NAME" mount to '/'\n");
    }
    else
    {
        if(dfs_mkfs("elm", BLK_DEV_NAME) == 0)
        {
            if (dfs_mount(BLK_DEV_NAME, "/", "elm", 0, 0) == 0)
            {
                rt_kprintf(BLK_DEV_NAME" mount to '/'\n");
            }
            else
            {
                rt_kprintf(BLK_DEV_NAME" mount to '/' failed!\n");
            }
        }
        else
        {
            rt_kprintf(BLK_DEV_NAME" mkfs failed!");
        }
    }

    return 0;
}

int mount_init(void)
{
#ifdef RT_USING_DFS_ROMFS /* 默认不使用ROMFS */
    if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
    {
        LOG_E("rom mount to '/' failed!");
    }
#endif
#ifdef BSP_USING_SPI_FLASH_FS
    mnt_spi_flash_init(); /* 挂载 SPI Flash 为 根目录 */
#if 0 /* 以下为 lfs 挂载,注释掉 */
    struct rt_device *flash_dev = RT_NULL;

    #ifndef RT_USING_WIFI
    fal_init();
    #endif

    flash_dev = fal_mtd_nor_device_create("filesystem");

    if (flash_dev)
    {
        //mount filesystem
        if (dfs_mount(flash_dev->parent.name, "/", "elm", 0, 0) != 0)
        {
            LOG_W("mount to '/' failed! try to mkfs %s", flash_dev->parent.name);
            dfs_mkfs("lfs", flash_dev->parent.name);

            if (dfs_mount(flash_dev->parent.name, "/flash", "lfs", 0, 0) == 0)
            {
                LOG_I("mount to '/flash' success!");
            }
        }
        else
        {
            LOG_I("mount to '/flash' success!");
        }
    }
    else
    {
        LOG_E("Can't create  block device  filesystem or bt_image partition.");
    }
#endif
#endif

    #ifdef BSP_USING_SDCARD_FS
    rt_thread_t tid;

    rt_pin_mode(SD_CHECK_PIN, PIN_MODE_INPUT_PULLUP);

    tid = rt_thread_create("sd_mount", sd_mount, RT_NULL,
                           2048, RT_THREAD_PRIORITY_MAX - 2, 20);

    if (tid != RT_NULL)
    {
        rt_thread_startup(tid);
    }
    else
    {
        LOG_E("create sd_mount thread err!");
    }

    #endif
    return RT_EOK;
}
INIT_APP_EXPORT(mount_init);

#endif /* BSP_USING_FS */
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 32
  • 33
  • 34
  • 35
  • 36
  • 37
  • 38
  • 39
  • 40
  • 41
  • 42
  • 43
  • 44
  • 45
  • 46
  • 47
  • 48
  • 49
  • 50
  • 51
  • 52
  • 53
  • 54
  • 55
  • 56
  • 57
  • 58
  • 59
  • 60
  • 61
  • 62
  • 63
  • 64
  • 65
  • 66
  • 67
  • 68
  • 69
  • 70
  • 71
  • 72
  • 73
  • 74
  • 75
  • 76
  • 77
  • 78
  • 79
  • 80
  • 81
  • 82
  • 83
  • 84
  • 85
  • 86
  • 87
  • 88
  • 89
  • 90
  • 91
  • 92
  • 93
  • 94
  • 95
  • 96
  • 97
  • 98
  • 99
  • 100
  • 101
  • 102
  • 103
  • 104
  • 105
  • 106
  • 107
  • 108
  • 109
  • 110
  • 111
  • 112
  • 113
  • 114
  • 115
  • 116
  • 117
  • 118
  • 119
  • 120
  • 121
  • 122
  • 123
  • 124
  • 125
  • 126
  • 127
  • 128
  • 129
  • 130
  • 131
  • 132
  • 133
  • 134
  • 135
  • 136
  • 137
  • 138
  • 139
  • 140
  • 141
  • 142
  • 143
  • 144
  • 145
  • 146
  • 147
  • 148
  • 149
  • 150
  • 151
  • 152
  • 153
  • 154
  • 155
  • 156
  • 157
  • 158
  • 159
  • 160
  • 161
  • 162
  • 163
  • 164
  • 165
  • 166
  • 167
  • 168
  • 169
  • 170
  • 171
  • 172
  • 173
  • 174
  • 175
  • 176
  • 177
  • 178
  • 179
  • 180
  • 181
  • 182
  • 183
  • 184
  • 185
  • 186
  • 187
  • 188
  • 189
  • 190
  • 191
  • 192
  • 193
  • 194
  • 195
  • 196
  • 197
  • 198
  • 199
  • 200
  • 201
  • 202
  • 203
  • 204
  • 205
  • 206
  • 207
  • 208
  • 209
  • 210
  • 211
  • 212
  • 213
  • 214
  • 215
  • 216
  • 217
  • 218

编译下载

   ___  ______  _____         ______  _   ______  _____  _____  _____ 
  / _ \ | ___ \|_   _|        | ___ \(_)  | ___ \/  _  \/  _  \|_   _|
 / /_\ \| |_/ /  | |   ______ | |_/ / _   | |_/ /| | | || | | |  | |  
 |  _  ||    /   | |  |______||  __/ | |  | ___ \| | | || | | |  | |  
 | | | || |\ \   | |          | |    | |  | |_/ /\ \_/ /\ \_/ /  | |  
 \_| |_/\_| \_|  \_/          \_|    |_|  \____/  \___/  \___/   \_/  

 Powered by RT-Thread.

[D/drv.sdram] sdram init success, mapped at 0xC0000000, size is 33554432 bytes, data width is 16

 \ | /
- RT -     Thread Operating System
 / | \     4.1.0 build Mar 31 2022 23:53:12
 2006 - 2022 Copyright by RT-Thread team
[I/SFUD] Find a Winbond flash chip. Size is 16777216 bytes.
[I/SFUD] norflash0 flash device is initialize success.
[I/SFUD] Probe SPI flash norflash0 by SPI device spi10 success.
[D/FAL] (fal_flash_init:47) Flash device |                norflash0 | addr: 0x00000000 | len: 0x01000000 | blk_size: 0x00001000 |initialized finish.
[I/FAL] ==================== FAL partition table ====================
[I/FAL] | name       | flash_dev |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | wifi_image | norflash0 | 0x00000000 | 0x00080000 |
[I/FAL] | bt_image   | norflash0 | 0x00080000 | 0x00080000 |
[I/FAL] | download   | norflash0 | 0x00100000 | 0x00200000 |
[I/FAL] | easyflash  | norflash0 | 0x00300000 | 0x00100000 |
[I/FAL] | filesystem | norflash0 | 0x00400000 | 0x00c00000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer initialize success.
[I/FAL] The FAL block device (filesystem) created successfully
filesystem mount to '/'
  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 19
  • 20
  • 21
  • 22
  • 23
  • 24
  • 25
  • 26
  • 27
  • 28
  • 29
  • 30
  • 31
  • 注意这个工程,需要先烧写 bootloader,使用ART-Pi 的SDK bootloader工程 sdk-bsp-stm32h750-realthread-artpi\projects\art_pi_bootloader
  • 发现挂载成功了
msh />df
disk free: 11.9 MB [ 3063 block, 4096 bytes per block ]
  • 1
  • 2
  • 发现可以创建目录与文件
msh />ls
Directory /:

msh />mkdir logs

msh />cd logs
msh /logs>ls
Directory /logs:

msh /logs>echo aaa log.txt
msh /logs>ls
Directory /logs:
log.txt             3                        

msh /logs>cat log.txt
aaa
msh /logs>

  • 1
  • 2
  • 3
  • 4
  • 5
  • 6
  • 7
  • 8
  • 9
  • 10
  • 11
  • 12
  • 13
  • 14
  • 15
  • 16
  • 17
  • 18
  • 自此,ART-Pi 外部16MB SPI Flash的12MB 分区,成功挂载为 elm : FatFS 文件系统了

小结

  • 熟悉RT-Thread 文件系统的框架
  • 熟悉elm FatFS 文件系统的配置,如扇区(sector)大小的设置
声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/知新_RL/article/detail/464081
推荐阅读
相关标签
  

闽ICP备14008679号