Chinaunix首页 | 论坛 | 博客
  • 博客访问: 72801
  • 博文数量: 13
  • 博客积分: 302
  • 博客等级: 二等列兵
  • 技术积分: 210
  • 用 户 组: 普通用户
  • 注册时间: 2010-07-27 18:51
文章分类

全部博文(13)

文章存档

2012年(1)

2011年(12)

我的朋友

分类: 嵌入式

2011-05-31 14:56:02

环境描述:
  此文档基于UBUNTU 10.04 LTS +PC 和AT91RM9200DK

1. 安装 KERMIT
#apt-get install kermit
修改kermit 配置如下
#vi ~/.kermrc

set line /dev/ttyS0
set speed 115200
set carrier-watch off
set handshake none
set flow-control none
robust
set file type bin
set file name lit
set rec pack 1000
set send pack 5096
set window 5


2. kermit 使用 xmodem 发送文件全过程

A)安装XMODEM 程序,该程序源代码如下
  1. /*
  2.  * xmodem-at91.c
  3.  *
  4.  * A simple program using xmodem/1kxmode upload file to at91rm9200 based board.
  5.  * Created by (C) Copyright 2004
  6.  * Linhang.Zhang, Jilin University of PR.China, linxing@jlu.edu.cn.
  7.  *
  8.  * Modified 01-Feb-2005 (C)Copyright 2005
  9.  * Marco Cavallini, www.KoanSoftware.com - ITALY, m.cavallini@koansoftware.com
  10.  * - edited indentations and changed break usage in switch statement.
  11.  * - added "\r" to printf
  12.  *
  13.  * Modified 13-Apr-2006 Marco Cavallini
  14.  * - typo error (thank you Goran !)
  15.  *
  16.  * - build with
  17.  * gcc sx-at91.c -o sx-at91
  18.  *
  19.  * - Howto use this program with minicom/xminicom and AT91
  20.  * start minicom or xminicom
  21.  * edit Options / File transfer protocol,
  22.  * add a name (for example J) like the following example
  23.  *
  24.  * | Name Program Name U/D FullScr IO-Red. Multi |
  25.  * | A zmodem /usr/bin/sz -vv -b Y U N Y Y |
  26.  * | B ymodem /usr/bin/sb -vv Y U N Y Y |
  27.  * | C xmodem /usr/bin/sx -vv Y U N Y N |
  28.  * | D zmodem /usr/bin/rz -vv -b -E N D N Y Y |
  29.  * | E ymodem /usr/bin/rb -vv N D N Y Y |
  30.  * | F xmodem /usr/bin/rx -vv Y D N Y N |
  31.  * | G kermit /usr/bin/kermit -i -l %l -s Y U Y N N |
  32.  * | H kermit /usr/bin/kermit -i -l %l -r N D Y N N |
  33.  * | I ascii /usr/bin/ascii-xfr -dsv Y U N Y N |
  34.  * | J at91 /home/koan/xmodem/sx-at91 Y U Y N N |
  35.  * | K - |
  36.  * | L - |
  37.  *
  38.  * save and use it selecting at91 protocol when you start an Xmodem upload to AT91
  39.  *
  40.  *************************************************************************************
  41.  *
  42.  * This program is free software; you can redistribute it and/or
  43.  * modify it under the terms of the GNU General Public License as
  44.  * published by the Free Software Foundation; either version 2 of
  45.  * the License, or (at your option) any later version.
  46.  *
  47.  * This program is distributed in the hope that it will be useful,
  48.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  49.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  50.  * GNU General Public License for more details.
  51.  *
  52.  * You should have received a copy of the GNU General Public License
  53.  * along with this program; if not, write to the Free Software
  54.  * Foundation, Inc., 59 Temple Place, Suite 330, Boston,
  55.  * MA 02111-1307 USA
  56.  */

  57. #include <stdio.h>
  58. #include <stdlib.h>
  59. #include <unistd.h>
  60. #include <sys/types.h>
  61. #include <sys/stat.h>
  62. #include <fcntl.h>
  63. #include <termios.h>
  64. #include <errno.h>
  65. #include <time.h>

  66. /*
  67. Xmodem Frame form: <SOH><blk #><255-blk #><--128 data bytes--><CRC hi><CRC lo>
  68. */

  69. #define XMODEM_SOH 0x01
  70. #define XMODEM_STX 0x02
  71. #define XMODEM_EOT 0x04
  72. #define XMODEM_ACK 0x06
  73. #define XMODEM_NAK 0x15
  74. #define XMODEM_CRC_CHR    'C'
  75. #define XMODEM_CRC_SIZE 2            /* Crc_High Byte + Crc_Low Byte */
  76. #define XMODEM_FRAME_ID_SIZE 2 /* Frame_Id + 255-Frame_Id */
  77. #define XMODEM_DATA_SIZE_SOH 128 /* for Xmodem protocol */
  78. #define XMODEM_DATA_SIZE_STX 1024 /* for 1K xmodem protocol */
  79. #define USE_1K_XMODEM 0 /* 1 for use 1k_xmodem 0 for xmodem */

  80. #if (USE_1K_XMODEM)
  81.     #define XMODEM_DATA_SIZE     XMODEM_DATA_SIZE_STX
  82.     #define XMODEM_HEAD        XMODEM_STX
  83. #else
  84.     #define XMODEM_DATA_SIZE     XMODEM_DATA_SIZE_SOH
  85.     #define XMODEM_HEAD         XMODEM_SOH
  86. #endif
  87. /*********/

  88. #define SERIAL_DEVICE "/dev/ttyS0"
  89. #define MYBAUDRATE B115200

  90. /***************SUB PROGRAM*******/
  91. unsigned short GetCrc16 ( char *ptr, unsigned short count )
  92. {
  93.     unsigned short crc, i;

  94.     crc = 0;
  95.     while(count--)
  96.     {
  97.         crc = crc ^ (int) *ptr++ << 8;
  98.     
  99.         for(i = 0; i < 8; i++)
  100.         {
  101.             if(crc & 0x8000)
  102.                 crc = crc << 1 ^ 0x1021;
  103.             else            
  104.                 crc = crc << 1;
  105.         }
  106.     }

  107.     return (crc & 0xFFFF);
  108. }

  109. /*******************************/
  110. int Initial_SerialPort(void)
  111. {
  112.     int fd;
  113.     struct termios options;

  114.     fd = open( SERIAL_DEVICE , O_RDWR | O_NOCTTY | O_NDELAY );
  115.     if ( fd == -1 )
  116.     {
  117.         /*open
  118.         perror("Can't open serial port!");
  119.         return -1;
  120.     }

  121.     /*Get the current options for the port...*/
  122.     tcgetattr(fd, &options);

  123.     /*Set the baud rates to BAUDRATE...*/
  124.     cfsetispeed(&options,MYBAUDRATE);
  125.     cfsetospeed(&options,MYBAUDRATE);
  126.     tcsetattr(fd, TCSANOW, &options);
  127.     if (0 != tcgetattr(fd, &options))
  128.     {
  129.         perror("SetupSerial 1");
  130.         return -1;
  131.     }
  132.     
  133.     /*
  134.      * 8bit Data,no partity,1 stop bit...
  135.      */
  136.     options.c_cflag &= ~PARENB;
  137.     options.c_cflag &= ~CSTOPB;
  138.     options.c_cflag &= ~CSIZE;
  139.     options.c_cflag |= CS8;
  140.     tcflush(fd,TCIFLUSH);

  141.     /***Choosing Raw Input*/
  142.     options.c_lflag &= ~(ICANON | ECHO | ECHOE | ISIG);
  143.     options.c_oflag &= ~OPOST;

  144.     /*
  145.      * Set the new options for the port...
  146.      */
  147.     if (0 != tcsetattr(fd, TCSANOW, &options))
  148.     {
  149.          perror("SetupSerial error");
  150.          return -1 ;
  151.     }

  152.     return fd ;
  153. }

  154. //******************************
  155. void ClearReceiveBuffer(int fd)
  156. {
  157.     unsigned char tmp;
  158.     while ((read(fd,&tmp,1))>0);
  159.     
  160.     return;
  161. }

  162. //********************************
  163. int main(int argc,char *argv[])
  164. {
  165.     int fd;
  166.     char *data_file_name;
  167.     char packet_data[ XMODEM_DATA_SIZE ];
  168.     char frame_data[ XMODEM_DATA_SIZE + XMODEM_CRC_SIZE + XMODEM_FRAME_ID_SIZE + 1 ];
  169.     unsigned char tmp;
  170.     FILE *datafile;
  171.     int complete,retry_num,pack_counter,read_number,write_number,i;
  172.     unsigned short crc_value;
  173.     unsigned char ack_id;

  174.     printf("sx-at91 started...\r\n");
  175.     
  176.     //**open serial port1
  177.     if ( (fd = Initial_SerialPort()) == -1)
  178.         return -1 ;

  179.     data_file_name = argv[1];

  180.     if((datafile=fopen(data_file_name,"rb"))==NULL)
  181.     {
  182.         perror ("Can't open file!");
  183.         return -1 ;
  184.     }

  185.     //*******************************
  186.     
  187.     pack_counter = 0;
  188.     complete = 0;
  189.     retry_num = 0;
  190.     ClearReceiveBuffer(fd);

  191.     while((read(fd,&ack_id,1))<=0);
  192.     
  193.     printf("%c\r\n",ack_id);
  194.     ack_id=XMODEM_ACK;
  195.     while(!complete)
  196.     {
  197.         switch(ack_id)
  198.         {
  199.         case XMODEM_ACK:
  200.             retry_num = 0;
  201.             pack_counter++;
  202.             read_number = fread( packet_data, sizeof(char), XMODEM_DATA_SIZE, datafile);
  203.             if(read_number>0)
  204.             {
  205.                 if(read_number<XMODEM_DATA_SIZE_SOH)
  206.                 {
  207.             
  208.                     printf("Start filling the last frame!\r\n");
  209.                     for(;read_number<XMODEM_DATA_SIZE;read_number++)
  210.                         packet_data[read_number] = 0x00;
  211.                 }
  212.                 frame_data[0] = XMODEM_HEAD;
  213.                 frame_data[1] = (char)pack_counter;
  214.                 frame_data[2] = (char)(255-frame_data[1]);
  215.     
  216.                 for(i=0;i<XMODEM_DATA_SIZE;i++)
  217.                     frame_data[i+3]=packet_data[i];
  218.     
  219.                 crc_value = GetCrc16(packet_data,XMODEM_DATA_SIZE);
  220.                 frame_data[XMODEM_DATA_SIZE_SOH+3]=(unsigned char)(crc_value >> 8);
  221.                 frame_data[XMODEM_DATA_SIZE_SOH+4]=(unsigned char)(crc_value);
  222.                 write_number = write( fd, frame_data, XMODEM_DATA_SIZE_SOH + 5);
  223.                 printf("waiting for ACK,%d,%d,...",pack_counter,write_number);
  224.                 while((read(fd,&ack_id,1))<=0);
  225.             
  226.                 if(ack_id == XMODEM_ACK)
  227.                     printf("Ok!\r\n");
  228.                 else
  229.                     printf("Error!\r\n");
  230.             }
  231.             else
  232.             {
  233.                 ack_id = XMODEM_EOT;
  234.                 complete = 1;
  235.                 printf("Waiting for complete ACK ...");
  236.             
  237.                 while(ack_id != XMODEM_ACK)
  238.                 {
  239.                     ack_id = XMODEM_EOT;    
  240.                     write_number=write(fd,&ack_id,1);
  241.                     while((read(fd,&ack_id,1))<=0);
  242.                 }
  243.                 printf("OK\r\n");
  244.         
  245.                 printf("Sending file complete\r\n");
  246.             }
  247.         break;

  248.         case XMODEM_NAK:
  249.             if( retry_num++ > 10)
  250.             {
  251.                 printf("Retry too many times,Quit!\r\n");
  252.                 complete = 1;
  253.             }
  254.             else
  255.             {
  256.                 write_number = write(fd,frame_data,XMODEM_DATA_SIZE + 5);
  257.                 printf("Retry for ACK,%d,%d...",pack_counter,write_number);
  258.                 while((read(fd,&ack_id,1))<=0);
  259.                 
  260.                 if( ack_id == XMODEM_ACK )
  261.                     printf("OK\r\n");
  262.                 else
  263.                     printf("Error!\r\n");
  264.             }
  265.         break;

  266.         default:
  267.             printf("Fatal Error!\r\n");
  268.             complete = 1;
  269.         break;
  270.         }

  271.     }

  272.     fclose(datafile);
  273.     close(fd);

  274.     return 0;
  275. }

编译之
# gcc -o xmodem xmodem-at91.c
然后安装到/usr/bin目录下
# cp xmodem /usr/bin/

B)启动目标板上的XMODEM协议,AT91RM9200DK只需要设置从ROM启动,即启动了XMODEM协议,此时一直显示
CCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCCC

C) 切换到 KERMIT    ctal+\+c

D) C-kermit> run xmodem loader.bin
   C-kermit> run xmodem uboot.bin

E)用KERMIT协议发送文件只需要如下命令即可
EG:
C-kermit> send kernl.img


阅读(3360) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~