📄 nf.c
字号:
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
Usb_write_byte(Nf_rd_byte());
if (begin_ping_pong)
{
begin_ping_pong = FALSE;
}
else
{
while (!Usb_tx_complete()); /* wait end of transfer */
Usb_clear_TXCMPL(); /* ack transfer */
}
Usb_set_TXRDY(); /* start usb transfer */
}
gl_ptr_mem++; /* new page */
Nf_CS_OFF();
if (!(gl_ptr_mem & 0x1F)) /* New block ? */
{
nf_read_open(gl_ptr_mem);
}
else
{
nf_current_physical_sector_addr++;
}
nb_sector--;
}
while (nb_sector != 0);
while (!Usb_tx_complete()); /* wait end of last transfer */
Usb_clear_TXCMPL(); /* ack transfer */
return OK;
}
/*F**************************************************************************
* NAME: nf_write_open
*----------------------------------------------------------------------------
*----------------------------------------------------------------------------
* PARAMS:
* pos: address of the the next write data
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write update
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_open (Uint32 pos)
{
Byte i;
Byte j;
bit change_zone = FALSE;
gl_ptr_mem = pos;
gl_cpt_page = 0;
/* Determine the logical block value */
nf_logical_block = (gl_ptr_mem >> 5);
nf_zone = 0;
while (nf_logical_block > 999)
{
nf_logical_block -= 1000;
nf_zone++;
}
Nf_CS_ON();
if (nf_zone != nf_old_zone)
{
change_zone = TRUE;
if (nf_lut_modified)
{ /* save old lut */
i = nf_old_zone;
nf_old_zone = nf_zone;
nf_zone = i;
nf_reassign_block();
nf_lut_modified = FALSE;
nf_zone = nf_old_zone;
}
else
{ /* update old zone value */
nf_old_zone = nf_zone;
}
nf_block_min = 0xFFFF;
gl_buf_free_idx = 0;
}
if ((nf_logical_block >= nf_block_min) && (nf_logical_block < (nf_block_min + NF_BUFFER_SIZE - 5)))
{ /* we don't have to update the buffer */
nf_calc_logical_block();
nf_gl_buf_idx = nf_logical_block - nf_block_min;
}
else
{ /* we have to update the buffer */
/* Store the look up table block address */
nf_look_up_table_block = nf_lut_block[nf_zone];
/* If LUT have been modified */
if (nf_lut_modified)
{
nf_reassign_block(); /* update the LUT */
}
/* Calculate the address for LUT access */
gl_address = ((Uint32)(nf_look_up_table_block)<<5) + ((Uint32)(nf_logical_block) >> 8);
gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
/* Calculate the redundant block address value */
nf_calc_logical_block();
i = nf_logical_block & 0xFF;
/* For the current zone, initialize the number of spare block */
nf_spare_block = nf_spare_block_number[nf_zone];
/* Calculate the low and the high block stored in the buffer */
nf_block_min = nf_logical_block;
nf_block_max = ((nf_logical_block + NF_BUFFER_SIZE) > 999) ? (1000 - nf_logical_block) : NF_BUFFER_SIZE;
/* Open the look-up table */
Nf_wait_busy();
if (nf_logical_block & 0x80)
{
Nf_send_command(NF_READ_B_AREA_CMD); /* 2nd half page */
Nf_send_address((nf_logical_block << 1) - 256);
}
else
{
Nf_send_command(NF_READ_A_AREA_CMD); /* first half page */
Nf_send_address(nf_logical_block << 1);
}
Nf_send_address (((Byte*)&gl_address)[3]); /* 2nd address cycle */
Nf_send_address (((Byte*)&gl_address)[2]); /* 3rd address cycle */
if (NF_4_CYCLE_ADDRESS) /* Size of card >= 64Mbytes ?*/
Nf_send_address (((Byte*)&gl_address)[1]); /* 4th address cycle */
Nf_wait_busy();
for (j = 0; j < nf_block_max ; j++)
{
/* Read conversion table : 64 datas */
nf_buf[j].b[0] = Nf_rd_byte();
nf_buf[j].b[1] = Nf_rd_byte();
i++;
if (i == 0)
{
gl_address++;
Nf_read_open_A_area(gl_address, 0x00);
}
}
if (change_zone) /* If it is a new zone, then load the spare buffer */
{
/* Check for used block : first, read in the LUT the free physical block */
gl_address = ((Uint32)(nf_look_up_table_block)<<5) + 3;
gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
Nf_read_open_B_area(gl_address, 208);
for (i = 0; i <= (nf_spare_block); i++)
{
nf_buf_free[i] = (Uint16)(Nf_rd_byte()<<8);
nf_buf_free[i] += Nf_rd_byte();
}
}
nf_gl_buf_idx = 0; /* initialize index for main buffer */
nf_gl_buf_idx_max = 0; /* initialize the max index for the buffer */
}
/* if block is already assigned, then invert with a spare block */
nf_block_used = (!(nf_buf[nf_gl_buf_idx].w & 0x8000)) ? TRUE : FALSE;
if (nf_block_used)
{
if ((nf_buf_free[gl_buf_free_idx] & 0x7FFF) == nf_look_up_table_block)
{
gl_buf_free_idx++;
if (gl_buf_free_idx >= nf_spare_block)
{
gl_buf_free_idx = 0;
}
}
/* assign block to be deleted */
nf_block_to_be_deleted = nf_buf[nf_gl_buf_idx].w;
nf_buf[nf_gl_buf_idx].w = nf_buf_free[gl_buf_free_idx];
nf_buf_free[gl_buf_free_idx++] = nf_block_to_be_deleted | 0x8000;
/* increase the index for spare block buffer */
if (gl_buf_free_idx >= nf_spare_block)
{
gl_buf_free_idx = 0;
}
}
/* Mark block as assigned */
nf_buf[nf_gl_buf_idx].w &= 0x7FFF;
if (nf_gl_buf_idx > nf_gl_buf_idx_max)
nf_gl_buf_idx_max = nf_gl_buf_idx;
/* Update the current physical sector address */
nf_current_physical_sector_addr = ((Uint32)(nf_buf[nf_gl_buf_idx].w) << 5);
if (nf_block_used) /* Fisrt block already used block ? */
{
nf_copy_block_head(); /* Copy the first part of the block */
}
else
{
nf_init_spare(); /* else init spare data for new logical block */
}
nf_busy = FALSE; /* Clear flag busy */
nf_lut_modified = TRUE;
/* Set LUT as modified */
gl_address = ((Uint32)(nf_look_up_table_block)<<5);
gl_address += (Uint32)(nf_lut_index[nf_zone])<<2;
Nf_wait_busy();
Nf_write_open_C_area(gl_address, 0x00);
Nf_wr_byte(0x00); /* Reset first byte */
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Send program command to the device */
Nf_wait_busy();
return OK;
}
/*F**************************************************************************
* NAME: nf_write_close
*----------------------------------------------------------------------------
* PARAMS:
*
* return:
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write close: release NF
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_close (void)
{
Nf_CS_ON();
if (gl_cpt_page != 0) /* uncomplete write */
{
if (gl_cpt_page < 256) /* First half page */
{
for (; gl_cpt_page < 256; gl_cpt_page++) gl_buffer[gl_cpt_page] = 0x00;
Nf_wait_busy();
Nf_write_open_A_area(nf_current_physical_sector_addr, 0x00);
nf_download_buffer(); /* Copy the buffer */
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Program the device */
}
/* 2nd half page */
for ( ; gl_cpt_page < 512; gl_cpt_page++)
gl_buffer[gl_cpt_page] = 0x00;
Nf_wait_busy();
Nf_write_open_B_area(nf_current_physical_sector_addr, 0x00);
nf_download_buffer(); /* Copy the buffer */
nf_update_spare_data();
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* Send program command to the device */
gl_cpt_page = 0;
gl_ptr_mem++;
}
/* Copy last part of a block if the block was assigned */
if (nf_block_used)
{
if (gl_ptr_mem & 0x1F)
nf_copy_block_tail();
nf_block_erase((Uint32)(nf_block_to_be_deleted) << 5); /* Erase old block */
}
Nf_wait_busy();
Nf_CS_OFF();
return OK;
}
/*F**************************************************************************
* NAME: nf_write_byte
*----------------------------------------------------------------------------
* PARAMS:
* b: data to write
*
* RETURN:
* write status: OK: write done
* KO: write not done
*
*----------------------------------------------------------------------------
* PURPOSE:
* Low level memory write function
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_byte (Byte b)
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -