背景:
外挂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] [32;22m[I/FAL] ==================== FAL partition table ====================[0m
[32;22m[I/FAL] | name | flash_dev | offset | length |[0m
[32;22m[I/FAL] -------------------------------------------------------------[0m
[32;22m[I/FAL] | easyflash | W25Q256 | 0x00000000 | 0x00200000 |[0m
[32;22m[I/FAL] | download | W25Q256 | 0x00200000 | 0x02000000 |[0m
[32;22m[I/FAL] =============================================================[0m
[32;22m[I/FAL] RT-Thread Flash Abstraction Layer (V0.5.0) initialize success.[0m
ROMFS File System initialized!
[32;22m[I/FAL] The FAL MTD NOR device (download) created successfully[0m
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
自带的,不大可能唯独我的代码出错啊~
看样子你是想对 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 */
跟进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);
}
再来个SFUD加debug的log,明明init的时候正常的啊
>
\ | /
- RT - Thread Operating System
/ | \ 3.1.4 build Mar 1 2021
2006 - 2019 Copyright by rt-thread team
rt_hw_spi_device_attach succ!
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud.c:861) The flash device manufacturer ID is 0xEF, memory type ID is 0x40, capacity ID is 0x19.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:131) Check SFDP header is OK. The reversion is V1.0, NPN is 0.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:173) Check JEDEC basic flash parameter header is OK. The table id is 0, reversion is V1.0, length is 9, parameter table pointer is *[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:203) JEDEC basic flash parameter table info:
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:204) MSB-LSB 3 2 1 0
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0001] 0xFF 0xF3 0x20 0xE5
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0002] 0x0F 0xFF 0xFF 0xFF
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0003] 0x6B 0x08 0xEB 0x44
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0004] 0xBB 0x42 0x3B 0x08
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0005] 0xFF 0xFF 0xFF 0xFE
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0006] 0x00 0x00 0xFF 0xFF
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0007] 0xEB 0x21 0xFF 0xFF
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0008] 0x52 0x0F 0x20 0x0C
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:206) [0009] 0x00 0x00 0xD8 0x10
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:215) 4 KB Erase is supported throughout the device. Command is 0x20.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:234) Write granularity is 64 bytes or larger.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:245) Target flash status register is non-volatile.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:276) 3- or 4-Byte addressing.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:305) Capacity is 33554432 Bytes.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:311) Flash device supports 4KB block erase. Command is 0x20.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:311) Flash device supports 32KB block erase. Command is 0x52.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud_sfdp.c:311) Flash device supports 64KB block erase. Command is 0xD8.
[SFUD] Find a Winbond flash chip. Size is 33554432 bytes.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud.c:840) Flash device reset success.
[SFUD] (..\..\..\components\drivers\spi\sfud\src\sfud.c:941) Enter 4-Byte addressing mode success.
[SFUD] W25Q256 flash device is initialize success.
[SFUD] Probe SPI flash W25Q256 by SPI device spi10 success.
[D/FAL] (fal_flash_init:61) Flash device | W25Q256 | addr: 0x00000000 | len: 0x02000000 | blk_size: 0x00001000 |initialized finish.
[32;22m[I/FAL] ==================== FAL partition table ====================[0m
[32;22m[I/FAL] | name | flash_dev | offset | length |[0m
[32;22m[I/FAL] -------------------------------------------------------------[0m
[32;22m[I/FAL] | easyflash | W25Q256 | 0x00000000 | 0x00200000 |[0m
[32;22m[I/FAL] | download | W25Q256 | 0x00200000 | 0x02000000 |[0m
[32;22m[I/FAL] =============================================================[0m
[32;22m[I/FAL] RT-Thread Flash Abstraction Layer (V0.5.0) initialize success.[0m
ROMFS File System initialized!
[32;22m[I/FAL] The FAL MTD NOR device (download) created successfully[0m
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
感谢回复!
按照你的建议改了代码,如下:
打印的log如下:
@Juggernaut 很明显你根目录的
romfs
就挂载失败了,一步一步的来解决@whj467467222 感谢大佬回复,
ROMFS File System initialized!
这个log显示romfs是OK的