搜索
bottom↓
回复: 4

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

[复制链接]

出0入0汤圆

发表于 2011-11-23 01:32:02 | 显示全部楼层 |阅读模式
各位好,请教一下啊,我想直接用数组写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

出0入0汤圆

 楼主| 发表于 2011-11-23 01:34:40 | 显示全部楼层
回复【楼主位】poipoi09876
-----------------------------------------------------------------------

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

出0入0汤圆

 楼主| 发表于 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

出0入0汤圆

 楼主| 发表于 2011-11-24 08:18:41 | 显示全部楼层
不好意思啊,献丑了,实际上是这样的:这些都是两个一起的十六进制数,先把他们转化成十进制的,然后再放在数组里面写入就可以啦,O(∩_∩)O哈哈~,打扰大家啦不好意思啦

出0入0汤圆

发表于 2014-9-25 09:22:00 | 显示全部楼层
这个思路不错   
回帖提示: 反政府言论将被立即封锁ID 在按“提交”前,请自问一下:我这样表达会给举报吗,会给自己惹麻烦吗? 另外:尽量不要使用Mark、顶等没有意义的回复。不得大量使用大字体和彩色字。【本论坛不允许直接上传手机拍摄图片,浪费大家下载带宽和论坛服务器空间,请压缩后(图片小于1兆)才上传。压缩方法可以在微信里面发给自己(不要勾选“原图),然后下载,就能得到压缩后的图片。注意:要连续压缩2次才能满足要求!!】。另外,手机版只能上传图片,要上传附件需要切换到电脑版(不需要使用电脑,手机上切换到电脑版就行,页面底部)。
您需要登录后才可以回帖 登录 | 注册

本版积分规则

手机版|Archiver|amobbs.com 阿莫电子技术论坛 ( 粤ICP备2022115958号, 版权所有:东莞阿莫电子贸易商行 创办于2004年 (公安交互式论坛备案:44190002001997 ) )

GMT+8, 2024-9-1 20:16

© Since 2004 www.amobbs.com, 原www.ourdev.cn, 原www.ouravr.com

快速回复 返回顶部 返回列表