DataFlash.C
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:16k
- #include "my_head.h"
- #include "mem_addr.h"
- #include "DataFlash.H"
- //#define _DEBUG
- #ifdef _DEBUG
- #include "Debug.H"
- #endif
- #define THE_BUFFER_ONE 0
- #define THE_BUFFER_TWO 1
- //卡状态宏定义
- #define BLACKLIST_MAIN 0x12 //黑名单主表
- #define BLACKLIST_TEMP 0x02 //黑名单临时表
- #define RESUME_LIST 0x00 //卡恢复列表
- extern unsigned char pdata Rf_Buffer[];//RF模块通讯接收缓冲
- extern sttOtherTableInfo xdata stOtherTableInfo;
- extern unsigned char xdata gcDateTime[7]; //全局时钟
- extern unsigned char Write_To_Buffer(void *ptr,int length,bit buffer,unsigned int byteaddr);
- extern unsigned char Buffer_To_Mem(bit buffer,unsigned int pageaddr);
- extern unsigned char Mem_To_Buffer(bit buffer,unsigned int pageaddr);
- extern unsigned char Read_From_Buffer(void *ptr,int length,bit buffer,unsigned int byteaddr);
- extern unsigned char Memory_ArrayRead(void *ptr,int length,unsigned int pageaddr,unsigned int byteaddr);
- extern unsigned char Read_From_Buffer(void *ptr,int length,bit buffer,unsigned int BYTEaddr);
- //extern unsigned char xdata UART_SendBuffer[150];//发送缓冲
- #ifdef _DEBUG
- unsigned char TempCount=0;
- #endif
- //=======================卡状态操作函数=========================
- //读卡状态表
- unsigned char ReadCardStatusTable(unsigned int PageIndex,//页序号 主表为0-939,其它只有1和2值
- unsigned char *Buffer,//具体卡系列号
- unsigned char Count, //个数,1-33,最多一次33个
- unsigned char Status) //状态(0:解挂 2:挂失 12:主表挂失)
- {
- unsigned int iPage; //页地址
- unsigned int iAddr; //页内地址
- #ifdef _DEBUG
- DebugString("PageIndex=");DebugData(2,2,(unsigned char *)&PageIndex);
- DebugString("Count=");DebugData(1,1,&Count);
- DebugString("Status=");DebugData(1,1,&Status);
- #endif
- if(Count > 33) return FORMAT_ERR; //参数错误
- switch(Status)
- {
- case RESUME_LIST: //解挂
- iPage = RESUMECARD_TEMP_PAGEADDR; //给页地址
- if(PageIndex == 0)
- iAddr = 0;
- else if(PageIndex == 1)
- iAddr = 132;
- else
- return MEMADDR_ERR; //地址页超界
- break;
- case BLACKLIST_TEMP: //挂失临时表
- iPage = CARDSTATE_TEMP_PAGEADDR; //给页地址
- if(PageIndex == 0)
- iAddr = 0;
- else if(PageIndex == 1)
- iAddr = 132;
- else
- return MEMADDR_ERR; //地址页超界
- break;
- case BLACKLIST_MAIN://主表挂失
- iPage = PageIndex / 2 + CARDSTATE_START_PAGEADDR; //计算页地址
- if(iPage > CARDSTATE_END_PAGEADDR) return MEMADDR_ERR; //地址页超界
- if(PageIndex % 2) iAddr = 132; else iAddr = 0; //计算开始地址
- break;
- default: return FORMAT_ERR; //参数错误
- }
- #ifdef _DEBUG
- DebugString("iPage=");DebugData(2,2,(unsigned char *)&iPage);
- DebugString("iAddr=");DebugData(2,2,(unsigned char *)&iAddr);
- #endif
-
- return Memory_ArrayRead(Buffer,Count*4,iPage,iAddr);
- }
- unsigned char SaveCardStatusTable(unsigned int PageIndex,//序号 主表为0-939,其它只有1和2值
- unsigned char *Buffer, //具体卡系列号
- unsigned char Count, //个数,最多一次33个
- unsigned char Status) //状态(0:解挂 2:挂失 12:主表挂失)
- {
- unsigned int iPage; //页地址
- unsigned int iAddr; //页内地址
- unsigned char rst; //函数返回值
- if(Count > 33) return FORMAT_ERR; //参数错误
- switch(Status)
- {
- case RESUME_LIST: //解挂
- iPage = RESUMECARD_TEMP_PAGEADDR; //给页地址
- if(PageIndex == 0)
- iAddr = 0;
- else if(PageIndex == 1)
- iAddr = 132;
- else
- return MEMADDR_ERR; //地址页超界
- break;
- case BLACKLIST_TEMP: //挂失临时表
- iPage = CARDSTATE_TEMP_PAGEADDR; //给页地址
- if(PageIndex == 0)
- iAddr = 0;
- else if(PageIndex == 1)
- iAddr = 132;
- else
- return MEMADDR_ERR; //地址页超界
- break;
- case BLACKLIST_MAIN://主表挂失
- iPage = PageIndex / 2 + CARDSTATE_START_PAGEADDR; //计算页地址
- if(iPage > CARDSTATE_END_PAGEADDR) return MEMADDR_ERR; //地址页超界
- if(PageIndex % 2) iAddr = 132; else iAddr = 0; //计算开始地址
- break;
- default: return FORMAT_ERR; //参数错误
- }
- if(iAddr) //地址大于0时,必须先读出原Flash内数据
- {
- rst = Mem_To_Buffer(THE_BUFFER_ONE,iPage);
- if(rst != SUCCESS) return rst;
- }
- //写数据到Flash缓冲区
- rst = Write_To_Buffer(Buffer,Count * 4,THE_BUFFER_ONE,iAddr);
- if(rst != SUCCESS) return rst;
- //把Flash的缓冲区数据存到Flash的存贮区上
- rst = Buffer_To_Mem(THE_BUFFER_ONE,iPage);
- switch(Status)
- {
- case RESUME_LIST: //解挂
- stOtherTableInfo.iResumeCardTempTableRecordCount = PageIndex * 33;
- if(rst == SUCCESS)
- stOtherTableInfo.iResumeCardTempTableRecordCount += Count;
- break;
- case BLACKLIST_TEMP: //挂失临时表
- stOtherTableInfo.iCardStatusTempTableRecordCount = PageIndex * 33;
- if(rst == SUCCESS)
- stOtherTableInfo.iCardStatusTempTableRecordCount += Count;
- break;
- case BLACKLIST_MAIN://主表挂失
- stOtherTableInfo.iCardStatusMainTableRecordCount = PageIndex * 33;
- if(rst == SUCCESS)
- stOtherTableInfo.iCardStatusMainTableRecordCount += Count;
- break;
- default: return FORMAT_ERR; //参数错误
- }
- return rst;
- }
- unsigned char ReadCardStatusList(unsigned long *plCardSerialNo, //返回的卡系列号
- unsigned int Offset, //位置偏移量
- unsigned char Status) //状态(0:解挂 2:挂失 12:主表挂失)
- {
- unsigned int iPage; //页地址
- unsigned int iAddr; //页内地址
- unsigned char rst;
- #ifdef _DEBUG
- DebugString("Offset=");DebugData(2,2,(unsigned char *)&Offset);
- DebugString("Status=");DebugData(1,1,&Status);
- #endif
- switch(Status)
- {
- case RESUME_LIST: //解挂
- if(Offset > 65) return MEMADDR_ERR;
- iPage = RESUMECARD_TEMP_PAGEADDR;
- break;
- case BLACKLIST_TEMP: //挂失临时表
- if(Offset > 65) return MEMADDR_ERR;
- iPage = CARDSTATE_TEMP_PAGEADDR;
- break;
- case BLACKLIST_MAIN://主表挂失
- iPage = CARDSTATE_START_PAGEADDR;
- #ifdef _DEBUG
- DebugString("Init iPage=");
- DebugData(2,2,(unsigned char *)&iPage);
- #endif
- iPage += Offset / 66; //计算具体页地址
- if(iPage > CARDSTATE_END_PAGEADDR)
- return MEMADDR_ERR;
- break;
- default: return FORMAT_ERR; //参数错误
- }
- iAddr = Offset % 66 * 4;//计算具体页内地址
- #ifdef _DEBUG
- DebugString("iPage=");DebugData(2,2,(unsigned char *)&iPage);
- DebugString("iAddr=");DebugData(2,2,(unsigned char *)&iAddr);
- #endif
- rst = Memory_ArrayRead(plCardSerialNo,4,iPage,iAddr);
- #ifdef _DEBUG
- DebugString("Read DataFlash Result:");DebugData(1,1,&rst);
- #endif
- return rst;
- }
- unsigned char IsInCardStatusList(unsigned long lCardSerialNo,
- unsigned char Status)
- {
- unsigned long lSerialNo;
- short iMin,iMax; //此处必须用符号数
- unsigned int iMid;
- unsigned char rst;
-
- #ifdef _DEBUG
- DebugString("lCardSerialNo=");
- DebugData(4,4,(unsigned char *)&lCardSerialNo);
- DebugString("Status=");
- DebugData(1,1,&Status);
- #endif
- //取得列表里记录数
- switch(Status)
- {
- case RESUME_LIST: //解挂
- iMax = stOtherTableInfo.iResumeCardTempTableRecordCount;
- break;
- case BLACKLIST_TEMP: //挂失临时表
- iMax = stOtherTableInfo.iCardStatusTempTableRecordCount;
- break;
- case BLACKLIST_MAIN://主表挂失
- iMax = stOtherTableInfo.iCardStatusMainTableRecordCount;
- break;
- default: return FORMAT_ERR; //参数错误
- }
- #ifdef _DEBUG
- DebugString("Record Count=");DebugData(4,4,(unsigned char *)&iMax);
- #endif
- iMin=0;
- while(iMin < iMax) //判断终止条件
- {
- iMid = ((unsigned int)iMin + (unsigned int)iMax) / 2; //中折
- #ifdef _DEBUG
- DebugString("iMax=");DebugData(2,2,(unsigned char *)&iMax);
- DebugString("iMin=");DebugData(2,2,(unsigned char *)&iMin);
- DebugString("iMid=");DebugData(2,2,(unsigned char *)&iMid);
- #endif
- rst = ReadCardStatusList(&lSerialNo,iMid,Status);
- #ifdef _DEBUG
- DebugString("Current lSerialNo=");
- DebugData(4,4,(unsigned char *)&lSerialNo);
- #endif
- if(rst != SUCCESS) return rst;//返回出错
- if(lSerialNo == lCardSerialNo) //找到当前卡号
- return SUCCESS;
- else if(lSerialNo < lCardSerialNo) //当前卡列号比黑名单表里的小
- iMin = iMid + 1; //继续找后面的数据
- else iMax = iMid; //继续找前面的数据
- }
- #ifdef _DEBUG
- DebugString("the Card SerialNo no foundrn");
- #endif
- return FAILURE; //返回未找到
- }
- unsigned char FoundCardStatusTable(unsigned long lCardSerialNo)
- {
- unsigned char rst=FAILURE; //函数返回值
-
- #ifdef _DEBUG
- DebugString("stOtherTableInfo Data:rn");
- DebugData(sizeof(sttOtherTableInfo),sizeof(sttOtherTableInfo),(unsigned char *)&stOtherTableInfo);
- #endif
- //查找挂失临时表
- #ifdef _DEBUG
- DebugString("查找挂失临时表rn");
- #endif
- rst = IsInCardStatusList(lCardSerialNo,BLACKLIST_TEMP);
- if(rst == SUCCESS) return rst; //若在挂失临时表里找到,直接返回
- if(rst != FAILURE) return rst; //返回函数操作出错
- //查找挂失主表
- #ifdef _DEBUG
- DebugString("查找挂失主表rn");
- #endif
- rst = IsInCardStatusList(lCardSerialNo,BLACKLIST_MAIN);
- if(rst == SUCCESS)
- { //查找解挂临时表
- #ifdef _DEBUG
- DebugString("查找解挂临时表rn");
- #endif
- rst = IsInCardStatusList(lCardSerialNo,RESUME_LIST);
- if(rst == SUCCESS)
- return FAILURE; //若在解挂临时表里找到,直接返回
- else if(rst == FAILURE) return SUCCESS;
- }
- return rst; //返回没找到(FAILURE)或函数操作出错
- }
- //=========================自动充值:充值表部分函数==============================
- //读取充值限制信息
- //pstFillLimitInfo:返回充值限制信息数据结构指针
- //返回:见存贮器返回定义
- unsigned char ReadFillLimitInfo(sttFillLimitInfo *pstFillLimitInfo)
- {
- unsigned int iPage,iAddr;
- iPage = FILL_LIMIT_INFO_PAGEADDR;
- iAddr = 0;
- return Memory_ArrayRead(pstFillLimitInfo,sizeof(sttFillLimitInfo),iPage,iAddr);
- }
- //写充值限制信息
- //pstFillLimitInfo:要写入的充值限制信息数据结构指针
- //返回:见存贮器返回定义
- unsigned char WriteFillLimitInfo(sttFillLimitInfo *pstFillLimitInfo)
- {
- unsigned int iPage,iAddr;
- unsigned char rst;
- #ifdef _DEBUG
- for(iPage=0;iPage<30;iPage++)
- for(iAddr=0;iAddr<60000;iAddr++);
- DebugString("sttFillLimitInfo Data:rn");
- DebugData(12,12,(unsigned char *)pstFillLimitInfo);
- #endif
- iPage = FILL_LIMIT_INFO_PAGEADDR;
- iAddr = 0;
- rst = Write_To_Buffer(pstFillLimitInfo,
- sizeof(sttFillLimitInfo),
- THE_BUFFER_ONE,iAddr);
- if(rst != SUCCESS) return rst;
- //把Flash的缓冲区数据存到Flash的存贮区上
- return Buffer_To_Mem(THE_BUFFER_ONE,iPage);
- }
- //读充值信息表信息
- //iPageIndex: 包系列号0..20030
- //cRecordCount: 记录数
- //stFillPara[16/17]:充值信息表数据,偶数页为16条,奇数页为17条
- //返回:见存贮器返回定义
- unsigned char ReadFillPara(unsigned int iPageIndex,
- unsigned char cRecordCount,
- sttFillPara stFillPara[])
- {
- unsigned int iPage,iAddr;
- if(iPageIndex % 2)
- {
- if(cRecordCount > 17) return FORMAT_ERR; //参数错误
- iAddr = 128;
- }else{
- if(cRecordCount > 16) return FORMAT_ERR; //参数错误
- iAddr = 0;
- }
- iPage = iPageIndex + FILL_INFO_START_PAGEADDR;
- if(iPage > FILL_INFO_END_PAGEADDR) return FORMAT_ERR; //参数错误
- return Memory_ArrayRead(stFillPara,sizeof(sttFillPara) * cRecordCount,iPage,iAddr);
- }
- //写充值信息表信息
- //iPageIndex: 包系列号0..20030
- //cRecordCount: 记录数
- //stFillPara[16/17]:充值信息表数据,偶数页为16条,奇数页为17条
- //返回:见存贮器返回定义
- unsigned char WriteFillPara(unsigned int iPageIndex,
- unsigned char cRecordCount,
- sttFillPara stFillPara[])
- {
- unsigned int iPage,iAddr;
- unsigned char rst;
- #ifdef _DEBUG
- if(TempCount > 5)
- {
- for(iPage=0;iPage<30;iPage++)
- for(iAddr=0;iAddr<60000;iAddr++);
- DebugString("iPageIndex=");DebugData(2,2,(unsigned char*)&iPageIndex);
- DebugString("cRecordCount=");DebugData(1,1,&cRecordCount);
- DebugString("stFillPara Data:rn");
- //DebugData(16,8,(unsigned char *)stFillPara);
- DebugData(cRecordCount*sizeof(sttFillPara),sizeof(sttFillPara),(unsigned char *)stFillPara);
- }
- TempCount++;
- #endif
- iPage = iPageIndex / 2 + FILL_INFO_START_PAGEADDR;
- if(iPage > FILL_INFO_END_PAGEADDR) return FORMAT_ERR; //参数错误
- if(iPageIndex % 2)
- {
- if(cRecordCount > 17) return FORMAT_ERR; //参数错误
- iAddr = 128;
- }else{
- if(cRecordCount > 16) return FORMAT_ERR; //参数错误
- iAddr = 0;
- }
- if(iAddr > 0)
- {
- rst = Mem_To_Buffer(THE_BUFFER_ONE,iPage);
- if(rst != SUCCESS) return rst;
- }
- #ifdef _DEBUG
- // DebugString("iPage=");DebugData(2,2,(unsigned char *)&iPage);
- // DebugString("iAddr=");DebugData(2,2,(unsigned char *)&iAddr);
- #endif
- rst = Write_To_Buffer(stFillPara,
- sizeof(sttFillPara) * cRecordCount,
- THE_BUFFER_ONE,iAddr);
- if(rst != SUCCESS) return rst;
- //把Flash的缓冲区数据存到Flash的存贮区上
- rst = Buffer_To_Mem(THE_BUFFER_ONE,iPage);
- stOtherTableInfo.iFillParaTableRecordCount = (iPageIndex / 2) * 33;
- if(iPageIndex % 2) stOtherTableInfo.iFillParaTableRecordCount += 16;
- if(rst == SUCCESS)
- stOtherTableInfo.iFillParaTableRecordCount += cRecordCount;
- return rst;
- }
- //查找指定卡流水号的充值信息数据
- //pstFillPara:传入的卡流水号和返回的具体充值信息数据
- //piOffSet: 返回当前卡流水号的充值信息数据所处的位置
- //返回:见存贮器返回定义
- unsigned char FoundFillPara(sttFillPara *pstFillPara,unsigned int *piOffSet)
- {
- unsigned int iPage; //页地址
- unsigned int iAddr; //页内地址
- short iMin,iMax; //此处必须用符号数
- unsigned int iMid;
- char rst;
- sttFillPara xdata stFillParaTemp;
- /* for(iPage = FILL_INFO_START_PAGEADDR;iPage < FILL_INFO_START_PAGEADDR + 4; iPage ++)
- {
- iAddr = 0;iMid=128;
- DebugString("iPage=");DebugData(2,2,(unsigned char*)&iPage);
- DebugString("iAddr=");DebugData(2,2,(unsigned char*)&iAddr);
- rst=Memory_ArrayRead(UART_SendBuffer,iMid,iPage,iAddr);
- if(rst == SUCCESS)
- DebugData(iMid,8,UART_SendBuffer);
- else
- DebugString("Read Memory Errorrn");
- iAddr = 128;iMid=136;
- DebugString("iPage=");DebugData(2,2,(unsigned char*)&iPage);
- DebugString("iAddr=");DebugData(2,2,(unsigned char*)&iAddr);
- rst=Memory_ArrayRead(UART_SendBuffer,iMid,iPage,iAddr);
- if(rst == SUCCESS)
- DebugData(iMid,8,UART_SendBuffer);
- else
- DebugString("Read Memory Errorrn");
- }
- */
- #ifdef _DEBUG
- DebugString("sttFillPara Data:");
- DebugData(8,8,(unsigned char*)pstFillPara);
- DebugString("充值信息表记录数:");
- DebugData(2,2,(unsigned char *)&stOtherTableInfo.iFillParaTableRecordCount);
- #endif
- iMax = stOtherTableInfo.iFillParaTableRecordCount;
- iMin = 0;
- while(iMin < iMax) //判断终止条件
- {
- iMid = ((unsigned int)iMin + (unsigned int)iMax) / 2; //中折
- iPage = FILL_INFO_START_PAGEADDR
- + iMid / FILL_INFO_PAGE_MAX_RECORD_COUNT;
- iAddr = iMid % FILL_INFO_PAGE_MAX_RECORD_COUNT
- * sizeof(sttFillPara);
- #ifdef _DEBUG
- DebugString("iMax=");DebugData(2,2,(unsigned char *)&iMax);
- DebugString("iMin=");DebugData(2,2,(unsigned char *)&iMin);
- DebugString("iMid=");DebugData(2,2,(unsigned char *)&iMid);
- DebugString("iPage=");DebugData(2,2,(unsigned char *)&iPage);
- DebugString("iAddr=");DebugData(2,2,(unsigned char *)&iAddr);
- #endif
- rst = Memory_ArrayRead(&stFillParaTemp,sizeof(sttFillPara),iPage,iAddr);
- if(rst != SUCCESS)
- {
- #ifdef _DEBUG
- DebugString("Read Order Dinner Parameter From DataFlash Error.rn");
- #endif
- return rst; //出错返回
- }
- #ifdef _DEBUG
- DebugString("Current Record Data:");
- DebugData(8,8,(unsigned char*)&stFillParaTemp);
- #endif
- rst = memcmp(pstFillPara->CardIndex, stFillParaTemp.CardIndex, 3);
- if(rst == 0)
- {
- memcpy(pstFillPara,&stFillParaTemp,sizeof(sttFillPara));
- *piOffSet = iMid;
- return SUCCESS;
- }
- else if(rst > 0) iMin = iMid + 1; else iMax = iMid;
- }
- return UNALLOWED_CARDCLASS;
- }
- //更新充值表信息状态字为已充值状态
- //iOffSet:要修改的记录偏移量,即当前记录位置
- unsigned char UpdateFillParaStatus(unsigned int iOffSet)
- {
- unsigned int idata iPage,iAddr;
- unsigned char rst,i;
- iPage = FILL_INFO_START_PAGEADDR
- + iOffSet / FILL_INFO_PAGE_MAX_RECORD_COUNT;
- iAddr = iOffSet % FILL_INFO_PAGE_MAX_RECORD_COUNT
- * sizeof(sttFillPara) + sizeof(sttFillPara) - 1;
- for(i=0;i<3;i++)
- {
- rst = Mem_To_Buffer(THE_BUFFER_ONE,iPage);
- if(rst != SUCCESS) continue;
- rst = 0;
- rst = Write_To_Buffer(&rst,1,THE_BUFFER_ONE,iAddr);
- if(rst != SUCCESS) continue;
- //把Flash的缓冲区数据存到Flash的存贮区上
- rst = Buffer_To_Mem(THE_BUFFER_ONE,iPage);
- if(rst != SUCCESS) continue;
- }
- return rst;
- }