]> rtime.felk.cvut.cz Git - sysless.git/commitdiff
Included TOHIT H8300 application and flash loader.
authorMichal Sojka <sojkam1@fel.cvut.cz>
Wed, 21 Jul 2010 13:54:30 +0000 (15:54 +0200)
committerPavel Pisa <pisa@cmp.felk.cvut.cz>
Tue, 16 Nov 2010 15:54:52 +0000 (16:54 +0100)
The app/ directory was mess. This is an attempt to make the situation
better.

13 files changed:
arch/h8300/generic/Makefile.omk
arch/h8300/generic/tools/Makefile [new file with mode: 0644]
arch/h8300/generic/tools/Makefile.omk [new file with mode: 0644]
arch/h8300/generic/tools/tohit/Makefile [new file with mode: 0644]
arch/h8300/generic/tools/tohit/Makefile.omk [new file with mode: 0644]
arch/h8300/generic/tools/tohit/Makefile.std [new file with mode: 0644]
arch/h8300/generic/tools/tohit/README [new file with mode: 0644]
arch/h8300/generic/tools/tohit/cfsetspeed.c [new file with mode: 0644]
arch/h8300/generic/tools/tohit/hiterm.c [new file with mode: 0644]
arch/h8300/generic/tools/tohit/rs232_lt.c [new file with mode: 0644]
arch/h8300/generic/tools/tohit/tohit.c [new file with mode: 0644]
arch/h8300/generic/tools/tohit/tohit_fn.c [new file with mode: 0644]
arch/h8300/generic/tools/tohit/tohit_fn.h [new file with mode: 0644]

index 81bac758eb9485b822336a77522e5eb56cb0c4e6..6a31eafe10bef75e8a9f9a6f3fa175529e4a4565 100644 (file)
@@ -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 (file)
index 0000000..76b56fd
--- /dev/null
@@ -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 (file)
index 0000000..1739569
--- /dev/null
@@ -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 (file)
index 0000000..76b56fd
--- /dev/null
@@ -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 (file)
index 0000000..f1d02c3
--- /dev/null
@@ -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 (file)
index 0000000..7546cbb
--- /dev/null
@@ -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 (file)
index 0000000..c3f39ad
--- /dev/null
@@ -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 (file)
index 0000000..d183f45
--- /dev/null
@@ -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 <termios.h>
+#include <errno.h>
+#include <stddef.h>
+
+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 (file)
index 0000000..26f8823
--- /dev/null
@@ -0,0 +1,450 @@
+#include <stdio.h>
+#include <string.h>
+#include <termios.h>
+#include <stdlib.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <asm/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <errno.h>
+#include <sys/stat.h>
+
+
+#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(i<cnt) free(arg[i]);
+        arg[i]=(char *)malloc(j);
+        do {
+         j--;
+         arg[i][j]=str[j];
+        }while(j);
+        i++;
+        if(i==10) i--;
+       }
+       else{
+        if(*buf!=' ' && *buf!=10 && *buf!=13) str[j++]=*buf;
+        if (j==100) j--;
+       } 
+       buf++;
+ }
+ if(i>1){
+  cnt=i;
+ }
+ for (i=0;i<cnt;i++){
+  printf("  A%d : %s\n",i,arg[i]);
+ }
+ if(cnt){
+  if(fl==0){
+        write(fd,"LOAD\n",5);
+        usleep(100000);
+        read(fd,str,100);
+       } 
+       tohit(cnt,arg);
+       tohit(3,a);
+       usleep(200000);
+       read(fd,str,100);
+ }
+ else{
+  printf("Bad Parametr\n");
+ }
+}
+
+char GName[100];
+char GStart[20];
+char GLen[20];
+int GChan;
+int GSt=0;
+int Cb=0;
+
+void Get(int fd,char *buf,int len)
+{
+ char str[100];
+ int i;
+ while(*buf && *buf!=' ') buf++;
+ while(*buf && *buf==' ') buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) str[i++]=*buf++;
+ str[i]=0;
+ GChan=strtol(str,NULL,10);
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GStart[i++]=*buf++;
+ GStart[i]=0;
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GLen[i++]=*buf++;
+ GLen[i]=0;
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GName[i++]=*buf++;
+ GName[i]=0;
+ if(i<1){
+  printf("Error bad parametr \n");
+       return;
+ }
+ Cb=1;
+ GSt=0;
+ sprintf(str,"GET 0,%s,%s\n",GStart,GLen);
+ printf("%s",str);
+ write(fd,str,strlen(str));
+}
+
+void GetM(int fd,char *buf,int len)
+{
+ char str[100];
+ int i;
+ while(*buf && *buf!=' ') buf++;
+ while(*buf && *buf==' ') buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) str[i++]=*buf++;
+ str[i]=0;
+ GChan=strtol(str,NULL,10);
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GStart[i++]=*buf++;
+ GStart[i]=0;
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GLen[i++]=*buf++;
+ GLen[i]=0;
+ while(*buf && (*buf==' ' || *buf==',')) buf++;
+
+ i=0;
+ while(*buf && *buf!=',' && *buf!=' ' && *buf!=10 && *buf!=13) GName[i++]=*buf++;
+ GName[i]=0;
+ if(i<1){
+  printf("Error bad parametr \n");
+       return;
+ }
+ Cb=2;
+ GSt=0;
+ sprintf(str,"GET 0,%s,%s\n",GStart,GLen);
+ printf("%s",str);
+ write(fd,str,strlen(str));
+}
+
+void GetCB(int fd)
+{
+ char str[100];
+ int i;
+ FILE * F;
+ __s16 * x;
+ sprintf(str,"%s.%03d",GName,GSt);
+
+ 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);
+ GSt++;
+ if(GSt>GChan-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<oi){
+                  obuf[i++]=*x++;
+                       }
+                       oi=i;
+                       obuf[oi]=0;
+                }
+                if(!dlen) CB(fd);
+               }
+               else {
+          x=strstr(obuf,"061:");
+          if(x){
+                       x+=4;
+                       sscanf(x,"%ld",&Len);
+                       printf("len=%ld\n",Len);
+                       len=Len;
+                       if(bbuf) free(bbuf);
+                       bbuf=(unsigned char *)malloc(len);
+                       bb=bbuf;
+                       while(*x!=0xa && *x!=0xd && *x) x++;
+                       if(*x==0xd || *x==0xa) x++;
+                       i=0;
+                       while(x-obuf<oi){
+                  obuf[i++]=*x++;
+                       }
+                       oi=i;
+                       obuf[oi]=0;
+                }
+          x=strstr(obuf,"062:");
+                if(x){
+                 dlen=len;
+                       len=0;
+                       x+=4;
+                       if(oi-(x-obuf)>=dlen){
+                        memcpy(bb,x,dlen);
+                        bb+=dlen;
+                  x+=dlen;
+                        dlen=0;
+                        i=0;
+                        while(x-obuf<oi){
+                       obuf[i++]=*x++;
+                        }
+                        oi=i;
+                        obuf[oi]=0;
+                       }
+                       else{
+                        memcpy(bb,x,oi-(x-obuf));
+                        bb+=oi-(x-obuf);
+                  dlen-=oi-(x-obuf);
+                  oi=0;
+                       }
+                       if(!dlen) CB(fd);
+                }
+               } 
+               write(0, obuf, oi);
+               oi=0;
+        }      
+       }
+  if(rs232_test(1,10000)==1){
+        ii+=read(1, ibuf+ii, 500-ii);
+        if(ibuf[ii-1]==0xa || ibuf[ii-1]==0xd || ii>400){
+         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 (file)
index 0000000..92d59a2
--- /dev/null
@@ -0,0 +1,184 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <termios.h>
+#include <ncurses.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <getopt.h>
+#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 <parameters> <send_file>\n");
+  printf("  -d, --sdev <name>        name of RS232 device [%s]\n",tohit_sdev);
+  printf("  -B, --baud <num>         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<cnt;i++)
+          mem_buf[i]=i^(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 (file)
index 0000000..5f6ba88
--- /dev/null
@@ -0,0 +1,229 @@
+#include <stdlib.h>
+#include <stdio.h>
+#include <string.h>
+#include <termios.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <getopt.h>
+#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 <parameters> <send_file>\n");
+  printf("  -d, --sdev <name>        name of RS232 device [%s]\n",tohit_sdev);
+  printf("  -B, --baud <num>         RS232 baudrate [%d]\n",tohit_baud);
+  printf("  -c, --command <num>      numeric command value (B means to use on-chip boot-mode algorithm)\n");
+  printf("  -b, --blockmode <num>    block size\n");
+  printf("  -w, --wait-reply <num>   time to wait for reply in ms\n");
+  printf("  -e, --erase                     erase region defined by -s -l\n");
+  printf("  -E, --blockerase <block> erase block\n");
+  printf("  -s, --start  <addr>      start address of transfer\n");
+  printf("  -l, --length <num>       length of upload block\n");
+  printf("  -g, --go     <addr>      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>    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&&(optind<argc)){
+    F=fopen(argv[optind++],"r");
+    if(F==NULL){
+      fprintf(stderr,"Error to open file for reading\n");
+      exit(1); 
+    }
+    mem_length=fread(mem_buf,1,MEM_BUF_LEN,F);
+    if(mem_length<0){
+      fprintf(stderr,"Error to read mem contents\n");
+      fclose(F);
+      exit(1); 
+    }
+    fclose(F);
+  
+    if(blockmode){
+      for(i=1;(i<blockmode)&&(i<128);i<<=1);
+      blockmode=i;
+    }
+    if(tohit_writemem(command,mem_buf,mem_start,mem_length,blockmode)<0){
+      fprintf(stderr,"Error in tohit_writemem\n");
+      exit(1); 
+    }
+
+  }else 
+  if(upload_flg&&(optind<argc)){
+    if(command==0) command=TOHIT_READ;
+    if(mem_length>MEM_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 (file)
index 0000000..1083497
--- /dev/null
@@ -0,0 +1,775 @@
+#include <stdio.h>
+#include <string.h>
+#include <errno.h>
+#include <termios.h>
+#include <sys/time.h>
+#include <sys/types.h>
+#include <sys/ioctl.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include "tohit_fn.h"
+#include <boot_fn.h>
+
+#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<tohit_waitrep)
+    time=tohit_waitrep;
+  tv.tv_sec = 0;
+  tv.tv_usec = time;
+  FD_ZERO(&rfds);
+  FD_SET(fd, &rfds);
+  x=select(fd + 1, &rfds, NULL, NULL, &tv);
+ #if DEBUG
+  printf("rs232_test %d ",x);
+ #endif
+  return x;
+}
+
+void tohit_sendi(int fd,long a, int bytes)
+{
+  while(bytes--){
+    rs232_sendch(fd,(a>>(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++){
+      rs232_sendch(fd,buf[i]);
+      rs232_test(fd,500000);
+      c=rs232_recch(fd);
+      if (c!=buf[i]) {
+       if (i % FLASH_ROW == FLASH_ROW-1) {
+         print_flash_error(rbuf[i]);
+       } else {
+         printf("Error in data reply (expected 0x%02x, received 0x%02x)\n", buf[i], c);
+       }
+        close(fd);
+       return -5;
+      }        
+      if((i%128)==0){
+       printf(".");
+       fflush(stdout);
+      }
+     #if DEBUG
+      printf("\n");
+     #endif
+    }
+  }else{       /*TOHIT_WRITEFL*/
+    i=0;
+    verbose(2, "Writing in block mode (block size=%d)\n", blockmode);
+    count=blockmode-(adr&(blockmode-1));
+    while(i<size){
+      if(count>(size-i)) count=size-i;
+      
+      verbose(3, " writing %d bytes\n",count);
+
+      j=0;
+      while(j<count){
+        k=write(fd, buf+i+j, count-j);
+       j+=k;
+       if((k<=0)||(j>count)){
+         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(j<count);
+      if (tohit_verbosity < 3) {
+             printf(".");
+             fflush(stdout);
+      }
+      for(j=0;j<count;j++){
+       if (rbuf[j]!=buf[i+j]) {
+         if (i+j % FLASH_ROW == FLASH_ROW-1) {
+           print_flash_error(rbuf[j]);
+         } else {
+           printf("Error in data reply at position %d\n", j);
+
+           printf("Sent:    ");
+           for (k=j; k < count && k < j+8; k++)
+             printf(" 0x%02x", rbuf[j+k]);
+           printf("\nReceived:");
+           for (k=j; k < count && k < j+8; k++)
+             printf(" 0x%02x", buf[i+j+k]);
+           printf("\n");
+         }
+         close(fd);
+         return -5;
+       }       
+      }
+      i+=count;
+      count=blockmode;
+    }
+    printf("\n");
+  }    
+
+  if(tohit_cmdrepchk(fd)<0){
+    printf("Error no end reply\n");
+    close(fd);
+    return -4;
+  }    
+  close(fd);
+  return 1;
+}
+
+int tohit_readmem(int cmd, unsigned char *buf, 
+                 unsigned long adr, unsigned long size, int blockmode)
+{
+  int fd;
+  unsigned long i;
+  int c,k,ret;
+  
+  if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, cmd))<0)
+  {
+    printf("Error in tohit_open4cmd\n");
+    close(fd);
+    return -1;
+  }
+  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(!blockmode){
+    /* Read memory by single byte */
+    for(i=0;i<size;i++){
+      rs232_test(fd,500000);
+      if((c=rs232_recch(fd))<0){
+        printf("Error in receive char\n");
+        close(fd);
+        return -4;
+      }
+      buf[i]=c;
+      rs232_sendch(fd,c);
+      if((i%128)==0){
+        printf(".");
+        fflush(stdout);
+      }
+     #if DEBUG
+      printf("\n");
+     #endif
+    }
+    printf("\n");
+
+    if(tohit_cmdrepchk(fd)<0){
+      printf("Error no end reply\n");
+      close(fd);
+      return -4;
+    }  
+  }else{
+    /* Read memory by blocks */
+    for(i=0;i<size;){
+      if(size-i>blockmode) c=blockmode;
+      else c=size-i;
+      for(k=0;k<c;k++)
+        while(rs232_sendch(fd,0)<0);
+      for(k=0;k<c;){
+        rs232_test(fd,500000);
+        ret=read(fd,buf+i+k,c-k);
+        if(ret<0){
+          printf("Error in block receive\n");
+          close(fd);
+          return -4;
+        }
+        k+=ret;
+      }
+      printf(".");
+      fflush(stdout);
+      i+=c;
+    }
+    if(rs232_recch(fd)!=0xff){
+      printf("Error no end reply\n");
+      close(fd);
+      return -4;
+    }  
+  }
+  close(fd);
+  return 1;
+}
+
+int tohit_blockerase(int block)
+{
+  int fd;
+  
+  if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, TOHIT_ERASEBL))<0)
+  {
+    printf("Error in tohit_open4cmd\n");
+    close(fd);
+    return -1;
+  }
+
+  verbose(2, "Sending block number: %d\n", block);
+  rs232_sendch(fd,block);
+  rs232_test(fd,2000000);
+  rs232_test(fd,2000000);
+
+  if(tohit_cmdrepchk(fd)<0){
+    printf("Error no end reply\n");
+    close(fd);
+    return -4;
+  }    
+  close(fd);
+  return 1;
+}
+
+int tohit_regerase(unsigned long adr, unsigned long size)
+{
+  int fd;
+  
+  if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, TOHIT_ERASEREG))<0)
+  {
+    printf("Error in tohit_open4cmd\n");
+    close(fd);
+    return -1;
+  }
+  verbose(2, "Sending start 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;
+  }    
+
+  printf("\n");
+
+  if(tohit_cmdrepchk(fd)<0){
+    printf("Error no end reply\n");
+    close(fd);
+    return -4;
+  }    
+  close(fd);
+  return 1;
+}
+
+int tohit_reset(void)
+{
+  int fd;
+  if((fd=tohit_open4cmd(tohit_sdev, tohit_baud, TOHIT_RESET))<0)
+  {
+    printf("Error in tohit_open4cmd\n");
+    close(fd);
+    return -1;
+  }
+  close(fd);
+  return 1;
+}
+
+int tohit_break(void)
+{
+  int fd;
+  int i;
+  char c=0;
+
+  /* Open RS232 device */
+  if ((fd = open(tohit_sdev, O_RDWR | O_NONBLOCK)) == -1) {
+    printf("Error openning %s\n",tohit_sdev);
+    return -1;
+  }
+
+  if(rs232_setmode(fd,tohit_baud/2,0,0)<0)
+  {
+    close(fd);
+    return -1;
+  }
+  
+  verbose(1, "Sending break chars \n");
+
+  for(i=100;i--;)
+    write(fd,&c,1);
+  
+  close(fd);
+  return 1;
+}
diff --git a/arch/h8300/generic/tools/tohit/tohit_fn.h b/arch/h8300/generic/tools/tohit/tohit_fn.h
new file mode 100644 (file)
index 0000000..6b6902f
--- /dev/null
@@ -0,0 +1,66 @@
+#ifndef _TOHIT_FN_H
+#define _TOHIT_FN_H
+
+/* cmd -1 write memory using ROM boot loader */
+/* cmd 0 write memory */
+/* cmd 1 write memory */
+/* cmd 2 read memory */
+/* cmd 3 erase flash block */
+/* cmd 4 erase region*/
+/* cmd 6 call address */
+/* cmd 7 reset */
+
+#define TOHIT_WRITEBB   (-1)
+#define TOHIT_WRITE       0
+#define TOHIT_WRITEFL    1
+#define TOHIT_READ       2
+#define TOHIT_ERASEBL    3
+#define TOHIT_ERASEREG   4
+#define TOHIT_GOTO       6
+#define TOHIT_RESET      7
+
+int rs232_setmode(int fd, int baud, int mode, int flowc);
+
+int rs232_sendch(int fd,unsigned char c);
+
+int rs232_recch(int fd);
+
+int rs232_test(int fd,int time);
+
+void tohit_sendi(int fd,long a, int bytes);
+
+long tohit_reci(int fd, int bytes);
+
+int tohit_sendichk(int fd,long a, int bytes);
+
+int tohit_synchronize(int fd);
+
+int tohit_open4cmd(char *sdev, int baud, int cmd);
+
+int tohit_cmdrepchk(int fd);
+
+int tohit_goto(unsigned long adr);
+
+int tohit_writemem(int cmd, const unsigned char *buf, 
+                  unsigned long adr, unsigned long size, int blockmode);
+
+int tohit_readmem(int cmd, unsigned char *buf, 
+                 unsigned long adr, unsigned long size, int blockmode);
+
+int tohit_blockerase(int block);
+
+int tohit_regerase(unsigned long adr, unsigned long size);
+
+
+char *tohit_sdev;
+int tohit_baud;
+long tohit_waitrep;
+extern int tohit_verbosity;
+
+int tohit_reset(void);
+
+int tohit_break(void);
+
+#endif /* _TOHIT_FN_H */
+
+