memory.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:28k
- #include "my_head.h"
- #include "mem_addr.h"
- #include "carddef.h"
- //#define _DEBUG
- #ifdef _DEBUG
- #include "Debug.h"
- #endif
- /********************************************/
- #define MEM_CS P1_4
- #define MEM_RDY P1_3
- #define RESET P1_2
- //-------------------------------------------
- #define MYALL_FORBYTE_FLAG 45
- #define THE_BUFFER_ONE 0
- #define THE_BUFFER_TWO 1
- #define CONTINUE_ARREAD_ORDOR 0x68
- #define PAGE_ERASE_ORDOR 0x81
- #define BLOCK_ERASE_ORDOR 0x50
- #define BUFER1_WRIT_ORDOR 0X84
- #define BUFER1_TO_MAIN_ORDOR 0X83
- #define MAIN_TO_BUFER1_ORDOR 0X53
- #define BUFER1_READ_ORDOR 0x54
- #define BUFER2_WRIT_ORDOR 0X87
- #define BUFER2_TO_MAIN_ORDOR 0X86
- #define MAIN_TO_BUFER2_ORDOR 0X55
- #define BUFER2_READ_ORDOR 0x56
- /*******************************************************
- QUEUE_MEMPAGE xdata mem_rbuf;
- QUEUE_MEMPAGE xdata mem_wbuf;
- void MemRbuf_Clear(void)
- {
- memset(&mem_rbuf,0,sizeof(QUEUE_MEMPAGE));
- mem_rbuf.tail=mem_rbuf.head=mem_rbuf.buf;
- }
- void MemWbuf_Clear(void)
- {
- memset(&mem_wbuf,0,sizeof(QUEUE_MEMPAGE));
- mem_wbuf.tail=mem_wbuf.head=mem_wbuf.buf;
- }
- /*******************************************************/
- const BYTE code Default_Pwd[3]={0x11,0x11,0x11};
- const BYTE code YsnOysFlag_Str[]="jjyyonig20050428";
- const BYTE code MyReVision_Str[]="SonOllV1.00A";
- const BYTE code Con_Types_Str[] ="wyhjis";
- Equip_Addr pdata equip_addr; //={0x5A,0x11,111};
- //Card_Info pdata mycard;
- //Op_User_Info xdata op_user_info;//={{0x11,0x11,0x11},{0x11,0x11,0x11},0,0};
- //Con_param_Info xdata con_param_info;
- /*
- ={
- 9999900,
- {0},
- {0},
- JC_MODE,//5
- {0},
- {0},
- {0}
- };
- */
- SeedCard_Info xdata seedcard_info;
- /*
- ={
- {0x11,0x11,0x11},
- {0x11,0x11,0x11},
- 0x11,5,6,{0}
- };
- */
- Myall_SaveData xdata myall_savedata;
- /*
- ={
- CONSUME_RECORD_PAGEADDR,0,
- CONSUME_RECORD_PAGEADDR,0,
- CONSUME_RECORD_PAGEADDR,0
- };
- */
- Con_StatusInfo xdata con_statusinfo; //={0L,0L,0L,0L,0L,0L};
- sttOtherTableInfo xdata stOtherTableInfo;
- //Dc_Peoples xdata dc_peoples; //={0,{0}};
- BYTE xdata cardstate_time[6];//上一次成功下传(卡状态)挂失卡组时的时间
- /**************************************************************/
- void Init_Memory(void)
- {
- MEM_RDY= 1;
- MEM_CS = 1;
- RESET = 0; delay_ms(2); RESET = 1;
- #if(MCU_IC==SST564) //EXTRAM = AUXR^2;
- AUXR &= ~0x02; //0: Internal Expanded RAM access.
- #elif(MCU_IC==SM5964)//OME = SCONF^2;
- SCONF |= 0x02; //1: default is enable.
- #endif
- //MemRbuf_Clear();
- //MemWbuf_Clear();
- memset(&seedcard_info,0,sizeof(SeedCard_Info));//for send card!
- }
- bit SendMemoryData(BYTE *buf,int length)
- {
- int ii;
- Set_Timer20ms(3);
- while(!MEM_RDY)if(!Get_Timer20ms)return(FALSE);
- for(ii=0;ii<length;ii++)
- {
- MemSpiByte_SendReceive(buf[ii]);
- }
- return(TRUE);
- }
- bit ReceiveMemoryData(BYTE *buf,int length)
- {
- int ii;
- Set_Timer20ms(3);
- while(!MEM_RDY)if(!Get_Timer20ms)return(FALSE);
- for(ii=0;ii<length;ii++)
- {
- buf[ii]=MemSpiByte_SendReceive(0xff);
- }
- return(TRUE);
- }
- bit MomeryAddr_Trans(BYTE buf[4],BYTE ordor,WORD pageaddr,WORD byteaddr)
- {
- if((pageaddr>MAX_PAGES-1)||(byteaddr>MEMPAGE_SIZE-1))return(FALSE);
- buf[0] = ordor;
- pageaddr<<= 1;
- buf[1] = (BYTE)(pageaddr/256);
- buf[2] = (BYTE)(pageaddr%256);
- if((BYTE)(byteaddr/256))buf[2] |= 0x01;
- buf[3] = (BYTE)(byteaddr%256);
- return(TRUE);
- }
- BYTE Write_To_Buffer(void *ptr,int length,bit buffer,WORD byteaddr)
- {
- BYTE ordor,buf[4];
- if(buffer)ordor=BUFER2_WRIT_ORDOR;
- else ordor=BUFER1_WRIT_ORDOR;
- if(!MomeryAddr_Trans(buf,ordor,0,byteaddr))return(MEMADDR_ERR);
- MEM_CS = 0;
- if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
- if(!SendMemoryData(ptr,length))return(MEMWRITE_ERR);
- MEM_CS = 1;
- //SendStr(buf,4);
- return(SUCCESS);
- }
- BYTE Buffer_To_Mem(bit buffer,WORD pageaddr)
- {
- BYTE ordor,buf[4];
- if(buffer)ordor=BUFER2_TO_MAIN_ORDOR;
- else ordor=BUFER1_TO_MAIN_ORDOR;
- if(!MomeryAddr_Trans(buf,ordor,pageaddr,0))return(MEMADDR_ERR);
- MEM_CS = 0;
- if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
- MEM_CS = 1;
-
- delay_ms(18);//must!
- //SendStr(buf,4);
- return(SUCCESS);
- }
- BYTE Mem_To_Buffer(bit buffer,WORD pageaddr)
- {
- BYTE ordor,buf[4];
- if(buffer)ordor=MAIN_TO_BUFER2_ORDOR;
- else ordor=MAIN_TO_BUFER1_ORDOR;
- if(!MomeryAddr_Trans(buf,ordor,pageaddr,0))return(MEMADDR_ERR);
- MEM_CS = 0;
- if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
- MEM_CS = 1;
- delay_ms(1);//must!
- //SendStr(buf,4);
- return(SUCCESS);
- }
- BYTE Read_From_Buffer(void *ptr,int length,bit buffer,WORD byteaddr)
- {
- BYTE ordor,buf[5];
- if(buffer)ordor=BUFER2_READ_ORDOR;
- else ordor=BUFER1_READ_ORDOR;
- if(!MomeryAddr_Trans(buf,ordor,0,byteaddr))return(MEMADDR_ERR);
- buf[4] = 0;
- MEM_CS = 0;
- if(!SendMemoryData(buf,5))return(MEMWRITE_ERR);//must "5"!!
- if(!ReceiveMemoryData(ptr,length))return(MEMREAD_ERR);
- MEM_CS = 1;
- //SendStr(buf,5);
- return(SUCCESS);
- }
- BYTE Memory_ArrayRead(void *ptr,int length,WORD pageaddr,WORD byteaddr)
- {
- BYTE buf[8];
- if(!MomeryAddr_Trans(buf,CONTINUE_ARREAD_ORDOR,pageaddr,byteaddr))
- return(MEMADDR_ERR);
- buf[7] = buf[6] =buf[5] =buf[4] =0;
- MEM_CS = 0;
- if(!SendMemoryData(buf,8))return(MEMWRITE_ERR);
- if(!ReceiveMemoryData(ptr,length))return(MEMREAD_ERR);
- MEM_CS = 1;
- //SendStr(buf,8);
- return(SUCCESS);
- }
- BYTE Memory_PageErase(WORD startpage,BYTE pages)
- {
- BYTE i, buf[4];
- for(i=0;i<pages;i++,startpage++)
- {
- if(!MomeryAddr_Trans(buf,PAGE_ERASE_ORDOR,startpage,0))return(MEMADDR_ERR);
- MEM_CS = 0;
- if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
- MEM_CS = 1;
- delay_ms(12);//must!!
- }
- //SendStr(buf,4);
- return(SUCCESS);
- }
- //==============以上为基本函数============================================
- BYTE Save_ConData_to_Memory(Con_Record *con_record)
- {
- int ii,jj;
- WORD save_page;
- #ifdef _DEBUG
- DebugString("Begin Save Record,Current Data:rn");
- DebugData(sizeof(Con_Record),sizeof(Con_Record),(unsigned char *)con_record);
- #endif
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- if(myall_savedata.conbuffer2_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
- {
- #ifdef _DEBUG
- DebugString("Current Data in one pagern");
- DebugString("Current myall_savedata.conbuffer2_byte=");
- DebugData(2,2,(unsigned char *)&myall_savedata.conbuffer2_byte);
- #endif
- if((ii=Write_To_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conbuffer2_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.conbuffer2_byte += sizeof(Con_Record);
- #ifdef _DEBUG
- DebugString("After Edit, myall_savedata.conbuffer2_byte=");
- DebugData(2,2,(unsigned char *)&myall_savedata.conbuffer2_byte);
- #endif
- return(SUCCESS);
- }
- #ifdef _DEBUG
- DebugString("Current Data in two pagern");
- #endif
- save_page = myall_savedata.conwrite_page;
- if(++myall_savedata.conwrite_page > (MAX_PAGES-1))
- {
- //SendStr("1",1);
- //SendStr((BYTE *)&myall_savedata.conwrite_page,2);
- myall_savedata.cycle_flag = MYALL_FORBYTE_FLAG; //数据循环标志。(最大可恢复点)
- myall_savedata.conwrite_page = CON_RECORD_START_PAGEADDR;
- }
- if(myall_savedata.conwrite_page == myall_savedata.conread_page)
- {//数据页满
- //SendStr("2",1);
- //SendStr((BYTE *)&myall_savedata.conread_page,2);
- myall_savedata.conwrite_page=save_page;
- Equip_CurStatus = MEM_FULL_ARALM;
- return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
- }
- jj = (MEMPAGE_SIZE-1)-myall_savedata.conbuffer2_byte;
- if((ii=Write_To_Buffer(con_record,jj,THE_BUFFER_TWO,myall_savedata.conbuffer2_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- Buffer_To_Mem(THE_BUFFER_TWO,save_page);//写入FLASH!
- myall_savedata.conbuffer2_byte = sizeof(Con_Record)-jj;//如果不这样,满时就会多读出一些数据!
- Write_To_Buffer(((BYTE*)con_record)+jj,myall_savedata.conbuffer2_byte,THE_BUFFER_TWO,0);
- /************************************************************************/
- //计算所剩空间,mem will full --提前告警。
- if(myall_savedata.conwrite_page > myall_savedata.conread_page)
- {
- ii =((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii =myall_savedata.conread_page - myall_savedata.conwrite_page;
- if(ii<MYALL_FORBYTE_FLAG)
- {
- Dis_DataFull_Led(TRUE);
- Equip_CurStatus=MEM_WILLFULL_WARNING;
- return(MEM_WILLFULL_WARNING);//goto END;
- }
- return(SUCCESS);
- }
- BYTE Read_ConData_From_Memory(Con_Record *con_record)
- {
- int ii,jj;
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- //SendStr("1",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- if(myall_savedata.lread_page == myall_savedata.conwrite_page)
- {
- if(myall_savedata.lread_byte+sizeof(Con_Record) > myall_savedata.conbuffer2_byte)
- {//没有消费数据
- //myall_savedata.lread_byte = myall_savedata.conbuffer2_byte;//理论上是没必要的!
- return(NO_CON_RECORD);//ii=NO_CON_RECORD;goto END;
- }
- //SendStr("2",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- if((ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.lread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- //SendStr("3",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- myall_savedata.lread_byte += sizeof(Con_Record);
- return(SUCCESS);
- }
- //==============================================================
- //SendStr("4",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- if(myall_savedata.lread_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
- {
- if((ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.lread_page,myall_savedata.lread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.lread_byte += sizeof(Con_Record);
- return(SUCCESS);
- }
- //SendStr("5",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- jj = (MEMPAGE_SIZE-1)-myall_savedata.lread_byte;
- if((ii=Memory_ArrayRead(con_record,jj,myall_savedata.lread_page,myall_savedata.lread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- //SendStr("6",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- //SendStr((BYTE *)&myall_savedata.lread_byte,2);//for test!
- //SendStr((BYTE *)&jj,2);//for test!
- if(++myall_savedata.lread_page>(MAX_PAGES-1))
- {
- myall_savedata.lread_page = CON_RECORD_START_PAGEADDR;
- }
- myall_savedata.lread_byte = sizeof(Con_Record)-jj;
- if(myall_savedata.lread_page == myall_savedata.conwrite_page)
- {//我烤,为了一个(BYTE*)花了老子三四天时间!!惨惨!!!
- ii=Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.lread_byte,THE_BUFFER_TWO,0);
- }
- else
- {
- ii=Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.lread_byte,myall_savedata.lread_page,0);
- }
- //SendStr("7",1);//for test!
- //SendStr(uartSbuf,7);//for test!
- //SendStr((BYTE *)&myall_savedata.lread_byte,2);//for test!
- //SendStr((BYTE *)&jj,2);//for test!
- return(ii);
- }
- BYTE Del_ConData_From_Memory(Con_Record *con_record)
- {
- int ii,jj;
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- if(myall_savedata.conread_page==myall_savedata.conwrite_page)
- {
- if(myall_savedata.conread_byte+sizeof(Con_Record) > myall_savedata.conbuffer2_byte)
- {//没有消费数据
- //myall_savedata.conread_byte = myall_savedata.conbuffer2_byte;//理论上是没必要的!
- return(NO_CON_RECORD);//ii=NO_CON_RECORD;goto END;
- }
- if((ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.conread_byte += sizeof(Con_Record);
-
- return(SUCCESS);
- }
- //===============================================================
- if(myall_savedata.conread_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
- {
- if((ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.conread_page,myall_savedata.conread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.conread_byte += sizeof(Con_Record);
- return(SUCCESS);
- }
- jj = (MEMPAGE_SIZE-1)-myall_savedata.conread_byte;
- if((ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- if(++myall_savedata.conread_page>(MAX_PAGES-1))
- {
- myall_savedata.conread_page = CON_RECORD_START_PAGEADDR;
- }
- myall_savedata.conread_byte = sizeof(Con_Record)-jj;
- if(myall_savedata.conread_page==myall_savedata.conwrite_page) //关键点!!
- {
- ii =Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.conread_byte,THE_BUFFER_TWO,0);
- }
- else
- {
- ii =Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.conread_byte,myall_savedata.conread_page,0);
- }
- if(ii!=SUCCESS)return(ii);
- /************************************************************************
- //mem will full --提前告警。
- if(myall_savedata.conwrite_page>myall_savedata.conread_page)
- {
- ii=((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii=myall_savedata.conread_page-myall_savedata.conwrite_page;
- if(ii>MYALL_FORBYTE_FLAG)
- {
- Equip_CurStatus =SUCCESS;
- Do_DisLed_Flash(0x00,0x00,100);
- Dis_DataFull_Led(FALSE);
- }
- if(ii>1)
- {
- Do_DisLed_Flash(0x00,0x00,100);
- Equip_CurStatus = SUCCESS;
- }
- /************************************************************************/
- //mem will full --提前告警。(剩下的空间)
- if(Equip_CurStatus == MEM_FULL_ARALM)
- {
- Equip_CurStatus = MEM_WILLFULL_WARNING;
- Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
- Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
- }
- else if((Equip_CurStatus == MEM_WILLFULL_WARNING))
- {
- if(myall_savedata.conwrite_page > myall_savedata.conread_page)
- {
- ii=((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii=myall_savedata.conread_page-myall_savedata.conwrite_page;
-
- if(ii>MYALL_FORBYTE_FLAG)
- {
- Equip_CurStatus = 0;
- Dis_DataFull_Led(FALSE);
- Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
- Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
- }
- }
- return(SUCCESS);
- }
- BYTE Renew_ConData_From_Memory(Con_Record *con_record)
- {
- int ii,jj;
- WORD save_page;
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- if(myall_savedata.conread_page == myall_savedata.conwrite_page)
- {
- if(myall_savedata.conread_byte-sizeof(Con_Record) >= 0)
- {
- myall_savedata.conread_byte -= sizeof(Con_Record);
- ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conread_byte);
- return(ii);
- }
- if(--myall_savedata.conread_page < CON_RECORD_START_PAGEADDR)
- {
- //SendStr("3",1);
- //SendStr(&myall_savedata.cycle_flag,1);
- if(myall_savedata.cycle_flag != MYALL_FORBYTE_FLAG)
- {
- myall_savedata.conread_page = CON_RECORD_START_PAGEADDR; //must!!
- return(NO_CON_RECORD); //数据循环标志。(最大可恢复点)
- }
- myall_savedata.conread_page = (MAX_PAGES-1);
- }
- /************************************************************
- 理论上这里不会运行的,因为缓存区不只一两页!!
- if((myall_savedata.conread_page == myall_savedata.conwrite_page)
- && myall_savedata.conread_byte <=0)
- {
- myall_savedata.conread_page++; //已达最大恢复点!
- return(NO_CON_RECORD);
- }
- /**************************************************************/
- jj = sizeof(Con_Record)-myall_savedata.conread_byte;
- if((ii=Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.conread_byte,THE_BUFFER_TWO,0))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.conread_byte = (MEMPAGE_SIZE-1) - jj;
- ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte);
- return(ii); //must!
- }
- //===============================================================
- if(myall_savedata.conread_byte-sizeof(Con_Record) >= 0)
- {
- myall_savedata.conread_byte -= sizeof(Con_Record);
- ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.conread_page,myall_savedata.conread_byte);
- return(ii);//goto END;
- }
- save_page = myall_savedata.conread_page;
- if(--myall_savedata.conread_page < CON_RECORD_START_PAGEADDR)
- {
- if(myall_savedata.cycle_flag != MYALL_FORBYTE_FLAG) //MUST!!!!!!!花了太多时间
- {
- myall_savedata.conread_page = save_page; //must!!
- return(NO_CON_RECORD); //数据循环标志。(最大可恢复点)
- }
- myall_savedata.conread_page = (MAX_PAGES-1);
- }
- if(myall_savedata.conread_page == myall_savedata.conwrite_page) //关键点!!
- {
- myall_savedata.conread_page = save_page; //已达最大恢复点,数据满
- Equip_CurStatus = MEM_FULL_ARALM;
- return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
- }
- jj = sizeof(Con_Record)-myall_savedata.conread_byte;
- if((ii=Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.conread_byte,save_page,0))!=SUCCESS)
- {
- return(ii);//goto END;
- }
- myall_savedata.conread_byte = (MEMPAGE_SIZE-1) - jj;
- ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte);
- if(ii!=SUCCESS)return(ii);
- /************************************************************************
- //mem will full --提前告警。
- if(myall_savedata.conwrite_page > myall_savedata.conread_page)
- {
- ii=((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
- if(ii<MYALL_FORBYTE_FLAG)
- {
- Dis_DataFull_Led(TRUE);
- Equip_CurStatus=MEM_WILLFULL_WARNING;
- return(MEM_WILLFULL_WARNING);//goto END;
- }
- if(ii<2)
- {
- Equip_CurStatus = MEM_FULL_ARALM;
- return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
- }
- /************************************************************************/
- //mem will full --提前告警。
- if(myall_savedata.conwrite_page > myall_savedata.conread_page)
- {
- ii= ((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
- if(ii<MYALL_FORBYTE_FLAG)
- {
- Equip_CurStatus=MEM_WILLFULL_WARNING;
- Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
- Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
- //return(MEM_WILLFULL_WARNING);//goto END;
- //注意:这样将很难完全恢复的。
- }
- return(SUCCESS);
- }
- BYTE Saveparam_To_Mem(void)
- {
- BYTE ii,alllen,len;
- #ifdef _DEBUG
- DebugString("Save param To Memoryrn");
- #endif
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- ii=Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
- if(ii != SUCCESS)return(ii);
- alllen=PARA_START_BYTEADDR;
- len=sizeof(Equip_Addr);
- Write_To_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
- if(ii != SUCCESS)return(ii);
- /*
- alllen += len;
- len=sizeof(Op_User_Info);
- ii=Write_To_Buffer(&op_user_info,len,THE_BUFFER_ONE,alllen);
- if(ii != SUCCESS)return(ii);
- alllen += len;
- len=sizeof(Con_param_Info);
- ii=Write_To_Buffer(&con_param_info,len,THE_BUFFER_ONE,alllen);
- if(ii != SUCCESS)return(ii);
- */
- alllen += len;
- len=sizeof(SeedCard_Info);
- ii=Write_To_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
- if(ii != SUCCESS)return(ii);
- ii=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
- if(ii != SUCCESS)return(ii);
- return(SUCCESS);
- }
- BYTE Save_My_All_Data(void)
- {
- BYTE alllen,len,i;
- #ifdef _DEBUG
- DebugString("Save my All To Memoryrn");
- #endif
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- i=Buffer_To_Mem(THE_BUFFER_TWO,SAVE_BUFFER2_PAGEADDR);
- if(i != SUCCESS)return(i);
- alllen = 0;
- len=sizeof(Myall_SaveData);
- Write_To_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len=sizeof(Con_StatusInfo);
- Write_To_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len=sizeof(sttOtherTableInfo);
- Write_To_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
- /*
- alllen += len;
- len=sizeof(Dc_Peoples);
- Write_To_Buffer(&dc_peoples,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len = 6*sizeof(BYTE);
- Write_To_Buffer(cardstate_time,len,THE_BUFFER_ONE,alllen);
- */
- i=Buffer_To_Mem(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
- return(i);
- }
- BYTE ClearConParam_From_Mem(void) //恢复到初始出厂状态。
- {
- Memory_PageErase(PARAMETER_PAGE1ADDR,2);//FOR TEST.
- return(Memory_Check());
- }
- bit mymem_ok_flag = FALSE;
- BYTE Memory_Check(void) //注意:要保证缺省状态下都能消费!
- {
- // Card_Class_Para pdata card_class;
- BYTE i,alllen,len;
- mymem_ok_flag = TRUE;
- //Memory_PageErase(MYTEST_START_PAGEADDR,MYTEST_END_PAGEADDR);//IS A TEST!
- //Memory_PageErase(PARAMETER_PAGE1ADDR,2);//FOR TEST.
- alllen=PARA_START_BYTEADDR; //for "else if"--cmp rev
- len=strlen(YsnOysFlag_Str);
- for(i=0;i<45;i++)
- {//防止电压不稳等!
- Memory_ArrayRead(uartSbuf,alllen,PARAMETER_PAGE1ADDR,0);
- if(!memcmp(uartSbuf+YSN_FLAG_BYTEADDR,YsnOysFlag_Str,len))break;
- delay_ms(25);
- }
- if(i==45)
- {
- #ifdef _DEBUG
- DebugString("Init Memory all parameterrn");
- #endif
- //SendStr("t0",2);
- //len=strlen(YsnOysFlag_Str);
- Write_To_Buffer(YsnOysFlag_Str,len,THE_BUFFER_ONE,YSN_FLAG_BYTEADDR);
- //-------------------------------------------------------------------
- Read_From_Buffer(uartSbuf,len,THE_BUFFER_ONE,YSN_FLAG_BYTEADDR);
- if(memcmp(uartSbuf+YSN_FLAG_BYTEADDR,YsnOysFlag_Str,len))
- {
- mymem_ok_flag = FALSE;
- Equip_CurStatus= MEM_IC_ERR;
- //SendStr(uartSbuf,len);--for test.
- //SendStr("t1",2);
- return(MEM_IC_ERR);//for time!
- }
- //-------------------------------------------------------------------
- //Memory_PageErase(PARAMETER_PAGE1ADDR,2);
- Memory_PageErase(SAVE_BUFFER1_PAGEADDR,50);
- alllen=YSN_FLAG_BYTEADDR;
- len=strlen(YsnOysFlag_Str);
- Write_To_Buffer(YsnOysFlag_Str,len,THE_BUFFER_ONE,alllen);
- alllen=YSN_REV_BYTEADDR;
- len=strlen(MyReVision_Str);
- Write_To_Buffer(MyReVision_Str,len,THE_BUFFER_ONE,alllen);
- alllen=PARA_START_BYTEADDR;
- len=sizeof(Equip_Addr);
- memset(&equip_addr,0,len);
- equip_addr.ntype=5;
- equip_addr.number_addr=5000;
- //memcpy(uartSbuf,&equip_addr,len);
- i=Write_To_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
- if(i != SUCCESS)return(i);
- /*
- alllen += len;
- len=sizeof(Op_User_Info);
- //memset(&op_user_info,0,len);
- memcpy(op_user_info.op_cardcode,Default_Pwd,3);
- memcpy(op_user_info.op_password,Default_Pwd,3);
- i=Write_To_Buffer(&op_user_info,len,THE_BUFFER_ONE,alllen);
- if(i != SUCCESS)return(i);
- alllen += len;
- len=sizeof(Con_param_Info);
- memset(&con_param_info,0,len);
- con_param_info.con_mode =JC_MODE;//5
- con_param_info.max_money=9999900L;
- for(i=0;i<MAX_PERIOD_S;i++)
- {
- con_param_info.dz_money[i]=100*(i+1);
- con_param_info.Js_money[i]=100*(i+1);
- }
- con_param_info.Js_timer[0]=0x01;
- con_param_info.end_times[0][0]=0x24;
- //memcpy(uartSbuf,&con_param_info,len);
- i=Write_To_Buffer(&con_param_info,len,THE_BUFFER_ONE,alllen);
- if(i != SUCCESS)return(i);
- */
- alllen += len;
- len=sizeof(SeedCard_Info);
- memset(&seedcard_info,0,len);
- memset(seedcard_info.unit_code,0x01,3);
- i=Write_To_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
- if(i != SUCCESS)return(i);
- i=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
- if(i != SUCCESS)return(i);
- //alllen += len;
- //SendStr(&alllen,1); //for test
- /*********************************************************/
- /* alllen = 0;
- len=sizeof(Card_Class_Para);
- memset(&card_class,0,len);
- card_class.daymax_money = 9999900;
- card_class.oncemax_money = 9999900;
- card_class.password_money = 9999900;
- card_class.Kq_time[0][1] = 0x24;
- for(i=0;i<MAX_PERIOD_S;i++)
- {
- card_class.discount[i] = 100;
- card_class.times[i] = 100;
- }
- for(i=0;i<8;i++,alllen += len)
- {
- card_class.card_class=i+1;
- Write_To_Buffer(&card_class,len,THE_BUFFER_ONE,alllen);
- }
- */
- i=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE2ADDR);
- /*********************************************************/
- //i=Memory_PageErase(PARAMETER_PAGE2ADDR,1);
- if(i != SUCCESS)return(i);
- //========================================================
- i=Memory_PageErase(SAVE_BUFFER2_PAGEADDR,1);
- if(i != SUCCESS)return(i);
- alllen = 0;
- len=sizeof(Myall_SaveData);
- memset(&myall_savedata,0,len); //cycle_flag = 0;
- myall_savedata.conwrite_page= CON_RECORD_START_PAGEADDR;
- myall_savedata.conread_page = CON_RECORD_START_PAGEADDR;
- myall_savedata.lread_page = CON_RECORD_START_PAGEADDR;
- myall_savedata.cycle_flag = 0;//理论上不必要!
- Write_To_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len=sizeof(Con_StatusInfo);
- memset(&con_statusinfo,0,len);
- Write_To_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len=sizeof(sttOtherTableInfo);
- memset(&stOtherTableInfo,0,len);
- Write_To_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
- i=Buffer_To_Mem(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
- //SendStr("t1",2);
- if(i != SUCCESS)return(i);
- }
- else if(memcmp(uartSbuf+YSN_REV_BYTEADDR,MyReVision_Str,strlen(MyReVision_Str)))
- {
- Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);//must!!
- len=strlen(MyReVision_Str);
- Write_To_Buffer(MyReVision_Str,len,THE_BUFFER_ONE,YSN_REV_BYTEADDR);
- Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
- }
- i=Restore_My_All_Data();
- //SendStr("t4",2);
- return(i);
- }
- BYTE Restore_My_All_Data(void)
- {
- BYTE alllen,len,i,ii;
- #ifdef _DEBUG
- DebugString("Restore my all datarn");
- #endif
- if(!mymem_ok_flag)return(MEM_IC_ERR);
- i=Mem_To_Buffer(THE_BUFFER_TWO,SAVE_BUFFER2_PAGEADDR);
- if(i != SUCCESS)return(i);
- //DoTest_ArrayRead_Memory(MyALL_SAVEDATA_PAGEADDR);//for test.
- Mem_To_Buffer(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
- alllen = 0;
- len=sizeof(Myall_SaveData);
- Read_From_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
- //memcpy(uartSbuf,&myall_savedata,len);
- //SendStr(uartSbuf,len);
- alllen += len;
- len=sizeof(Con_StatusInfo);
- Read_From_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
- con_statusinfo.con_bootmoney =0L; //本次开机后的消费的总金额;
- con_statusinfo.con_bootrecords=0; //本次开机后消费记录数;
- alllen += len;
- len=sizeof(sttOtherTableInfo);
- Read_From_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
- #ifdef _DEBUG
- DebugString("stOtherTableInfo datarn");
- DebugData(len,len,(unsigned char *)&stOtherTableInfo);
- #endif
- //===========================================================
- //DoTest_ArrayRead_Memory(PARAMETER_PAGE1ADDR);//for test.
- i=Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
- if(i != SUCCESS)return(i);
- alllen=PARA_START_BYTEADDR;
- len=sizeof(Equip_Addr);
- Read_From_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
- alllen += len;
- len=sizeof(SeedCard_Info);
- i=Read_From_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
- /************************************************************************/
- if(myall_savedata.conwrite_page > myall_savedata.conread_page)
- {
- ii=((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
- }
- else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
- if(ii==0)return(i);
- else if(ii==1)
- {
- Equip_CurStatus = MEM_FULL_ARALM;
- Dis_DataFull_Led(TRUE);
- return(MEM_FULL_ARALM);
- }
- else if(ii<MYALL_FORBYTE_FLAG)
- {
- Equip_CurStatus=MEM_WILLFULL_WARNING;
- return(MEM_WILLFULL_WARNING);
- }
- return(i);
- }