changeset 49:54392d1ea474

loadtools: first beginnings for the baud rate switching logic
author Michael Spacefalcon <msokolov@ivan.Harhan.ORG>
date Sun, 23 Jun 2013 02:53:16 +0000
parents 38664e0b7c32
children f1df95eed62c
files loadtools/baudrate.h loadtools/romload.c loadtools/sercomm.c
diffstat 3 files changed, 35 insertions(+), 5 deletions(-) [+]
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/loadtools/baudrate.h	Sun Jun 23 02:53:16 2013 +0000
@@ -0,0 +1,7 @@
+/* this header file defines the data structure for baud rate machinations */
+
+struct baudrate {
+	char	*name;
+	speed_t	termios_code;
+	int	bootrom_code;
+};
--- a/loadtools/romload.c	Sun Jun 23 01:13:03 2013 +0000
+++ b/loadtools/romload.c	Sun Jun 23 02:53:16 2013 +0000
@@ -13,14 +13,17 @@
 #include <strings.h>
 #include <termios.h>
 #include <unistd.h>
+#include "baudrate.h"
 #include "srecreader.h"
 
 extern int errno;
 
 extern char *target_ttydev;
 extern int target_fd;
+extern struct baudrate baud_rate_table[];
 
 struct srecreader iramimage;
+struct baudrate *romload_baud_rate = baud_rate_table;	/* 1st entry default */
 
 static int beacon_interval = 13;	/* in milliseconds */
 
@@ -153,6 +156,7 @@
 
 	usleep(SERIAL_FLUSH_DELAY * 1000);
 	tcflush(target_fd, TCIFLUSH);
+	param_cmd[2] = romload_baud_rate->bootrom_code;
 	write(target_fd, param_cmd, sizeof param_cmd);
 	resp = expect_response(INTERMEDIATE_TIMEOUT);
 	if (resp != 'p') {
@@ -168,8 +172,9 @@
 				resp);
 		exit(1);
 	}
-	printf("<p command successful, switching to 115200 baud\n");
-	switch_baud_rate(B115200);
+	printf("<p command successful, switching to %s baud\n",
+		romload_baud_rate->name);
+	switch_baud_rate(romload_baud_rate);
 	usleep(SERIAL_FLUSH_DELAY * 1000);
 	tcflush(target_fd, TCIFLUSH);
 
--- a/loadtools/sercomm.c	Sun Jun 23 01:13:03 2013 +0000
+++ b/loadtools/sercomm.c	Sun Jun 23 02:53:16 2013 +0000
@@ -10,11 +10,27 @@
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
+#include "baudrate.h"
 
 char *target_ttydev;
 int target_fd;
 struct termios target_termios;
 
+struct baudrate baud_rate_table[] = {
+	/* the first listed rate will be our default */
+	{"115200",	B115200,	0},
+	{"57600",	B57600,		1},
+	{"38400",	B38400,		2},
+	{"19200",	B19200,		4},
+	/* non-standard high baud rates "remapped" by CP2102 usb2serial IC */
+	{"812500",	B921600,	-1},
+	{"406250",	B460800,	-1},
+	{"203125",	B230400,	-1},
+	/* table search terminator */
+	{NULL,		B0,		-1},
+};
+struct baudrate *current_baud_rate;
+
 open_target_serial()
 {
 	target_fd = open(target_ttydev, O_RDWR|O_NONBLOCK);
@@ -38,12 +54,14 @@
 	return 0;
 }
 
-switch_baud_rate(code)
+switch_baud_rate(br)
+	struct baudrate *br;
 {
-	cfsetispeed(&target_termios, code);
-	cfsetospeed(&target_termios, code);
+	cfsetispeed(&target_termios, br->termios_code);
+	cfsetospeed(&target_termios, br->termios_code);
 	if (tcsetattr(target_fd, TCSAFLUSH, &target_termios) < 0) {
 		perror("tcsetattr to switch baud rate");
 		exit(1);
 	}
+	current_baud_rate = br;
 }