📄 download.c
字号:
/** * Title: download.c * Type: C (*.c) * Complier: gcc version 3.3.2 20031022 (Red Hat Linux 3.3.2-1) * Description: send file to armboard's flash * Copyright: Copyright (c) 2004 * Company: djws * author djws * version 1.0 */#include <stdio.h> /*标准输入输出定义*/#include <stdlib.h> /*标准函数库定义*/#include <unistd.h> /*Unix标准函数定义*/#include <sys/types.h> /**/#include <sys/stat.h> /**/#include <fcntl.h> /*文件控制定义*/#include <termios.h> /*PPSIX终端控制定义*/#include <errno.h> /*错误号定义*/#include <string.h>#define PRINT_VERSION printf("version 1.0\n");// for armboard transfer#define BR_115200 1#define BR_57600 3#define BR_38400 5#define BR_19200 11#define BR_9600 23#define BR_2400 95#define BR_1200 191#define TRUE 1#define FALSE 0// old functionsint set_speed(int fd, int speed);int set_Parity(int fd,int databits,int stopbits,int parity);int OpenDev(char *Dev);// my functionsvoid print_help();void default_init(char *dev, int *rate, char *filename, char *addr);void send_client(int fd);int atohex(char *str);int flen(FILE *fp);int transfer(int fd, int rate, char *addr, char *filename);int tty_raw(int fd);int tty_reset(int fd);static struct termios save_termios;static int ttysavefd = -1;static enum { RESET, RAW, CBREAK } ttystate = RESET;/***@brief 设置串口通信速率*@param fd 类型 int 打开串口的文件句柄*@param speed 类型 int 串口速度*@return void*/int speed_arr[] = { B115200, B57600, B38400, B19200, B9600, B2400, B1200 };int name_arr[] = { 115200L, 57600L, 38400L, 19200L, 9600L, 2400L, 1200L };// for client obj filechar client_obj[2048] = "Copy Client Binary Program Here...";int set_speed(int fd, int speed){ int i; int status; struct termios Opt; tcgetattr(fd, &Opt); for ( i= 0; i < sizeof(speed_arr) / sizeof(int); i++) { if (speed == name_arr[i]) { tcflush(fd, TCIOFLUSH); cfsetispeed(&Opt, speed_arr[i]); cfsetospeed(&Opt, speed_arr[i]); status = tcsetattr(fd, TCSANOW, &Opt); if (status != 0) perror("tcsetattr fd1"); return (TRUE); } tcflush(fd,TCIOFLUSH); } return (FALSE);}/***@brief 设置串口数据位,停止位和效验位*@param fd 类型 int 打开的串口文件句柄**@param databits 类型 int 数据位 取值 为 7 或者8**@param stopbits 类型 int 停止位 取值为 1 或者2**@param parity 类型 int 效验类型 取值为N,E,O,,S*/int set_Parity(int fd,int databits,int stopbits,int parity){ struct termios options; if ( tcgetattr( fd,&options) != 0) { perror("SetupSerial 1"); return(FALSE); } options.c_cflag &= ~CSIZE; switch (databits) /*设置数据位数*/ { case 7: options.c_cflag |= CS7; break; case 8: options.c_cflag |= CS8; break; default: fprintf(stderr,"Unsupported data size\n"); return (FALSE); } switch (parity) { case 'n': case 'N': options.c_cflag &= ~PARENB; /* Clear parity enable */ options.c_iflag &= ~INPCK; /* Enable parity checking */ break; case 'o': case 'O': options.c_cflag |= (PARODD | PARENB); /* 设置为奇效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'e': case 'E': options.c_cflag |= PARENB; /* Enable parity */ options.c_cflag &= ~PARODD; /* 转换为偶效验*/ options.c_iflag |= INPCK; /* Disnable parity checking */ break; case 'S': case 's': /*as no parity*/ options.c_cflag &= ~PARENB; options.c_cflag &= ~CSTOPB; break; default: fprintf(stderr,"Unsupported parity\n"); return (FALSE); } /* 设置停止位*/ switch (stopbits) { case 1: options.c_cflag &= ~CSTOPB; break; case 2: options.c_cflag |= CSTOPB; break; default: fprintf(stderr,"Unsupported stop bits\n"); return (FALSE); } /* Set input parity option */ if (parity != 'n') options.c_iflag |= INPCK; options.c_cc[VTIME] = 150; // 15 seconds options.c_cc[VMIN] = 0; tcflush(fd,TCIFLUSH); /* Update the options and do it NOW */ if (tcsetattr(fd,TCSANOW,&options) != 0) { perror("SetupSerial 3"); return (FALSE); } return (TRUE); }/***@breif 打开串口*/int OpenDev(char *Dev){ int fd = open( Dev, O_RDWR ); //| O_NOCTTY | O_NDELAY FILE *fp = fdopen(fd, "rb+"); if (-1 == fd) { /*设置数据位数*/ perror("Can't Open Serial Port"); return -1; } else { tty_raw(fd); setvbuf(fp, (char *)NULL, _IONBF, 0); return fd; }}inttty_raw(int fd) /* put terminal into a raw mode */{ struct termios buf; if (tcgetattr(fd, &save_termios) < 0) return(-1); buf = save_termios; /* structure copy */ buf.c_lflag &= ~(ECHO | ICANON | IEXTEN | ISIG); /* echo off, canonical mode off, extended input processing off, signal chars off */ buf.c_iflag &= ~(BRKINT | ICRNL | INPCK | ISTRIP | IXON); /* no SIGINT on BREAK, CR-to-NL off, input parity check off, don't strip 8th bit on input, output flow control off */ buf.c_cflag &= ~(CSIZE | PARENB); /* clear size bits, parity checking off */ buf.c_cflag |= CS8; /* set 8 bits/char */ buf.c_oflag &= ~(OPOST); /* output processing off */ buf.c_cc[VMIN] = 1; /* Case B: 1 byte at a time, no timer */ buf.c_cc[VTIME] = 0; if (tcsetattr(fd, TCSAFLUSH, &buf) < 0) return(-1); ttystate = RAW; ttysavefd = fd; return(0);}inttty_reset(int fd) /* restore terminal's mode */{ if (ttystate != CBREAK && ttystate != RAW) return(0); if (tcsetattr(fd, TCSAFLUSH, &save_termios) < 0) return(-1); ttystate = RESET; return(0);}voidprint_help() { printf("Example: download [options]... [parameter]...\n"); printf("Options: \n"); printf(" -d, --device [DEVICENAME] set your device file path, default is '/dev/ttyS0'\n"); printf(" -r, --rate [BAUDRATE] set baud rate, default is 115200 \n"); printf(" Support rate: 115200 57600, 38400, 19200, 9600, 2400, 1200\n"); printf(" -f, --filename [BAUDRATE] set the file you send, default is 'u-boot.bin'\n"); printf(" -a, --address [ADDRESS] set the address you want to write, must be a hex number, default is '0x0'\n"); printf(" Sample: 0x20000 0x40000 The Max is 0x1000000\n"); printf(" -h, --help print help\n"); printf(" -v, --version print version\n"); printf("Finished.\n");}voiddefault_init(char *dev, int *rate, char *filename, char *addr) { strcpy(dev, "/dev/ttyS0"); *rate = 115200L; strcpy(filename, "u-boot.bin"); strcpy(addr, "0x0");}voidsend_client(int fd) { write(fd, client_obj, 2048);}intatohex(char *str) { int num = 0; int len = strlen(str); int i; char c; int tag = 0; if(len < 2) return(0); if(len > 10) return(0); if((str[0]!='0') || (str[1]!='x')) return(0); for(i=2; i<len; i++) { if((str[i]==0x30) && (tag==0)) continue; if((str[i]>='0') && (str[i]<='9')) { tag = 1; num = num<<4; c = str[i]&0x0f; num += c; } else if((str[i]>='A') && (str[i]<='F')) { tag = 1; num = num<<4; c = str[i]-55; num += c; } else if((str[i]>='a') && (str[i]<='f')) { tag = 1; num = num<<4; c = str[i]-87; num += c; } else return(0); } return(num);}intflen(FILE *fp) { int i = 0; if(fp == NULL) return 0; if(fseek(fp, 0, SEEK_SET)) return 0; while(!feof(fp)) { i++; fgetc(fp); } if(fseek(fp, 0, SEEK_SET)) return 0; return i-1;}/* our protocol *//*PC arm <发送2k >a a 是否准备好s s 设波特率 BR4字节 设置的数值,arm中间延时 返回od d 地址 地址4字节 返回 ol l
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -