memory.c
上传用户:shyuanyi
上传日期:2008-05-24
资源大小:69k
文件大小:28k
源码类别:

RFID编程

开发平台:

C/C++

  1. #include  "my_head.h"
  2. #include  "mem_addr.h"
  3. #include  "carddef.h"
  4. //#define _DEBUG
  5. #ifdef _DEBUG
  6. #include "Debug.h"
  7. #endif
  8. /********************************************/
  9. #define  MEM_CS     P1_4
  10. #define  MEM_RDY    P1_3
  11. #define  RESET      P1_2
  12. //-------------------------------------------
  13. #define  MYALL_FORBYTE_FLAG        45
  14. #define  THE_BUFFER_ONE            0
  15. #define  THE_BUFFER_TWO            1
  16. #define  CONTINUE_ARREAD_ORDOR     0x68
  17. #define  PAGE_ERASE_ORDOR          0x81
  18. #define  BLOCK_ERASE_ORDOR         0x50
  19. #define  BUFER1_WRIT_ORDOR         0X84
  20. #define  BUFER1_TO_MAIN_ORDOR      0X83
  21. #define  MAIN_TO_BUFER1_ORDOR      0X53
  22. #define  BUFER1_READ_ORDOR         0x54
  23. #define  BUFER2_WRIT_ORDOR         0X87
  24. #define  BUFER2_TO_MAIN_ORDOR      0X86
  25. #define  MAIN_TO_BUFER2_ORDOR      0X55
  26. #define  BUFER2_READ_ORDOR         0x56
  27. /*******************************************************
  28. QUEUE_MEMPAGE  xdata  mem_rbuf;
  29. QUEUE_MEMPAGE  xdata  mem_wbuf;
  30. void  MemRbuf_Clear(void)
  31. {
  32.   memset(&mem_rbuf,0,sizeof(QUEUE_MEMPAGE));
  33.   mem_rbuf.tail=mem_rbuf.head=mem_rbuf.buf;
  34. }
  35. void  MemWbuf_Clear(void)
  36. {
  37.   memset(&mem_wbuf,0,sizeof(QUEUE_MEMPAGE));
  38.   mem_wbuf.tail=mem_wbuf.head=mem_wbuf.buf;
  39. }
  40. /*******************************************************/
  41. const  BYTE     code   Default_Pwd[3]={0x11,0x11,0x11};
  42. const  BYTE     code   YsnOysFlag_Str[]="jjyyonig20050428";
  43. const  BYTE     code   MyReVision_Str[]="SonOllV1.00A";
  44. const  BYTE     code   Con_Types_Str[] ="wyhjis";
  45. Equip_Addr      pdata  equip_addr;  //={0x5A,0x11,111};
  46. //Card_Info       pdata  mycard;
  47. //Op_User_Info    xdata  op_user_info;//={{0x11,0x11,0x11},{0x11,0x11,0x11},0,0};
  48. //Con_param_Info  xdata  con_param_info;
  49. /*
  50. ={
  51. 9999900,
  52. {0},
  53. {0},
  54. JC_MODE,//5
  55. {0},
  56. {0},
  57. {0}
  58. };
  59. */
  60. SeedCard_Info   xdata  seedcard_info;
  61. /*
  62. ={
  63.  {0x11,0x11,0x11},
  64.  {0x11,0x11,0x11},
  65.  0x11,5,6,{0}
  66. };
  67. */
  68. Myall_SaveData  xdata  myall_savedata;
  69. /*
  70. ={
  71. CONSUME_RECORD_PAGEADDR,0,
  72. CONSUME_RECORD_PAGEADDR,0,
  73. CONSUME_RECORD_PAGEADDR,0
  74. };
  75. */
  76. Con_StatusInfo  xdata  con_statusinfo; //={0L,0L,0L,0L,0L,0L};
  77. sttOtherTableInfo xdata stOtherTableInfo;
  78. //Dc_Peoples      xdata  dc_peoples;     //={0,{0}};
  79. BYTE            xdata  cardstate_time[6];//上一次成功下传(卡状态)挂失卡组时的时间
  80. /**************************************************************/
  81. void  Init_Memory(void)
  82. {
  83.    MEM_RDY= 1;
  84.    MEM_CS = 1;
  85.    RESET  = 0; delay_ms(2); RESET  = 1;
  86. #if(MCU_IC==SST564)  //EXTRAM = AUXR^2;
  87.    AUXR  &= ~0x02;   //0: Internal Expanded RAM access.
  88. #elif(MCU_IC==SM5964)//OME = SCONF^2;
  89.    SCONF |= 0x02;    //1: default is enable.
  90. #endif
  91.    //MemRbuf_Clear();
  92.    //MemWbuf_Clear();
  93.    memset(&seedcard_info,0,sizeof(SeedCard_Info));//for  send card!
  94. }
  95. bit  SendMemoryData(BYTE *buf,int length)
  96. {
  97.    int  ii;
  98.    Set_Timer20ms(3);
  99.    while(!MEM_RDY)if(!Get_Timer20ms)return(FALSE);
  100.    for(ii=0;ii<length;ii++)
  101.    {
  102.     MemSpiByte_SendReceive(buf[ii]);
  103.    }
  104.    return(TRUE);
  105. }
  106. bit  ReceiveMemoryData(BYTE *buf,int length)
  107. {
  108.    int   ii;
  109.    Set_Timer20ms(3);
  110.    while(!MEM_RDY)if(!Get_Timer20ms)return(FALSE);
  111.    for(ii=0;ii<length;ii++)
  112.    {
  113. buf[ii]=MemSpiByte_SendReceive(0xff);
  114.    }
  115.    return(TRUE);
  116. }
  117. bit  MomeryAddr_Trans(BYTE buf[4],BYTE ordor,WORD pageaddr,WORD byteaddr)
  118. {
  119.   if((pageaddr>MAX_PAGES-1)||(byteaddr>MEMPAGE_SIZE-1))return(FALSE);
  120.   buf[0]    = ordor;
  121.   pageaddr<<= 1;
  122.   buf[1]    = (BYTE)(pageaddr/256);
  123.   buf[2]    = (BYTE)(pageaddr%256);
  124.   if((BYTE)(byteaddr/256))buf[2] |= 0x01;
  125.   buf[3]    = (BYTE)(byteaddr%256);
  126.   return(TRUE);
  127. }
  128. BYTE  Write_To_Buffer(void *ptr,int length,bit buffer,WORD byteaddr)
  129. {
  130.   BYTE  ordor,buf[4];
  131.   if(buffer)ordor=BUFER2_WRIT_ORDOR;
  132.   else      ordor=BUFER1_WRIT_ORDOR;
  133.   if(!MomeryAddr_Trans(buf,ordor,0,byteaddr))return(MEMADDR_ERR);
  134.   MEM_CS = 0;
  135.   if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
  136.   if(!SendMemoryData(ptr,length))return(MEMWRITE_ERR);
  137.   MEM_CS = 1;
  138.   //SendStr(buf,4);
  139.   return(SUCCESS);
  140. }
  141. BYTE  Buffer_To_Mem(bit buffer,WORD pageaddr)
  142. {
  143.   BYTE  ordor,buf[4];
  144.   if(buffer)ordor=BUFER2_TO_MAIN_ORDOR;
  145.   else      ordor=BUFER1_TO_MAIN_ORDOR;
  146.   if(!MomeryAddr_Trans(buf,ordor,pageaddr,0))return(MEMADDR_ERR);
  147.   MEM_CS = 0;
  148.   if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
  149.   MEM_CS = 1;
  150.   
  151.   delay_ms(18);//must!
  152.   //SendStr(buf,4);
  153.   return(SUCCESS);
  154. }
  155. BYTE  Mem_To_Buffer(bit buffer,WORD pageaddr)
  156. {
  157.   BYTE  ordor,buf[4];
  158.   if(buffer)ordor=MAIN_TO_BUFER2_ORDOR;
  159.   else      ordor=MAIN_TO_BUFER1_ORDOR;
  160.   if(!MomeryAddr_Trans(buf,ordor,pageaddr,0))return(MEMADDR_ERR);
  161.   MEM_CS = 0;
  162.   if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
  163.   MEM_CS = 1;
  164.   delay_ms(1);//must!
  165.   //SendStr(buf,4);
  166.   return(SUCCESS);
  167. }
  168. BYTE  Read_From_Buffer(void *ptr,int length,bit buffer,WORD byteaddr)
  169. {
  170.   BYTE  ordor,buf[5];
  171.   if(buffer)ordor=BUFER2_READ_ORDOR;
  172.   else      ordor=BUFER1_READ_ORDOR;
  173.   if(!MomeryAddr_Trans(buf,ordor,0,byteaddr))return(MEMADDR_ERR);
  174.   buf[4] = 0;
  175.   MEM_CS = 0;
  176.   if(!SendMemoryData(buf,5))return(MEMWRITE_ERR);//must "5"!!
  177.   if(!ReceiveMemoryData(ptr,length))return(MEMREAD_ERR);
  178.   MEM_CS = 1;
  179.   //SendStr(buf,5);
  180.   return(SUCCESS);
  181. }
  182. BYTE  Memory_ArrayRead(void *ptr,int length,WORD pageaddr,WORD byteaddr)
  183. {
  184.   BYTE  buf[8];
  185.   if(!MomeryAddr_Trans(buf,CONTINUE_ARREAD_ORDOR,pageaddr,byteaddr))
  186.      return(MEMADDR_ERR);
  187.   buf[7] = buf[6] =buf[5] =buf[4] =0;
  188.   MEM_CS = 0;
  189.   if(!SendMemoryData(buf,8))return(MEMWRITE_ERR);
  190.   if(!ReceiveMemoryData(ptr,length))return(MEMREAD_ERR);
  191.   MEM_CS = 1;
  192.   //SendStr(buf,8);
  193.   return(SUCCESS);
  194. }
  195. BYTE  Memory_PageErase(WORD startpage,BYTE pages)
  196. {
  197.   BYTE  i, buf[4];
  198.   for(i=0;i<pages;i++,startpage++)
  199.   {
  200.    if(!MomeryAddr_Trans(buf,PAGE_ERASE_ORDOR,startpage,0))return(MEMADDR_ERR);
  201.    MEM_CS = 0;
  202.    if(!SendMemoryData(buf,4))return(MEMWRITE_ERR);
  203.    MEM_CS = 1;
  204.    delay_ms(12);//must!!
  205.   }
  206.   //SendStr(buf,4);
  207.   return(SUCCESS);
  208. }
  209. //==============以上为基本函数============================================
  210. BYTE  Save_ConData_to_Memory(Con_Record  *con_record)
  211. {
  212.   int   ii,jj;
  213.   WORD  save_page;
  214. #ifdef _DEBUG
  215. DebugString("Begin Save Record,Current Data:rn");
  216. DebugData(sizeof(Con_Record),sizeof(Con_Record),(unsigned char *)con_record);
  217. #endif
  218.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  219.   if(myall_savedata.conbuffer2_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
  220.     {
  221. #ifdef _DEBUG
  222. DebugString("Current Data in one pagern");
  223. DebugString("Current myall_savedata.conbuffer2_byte=");
  224. DebugData(2,2,(unsigned char *)&myall_savedata.conbuffer2_byte);
  225. #endif
  226.      if((ii=Write_To_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conbuffer2_byte))!=SUCCESS)
  227.    {
  228.     return(ii);//goto END;
  229.    }
  230.  myall_savedata.conbuffer2_byte += sizeof(Con_Record);
  231. #ifdef _DEBUG
  232. DebugString("After Edit, myall_savedata.conbuffer2_byte=");
  233. DebugData(2,2,(unsigned char *)&myall_savedata.conbuffer2_byte);
  234. #endif
  235.  return(SUCCESS);
  236. }
  237. #ifdef _DEBUG
  238. DebugString("Current Data in two pagern");
  239. #endif
  240.   save_page = myall_savedata.conwrite_page;
  241.   if(++myall_savedata.conwrite_page > (MAX_PAGES-1))
  242.     {
  243.      //SendStr("1",1);
  244.  //SendStr((BYTE *)&myall_savedata.conwrite_page,2);
  245.  myall_savedata.cycle_flag    = MYALL_FORBYTE_FLAG; //数据循环标志。(最大可恢复点)
  246.      myall_savedata.conwrite_page = CON_RECORD_START_PAGEADDR;
  247. }
  248.   if(myall_savedata.conwrite_page == myall_savedata.conread_page)
  249.     {//数据页满
  250.  //SendStr("2",1);
  251.  //SendStr((BYTE *)&myall_savedata.conread_page,2);
  252.  myall_savedata.conwrite_page=save_page;
  253.      Equip_CurStatus = MEM_FULL_ARALM;
  254.      return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
  255. }
  256.   jj = (MEMPAGE_SIZE-1)-myall_savedata.conbuffer2_byte;
  257.   if((ii=Write_To_Buffer(con_record,jj,THE_BUFFER_TWO,myall_savedata.conbuffer2_byte))!=SUCCESS)
  258.     {
  259.       return(ii);//goto END;
  260.     }
  261.   Buffer_To_Mem(THE_BUFFER_TWO,save_page);//写入FLASH!
  262.   myall_savedata.conbuffer2_byte = sizeof(Con_Record)-jj;//如果不这样,满时就会多读出一些数据!
  263.   Write_To_Buffer(((BYTE*)con_record)+jj,myall_savedata.conbuffer2_byte,THE_BUFFER_TWO,0);
  264.   /************************************************************************/
  265.   //计算所剩空间,mem will full --提前告警。
  266.   if(myall_savedata.conwrite_page > myall_savedata.conread_page)
  267.     {
  268.    ii =((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  269. }
  270.   else ii =myall_savedata.conread_page - myall_savedata.conwrite_page;
  271.   if(ii<MYALL_FORBYTE_FLAG)
  272.     {
  273.      Dis_DataFull_Led(TRUE);
  274.      Equip_CurStatus=MEM_WILLFULL_WARNING;
  275.      return(MEM_WILLFULL_WARNING);//goto END;
  276.     }
  277.   return(SUCCESS);
  278. }
  279. BYTE  Read_ConData_From_Memory(Con_Record  *con_record)
  280. {
  281.   int  ii,jj;
  282.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  283.   //SendStr("1",1);//for test!
  284.   //SendStr(uartSbuf,7);//for test!
  285.   if(myall_savedata.lread_page == myall_savedata.conwrite_page)
  286.   {
  287.    if(myall_savedata.lread_byte+sizeof(Con_Record) > myall_savedata.conbuffer2_byte)
  288.      {//没有消费数据
  289.   //myall_savedata.lread_byte = myall_savedata.conbuffer2_byte;//理论上是没必要的!
  290.       return(NO_CON_RECORD);//ii=NO_CON_RECORD;goto END;
  291.  }
  292.    //SendStr("2",1);//for test!
  293.    //SendStr(uartSbuf,7);//for test!
  294.    if((ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.lread_byte))!=SUCCESS)
  295.  {
  296.       return(ii);//goto END;
  297.  }
  298.    //SendStr("3",1);//for test!
  299.    //SendStr(uartSbuf,7);//for test!
  300.    myall_savedata.lread_byte += sizeof(Con_Record);
  301.    return(SUCCESS);
  302.   }
  303.   //==============================================================
  304.   //SendStr("4",1);//for test!
  305.   //SendStr(uartSbuf,7);//for test!
  306.   if(myall_savedata.lread_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
  307.   {
  308.    if((ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.lread_page,myall_savedata.lread_byte))!=SUCCESS)
  309.  {
  310.    return(ii);//goto END;
  311.  }
  312.    myall_savedata.lread_byte += sizeof(Con_Record);
  313.    return(SUCCESS);
  314.   }
  315.   //SendStr("5",1);//for test!
  316.   //SendStr(uartSbuf,7);//for test!
  317.   jj = (MEMPAGE_SIZE-1)-myall_savedata.lread_byte;
  318.   if((ii=Memory_ArrayRead(con_record,jj,myall_savedata.lread_page,myall_savedata.lread_byte))!=SUCCESS)
  319.     {
  320.      return(ii);//goto END;
  321.     }
  322.   //SendStr("6",1);//for test!
  323.   //SendStr(uartSbuf,7);//for test!
  324.   //SendStr((BYTE *)&myall_savedata.lread_byte,2);//for test!
  325.   //SendStr((BYTE *)&jj,2);//for test!
  326.   if(++myall_savedata.lread_page>(MAX_PAGES-1))
  327.     {
  328.      myall_savedata.lread_page = CON_RECORD_START_PAGEADDR;
  329. }
  330.   myall_savedata.lread_byte = sizeof(Con_Record)-jj;
  331.   if(myall_savedata.lread_page == myall_savedata.conwrite_page)
  332.     {//我烤,为了一个(BYTE*)花了老子三四天时间!!惨惨!!!
  333.      ii=Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.lread_byte,THE_BUFFER_TWO,0);
  334. }
  335.   else
  336.     {
  337.  ii=Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.lread_byte,myall_savedata.lread_page,0);
  338.     }
  339.   //SendStr("7",1);//for test!
  340.   //SendStr(uartSbuf,7);//for test!
  341.   //SendStr((BYTE *)&myall_savedata.lread_byte,2);//for test!
  342.   //SendStr((BYTE *)&jj,2);//for test!
  343.   return(ii);
  344. }
  345. BYTE  Del_ConData_From_Memory(Con_Record  *con_record)
  346. {
  347.   int  ii,jj;
  348.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  349.   if(myall_savedata.conread_page==myall_savedata.conwrite_page)
  350.   {
  351.    if(myall_savedata.conread_byte+sizeof(Con_Record) > myall_savedata.conbuffer2_byte)
  352.      {//没有消费数据
  353.   //myall_savedata.conread_byte = myall_savedata.conbuffer2_byte;//理论上是没必要的!
  354.       return(NO_CON_RECORD);//ii=NO_CON_RECORD;goto END;
  355.  }
  356.    if((ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conread_byte))!=SUCCESS)
  357.  {
  358.       return(ii);//goto END;
  359.  }
  360.    myall_savedata.conread_byte += sizeof(Con_Record);
  361.    
  362.    return(SUCCESS);
  363.   }
  364.   //===============================================================
  365.   if(myall_savedata.conread_byte+sizeof(Con_Record) < MEMPAGE_SIZE)
  366. {
  367.  if((ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.conread_page,myall_savedata.conread_byte))!=SUCCESS)
  368.    {
  369. return(ii);//goto END;
  370.    }
  371.  myall_savedata.conread_byte += sizeof(Con_Record);
  372.  return(SUCCESS);
  373.     }
  374.   jj = (MEMPAGE_SIZE-1)-myall_savedata.conread_byte;
  375.   if((ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte))!=SUCCESS)
  376.     {
  377.      return(ii);//goto END;
  378.     }
  379.   if(++myall_savedata.conread_page>(MAX_PAGES-1))
  380.     {
  381.      myall_savedata.conread_page = CON_RECORD_START_PAGEADDR;
  382. }
  383.   myall_savedata.conread_byte = sizeof(Con_Record)-jj;
  384.   if(myall_savedata.conread_page==myall_savedata.conwrite_page) //关键点!!
  385.     {
  386.      ii =Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.conread_byte,THE_BUFFER_TWO,0);
  387. }
  388.   else
  389.     {
  390.  ii =Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.conread_byte,myall_savedata.conread_page,0);
  391.     }
  392.   if(ii!=SUCCESS)return(ii);
  393.   /************************************************************************
  394.   //mem will full --提前告警。
  395.   if(myall_savedata.conwrite_page>myall_savedata.conread_page)
  396.     {
  397.  ii=((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  398. }
  399.   else ii=myall_savedata.conread_page-myall_savedata.conwrite_page;
  400.   if(ii>MYALL_FORBYTE_FLAG)
  401.   {
  402.      Equip_CurStatus =SUCCESS;
  403.      Do_DisLed_Flash(0x00,0x00,100);
  404.      Dis_DataFull_Led(FALSE);
  405.   }
  406.   if(ii>1)
  407.   {
  408.  Do_DisLed_Flash(0x00,0x00,100);
  409.  Equip_CurStatus = SUCCESS;
  410.   }
  411.   /************************************************************************/
  412.   //mem will full --提前告警。(剩下的空间)
  413.   if(Equip_CurStatus == MEM_FULL_ARALM)
  414.     {
  415.  Equip_CurStatus = MEM_WILLFULL_WARNING;
  416.      Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
  417.      Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
  418. }
  419.   else if((Equip_CurStatus == MEM_WILLFULL_WARNING))
  420.     { 
  421.   if(myall_savedata.conwrite_page > myall_savedata.conread_page)
  422.     {
  423.    ii=((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  424. }
  425.   else ii=myall_savedata.conread_page-myall_savedata.conwrite_page;
  426.   if(ii>MYALL_FORBYTE_FLAG)
  427.     {
  428.  Equip_CurStatus = 0;
  429.      Dis_DataFull_Led(FALSE);
  430.      Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
  431.      Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
  432.     }
  433. }
  434.   return(SUCCESS);
  435. }
  436. BYTE  Renew_ConData_From_Memory(Con_Record  *con_record)
  437. {
  438.   int   ii,jj;
  439.   WORD  save_page;
  440.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  441.   if(myall_savedata.conread_page == myall_savedata.conwrite_page)
  442.   {
  443.    if(myall_savedata.conread_byte-sizeof(Con_Record) >= 0)
  444.  {
  445.       myall_savedata.conread_byte -= sizeof(Con_Record);
  446.       ii=Read_From_Buffer(con_record,sizeof(Con_Record),THE_BUFFER_TWO,myall_savedata.conread_byte);
  447.   return(ii);
  448.  }
  449.    if(--myall_savedata.conread_page < CON_RECORD_START_PAGEADDR)
  450.      {
  451.       //SendStr("3",1);
  452.   //SendStr(&myall_savedata.cycle_flag,1);
  453.   if(myall_savedata.cycle_flag != MYALL_FORBYTE_FLAG)
  454.     {
  455.      myall_savedata.conread_page = CON_RECORD_START_PAGEADDR; //must!!
  456.      return(NO_CON_RECORD); //数据循环标志。(最大可恢复点)
  457. }
  458.     myall_savedata.conread_page = (MAX_PAGES-1);
  459.  }
  460.    /************************************************************
  461.    理论上这里不会运行的,因为缓存区不只一两页!!
  462.    if((myall_savedata.conread_page == myall_savedata.conwrite_page)
  463.       && myall_savedata.conread_byte <=0)
  464.      {
  465.        myall_savedata.conread_page++;  //已达最大恢复点!
  466.    return(NO_CON_RECORD);
  467.  }
  468.    /**************************************************************/
  469.    jj = sizeof(Con_Record)-myall_savedata.conread_byte;
  470.    if((ii=Read_From_Buffer(((BYTE*)con_record)+jj,myall_savedata.conread_byte,THE_BUFFER_TWO,0))!=SUCCESS)
  471.      {
  472.    return(ii);//goto END;
  473.  }
  474.    myall_savedata.conread_byte = (MEMPAGE_SIZE-1) - jj;
  475.    ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte);
  476.    return(ii);    //must!
  477.   }
  478.   //===============================================================
  479.   if(myall_savedata.conread_byte-sizeof(Con_Record) >= 0)
  480.     {
  481.      myall_savedata.conread_byte -= sizeof(Con_Record);
  482.      ii=Memory_ArrayRead(con_record,sizeof(Con_Record),myall_savedata.conread_page,myall_savedata.conread_byte);
  483.  return(ii);//goto END;
  484.     }
  485.   save_page = myall_savedata.conread_page;
  486.   if(--myall_savedata.conread_page < CON_RECORD_START_PAGEADDR)
  487.     {
  488.  if(myall_savedata.cycle_flag != MYALL_FORBYTE_FLAG)  //MUST!!!!!!!花了太多时间
  489.    {
  490.     myall_savedata.conread_page = save_page; //must!!
  491.     return(NO_CON_RECORD); //数据循环标志。(最大可恢复点)
  492.    }
  493.    myall_savedata.conread_page = (MAX_PAGES-1);
  494. }
  495.   if(myall_savedata.conread_page == myall_savedata.conwrite_page) //关键点!!
  496.     {
  497.       myall_savedata.conread_page = save_page;  //已达最大恢复点,数据满
  498.       Equip_CurStatus = MEM_FULL_ARALM;
  499.       return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
  500. }
  501.   jj = sizeof(Con_Record)-myall_savedata.conread_byte;
  502.   if((ii=Memory_ArrayRead(((BYTE*)con_record)+jj,myall_savedata.conread_byte,save_page,0))!=SUCCESS)
  503.     {
  504.      return(ii);//goto END;
  505.     }
  506.   myall_savedata.conread_byte = (MEMPAGE_SIZE-1) - jj;
  507.   ii=Memory_ArrayRead(con_record,jj,myall_savedata.conread_page,myall_savedata.conread_byte);
  508.   if(ii!=SUCCESS)return(ii);
  509.   /************************************************************************
  510.   //mem will full --提前告警。
  511.   if(myall_savedata.conwrite_page > myall_savedata.conread_page)
  512.     {
  513.    ii=((MAX_PAGES-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  514. }
  515.   else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
  516.   if(ii<MYALL_FORBYTE_FLAG)
  517.   {
  518.      Dis_DataFull_Led(TRUE);
  519.      Equip_CurStatus=MEM_WILLFULL_WARNING;
  520.      return(MEM_WILLFULL_WARNING);//goto END;
  521.   }
  522.   if(ii<2)
  523.   {
  524.      Equip_CurStatus = MEM_FULL_ARALM;
  525.      return(MEM_FULL_ARALM);//如果不这样下面将可能只写入一半记录!!!
  526.   }
  527.   /************************************************************************/
  528.   //mem will full --提前告警。
  529.   if(myall_savedata.conwrite_page > myall_savedata.conread_page)
  530.     {
  531.    ii= ((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  532. }
  533.   else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
  534.   if(ii<MYALL_FORBYTE_FLAG)
  535.     {
  536.      Equip_CurStatus=MEM_WILLFULL_WARNING;
  537.      Stop_Timer(DISPLAY_DELAYEVENT_Tevent);//msut!!
  538.      Set_TimerEvent(DISPLAY_DELAYEVENT_Tevent,DISPLAY_DELAYEVENT_TIME);
  539.      //return(MEM_WILLFULL_WARNING);//goto END;
  540.  //注意:这样将很难完全恢复的。
  541.     }
  542.   return(SUCCESS);
  543. }
  544. BYTE  Saveparam_To_Mem(void)
  545. {
  546.   BYTE  ii,alllen,len;
  547. #ifdef _DEBUG
  548. DebugString("Save param To Memoryrn");
  549. #endif
  550.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  551.   ii=Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
  552.   if(ii != SUCCESS)return(ii);
  553.   alllen=PARA_START_BYTEADDR;
  554.   len=sizeof(Equip_Addr);
  555.   Write_To_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
  556.   if(ii != SUCCESS)return(ii);
  557. /*
  558.   alllen += len;
  559.   len=sizeof(Op_User_Info);
  560.   ii=Write_To_Buffer(&op_user_info,len,THE_BUFFER_ONE,alllen);
  561.   if(ii != SUCCESS)return(ii);
  562.   alllen += len;
  563.   len=sizeof(Con_param_Info);
  564.   ii=Write_To_Buffer(&con_param_info,len,THE_BUFFER_ONE,alllen);
  565.   if(ii != SUCCESS)return(ii);
  566. */
  567.   alllen += len;
  568.   len=sizeof(SeedCard_Info);
  569.   ii=Write_To_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
  570.   if(ii != SUCCESS)return(ii);
  571.   ii=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
  572.   if(ii != SUCCESS)return(ii);
  573.   return(SUCCESS);
  574. }
  575. BYTE  Save_My_All_Data(void)
  576. {
  577.   BYTE  alllen,len,i;
  578. #ifdef _DEBUG
  579. DebugString("Save my All To Memoryrn");
  580. #endif
  581.   if(!mymem_ok_flag)return(MEM_IC_ERR);
  582.   i=Buffer_To_Mem(THE_BUFFER_TWO,SAVE_BUFFER2_PAGEADDR);
  583.   if(i != SUCCESS)return(i);
  584.   alllen  = 0;
  585.   len=sizeof(Myall_SaveData);
  586.   Write_To_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
  587.   alllen += len;
  588.   len=sizeof(Con_StatusInfo);
  589.   Write_To_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
  590.   alllen += len;
  591.   len=sizeof(sttOtherTableInfo);
  592.   Write_To_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
  593. /*
  594.   alllen += len;
  595.   len=sizeof(Dc_Peoples);
  596.   Write_To_Buffer(&dc_peoples,len,THE_BUFFER_ONE,alllen);
  597.   alllen += len;
  598.   len = 6*sizeof(BYTE);
  599.   Write_To_Buffer(cardstate_time,len,THE_BUFFER_ONE,alllen);
  600. */
  601.   i=Buffer_To_Mem(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
  602.   return(i);
  603. }
  604. BYTE  ClearConParam_From_Mem(void)  //恢复到初始出厂状态。
  605. {
  606.   Memory_PageErase(PARAMETER_PAGE1ADDR,2);//FOR TEST.
  607.   return(Memory_Check());
  608. }
  609. bit   mymem_ok_flag = FALSE;
  610. BYTE  Memory_Check(void)    //注意:要保证缺省状态下都能消费!
  611. {
  612. //   Card_Class_Para  pdata card_class;
  613.    BYTE  i,alllen,len;
  614.    mymem_ok_flag = TRUE;
  615.    //Memory_PageErase(MYTEST_START_PAGEADDR,MYTEST_END_PAGEADDR);//IS A TEST!
  616.    //Memory_PageErase(PARAMETER_PAGE1ADDR,2);//FOR TEST.
  617.    alllen=PARA_START_BYTEADDR; //for "else if"--cmp rev
  618.    len=strlen(YsnOysFlag_Str);
  619.    for(i=0;i<45;i++)
  620.      {//防止电压不稳等!
  621.   Memory_ArrayRead(uartSbuf,alllen,PARAMETER_PAGE1ADDR,0);
  622.   if(!memcmp(uartSbuf+YSN_FLAG_BYTEADDR,YsnOysFlag_Str,len))break;
  623.   delay_ms(25);
  624.  }
  625.    if(i==45)
  626.      {
  627. #ifdef _DEBUG
  628.   DebugString("Init Memory all parameterrn");
  629. #endif
  630.   //SendStr("t0",2);
  631.      //len=strlen(YsnOysFlag_Str);
  632.   Write_To_Buffer(YsnOysFlag_Str,len,THE_BUFFER_ONE,YSN_FLAG_BYTEADDR);
  633.       //-------------------------------------------------------------------
  634.       Read_From_Buffer(uartSbuf,len,THE_BUFFER_ONE,YSN_FLAG_BYTEADDR);
  635.   if(memcmp(uartSbuf+YSN_FLAG_BYTEADDR,YsnOysFlag_Str,len))
  636.     {
  637.        mymem_ok_flag  = FALSE;
  638.  Equip_CurStatus= MEM_IC_ERR;
  639.  //SendStr(uartSbuf,len);--for test.
  640.  //SendStr("t1",2);
  641.      return(MEM_IC_ERR);//for time!
  642. }
  643.   //-------------------------------------------------------------------
  644.       //Memory_PageErase(PARAMETER_PAGE1ADDR,2);
  645.   Memory_PageErase(SAVE_BUFFER1_PAGEADDR,50);
  646.       alllen=YSN_FLAG_BYTEADDR;
  647.   len=strlen(YsnOysFlag_Str);
  648.   Write_To_Buffer(YsnOysFlag_Str,len,THE_BUFFER_ONE,alllen);
  649.       alllen=YSN_REV_BYTEADDR;
  650.      len=strlen(MyReVision_Str);
  651.   Write_To_Buffer(MyReVision_Str,len,THE_BUFFER_ONE,alllen);
  652.      alllen=PARA_START_BYTEADDR;
  653.   len=sizeof(Equip_Addr);
  654.   memset(&equip_addr,0,len);
  655.   equip_addr.ntype=5;
  656.   equip_addr.number_addr=5000;
  657.       //memcpy(uartSbuf,&equip_addr,len);
  658.   i=Write_To_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
  659.   if(i != SUCCESS)return(i);
  660. /*
  661.   alllen += len;
  662.   len=sizeof(Op_User_Info);
  663.   //memset(&op_user_info,0,len);
  664.   memcpy(op_user_info.op_cardcode,Default_Pwd,3);
  665.   memcpy(op_user_info.op_password,Default_Pwd,3);
  666.   i=Write_To_Buffer(&op_user_info,len,THE_BUFFER_ONE,alllen);
  667.   if(i != SUCCESS)return(i);
  668.       alllen += len;
  669.   len=sizeof(Con_param_Info);
  670.   memset(&con_param_info,0,len);
  671.   con_param_info.con_mode =JC_MODE;//5
  672.   con_param_info.max_money=9999900L;
  673.   for(i=0;i<MAX_PERIOD_S;i++)
  674.    {
  675.     con_param_info.dz_money[i]=100*(i+1);
  676.     con_param_info.Js_money[i]=100*(i+1);
  677.    }
  678.   con_param_info.Js_timer[0]=0x01;
  679.   con_param_info.end_times[0][0]=0x24;
  680.       //memcpy(uartSbuf,&con_param_info,len);
  681.       i=Write_To_Buffer(&con_param_info,len,THE_BUFFER_ONE,alllen);
  682.   if(i != SUCCESS)return(i);
  683. */
  684.   alllen += len;
  685.   len=sizeof(SeedCard_Info);
  686.   memset(&seedcard_info,0,len);
  687.   memset(seedcard_info.unit_code,0x01,3);
  688.   i=Write_To_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
  689.   if(i != SUCCESS)return(i);
  690.   i=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
  691.   if(i != SUCCESS)return(i);
  692.   //alllen += len;
  693.       //SendStr(&alllen,1);  //for test
  694.   /*********************************************************/
  695. /*      alllen = 0;
  696.     len=sizeof(Card_Class_Para);
  697.       memset(&card_class,0,len);
  698.   card_class.daymax_money   = 9999900;
  699.   card_class.oncemax_money  = 9999900;
  700.   card_class.password_money = 9999900;
  701.      card_class.Kq_time[0][1]  = 0x24;
  702.   for(i=0;i<MAX_PERIOD_S;i++)
  703.    {
  704.     card_class.discount[i] = 100;
  705. card_class.times[i]    = 100;
  706.    }
  707.   for(i=0;i<8;i++,alllen += len)
  708.    {
  709.    card_class.card_class=i+1;
  710.         Write_To_Buffer(&card_class,len,THE_BUFFER_ONE,alllen);
  711.    }
  712. */
  713.       i=Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE2ADDR);
  714.   /*********************************************************/
  715.       //i=Memory_PageErase(PARAMETER_PAGE2ADDR,1);
  716.      if(i != SUCCESS)return(i);
  717.       //========================================================
  718.   i=Memory_PageErase(SAVE_BUFFER2_PAGEADDR,1);
  719.   if(i != SUCCESS)return(i);
  720.   alllen = 0;
  721.   len=sizeof(Myall_SaveData);
  722.   memset(&myall_savedata,0,len); //cycle_flag = 0;
  723.       myall_savedata.conwrite_page= CON_RECORD_START_PAGEADDR;
  724.       myall_savedata.conread_page = CON_RECORD_START_PAGEADDR;
  725.       myall_savedata.lread_page   = CON_RECORD_START_PAGEADDR;
  726.   myall_savedata.cycle_flag   = 0;//理论上不必要!
  727.   Write_To_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
  728.   alllen += len;
  729.   len=sizeof(Con_StatusInfo);
  730.   memset(&con_statusinfo,0,len);
  731.   Write_To_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
  732.   alllen += len;
  733.   len=sizeof(sttOtherTableInfo);
  734.   memset(&stOtherTableInfo,0,len);
  735.   Write_To_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
  736.   i=Buffer_To_Mem(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
  737.   //SendStr("t1",2);
  738.   if(i != SUCCESS)return(i);
  739.     }
  740.    else if(memcmp(uartSbuf+YSN_REV_BYTEADDR,MyReVision_Str,strlen(MyReVision_Str)))
  741.      {
  742.       Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);//must!!
  743.   len=strlen(MyReVision_Str);
  744.   Write_To_Buffer(MyReVision_Str,len,THE_BUFFER_ONE,YSN_REV_BYTEADDR);
  745.      Buffer_To_Mem(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
  746.  }
  747.    i=Restore_My_All_Data();
  748.    //SendStr("t4",2);
  749.    return(i);
  750. }
  751. BYTE  Restore_My_All_Data(void)
  752. {
  753.    BYTE  alllen,len,i,ii;
  754. #ifdef _DEBUG
  755. DebugString("Restore my all datarn");
  756. #endif
  757.    if(!mymem_ok_flag)return(MEM_IC_ERR);
  758.    i=Mem_To_Buffer(THE_BUFFER_TWO,SAVE_BUFFER2_PAGEADDR);
  759.    if(i != SUCCESS)return(i);
  760.    //DoTest_ArrayRead_Memory(MyALL_SAVEDATA_PAGEADDR);//for test.
  761.    Mem_To_Buffer(THE_BUFFER_ONE,MyALL_SAVEDATA_PAGEADDR);
  762.    alllen = 0;
  763.    len=sizeof(Myall_SaveData);
  764.    Read_From_Buffer(&myall_savedata,len,THE_BUFFER_ONE,alllen);
  765.    //memcpy(uartSbuf,&myall_savedata,len);
  766.    //SendStr(uartSbuf,len);
  767.    alllen += len;
  768.    len=sizeof(Con_StatusInfo);
  769.    Read_From_Buffer(&con_statusinfo,len,THE_BUFFER_ONE,alllen);
  770.    con_statusinfo.con_bootmoney  =0L; //本次开机后的消费的总金额;
  771.    con_statusinfo.con_bootrecords=0;  //本次开机后消费记录数;
  772.    alllen += len;
  773.    len=sizeof(sttOtherTableInfo);
  774.    Read_From_Buffer(&stOtherTableInfo,len,THE_BUFFER_ONE,alllen);
  775. #ifdef _DEBUG
  776. DebugString("stOtherTableInfo datarn");
  777. DebugData(len,len,(unsigned char *)&stOtherTableInfo);
  778. #endif
  779.    //===========================================================
  780.    //DoTest_ArrayRead_Memory(PARAMETER_PAGE1ADDR);//for test.
  781.    i=Mem_To_Buffer(THE_BUFFER_ONE,PARAMETER_PAGE1ADDR);
  782.    if(i != SUCCESS)return(i);
  783.    alllen=PARA_START_BYTEADDR;
  784.    len=sizeof(Equip_Addr);
  785.    Read_From_Buffer(&equip_addr,len,THE_BUFFER_ONE,alllen);
  786.    alllen += len;
  787.    len=sizeof(SeedCard_Info);
  788.    i=Read_From_Buffer(&seedcard_info,len,THE_BUFFER_ONE,alllen);
  789.   /************************************************************************/
  790.   if(myall_savedata.conwrite_page > myall_savedata.conread_page)
  791.     {
  792.    ii=((CON_RECORD_END_PAGEADDR-1)-myall_savedata.conwrite_page)+(myall_savedata.conread_page-CON_RECORD_START_PAGEADDR);
  793. }
  794.   else ii= myall_savedata.conread_page-myall_savedata.conwrite_page;
  795.   if(ii==0)return(i);
  796.   else if(ii==1)
  797.   {
  798.      Equip_CurStatus = MEM_FULL_ARALM;
  799.      Dis_DataFull_Led(TRUE);
  800.      return(MEM_FULL_ARALM);
  801.   }
  802.   else if(ii<MYALL_FORBYTE_FLAG)
  803.   {
  804.      Equip_CurStatus=MEM_WILLFULL_WARNING;
  805.      return(MEM_WILLFULL_WARNING);
  806.   }
  807.   return(i);
  808. }