日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當(dāng)前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

【嵌入式干货】利用二分法定位Flash存有数据(非FF)的地址

發(fā)布時(shí)間:2025/4/16 编程问答 31 豆豆
生活随笔 收集整理的這篇文章主要介紹了 【嵌入式干货】利用二分法定位Flash存有数据(非FF)的地址 小編覺得挺不錯(cuò)的,現(xiàn)在分享給大家,幫大家做個(gè)參考.

前言

最近項(xiàng)目中需要實(shí)現(xiàn)這樣的功能:設(shè)備具備溫濕度數(shù)據(jù)采集并寫入flash保存的功能;當(dāng)設(shè)備復(fù)位后,還能重新開啟采集功能且將采集到的數(shù)據(jù)繼續(xù)接著寫入。問題來了,復(fù)位后,MCU會將RAM的數(shù)據(jù)清除,這樣會導(dǎo)致程序丟失了上次記錄溫濕度數(shù)據(jù)的最終flash地址,那么得在復(fù)位后程序能夠找到復(fù)位前記錄的flash地址(即flash中存有數(shù)據(jù)(非FF)的最后地址)。

實(shí)現(xiàn)

利用二分法對flash進(jìn)行數(shù)據(jù)查找,用的是2M byte flash芯片P25Q16U
一、首先在PC端用C語言跑通實(shí)現(xiàn)代碼如下:

//二分法查找上次寫的最后一個(gè)地址 #include "stdio.h" #include "string.h" #include "stdlib.h"unsigned char *my_memory=(unsigned char*)malloc(2016384); ; char i=0;unsigned int LastSectorHeadAddress; unsigned int NextSectorStart; unsigned int NextSectorStop; unsigned char buf; unsigned int Get_Last_Sector(unsigned int Sector_start, unsigned int Sector_stop) {static unsigned char cnt=0;cnt++;if(Sector_start<0x00004000 || Sector_stop>0x001EC480)//不在尋找區(qū)域內(nèi){return -1;}LastSectorHeadAddress = (Sector_start + Sector_stop)/2;// spiFlashRead(*LastSectorHeadAddress,1,buf);buf = *((unsigned char*)(my_memory+LastSectorHeadAddress));printf("第%d次查找的數(shù)據(jù)為:%d\n",cnt,buf);if(buf == 0xFF){ NextSectorStop = LastSectorHeadAddress;NextSectorStart = Sector_start;if(Sector_stop == NextSectorStop){return Sector_start; }else{return Get_Last_Sector(NextSectorStart,NextSectorStop);}}else{ NextSectorStop = Sector_stop;NextSectorStart = LastSectorHeadAddress;if(NextSectorStart == Sector_start){return Sector_start;}else{return Get_Last_Sector(NextSectorStart,NextSectorStop);}} }int main() {unsigned int my_address;unsigned char buf;memset(my_memory,0xff,2016384); //全部初始化為FFmemset(my_memory,0x0A,16384+4001); //初始化為0Abuf = *((unsigned char*)(my_memory+99));my_address = Get_Last_Sector(16384,2016384);printf("數(shù)據(jù)的最后地址是:%d,數(shù)據(jù)為:%d,總共寫了%d個(gè)數(shù)據(jù)\n",my_address,buf,my_address-16384+1);}

二、在硬件上跑通代碼如下;

//定位上次最后一個(gè)存有數(shù)據(jù)的地址 unsigned int MidAddress; unsigned int NextAddressStart; unsigned int NextAddressStop; unsigned char buf; unsigned int Get_Last_Sector(unsigned int Sector_start, unsigned int Sector_stop) {static unsigned char cnt=0;cnt++;if(Sector_start<0x00004000 || Sector_stop>0x001EC480)//不在尋找區(qū)域內(nèi){return -1;}MidAddress = (Sector_start + Sector_stop)/2;spiFlashRead(MidAddress,1,&buf);if(buf == 0xFF){ NextAddressStop = MidAddress;NextAddressStart = Sector_start;if(Sector_stop == NextAddressStop){return Sector_start; }else{return Get_Last_Sector(NextAddressStart,NextAddressStop);}}else{ NextAddressStop = Sector_stop;NextAddressStart = MidAddress;if(NextAddressStart == Sector_start){return Sector_start;}else{return Get_Last_Sector(NextAddressStart,NextAddressStop);}} }

三、結(jié)論
經(jīng)測試,在2M byte的flash中,最多只需20次便能定位到存有溫濕度數(shù)據(jù)(非FF)的最后一個(gè)字節(jié)(地址)。

總結(jié)

以上是生活随笔為你收集整理的【嵌入式干货】利用二分法定位Flash存有数据(非FF)的地址的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網(wǎng)站內(nèi)容還不錯(cuò),歡迎將生活随笔推薦給好友。