在RT-Thread ulog 组件中基于DS3231开启日志文件系统

参考文章:

RT-Thread Studio ulog日志

 RT-thread应用讲解——easyflash

ULOG 创建多个文件后端并保存不同日志方法

RT-Thread完整版fal及easyflash移植

前提是已经实现了SPI Flash文件系统挂载并能够读写,下面是操作步骤:

1,按下图方式 开启日志

在RT-Thread ulog 组件中基于DS3231开启日志文件系统_第1张图片

 在RT-Thread ulog 组件中基于DS3231开启日志文件系统_第2张图片

2, LOG选项使能 “在Flash上保存日志” 并配置保存日志区域的大小(255*4096)= 144480,而我们也可以看到下面的 “ulog_easyflash”选项也已经开启 。ulog_easylash是相当于上面选项Enable file backend,是ulog和easyflash之间的连接纽带。(注意:保存区域大小一定和fal_cfg.h文件中相同分区名的大小相同)

 在RT-Thread ulog 组件中基于DS3231开启日志文件系统_第3张图片

3,这个时候保存后直接编译,会产生ef_log_debug等未定义错误,原因是easy_flash/port目录下接口文件还未加入,如下图所示

 ​​​​​在RT-Thread ulog 组件中基于DS3231开启日志文件系统_第4张图片

 然后将上面提示的ef_sfud_port.c复制到上级目录的src目录下。然后重新编译,如果没有意外,编译通过。

4,将SFUD Flash设备对象关联到flash系统

将ef_port_init()的引用定义由w25q64修改成sfud_dev,如下:

EfErrCode ef_port_init(ef_env const **default_env, size_t *default_env_size) {
    EfErrCode result = EF_NO_ERR;

    *default_env = default_env_set;
    *default_env_size = sizeof(default_env_set) / sizeof(default_env_set[0]);

    rt_sem_init(&env_cache_lock, "env lock", 1, RT_IPC_FLAG_PRIO);

    extern rt_spi_flash_device_t sfud_dev;
    flash = (sfud_flash_t)(sfud_dev->user_data);

    return result;
}

然后在spi_flash硬件初始化部分加入ULOG_EASYFLASH_BACKEND_ENABLE条件定义,代码如下:

rt_spi_flash_device_t sfud_dev = RT_NULL;
static int rt_hw_spi_flash_init(void)
{
    SPI_FLASH_CS_PORT_CLK_EN();
    rt_hw_spi_device_attach("spi1", "spi10", SPI_FLASH_CS_PORT, SPI_FLASH_CS_PIN);
    sfud_dev = rt_sfud_flash_probe(NOR_FLASH_DEV_NAME, "spi10");
    if (RT_NULL == sfud_dev)
    {
        rt_kprintf("The sfud flash can't find flash device!\n");
        return -RT_ERROR;
    }
#ifdef ULOG_EASYFLASH_BACKEND_ENABLE
    easyflash_init();
#endif

    return RT_EOK;
}
INIT_DEVICE_EXPORT(rt_hw_spi_flash_init);

然后重新编译,下载运行,出现Can't find a RTC device 错误。

5,使用DS3231实现ulog的时间戳功能

使能ds3231软件包,如下:

保证如下I2C总线和RTC设备驱动被启用。

 定义I2C硬件接口

然后保存,下载软件包并重新编译。

之所以出现can't find RTC device错误提示,是因为ulog组件启动初始化时,优先级高于I2C,RTC,DS3231组件 。ulog_init是在INIT_BOARD_EXPORT(ulog_init);而DS3231是在INIT_DEVICE_EXPORT(rt_hw_ds3231_init)。DS3231需要通过I2C总线访问寄存器,I2C总线访问需要互斥信号量,此时系统还未开调度,无发启动互相信号量。但是在随后的过程中始终功能是可以成功启用的。

6,Log sector header error

出现问题:

