RT-Thread基于AT32单片机的Flash应用

1 硬件电路

RT-Thread基于AT32单片机的Flash应用_第1张图片

2 RT-Thread配置

RT-Thread内置FAL(flash抽象层),支持MCU内置Flash和SPI Flash,使用SFUD(串行 Flash 通用驱动库)。

JEDEC SFDP标准可以自动探测芯片的参数,目前市场上的SPI Flash芯片一般都支持。如果不支持的话,可以通过查表的方式进行配置。

FAL配置

RT-Thread基于AT32单片机的Flash应用_第2张图片

SPI和SFUD配置 

RT-Thread基于AT32单片机的Flash应用_第3张图片

3 软件编写

在drivers目录下添加

分区配置文件fal_cfg.h

#ifndef _FAL_CFG_H_
#define _FAL_CFG_H_

#include 
#include 

#include "drv_flash.h"
#include "fal.h"

#ifdef __cplusplus
extern "C" {
#endif


extern const struct fal_flash_dev at32_onchip_flash;
extern struct fal_flash_dev nor_flash0;

/* flash device table */
#ifdef FAL_USING_NOR_FLASH_DEV_NAME
#define FAL_FLASH_DEV_TABLE                                          \
{                                                                    \
    &at32_onchip_flash,                                              \
    &nor_flash0,                                                     \
}
#else
#define FAL_FLASH_DEV_TABLE                                          \
{                                                                    \
    &at32_onchip_flash,                                              \
}
#endif


/* ====================== Partition Configuration ========================== */
#ifdef FAL_PART_HAS_TABLE_CFG

#ifdef BSP_USING_AT_SPIM

/* partition table */
#define FAL_PART_TABLE                                                                       \
{                                                                                            \
    {FAL_PART_MAGIC_WROD,        "param", "onchip_flash",       0,           128 * 1024, 0}, \
    {FAL_PART_MAGIC_WROD,        "log",   "onchip_flash",       128 * 1024,  SPIM_FLASH_SIZE_MB * 1024 * 1024, 0}, \
}

#else
/* partition table */
#define FAL_PART_TABLE                                                                       \
{                                                                                            \
    {FAL_PART_MAGIC_WROD,        "app",   "onchip_flash",       0,           250 * 1024, 0}, \
    {FAL_PART_MAGIC_WROD,        "param", "onchip_flash",       250 * 1024,    6 * 1024, 0}, \
    {FAL_PART_MAGIC_WORD,      "flashdb", FAL_USING_NOR_FLASH_DEV_NAME,         0,           512 * 1024, 0}, \
    {FAL_PART_MAGIC_WORD,     "download", FAL_USING_NOR_FLASH_DEV_NAME,         512 * 1024,  512 * 1024, 0}, \
}


#endif


#endif /* FAL_PART_HAS_TABLE_CFG */

#ifdef __cplusplus
extern "C" {
#endif


#endif /* _FAL_CFG_H_ */

SPI/SFUD和FAL初始化 component_init.c 

#include 
#include 

/******************************************************************************
SPI_FLASH
*/
#ifdef BSP_USING_SPI_FLASH
#include "spi_flash.h"
#include "spi_flash_sfud.h"
#include "drv_spi.h"

static int rt_hw_spi_flash_init(void)
{
    rt_hw_spi_device_attach(SPI_FLASH_BUS_NAME, SPI_FLASH_DEVICE_NAME, GPIOB, GPIO_PINS_12);  // CS 脚:PB12

    /* 使用 SFUD 探测 spix 从设备,并将 spix0 连接的 flash 初始化为块设备,名称 */
    if (RT_NULL == rt_sfud_flash_probe(FAL_USING_NOR_FLASH_DEV_NAME, SPI_FLASH_DEVICE_NAME))
    {
        LOG_E("find sfud_flash failed!...\n");
        return -RT_ERROR;
    }

    return RT_EOK;
}
INIT_COMPONENT_EXPORT(rt_hw_spi_flash_init);
#endif

#ifdef RT_USING_FAL
#include "fal.h"

static int rt_hw_onchip_flash_init(void)
{
#ifdef BSP_USING_AT_SPIM
    at32_msp_spim_init();
#endif
    return fal_init();
}
INIT_COMPONENT_EXPORT(rt_hw_onchip_flash_init);
#endif

4 测试结果

RT-Thread基于AT32单片机的Flash应用_第4张图片

fal bench测试结果

msh >fal probe flashdb
Probed a flash partition | flashdb | flash_dev: norflash0 | offset: 0 | len: 524288 |.
msh >fal bench
Usage: fal bench              - benchmark test with per block size.
msh >fal bench 1024
DANGER: It will erase full chip or partition! Please run 'fal bench 1024 yes'.
msh >fal bench 1024 yes
Erasing 524288 bytes data, waiting...
Erase benchmark success, total time: 0.665S.
Writing 524288 bytes data, waiting...
Write benchmark success, total time: 2.048S.
Reading 524288 bytes data, waiting...
Read benchmark success, total time: 0.730S.

你可能感兴趣的:(掌上实验室V9,RT-Thread,单片机,嵌入式硬件)