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

📄 nf_drv.c

📁 D12 的源代码
💻 C
📖 第 1 页 / 共 2 页
字号:

extern xdata Byte volatile nf_data; 
extern xdata Byte nf_send_cmd;            /* Command */
extern xdata Byte nf_send_add;            /* Address */

/*_____ EXTERN DEFINITION ________________________________________________*/
extern bdata bit nf_64;                             /* Indicate that the SMC capacity >= 64 Mbytes */
extern bdata bit block_used;
extern bdata bit nf_busy;

extern pdata Byte   gl_buffer[];            /* global buffer of 256 bytes */
extern data  Uint32 gl_ptr_mem;          /* memory data pointer */

extern data  Uint32 address;
extern data  Uint32 current_physical_sector_addr;
extern data  Uint16 look_up_table_block;           /* Look up table block address  */
extern data  Byte index_block_erased;              /* give the number of block to be erased     */

extern idata Uint32 address_look_up_table;
extern idata Byte   index_reassign;                  /* give the number of block to be reassigned */
extern idata Byte   nf_zone_max;                     /* max zone number (from 1 to 8)             */
extern idata Byte   nf_device_type;                  /* Give the size of device in Mbytes   */
extern idata Byte   index_block_used[NF_FREE_BLOCK_SIZE];/* Index of new assigned block */
extern idata Uint16 block_to_be_erased[NF_FREE_BLOCK_SIZE];/* Block address to be erased   */
extern idata Uint16 redundant_logical_block_value;     /* Logical block value in the redundant area */

extern xdata Uint32 nf_disk_size;                  /* Size of the disk in sector - 1   */ 
extern xdata t_reassign_block  reassign_block[NF_FREE_BLOCK_SIZE];
/*_____ C O D E  _________________________________________________________*/
extern code  Byte nf_cis_table[];       /* CIS table */

/*_____ D E F I N I T I O N _____________________________________________*/
/*_____ D E C L A R A T I O N ___________________________________________*/
extern void 	flashwritefromram(Uint16);
extern void 	flashreadtoram(Uint16 offset);
extern void 	initrambuffer(Uint16);
/*F**************************************************************************
* NAME:     nf_check_status
*----------------------------------------------------------------------------
* PARAMS:   
*
* RETURN: SMARTMEDIA status
*
*----------------------------------------------------------------------------
* PURPOSE:  Check the status of the device after a program or an erase
*           operation
***************************************************************************/
bit nf_check_status(void)
{
  Nf_wait_busy();
  /* Status Type Command */
  Nf_send_command(NF_READ_STATUS_CMD);

  if ( (Nf_rd_byte() & 0x01) == 0x00)
  {  
    return OK;
  }
  else
  {
    return KO;
  }
}  

/*F**************************************************************************
* NAME:     nf_calc_logical_block
*----------------------------------------------------------------------------
* PARAMS:   
*           
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  This function calculate the logical block value (used in spare 
*           data area)
*
*****************************************************************************
* NOTE:     
*           
*
*****************************************************************************/
void nf_calc_logical_block(Uint16 value)
{
  volatile Byte dummy;
  bdata bit parity_bit = 0;

  redundant_logical_block_value = (value << 1) + 0x1000;
  /* Parity bit calculation */
  dummy = redundant_logical_block_value;
  parity_bit = P;
  dummy = ((Byte*)&redundant_logical_block_value)[0];
  if (P) parity_bit = ~parity_bit;

  if (parity_bit)
    redundant_logical_block_value++;

}

/*F**************************************************************************
* NAME:     nf_update_spare_data
*----------------------------------------------------------------------------
* PARAMS:   
*           
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  This function update the value of the logical block on the spare
*           data area
*
*****************************************************************************
* NOTE:     
*           
*
*****************************************************************************/
void nf_update_spare_data(void)
{
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(redundant_logical_block_value >> 8);
  Nf_wr_byte(redundant_logical_block_value );
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(redundant_logical_block_value >> 8 );
  Nf_wr_byte(redundant_logical_block_value );
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
  Nf_wr_byte(0xFF);
}



/*F**************************************************************************
* NAME:     nf_init_buffer
*----------------------------------------------------------------------------
* PARAMS:   
*           
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  This function initialize the global buffer at 0xFF
*          
*
*****************************************************************************
* NOTE:     
*           
*
*****************************************************************************/
void nf_init_buffer(void)
{
 	initrambuffer(0);
}
/*F**************************************************************************
* NAME:     nf_download_buffer
*----------------------------------------------------------------------------

*----------------------------------------------------------------------------
* PURPOSE:  This function write the 256 bytes of the global buffer to NF card
    
*           
*
*****************************************************************************/
void nf_download_buffer(void)
{
  flashwritefromram(0);
}


/*F**************************************************************************
* NAME:     nf_upload_buffer
-------------------------------------------------------------------------
* PURPOSE:  This function read 256 bytes from NF card to the global buffer
*          

*****************************************************************************/
void nf_upload_buffer(void)
{
  flashreadtoram(0);
}


/*F**************************************************************************
* NAME:     nf_init_spare
*----------------------------------------------------------------------------
* PARAMS:   
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  Init logical value in redundant data when a block is opened for 
*           the first time and the sector inside the block is not 0.
*
*****************************************************************************
* NOTE:     
*           
*
*****************************************************************************/
void nf_init_spare(void)
{
  Byte j;

  /* Calculate the current physical sector */

  for (j = (gl_ptr_mem & 0x1F); j != 0 ; j--)
  {
    Nf_wait_busy();
    /* Write open on spare data area */
    Nf_send_command (NF_READ_C_AREA_CMD);     
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle         */
    if (nf_64)                                                      /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle         */

    /* Update spare data */
    nf_update_spare_data();

    /* Send program command */
    Nf_send_command (NF_PAGE_PROGRAM_CMD);
    current_physical_sector_addr++;
  }
}



/*F**************************************************************************
* NAME:     nf_copy_block_head
*----------------------------------------------------------------------------
* PARAMS:   block : physical block number
*           nb_sector : number of sector to be copied
*
* RETURN:
*
*----------------------------------------------------------------------------
* PURPOSE:  This function copy the first part of a block that is not modified
*           during a write operation
*
*****************************************************************************
* NOTE: Uint32 address is global     
*           
*
*****************************************************************************/
void nf_copy_block_head(void)
{
  Byte sector_copied;

  /* address of the source physical block */
  address = (Uint32)(block_to_be_erased[index_block_erased]) << 5;  

  for (sector_copied = (gl_ptr_mem & 0x1F); sector_copied != 0; sector_copied--)
  {
    Nf_wait_busy();

    /* 1st half page */
    /* Read Open */
    Nf_send_command (NF_READ_A_AREA_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle         */
    if (nf_64)                                  /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */
    Nf_wait_busy();

    nf_upload_buffer();

    /* Write open */
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle         */
    if (nf_64)                                                      /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle         */

    nf_download_buffer();

    Nf_send_command (NF_PAGE_PROGRAM_CMD);
    Nf_wait_busy();

    /* 2nd half page */
    /* Read Open */
    Nf_send_command (NF_READ_B_AREA_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle         */
    if (nf_64)                                  /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */
    Nf_wait_busy();

    nf_upload_buffer();    

    /* Write open */
	nf_send_w_cmd(MODE_CMDB,1);


    nf_download_buffer();

    /* Update spare data */
    nf_update_spare_data();

    Nf_send_command (NF_PAGE_PROGRAM_CMD);

    /* increase counter */
    current_physical_sector_addr++;
    address++;
  }
}


/*F**************************************************************************
* NAME:     nf_copy_block_tail
*----------------------------------------------------------------------------
* PARAMS:   block : physical block number
*

*
*----------------------------------------------------------------------------
* PURPOSE:  This function copy the last part of a block that is not modified
*           during a write operation
*
*****************************************************************************
* NOTE:     Uint32 address is global
**************************************************************************/
void nf_copy_block_tail(void)
{
  Byte sector_pos;

  current_physical_sector_addr++;

  /* Address of the source block */
  address = ((Uint32)(block_to_be_erased[index_block_erased - 1]) << 5) + 
            (gl_ptr_mem & 0x1F);

  for (sector_pos = (gl_ptr_mem & 0x1F); sector_pos < 0x20; sector_pos++)
  {
    Nf_wait_busy();
    
    /* 1st half page */
    /* Read Open  */
    Nf_send_command (NF_READ_A_AREA_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle         */
    if (nf_64)                                  /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */
    Nf_wait_busy();

    nf_upload_buffer();

    /* Write open  */
    Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[3] );  /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&current_physical_sector_addr)[2] );  /* 3rd address cycle         */
    if (nf_64)                                                      /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&current_physical_sector_addr)[1] );/* 4th address cycle         */

    nf_download_buffer();

    Nf_send_command (NF_PAGE_PROGRAM_CMD);
    Nf_wait_busy();

    /* 2nd half page */
    /* Read Open  */
    Nf_send_command (NF_READ_B_AREA_CMD);
    Nf_send_address (0x00);
    Nf_send_address ( ((Byte*)&address)[3] );   /* 2nd address cycle         */
    Nf_send_address ( ((Byte*)&address)[2] );   /* 3rd address cycle         */
    if (nf_64)                                  /* Size of card >= 64Mbytes ?*/
      Nf_send_address ( ((Byte*)&address)[1] ); /* 4th address cycle         */
    Nf_wait_busy();

    nf_upload_buffer();

    /* Write open  */
nf_send_w_cmd(MODE_CMDB,1);

    nf_download_buffer();

    /* Update spare data */
    nf_update_spare_data();
    
    Nf_send_command (NF_PAGE_PROGRAM_CMD);

    current_physical_sector_addr++;
    address++;
  }
}


/*F**************************************************************************
* NAME:     nf_reassign_block
*----------------------------------------------------------------------------
* PARAMS:   block : reassign the physical block
*
* RETURN:
*
*----------------------------------------------------------------------------

⌨️ 快捷键说明

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