para.lst

来自「mifarea卡程序mifarea卡程序mifarea卡程序」· LST 代码 · 共 357 行

LST
357
字号
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 1   


C51 COMPILER V8.00, COMPILATION OF MODULE PARA
OBJECT MODULE PLACED IN .\out_sst89e564\para.obj
COMPILER INVOKED BY: C:\Keil\C51\BIN\C51.EXE source\para.c LARGE BROWSE ORDER NOAREGS DEBUG OBJECTEXTEND PRINT(.\para.ls
                    -t) OBJECT(.\out_sst89e564\para.obj)

line level    source

   1          /************************************
   2           * para.c                           *
   3           * save parameters                  *
   4           * design by Liulixun               *
   5           * create at 04-13-2007             *
   6           * update: 04-15-2007               *
   7           ************************************/
   8           
   9          /* **************************************
  10              Memory organization
  11           ------------------------
  12          | SEC0 | 55 f0 ...       |
  13           ------------------------
  14          | SEC1 | 55 f0 ...       |
  15           ------------------------
  16           
  17          0x55: head
  18          0xf0: using
  19          0x00: erased
  20          0xff: empty
  21          0xfc: copying
  22          
  23          If a sector is now in use, the 1st 2 byte is 0x55f0, else it should be 0x5500.
  24          When sector 0 is full, we should copy sector 0 to sector 1, then then write 
  25          0x5500 to the begining 2 bytes of sector 0.
  26          
  27          *****************************************/
  28           
  29          #include "includes.h"
  30          
  31          /*
  32          // parameters to save: 
  33          #define PARA_ERASHED        0x00        // parameter erased
  34          #define PARA_HEAD           0x55        // head of parameter
  35              #define H_USING         0xf0
  36              #define H_ERASED        0x00
  37              #define H_COPYING       0xfc
  38              #define H_EMPTY         0xff
  39          #define PARA_EMPTY          0xff        // unused
  40          #define PARA_SWITCHS        0xfe        // switch parameters
  41              #define F_BORFC         0x01        // beep on rf card
  42              #define F_BOTC          0x02        // beep on touch card
  43              #define F_CINV          0x04        // invert package
  44              #define F_NOTICE        0x08        // notice when card status changed
  45              #define F_SNOA          0x10        // stop notice on ack
  46              #define F_ASC           0x20        // auto search card
  47              #define F_LED           0x40        // enable/disable led operation
  48          #define PARA_LORC           0xfd        // led on rf card
  49          #define PARA_LOTC           0xfc        // led on both card
  50          #define PARA_LOBC           0xfb        // led on touch card
  51          #define PARA_LONC           0xfa        // led on none card
  52          #define PARA_RFCCT          0xf9        // rf card config time, 1 for 10 ms
  53          #define PARA_RFRXGAIN       0xf8        //////////////////////////////////////////////////////////////////
             -////////////////////////////////////
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 2   

  54          
  55          #define SECTOR_SIZE         0x80
  56          
  57          */
  58          
  59          static code unsigned int ParaStart = 0x1c00;
  60          static code unsigned int ParaSize = SECTOR_SIZE * 2;
  61          
  62          // copy parameters to next sector when one is full
  63          // dir: 1: sec 0 --> sect 1; 0: sec 1 --> sec 0
  64          static void ParaCopy(bit dir);
  65          
  66          
  67          // write a parameter
  68          // para: parameter type, defined upon
  69          // value: parameter value
  70          // return: positive value for new value, -1 for error
  71          int ParaWrite(unsigned char para, unsigned char value)
  72          {
  73   1          unsigned int i;
  74   1          unsigned char oldvalue;
  75   1          unsigned char oldpara;
  76   1          unsigned int StartAddr;
  77   1          bit fRedo = 0;
  78   1          
  79   1          // find which sector is now in use.
  80   1          if(FlashRdByte(ParaStart+1) == H_USING)
  81   1              StartAddr = ParaStart + 2;
  82   1          else if(FlashRdByte(ParaStart + SECTOR_SIZE + 1) == H_USING)
  83   1              StartAddr = ParaStart + SECTOR_SIZE + 2;
  84   1          else
  85   1              return -1;
  86   1          
  87   1          Redo:
  88   1          // search and write
  89   1          for(i=StartAddr; i<StartAddr+SECTOR_SIZE-2; i+=2)
  90   1          {
  91   2              oldpara = FlashRdByte(i);
  92   2              
  93   2              if(oldpara == PARA_EMPTY)                       // if empty, write para
  94   2              {
  95   3                  FlashWrByte(i, para);
  96   3                  oldpara = FlashRdByte(i);
  97   3              }
  98   2                  
  99   2              if(oldpara == para)                             // same as input para
 100   2              {
 101   3                  oldvalue = FlashRdByte(i+1);
 102   3                  if(oldvalue == value)                       // same value, return success
 103   3                  {
 104   4                      return oldvalue;
 105   4                  }
 106   3                  else if((oldvalue | value) == oldvalue)     // can be over write
 107   3                  {
 108   4                      FlashWrByte(i+1, value);
 109   4                      return FlashRdByte(i+1);
 110   4                  }
 111   3                  else                                        // should be erase 1st
 112   3                  {                                           // then find a new place
 113   4                      FlashWrByte(i, PARA_ERASHED);
 114   4                  }
 115   3              }
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 3   

 116   2          }
 117   1          
 118   1          // check redo flag
 119   1          if(fRedo)
 120   1              return -1;
 121   1          
 122   1          // copy parameters to new sector and erase old one
 123   1          // and update start address
 124   1          if(StartAddr == ParaStart+2)
 125   1          {
 126   2              ParaCopy(1);
 127   2              StartAddr = ParaStart + SECTOR_SIZE + 2;
 128   2          }
 129   1          else
 130   1          {
 131   2              ParaCopy(0);
 132   2              StartAddr = ParaStart + 2;
 133   2          }
 134   1          
 135   1          // update redo flag
 136   1          fRedo = 1;
 137   1          // again
 138   1          goto Redo;
 139   1      }
 140          
 141          // read a pafameter value
 142          // para: parameter to be read
 143          // return: positive value for reading, -1 for error
 144          int ParaRead(unsigned char para)
 145          {
 146   1          unsigned int i;
 147   1          unsigned int StartAddr;
 148   1          unsigned char oldpara;
 149   1          
 150   1          // find which sector is in use now
 151   1          if(FlashRdByte(ParaStart+1) == H_USING)
 152   1              StartAddr = ParaStart + 2;
 153   1          else if(FlashRdByte(ParaStart+SECTOR_SIZE+1) == H_USING)
 154   1              StartAddr = ParaStart + SECTOR_SIZE + 2;
 155   1          else
 156   1              return -1;
 157   1      
 158   1          // search parameter
 159   1          for(i=StartAddr; i<StartAddr+SECTOR_SIZE-2; i+=2)
 160   1          {
 161   2              oldpara = FlashRdByte(i);
 162   2              if(oldpara == para)
 163   2                  return FlashRdByte(i+1);
 164   2              else if(oldpara == PARA_EMPTY)
 165   2                  return -1;
 166   2          }
 167   1          
 168   1          return -1;
 169   1      }
 170          
 171          // parameter sector initialize
 172          // return: <2 for exist, >=2 for new formated
 173          unsigned char ParaMemInit(void)
 174          {
 175   1          unsigned int s0head, s0flag;
 176   1          unsigned int s1head, s1flag;
 177   1          unsigned char ch = 0;
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 4   

 178   1          
 179   1          ComWrite("Mem initializing...\r\n", 21);
 180   1          
 181   1          s0head = FlashRdByte(ParaStart);
 182   1          s0flag = FlashRdByte(ParaStart+1);
 183   1          s1head = FlashRdByte(ParaStart+SECTOR_SIZE);
 184   1          s1flag = FlashRdByte(ParaStart+SECTOR_SIZE+1);
 185   1          
 186   1          checkhead:
 187   1          // check sector 0
 188   1          if(s0head != PARA_HEAD)
 189   1          {
 190   2              FlashErSector(ParaStart);
 191   2              FlashWrByte(ParaStart, PARA_HEAD);
 192   2              s0flag = FlashRdByte(ParaStart+1);
 193   2              ch ++;
 194   2              ComWrite("S0 empty.\r\n", 11);
 195   2          }
 196   1          
 197   1          // check sector 1
 198   1          if(s1head != PARA_HEAD)
 199   1          {
 200   2              FlashErSector(ParaStart+SECTOR_SIZE);
 201   2              FlashWrByte(ParaStart+SECTOR_SIZE, PARA_HEAD);
 202   2              s1flag = FlashRdByte(ParaStart+SECTOR_SIZE+1);
 203   2              ch ++;
 204   2              ComWrite("S1 empty.\r\n", 11);
 205   2          }
 206   1          
 207   1          // deside which sector is to be used
 208   1          if((s0flag == H_EMPTY) && (s1flag == H_EMPTY))
 209   1          {
 210   2              FlashWrByte(ParaStart+1, H_USING);
 211   2              ch += 2;
 212   2              ComWrite("Both empty, use S0.\r\n", 21);
 213   2          }
 214   1          else if((s0flag == H_ERASED) && (s1flag == H_COPYING))
 215   1          {
 216   2              FlashWrByte(ParaStart+SECTOR_SIZE+1, H_USING);
 217   2              ComWrite("Use S1.\r\n", 9);
 218   2          }
 219   1          else if((s1flag == H_ERASED) && (s0flag == H_COPYING))
 220   1          {
 221   2              FlashWrByte(ParaStart+1, H_USING);
 222   2              ComWrite("Use S0.\r\n", 9);
 223   2          }
 224   1          else if((s0flag != H_USING) && (s1flag != H_USING))
 225   1          {
 226   2              s0head = PARA_EMPTY;
 227   2              s1head = PARA_EMPTY;
 228   2              ComWrite("Error, check again and format.\r\n", 32);
 229   2              goto checkhead;
 230   2          }
 231   1          
 232   1          return ch;
 233   1      }
 234          
 235          // set the initialzation values
 236          unsigned char ParaInit(void)
 237          {
 238   1          if(ParaMemInit() >= 2)
 239   1          {
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 5   

 240   2              ComWrite("New mem or mem error, use default values. Writing...\r\n", 54);
 241   2              ParaWrite(PARA_SWITCHS, F_BORFC | F_BOTC | F_NOTICE | F_ASC | F_SNOA | F_LED);
 242   2              ParaWrite(PARA_LORC, 12);
 243   2              ParaWrite(PARA_LOTC, 25);
 244   2              ParaWrite(PARA_LOBC, 0xff);
 245   2              ParaWrite(PARA_LONC, 0);
 246   2              ParaWrite(PARA_RFCCT, 20);
 247   2              ParaWrite(PARA_RFRXGAIN, 2);
 248   2              ParaWrite(PARA_LIMIT, 0xff);
 249   2              ComWrite("Ok.\r\n", 5);
 250   2          }
 251   1          
 252   1          return 0;
 253   1      }
 254          
 255          /******************************************** internal functions ***************************************/
 256          
 257          // copy parameters to next sector when one is full
 258          // dir: 1: sec 0 --> sect 1; 0: sec 1 --> sec 0
 259          static void ParaCopy(bit dir)
 260          {
 261   1          unsigned int i0, i01, i1, i11;
 262   1          unsigned int StartAddr;
 263   1          unsigned char oldpara;
 264   1          
 265   1          // 1, check dir
 266   1          if(dir)
 267   1          {
 268   2              StartAddr = ParaStart + 2;
 269   2              i0 = ParaStart + 2;
 270   2              i1 = ParaStart + SECTOR_SIZE + 2;
 271   2          }
 272   1          else
 273   1          {
 274   2              StartAddr = ParaStart + SECTOR_SIZE + 2;
 275   2              i1 = ParaStart + 2;
 276   2              i0 = ParaStart + SECTOR_SIZE + 2;
 277   2          }
 278   1          
 279   1          i11 = i1;
 280   1          i01 = i0;
 281   1          
 282   1          // 2, if new sector not empty, erase
 283   1          if(FlashRdByte(i1-1) != H_EMPTY)
 284   1              FlashErSector(i1-2);
 285   1          
 286   1          // 3, write head 0 
 287   1          if(FlashRdByte(i1-2) != PARA_HEAD)
 288   1              FlashWrByte(i1-2, PARA_HEAD);
 289   1      
 290   1          // 4, write head 1 to show copying status
 291   1          FlashWrByte(i1-1, H_COPYING);
 292   1          
 293   1          // 5, copy
 294   1          for( ; i0<StartAddr+SECTOR_SIZE-2; i0+=2)
 295   1          {
 296   2              oldpara = FlashRdByte(i0);
 297   2              
 298   2              if(oldpara == PARA_EMPTY)
 299   2              {
 300   3                  break;
 301   3              }
C51 COMPILER V8.00   PARA                                                                  04/23/2009 15:56:17 PAGE 6   

 302   2              if(oldpara == PARA_ERASHED)
 303   2              {
 304   3                  continue;
 305   3              }
 306   2              else
 307   2              {
 308   3                  FlashWrByte(i1, oldpara);
 309   3                  oldpara = FlashRdByte(i0+1);
 310   3                  i1 ++;
 311   3                  FlashWrByte(i1, oldpara);
 312   3                  i1 ++;
 313   3              }
 314   2          }
 315   1          
 316   1          // 6, write head 1 of old sector to show erashed
 317   1          FlashWrByte(i01-1, H_ERASED);
 318   1          // 7, write head 1 of new sector to show using it now
 319   1          FlashWrByte(i11-1, H_USING);
 320   1      }
 321          


MODULE INFORMATION:   STATIC OVERLAYABLE
   CODE SIZE        =   1515    ----
   CONSTANT SIZE    =    186    ----
   XDATA SIZE       =   ----      31
   PDATA SIZE       =   ----    ----
   DATA SIZE        =   ----    ----
   IDATA SIZE       =   ----    ----
   BIT SIZE         =   ----       2
END OF MODULE INFORMATION.


C51 COMPILATION COMPLETE.  0 WARNING(S),  0 ERROR(S)

⌨️ 快捷键说明

复制代码Ctrl + C
搜索代码Ctrl + F
全屏模式F11
增大字号Ctrl + =
减小字号Ctrl + -
显示快捷键?