📄 nand_flash.c
字号:
/********************************** Header File ********************************
Filename: flash.c
Description: LLD (Low Level Driver) for the NAND flash (528 byte/264 word)
small page family
*******************************************************************************/
#include "71x_lib.h"
#include "NAND_FLASH.h"
#define NANDF_CS (*(volatile unsigned char *)0x66000000)
/*******************************************************************************
HARDWARE DEPENDENT LAYER
Basic functions
*******************************************************************************/
/******************************************************************************
NAND_Open
Function: void NAND_Open()
Arguments: None
Return Value: na
Description: This function is called before a new operation on the NAND starts.
It assert the E of the NAND device. After any operations (such as
NAND_BlockErase, NAND_PageProgram, etc.) ends the NAND_close
function must be called to deassert the E.
This function must be implemented when E don't care option and
can be not implemented otherwise.
******************************************************************************/
void NAND_Open()
{
GPIO_BitWrite(GPIO2,4,1); //set NANF_/WP
GPIO_BitWrite(GPIO2,3,0); //set NANDF_CS low
}
/******************************* NAND_Open *********************************/
/*******************************************************************************
NAND_CommandInput
Function: void NAND_CommandInput(ubyte ubCommand)
Arguments: ubCommand: is the command to be issued to the NAND.
Return Value: na
Description: This function issues a command to the flash and control the
specific hardware signals between the MCU and the device.
*******************************************************************************/
void NAND_CommandInput(ubyte ubCommand)
{
ubyte i;
GPIO_BitWrite(GPIO2,6,0); //set AL low
GPIO_BitWrite(GPIO2,5,1); //set CL high
for(i=0; i<5; i++)
{;}
NANDF_CS = ubCommand; //write command
GPIO_BitWrite(GPIO2,5,0); //set CL low
//GPIO_BitWrite(GPIO2,6,1); //set AL high
}
/************************ NAND_CommandInput ***********************************/
/******************************************************************************
NAND_AddressInput
Function: void NAND_AddressInput(ubyte ubAddress)
Arguments: ubAddress: is the address to be issued to the NAND.
Return Value: na
Description: This function issues an address byte to the flash and control
the specific hardware signals between the MCU and the device
*******************************************************************************/
void NAND_AddressInput(ubyte ubAddress)
{
ubyte i;
GPIO_BitWrite(GPIO2,5,0); //set CL low
GPIO_BitWrite(GPIO2,6,1); //set AL high
for(i=0; i<5; i++)
{;}
NANDF_CS = ubAddress; //write address
GPIO_BitWrite(GPIO2,6,0); //set AL low
}
/************************* NAND_AddressInput *********************************/
/******************************************************************************
NAND_DataInput
Function: void NAND_DataInput(dataWidth ubData)
Arguments: ubData: is the data to be issued to the NAND.
Return Value: na
Description: This function issues an input to the flash and control the
specific hardware signals
******************************************************************************/
void NAND_DataInput(dataWidth ubData)
{
ubyte i;
GPIO_BitWrite(GPIO2,5,0); //set CL low
GPIO_BitWrite(GPIO2,6,0); //set AL low
for(i=0; i<5; i++)
{;}
NANDF_CS = ubData; //write data
}
/**************************** NAND_DataInput *********************************/
/******************************************************************************
NAND_DataOutput
Function: dataWidth NAND_DataOutput()
Arguments: na
Return Value: return the data readed from the flash
Description: This function get a data from the flash and control the
specific hardware signals
*******************************************************************************/
dataWidth NAND_DataOutput()
{
dataWidth outdata;
//ubyte i;
//GPIO_BitWrite(GPIO2,5,0); //set CL low
//GPIO_BitWrite(GPIO2,6,0); //set AL low
//for(i=0; i<6; i++)
//{;}
outdata = NANDF_CS; //read data
return(outdata);
}
/**************************** NAND_DataOutput ********************************/
/******************************************************************************
InsertAddress
Function: void InsertAddress(udword udaddress)
Arguments: udAaddress: the address that we want to issue
Return Value: na
Description: This function perform the data insertion in 4 cycles
(row & column address) or 3 cycle for (128,256 mbit device)
******************************************************************************/
void InsertAddress(udword udAddress)
{
ubyte i;
ubyte first_address;
ubyte second_address;
ubyte third_address;
ubyte forth_address;
GPIO_BitWrite(GPIO2,5,0); //set CL low
GPIO_BitWrite(GPIO2,6,1); //set AL high
first_address = (ubyte)(udAddress & 0xff);
second_address = (ubyte)((udAddress>>9) & 0xff);
third_address = (ubyte)((udAddress>>17)& 0xff);
forth_address = (ubyte)((udAddress>>25)& 0x01);
for(i=0; i<4; i++)
{;}
NANDF_CS = first_address; //write first address
NANDF_CS = second_address; //write second address
NANDF_CS = third_address; //write third address
NANDF_CS = forth_address; //write forth address
GPIO_BitWrite(GPIO2,6,0); //set AL low
}
/******************************************************************************
InsertColumnAddress
Function: void InsertColumnAddress(udword udAddress)
Arguments: udAddress: the address that we want to issue
Return Value: na
Description: This function perform the address insertion of bytes
that represent the column in the page
******************************************************************************/
void InsertColumnAddress(udword udAddress)
{
ubyte i;
ubyte second_address;
ubyte third_address;
ubyte forth_address;
GPIO_BitWrite(GPIO2,5,0); //set CL low
GPIO_BitWrite(GPIO2,6,1); //set AL high
second_address = (ubyte)((udAddress>>9) & 0xff);
third_address = (ubyte)((udAddress>>17)& 0xff);
forth_address = (ubyte)((udAddress>>25)& 0x01);
for(i=0; i<4; i++)
{;}
NANDF_CS = second_address; //write first address
NANDF_CS = third_address; //write second address
NANDF_CS = forth_address; //write third address
GPIO_BitWrite(GPIO2,6,0); //set AL low
}
/************************** InsertColumnAddress *******************************/
/******************************************************************************
NAND_Close
Function: void NAND_Close()
Arguments: na
Return Value: na
Description: This function is called after an operation on the NAND is completed.
It deassert the E of the NAND device.
This function must be implemented when E don't care option and
can be not implemented otherwise.
******************************************************************************/
void NAND_Close()
{
GPIO_BitWrite(GPIO2,3,1); //set NANDF_CS high
GPIO_BitWrite(GPIO2,4,0); //clear NANF_/WP
}
/***************************** NAND_Close *************************************/
/*******************************************************************************
waitForReady
Function: ubyte waitForReady()
Arguments: na
Return Value: Return the value of the status register
Description: This function is called after an operation on the NAND is asserted.
The function wait until the NAND is ready.
NOTE: after return the NAND is lived in status register mode
*******************************************************************************/
ubyte waitForReady()
{
ubyte ubStatus;
NAND_CommandInput((ubyte)0x70);
ubStatus = NAND_DataOutput();
while ( (ubStatus&(0x40)) != 0x40 )
{
ubStatus = NAND_DataOutput();
}
return(ubStatus);
}
/*{采用查询引脚的实现方法
ubyte ubStatus;
for(ubStatus = 0; ubStatus <1; ubStatus++)
{;}
ubStatus = GPIO_BitRead(GPIO2,7);
while ( ubStatus == 0 )
{
ubStatus = GPIO_BitRead(GPIO2,7);
}
return(ubStatus);
}*/
/******************************************************************************
HARDWARE INDIPENDENT LAYER
Nand operation functions
******************************************************************************/
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -