📄 nflash_1.c
字号:
switch(DEV[dev].K9fInfo.BlockAmount)
{
case 4096 :
PutStr("K9F1208,64Mbyte,");
break;
case 2048 :
PutStr("K9F5608,32Mbyte.");
break;
default:
PutStr("not support");
break;
}
PutNextLine();
#endif
#ifdef _DEBUG_
PutStr(" Erase and Write:ADDR start at 0x01 with 512 bytes ::");
#endif
for(i=0;i<512;i++)
WriterBuf[i]=i;
K9F_Erase_Block(dev,1);
K9F_ReadPage(dev,1,0,ReadBuf);
//测试ERASE是否正确
for(i=0;i<512;i++)
{
if(ReadBuf[i]==0x0ff)
continue;
else
{
#ifdef _DEBUG_
PutStr("ERASE ERROR!");
#endif //读取有错误.
return;
}
}
#ifdef _DEBUG_
PutStr("ERASE OK! ");
#endif
//测试WRITE是否正确
K9F_WritePage(dev,1,0,WriterBuf);
K9F_ReadPage(dev,1,0,ReadBuf);
for(i=0;i<100;i++)
{
if(ReadBuf[i]==WriterBuf[i])
continue;
else
{
#ifdef _DEBUG_
PutStr("::WRITE ERROR!");
#endif //读取有错误.
return;
}
}
#ifdef _DEBUG_
PutStr("::WRITE OK!");
#endif
}
//-----------------------------------------------------
//---071025
/****************************************************************/
/* 主函数 */
/* */
/* 作者 时间 版本 备注 */
/* */
/* 修改:sea_start 2003.3.11 Ver1.00 创建 */
/* */
/* */
/* */
/* E-Mail: */
/* */
/* 版权所有 侵权必究 (2003 - 2010) */
/****************************************************************/
#include "global.h"
#include "w90221.h"
#include "nflash.h"
#include "fat.h"
unNflashRw _Cmd_Nrw;
stNandFlash _Nand_Flash;
////////////////////////////////////////////////////////////////////////////////
//
// 函数名: AllMem ()
//
// 功能:
// - 选择所有内存
//
// 返回:
// - 无
//
//
////////////////////////////////////////////////////////////////////////////////
void AllMem ()
{
_Cmd_Nrw.bit.se = 0;
outportb(CS373, _Cmd_Nrw.byte);
}
////////////////////////////////////////////////////////////////////////////////
//
// 函数名: SkipMem ()
//
// 功能:
// - 选择主内存
//
// 返回:
// - 无
//
//
////////////////////////////////////////////////////////////////////////////////
void SkipMem ()
{
_Cmd_Nrw.bit.se = 1;
outportb(CS373, _Cmd_Nrw.byte);
}
void CmdLatchEnable ()
{
_Cmd_Nrw.bit.cle = 1;
outportb(CS373, _Cmd_Nrw.byte);
}
void CmdLatchDisable ()
{
_Cmd_Nrw.bit.cle = 0;
outportb(CS373, _Cmd_Nrw.byte);
}
void AddLatchEnable ()
{
_Cmd_Nrw.bit.ale = 1;
outportb(CS373, _Cmd_Nrw.byte);
}
void AddLatchDisable ()
{
_Cmd_Nrw.bit.ale = 0;
outportb(CS373, _Cmd_Nrw.byte);
}
void EnableChip ()
{
_Cmd_Nrw.bit.cs = 0;
outportb(CS373, _Cmd_Nrw.byte);
}
void DisableChip ()
{
_Cmd_Nrw.bit.cs = 1;
outportb(CS373, _Cmd_Nrw.byte);
}
void EnableWriteChip ()
{
_Cmd_Nrw.bit.wp = 0;
outportb(CS373, _Cmd_Nrw.byte);
}
void DisableWriteChip ()
{
_Cmd_Nrw.bit.wp = 1;
outportb(CS373, _Cmd_Nrw.byte);
}
void InitNflash ()
{
K9F_Init(0);//板上
}
void ResetChip () //ResetChip Flash
{
uint16 i;
CmdLatchEnable ();
outportb(DATA, RESET_CMD); //ResetChip command (ffh)
CmdLatchDisable ();
for(i = 0; i < 30000; i ++); // Waiting for tRST
}
uint16 ReadID () // Read ID
{
uint16 i, dent_bit;
uint8 m_code, d_code;
InitNflash ();
ResetChip ();
EnableChip ();
CmdLatchEnable ();
outportb(DATA, READID_CMD); //Read ID command
CmdLatchDisable ();
AddLatchEnable ();
outportb(DATA, 0x00); //1st address cycle
AddLatchDisable ();
m_code = inportb(DATA); //Reading 2 byte data
d_code = inportb(DATA);
DisableChip ();
if (d_code == 0x73 || d_code == 0xf2) dent_bit = 128;
else if (d_code == 0xf4 || d_code == 0x75) dent_bit = 256;
else if (d_code == 0xf7 || d_code == 0x76) dent_bit = 512;
else if (d_code == 0xf8 || d_code == 0x79) dent_bit = 1024;
else dent_bit = 0xff;
/*
if (d_code == 0xce || d_code == 0x4f) dent_bit = 128;
else if (d_code == 0x2f || d_code == 0xae) dent_bit = 256;
else if (d_code == 0xef || d_code == 0x6e) dent_bit = 512;
else if (d_code == 0x1f || d_code == 0x9e) dent_bit = 1024;
else dent_bit = 0xff;
*/
_Nand_Flash.device_maker = (uint16)m_code; // 0xec; samsung, 0x98: toshiba
switch (dent_bit)
{
case 128:
_Nand_Flash.device_type = 16; // device_type 1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
_Nand_Flash.blocks_disk = 1024; // blocks_disk value: blocks in a disk
_Nand_Flash.pages_block = 32; // pages_block value: pages in a block
_Nand_Flash.page_size = 528; // page_size value: page_size in bytes
_Nand_Flash.data_size = 512; // data_size value: data_size in bytes
_Nand_Flash.spare_size = 16; // spare_size value: spare_size in bytes
break;
case 256:
_Nand_Flash.device_type = 32; // device_type 1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
_Nand_Flash.blocks_disk = 2048; // blocks_disk value: blocks in a disk
_Nand_Flash.pages_block = 32; // pages_block value: pages in a block
_Nand_Flash.page_size = 528; // page_size value: page_size in bytes
_Nand_Flash.data_size = 512; // data_size value: data_size in bytes
_Nand_Flash.spare_size = 16; // spare_size value: spare_size in bytes
break;
case 512:
_Nand_Flash.device_type = 64; // device_type 1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
_Nand_Flash.blocks_disk = 4096; // blocks_disk value: blocks in a disk
_Nand_Flash.pages_block = 32; // pages_block value: pages in a block
_Nand_Flash.page_size = 528; // page_size value: page_size in bytes
_Nand_Flash.data_size = 512; // data_size value: data_size in bytes
_Nand_Flash.spare_size = 16; // spare_size value: spare_size in bytes
break;
case 1024:
_Nand_Flash.device_type = 128; // device_type 1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
_Nand_Flash.blocks_disk = 8192; // blocks_disk value: blocks in a disk
_Nand_Flash.pages_block = 32; // pages_block value: pages in a block
_Nand_Flash.page_size = 528; // page_size value: page_size in bytes
_Nand_Flash.data_size = 512; // data_size value: data_size in bytes
_Nand_Flash.spare_size = 16; // spare_size value: spare_size in bytes
break;
default:
_Nand_Flash.device_type = 0; // device_type 1:1M, 2:2M, 4:4M, 8:8M, 16:16M, 32:32M ...bytes
_Nand_Flash.blocks_disk = 0; // blocks_disk value: blocks in a disk
_Nand_Flash.pages_block = 0; // pages_block value: pages in a block
_Nand_Flash.page_size = 0; // page_size value: page_size in bytes
_Nand_Flash.data_size = 0; // data_size value: data_size in bytes
_Nand_Flash.spare_size = 0; // spare_size value: spare_size in bytes
dent_bit = 0xff;
break;
}
return (dent_bit); // Returning to device density
}
uint8 StatusCheck () //status check
{
int32 i;
uint8 status;
EnableChip ();
// DisableWriteChip ();
for (i = 0; i < 400000; i ++)
{
CmdLatchEnable ();
outportb(DATA, READ_STATUS_CMD);
CmdLatchDisable ();
status = inportb(DATA);
if (status == 0xc0)
// if (status == 0x03)
break;
}
EnableWriteChip ();
DisableChip ();
if (status == 0xc0)
// if (status == 0x03)
return Successful;
else
return Fail;
}
uint8 BlockErase (uint32 in_page_add) // Erasing single block
{
uint8 add2, add3, add4;
add4 = (uint32)((in_page_add << 8) >> 24);
add3 = (uint32)((in_page_add << 16) >> 24);
add2 = (uint32)((in_page_add << 24) >> 24);
EnableChip ();
ResetChip ();
DisableWriteChip ();
CmdLatchEnable ();
outportb(DATA, BLOCK_ERASE_CMD); // Block Erase command (60h)
CmdLatchDisable ();
AddLatchEnable ();
outportb(DATA, add2);
outportb(DATA, add3);
if (_Nand_Flash.device_type >= 64)
outportb(DATA, add4);
AddLatchDisable ();
CmdLatchEnable ();
outportb(DATA, BLOCK_ERASE_CONFIRM_CMD); // Block Erase Confirm command (D0h)
CmdLatchDisable ();
// EnableWriteChip ();
DisableChip ();
if (StatusCheck () == Successful)
{
return Successful;
printf("erase ok\n");
}
else
{
printf("erase error\n");
return Fail;
}
}
uint8 BlockRead (uint8 *in_page_data, uint32 in_page_add)
{
uint16 i;
int16 len;
uint8 add2, add3, add4, j;
add4 = (uint32)(((in_page_add << 5) << 8) >> 24);
add3 = (uint32)(((in_page_add << 5) << 16) >> 24);
add2 = (uint32)(((in_page_add << 5) << 24) >> 24);
// add4 = Sawpbyte (add4);
// add3 = Sawpbyte (add3);
// add2 = Sawpbyte (add2);
len = 0;
EnableChip ();
ResetChip ();
for (j = 0; j < _Nand_Flash.pages_block; j ++)
{
CmdLatchEnable ();
outportb(DATA, READ1_CMD); //Main area data read command(00h)
CmdLatchDisable ();
AddLatchEnable ();
outportb(DATA, 0x00);
outportb(DATA, add2 + j);
outportb(DATA, add3);
if(_Nand_Flash.device_type >= 64)
outportb(DATA, add4);
AddLatchDisable ();
for (i = 0; i < 500; i++); //Wait for tREAD
{
for (i = 0; i < _Nand_Flash.data_size; i ++)
{
in_page_data[i + len] = inportb(DATA); //Page data Read
}
}
len += _Nand_Flash.data_size;
}
DisableChip ();
return Successful;
}
uint8 PageRead (uint8 *in_page_data, uint32 in_page_add)
{
uint16 i;
uint8 add2, add3, add4;
add4 = (uint32)((in_page_add << 8) >> 24);
add3 = (uint32)((in_page_add << 16) >> 24);
add2 = (uint32)((in_page_add << 24) >> 24);
EnableChip ();
ResetChip ();
CmdLatchEnable ();
outportb(DATA, READ1_CMD); //Main area data read command(00h)
CmdLatchDisable ();
AddLatchEnable ();
outportb(DATA, 0x00);
outportb(DATA, add2);
outportb(DATA, add3);
if(_Nand_Flash.device_type >= 64)
outportb(DATA, add4);
AddLatchDisable ();
for (i = 0; i < 500; i++); //Wait for tREAD
for (i = 0; i < _Nand_Flash.data_size; i ++)
in_page_data[i] = inportb(DATA); //Page data Read
DisableChip ();
return Successful;
}
uint8 PageOneWrite (uint8 *in_write_data, uint32 in_page_add)
{
uint16 i;
uint8 page_data[528];
uint8 err = Successful;
uint8 add2, add3, add4;
add4 = (uint32)((in_page_add << 8) >> 24);
add3 = (uint32)((in_page_add << 16) >> 24);
add2 = (uint32)((in_page_add << 24) >> 24);
EnableChip ();
ResetChip ();
DisableWriteChip ();
CmdLatchEnable ();
outportb(DATA, SEQUENTIAL_DATA_INPUT_CMD); //Sequential Data Input command (80h)
CmdLatchDisable ();
AddLatchEnable ();
outportb(DATA, 0x00);
outportb(DATA, add2);
outportb(DATA, add3);
if (_Nand_Flash.device_type >= 64)
outportb(DATA, add4);
AddLatchDisable ();
for (i = 0; i < _Nand_Flash.data_size; i ++)
{
outportb(DATA, in_write_data[i]); //Page data write
}
CmdLatchEnable ();
outportb(DATA, PAGE_PROGRAM_CMD); //Page Program command (10h)
CmdLatchDisable ();
DisableChip ();
if (StatusCheck () != Successful)
{
printf("Error in StatusCheck()!!!\n");
loop: printf("Invalid Blocks are detected while Real Time Mapping(ADD= %x %x %x)\n", add4, add3, add2);
add2 &= 0xe0; //In case of 16MB, 32MB,64MB,128MB
in_page_add = (uint32)(add4 << 16 + add3 << 8 + add2);
// RealTimeBadMark (in_page_add);
return Fail;
}
PageRead (page_data, in_page_add);
for (i = 0; i < _Nand_Flash.data_size; i ++)
{
if (*(in_write_data + i) != *(page_data + i))
{
printf("\n%d = %x %x\n", i, *(in_write_data + i), *(page_data + i));
err = Fail;
break;
}
}
if (err == Fail)
{
printf("Error in Compdata!!!\n");
goto loop;
}
return Successful;
}
uint8 PageWrite (uint8 *in_write_data, uint32 in_page_add)
{
uint8 i;
uint8 read_buff[16896];
uint32 add;
// memcpy(page_buff, in_write_data, strlen(in_write_data));
for (i = 0; i < _Nand_Flash.pages_block; i ++)
{
PageRead ((read_buff + i * _Nand_Flash.data_size), (in_page_add & 0xffffffe0) + i);
}
BlockErase (in_page_add & 0xffffffe0); // Erasing single block
for (i = 0, add = in_page_add; i < _Nand_Flash.pages_block; i ++)
{
if (add == ((in_page_add & 0xffffffe0) + i))
{
PageOneWrite (in_write_data, (in_page_add & 0xffffffe0) + i);
//printf("%d %x %x\n", i, add, (in_page_add & 0xffffffe0) + i);
}
else
PageOneWrite ((read_buff + i * _Nand_Flash.data_size), (in_page_add & 0xffffffe0) + i);
}
}
uint8 BlockWrite (uint8 *in_write_data, uint32 in_page_add)
{
uint8 i;
BlockErase (in_page_add << 5); // Erasing single block
for (i = 0; i < _Nand_Flash.pages_block; i ++)
PageOneWrite (in_write_data + i * _Nand_Flash.data_size, (in_page_add << 5) + i);
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -