给的原理图还是有点瞎的:
U16 read_en29lv160ab(U32 addr)
{
return *((volatile U16 *)(addr));
}
norflash不仅能硬件复位,也能软件复位,思路是向任一地址写入复位命令 0xf0:
void reset_en29lv160ab(void)
{
*((volatile U16 *)0x0) = 0xf0;
}
#define flash_base 0x00000000
#define CMD_ADDR0 *((volatile U16 *)(0x555<<1+flash_base))
#define CMD_ADDR1 *((volatile U16 *)(0x2aa<<1+flash_base))
U8 en29lv160ab_program(U32 addr, U16 dat)
{
CMD_ADDR0 = 0xaa;
CMD_ADDR1 = 0x55;
CMD_ADDR0 = 0xa0;
*((volatile U16 *)(addr)) = dat;
return check_toggle();
}
U8 check_toggle()
{
volatile U16 newtoggle,oldtoggle;
oldtoggle = *((volatile U16 *)0x0);
while(1)
{
newtoggle = *((volatile U16 *)0x0);
if((oldtoggle & 0x40)==(newtoggle & 0x40))
break;
if(newtoggle & 0x20) //DQ5
{
oldtoggle = *((volatile U16 *)0x0);
newtoggle = *((volatile U16 *)0x0);
if((oldtoggle & 0x40)==(newtoggle & 0x40))
break;
else
return 0; //错误
}
oldtoggle = newtoggle;
}
return 1; //正确
}
// 输入参数为擦除块的首地址
U8 en29lv160ab_sector_erase(U32 section_addr)
{
CMD_ADDR0 = 0xaa;
CMD_ADDR1 = 0x55;
CMD_ADDR0 = 0x80;
CMD_ADDR0 = 0xaa;
CMD_ADDR1 = 0x55;
*((volatile U16 *)(section_addr)) = 0x30;
return check_toggle();
}
//读厂商 ID
U32 get_en29lv160ab_id(void)
{
U32 temp=0;
CMD_ADDR0 = 0xaa;
CMD_ADDR1 = 0x55;
CMD_ADDR0 = 0x90;
temp = (*(volatile unsigned short *)(flash_base+ (0x100<<1)))<<16;
temp |= *(volatile unsigned short *)(flash_base + (1<<1));
return temp;
}
…… ……
U16 buffer[1024];
char cmd;
…… ……
void test_en29lv160ab(void)
{
U32 temp;
U8 sta;
int i;
for(i=0;i<1024;i++)
buffer[i]=2*i+1;
//读ID
temp = get_en29lv160ab_id();
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0xff000000)>>24);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x00ff0000)>>16);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x0000ff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x000000ff));
reset_en29lv160ab(); //这里一定要复位
delay(100);
//擦除块33
sta=en29lv160ab_sector_erase(0xf0000);
if(sta == 0)
{
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=0xaf; //擦除出错
}
else
{
for(i=0;i<1024;i++)
{
sta = en29lv160ab_program(0xf0000+(i<<1),buffer[i]); //写
if(sta == 0) //写出错
{
while(!(rUTRSTAT0 & 0x2));
rUTXH0=0xbf;
break;
}
delay(200);
}
if(sta == 1)
{
for(i=0;i<1024;i++)
{
if(read_en29lv160ab(0xf0000+(i<<1))!=buffer[i]) //读出错
{
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=0xcf;
sta = 3;
break;
}
}
if(sta !=3) //全部操作都正确
{
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=0x66;
}
}
}
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=0x88; //结束
}
//简单测试CFI
void test_en29lv160ab_CFI(void)
{
U16 temp;
*((volatile U16 *)(0x55<<1+flash_base))=0x98; //CFI命令
temp = (*(volatile unsigned short *)(flash_base+ (0x10<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x11<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x12<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x13<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x14<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x15<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x16<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x17<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x18<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x19<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
temp = (*(volatile unsigned short *)(flash_base+ (0x1a<<1)));
//while(!(rUTRSTAT0 & 0x2)) ;
//rUTXH0=(U8)((temp&0xff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)(temp&0x00ff);
}
void __irq uartISR(void)
{
char ch;
rSUBSRCPND |= 0x1;
rSRCPND |= 0x1<<28;
rINTPND |= 0x1<<28;
ch=rURXH0;
switch(ch)
{
case 0x11: //get ID
cmd = 1;
break;
case 0x66: //test CFI
cmd = 6;
break;
case 0x77: //test norflash
cmd = 7;
break;
}
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=ch;
}
void Main(void)
{
U32 temp;
int i;
//uart0 port
rGPHCON = 0x00faaa;
rGPHUP = 0x7ff;
//init uart0
rULCON0 = 0x3;
rUCON0 = 0x5;
rUFCON0 = 0;
rUMCON0 = 0;
rUBRDIV0 = 26;
rSRCPND = (0x1<<19)|(0x1<<28);
rSUBSRCPND = 0x1;
rINTPND = (0x1<<19)|(0x1<<28);
rINTSUBMSK = ~(0x1);
rINTMSK = ~((0x1<<19)|(0x1<<28));
pISR_UART0 = (U32)uartISR;
cmd = 0;
while(1)
{
switch(cmd)
{
case 1: //读ID
cmd = 0;
temp = get_en29lv160ab_id();
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0xff000000)>>24);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x00ff0000)>>16);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x0000ff00)>>8);
while(!(rUTRSTAT0 & 0x2)) ;
rUTXH0=(U8)((temp&0x000000ff));
reset_en29lv160ab();
break;
case 0x7:
cmd = 0;
test_en29lv160ab();
break;
case 0x6:
cmd = 0;
test_en29lv160ab_CFI();
reset_en29lv160ab();
break;
}
}
}