📄 flash.c.bak
字号:
#define FLASH_BASE 0x00000000
#define BOOTLEN 0x8000
#define FLASH_555 (*(volatile unsigned long *)(0x1554 + FLASH_BASE))
#define FLASH_2AA (*(volatile unsigned long *)(0x0AA8 + FLASH_BASE))
#define FLASH_MANU (*(volatile unsigned long *)(0x0000 + FLASH_BASE))
#define FLASH_CODE (*(volatile unsigned long *)(0x0004 + FLASH_BASE))
void erase_sector(unsigned long addr);
void program_sector(unsigned long *dst, unsigned long *src, unsigned long len);
void InitFlash();
int Data_polling(unsigned long addr, unsigned long data);
extern unsigned long code[];
const unsigned long topSize[] = {
0x0,
0x10000,
0x20000
};
const unsigned long bottomSize[] = {
0x0,
0x4000,
0x6000,
0x8000,
0x10000,
0x20000
};
int c_entry()
{
int i = 0;
int t = 0;
init_serial();
InitFlash();
program_sector(0, code, BOOTLEN);
while(1)
{
;
}
}
int wait_flash_erase(unsigned long addr, unsigned long value)
{
int i;
for (i = 0; i < 100000; i++);
for (i = 0; i < 1000; i++){
if (*(volatile unsigned long *)addr == value){
return 0;
}
}
return -1;
}
void erase_sector(unsigned long addr)
{
int ret;
addr += FLASH_BASE;
FLASH_555 = 0x00aa00aa;
FLASH_2AA = 0x00550055;
FLASH_555 = 0x00800080;
FLASH_555 = 0x00aa00aa;
FLASH_2AA = 0x00550055;
*(unsigned long *)addr = 0x00300030;
wait_flash_erase(addr, 0xffffffff);
}
void program_sector(unsigned long *dst, unsigned long *src, unsigned long len)
{
int i;
int ret;
dst = (unsigned long *)(FLASH_BASE + (unsigned long)dst);
for (i = 0; i < len; i++){
FLASH_555 = 0x00aa00aa;
FLASH_2AA = 0x00550055;
FLASH_555 = 0x00a000a0;
*dst = *src;
ret = Data_polling(dst, *src);
if (ret != 1)
{
ret = Data_polling(dst, *src);
}
dst ++;
src ++;
if ((i & 0xFF) == 0){
uart_putc('.');
}
}
uart_putc('#');
}
void InitFlash()
{
unsigned short manu ;
unsigned short deviceid;
FLASH_555 = 0x00aa00aa;
FLASH_2AA = 0x00550055;
FLASH_555 = 0x00900090;
manu = FLASH_MANU;
FLASH_555 = 0x00aa00aa;
FLASH_2AA = 0x00550055;
FLASH_555 = 0x00900090;
deviceid = FLASH_CODE;
if (((manu&0x00ff) == 0x04) || ((manu&0x00ff) == 0x01))
{
if ((deviceid&0xffff) == 0x22c4)
{
erase_sector(0);
erase_sector(0x8000<<2);
erase_sector(0x10000<<2);
erase_sector(0x18000<<2);
erase_sector(0x20000<<2);
erase_sector(0x28000<<2);
erase_sector(0x30000<<2);
erase_sector(0x38000<<2);
}
else if ((deviceid&0xffff) == 0x2249)
{
erase_sector(0);
erase_sector(0x2000<<2);
erase_sector(0x3000<<2);
erase_sector(0x4000<<2);
erase_sector(0x8000<<2);
erase_sector(0x10000<<2);
erase_sector(0x18000<<2);
erase_sector(0x20000<<2);
erase_sector(0x28000<<2);
erase_sector(0x30000<<2);
erase_sector(0x38000<<2);
}
}
}
int Data_polling(unsigned long addr, unsigned long data)
{
volatile unsigned long __temp;
volatile unsigned long *__p = (unsigned long*)addr;
while(1)
{
__temp = __p;
if ((__temp & 0x00800080) == (data & 0x00800080))
{
return 1; // Finish writing
}
else
{
if ((__temp & 0x00080008) != 0) //Bit 3 is not zero, Vpp is not enough high.
{
return 2;
}
if ((__temp & 0x00200020) != 0) //Bit 5 is not zero, exceed time limit or sector protected.
{
return 2;
}
return 0; //Write not over. Keep polling
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -