8.3. 应用示例

8.3.1. flash 初始化

通过调用 \ref gx_nor_init 接口初始化 flash 设备; 通过调用 \ref gx_nor_xip_init 接口初始化 flash xip;

    gx_nor_init();
    gx_nor_xip_init(2, 0);

8.3.2. flash 读

通过 \ref gx_nor_read 接口来读取 flash 中数据,其中参数 \ref GX_SPINOR_RW_T 定义如下:

  • addr 为 flash 需要读取的偏移地址;

  • len 为 flash 需要读取长度;

  • data 为 flash 读取数据存储 buffer;

  • dma_en 为 是否使用 dma 模式读取数据;

  • bus_mode 为传输模式,可配置单线、双线、四线等传输模式;

  • data_width_8or32bit 为 flash spi 的传输位宽,可配置 0 (8bit) 或 1 (32bit);

  • espi_ddr_en 为双沿模式,暂不支持;

从 flash 地址 0x0 处读取 0x100 个字节数据,示例如下:

    GX_SPINOR_RW_T r = {0};
    uint32_t readlen = 0;
    uint8_t buff[256];

    r.addr = 0x0;
    r.len = 0x100;
    r.data = (void *)buff;
    r.bus_mode = 2;
    r.dma_en = 0;
    r.data_width_8or32bit = 1;
    r.espi_ddr_en = 0;
    readlen = gx_nor_read(&r);

8.3.3. flash 擦除

通过 \ref gx_nor_erase_data 接口擦除 flash 中数据,其中参数定义如下:

  • addr 为 flash 需要擦除的偏移地址;

  • len 为 flash 需要擦除长度; 注意,flash 擦除最小单位大小为一个 sector (4KB) 。

示例如下:

    gx_nor_erase_data(0x0, 0x1000);

8.3.4. flash 写

通过 \ref gx_nor_write 接口向 flash 中写数据,其中参数 \ref GX_SPINOR_RW_T 定义如下:

  • addr 为 flash 需要写入的偏移地址;

  • len 为 flash 需要写入长度;

  • data 为 flash 写入数据存储 buffer;

  • dma_en 为 是否使用 dma 模式写数据;

  • bus_mode 为传输模式,可配置单线、双线、四线等传输模式;

  • data_width_8or32bit 为 flash spi 的传输位宽,可配置 0 (8bit) 或 1 (32bit);

  • espi_ddr_en 为双沿模式,暂不支持; 注意,在 flash 写操作前,必须先擦除 flash 对应区域的数据后,才能进行写入操作。

向 flash 地址 0x0 处写入 0x100 个字节数据,示例如下:

    GX_SPINOR_RW_T w = {0};
    uint32_t writelen = 0;
    uint8_t buff[256];

    w.addr = 0x0;
    w.len = 0x100;
    w.data = (void *)buff;
    w.bus_mode = 2;
    w.dma_en = 0;
    w.data_width_8or32bit = 1;
    w.espi_ddr_en = 0;
    writelen = gx_nor_read(&w);

8.3.5. flash xip 读

读取 xip 地址数据,可以像访问 sram 一样访问 xip 地址。

从 xip 地址 0x0 处读取 0x100 个字节数据,示例如下:

    uint8_t buff[256];
    memcpy(buff, (void *)CONFIG_FLASH_XIP_BASE, 0x100);

8.3.6. flash 写保护

通过 \ref gx_nor_write_protect_lock 接口设置 flash 中写保护操作,可将 flash 从起始地址 0 至 特定长度的区域设置成写保护状态,其中参数定义如下:

  • protect_len 为 flash 写保护的长度,一般为 block size (64KB) 的整数倍;

将 flash 地址 0x0 至 1MB 处的区域写保护,示例如下:

    gx_nor_write_protect_lock(0x100000);

8.3.7. flash 解除写保护

通过 \ref gx_nor_write_protect_unlock 接口解除整个 flash 中写保护状态。

示例如下:

    gx_nor_write_protect_unlock();