?? mx1-load-rs232.c
字號:
/******************************************************************* MX1-Loader - DragonBall M9328/MX1 loader mx1-load-rs232.c - RS232 factory ROM bootstrap protocol support (C) Copyright 2004 by Pavel Pisa - project originator http://cmp.felk.cvut.cz/~pisa (C) Copyright 2004 PiKRON Ltd. http://www.pikron.com USB support based on work of Roman Bartosinski (bartosr@centrum.cz) The MX1-Loader project can be used and distributed in compliance with any of next licenses - GPL - GNU Public License See file COPYING for details. - LGPL - Lesser GNU Public License - MPL - Mozilla Public License - and other licenses added by project originator Code can be modified and re-distributed under any combination of the above listed licenses. If contributor does not agree with some of the licenses, he/she can delete appropriate line. WARNING: if you delete all lines, you are not allowed to distribute code or sources in any form. *******************************************************************/#define _GNU_SOURCE#include <stdlib.h>#include <stdio.h>#include <string.h>#include <ctype.h>#include <termios.h>#include <sys/time.h>#include <sys/types.h>#include <errno.h>#include <unistd.h>#include <fcntl.h>#include "rs232_fn.h"#include "universal-load.h"int get_hex(char **p, unsigned *v, int chars){ unsigned u=0; unsigned char c; *v=0; while(**p==' ') (*p)++; do { c=**p; if((c>='0')&&(c<='9')) c-='0'; else if((c>='A')&&(c<='F')) c-=('A'-10); else { if(chars>=0) return -1; *v=u; return -chars; } u<<=4; u+=c; (*p)++; } while(--chars); *v=u; return 0;};int put_hex(char **p, unsigned long v, int chars){ unsigned char c; while(chars--){ c=(v>>(4*chars))&0xf; c+='0'; if(c>'9') c+='A'-'9'-1; **p=c; (*p)++; } return 0;};int mx1_setup(uniload_alg_info_t *algi){ int fd=algi->dev_fd; if(rs232_sendf(fd, 0, "a")<0) return -1; if(rs232_wait_delimit(fd,":",1000)<0){ fprintf(stderr,"No ':' reply\n"); } rs232_sendch(fd,0xd); if(rs232_wait_delimit(fd,"\015",1000)<0){ fprintf(stderr,"No new line reply\n"); } return 0;}int mx1_mem_write(uniload_alg_info_t *algi, int mem_type, unsigned long start, unsigned long len, const char *buff){ int fd=algi->dev_fd; int res; char s[(4+1+32)*2]; char *p; const unsigned char *b; int blen=31; int i; int cnt; char cr=0xd; while(len){ cnt=(len>blen)?blen:len; p=s; b=buff; put_hex(&p, start, 8); put_hex(&p, 0x30*0+0x20*0+cnt, 2); for(i=0;i<cnt;i++) put_hex(&p, *(b++), 2); if((res=rs232_writem(fd, RS232_SF_ECHO, s, p-s))<0){ fprintf(stderr,"write data bytes failed\n"); return -1; } if((res=rs232_recch_wait(fd, 100))!='/'){ fprintf(stderr,"write bad reply %c\n",res); return -1; } if((res=rs232_writem(fd, RS232_SF_ECHO, &cr, 1))<0){ fprintf(stderr,"write line end failed\n"); return -1; } start+=cnt; len-=cnt; buff+=cnt; } return 0;}int mx1_mem_read(uniload_alg_info_t *algi, int mem_type, unsigned long start, unsigned long len, char *buff){ int fd=algi->dev_fd; int res; char s[(4+1+32)*2]; char *p; unsigned char *b; int blen=31; int i; int cnt; unsigned val; char cr=0xd; while(len){ cnt=(len>blen)?blen:len; p=s; b=buff; put_hex(&p, start, 8); put_hex(&p, 0x30*0+0x20*1+cnt, 2); if((res=rs232_writem(fd, RS232_SF_ECHO, s, p-s))<0){ fprintf(stderr,"read command failed\n"); return -1; } if((res=rs232_readm(fd, 1000, s, cnt*2))!=cnt*2){ fprintf(stderr,"read data failed\n"); return -1; } p=s; for(i=0;i<cnt;i++){ if((res=get_hex(&p,&val,2))<0){ fprintf(stderr,"read hex bad format\n"); return -1; } *(b++)=val; } if((res=rs232_recch_wait(fd, 100))!='/'){ fprintf(stderr,"read bad reply %c\n",res); return -1; } if((res=rs232_writem(fd, RS232_SF_ECHO, &cr, 1))<0){ fprintf(stderr,"write line end failed\n"); return -1; } start+=cnt; len-=cnt; buff+=cnt; } return 0;}int mx1_go_addr(uniload_alg_info_t *algi, unsigned long addr){ int fd=algi->dev_fd; char s[16]; char *p; int res; p=s; dbprintf("Starting target from %08lX\n", addr); put_hex(&p, addr, 8); put_hex(&p, 0x30*0+0x20*0+0, 2); if((res=rs232_writem(fd, RS232_SF_ECHO, s, p-s))<0){ fprintf(stderr,"go command failed\n"); return -1; } if((res=rs232_recch_wait(fd, 100))!='/'){ fprintf(stderr,"go bad reply %c\n",res); return -1; } return 0;}uniload_alg_ops_t mx1_rs232_alg_ops={ .setup=mx1_setup, .mem_write=mx1_mem_write, .mem_read=mx1_mem_read, .go_addr=mx1_go_addr,};int mx1_rs232_open(uniload_alg_info_t *algi, char *sdev, int baud, int mode, int flowc){ int dev_fd; /* Open RS232 device */ if ((dev_fd = open(sdev, O_RDWR | O_NONBLOCK)) == -1) { fprintf(stderr,"Cannot open serial device %s\n",sdev); return -1; } /* Set RS232 device mode and speed */ if(rs232_setmode(dev_fd,baud,0,0)<0){ printf("Error in rs232_setmode\n"); close(dev_fd); return -1; } sleep(1); tcflush(dev_fd, TCIOFLUSH); sleep(1); algi->dev_fd=dev_fd; algi->dev_name=sdev; algi->alg_ops=&mx1_rs232_alg_ops; return 0;}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -