dfs_mkfs() 调用format error

发布于 2021-02-28 21:55:46

背景:
外挂SPI flash,W25Q256,SFUD/FAL都没啥问题,具体见启动打印:

 \ | /
- RT -     Thread Operating System
 / | \     3.1.4 build Feb 28 2021
 2006 - 2019 Copyright by rt-thread team
rt_hw_spi_device_attach succ! 
[SFUD] Find a Winbond flash chip. Size is 33554432 bytes.
[SFUD] W25Q256 flash device is initialize success.
[D/FAL] (fal_flash_init:61) Flash device |                  W25Q256 | addr: 0x00000000 | len: 0x02000000 | blk_size: 0x00001000 |initialized finish.
[21:32:45.476] [I/FAL] ==================== FAL partition table ====================
[I/FAL] | name      | flash_dev |   offset   |    length  |
[I/FAL] -------------------------------------------------------------
[I/FAL] | easyflash | W25Q256   | 0x00000000 | 0x00200000 |
[I/FAL] | download  | W25Q256   | 0x00200000 | 0x02000000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer (V0.5.0) initialize success.
ROMFS File System initialized!
[I/FAL] The FAL MTD NOR device (download) created successfully
format error
mkfs  failed! 
msh />device           type         ref count
-------- -------------------- ----------
download MTD Device           0       
W25Q256  Block Device         0       
spi10    SPI Device           0       
spi1     SPI Bus              0       
uart3    Character Device     2       
pin      Miscellaneous Device 0  

挂载DFS的时候先mkfs,发现在dfs_elm_mkfs()中调用f_mkfs()的时候出错了,返回(0x01 FR_DISK_ERR,A hard error occurred in the low level disk I/O layer)

操作代码如下:

int rt_hw_spi_flash_init(void)
{
    int ret;
    __HAL_RCC_GPIOA_CLK_ENABLE();
    ret = rt_hw_spi_device_attach("spi1", "spi10", GPIOA, GPIO_PIN_4);
    if (ret != RT_EOK)
    {
    rt_kprintf("rt_hw_spi_device_attach failed! \n");
     return ret;
    }
    else
    {
    rt_kprintf("rt_hw_spi_device_attach succ! \n");
    }

    if (RT_NULL == rt_sfud_flash_probe("W25Q256", "spi10"))
    {
     rt_kprintf("rt_sfud_flash_probe failed! \n");
        return -RT_ERROR;
    }
        
    fal_init();
        
    return RT_EOK;
}

int mount_init(void)
{
    struct rt_device *flash_dev = RT_NULL;
    
    /* mount ROMFS as root directory */
    if (dfs_mount(RT_NULL, "/", "rom", 0, (const void *)DFS_ROMFS_ROOT) == 0)
    {
        rt_kprintf("ROMFS File System initialized!\n");
    }
    else
    {
        rt_kprintf("ROMFS File System initialized Failed!\n");
    }

    flash_dev = fal_mtd_nor_device_create("download");
    if (flash_dev)
    {
    // mkfs firstly
    if (dfs_mkfs("elm", "download") != 0)
    {
       rt_kprintf("mkfs  failed! \n");
       return 1;
    }
            
        //mount filesystem
        if (dfs_mount("download", "/flash", "elm", 0, 0) != 0)
        {
            rt_kprintf("mount to '/flash' failed! \n");
        return 1;
        }
        else
        {
            rt_kprintf("mount to '/flash' success! \n");
        }
    }
    else
    {
        rt_kprintf("Can't create  block device \n");
    }
    return RT_EOK;
}
INIT_APP_EXPORT(mount_init);

问题:
因为f_mkfs的具体实现没找到,如果如描述的low level disk I/O layer error的话,为什么FAL启动的时候不提示错误?设备的r/w等函数都是SFUD/FAL自带的,不大可能唯独我的代码出错啊~

查看更多

关注者
0
被浏览
146
2 个回答
Juggernaut
Juggernaut 2021-02-28

跟进f_mkfs()stat = disk_initialize(pdrv);这个返回0是正常,第一次调用disk_ioctl()返回的时候sz_blk的值为1,第2次调用disk_ioctl()返回的时候ss的值为0;

    /* Check physical drive status */
    stat = disk_initialize(pdrv);
    if (stat & STA_NOINIT) return FR_NOT_READY;
    if (stat & STA_PROTECT) return FR_WRITE_PROTECTED;
    if (disk_ioctl(pdrv, GET_BLOCK_SIZE, &sz_blk) != RES_OK || !sz_blk || sz_blk > 32768 || (sz_blk & (sz_blk - 1))) sz_blk = 1;    /* Erase block to align data area */
#if _MAX_SS != _MIN_SS        /* Get sector size of the medium */
    if (disk_ioctl(pdrv, GET_SECTOR_SIZE, &ss) != RES_OK) return FR_DISK_ERR;
    if (ss > _MAX_SS || ss < _MIN_SS || (ss & (ss - 1))) return FR_DISK_ERR;

跟进去看了下,在disk_ioctl()函数执行如下代码的时候geometry没有拿到数据~
SFUD/FAL的大神能否帮忙看看

    else if (ctrl == GET_SECTOR_SIZE)
    {
        struct rt_device_blk_geometry geometry;

        rt_memset(&geometry, 0, sizeof(geometry));
        rt_device_control(device, RT_DEVICE_CTRL_BLK_GETGEOME, &geometry);

        *(WORD *)buff = (WORD)(geometry.bytes_per_sector);
    }
whj467467222
whj467467222 认证专家 2021-03-01

看样子你是想对 FLASH 进行分区,然后挂载文件系统。
这个代码在 ART-Pi 的 SDK 拷贝出来的,验证通过的,你可以自己去下载一份看看其他的配置。

参考一下这个的实现吧.

/*
 * Copyright (c) 2006-2018, 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

#ifdef BSP_USING_SPI_FLASH_FS
#include "fal.h"
#endif

#include <dfs_fs.h>
#include "dfs_romfs.h"
#include "drv_sdio.h"

#define DBG_TAG "app.filesystem"
#define DBG_LVL DBG_INFO
#include <rtdbg.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])};

#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 */

int mount_init(void)
{
    if (dfs_mount(RT_NULL, "/", "rom", 0, &(romfs_root)) != 0)
    {
        LOG_E("rom mount to '/' failed!");
    }
#ifdef BSP_USING_SPI_FLASH_FS
    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, "/flash", "lfs", 0, 0) != 0)
        {
            LOG_W("mount to '/flash' 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

#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 */

撰写答案

请登录后再发布答案,点击登录

发布
问题

分享
好友

手机
浏览

扫码手机浏览