AliOS-Things--ESP8266 (7)flash
阿新 • • 發佈:2018-10-31
參考部落格:
- http://wiki.jackslab.org/ESP8266_Flash
- https://blog.csdn.net/xh870189248/article/details/81017735
- https://www.jianshu.com/p/0f8e6831cbad
- https://blog.csdn.net/k7arm/article/details/51812021
- http://club.gizwits.com/thread-5092-1-1.html
前言
- 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;
}