From 4e2d4c761d62ad622a6c2259657452b1b562cf67 Mon Sep 17 00:00:00 2001 From: Michal Sojka Date: Wed, 21 Jul 2010 15:54:30 +0200 Subject: [PATCH] Included TOHIT H8300 application and flash loader. The app/ directory was mess. This is an attempt to make the situation better. --- arch/h8300/generic/Makefile.omk | 2 +- arch/h8300/generic/tools/Makefile | 14 + arch/h8300/generic/tools/Makefile.omk | 3 + arch/h8300/generic/tools/tohit/Makefile | 14 + arch/h8300/generic/tools/tohit/Makefile.omk | 4 + arch/h8300/generic/tools/tohit/Makefile.std | 27 + arch/h8300/generic/tools/tohit/README | 5 + arch/h8300/generic/tools/tohit/cfsetspeed.c | 129 ++++ arch/h8300/generic/tools/tohit/hiterm.c | 450 ++++++++++++ arch/h8300/generic/tools/tohit/rs232_lt.c | 184 +++++ arch/h8300/generic/tools/tohit/tohit.c | 229 ++++++ arch/h8300/generic/tools/tohit/tohit_fn.c | 775 ++++++++++++++++++++ arch/h8300/generic/tools/tohit/tohit_fn.h | 66 ++ 13 files changed, 1901 insertions(+), 1 deletion(-) create mode 100644 arch/h8300/generic/tools/Makefile create mode 100644 arch/h8300/generic/tools/Makefile.omk create mode 100644 arch/h8300/generic/tools/tohit/Makefile create mode 100644 arch/h8300/generic/tools/tohit/Makefile.omk create mode 100644 arch/h8300/generic/tools/tohit/Makefile.std create mode 100644 arch/h8300/generic/tools/tohit/README create mode 100644 arch/h8300/generic/tools/tohit/cfsetspeed.c create mode 100644 arch/h8300/generic/tools/tohit/hiterm.c create mode 100644 arch/h8300/generic/tools/tohit/rs232_lt.c create mode 100644 arch/h8300/generic/tools/tohit/tohit.c create mode 100644 arch/h8300/generic/tools/tohit/tohit_fn.c create mode 100644 arch/h8300/generic/tools/tohit/tohit_fn.h diff --git a/arch/h8300/generic/Makefile.omk b/arch/h8300/generic/Makefile.omk index 81bac75..6a31eaf 100644 --- a/arch/h8300/generic/Makefile.omk +++ b/arch/h8300/generic/Makefile.omk @@ -1,3 +1,3 @@ # -*- makefile -*- -SUBDIRS = defines libs drivers +SUBDIRS = defines libs drivers tools diff --git a/arch/h8300/generic/tools/Makefile b/arch/h8300/generic/tools/Makefile new file mode 100644 index 0000000..76b56fd --- /dev/null +++ b/arch/h8300/generic/tools/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or parent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/arch/h8300/generic/tools/Makefile.omk b/arch/h8300/generic/tools/Makefile.omk new file mode 100644 index 0000000..1739569 --- /dev/null +++ b/arch/h8300/generic/tools/Makefile.omk @@ -0,0 +1,3 @@ +# -*- makefile -*- + +SUBDIRS := tohit diff --git a/arch/h8300/generic/tools/tohit/Makefile b/arch/h8300/generic/tools/tohit/Makefile new file mode 100644 index 0000000..76b56fd --- /dev/null +++ b/arch/h8300/generic/tools/tohit/Makefile @@ -0,0 +1,14 @@ +# Generic directory or leaf node makefile for OCERA make framework + +ifndef MAKERULES_DIR +MAKERULES_DIR := $(shell ( old_pwd="" ; while [ ! -e Makefile.rules ] ; do if [ "$$old_pwd" = `pwd` ] ; then exit 1 ; else old_pwd=`pwd` ; cd -L .. 2>/dev/null ; fi ; done ; pwd ) ) +endif + +ifeq ($(MAKERULES_DIR),) +all : default +.DEFAULT:: + @echo -e "\nThe Makefile.rules has not been found in this or parent directory\n" +else +include $(MAKERULES_DIR)/Makefile.rules +endif + diff --git a/arch/h8300/generic/tools/tohit/Makefile.omk b/arch/h8300/generic/tools/tohit/Makefile.omk new file mode 100644 index 0000000..f1d02c3 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/Makefile.omk @@ -0,0 +1,4 @@ +# -*- makefile -*- +utils_PROGRAMS = tohit + +tohit_SOURCES = tohit.c tohit_fn.c diff --git a/arch/h8300/generic/tools/tohit/Makefile.std b/arch/h8300/generic/tools/tohit/Makefile.std new file mode 100644 index 0000000..7546cbb --- /dev/null +++ b/arch/h8300/generic/tools/tohit/Makefile.std @@ -0,0 +1,27 @@ +.PHONY: all + +ifeq ($(SOURCES_DIR),) +all: tohit rs232_lt +else +# if called from OMK +all: + $(MAKE) -C $(SOURCES_DIR) SOURCES_DIR='' all +endif + +# if called from OMK +binary-pass: all +check-dir include-pass library-pass utils-pass default-config-pass: + +#all: tohit hiterm + +CFLAGS=-O2 -Wall + +tohit: tohit.o tohit_fn.o + +hiterm: hiterm.o tohit_fn.o + +rs232_lt: rs232_lt.o tohit_fn.o + $(CC) $(LDFLAGS) $(CFLAGS) -lncurses -o $@ $^ + +clean: + rm -f *.o diff --git a/arch/h8300/generic/tools/tohit/README b/arch/h8300/generic/tools/tohit/README new file mode 100644 index 0000000..c3f39ad --- /dev/null +++ b/arch/h8300/generic/tools/tohit/README @@ -0,0 +1,5 @@ + +TOHIT +===== + +Program uploader for Hitachi H8S/263x processor. diff --git a/arch/h8300/generic/tools/tohit/cfsetspeed.c b/arch/h8300/generic/tools/tohit/cfsetspeed.c new file mode 100644 index 0000000..d183f45 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/cfsetspeed.c @@ -0,0 +1,129 @@ +/* Copyright (C) 1992, 1993, 1996, 1997, 1998 Free Software Foundation, Inc. + This file is part of the GNU C Library. + + The GNU C Library is free software; you can redistribute it and/or + modify it under the terms of the GNU Library General Public License as + published by the Free Software Foundation; either version 2 of the + License, or (at your option) any later version. + + The GNU C Library is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU + Library General Public License for more details. + + You should have received a copy of the GNU Library General Public + License along with the GNU C Library; see the file COPYING.LIB. If not, + write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330, + Boston, MA 02111-1307, USA. */ + +#include +#include +#include + +struct speed_struct +{ + speed_t value; + speed_t internal; +}; + +static const struct speed_struct speeds[] = + { +#ifdef B0 + { 0, B0 }, +#endif +#ifdef B50 + { 50, B50 }, +#endif +#ifdef B75 + { 75, B75 }, +#endif +#ifdef B110 + { 110, B110 }, +#endif +#ifdef B134 + { 134, B134 }, +#endif +#ifdef B150 + { 150, B150 }, +#endif +#ifdef B200 + { 200, B200 }, +#endif +#ifdef B300 + { 300, B300 }, +#endif +#ifdef B600 + { 600, B600 }, +#endif +#ifdef B1200 + { 1200, B1200 }, +#endif +#ifdef B1200 + { 1200, B1200 }, +#endif +#ifdef B1800 + { 1800, B1800 }, +#endif +#ifdef B2400 + { 2400, B2400 }, +#endif +#ifdef B4800 + { 4800, B4800 }, +#endif +#ifdef B9600 + { 9600, B9600 }, +#endif +#ifdef B19200 + { 19200, B19200 }, +#endif +#ifdef B38400 + { 38400, B38400 }, +#endif +#ifdef B57600 + { 57600, B57600 }, +#endif +#ifdef B76800 + { 76800, B76800 }, +#endif +#ifdef B115200 + { 115200, B115200 }, +#endif +#ifdef B153600 + { 153600, B153600 }, +#endif +#ifdef B230400 + { 230400, B230400 }, +#endif +#ifdef B307200 + { 307200, B307200 }, +#endif +#ifdef B460800 + { 460800, B460800 }, +#endif + }; + + +/* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */ +int +cfsetspeed (struct termios *termios_p, speed_t speed) +{ + size_t cnt; + + for (cnt = 0; cnt < sizeof (speeds) / sizeof (speeds[0]); ++cnt) + if (speed == speeds[cnt].internal) + { + cfsetispeed (termios_p, speed); + cfsetospeed (termios_p, speed); + return 0; + } + else if (speed == speeds[cnt].value) + { + cfsetispeed (termios_p, speeds[cnt].internal); + cfsetospeed (termios_p, speeds[cnt].internal); + return 0; + } + + __set_errno (EINVAL); + + return -1; +} diff --git a/arch/h8300/generic/tools/tohit/hiterm.c b/arch/h8300/generic/tools/tohit/hiterm.c new file mode 100644 index 0000000..26f8823 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/hiterm.c @@ -0,0 +1,450 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +#define DEBUG 0 + +int cnt; +char * arg[10]; + +unsigned char * bbuf=NULL; +long Len=0; + +int tohit(int argc, char **argv); + +void SaveBB(int fd,char *buf,int len) +{ + char str[100]; + int i; + int fd1; + + if (!bbuf){ + printf("Error bufer empty\n"); + return; + } + while(*buf && *buf!=' ') buf++; + while(*buf && *buf==' ') buf++; + i=0; + while(*buf && *buf!=' ' && *buf!=10 && *buf!=13) str[i++]=*buf++; + str[i]=0; + + if(i<1){ + printf("Error bad parametr \n"); + return; + } + if ((fd1 = open(str, O_CREAT | O_WRONLY, 0644)) == -1) { + printf("Error openning %s\n",str); + printf("%s\n",strerror(errno)); + return; + } + i=write(fd1,bbuf,Len); + if(i<0){ + printf("%s\n",strerror(errno)); + } + else if(i!=Len){ + printf("Error writing %s\n",str); + } + close(fd1); + printf("Write %d bytes\n",i); +} + +void SaveBA(int fd,char *buf,int len) +{ + char str[100]; + int i; + FILE * F; + __s16 * x; + + if (!bbuf){ + printf("Error bufer empty\n"); + return; + } + while(*buf && *buf!=' ') buf++; + while(*buf && *buf==' ') buf++; + i=0; + while(*buf && *buf!=' ' && *buf!=10 && *buf!=13) str[i++]=*buf++; + str[i]=0; + + if(i<1){ + printf("Error bad parametr \n"); + return; + } + F=fopen(str,"w"); + if (F==0) { + printf("Error openning %s\n",str); + printf("%s\n",strerror(errno)); + return; + } + x=(__s16 *)bbuf; + for(i=0;i<(Len/2);i++){ + fprintf(F,"%07d\n",*x); + x++; + } + fclose(F); + printf("Write %d num\n",i); +} + + +void Load(int fd,char *buf,int len,int fl) +{ + int i; + int j; + char str[100]; + char a1[]="7"; + char * a[3]; + + a[0]=NULL; + a[1]=a1; + a[2]=a1; + + buf[len]=' '; + buf[len+1]=0; + buf+=5; + if(*buf!=' ') buf++; + i=1; + arg[0]=NULL; + j=0; + while(*buf){ + if(j>0 && *buf==' '){ + str[j]=0; + j++; + if(i1){ + cnt=i; + } + for (i=0;iGChan-1){ + Cb=0; + GSt=0; + return; + } + sprintf(str,"GET %d,%s,%s\n",GSt,GStart,GLen); + printf("%s",str); + write(fd,str,strlen(str)); +} + +void GetMCB(int fd) +{ + char str[100]; + int i; + FILE * F; + __s16 * x; + + sprintf(str,"%s.dat",GName); + if(GSt==0)F=fopen(str,"w"); + else F=fopen(str,"a"); + if (F==0) { + printf("Error openning %s\n",str); + printf("%s\n",strerror(errno)); + return; + } + x=(__s16 *)bbuf; + for(i=0;i<(Len/2);i++){ + if(i==(Len/2)-1) fprintf(F,"%07d\n",*x); + else fprintf(F,"%07d ",*x); + x++; + } + fclose(F); +// printf("Write %d num\n",i); + GSt++; + if(GSt>GChan-1){ + printf("Write %d chanels\n",GSt); + Cb=0; + GSt=0; + return; + } + sprintf(str,"GET %d,%s,%s\n",GSt,GStart,GLen); + printf("%s",str); + write(fd,str,strlen(str)); +} + +void LCmd(int fd,char* buf,int len) +{ + buf[len]=0; + if((strstr(buf,"!loadb")==buf) || (strstr(buf,"!LOADB")==buf)) + Load(fd,buf,len,1); + else if((strstr(buf,"!load")==buf) || (strstr(buf,"!LOAD")==buf)) + Load(fd,buf,len,0); + else if((strstr(buf,"!savebuf")==buf) || (strstr(buf,"!SAVEBUF")==buf)) + SaveBB(fd,buf,len); + else if((strstr(buf,"!saveasc")==buf) || (strstr(buf,"!SAVEASC")==buf)) + SaveBA(fd,buf,len); + else if((strstr(buf,"!getm")==buf) || (strstr(buf,"!GETM")==buf)) + Get(fd,buf,len); + else if((strstr(buf,"!get")==buf) || (strstr(buf,"!GET")==buf)) + GetM(fd,buf,len); + else printf ("Unknown comand : %s\n",buf); +} + +void CB(int fd) +{ + if(Cb==1) GetCB(fd); + if(Cb==2) GetMCB(fd); +} + + +int main(int argc, char **argv) +{ + int ii; + int oi; + int i; + unsigned char ibuf[500]; + unsigned char obuf[500]; + unsigned char * sdev="/dev/ttyS1"; + int fd=-1; + long len=0; + long dlen=0; + unsigned char *x; + + unsigned char * bb; + + cnt=0; + + if ((fd = open(sdev, O_RDWR | O_NONBLOCK)) == -1) { + printf("Error openning %s\n",sdev); + exit(-1); + } + rs232_setmode(fd,38800,0,0); + + + ii=0; + oi=0; + while(1){ + if(rs232_test(fd,10000)==1){ + oi+=read(fd, obuf+oi, 500-oi); + if(obuf[oi-1]==0xa || obuf[oi-1]==0xd || oi>400){ + obuf[oi]=0; + if(dlen){ + if(oi<=dlen){ + memcpy(bb,obuf,oi); + bb+=oi; + dlen-=oi; + oi=0; + } + else{ + memcpy(bb,obuf,dlen); + bb+=dlen; + x=obuf+dlen; + dlen=0; + i=0; + while(x-obuf=dlen){ + memcpy(bb,x,dlen); + bb+=dlen; + x+=dlen; + dlen=0; + i=0; + while(x-obuf400){ + if(ibuf[0]=='!'){ + LCmd(fd,ibuf,ii); + } + else{ + write(fd, ibuf, ii); + } + ii=0; + } + } + } +} + diff --git a/arch/h8300/generic/tools/tohit/rs232_lt.c b/arch/h8300/generic/tools/tohit/rs232_lt.c new file mode 100644 index 0000000..92d59a2 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/rs232_lt.c @@ -0,0 +1,184 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tohit_fn.h" + +#define DEBUG 0 +#define HAS_GETOPT_LONG 1 + +#define MEM_BUF_LEN 0x40000 + +unsigned char mem_buf[MEM_BUF_LEN]; + +int rs232_loop_test(char *sdev, int baud, int flowc); + +struct termios init_saved_termios; + + +int flowc; + +static void +usage(void) +{ + printf("usage: tohit \n"); + printf(" -d, --sdev name of RS232 device [%s]\n",tohit_sdev); + printf(" -B, --baud RS232 baudrate [%d]\n",tohit_baud); + printf(" -f, --flowc-rts flow control\n"); + printf(" -V, --version show version\n"); + printf(" -h, --help this usage screen\n"); +} + +int main(int argc, char **argv) +{ + /* FILE *F; */ + + static struct option long_opts[] = { + { "sdev", 1, 0, 'd' }, + { "baud", 1, 0, 'B' }, + { "flowc-rts",0, 0, 'f' }, + { "version",0,0, 'V' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0} + }; + int opt; + int ret; + + tohit_baud=9600; + + #ifndef HAS_GETOPT_LONG + while ((opt = getopt(argc, argv, "d:B:fVh")) != EOF) + #else + while ((opt = getopt_long(argc, argv, "d:B:fVh", + &long_opts[0], NULL)) != EOF) + #endif + switch (opt) { + case 'd': + tohit_sdev=optarg; + break; + case 'B': + tohit_baud = strtol(optarg,NULL,0); + break; + case 'f': + flowc=1; + break; + case 'V': + fputs("tohit pre alpha\n", stdout); + exit(0); + case 'h': + default: + usage(); + exit(opt == 'h' ? 0 : 1); + } + + def_shell_mode(); + savetty(); + /*tcgetattr(0, &init_saved_termios);*/ + initscr(); cbreak(); noecho(); + nonl(); intrflush(stdscr, FALSE); keypad(stdscr, TRUE); + nodelay(stdscr, TRUE); + + ret=rs232_loop_test(tohit_sdev,tohit_baud,flowc); + + endwin(); + + return ret; +} + + +int rs232_loop_test(char *sdev, int baud, int flowc) +{ + int fd; + int c; + unsigned char *pout=NULL, *pin=NULL, uc; + int cntout=0,cntin=0, cnt; + int i, test_loop_fl=0; + int errorcnt=0; + int idle; + int stopin=0; + /* int stopout=0; */ + + /* Open RS232 device */ + if ((fd = open(sdev, O_RDWR | O_NONBLOCK)) == -1) { + printf("Error openning %s\n",sdev); + return -1; + } + + /* Set RS232 device mode and speed */ + if(rs232_setmode(fd,baud,0,flowc)<0){ + printf("Error in rs232_setmode\n"); + return -1; + } + +/* + rs232_sendch(int fd,unsigned char c); + rs232_recch(int fd); + rs232_test(int fd,int time); +*/ + + mvprintw(/*y*/2,/*x*/2,"Test of RS-232 transfers"); + + do{ + c=getch(); + idle=(c==ERR); + + switch(c) { + case 't' : + cnt=20000; + if(cnt>MEM_BUF_LEN) cnt=MEM_BUF_LEN; + for(i=0;i>7); + test_loop_fl=1; + cntout=cntin=cnt; + pout=pin=mem_buf; + errorcnt=0; + mvprintw(/*y*/11,/*x*/0,"Loop test : %s", + "Running"); + mvprintw(/*y*/9,/*x*/0," "); + mvprintw(/*y*/10,/*x*/0," "); + break; + case 's' : + stopin=!stopin; + break; + } + + if(test_loop_fl) { + if(cntout) + if(write(fd, pout, 1) == 1) { + pout++; + cntout--; + idle=0; + } + if(cntin&&!stopin) + if(read(fd, &uc, 1) == 1) { + if(*pin!=uc) { + errorcnt++; + mvprintw(/*y*/9,/*x*/0,"Diff : %02X != %02X",uc,*pin); + mvprintw(/*y*/10,/*x*/0,"Errors : %4d",errorcnt); + } + pin++; + cntin--; + idle=0; + } + if(!cntin&&!cntout) { + mvprintw(/*y*/11,/*x*/0,"Loop test : %s", + errorcnt?"Failed ":"Passed "); + test_loop_fl=0; + } + } + + if(idle) { + mvprintw(/*y*/8,/*x*/0,"Cnt Out: %6d In : %6d %s", + cntout,cntin,stopin?"Stop":" "); + } + } while(c!=KEY_F(10)); + + return 0; +} + diff --git a/arch/h8300/generic/tools/tohit/tohit.c b/arch/h8300/generic/tools/tohit/tohit.c new file mode 100644 index 0000000..5f6ba88 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/tohit.c @@ -0,0 +1,229 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tohit_fn.h" + +#define DEBUG 0 +#define HAS_GETOPT_LONG 1 + +int go_flg=0; +int reset_flg=0; +int break_flg=0; +int upload_flg=0; +int blockerase_flg=0; +int regerase_flg=0; + +int command=TOHIT_WRITE; +int blockmode=0; +int erase_block=-1; +unsigned long mem_start=0; +unsigned long mem_length=0; +unsigned long go_addr=0; + +#define MEM_BUF_LEN 0x40000 + +unsigned char mem_buf[MEM_BUF_LEN]; + +static void +usage(void) +{ + printf("usage: tohit \n"); + printf(" -d, --sdev name of RS232 device [%s]\n",tohit_sdev); + printf(" -B, --baud RS232 baudrate [%d]\n",tohit_baud); + printf(" -c, --command numeric command value (B means to use on-chip boot-mode algorithm)\n"); + printf(" -b, --blockmode block size\n"); + printf(" -w, --wait-reply time to wait for reply in ms\n"); + printf(" -e, --erase erase region defined by -s -l\n"); + printf(" -E, --blockerase erase block\n"); + printf(" -s, --start start address of transfer\n"); + printf(" -l, --length length of upload block\n"); + printf(" -g, --go start program from address\n"); + printf(" -r, --reset reset before download\n"); + printf(" -k, --break send communication break character\n"); + printf(" -u, --upload upload memory block [download]\n"); + printf(" -f, --format format of data file [intelhex]\n"); + printf(" -v, --verbose increase verbosity level\n"); + printf(" -V, --version show version\n"); + printf(" -h, --help this usage screen\n"); +} + +int main(int argc, char **argv) +{ + int i; + FILE *F; + + static struct option long_opts[] = { + { "sdev", 1, 0, 'd' }, + { "baud", 1, 0, 'B' }, + { "command",1,0, 'c' }, + { "blockmode",1,0,'b' }, + { "wait-reply",1,0,'w' }, + { "blockerase", 1, 0, 'E' }, + { "erase", 0, 0, 'e' }, + { "start", 1, 0, 's' }, + { "length",1, 0, 'l' }, + { "go", 1, 0, 'g' }, + { "reset", 0, 0, 'r' }, + { "break", 0, 0, 'k' }, + { "upload",0, 0, 'u' }, + { "format",1, 0, 'f' }, + { "version",0,0, 'V' }, + { "verbose",0,0, 'v' }, + { "help", 0, 0, 'h' }, + { 0, 0, 0, 0} + }; + int opt; + + #ifndef HAS_GETOPT_LONG + while ((opt = getopt(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:vVhD:")) != EOF) + #else + while ((opt = getopt_long(argc, argv, "d:B:c:b:w:E:es:l:g:rkuf:vVhD:", + &long_opts[0], NULL)) != EOF) + #endif + switch (opt) { + case 'd': + tohit_sdev=optarg; + break; + case 'B': + tohit_baud = strtol(optarg,NULL,0); + break; + case 'c': + if(optarg[0]=='B'){ + command=TOHIT_WRITEBB; + break; + } + command = strtol(optarg,NULL,0); + break; + case 'b': + blockmode = strtol(optarg,NULL,0); + break; + case 'w': + tohit_waitrep = strtol(optarg,NULL,0)*1000; + break; + case 'E': + erase_block = strtol(optarg,NULL,0); + blockerase_flg=1; + break; + case 'e': + regerase_flg=1; + break; + case 's': + mem_start = strtol(optarg,NULL,0); + break; + case 'l': + mem_length = strtol(optarg,NULL,0); + break; + case 'g': + go_addr = strtol(optarg,NULL,0); + go_flg = 1; + break; + case 'r': + reset_flg = 1; + break; + case 'k': + break_flg = 1; + break; + case 'u': + upload_flg = 1; + break; + case 'f': + break; + case 'v': + tohit_verbosity++; + break; + case 'V': + fputs("tohit pre alpha\n", stdout); + exit(0); + case 'h': + default: + usage(); + exit(opt == 'h' ? 0 : 1); + } + + if(break_flg){ + if(tohit_break()<0){ + fprintf(stderr,"Error in tohit_break\n"); + exit(1); + } + } + + if(reset_flg){ + if(tohit_reset()<0){ + fprintf(stderr,"Error in tohit_reset\n"); + exit(1); + } + } + + if(blockerase_flg){ + if(tohit_blockerase(erase_block)<0){ + fprintf(stderr,"Error in tohit_blockerase\n"); + exit(1); + } + } + + if(regerase_flg){ + if(tohit_regerase(mem_start,mem_length)<0){ + fprintf(stderr,"Error in tohit_regerase\n"); + exit(1); + } + } + + if(!upload_flg&&(optindMEM_BUF_LEN) mem_length=MEM_BUF_LEN; + if(tohit_readmem(command,mem_buf,mem_start,mem_length,blockmode)<0){ + fprintf(stderr,"Error in tohit_readmem\n"); + exit(1); + } + F=fopen(argv[optind++],"w"); + if(F==NULL){ + fprintf(stderr,"Error to open file for writting\n"); + exit(1); + } + if(mem_length!=fwrite(mem_buf,1,mem_length,F)){ + fprintf(stderr,"Error to write mem contents to file\n"); + fclose(F); + exit(1); + } + fclose(F); + } + + if(go_flg){ + if(tohit_goto(go_addr)<0){ + fprintf(stderr,"Error in tohit_goto\n"); + exit(1); + } + } + + return 0; +} diff --git a/arch/h8300/generic/tools/tohit/tohit_fn.c b/arch/h8300/generic/tools/tohit/tohit_fn.c new file mode 100644 index 0000000..1083497 --- /dev/null +++ b/arch/h8300/generic/tools/tohit/tohit_fn.c @@ -0,0 +1,775 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "tohit_fn.h" +#include + +#define DEBUG 0 +#define DEBUG_COUNT 0 + +int tohit_verbosity; + +#define verbose(level, ...) \ + do { if (tohit_verbosity >= (level)) { printf("tohit: " __VA_ARGS__); } } while (0) + +//#define WITHOUT_CFSETSPEED + +#ifdef WITHOUT_CFSETSPEED + + +struct rs232_speed_struct +{ + speed_t value; + speed_t internal; +}; + +static const struct rs232_speed_struct rs232_speeds[] = + { +#ifdef B0 + { 0, B0 }, +#endif +#ifdef B50 + { 50, B50 }, +#endif +#ifdef B75 + { 75, B75 }, +#endif +#ifdef B110 + { 110, B110 }, +#endif +#ifdef B134 + { 134, B134 }, +#endif +#ifdef B150 + { 150, B150 }, +#endif +#ifdef B200 + { 200, B200 }, +#endif +#ifdef B300 + { 300, B300 }, +#endif +#ifdef B600 + { 600, B600 }, +#endif +#ifdef B1200 + { 1200, B1200 }, +#endif +#ifdef B1200 + { 1200, B1200 }, +#endif +#ifdef B1800 + { 1800, B1800 }, +#endif +#ifdef B2400 + { 2400, B2400 }, +#endif +#ifdef B4800 + { 4800, B4800 }, +#endif +#ifdef B9600 + { 9600, B9600 }, +#endif +#ifdef B19200 + { 19200, B19200 }, +#endif +#ifdef B38400 + { 38400, B38400 }, +#endif +#ifdef B57600 + { 57600, B57600 }, +#endif +#ifdef B76800 + { 76800, B76800 }, +#endif +#ifdef B115200 + { 115200, B115200 }, +#endif +#ifdef B153600 + { 153600, B153600 }, +#endif +#ifdef B230400 + { 230400, B230400 }, +#endif +#ifdef B307200 + { 307200, B307200 }, +#endif +#ifdef B460800 + { 460800, B460800 }, +#endif + }; + +/* Set both the input and output baud rates stored in *TERMIOS_P to SPEED. */ +int +rs232_cfsetspeed (struct termios *termios_p, speed_t speed) +{ + size_t cnt; + + for (cnt = 0; cnt < sizeof (rs232_speeds) / sizeof (rs232_speeds[0]); ++cnt) + if (speed == rs232_speeds[cnt].internal) + { + cfsetispeed (termios_p, speed); + cfsetospeed (termios_p, speed); + return 0; + } + else if (speed == rs232_speeds[cnt].value) + { + cfsetispeed (termios_p, rs232_speeds[cnt].internal); + cfsetospeed (termios_p, rs232_speeds[cnt].internal); + return 0; + } + /*__set_errno (EINVAL);*/ + + return -1; +} + +#endif /* WITHOUT_CFSETSPEED */ + +/* Set right mode and speed for RS232 interface */ +/* baud can be either speed in character per second or special Bxxxx constant */ +int rs232_setmode(int fd, int baud, int mode, int flowc) +{ + struct termios ts; + + /* Flush input and output queues. */ + if (tcflush(fd, TCIOFLUSH) != 0) { + fprintf(stderr,"Error in tcflush\n"); + return -1; + } + + /* Fetch the current terminal parameters. */ + if (tcgetattr(fd, &ts) != 0) { + fprintf(stderr,"Error in tcgetattr\n"); + return -1; + } + + /* Sets hardware control flags: */ + /* 8 data bits */ + /* Enable receiver */ + /* Ignore CD (local connection) */ + ts.c_cflag = CS8 | CREAD | CLOCAL; + if(flowc&1){ + /* Use RTS/CTS flow control */ + ts.c_cflag |= CRTSCTS; /* CCTS_OFLOW | CRTS_IFLOW */ + } + ts.c_iflag = 0; + ts.c_oflag = NL0 | CR0 | TAB0 | BS0 | VT0 | FF0; + ts.c_lflag = 0; + + /* set right ispeed and ospeed */ + #ifdef WITHOUT_CFSETSPEED + if(rs232_cfsetspeed(&ts,baud)<0){ + fprintf(stderr,"Error in rs232_cfsetspeed\n"); + return -1; + } + #else /* WITHOUT_CFSETSPEED */ + if(cfsetspeed(&ts,baud)<0){ + fprintf(stderr,"Error in cfsetspeed\n"); + return -1; + } + #endif /* WITHOUT_CFSETSPEED */ + + ts.c_cc[VINTR] = '\0'; + ts.c_cc[VQUIT] = '\0'; + ts.c_cc[VERASE] = '\0'; + ts.c_cc[VKILL] = '\0'; + ts.c_cc[VEOF] = '\0'; + ts.c_cc[VTIME] = '\0'; + ts.c_cc[VMIN] = 1; + ts.c_cc[VSWTC] = '\0'; + ts.c_cc[VSTART] = '\0'; + ts.c_cc[VSTOP] = '\0'; + ts.c_cc[VSUSP] = '\0'; + ts.c_cc[VEOL] = '\0'; + ts.c_cc[VREPRINT] = '\0'; + ts.c_cc[VDISCARD] = '\0'; + ts.c_cc[VWERASE] = '\0'; + ts.c_cc[VLNEXT] = '\0'; + ts.c_cc[VEOL2] = '\0'; + + /* Sets the new terminal parameters. */ + if (tcsetattr(fd, TCSANOW, &ts) != 0) { + fprintf(stderr,"Error in tcsetattr\n"); + return -1; + } + + return 0; +} + +int rs232_sendch(int fd,unsigned char c) +{ + verbose(3, " > sending 0x%02X\n", c); + if(write(fd, &c, 1) != 1){ + fprintf(stderr,"Error in rs232_sendch\n"); + return -1; + } + + return c; +} + +int rs232_recch(int fd) +{ + unsigned char c; + int trycount=10; +/* const char *message; */ + int recieved; + do{ + recieved=read(fd, &c, 1); + + }while(trycount-- && (recieved==-1) && ((errno==EINTR)||(errno==EAGAIN))); + + if (trycount <= 0){ + fprintf(stderr,"Timeout in rs232_recch\n"); + return -1; + } + if (recieved != 1){ + fprintf(stderr,"Error in rs232_recch errno = %d\n", errno); + perror("rs232_recch"); + printf("Read 0x%02X a recieved:%d \n",c,recieved); + return -1; + } + + verbose(3, " < read 0x%02X \n",c); + + return c; +} + +int rs232_test(int fd,int time) +{ + struct timeval tv; + fd_set rfds; + int x; + + if(time>(8*bytes)) & 0xFF); + } +} + +long tohit_reci(int fd, int bytes) +{ + unsigned long x=0; + unsigned long c; + while(bytes--){ + rs232_test(fd,500000); + c=rs232_recch(fd); + if(c==-1) return -1; + x|=c<<(8*bytes); + } + return x; +} + +int tohit_sendichk(int fd,long a, int bytes) +{ + tohit_sendi(fd,a,bytes); + rs232_test(fd,500000); + if(tohit_reci(fd,bytes)!=a) + return -1; + return 0; +} + +/* Synchronize with target */ +int tohit_synchronize(int fd) +{ + int i; + unsigned char c; + verbose(1, "Synchronizing with target\n"); + i=10; + do{ + c=0; + rs232_sendch(fd,c); + + if(rs232_test(fd,500000)>0){ + c=rs232_recch(fd); + if(c==0) break; + } + i--; + #if DEBUG + printf("\n"); + #endif + }while (i>0); + + if (i==0){ + printf("Error timeout\n"); + return -3; + } + + /* Run 55=>AA synchronization */ + #if DEBUG + printf("\n"); + #endif + rs232_sendch(fd,0x55); + rs232_test(fd,500000); + c=rs232_recch(fd); + #if DEBUG + printf("\n"); + #endif + if (c!=0xAA) { + printf("Error in AA reply\n"); + return -4; + } + return 0; +} + +const char *cmd_name[] = { + [1+TOHIT_WRITEBB] = "WRITEBB", + [1+TOHIT_WRITE] = "WRITE", + [1+TOHIT_WRITEFL] = "WRITEFL", + [1+TOHIT_READ] = "READ", + [1+TOHIT_ERASEBL] = "ERASEBL", + [1+TOHIT_ERASEREG] = "ERASEREG", + [1+TOHIT_GOTO] = "GOTO", + [1+TOHIT_RESET] = "RESET" +}; + +const char *boot_err[] = { + [EBOOT_PROG_FAILURE] = "Flash programming failure (damaged flash?)", + [EBOOT_EXT_FLASH_VERIFY] = "External flash verify failed", + [EBOOT_FLASH_VERIFY] = "Flash verify failed", + [EBOOT_ROW_NOT_ERASED] = "Flash row was not erased", + [EBOOT_NO_FWE] = "Flash write not enabled (FWE pin)", + [EBOOT_ROW_BEGIN] = "Programming starts in the middle of a row", + [EBOOT_BLOCKADDR] = "Wrong flash block start/end address", + [EBOOT_BLNUM_HIGH] = "Flash block number to high", + [EBOOT_BLNUM_LOW] = "Flash block number to low (negative)", + [EBOOT_FLASH_ERROR] = "Flash error indicated", + [EBOOT_ERASE_FAILURE] = "Flash erase failure" +}; + +#define ARR_SIZE(x) (sizeof(x)/sizeof(x[0])) + + +int tohit_open4cmd(char *sdev, int baud, int cmd) +{ + int fd; + int c; + + /* Open RS232 device */ + if ((fd = open(sdev, O_RDWR | O_NONBLOCK)) == -1) { + printf("Error openning %s\n",sdev); + return -1; + } + + /* Set RS232 device mode and speed */ + if(rs232_setmode(fd,baud,0,0)<0){ + printf("Error in rs232_setmode\n"); + return -1; + } + + /* Synchronize with target */ + if(tohit_synchronize(fd)<0){ + printf("Error in tohit_synchronize\n"); + return -1; + } + + if(cmd!=-1){ + /* send cmd */ + verbose(1, "Sending command 0x%02X (%s)\n", cmd, cmd_name[1+cmd]); + c=cmd | ((cmd ^ 7) << 3); + rs232_sendch(fd,c); + rs232_test(fd,500000); + if ((c | 0x80)!=rs232_recch(fd)) { + printf("Error in cmd reply\n"); + return -4; + } + } + return fd; +} + +int tohit_cmdrepchk(int fd) +{ + int res; + verbose(2, "Waiting for end reply\n"); + rs232_test(fd,2000000); + res=rs232_recch(fd); + if(res<0){ + printf("Error no end reply\n"); + return -6; + } + if (res!=0xAA && res!=0x5a) { + int err = (int)(signed char)res; + printf("Error in end reply - received 0x%02X == %d (%s)\n", + res, err, + (err < 0 && -err < ARR_SIZE(boot_err)) ? boot_err[-err] : NULL); + return -6; + } + return 0; +} + +#define BLEN 0x40000 + +char *tohit_sdev="/dev/ttyS1"; +int tohit_baud=4800; + + +int tohit_goto(unsigned long adr) +{ + int fd; + if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, TOHIT_GOTO))<0) + { + printf("Error in tohit_open4cmd\n"); + close(fd); + return -1; + } + + verbose(2, "Sending go address 0x%08lx\n", adr); + if (tohit_sendichk(fd,adr,4)<0) { + printf("Error in goto adr send and reply\n"); + close(fd); + return -4; + } + + close(fd); + return 1; +} + +void print_flash_error(int err) +{ + printf("Flash error: %s\n", + (err >= 0 && err < ARR_SIZE(boot_err)) ? boot_err[err] : "unknown error"); +} + +int tohit_writemem(int cmd, const unsigned char *buf, + unsigned long adr, unsigned long size, int blockmode) +{ + int fd; + int count; + unsigned long i; + int j, k; + int c; + unsigned char rbuf[128]; + + if((blockmode==1)||(blockmode>128)) blockmode=128; + + if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, cmd))<0) + { + printf("Error in tohit_open4cmd\n"); + close(fd); + return -1; + } + if(cmd==TOHIT_WRITEBB){ + verbose(2, "Sending size 0x%04lx\n", size); + if (tohit_sendichk(fd,size,2)<0) { + printf("Error in start adr send and reply\n"); + close(fd); + return -4; + } + }else{ + verbose(2, "Sending address 0x%08lx\n", adr); + if (tohit_sendichk(fd,adr,4)<0) { + printf("Error in start adr send and reply\n"); + close(fd); + return -4; + } + verbose(2, "Sending size 0x%08lx\n", size); + if (tohit_sendichk(fd,size,4)<0) { + printf("Error in size send and reply\n"); + close(fd); + return -4; + } + } + + #if DEBUG + printf("Data send\n"); + #endif /* DEBUG */ + if(!blockmode){ + verbose(2, "Writing in single byte mode\n"); + for(i=0;i(size-i)) count=size-i; + + verbose(3, " writing %d bytes\n",count); + + j=0; + while(jcount)){ + perror("write"); + printf("Error in blk write (%d,%d)\n",j,k); + close(fd); + return -2; + } + } + + verbose(3, " reading %d bytes\n",count); + j=0; + do{ + if ((i+j) % FLASH_ROW == FLASH_ROW - 1) { + verbose(3, " waiting for flashing a row\n"); + rs232_test(fd,2/*sec*/*1000000); + } else { + rs232_test(fd,500000); + } + k=read(fd, rbuf+j, count-j); + if(k>=0) { + j+=k; + } else { + perror("read"); + printf("Error in blk write - no reply (%d,%d)\n",j,k); + close(fd); + return -2; + } + }while(jblockmode) c=blockmode; + else c=size-i; + for(k=0;k