#include "xspOptFlash.h" #include "../Hardware/boardinit.h" #include "xspDataOpt.h" FlahOpt g_flashopt; /*------------------------------------------------------------------------------- * 初始化 * ------------------------------------------------------------------------------*/ void init_flash(void) { g_flashopt.head = 0; g_flashopt.tail = 0; g_flashopt.flag = FALSE; g_flashopt.totalPage = 64; //一共64页 g_flashopt.pageRecord = 32; // 一页32条记录 } /*------------------------------------------------------------------------------------------- * 更新当前记录的上报标识 bsend ,将0xFF 更新为0x00, 说明已经上报 * ------------------------------------------------------------------------------------------*/ static void update_bsend_to_0x00(uint32_t baseAddr) { uint16_t data = 0x0000; Flash_BufferWrite_uint16(baseAddr+4,&data,2);//正常的flash 写入 return; } /*------------------------------------------------------------------------------- * 读取flash中的记录 * 返回值 1:成功 0:失败 * --------------------------------------------------------------------------------*/ uint8_t read_flash(uint8_t *outda,uint8_t bupdateSend) { uint32_t addr; uint8_t curPage,curRecord; if((g_flashopt.tail== g_flashopt.head) &&(g_flashopt.flag == FALSE)){ // 队列为空 // printf("没有新记录\r\n"); return 0; } curPage = g_flashopt.head/32; curRecord = g_flashopt.head%32; addr = START_FLASH_ADDR + curPage*PAGE_SIZE + curRecord*RECORD_SIZE; Flash_BufferRead(addr,outda,64);//数据存入到outda中 if((1 == bupdateSend) &&(0xFFFFFFFF != *((uint32_t*)outda)) ) update_bsend_to_0x00(addr); g_flashopt.head = (g_flashopt.head+1)%RECORD_FIFO; if(g_flashopt.head == g_flashopt.tail) g_flashopt.flag = FALSE; return 1; } /*----------------------------------------------------------------------------------- * 写入 * ----------------------------------------------------------------------------------*/ void write_flash(uint8_t *data) { uint32_t addr; uint8_t curPage,curRecord,ret; if((g_flashopt.tail == g_flashopt.head) &&(g_flashopt.flag==TRUE)){ // fifo满了 printf("队列已满 tail=%d, head=%d\r\n",g_flashopt.tail,g_flashopt.head); g_flashopt.head = (g_flashopt.head+1)%RECORD_FIFO; } curPage = g_flashopt.tail/32; curRecord = g_flashopt.tail%32; addr = START_FLASH_ADDR + curPage*PAGE_SIZE + curRecord*RECORD_SIZE; if(curRecord==0){ // 第0条记录时 要擦除本页 Flash_RangeErase(addr,PAGE_SIZE); } ret = Flash_BufferWrite(addr,(uint32_t*)data,64);//正常的flash 写入 if(ret == 0) printf("++++++++++++++写入flash出错误\r\n"); g_flashopt.tail = (g_flashopt.tail+1)%RECORD_FIFO; if(g_flashopt.tail == g_flashopt.head) g_flashopt.flag = TRUE; printf("fifo head = %d, tail=%d\r\n",g_flashopt.head,g_flashopt.tail); } /*------------------------------------------------------------------------------- * 读取 * readsize:读取的字节数据 * --------------------------------------------------------------------------------*/ void find_last_record(uint8_t pageIndex, uint8_t recordIndex, uint8_t readsize,uint8_t *outda) { uint32_t addr; addr = START_FLASH_ADDR + pageIndex*PAGE_SIZE + recordIndex*RECORD_SIZE; Flash_BufferRead(addr,outda,readsize);//数据存入到outda中 return ; } #if( 1 ) void find_fifo_head(void) { uint8_t i=0,j=0;//,pageIndex = 0, recordIndex = 0, uint8_t bfirst = 0; uint32_t min = 0xFFFFFFFF; tempRecord head;//,tail; g_flashopt.head = 0; for(i=0;i=head.da.recordId) { g_flashopt.head = i*g_flashopt.pageRecord + j; min = head.da.recordId; } } } else if((head.da.recordId == 0xFFFFFFFF)&&(min == 0xFFFFFFFF)){ if(head.da.bSend == 0xFFFF && bfirst == 0){ g_flashopt.head = i*g_flashopt.pageRecord + j; bfirst = 1; } } } } } void find_fifo_head00(void) { uint8_t i=0,j=0,bfinish = 0;//,pageIndex = 0, recordIndex = 0; uint32_t min = 0xFFFFFFFF; tempRecord head,tail; g_flashopt.head = 0; for(i=0;ihead.da.recordId){ min = head.da.recordId; g_flashopt.head = i*g_flashopt.pageRecord + j; } } } } } else if((head.da.recordId == 0xFFFFFFFF) &&(tail.da.recordId==0xFFFFFFFF)){ if(min != 0xFFFFFFFF) continue; if((i+1) == g_flashopt.totalPage) break; find_last_record(i+1,0,6,head.data); if(head.da.recordId == 0xFFFFFFFF){ g_flashopt.head = i*g_flashopt.pageRecord; break; } else { if(head.da.bSend == 0xFFFF) { g_flashopt.head = i*g_flashopt.pageRecord; break; } else { for(j=1;jhead.da.recordId){ pageIndex = i; recordIndex = j; min = head.da.recordId; bfind = 1; break; } } } } if(bfind == 1) break; } } g_flashopt.head = pageIndex*g_flashopt.pageRecord + recordIndex; } #endif /*----------------------------------------------------------------------------------------- * powerup 上电后,读取flash中的数据 且根据记录ID找出最新的一条记录的位置 * 以及找到最后一条没有上报的记录的位置 * ----------------------------------------------------------------------------------------*/ void powerup_read_record_from_flash(uint8_t *recordDA) { uint8_t i=0,j=0,bfind=0;//,curpage=0,currecord=0 uint8_t data[4] = {0}; uint32_t curread;//,maxread = 0; /*for(i=0;i