#include #include #include #include #include #include #include typedef unsigned long word; typedef unsigned short half; typedef unsigned char byte; #define swap_w(w) (BE)?(w):((((w)<<24)&0xff000000)|(((w)<< 8)&0x00ff0000)|\ (((w)>> 8)&0x0000ff00)|(((w)>>24)&0x000000ff)) #define swap_h(w) (BE)?(w):((((w)<< 8)&0x0000ff00)|(((w)>> 8)&0x000000ff)) #define BAUDRATE B57600 /* 通信速度の設定 */ #define MODEMDEVICE "/dev/ttyS14" /* デバイスファイルの指定 */ #define FALSE 0 #define TRUE 1 #define MC 1 /* 読み込む文字数 */ #define SEND_BYTE_LEN 16 /* writeb で書き込むバイト数 */ #define MAX_CMD_LEN 256 /* 受け付けれるコマンドの最大長 */ #define BLOCK_SIZE 65536 /* FLASHのブロックサイズ */ #define FLASH_TOP_ADDR 0xff800000 /* 書き込み先の先頭アドレス */ volatile int STOP=FALSE; int BE; //endian 1:big 0:little char fbuf[BLOCK_SIZE]; int build_writeb( char *bin, char *cmdstr, int addr, int len); int build_writef( char *bin, char *cmdstr, int addr ); int wait_complite(int fd); word checksum( char *bin ); int readn(int fd,char *buf, int len); int write_block(int fd, char *bin, int blk); int main(int argc, char *argv[]) { int fd, c, res, i,l; /* fd:ファイルディスクリプタ res:受け取った文字数 */ struct termios oldtio, newtio; /* 通信ポートを制御するためのインターフェイス */ char buf[MAX_CMD_LEN]; /* 受信文字を格納 */ FILE *fp; int count; int wait_count; struct timespec req, rem; int ret; int blk; if(argc<2) { printf("sci filename [start block]\r\n"); exit(-1); } fp = fopen(argv[1], "rb"); if( fp == NULL ) { printf("can't open %s\r\n", argv[1]); exit(-1); } if((fd=open(MODEMDEVICE, O_RDWR | O_NOCTTY))== -1){ printf("can't open ttys\r\n"); fclose(fp); perror(MODEMDEVICE); exit(-1); } tcgetattr(fd, &oldtio); /* 現在のシリアルポートの設定を退避させる */ bzero(&newtio, sizeof(newtio)); /* 新しいポートの設定の構造体をクリア */ newtio.c_cflag= (BAUDRATE | CS8 | CLOCAL | CREAD); newtio.c_iflag=IGNPAR; /* FER,PER無視 */ newtio.c_oflag=0; /* */ newtio.c_lflag=0; /* 非カノニカルモード */ newtio.c_cc[VTIME]=0; /* タイムアウトなし */ newtio.c_cc[VMIN]=MC; /* 最小文字数1 */ tcflush(fd,TCIOFLUSH); /* 送受信バッファクリア */ tcsetattr(fd, TCSANOW, &newtio); /* 新パラメータ設定 */ /* ファイルサイズ取得 */ fseek(fp, 0, SEEK_END); count = ftell(fp); fseek(fp, 0, SEEK_SET); printf("file size = %d\r\n", count); /* 初期ブロック指定 */ blk = 0; if(argc==3){ blk = atoi(argv[2]); printf("start BLK=%d\r\n", blk); fseek(fp, blk*BLOCK_SIZE, SEEK_SET); } /* ファームウェア送信 */ for(; blk<(count/BLOCK_SIZE); blk++) { printf("BLK = %4d\r\n", blk); fread(&fbuf[0], BLOCK_SIZE, 1, fp); /* バイナリ読み込み */ write_block( fd, &fbuf[0], blk ); /* 1ブロック書き込み */ //break; } /* ファームウェア送信(64KB未満の部分) */ printf("BLK = %4d\r\n", blk); for(i=0; i' ){ // >RET:0 ret = retstr[5]-'0'; } readn(fd, &retstr[6], 2); retstr[8] = 0x00; return ret; } word checksum( char *bin ){ word sum; word *wp; word w; int c; int fs; c = 0x01000000; BE = *(byte *)(&c); fs = BLOCK_SIZE; sum = 0; wp = (word *)bin; for(c = 0; c < (fs - 3); c += 4) { w = *wp++; sum += swap_w(w); } if (c < fs) { w = swap_w(*wp); switch(fs - c) { case 1: sum += (w & 0xff000000); break; case 2: sum += (w & 0xffff0000); break; case 3: sum += (w & 0xffffff00); break; } } return sum; } int readn(int fd,char *buf, int len) { char tmp[MAX_CMD_LEN]; int i; int cur; int ret; cur = 0; while(cur0 ){ buf[cur] = tmp[0]; cur++; } } return 0; } int write_block(int fd, char *bin, int blk) { int i; int ret; char cmdstr[MAX_CMD_LEN]; char buf[MAX_CMD_LEN]; for(i=0; i