ao-bringup: Add turnon_telemega script
authorKeith Packard <keithp@keithp.com>
Tue, 10 Dec 2013 07:12:40 +0000 (23:12 -0800)
committerKeith Packard <keithp@keithp.com>
Tue, 10 Dec 2013 07:13:26 +0000 (23:13 -0800)
And a few helper programs

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-bringup/cal-accel [new file with mode: 0755]
ao-bringup/cal-freq [new file with mode: 0755]
ao-bringup/get-radio-cal [new file with mode: 0755]
ao-bringup/turnon_telemega [new file with mode: 0755]

diff --git a/ao-bringup/cal-accel b/ao-bringup/cal-accel
new file mode 100755 (executable)
index 0000000..96e51e6
--- /dev/null
@@ -0,0 +1,120 @@
+#!/usr/bin/nickle
+
+import File;
+
+string timed_read(file f, int timeout) {
+       thread reader = fork func() {
+               try {
+                       return fgets(f);
+               } catch Thread::signal(int i) {
+                       return "";
+               }
+       }();
+
+       thread killer = fork func() {
+               try {
+                       sleep (timeout);
+                       Thread::send_signal(reader, 1);
+               } catch Thread::signal(int i) {
+                       return;
+               }
+       }();
+
+       poly v = Thread::join(reader);
+       Thread::send_signal(killer, 1);
+       Thread::join(killer);
+       if (is_string(v))
+               return v;
+       return "";
+}
+
+void flush_input(file f) {
+       for (;;) {
+               string s = timed_read(f, 200);
+               if (s == "")
+                       break;
+       }
+}
+
+string[*] settings(file f) {
+       string[...] x = {};
+
+       flush_input(f);
+       fprintf (f, "c s\nv\n");
+       flush(f);
+       for (;;) {
+               string l = File::fgets(f);
+               x[dim(x)] = l;
+               if (String::index(l, "software-version") == 0)
+                       break;
+       }
+       return x;
+}
+
+string[*] find_setting(string[*] s, string match) {
+       for (int i = 0; i < dim(s); i++)
+               if (String::index(s[i], match) == 0)
+                       return String::split(s[i], " ");
+       return (string[*]) {};
+}
+
+bool
+do_cal(file f) {
+       flush_input(f);
+       fprintf(f, "E 1\nc a 0\n");
+       flush(f);
+       string s = "";
+       bool worked = true;
+       bool running = false;
+
+       thread put = fork func() {
+               try {
+                       for (;;) {
+                               putc(getchar(), f);
+                               flush(f);
+                       }
+               } catch Thread::signal(int i) {
+                       return;
+               }
+       }();
+
+       for (;;) {
+               int c = getc(f);
+               if (c == '\n')
+                       s = "";
+               else
+                       s = s + String::new(c);
+               putchar(c); flush(stdout);
+               if (String::index(s, "press a key...") >= 0)
+                       running = true;
+               if (String::index(s, "Invalid") >= 0)
+                       worked = false;
+               if (running && String::index(s, ">") >= 0)
+                       break;
+       }
+       fprintf (f, "E 0\n");
+       if (worked)
+               fprintf (f, "c w\n");
+       sleep(200);
+       Thread::send_signal(put, 1);
+       Thread::join(put);
+
+       return worked;
+}
+
+void main () {
+       string  name = argv[1];
+       file    f = open(name, "r+");
+
+       if (do_cal(f)) {
+               string[*] s = settings(f);
+               string[*] ac = find_setting(s, "Accel cal");
+               printf ("Calibration value +1g %s -1g %s saved\n", ac[3], ac[5]);
+               exit (0);
+       } else {
+               printf ("Calibration failed\n");
+               exit (1);
+       }
+}
+
+main();
diff --git a/ao-bringup/cal-freq b/ao-bringup/cal-freq
new file mode 100755 (executable)
index 0000000..dc2f221
--- /dev/null
@@ -0,0 +1,46 @@
+#!/bin/sh
+
+case $# in
+1)
+       dev="$1"
+       ;;
+*)
+       echo "Usage: $0 <device>"
+       exit 1;
+       ;;
+esac
+
+while true; do
+       echo 'C 1' > $dev
+
+       echo -n "Generating RF carrier. Please enter measured frequency [enter for done]: "
+
+       read FREQ
+
+       echo 'C 0' > $dev
+
+       case "$FREQ" in
+       "")
+               exit 0
+               ;;
+       *)
+               calline=`./get-radio-cal $dev`
+               CURRENT_CAL=`echo $calline | awk '{print $2}'`
+               CURRENT_FREQ=`echo $calline | awk '{print $4}'`
+
+               echo "Current radio calibration "$CURRENT_CAL
+               echo "Current radio frequency "$CURRENT_FREQ
+
+               CAL_VALUE=`nickle -e "floor($CURRENT_FREQ / $FREQ * $CURRENT_CAL + 0.5)"`
+
+               echo "Programming flash with cal value " $CAL_VALUE
+
+               cat << EOF > $dev
+c f $CAL_VALUE
+c w
+EOF
+
+               echo "Serial number "$SERIAL" programmed with RF cal value "$CAL_VALUE
+               ;;
+       esac
+done
diff --git a/ao-bringup/get-radio-cal b/ao-bringup/get-radio-cal
new file mode 100755 (executable)
index 0000000..8f975fe
--- /dev/null
@@ -0,0 +1,66 @@
+#!/usr/bin/nickle
+
+import File;
+
+string timed_read(file f, int timeout) {
+       thread reader = fork func() { try { return fgets(f); } catch Thread::signal(int i) { return ""; } }();
+       thread killer = fork func() { sleep (timeout); Thread::send_signal(reader, 1); }();
+       poly v = Thread::join(reader);
+       if (is_string(v))
+               return v;
+       return "";
+}
+
+void flush_input(file f) {
+       for (;;) {
+               string s = timed_read(f, 100);
+               if (s == "")
+                       break;
+       }
+}
+
+string[*] settings(file f) {
+       string[...] x = {};
+
+       flush_input(f);
+       fprintf (f, "c s\nv\n");
+       flush(f);
+       for (;;) {
+               string l = File::fgets(f);
+               x[dim(x)] = l;
+               if (String::index(l, "software-version") == 0)
+                       break;
+       }
+       return x;
+}
+
+string[*] find_setting(string[*] s, string match) {
+       for (int i = 0; i < dim(s); i++)
+               if (String::index(s[i], match) == 0)
+                       return String::split(s[i], " ");
+       return (string[*]) {};
+}
+
+int[*]
+get_radio (file f) {
+       string[*] s = settings(f);
+
+       string[*] cal = find_setting(s, "Radio cal:");
+       string[*] freq = find_setting(s, "Frequency:");
+       if (dim(cal) == 0 || dim(freq) == 0)
+               return (int[2]) { 0, 0 };
+
+       int cal_val = string_to_integer(cal[2]);
+       int freq_val = string_to_integer(freq[1]);
+       return (int[2]) { cal_val, freq_val };
+}
+
+void main () {
+       string  name = argv[1];
+       file    f = open(name, "r+");
+
+       int[*] vals = get_radio(f);
+       printf ("cal %d freq %f\n", vals[0], vals[1] / 1000);
+}
+
+main();
diff --git a/ao-bringup/turnon_telemega b/ao-bringup/turnon_telemega
new file mode 100755 (executable)
index 0000000..bddb762
--- /dev/null
@@ -0,0 +1,61 @@
+#!/bin/sh
+
+if [ -x ../ao-tools/ao-stmload/ao-stmload ]; then
+       STMLOAD=../ao-tools/ao-stmload/ao-stmload
+elif [ -x /usr/bin/ao-stmload ]; then
+       STMLOAD=/usr/bin/ao-stmload
+else
+       echo "Can't find ao-stmload!  Aborting."
+       exit 1
+fi
+
+if [ -x ../ao-tools/ao-usbload/ao-usbload ]; then
+       USBLOAD=../ao-tools/ao-usbload/ao-usbload
+elif [ -x /usr/bin/ao-usbload ]; then
+       USBLOAD=/usr/bin/ao-usbload
+else
+       echo "Can't find ao-usbload!  Aborting."
+       exit 1
+fi
+
+VERSION=1.0
+#VERSION=0.1
+
+echo "TeleMega v$VERSION Turn-On and Calibration Program"
+echo "Copyright 2010 by Bdale Garbee.  Released under GPL v2"
+echo
+echo "Expectations:"
+echo "\tTeleMega v$VERSIOn powered from USB"
+echo "\t\twith ST-Link-V2 cabled to debug header"
+echo "\t\twith coax from UHF to frequency counter"
+echo
+echo -n "TeleMega-$VERSION serial number: "
+read SERIAL
+
+echo $STMLOAD
+
+$STMLOAD --raw ../src/telemega-v$VERSION/flash-loader/*.elf || exit 1
+
+sleep 2
+
+$USBLOAD ../src/telemega-v$VERSION/*.ihx || exit 1
+
+sleep 2
+
+dev=`ao-list | awk '/TeleMega-v'"$VERSION"'/ { print $3; exit(0); }'`
+
+case "$dev" in
+/dev/tty*)
+       echo "TeleMega found on $dev"
+       ;;
+*)
+       echo 'No TeleMega-v'"$VERSION"' found'
+       exit 1
+       ;;
+esac
+
+echo 'E 0' > $dev
+
+./cal-freq $dev
+
+./cal-accel $dev