|
![](static/image/common/ico_lz.png)
楼主 |
发表于 2011-11-3 16:45:39
|
显示全部楼层
回复【9楼】qwerttt
-----------------------------------------------------------------------
我暑假搞过6455上的bootloader in Flash,和你说的情况类似。
工程其实很简单,只贴主程序,涉及Flash烧写的程序可以参考HX提供的例程
//Flash烧写的工程main 函数
void main(void)
{
unsigned char *pflash=(unsigned char *)0xb0000000;//指向Flash起始地址
unsigned int i,n=0;
unsigned int p;
FILE *fp;//文件句柄,用于从PC磁盘读.bin文件
fp=fopen("usercode.bin","rb");//打开文件,只读方式
n=fread( temp_array,1,MAX_SIZE_OF_FILE,fp);//读取文件内容到数组temp_array,返回值n代表实际读取字节数
if (n==0)
{
printf("error\n");
while(1);
}
EraseFlash();//擦除整片Flash
WriteData(0,n,temp_array);//将usercode.bin写入Flash
printf("Burn OK\n");
while(1);
}
//Bootloader 主函数
void (*pfun)(void);
void copy_flash_ram(void)
{
unsigned int i,j,byte_count,ram_start_addr,rom_section_start_addr;
pfun=(void (*)(void))(*((unsigned int *)PROG_IN_FLASH_ADDR));
byte_count=(*((unsigned int *)PROG_IN_FLASH_ADDR+1));
ram_start_addr=(*((unsigned int *)PROG_IN_FLASH_ADDR+2));
rom_section_start_addr=PROG_IN_FLASH_ADDR+12;
if(byte_count==0)
{
byte_count=0;
return;
}
while(1)
{
unsigned int temp=byte_count&0x03;
for(i=0;i<byte_count;i++)
{
for(j=0;j<3*1024;j++);
*((unsigned char *)ram_start_addr+i)=
*((unsigned char *)rom_section_start_addr+i);
}
if(temp)
{
temp=4-temp;
}
rom_section_start_addr+=byte_count+temp;//必须对其到4的整数倍
byte_count=(*((unsigned int *)rom_section_start_addr));
rom_section_start_addr+=4;
ram_start_addr=(*((unsigned int *)rom_section_start_addr));
rom_section_start_addr+=4;
if(byte_count==0)
{
pfun();
}
}
} |
|