收藏官网首页
查看: 15146|回复: 1

[问答] ESP8266_12f中写flash的问题

10

主题

71

帖子

828

积分

高级会员

Rank: 4

积分
828
QQ
跳转到指定楼层
楼主
发表于 2017-3-1 16:48:30 | 只看该作者 |只看大图 回帖奖励 |倒序浏览 |阅读模式
校园创客福利
本帖最后由 杀进丧尸城 于 2017-3-8 14:03 编辑

不管在哪个项目里基本上都会用到掉电记录数据的,所以不得不搞清楚。
根据2C-esp8266__SDK__Programming Guide__CN_v1.2.0中38页所说一个扇区4KB,读写必须4字节对齐;所以读写一次只能读写uint32,也就是4个字节;这么说起来,读写的次数那就多了,怎么办???


写入数据:
        uint32 value;
        uint8 *addr_case1 = (uint8 *)&value;
        addr_case1[0]=....
        addr_case1[1]=.....
        addr_case1[2]=....
        addr_case1[3]=....

        spi_flash_erase_sector(0x7C);//擦出当前地址的flash
        spi_flash_write(0x7C000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
        os_printf("@@@@@@FLASH write@@@@@:%02x%02x%02x%02x\r\n",addr_case1[0], addr_case1[1], addr_case1[2], addr_case1[3]);
读取数据:
        uint32 value;
        uint8 *addr_case1 = (uint8 *)&value;
        spi_flash_read(0x7C000,(uint32 *)addr_case1,sizeof(addr_case1));//读取flash值
        os_printf("@@@@@@FLASH read@@@@@:%02x%02x%02x%02x\r\n",addr_case1[0], addr_case1[1], addr_case1[2], addr_case1[3]);
以上读写是正常的,没有问题;


如果我的数据不止4个字节,又该怎么读写???
想了半天,只有用死办法了。
写入数据:
        uint32 value,cal,al;
        uint8 *addr_case1 = (uint8 *)&value;
        uint8 *adlr_case1 = (uint8 *)&cal;
        uint8 *adcr_case1 = (uint8 *)&al;
        addr_case1[0]=....
        addr_case1[1]=...
        addr_case1[2]=...
        addr_case1[3]=...

        adlr_case1[0]=....
        adlr_case1[1]=...
        adlr_case1[2]=....
        adlr_case1[3]=....

        adcr_case1[0]=....
        adcr_case1[1]=...
        adcr_case1[2]=...
        spi_flash_erase_sector(0x7C);//擦出当前地址的flash
        spi_flash_write(0x7C000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
        os_printf("@@@@@@FLASH write@@@@@:%02x%02x%02x%02x\r\n",addr_case1[0], addr_case1[1],addr_case1[2],addr_case1[3]);
        spi_flash_write(0x7C010,(uint32 *)adlr_case1,sizeof(adlr_case1));//写flash,4个字节共32位
        os_printf("@@@@@@FLASH write@@@@@:%02x%02x%02x%02x\r\n",adlr_case1[0], adlr_case1[1], adlr_case1[2], adlr_case1[3]);
        spi_flash_write(0x7C020,(uint32 *)adcr_case1,sizeof(adcr_case1));//写flash,4个字节共32位
        os_printf("@@@@@@FLASH write@@@@@:%02x%02x%02x\r\n",adcr_case1[0], adcr_case1[1], adcr_case1[2]);


读取数据:
        uint32 value,cal,al;
        uint8 *addr_case1 = (uint8 *)&value;
        uint8 *adlr_case1 = (uint8 *)&cal;
        uint8 *adcr_case1 = (uint8 *)&al;
        spi_flash_read(0x7C000,(uint32 *)addr_case1,sizeof(addr_case1));//读取flash值
        spi_flash_read(0x7C010,(uint32 *)adlr_case1,sizeof(adlr_case1));//读取flash值
        spi_flash_read(0x7C020,(uint32 *)adcr_case1,sizeof(adcr_case1));//读取flash值

        os_printf("@@@@@@FLASH read@@@@@:%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x,%02x\r\n",addr_case1[0], addr_case1[1],addr_case1[2],addr_case1[3],adlr_case1[0], adlr_case1[1],adlr_case1[2],adlr_case1[3],adcr_case1[0], adcr_case1[1],adcr_case1[2]);
以下是打印出来的数据:

如果这样写下去,不是要写疯了,并且读取的时候也很耽误时间的;有没有更好的方法减少代码量啊!!!
或者有没有其他写法,求指教。谢谢!!!


10

主题

71

帖子

828

积分

高级会员

Rank: 4

积分
828
QQ
推荐
 楼主| 发表于 2017-3-17 16:31:43 | 只看该作者
终于搞明白了,读和写必须四个字节四个字节的读写;擦出一次就是一个扇区,一个扇区就是4K=41024=4096,转换成16进制就是0x1000;写一次必须4个字节,如果再次写,在当前扇区就必须先擦出再写了,并且先前写的数据也不见了,所以写那么一次写一个扇区的数据,要么就换一个扇区写;读取数据的时候可以4个字节的读,没什么问题。
比如:第一次写入:
spi_flash_erase_sector(0x7C);//擦出当前地址的flash
spi_flash_write(0x7C000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
spi_flash_write(0x7C010,(uint32 *)adlr_case1,sizeof(adlr_case1));//写flash,4个字节共32位
spi_flash_write(0x7C020,(uint32 *)adcr_case1,sizeof(adcr_case1));//写flash,4个字节共32位

第二次写入:
spi_flash_erase_sector(0x7D);//擦出当前地址的flash
spi_flash_write(0x7D000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
spi_flash_write(0x7D010,(uint32 *)adlr_case1,sizeof(adlr_case1));//写flash,4个字节共32位
spi_flash_write(0x7D020,(uint32 *)adcr_case1,sizeof(adcr_case1));//写flash,4个字节共32位

第三次写入:
spi_flash_erase_sector(0x7E);//擦出当前地址的flash
spi_flash_write(0x7E000,(uint32 *)addr_case1,sizeof(addr_case1));//写flash,4个字节共32位
spi_flash_write(0x7E010,(uint32 *)adlr_case1,sizeof(adlr_case1));//写flash,4个字节共32位
spi_flash_write(0x7E020,(uint32 *)adcr_case1,sizeof(adcr_case1));//写flash,4个字节共32位

您需要登录后才可以回帖 登录 | 立即注册

本版积分规则

加入Q群 返回顶部

版权与免责声明 © 2006-2024 Gizwits IoT Technology Co., Ltd. ( 粤ICP备11090211号 )

快速回复 返回顶部 返回列表