poipoi09876 发表于 2011-11-23 01:32:02

各位好,请教一下啊,我想直接用数组写或者不用Xmodem协议直接用串口flash做bootloader,

各位好,请教一下啊,我想直接用数组写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<<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=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 300C9446000C945D00
00000001:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000002:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000003:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000004:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000005:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000006:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
00000007:30 43 39 34 34 36 30 30-30 43 39 34 35 44 30 300C9446000C945D00
……

我看马超老师的程序,二进制文件写入之后都是在中间区域的,我这怎么到边上去了?而且中间显示的是对应的ASCII码~~~请大家不吝指教

poipoi09876 发表于 2011-11-23 01:34:40

回复【楼主位】poipoi09876
-----------------------------------------------------------------------

今天不用Xmodem协议,直接用串口发送这个数组,写入的情况也是一样~~~麻烦大家指教!

poipoi09876 发表于 2011-11-23 10:34:20

另外,在GCC的环境下,采用库里的标准函数:
#include <inttypes.h>
#include <avr/interrupt.h>
#include <avr/pgmspace.h>

void boot_program_page (uint32_t page, uint8_t *buf)
{
    uint16_t i;
    uint8_t sreg;

    // Disable interrupts.

    sreg = SREG;
    cli();

    eeprom_busy_wait ();

    boot_page_erase (page);
    boot_spm_busy_wait ();   // Wait until the memory is erased.

    for (i=0; i<SPM_PAGESIZE; i+=2)
    {
      // Set up little-endian word.

      uint16_t w = *buf++;
      w += (*buf++) << 8;
   
      boot_page_fill (page + i, w);
    }

    boot_page_write (page);   // Store buffer in flash page.
    boot_spm_busy_wait();   // Wait until the memory is written.

    // Reenable RWW-section again. We need this if we want to jump back
    // to the application after bootloading.

    boot_rww_enable ();

    // Re-enable interrupts (if they were ever enabled).

    SREG = sreg;
}
情况也基本一样,都是写在边上,难道不用Xmodem协议就一定不行么?我这用Xmodem是可以的
马潮老师若有时间也请看看吧,感激不尽。
我就是想不用Xmodem协议做一个串口直接发送二进制代码(作为数组从主机到从机发送)做bootloader,这样可行吗?我使用的是Mega128

poipoi09876 发表于 2011-11-24 08:18:41

不好意思啊,献丑了,实际上是这样的:这些都是两个一起的十六进制数,先把他们转化成十进制的,然后再放在数组里面写入就可以啦,O(∩_∩)O哈哈~,打扰大家啦不好意思啦

chunyu 发表于 2014-9-25 09:22:00

这个思路不错   
页: [1]
查看完整版本: 各位好,请教一下啊,我想直接用数组写或者不用Xmodem协议直接用串口flash做bootloader,