|
各位好,请教一下啊,我想直接用数组写flash做bootloader, 为什么数组写进去后位置在边上呢?
写地址的代码是参照马潮老师的,只是想将程序编译后的二进制文件作为一个数组在程序中直接写入,不用串口了
用Xmodem协议发送的也是一样的二进制文件呀,用串口看每个写入的元素也一样的
是不是程序运行的时候,flash被锁死保护了?
主要代码如下:
//data1有256个元素,正好是一页,只是二进制文件的第一个数组
char data1[]={"0C9446000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D000C945D00"};
long address = 0;
void boot_page_ew(long p_address,char code)
{
/*
SPMCSR:
Bit 7 ¨C SPMIE: SPM Interrupt Enable
Bit 6 ¨C RWWSB: Read-While-Write Section Busy
Bit 5 ¨C Res: Reserved Bit
Bit 4 ¨C RWWSRE: Read-While-Write Section Read Enable
Bit 3 ¨C BLBSET: Boot Lock Bit Set
Bit 2 ¨C PGWRT: Page Write
Bit 1 ¨C PGERS: Page Erase
Bit 0 ¨C SPMEN: Store Program Memory Enable
RAMPZ:
Bits 7..1 ¨C Res: Reserved Bits
Bit 0 ¨C RAMPZ0: Extended RAM Page Z-pointer
RAMPZ0 = 0: Program memory address $0000 - $7FFF (lower 64K bytes) is
accessed by ELPM/SPM
RAMPZ0 = 1: Program memory address $8000 - $FFFF (higher 64K bytes) is
accessed by ELPM/SPM
*/
asm("mov r30,r16\n"
"mov r31,r17\n"
"out 0x3b,r18\n");
SPMCSR = code;
asm("spm\n");
}
void boot_page_fill(unsigned int address,int data)
{
asm("mov r30,r16\n"
"mov r31,r17\n"
"mov r0,r18\n"
"mov r1,r19\n");
SPMCSR = 0x01;
asm("spm\n");
}
void wait_page_rw_ok(void)
{
while(SPMCSR & 0x40)
{
while(SPMCSR & 0x01);
SPMCSR = 0x11;
asm("spm\n");
}
}
void write_one_page(void)
{
int i;
boot_page_ew(address,0x03);
wait_page_rw_ok();
for(i=0;i<SPM_PAGESIZE;i+=2)
{
boot_page_fill(i, data+(data[i+1]<<8));
}
boot_page_ew(address,0x05);
wait_page_rw_ok();
}
main
{
int i = 0;
unsigned char timercount = 0;
unsigned char packNO = 1;
int bufferPoint = 0;
for(i=0;i<256;i++)
{
data[bufferPoint]=data1;
bufferPoint++;
}
while(bufferPoint >= SPM_PAGESIZE)
{
write_one_page();
address += SPM_PAGESIZE;
bufferPoint = 0;
}
}
写入的效果如下:
00000000:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000001:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000002:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000003:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000004:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000005:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000006:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
00000007:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 30 0C9446000C945D00
……
我看马超老师的程序,二进制文件写入之后都是在中间区域的,我这怎么到边上去了?而且中间显示的是对应的ASCII码~~~请大家不吝指教 |
阿莫论坛20周年了!感谢大家的支持与爱护!!
该献的血还是要献的。你不献他不献。难道让我去献? --- 出自坛友:lovejp1981
|