Chinaunix首页 | 论坛 | 博客
  • 博客访问: 32150
  • 博文数量: 12
  • 博客积分: 445
  • 博客等级: 下士
  • 技术积分: 150
  • 用 户 组: 普通用户
  • 注册时间: 2012-10-06 16:49
文章分类

全部博文(12)

文章存档

2012年(12)

我的朋友

分类: 嵌入式

2012-10-21 22:56:56

9、u-boot增加串口xmodem协议

   
    u-boot中已经有了串口驱动,现在要添加load命令,即使用串口xmodem协议传输文件。
(1)在smdk2440.h中增加支持loadx命令的配置,修改代码如下:
  1. #define CONFIG_MTD_DEVICE
  2. #define CONFIG_MTD_PARTITIONS
  3. #define MTDIDS_DEFAULT "nand0=nandflash0"
  4. #define MTDPARTS_DEFAULT        "mtdparts=nandflash0:1m@0(bios)," \
  5.                                     "128k(params)," \
  6.                                     "4m(kernel)," \
  7.                                     "-(root)"
  8. #define ENABLE_CMD_LOADB_X    1 

(2)修改common目录下的cmd_load.c文件,加入load_serial_xmodem的函数声明,修改的代码如下:
  1. #if defined(CONFIG_CMD_LOADB)
  2. #if defined(ENABLE_CMD_LOADB_X)
  3. static ulong load_serial_xmodem (ulong offset);
  4. #endif
  5. static ulong load_serial_ymodem (ulong offset);
  6. #endif

(3)在do_load_serial_bin 函数中修改代码如下:
  1. if (load_baudrate != current_baudrate) {
  2.         printf ("## Switch baudrate to %d bps and press ENTER ...\n",
  3.             load_baudrate);
  4.         udelay(50000);
  5.         gd->baudrate = load_baudrate;
  6.         serial_setbrg ();
  7.         udelay(50000);
  8.         for (;;) {
  9.             if (getc() == '\r')
  10.                 break;
  11.         }
  12.     }
  13.      
  14. #if defined(ENABLE_CMD_LOADB_X)
  15.     if (strcmp(argv[0],"loadx")==0) {
  16.         printf ("## Ready for binary (xmodem) download "
  17.             "to 0x%08lX at %d bps...\n",
  18.             offset,
  19.             load_baudrate);

  20.         addr = load_serial_xmodem (offset);

  21.     } else if (strcmp(argv[0],"loady")==0) {
  22. #else
  23.     if (strcmp(argv[0],"loady")==0) {
  24. #endif

  25.     //if (strcmp(argv[0],"loady")==0) {    /* zjw del 20121012 */
  26.         printf ("## Ready for binary (ymodem) download "
  27.             "to 0x%08lX at %d bps...\n",
  28.             offset,
  29.             load_baudrate);

  30.         addr = load_serial_ymodem (offset);

  31.     } else {

  32.         printf ("## Ready for binary (kermit) download "
  33.             "to 0x%08lX at %d bps...\n",
  34.             offset,
  35.             load_baudrate);
  36.         addr = load_serial_bin (offset);

  37.         if (addr == ~0) {
  38.             load_addr = 0;
  39.             printf ("## Binary (kermit) download aborted\n");
  40.             rcode = 1;
  41.         } else {
  42.             printf ("## Start Addr = 0x%08lX\n", addr);
  43.             load_addr = addr;
  44.         }
  45.     }

(4)在函数load_serial_ymodem之前增加load_serial_xmodem函数的实现,代码如下:
  1. #if defined(ENABLE_CMD_LOADB_X)
  2. static ulong load_serial_xmodem (ulong offset)
  3. {
  4.     int size;
  5.     char buf[32];
  6.     int err;
  7.     int res;
  8.     connection_info_t info;
  9.     char xmodemBuf[1024];
  10.     ulong store_addr = ~0;
  11.     ulong addr = 0;

  12.     size = 0;
  13.     info.mode = xyzModem_xmodem;
  14.     res = xyzModem_stream_open (&info, &err);
  15.     if (!res) {

  16.         while ((res =
  17.             xyzModem_stream_read (xmodemBuf, 1024, &err)) > 0) {
  18.             store_addr = addr + offset;
  19.             size += res;
  20.             addr += res;
  21. #ifndef CFG_NO_FLASH
  22.             if (addr2info (store_addr)) {
  23.                 int rc;

  24.                 rc = flash_write ((char *) xmodemBuf,
  25.                 store_addr, res);
  26.                 if (rc != 0) {
  27.                 flash_perror (rc);
  28.                 return (~0);
  29.                 }
  30.             } else
  31. #endif
  32.             {
  33.                 memcpy ((char *) (store_addr), xmodemBuf,
  34.                     res);
  35.             }

  36.         }
  37.     } else {
  38.         printf ("%s\n", xyzModem_error (err));
  39.     }

  40.     xyzModem_stream_close (&err);
  41.     xyzModem_stream_terminate (false, &getcxmodem);


  42.     flush_cache (offset, size);

  43.     printf ("## Total Size = 0x%08x = %d Bytes\n", size, size);
  44.     sprintf (buf, "%X", size);
  45.     setenv ("filesize", buf);

  46.     return offset;
  47. }
  48. #endif

(5)在最后面的U_BOOT_CMD处加入loadx命令,修改的代码如下:
  1. #if defined(CONFIG_CMD_LOADB)
  2. U_BOOT_CMD(
  3.     loadb, 3, 0,    do_load_serial_bin,
  4.     "load binary file over serial line (kermit mode)",
  5.     "[ off ] [ baud ]\n"
  6.     " - load binary file over serial line"
  7.     " with offset 'off' and baudrate 'baud'"
  8. );

  9. #if defined(ENABLE_CMD_LOADB_X)
  10. U_BOOT_CMD(
  11.     loadx, 3, 0, do_load_serial_bin,
  12.     "load binary file over serial line (xmodem mode)",
  13.     "[ off ] [ baud ]\n"
  14.     " - load binary file over serial line"
  15.     " with offset 'off' and baudrate 'baud'"
  16. );
  17. #endif

  18. U_BOOT_CMD(
  19.     loady, 3, 0,    do_load_serial_bin,
  20.     "load binary file over serial line (ymodem mode)",
  21.     "[ off ] [ baud ]\n"
  22.     " - load binary file over serial line"
  23.     " with offset 'off' and baudrate 'baud'"
  24. );

  25. #endif

   重新编译u-boot,下载到NandFlash中运行,可以看到已经支持了loadx命令,可以使用loadx命令通过串口来下载东西了,效果如下:
阅读(907) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~