📄 flash.c
字号:
#include "common.h"
/****************************************************************************/
//
// FLASH burnning functions used in burn.c
// Created by Strong Embeded Studio
// embed.8800.org
// If there are any concerns on the code
// go to embed.8800.org for discussion
//
/****************************************************************************/
//
// Change the type here
//
#define UNKNOWN 0x1000
#define F39SST160 0x2782
#define F29LV160T 0x2249
//
// Define the FLASH sector structure
//
static uint SecData[] = {
//
// 29LV160T
//
F29LV160T, 0x000,
0x004000, 0x001,
0x002000, 0x002,
0x008000, 0x001,
0x010000, 0x01F,
//
// 39SST160
//
F39SST160, 0x000,
0x001000, 0x200,
//
// End
//
0x000000, 0x000
};
#define FLASHBASEADDR 0
uint flashbase;
uint16 flashtype;
uint16 GetFlashType(uint base)
{
uint16 device;
*(volatile unsigned short *)(base + 0x0000AAAA)=0xAAAA;
*(volatile unsigned short *)(base + 0x00005554)=0x5555;
*(volatile unsigned short *)(base + 0x0000AAAA)=0x9090;
device = *(volatile unsigned short *)(base+2);
*(volatile unsigned short *)(base)=0xF0F0;
if (F39SST160 == device) return device;
if (F29LV160T == device) return device;
return UNKNOWN;
}
char *fdata[] = {"39SST160","29LV160T","UNKNOWN"};
char *SetFlashBase(uint base)
{
flashbase = base;
flashtype = GetFlashType(base);
if (flashtype == F39SST160) return fdata[0];
if (flashtype == F29LV160T) return fdata[1];
flashtype = F39SST160;
return fdata[2];
}
//
// Get the sector index from the offset
//
uint GetIndex(uint offset)
{
uint i;
uint sec;
uint addr;
addr = 0;
i = 0;
sec = 0;
while (SecData[i]) {
if (SecData[i]==flashtype && SecData[i+1]==0) {
i += 2;
while (SecData[i+1]) {
sec += SecData[i+1];
addr+= (SecData[i] * SecData[i+1]);
if (addr >= offset) {
addr -= (SecData[i] * SecData[i+1]);
sec -= SecData[i+1];
return sec+ (offset-addr)/(SecData[i]);
}
i += 2;
}
return -1;
} else {
i += 2;
continue;
}
}
return -1;
}
//
// Get the address according to the index
//
uint GetSecAddr(uint index)
{
uint i;
uint sec;
uint addr;
addr = 0;
i = 0;
sec = 0;
while (SecData[i]) {
if (SecData[i] == flashtype && SecData[i+1]==0) {
i += 2;
while (SecData[i+1]) {
sec += SecData[i+1];
addr+= (SecData[i] * SecData[i+1]);
if (sec >= index) {
if (sec == index) return addr;
else return (addr - (SecData[i] * (sec - index)));
}
i += 2;
}
return -1;
} else {
i += 2;
continue;
}
}
return -1;
}
uint WriteSector(int index,char *data_buffer)
{
volatile unsigned short *Dest;
volatile unsigned short *Org;
uint len=0,i;
unsigned short temp;
len = GetSecAddr(index+1)-GetSecAddr(index);
Org = (volatile unsigned short *)data_buffer;
Dest = (volatile unsigned short *)(flashbase + GetSecAddr(index));
//OutPut(0, "src=%08x dst=%08x l=%08x\n", Org, Dest, len);
*(volatile unsigned short *)(0x0000AAAA)=0xAAAA;
*(volatile unsigned short *)(0x00005554)=0x5555;
*(volatile unsigned short *)(0x0000AAAA)=0x8080;
*(volatile unsigned short *)(0x0000AAAA)=0xAAAA;
*(volatile unsigned short *)(0x00005554)=0x5555;
*Dest = 0x3030;
while(1)
{
if (*Dest == 0xFFFF) break;
temp = *((volatile unsigned short* )Dest)&0x40;
if(temp!=*((volatile unsigned short*)Dest)&0x40) //D6 == D6
continue;
if(*((volatile unsigned short *)Dest)&0x80)
break; //D7 == 1
}
for (i=0;i<len/2;i++)
{
*(volatile unsigned short *)(0x0000AAAA)=0xAAAA;
*(volatile unsigned short *)(0x00005554)=0x5555;
*(volatile unsigned short *)(0x0000AAAA)=0xA0A0;
*(Dest+i)=*(Org+i);
while(1)
{
temp = *(volatile unsigned short *)(Dest+i)&0x40;
if(temp!= *(volatile unsigned short *)(Dest+i)&0x40) //D6 == D6
continue;
if((*(volatile unsigned short *)(Dest+i)&0x80)==(*(Org+i)&0x80))
break; //D7 == D7
}
}
for (i=0;i<len/2;i++)
if ((*(Dest+i))!=(*(Org+i))) return 0;
return len;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -