⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 nf.lst

📁 串口小程序
💻 LST
📖 第 1 页 / 共 5 页
字号:
 761          /*F**************************************************************************
 762          * NAME: nf_write_open
 763          *----------------------------------------------------------------------------
 764          *----------------------------------------------------------------------------
 765          * PARAMS:
 766          *   pos: address of the the next write data
 767          *
 768          * return:
 769          *----------------------------------------------------------------------------
 770          * PURPOSE:
 771          *   Low level memory write update
 772          *----------------------------------------------------------------------------
 773          * EXAMPLE:
 774          *----------------------------------------------------------------------------
 775          * NOTE:
 776          *----------------------------------------------------------------------------
 777          * REQUIREMENTS:
 778          *****************************************************************************/
 779          bit nf_write_open (Uint32 pos)
 780          {
 781   1      Byte i;
 782   1      Byte j;
 783   1      bit change_zone = FALSE;
 784   1      
 785   1        gl_ptr_mem = pos;
 786   1        gl_cpt_page = 0;
 787   1      
 788   1        /* Determine the logical block value */
 789   1        nf_logical_block = (gl_ptr_mem >> 5);
 790   1        nf_zone = 0;
 791   1      
 792   1        while (nf_logical_block > 999)
 793   1        {
 794   2          nf_logical_block -= 1000;
 795   2          nf_zone++;
 796   2        }
 797   1        Nf_CS_ON();
C51 COMPILER V7.02a   NF                                                                   09/13/2007 11:53:06 PAGE 14  

 798   1      
 799   1        if (nf_zone != nf_old_zone)
 800   1        {
 801   2          change_zone = TRUE;
 802   2          if (nf_lut_modified)
 803   2          { /* save old lut */
 804   3            i = nf_old_zone;
 805   3            nf_old_zone = nf_zone;
 806   3            nf_zone = i;
 807   3            nf_reassign_block();
 808   3            nf_lut_modified = FALSE;
 809   3            nf_zone = nf_old_zone;
 810   3          }
 811   2          else
 812   2          { /* update old zone value */
 813   3            nf_old_zone = nf_zone;
 814   3          }
 815   2          nf_block_min = 0xFFFF;
 816   2          gl_buf_free_idx = 0;
 817   2        }
 818   1      
 819   1        if ((nf_logical_block >= nf_block_min) && (nf_logical_block < (nf_block_min + NF_BUFFER_SIZE - 5)))
 820   1        { /* we don't have to update the buffer */
 821   2          nf_calc_logical_block();
 822   2          nf_gl_buf_idx = nf_logical_block - nf_block_min;
 823   2        }
 824   1        else 
 825   1        { /* we have to update the buffer */
 826   2          /* Store the look up table block address */
 827   2          nf_look_up_table_block =  nf_lut_block[nf_zone];
 828   2          /* If LUT have been modified */
 829   2          if (nf_lut_modified)           
 830   2          {
 831   3            nf_reassign_block();                  /* update the LUT */
 832   3          }
 833   2          /* Calculate the address for LUT access */
 834   2          gl_address = ((Uint32)(nf_look_up_table_block)<<5) + ((Uint32)(nf_logical_block) >> 8);
 835   2          gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
 836   2          /* Calculate the redundant block address value */
 837   2          nf_calc_logical_block();
 838   2          i = nf_logical_block;
 839   2          /* For the current zone, initialize the number of spare block */
 840   2          nf_spare_block = nf_spare_block_number[nf_zone];
 841   2          /* Calculate the low and the high block stored in the buffer */
 842   2          nf_block_min = nf_logical_block;
 843   2          nf_block_max = ((nf_logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - nf_logical_block) : NF_BUFFER_SIZ
             -E;
 844   2      
 845   2          /* Open the look-up table */
 846   2          Nf_wait_busy();     
 847   2          if (((Byte*)&nf_logical_block)[1] & 0x80)
 848   2          {
 849   3            Nf_send_command(NF_READ_B_AREA_CMD);  /* 2nd half page */
 850   3            Nf_send_address((nf_logical_block << 1) - 256);
 851   3          }
 852   2          else
 853   2          {
 854   3            Nf_send_command(NF_READ_A_AREA_CMD);  /* first half page */
 855   3            Nf_send_address(nf_logical_block << 1);
 856   3          }
 857   2      
 858   2          Nf_send_address (((Byte*)&gl_address)[3]);     /* 2nd address cycle         */
C51 COMPILER V7.02a   NF                                                                   09/13/2007 11:53:06 PAGE 15  

 859   2          Nf_send_address (((Byte*)&gl_address)[2]);     /* 3rd address cycle         */
 860   2          if (NF_4_CYCLE_ADDRESS_BIT)                    /* Size of card >= 64Mbytes ?*/
 861   2            Nf_send_address ( ((Byte*)&gl_address)[1] ); /* 4th address cycle         */
 862   2        
 863   2          Nf_wait_busy();
 864   2      
 865   2          for (j = 0; j < nf_block_max ; j++)
 866   2          {
 867   3            /* Read conversion table : 64 datas */
 868   3            nf_buf[j].b[0] = Nf_rd_byte();
 869   3            nf_buf[j].b[1] = Nf_rd_byte();
 870   3            i++;
 871   3            if (i == 0)
 872   3            {
 873   4              gl_address++;
 874   4              Nf_read_open_A_area(gl_address, 0x00);
 875   4            }
 876   3          }
 877   2        
 878   2          if (change_zone)        /* If it is a new zone, then load the spare buffer */
 879   2          {
 880   3            /* Check for used block : first, read in the LUT the free physical block */
 881   3            gl_address = ((Uint32)(nf_look_up_table_block)<<5) + 3;
 882   3            gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
 883   3            Nf_read_open_B_area(gl_address, 208);
 884   3            for (i = 0; i <= (nf_spare_block); i++)
 885   3            {
 886   4              nf_buf_free[i] = (Uint16)(Nf_rd_byte()<<8);
 887   4              nf_buf_free[i] +=  Nf_rd_byte();
 888   4            }
 889   3          }
 890   2          nf_gl_buf_idx = 0;           /* initialize index for main buffer */
 891   2          nf_gl_buf_idx_max = 0;       /* initialize the max index for the buffer */
 892   2        }
 893   1        
 894   1        /* if block is already assigned, then invert with a spare block */
 895   1        nf_block_used = (!(nf_buf[nf_gl_buf_idx].w & 0x8000)) ? TRUE : FALSE;
 896   1        if (nf_block_used)
 897   1        {
 898   2          if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
 899   2          {
 900   3            gl_buf_free_idx++;
 901   3            if (gl_buf_free_idx >= nf_spare_block)
 902   3            {
 903   4              gl_buf_free_idx = 0;
 904   4            }      
 905   3          }
 906   2          /* assign block to be deleted */
 907   2          nf_block_to_be_deleted = nf_buf[nf_gl_buf_idx].w;
 908   2          nf_buf[nf_gl_buf_idx].w = nf_buf_free[gl_buf_free_idx];
 909   2          nf_buf_free[gl_buf_free_idx++] = nf_block_to_be_deleted | 0x8000;
 910   2          /* increase the index for spare block buffer */
 911   2          if (gl_buf_free_idx >= nf_spare_block)
 912   2          {
 913   3            gl_buf_free_idx = 0;
 914   3          }
 915   2        }
 916   1      
 917   1        /* Mark block as assigned */
 918   1        nf_buf[nf_gl_buf_idx].w &= 0x7FFF;       
 919   1      
 920   1        if (nf_gl_buf_idx > nf_gl_buf_idx_max)
C51 COMPILER V7.02a   NF                                                                   09/13/2007 11:53:06 PAGE 16  

 921   1          nf_gl_buf_idx_max = nf_gl_buf_idx;
 922   1      
 923   1        /* Update the current physical sector address */
 924   1        nf_current_physical_sector_addr = ((Uint32)(nf_buf[nf_gl_buf_idx].w) << 5);
 925   1      
 926   1        if (nf_block_used)                        /* Fisrt block already used block ?  */
 927   1        { 
 928   2          nf_copy_block_head();                   /* Copy the first part of the block  */
 929   2        }
 930   1        else                                 
 931   1        { 
 932   2          nf_init_spare();                        /* else init spare data for new logical block  */
 933   2        }
 934   1      
 935   1        nf_busy = FALSE;                          /* Clear flag busy */
 936   1        nf_lut_modified = TRUE;
 937   1        /* Set LUT as modified */
 938   1        gl_address = ((Uint32)(nf_look_up_table_block)<<5);
 939   1        gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
 940   1        Nf_wait_busy();
 941   1        Nf_write_open_C_area(gl_address, 0x00);
 942   1        Nf_wr_byte(0x00);                         /* Reset first byte */
 943   1        Nf_send_command(NF_PAGE_PROGRAM_CMD);     /* Send program command to the device */
 944   1        
 945   1        return OK; 
 946   1      }
 947          
 948          
 949          /*F**************************************************************************
 950          * NAME: nf_write_close
 951          *----------------------------------------------------------------------------
 952          * PARAMS:
 953          *
 954          * return:
 955          *----------------------------------------------------------------------------
 956          * PURPOSE:
 957          *   Low level memory write close: release NF 
 958          *----------------------------------------------------------------------------
 959          * EXAMPLE:
 960          *----------------------------------------------------------------------------
 961          * NOTE:
 962          *----------------------------------------------------------------------------
 963          * REQUIREMENTS:
 964          *****************************************************************************/
 965          bit nf_write_close (void)
 966          { 
 967   1        Nf_CS_ON();
 968   1        if (gl_cpt_page)
 969   1        {
 970   2          while (((Byte*)&gl_cpt_page)[0] != NF_DATA_SIZE_H)
 971   2          {
 972   3            gl_cpt_page++;
 973   3            Nf_wr_byte(0x00);
 974   3          }
 975   2          nf_update_spare_data();
 976   2          Nf_send_command(NF_PAGE_PROGRAM_CMD);
 977   2          ((Byte*)&gl_cpt_page)[0] = 0;
 978   2          gl_ptr_mem++;
 979   2          ((Byte*)&nf_current_physical_sector_addr)[3]++;
 980   2        }
 981   1      
 982   1        /* Copy last part of a block if the block was assigned */
C51 COMPILER V7.02a   NF                                                                   09/13/2007 11:53:06 PAGE 17  

 983   1        if (nf_block_used)
 984   1        {
 985   2          if (((Byte*)&gl_ptr_mem)[3] & NF_BLOCK_MASK) 
 986   2            nf_copy_block_tail();
 987   2          nf_block_erase((Uint32)(nf_block_to_be_deleted) << 5);    /* Erase old block */
 988   2        }
 989   1        Nf_CS_OFF();
 990   1        return OK;
 991   1      }
 992          
 993          
 994          /*F**************************************************************************
 995          * NAME: nf_write_byte
 996          *----------------------------------------------------------------------------
 997          * PARAMS:
 998          *   b: data to write
 999          *
1000          * RETURN:
1001          *   write status: OK: write done
1002          *                 KO: write not done
1003          *
1004          *----------------------------------------------------------------------------
1005          * PURPOSE:
1006          *   Low level memory write function
1007          *----------------------------------------------------------------------------
1008          * EXAMPLE:
1009          *----------------------------------------------------------------------------
1010          * NOTE:
1011          *----------------------------------------------------------------------------
1012          * REQUIREMENTS:
1013          *****************************************************************************/
1014          bit nf_write_byte (Byte b)
1015          {
1016   1        if (nf_busy)

⌨️ 快捷键说明

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