[Flash] (packages\EasyFlash-v4.1.0\src\ef_env.c:1818) ENV start address is 0x00000000, size is 8192 bytes.
[Flash] Warning: Sector header check failed. Format this sector (0x00000000).
06 15:27:08 I/SFUD: Find a Winbond flash chip. Size is 16777216 bytes.
07-06 15:27:08 I/SFUD: norflash0 flash device is initialize success.
07-06 15:27:08 I/SFUD: Probe SPI flash norflash0 by SPI device spi10 success.
07-06 15:27:08 I/utest: utest is initialize success.
07-06 15:27:08 I/utest: total utest testcase num: (2)
[Flash] Warning: Sector header check failed. Format this sector (0x00001000).
[Flash] Warning: All sector header check failed. Set it to default.
[Flash] (packages\EasyFlash-v4.1.0\src\ef_log.c:359) Error: Log sector header error! Now will clean all log area.
[Flash] EasyFlash V4.1.0 is initialize success.
[Flash] You can get the latest version on https://github.com/armink/EasyFlash .

经过反复研究和尝试,得出结论,ulog easyflash需要一个单独分区,并且这分区需要大于上面设置的保存日志大小的最小擦除粒度(1044480)。

#define FAL_PART_TABLE                                                                                  \
{                                                                                                       \
    {FAL_PART_MAGIC_WORD, ONCHIP_FLASH_PARTITION_NAME, ONCHIP_FLASH_DEV_NAME, ONCHIP_FLASH_ADDR_OFFSET,  2*FLASH_SIZE_GRANULARITY_128K, 0}, \
    /*{FAL_PART_MAGIC_WORD,       "app",     "onchip_flash",            0,  FLASH_SIZE_GRANULARITY_64K, 0},*/  \
    {FAL_PART_MAGIC_WORD, "easyflash", NOR_FLASH_DEV_NAME,            0,  2*1024*1024, 0}, \
    /*{FAL_PART_MAGIC_WORD, "download",  NOR_FLASH_DEV_NAME,    1024*1024,  1024*1024, 0},*/ \
    {FAL_PART_MAGIC_WORD, SPI_FLASH_PARTITION_NAME, NOR_FLASH_DEV_NAME, 2*1024*1024,  12*1024*1024, 0}, \
}

设置后,首次运行还会出现此错误,下次运行就不会出现了。

 \ | /
- RT -     Thread Operating System
 / | \     4.1.1 build Jul  5 2023 18:50:08
 2006 - 2022 Copyright by RT-Thread team
The rtc of ds3231 init succeed!
07-[D/FAL] (fal_flash_init:47) Flash device |        onchip_flash_128k | addr: 0x08020000 | len: 0x000e0000 | blk_size: 0x00020000 |initialized finish.
[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] | onchip_lfs | onchip_flash_128k | 0x000a0000 | 0x001c0000 |
[I/FAL] | easyflash  | norflash0         | 0x00000000 | 0x00200000 |
[I/FAL] | sflash_elm | norflash0         | 0x00200000 | 0x00c00000 |
[I/FAL] =============================================================
[I/FAL] RT-Thread Flash Abstraction Layer initialize success.
[Flash] (packages\EasyFlash-v4.1.0\src\ef_env.c:1818) ENV start address is 0x00000000, size is 8192 bytes.
[Flash] Warning: Sector header check failed. Format this sector (0x00000000).
06 15:38:21 I/SFUD: Find a Winbond flash chip. Size is 16777216 bytes.
07-06 15:38:21 I/SFUD: norflash0 flash device is initialize success.
07-06 15:38:21 I/SFUD: Probe SPI flash norflash0 by SPI device spi10 success.
07-06 15:38:21 I/utest: utest is initialize success.
07-06 15:38:21 I/utest: total utest testcase num: (2)
[Flash] Warning: Sector header check failed. Format this sector (0x00001000).
[Flash] Warning: All sector header check failed. Set it to default.
[Flash] (packages\EasyFlash-v4.1.0\src\ef_log.c:359) Error: Log sector header error! Now will clean all log area.
[Flash] EasyFlash V4.1.0 is initialize success.
[Flash] You can get the latest version on https://github.com/armink/EasyFlash .
[I/FAL] =============env boot================================
[I/FAL] The system now boot 1 times
[I/FAL] =============env boot================================
[I/FAL] The FAL MTD NOR device (onchip_lfs) created successfully
[I/FAL] The FAL block device (sflash_elm) created successfully
msh />07-06 15:38:25 I/SFUD: Onchip flash mount to '/cf'
07-06 15:38:25 I/SFUD: spi flash mount to '/sf'

问题:这样设置之后,日志虽然保存到flash里面去,但如何得到此日志文件?

你可能感兴趣的:(实时音视频,单片机,stm32,嵌入式硬件)