AliOS-Things--ESP8266 (7)flash

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/qq_28877125/article/details/83035016

参考博客:

前言

  • ESP8266Flash接口:platform\mcu\esp8266\bsp\include\espressif\spi_flash.h
  • sector:扇区

一、ESP8266 Flash读写例程

1、写入数据:

 spi_flash_erase_sector函数只能每次擦除4KByte,写的时候,只能在已经擦除的区域里写。4KByte就是4096Byte对应十六进制就是0x1000,而每次写又得以4个字节那样对齐地写。也就是说擦除一次可以写入1024次。即可以:

  • 擦除第0x201扇区
  • 写数据1的起始地址为:0x201000
  • 写数据2的起始地址为:0x201004
  • 写数据3的起始地址为:0x201008
  • 依次累加,读的时候也是如此
uint32 value;
uint8 *addr_case1 = (uint8 *)&value;
addr_case1[0]=0x11;
addr_case1[1]=0x12;
addr_case1[2]=0x13;
addr_case1[3]=0x14;

spi_flash_erase_sector(0x201);//擦出当前地址的flash
spi_flash_write(0x201000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
printf("write:%02x%02x%02x%02x\r\n",addr_case1[0], addr_case1[1], addr_case1[2], addr_case1[3]);

2、读取数据:

uint32 value;
uint8 *addr_case1 = (uint8 *)&value;

spi_flash_read(0x201000,(uint32 *)addr_case1,sizeof(addr_case1));//读取flash值
printf("read:%02x%02x%02x%02x\r\n",addr_case1[0], addr_case1[1], addr_case1[2], addr_case1[3]);

3、解析

typedef enum {
    SPI_FLASH_RESULT_OK,        /**< SPI Flash operating OK */
    SPI_FLASH_RESULT_ERR,       /**< SPI Flash operating fail */
    SPI_FLASH_RESULT_TIMEOUT    /**< SPI Flash operating time out */
} SpiFlashOpResult;


/**
  * @brief  Erase the Flash sector.
  *
  * @param  uint16 sec : 扇区数,从扇区0开始计数,每个扇区长度为 4KB。
  *
  * @return SpiFlashOpResult
  */
SpiFlashOpResult spi_flash_erase_sector(uint16 sec);

 从上面对函数spi_flash_erase_sector()的解释可以看出:擦除的扇区数是以模组整个的Flash总扇区数来计算的。我使用的是ESP8266-12F模组,默认flash为32Mb也就是4MB。那么flash的总扇区数为:4*1024 = 4096(kB) 4096/4 = 1024(个扇区)

Running Cesanta flasher (speed 115200)...
params:  ?
MEMORY START
ELSE.... 26
P: OHAI
Writing 4096 @ 0x0... 0 (0 %)
1024 (25 %)
2048 (50 %)
3072 (75 %)
4096 (100 %)

Wrote 4096 bytes at 0x00000000 in 0.4 seconds (89.0 kbit/s)...
Writing 339968 @ 0x1000... 0 (0 %)
1024 (0 %)
2048 (0 %)
......
Wrote 339968 bytes at 0x00001000 in 29.5 seconds (92.3 kbit/s)...
Writing 4096 @ 0x3fc000... 0 (0 %)
1024 (25 %)
2048 (50 %)
3072 (75 %)
4096 (100 %)

Wrote 4096 bytes at 0x003fc000 in 0.4 seconds (88.8 kbit/s)...
Writing 4096 @ 0x3fe000... 0 (0 %)
1024 (25 %)
2048 (50 %)
3072 (75 %)
4096 (100 %)

Wrote 4096 bytes at 0x003fe000 in 0.4 seconds (89.3 kbit/s)...
Leaving...
com closed

以上是在下载ESP8266程序时打印出来的信息,可以看到:

  • 第1个扇区已经被写了:Wrote 4096 bytes at 0x00000000 …
  • 第2个扇区到第332个扇区已经被写了:Wrote 339968 bytes at 0x00001000 …
  • 第1020个扇区已经被写了:Wrote 4096 bytes at 0x003fc000 …
  • 第1022个扇区已经被写了:Wrote 4096 bytes at 0x003fe000 …

AliOS-Things上的使用例程:

/*
 * Copyright (C) 2015-2017 Alibaba Group Holding Limited
 */

#include <aos/aos.h>
#include <hal/soc/soc.h>
#include "stdio.h"
#include "../../../platform/mcu/esp8266/bsp/include/espressif/spi_flash.h"

// 写flash
void write_config(void) 
{
    uint32 value, value2, value3, value4;
    value = 1994;
    value2 = 1995;
    value3 = 1996;
    value4 = 1997;

    printf("erase:%d\r\n", spi_flash_erase_sector(0x201));
    printf("write:%d\r\n", spi_flash_write(0x201000, (uint32 *)&value, sizeof(value)));
    printf("write:%d\r\n", spi_flash_write(0x201004, (uint32 *)&value2, sizeof(value2)));
    printf("write:%d\r\n", spi_flash_write(0x201008, (uint32 *)&value3, sizeof(value3)));
    printf("write:%d\r\n", spi_flash_write(0x20100C, (uint32 *)&value4, sizeof(value4)));
    printf("sizeof:%d\r\n", sizeof(value));
} 

//读取flash内容
void read_config(void) {
    uint32 temp, temp2, temp3, temp4;
    
    printf("sizeof:%d\r\n", sizeof(temp));
    printf("read:%d\r\n", spi_flash_read(0x201000, (uint32 *)&temp, sizeof(temp)));
    printf("read:%d\r\n", spi_flash_read(0x201004, (uint32 *)&temp2, sizeof(temp2)));
    printf("read:%d\r\n", spi_flash_read(0x201008, (uint32 *)&temp3, sizeof(temp3)));
    printf("read:%d\r\n", spi_flash_read(0x20100C, (uint32 *)&temp4, sizeof(temp4)));
    printf("temp:%d\r\n", temp);
    printf("temp2:%d\r\n", temp2);
    printf("temp3:%d\r\n", temp3);
    printf("temp4:%d\r\n", temp4);
} 


int application_start(int argc, char *argv[])
{
    printf("liefyuan's esp8266 :%d\r\n", user_rf_cal_sector_set());

    write_config();

    read_config();

    aos_loop_run();

    return 0;
}

猜你喜欢

转载自blog.csdn.net/qq_28877125/article/details/83035016