|
本帖最后由 杀进丧尸城 于 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]);
以下是打印出来的数据:
如果这样写下去,不是要写疯了,并且读取的时候也很耽误时间的;有没有更好的方法减少代码量啊!!!
或者有没有其他写法,求指教。谢谢!!!
|
|