📄 nf_drv.c
字号:
#include "nf_drv.h" /* nf driver definition */
/*_____ D E C L A R A T I O N ______________________________________________*/
xdata Byte nf_send_cmd At(NF_CMD_LATCH_ENABLE_ADD); /* Command 0x7D00 */
xdata Byte nf_send_add At(NF_ADD_LATCH_ENABLE_ADD); /* Address 0x7E00 */
xdata Byte volatile nf_data At(NF_ADDRESS_CMD_DATA);/* Data nf_data=0x7C00 */
Uint32 nf_block_page_add; // general block and page address
Uchar nf_column_add; // general column address
Uchar nf_ptr_operation; // general pointer operation
Uchar test;
/*****************************************************************/
/*功能:擦除FLASH中某个Block的数据 */
/*在文件系统中,有如下对应关系:Block=Cluster */
/* Page =Sector */
/*输入:Uint32 block_page_add(存放数据的地址) */
/*返回值:bit ( OK=1,KO=0) */
/*****************************************************************/
bit nf_block_erase(){
// Nf_partition_address(pos);
Nf_CS_ON(); // selected Nand Flash device
Nf_wait_busy(); // waiting fot the Nand Flash when it can put into operation
Nf_send_command(NF_BLOCK_ERASE_CMD); // send Auto Block Erase Command 60H
Nf_send_address( ((Byte*)&nf_block_page_add)[3] ); // send block page address
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_send_command(NF_BLOCK_ERASE_CONFIRM_CMD); //send block erase comfire command D0H
Nf_wait_busy();
Nf_send_command(NF_READ_STATUS_CMD); //send Read Status Command 70H
if(Nf_rd_byte() & 0x01){ //I/O_0=1 则擦除失败 需要进行Mask Bad Block
Nf_CS_OFF();
return KO;
}
else{ // I/0_0=0 则擦除成功
Nf_CS_OFF();
return OK;
}
}
/*****************************************************************/
/*功能:从Flash中读取lenth个字节数据 */
/*说明:读取数据的地址由nf_block_page_add */
/* nf_column_add确定上半页还是下半页 */
/*参数:Byte lenth (指定读取数据的字节个数) */
/*返回值:Uchar i (返回实际读取到的字节个数 */
/*****************************************************************/
Uint16 nf_read_bytes(Int16 len){
Uint16 i;
Nf_CS_ON(); // selected Nand Flash device
Nf_send_command(nf_ptr_operation); // nf_ptr_operation maybe is 00h or 01H
// Nf_send_command(NF_READ_CMD); // 00h read first half page, 01h read second half page
Nf_send_address(nf_column_add); // send column address
Nf_send_address( ((Byte*)&nf_block_page_add)[3] ); // send block page address
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_wait_busy();
for(i=0;i<len;i++) // download the content of the flash on buffer
gl_buffer[i]=Nf_rd_byte();
Nf_CS_OFF();
return i;
}
/*****************************************************************/
/*功能:从Flash中读取一个字节数据 */
/*说明:读取数据的地址由nf_block_page_add */
/* nf_column_add确定上半页还是下半页 */
/*参数:Byte lenth (指定读取数据的字节个数) */
/*返回值:Byte c (返回实际读取到的字节 */
/*****************************************************************/
Byte nf_read_byte()
{
Byte c1;
Nf_CS_ON(); // selected Nand Flash device
Nf_send_command(nf_ptr_operation); // nf_ptr_operation maybe is 00h or 01H
// Nf_send_command(NF_READ_CMD); // 00h read first half page, 01h read second half page
Nf_send_address(nf_column_add); // send column address
Nf_send_address( ((Byte*)&nf_block_page_add)[3] ); // send block page address
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_wait_busy();
c1=Nf_rd_byte();
Nf_CS_OFF();
return c1;
}
/************************************************************
* 功能:从一页的备份区中读取一个字节的数据 *
* 说明:要读取的数据的地址由nf_block_page_add *
* 和nf_column_add确定 *
* 参数: void *
* 返回值: Byte 返回读取的字节数据 *
*************************************************************/
Byte nf_read_spare_byte()
{
Byte cc;
Nf_CS_ON();
Nf_send_command(NF_READ_C_AREA_CMD);
// Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address( nf_column_add );
Nf_send_address( ((Byte*)&nf_block_page_add)[3] );
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_wait_busy();
cc=Nf_rd_byte();
Nf_CS_OFF();
return cc;
}
// download the content of the spare sector on gl_buffer
void nf_read_spare_sector(){
Uchar i;
Nf_CS_ON();
Nf_send_command(NF_READ_C_AREA_CMD);
// Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address( 0x00 );
Nf_send_address( ((Byte*)&nf_block_page_add)[3] );
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_wait_busy();
for(i=0;i<16;i++)
gl_buffer[i]=Nf_rd_byte();
Nf_CS_OFF();
}
// 根据nf_block_page_add 和 nf_column_add的值确定
// 具体的spare地址并在此地址上写入一数据
/************************************************************
* 功能:向一页的备份区写入一个字节的数据 *
* 说明:写入数据的地址nf_block_page_add *
* 和nf_column_add确定 *
* nf_ptr_operation确定下到上半页还是下半页 *
* 参数: Byte b *
* 返回值: 1 OK 0 KO *
*************************************************************/
bit nf_write_spare_byte(Byte b){
Nf_CS_ON();
Nf_send_command(NF_READ_C_AREA_CMD);
Nf_send_command(NF_SEQUENTIAL_DATA_INPUT_CMD);
Nf_send_address( nf_column_add);
Nf_send_address( ((Byte*)&nf_block_page_add)[3] );
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_wr_byte(b);
Nf_send_command(NF_PAGE_PROGRAM_CMD); /* valid the page programmation, page program confirm command 10h */
Nf_wait_busy(); /* untill R/B=1 or I/O 6=1 means: waiting page programming to completed */
/* Status Type Command */
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) == 0x00) { /* I/O0 =0 successful program I/O0=1 Error in program */
Nf_CS_OFF();
return OK;
}
else{
Nf_CS_OFF();
return KO;
}
}
/************************************************************
* 功能:向flash写入指定长度的字节数据, *
* 要写入的数据保存在gl_buffer中 *
* 说明:写入数据的地址由nf_block_page_add *
* 和nf_column_add确定 *
* nf_ptr_operation确定上半页还是下半页 *
* 参数: 要写入数据的长度 *
* 返回值: 1 OK 0 KO *
*************************************************************/
bit nf_write_bytes(Int16 len){
Int16 i=0;
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command(nf_ptr_operation); // Destination of pointer (maybe 00H or 01H
Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD); // program command 80h
Nf_send_address(nf_column_add);
Nf_send_address( ((Byte*)&nf_block_page_add)[3]);
Nf_send_address( ((Byte*)&nf_block_page_add)[2]);
Nf_send_address( ((Byte*)&nf_block_page_add)[1]);
for(i=0;i<len;i++) // upload the content of the gl_buffer on the flash
Nf_wr_byte(gl_buffer[i]);
Nf_send_command(NF_PAGE_PROGRAM_CMD); // page program confirm command 10h
Nf_wait_busy(); // untill R/B=1 or I/O_6=1 means: waiting page programming to completed */
/* Status Type Command */
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) ) { // I/O0=1 Error in program
Nf_CS_OFF();
return KO;
}
else{ // I/O_0 =1 successful program
Nf_CS_OFF();
return OK;
}
}
/************************************************************
* 功能:向flash写入一个字节数据, *
* 要写入的数据保存在b中 *
* 说明:写入数据的地址由nf_block_page_add *
* 和nf_column_add确定 *
* nf_ptr_operation确定上半页还是下半页 *
* 参数: 要写入数据的长度 *
* 返回值: 1 OK 0 KO *
*************************************************************/
bit nf_write_byte(Byte b){
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command(nf_ptr_operation); // Destination of pointer (maybe 00H or 01H
Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD); // program command 80h
Nf_send_address(nf_column_add);
Nf_send_address( ((Byte*)&nf_block_page_add)[3]);
Nf_send_address( ((Byte*)&nf_block_page_add)[2]);
Nf_send_address( ((Byte*)&nf_block_page_add)[1]);
Nf_wr_byte(b);
Nf_send_command(NF_PAGE_PROGRAM_CMD); // page program confirm command 10h
Nf_wait_busy(); // untill R/B=1 or I/O_6=1 means: waiting page programming to completed */
/* Status Type Command */
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) ) { // I/O0=1 Error in program
Nf_CS_OFF();
return KO;
}
else{ // I/O_0 =1 successful program
Nf_CS_OFF();
return OK;
}
}
/*****************************************************************/
/*功能:向Flash中写入一页数据,要写入的数据存放在gl_buffer中 */
/*说明:写入数据的地址由nf_block_page_add确定 */
/* nf_ptr_operation确定上半页还是下半页 */
/*参数:void */
/*返回值:Uchar i (返回实际写入到flash中的字节个数 */
/*****************************************************************/
bit nf_write_page(){
int i=0;
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command(nf_ptr_operation); // Destination of pointer (maybe 00H or 01H
Nf_send_command (NF_SEQUENTIAL_DATA_INPUT_CMD); // program command 80h
Nf_send_address(0x00);
Nf_send_address( ((Byte*)&nf_block_page_add)[3]);
Nf_send_address( ((Byte*)&nf_block_page_add)[2]);
Nf_send_address( ((Byte*)&nf_block_page_add)[1]);
for(i=0;i<GL_BUF_SIZE;i++) // upload the content of the gl_buffer on the flash
Nf_wr_byte(gl_buffer[i]);
Nf_send_command(NF_PAGE_PROGRAM_CMD); // page program confirm command 10h
Nf_wait_busy(); // untill R/B=1 or I/O_6=1 means: waiting page programming to completed */
/* Status Type Command */
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) ) { // I/O0=1 Error in program
Nf_CS_OFF();
return KO;
}
else{ // I/O_0 =1 successful program
Nf_CS_OFF();
return OK;
}
}
/************************************************************
* 功能:将源地址的一页数据拷贝到目标地址中 *
* 说明:源地址和目标地址必须在同一plane中,每次拷贝528字节 *
* 参数: Uint32 sourceadd, Uint32 targetadd *
* 返回值: 1 OK 0 KO *
*************************************************************/
bit nf_page_copy_back(Uint32 source_add,Uint32 target_add){
Nf_partition_address(source_add);
// make sure that source_add and target_add must be in the same plane
Nf_CS_ON();
Nf_wait_busy();
Nf_send_command(0x00); // Destination of pointer (maybe 00H or 01H
// Send Read CMD
Nf_send_command(NF_READ_CMD);
// send source address
Nf_send_address( 0x00 );
Nf_send_address( ((Byte*)&nf_block_page_add)[3] );
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
Nf_partition_address(target_add);
Nf_wait_busy();
//send One Page Copy-Back program Operation Command 0Ah
Nf_send_command(NF_COPY_BACK_CMD);
//send target address
Nf_send_address( 0x00);
Nf_send_address( ((Byte*)&nf_block_page_add)[3] );
Nf_send_address( ((Byte*)&nf_block_page_add)[2] );
Nf_send_address( ((Byte*)&nf_block_page_add)[1] );
// send program confirm command
Nf_send_command(NF_PAGE_PROGRAM_CMD);
Nf_wait_busy();
Nf_send_command(NF_READ_STATUS_CMD);
if ( (Nf_rd_byte() & 0x01) ) { // I/O0=1 Error in program
Nf_CS_OFF();
return KO;
}
else{ // I/O_0 =1 successful program
Nf_CS_OFF();
return OK;
}
}
void initial_gl_buffer(){
Uint16 i=0;
for(i=0;i<GL_BUF_SIZE;i++)
gl_buffer[i]=0xAA;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -