FLASH

介绍

对于不同底层驱动的flash操作实现,统一封装成本文所述hal flash接口。 hal相关头文件位于目录:include/aos/hal hal相关实现位于具体的mcu目录下,如:platform/mcu/stm32f1xx/hal

API列表

函数名称 功能描述
hal_flash_info_get 获取指定区域的FLASH信息
hal_flash_erase 擦除FLASH的指定区域
hal_flash_write 写FLASH的指定区域
hal_flash_erase_write 先擦除再写FLASH的指定区域
hal_flash_read 读FLASH的指定区域

API详情

相关宏定义

#define PAR_OPT_READ_POS  ( 0 )
#define PAR_OPT_WRITE_POS ( 1 )

#define PAR_OPT_READ_MASK  ( 0x1u << PAR_OPT_READ_POS )
#define PAR_OPT_WRITE_MASK ( 0x1u << PAR_OPT_WRITE_POS )

#define PAR_OPT_READ_DIS  ( 0x0u << PAR_OPT_READ_POS )
#define PAR_OPT_READ_EN   ( 0x1u << PAR_OPT_READ_POS )
#define PAR_OPT_WRITE_DIS ( 0x0u << PAR_OPT_WRITE_POS )
#define PAR_OPT_WRITE_EN  ( 0x1u << PAR_OPT_WRITE_POS )

相关数据结构

hal_logic_partition_t

typedef struct {
    hal_flash_t partition_owner;
    const char *partition_description;
    uint32_t    partition_start_addr;
    uint32_t    partition_length;
    uint32_t    partition_options;
} hal_logic_partition_t;

hal_flash_err_t

typedef enum {
    HAL_FLASH_ERR_OK,           /* operation success */
    HAL_FLASH_ERR_NAND_BAD,     /* Bad block */
    HAL_FLASH_ERR_NAND_READ,    /* Read fail, can't correct */
    HAL_FLASH_ERR_NAND_WRITE,   /* Write fail */
    HAL_FLASH_ERR_NAND_ERASE,   /* Erase fail */
    HAL_FLASH_ERR_NAND_FLIPS,   /* Too many bitflips, uncorrected */
    /* add more hereafter */
} hal_flash_err_t;

hal_flash_t

typedef enum {
    HAL_FLASH_EMBEDDED,
    HAL_FLASH_SPI,
    HAL_FLASH_QSPI,
    HAL_FLASH_MAX,
    HAL_FLASH_NONE,
} hal_flash_t;

hal_partition_t

typedef enum {
    HAL_PARTITION_ERROR = -1,
    HAL_PARTITION_BOOTLOADER,   /**< Bootloader partition index */
    HAL_PARTITION_APPLICATION,  /**< App partition index; Or OTA A partition */
    HAL_PARTITION_ATE,          /**< For ATE */
    HAL_PARTITION_OTA_TEMP,     /**< For OTA upgrade */
    HAL_PARTITION_RF_FIRMWARE,  /**< For RF firmware */
    HAL_PARTITION_PARAMETER_1,  /**< For OTA args */
    HAL_PARTITION_PARAMETER_2,  /**< For kv storage */
    HAL_PARTITION_PARAMETER_3,  /**< For User defined */
    HAL_PARTITION_PARAMETER_4,  /**< Used by security */
    HAL_PARTITION_BT_FIRMWARE,  /**< For bt firmware */
    HAL_PARTITION_SPIFFS,       /**< For spiffs file system */
    HAL_PARTITION_LITTLEFS,
    HAL_PARTITION_LITTLEFS2,
    HAL_PARTITION_LITTLEFS3,
    HAL_PARTITION_CUSTOM_1,     /**< For User defined */
    HAL_PARTITION_CUSTOM_2,     /**< For User defined */
    HAL_PARTITION_2ND_BOOT,     /**< For 2nd boot */
    HAL_PARTITION_MBINS_APP,    /**< For app bin when muti bins */
    HAL_PARTITION_MBINS_KERNEL, /**< For kernel bin when muti bins */
    HAL_PARTITION_OTA_SUB,      /**< For Sub device OTA */
    HAL_PARTITION_GPT,
    HAL_PARTITION_ENV,
    HAL_PARTITION_ENV_REDUND,
    HAL_PARTITION_RTOSA,
    HAL_PARTITION_RTOSB,
    HAL_PARTITION_BOOT1,
    HAL_PARTITION_BOOT1_REDUND,
    HAL_PARTITION_MAX,
    HAL_ALL_FLASH_0,           /**< For all flash0 operation */
    HAL_ALL_FLASH_1,           /**< For all flash1 operation */
    HAL_ALL_FLASH_2,           /**< For all flash2 operation */
    HAL_PARTITION_NONE,
} hal_partition_t;

hal_flash_info_get

获取指定区域的FLASH信息

函数原型

int32_t hal_flash_info_get(hal_partition_t in_partition, hal_logic_partition_t *partition);

参数

参数名称 出入参 参数描述 参数示例
hal_partition_t in_partition 入参 FLASH分区号 HAL_PARTITION_APPLICATION
hal_logic_partition_t *partition 出参 分区信息 用户自定义一个hal_logic_partition_t结构体

返回值

成功则返回0,非0表示失败

调用示例

hal_logic_partition_t partition_info = {0};
ret = hal_flash_info_get(HAL_PARTITION_APPLICATION,&partition_info);

hal_flash_erase

擦除FLASH的指定区域

函数原型

int32_t hal_flash_erase(hal_partition_t in_partition, uint32_t off_set, uint32_t size);

参数

参数名称 出入参 参数描述 参数示例
hal_partition_t in_partition 入参 FLASH分区号 HAL_PARTITION_APPLICATION
uint32_t off_set 入参 偏移量 0
uint32_t size 入参 要擦除的字节数 512

返回值

返回成功或失败, 返回0表示擦除成功,非0表示失败

调用示例

ret = hal_flash_erase(HAL_PARTITION_APPLICATION,0,512);

hal_flash_write

写FLASH的指定区域

函数原型

int32_t hal_flash_write(hal_partition_t in_partition, uint32_t *off_set, const void *in_buf, uint32_t in_buf_len);

参数

参数名称 出入参 参数描述 参数示例
hal_partition_t in_partition 入参 FLASH分区号 HAL_PARTITION_APPLICATION
uint32_t *off_set 入参+出参 偏移量,写入后其值会刷新为写尾部
const void *in_buf 入参 指向要写入数据的指针 char buf[512]
uint32_t in_buf_len 入参 要写入的字节数 512

返回值

返回成功或失败, 返回0表示写入成功,非0表示失败

调用示例

uint32_t off = 0;
char buf[512] = {0};
ret = hal_flash_write(HAL_PARTITION_APPLICATION,&off,buf,512);

hal_flash_erase_write

先擦除再写FLASH的指定区域

函数原型

int32_t hal_flash_erase_write(hal_partition_t in_partition, uint32_t *off_set, const void *in_buf, uint32_t in_buf_len);

参数

参数名称 出入参 参数描述 参数示例
hal_partition_t in_partition 入参 FLASH分区号 HAL_PARTITION_APPLICATION
uint32_t *off_set 入参+出参 偏移量,写入后其值会更新为写尾部 偏移量
const void *in_buf 入参 指向要写入数据的指针 char buf[512]
uint32_t in_buf_len 入参 要擦除和写入的字节数 512

返回值

返回成功或失败, 返回0表示擦除写入成功,非0表示失败

调用示例

uint32_t off = 0;
char buf[512] = {0};
ret = hal_flash_erase_write(HAL_PARTITION_APPLICATION,&off,buf,512);

hal_flash_read

读取FLASH的指定区域

函数原型

int32_t hal_flash_read(hal_partition_t in_partition, uint32_t *off_set, void *out_buf, uint32_t in_buf_len);

参数

参数名称 出入参 参数描述 参数示例
hal_partition_t in_partition 入参 FLASH分区号 HAL_PARTITION_APPLICATION
uint32_t *off_set 入参+出参 偏移量,读取后其值会更新为读取的尾部偏移位置
void *out_buf 入参 数据缓冲区地址 char buf[512]
uint32_t in_buf_len 入参 要读取的字节数 512

返回值

返回成功或失败, 返回0表示读取成功,非0表示失败

调用示例

uint32_t off = 0;
char buf[512] = {0};
ret = hal_flash_read(HAL_PARTITION_APPLICATION,&off,buf,512);

使用

添加该组件

在相应的platform/mcu的mk内,添加对应hal文件的编译包含。

包含头文件

#include "aos/hal/flash.h"

使用示例

results matching ""

    No results matching ""