如果你认为本程序对您有价值,请反馈意见
/*
*******************************************************************************************************************
*** 工程名称: QDT64481BC终端主板控制器程序
*** 文件名称: K9F1G08_Driver.C
*** 文件用途:NAND FLASH驱动函数
*** 其他说明:实现NAND FLASH 驱动函数功能
*** 最后修改:
*******************************************************************************************************************
*/
#include"Project.H"
/*
==================================================================================================================
=== 功能描述:读取FLASH的ID
=== 入口参数:无
=== 出口参数:返回id ,与FLASH_ID比较
==================================================================================================================
*/
void Flash_Reset(void)
{FLASH_CE = 0;
FLASH_ALE = 0;
FLASH_CLE = 1;
FLASH_RE = 1;
FLASH_WE = 0;
FLASH_DATA=0x90;
FLASH_WE = 1;
Delay(10);
while(!FLASH_RB);
FLASH_CLE = 0;
}
/*
==================================================================================================================
=== 功能描述:读取FLASH的ID
=== 入口参数:无
=== 出口参数:返回id ,与FLASH_ID比较
==================================================================================================================
*/
uint Flash_ChekeId(void) //本函数成功读取ID
{uint id;
FLASH_CE = 0;
FLASH_ALE = 0;
FLASH_CLE = 1;
FLASH_RE = 1;
FLASH_WE = 0;
FLASH_DATA=0x90;
FLASH_WE = 1;
while(!FLASH_RB);
FLASH_RE = 1;
FLASH_ALE = 1;
FLASH_CLE = 0;
FLASH_WE = 0;
FLASH_DATA=0x00;
FLASH_WE = 1;
while(!FLASH_RB);
FLASH_ALE = 0;
FLASH_RE = 0;
FLASH_DATA=0xff;
id=(((uint)FLASH_DATA)<<8);
FLASH_RE = 1;
FLASH_RE = 0;
FLASH_DATA=0xff;
id|=(((uint)FLASH_DATA));
FLASH_RE = 1;
return id;
}
/*
==================================================================================================================
=== 功能描述:擦除FLASH中的一块(BLOCK)
=== 入口参数:指定擦除的块号(FLASH_BLOCK);
=== 出口参数:0-擦除成功 1-擦除失败
==================================================================================================================
*/
uchar Flash_EraseBlock(unsigned long Block)
{
ucharstate=1;
unsigned long BlockPage;
BlockPage = Block<<18; //(5-->14)
FLASH_CE=0;
FLASH_RE=1;
FLASH_CLE=1;
FLASH_ALE=0;
FLASH_WE=0;
FLASH_DATA = 0x60;
FLASH_WE=1;
FLASH_CLE=0;
FLASH_ALE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)(BlockPage>>12 & 0xC0);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)(BlockPage>>20 & 0xFF);
FLASH_WE=1;
FLASH_ALE=0;
FLASH_CLE=1;
FLASH_WE=0;
FLASH_DATA=0xD0;
FLASH_WE=1;
FLASH_CLE=0;
Delay(10); //wait tWB(100ns)
while(FLASH_RB==0);
FLASH_CLE=1;
FLASH_WE=0;
FLASH_DATA=0x70;
FLASH_WE=1;
FLASH_CLE=0;
FLASH_RE=0;
FLASH_DATA=0xff;
state=(bit)(FLASH_DATA&0x01);
FLASH_RE=1;
return(state);
}
/*
==================================================================================================================
=== 功能描述:对FLASH指定的块(FLASH_BLOCK)+页(FLASH_PAGE)写入数据
=== 入口参数:*str 欲写入FLASH的数据;Block块号(FLASH_BLOCK);Page页号(FLASH_PAGE);
=== 出口参数:0-写FLASH成功 1-写FLASH失败
==================================================================================================================
*/
uchar Flash_write(uchar *str,unsigned long Block,uchar Page)
{
ushort i;
ucharstate=1;
unsigned long BlockPage;
Page &=0x1F;
BlockPage = (Block<<18) + (Page<<12);//(<<9)
FLASH_CE=0;
FLASH_RE=1;
FLASH_CLE=1;
FLASH_ALE=0;
FLASH_WE=0;
FLASH_DATA=0x80;
FLASH_WE=1;
FLASH_ALE=1;
FLASH_CLE=0;
FLASH_WE=0;
FLASH_DATA = (uchar)(BlockPage & 0x00);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>8) & 0x00);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>12) & 0xFF);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>20) & 0xff);
FLASH_WE=1;
FLASH_WE=0;
FLASH_ALE=0;
for(i=0;i<FLASH_BYTE;i++)
{
FLASH_WE=0;
FLASH_DATA=str[i];
FLASH_WE=1;
}
FLASH_CLE=1;
FLASH_WE=0;
FLASH_DATA=0x10;
FLASH_WE=1;
while(FLASH_RB==0);
FLASH_WE=0;
FLASH_DATA=0x70;
FLASH_WE=1;
FLASH_CLE=0;
Delay(7); //twhr>60ns
FLASH_RE=0;
FLASH_DATA=0xff;
state=(bit)(FLASH_DATA&0x01);
FLASH_RE=1;
return(state);
}
/*
==================================================================================================================
=== 功能描述:对FLASH指定的块(FLASH_BLOCK)+页(FLASH_PAGE)读出数据
=== 入口参数:Block块号(FLASH_BLOCK);Page页号(FLASH_PAGE);
=== 出口参数:无
==================================================================================================================
*/
void Flash_read(unsigned long Block,uchar Page,uchar *pReadTo)
{
ushort i;
unsigned long BlockPage;
Page &= 0x1F;
BlockPage = (Block<<18) + (Page<<12);
FLASH_CE=0;
FLASH_RE=1;
FLASH_CLE=1;
FLASH_ALE=0;
FLASH_WE=0;
FLASH_DATA=0x00;
FLASH_WE=1;
FLASH_CLE=0;
FLASH_ALE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)(BlockPage & 0x00);//(FF-->00)
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>8) & 0x00);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>12) & 0xFF);
FLASH_WE=1;
FLASH_WE=0;
FLASH_DATA = (uchar)((BlockPage>>20) & 0xFF);
FLASH_WE=1;
Delay(10);
FLASH_ALE=0;
FLASH_CLE=1;
FLASH_WE=0;
FLASH_DATA=0x30;
FLASH_WE=1;
FLASH_CLE=0;
while(FLASH_RB==0);
for(i=0;i<FLASH_BYTE;i++)
{FLASH_RE=0;
FLASH_DATA =0xff;
pReadTo[i]=FLASH_DATA;
FLASH_RE=1;
}
FLASH_RE=1;
FLASH_CE=1;
}
/*
*******************************************************************************************************************
*** 工程名称: QDT64481BC终端主板控制器程序
*** 文件名称: K9F1G08_Driver.H
*** 文件用途:NAND FLASH驱动头文件
*** 其他说明:NAND FLASH函数驱动接口定义
*** 最后修改:
*******************************************************************************************************************
*/
#ifndef K9F1G08_Driver_H_
#define K9F1G08_Driver_H_
//flash类型定义
#define TYPE_K9F120864
#define TYPE_K9F1G08256
#define FLASH_TYPE TYPE_K9F1G08
//FLASH控制接口位定义
#define FLASH_DATA P4
sbitFLASH_CE = P5^5; //芯片选择控制
sbitFLASH_RE = P5^6; //数据输出控制,有效时数据送到I/O总线上
sbit FLASH_WP = P3^3; //写保护
sbitFLASH_RB = P5^7; //器件状态:0为忙,1为闲
sbitFLASH_WE = P5^2; //写I/O口控制,命令地址数据在上升沿锁存
sbitFLASH_CLE= P5^4; //命令锁存使能
sbitFLASH_ALE = P5^3; //地址锁存
//FLASH参数标记
#ifFLASH_TYPE==TYPE_K9F1G08
#define FLASH_BLOCK 1024
#define FLASH_PAGE64
#define FLASH_BYTE(2112)
#define FLASH_ID0xECF1
#elifFLASH_TYPE==TYPE_K9F1208
#define FLASH_BLOCK 4096
#define FLASH_PAGE32
#define FLASH_BYTE(528)
#define FLASH_ID0xEC76
#endif
//函数宏定义
void Flash_Reset(void);
uint Flash_ChekeId(void);
uchar Flash_EraseBlock(unsigned long Block);
uchar Flash_write(uchar *str,unsigned long Block,uchar Page);
void Flash_read(unsigned long Block,uchar Page,uchar *pReadTo);
#endif