?? nf.c
字號:
{
while (((Byte*)&gl_cpt_page)[0] != 0x02)
{
Nf_wr_byte_even(0x00);
gl_cpt_page++;
}
nf_update_spare_data();
Nf_send_command_even(NF_PAGE_PROGRAM_CMD);
nf_current_physical_sector_addr_even++;
}
gl_cpt_page = 0;
gl_ptr_mem++;
}
if (nf_block_used)
{
if ( ((Byte*)&gl_ptr_mem)[3] & 0x3F )
nf_copy_block_tail(); /* Copy last part of a block if the block was assigned */
Nf_CS_EVEN();
nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5); /* Erase old block */
Nf_CS_ODD();
nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5); /* Erase old block */
}
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)
{
if (nf_busy)
{
nf_busy = FALSE;
if ( !(((Byte*)&gl_ptr_mem)[3] & 0x3F) ) /* if there is a block change */
{
if (nf_block_used) /* If previous block have to de deleted */
{
Nf_CS_EVEN();
nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5);
Nf_CS_ODD();
nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5);
}
nf_gl_buf_idx++; /* increase the main buffer index */
if (nf_gl_buf_idx >= block_max) /* if block range change */
{
nf_write_open(gl_ptr_mem);
}
else
{
/* if the block in the buffer is already assign, then invert it with a spare block */
if ( !(buf[nf_gl_buf_idx].even.w & 0x8000))
{
if ((buf_free[nf_gl_buf_free_idx].even & 0x7FFF) == lut_block[nf_zone])
{
nf_gl_buf_free_idx++;
if (nf_gl_buf_free_idx >= nf_spare_block)
{
nf_gl_buf_free_idx = 0;
}
}
/* invert the block and assign the next block to be deleted */
nf_block_to_be_deleted_even = buf[nf_gl_buf_idx].even.w;
nf_block_to_be_deleted_odd = buf[nf_gl_buf_idx].odd.w;
buf[nf_gl_buf_idx].even.w = buf_free[nf_gl_buf_free_idx].even & 0x7FFF;
buf[nf_gl_buf_idx].odd.w = buf_free[nf_gl_buf_free_idx].odd & 0x7FFF;
buf_free[nf_gl_buf_free_idx].even = nf_block_to_be_deleted_even | 0x8000;
buf_free[nf_gl_buf_free_idx].odd = nf_block_to_be_deleted_odd | 0x8000;
nf_block_used = TRUE; /* block have to be deleted */
nf_gl_buf_free_idx++; /* increase the spare buffer index */
if ((nf_gl_buf_free_idx++) >= nf_spare_block )
{
nf_gl_buf_free_idx = 0;
}
}
else
{
buf[nf_gl_buf_idx].even.w &= 0x7FFF; /* block is not assigned */
buf[nf_gl_buf_idx].odd.w &= 0x7FFF;
nf_block_used = FALSE;
}
if (nf_gl_buf_idx > nf_gl_buf_idx_max)
nf_gl_buf_idx_max = nf_gl_buf_idx; /* update the max index buffer */
/* Update current physical sector */
nf_current_physical_sector_addr_even = (Uint32)(buf[nf_gl_buf_idx].even.w) << 5;
nf_current_physical_sector_addr_odd = (Uint32)(buf[nf_gl_buf_idx].odd.w) << 5;
nf_logical_block++; /* increase the logical block */
nf_calc_logical_block(); /* calculate the redundant block address */
}
}
if (((Byte*)&gl_ptr_mem)[3] & 0x01)
{
nf_parity_bit = NF_ODD;
}
else
{
nf_parity_bit = NF_EVEN;
}
}
if (!gl_cpt_page) /* if first byte */
{
if (nf_parity_bit == NF_ODD) /* check nand flash slot */
{
Nf_active_ODD(); /* send write open cmd */
Nf_wait_busy_odd(); /* for odd slot */
Nf_write_open_A_area_odd(nf_current_physical_sector_addr_odd, 0x00);
Nf_wr_byte_odd(b); /* write first byte */
}
else
{
Nf_active_EVEN(); /* send write open cmd */
Nf_wait_busy_even(); /* for even slot */
Nf_write_open_A_area_even(nf_current_physical_sector_addr_even, 0x00);
Nf_wr_byte_even(b); /* write first byte */
}
}
else /* if not the first byte */
{
if (nf_parity_bit == NF_ODD) /* check nand flash slot */
{
Nf_wr_byte_odd(b); /* write byte */
}
else
{
Nf_wr_byte_even(b); /* write byte */
}
}
gl_cpt_page++; /* Increase internal page counter */
if (((Byte*)&gl_cpt_page)[0] == 0x02) /* if end of page */
{
nf_update_spare_data(); /* update spare data */
if (nf_parity_bit == NF_ODD)
{
Nf_send_command_odd(NF_PAGE_PROGRAM_CMD); /* send program command */
((Byte*)&nf_current_physical_sector_addr_odd)[3]++; /* increase odd sector */
}
else
{
Nf_send_command_even(NF_PAGE_PROGRAM_CMD); /* send program command */
((Byte*)&nf_current_physical_sector_addr_even)[3]++; /* increase even sector */
}
nf_busy = TRUE; /* set busy flag */
gl_ptr_mem++; /* increase sector pointer */
nf_parity_bit = ~nf_parity_bit; /* change nand flash slot */
gl_cpt_page = 0; /* reset internal page counter */
} /* end if end of page */
return OK;
}
/*F**************************************************************************
* NAME: nf_write_sector
*----------------------------------------------------------------------------
* PARAMS:
* global: gl_ptr_mem
*
* return:
* write status: OK: write done
* KO: write not done
*----------------------------------------------------------------------------
* PURPOSE:
* This function is an optimized function that writes 512 bytes from USB
* controller to NF
*----------------------------------------------------------------------------
* EXAMPLE:
*----------------------------------------------------------------------------
* NOTE:
*----------------------------------------------------------------------------
* REQUIREMENTS:
*****************************************************************************/
bit nf_write_sector (Uint16 nb_sector)
{
Byte j;
do
{
if (nf_busy)
{
nf_busy = FALSE;
if ( !(gl_ptr_mem & 0x3F) ) /* if there is a block change */
{
if (nf_block_used) /* if previous block have to de deleted */
{
Nf_CS_EVEN();
nf_block_erase((Uint32)(nf_block_to_be_deleted_even) << 5);
Nf_CS_ODD();
nf_block_erase((Uint32)(nf_block_to_be_deleted_odd) << 5);
}
nf_gl_buf_idx++; /* increase the main buffer index */
if (nf_gl_buf_idx >= block_max) /* if range block change */
{
nf_write_open(gl_ptr_mem); /* update the main buffer */
}
else /* don't change the buffer */
{
/* if the block in the buffer is already assign, then invert it with a spare block */
if ( !(buf[nf_gl_buf_idx].even.w & 0x8000))
{
if ((buf_free[nf_gl_buf_free_idx].even & 0x7FFF) == lut_block[nf_zone])
{
nf_gl_buf_free_idx++;
if (nf_gl_buf_free_idx >= nf_spare_block)
{
nf_gl_buf_free_idx = 0;
}
}
/* invert the block and assign the next block to be deleted */
nf_block_to_be_deleted_even = buf[nf_gl_buf_idx].even.w;
nf_block_to_be_deleted_odd = buf[nf_gl_buf_idx].odd.w;
buf[nf_gl_buf_idx].even.w = buf_free[nf_gl_buf_free_idx].even & 0x7FFF;
buf[nf_gl_buf_idx].odd.w = buf_free[nf_gl_buf_free_idx].odd & 0x7FFF;
buf_free[nf_gl_buf_free_idx].even = nf_block_to_be_deleted_even | 0x8000;
buf_free[nf_gl_buf_free_idx].odd = nf_block_to_be_deleted_odd | 0x8000;
nf_block_used = TRUE; /* block have to be deleted */
nf_gl_buf_free_idx++; /* increase the spare buffer index */
if ((nf_gl_buf_free_idx++) >= nf_spare_block )
{
nf_gl_buf_free_idx = 0;
}
}
else /* The block is not assigned */
{
buf[nf_gl_buf_idx].even.w &= 0x7FFF;
buf[nf_gl_buf_idx].odd.w &= 0x7FFF;
nf_block_used = FALSE;
}
if (nf_gl_buf_idx > nf_gl_buf_idx_max) /* update the max index buffer */
nf_gl_buf_idx_max = nf_gl_buf_idx;
/* Update current physical sector */
nf_current_physical_sector_addr_even = (Uint32)(buf[nf_gl_buf_idx].even.w) << 5;
nf_current_physical_sector_addr_odd = (Uint32)(buf[nf_gl_buf_idx].odd.w) << 5;
nf_logical_block++; /* increase the logical block */
nf_calc_logical_block(); /* calculate the redundant block address */
}
nf_parity_bit = NF_EVEN;
}
}
if (nf_parity_bit == NF_ODD)
{
Nf_active_ODD();
Nf_wait_busy_odd();
Nf_write_open_A_area_odd(nf_current_physical_sector_addr_odd, 0x00);
for (j = 8; j != 0; j--)
{
while (!Usb_rx_complete()); /* wait end of reception */
Nf_wr_byte_odd(Usb_read_byte()); /* write 64 bytes to card */
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_read_byte());
Nf_wr_byte_odd(Usb_rea
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -