From af2e2299a2e4f4aca56de844f0c5011435672b92 Mon Sep 17 00:00:00 2001
From: hackbard <hackbard@staubsauger.localdomain>
Date: Wed, 8 Aug 2007 21:01:42 +0200
Subject: [PATCH] fwdump working now, adapted fwbc fw (reading/transmitting
 flash should work now!)

---
 betty/Makefile |   2 +-
 betty/fwbc.c   |  36 ++++++++++++---
 betty/fwdump.c | 117 ++++++++++++++++++++++++++++++++++++++++++++++++-
 3 files changed, 148 insertions(+), 7 deletions(-)

diff --git a/betty/Makefile b/betty/Makefile
index a1e44da..d9f49e7 100644
--- a/betty/Makefile
+++ b/betty/Makefile
@@ -5,7 +5,7 @@ ARMCC = /scratch/arm-elf/bin/arm-elf-gcc
 ARMCFLAGS = -Wall -mcpu=arm7tdmi-s 
 ARMOBJCOPY = /scratch/arm-elf/bin/arm-elf-objcopy
 
-HOSTOBJECTS = lpcload
+HOSTOBJECTS = lpcload fwdump
 
 ARMOBJECTS = fwbc.hex
 
diff --git a/betty/fwbc.c b/betty/fwbc.c
index 9682ee8..1694055 100644
--- a/betty/fwbc.c
+++ b/betty/fwbc.c
@@ -27,11 +27,16 @@
 #define MMAP_RAM		0x02
 #define MMAP_EXT		0x03
 
+/* band 0/2 addr */
+#define BANK0			0x80000000
+#define BANK2			0x82000000
+
 /*
  * type definitions
  */
 
 typedef unsigned char u8;
+typedef unsigned short u16;
 typedef unsigned int u32;
 
 /*
@@ -49,29 +54,50 @@ void uart0_send_char(char send);
 
 int main(void) {
 
+	/* variables */
+
+	u16 *flash;
+	u32 i;
+	u8 start;
+
 	/* memory mapping of interrupt vectors to static ram */
 
 	//mmap_init(MMAP_RAM);
 
 	/* uart initialization */
 
-#ifdef USE_FUNCTIONS
 	//uart0_init();
-#else
+
 	PINSEL0=0x05;			// pin select -> tx, rx
 	UART0_FCR=0x07;			// enable fifo
 	UART0_LCR=0x83;			// set dlab + word length
 	UART0_DLL=0x10;			// br: 9600 @ 10/4 mhz
 	UART0_DLM=0x00;
 	UART0_LCR=0x03;			// unset dlab
-#endif
 
 	/* external memory init */
 
-	while(1) {
+	//BCFG0=;
+	//BCFG2=;
+	flash=(u16 *)BANK0;
+
+	/* wait for fwdump to send transmit start character */
+	start=0;
+	while(start!=0x23) {
+		while(!(UART0_LSR&(1<<0)))
+			continue;
+		start=UART0_RBR;
+	}
+
+	/* transmit 1 mb of data */
+	for(i=0;i<524288;i++) {
+		while(!(UART0_LSR&(1<<5)))
+			continue;
+		UART0_THR=((*flash&0xff00)>>8);
 		while(!(UART0_LSR&(1<<5)))
 			continue;
-		UART0_THR=0x23;
+		UART0_THR=(*flash&0xff);
+		flash++;
 	}
 
 	return 0;
diff --git a/betty/fwdump.c b/betty/fwdump.c
index 3769508..a3c7ed4 100644
--- a/betty/fwdump.c
+++ b/betty/fwdump.c
@@ -6,10 +6,125 @@
  *
  */
 
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <termios.h>
+
+#define BUFSIZE		16
+
+int open_serial_port(char *sdev) {
+
+	struct termios term;
+	int fd;
+
+	/* open serial device */
+
+	fd=open(sdev,O_RDWR);
+	if(fd<0) {
+		perror("tts open");
+		return fd;
+	}
+
+	/* configure the serial device */
+
+	tcgetattr(fd,&term);
+
+	// input/output baudrate
+
+	cfsetispeed(&term,B9600);
+	cfsetospeed(&term,B9600);
+
+	// control options -> 8n1
+
+	term.c_cflag&=~PARENB;	// no parity
+	term.c_cflag&=~CSTOPB;	// only 1 stop bit
+	term.c_cflag&=~CSIZE;	// no bit mask for data bits
+	term.c_cflag|=CS8;	// 8 data bits
+
+	// line options -> raw input
+	
+	term.c_lflag&=~(ICANON|ECHO|ECHOE|ISIG);
+
+	// input options -> enable flow control
+	
+	term.c_iflag&=~(IXON|IXOFF|IXANY|INLCR|ICRNL);
+	
+	// output options
+
+	term.c_oflag=0;
+
+	// more control options -> timeout / flow control
+	
+	term.c_cc[VMIN]=0;
+	term.c_cc[VTIME]=40;	// 4 second timeout
+	term.c_cc[VSTART]=0x11;
+	term.c_cc[VSTOP]=0x13;
+
+	tcsetattr(fd,TCSANOW,&term);
+
+	return fd;
+}
+
+int open_dump_file(char *file) {
+
+	int fd;
+
+	/* open dump file */
+
+	fd=open(file,O_WRONLY|O_CREAT);
+
+	if(fd<0)
+		perror("fw open");
+
+	return fd;
+}
+
 int main(int argc,char **argv) {
 
+	int sfd,dfd;
+	unsigned char buf[BUFSIZE];
+	int cnt,size,ret;
+
+	if(argc!=3) {
+		printf("usage: %s <serial port> <dump file>\n",argv[0]);
+		return -1;
+	}
+
+	sfd=open_serial_port(argv[1]);
 
+	dfd=open_dump_file(argv[2]);
+
+	if((sfd<0)|(dfd<0))
+		return -1;
+
+	/* send start byte */
+	buf[0]=0x23;
+	ret=write(sfd,buf,1);
+	if(ret!=1) {
+		perror("write start byte");
+		return ret;
+	}
+
+	/* receive flash content */
+	while(ret) {
+		ret=read(sfd,buf,BUFSIZE);
+		size=ret;
+		cnt=0;
+		while(size) {
+			ret=write(dfd,buf+cnt,size-cnt);
+			if(ret<0) {
+				perror("write to dump file");
+				return ret;
+			}
+			size-=ret;
+			cnt+=ret;
+		}
+	}
 
 	return 0;
 }
-
-- 
2.39.5