ao-tools: Split out USB loader to ao-usbload
authorKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 18:44:07 +0000 (10:44 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 18:44:07 +0000 (10:44 -0800)
Leave ao-stmload using just stlinkv2

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-stmload/ao-stmload.c
ao-tools/ao-usbload/Makefile.am [new file with mode: 0644]
ao-tools/ao-usbload/ao-usbload.1 [new file with mode: 0644]
ao-tools/ao-usbload/ao-usbload.c [new file with mode: 0644]
ao-tools/ao-usbload/ao-usbload.h [new file with mode: 0644]
configure.ac

index f78626ac91ecdfe0eb0e9411f9e1091c461ebed4..618ace21c7d91c03d7b3363be1b040314deae46c 100644 (file)
@@ -29,7 +29,6 @@
 #include "stlink-common.h"
 #include "ao-elf.h"
 #include "ccdbg.h"
 #include "stlink-common.h"
 #include "ao-elf.h"
 #include "ccdbg.h"
-#include "cc-usb.h"
 #include "cc.h"
 #include "ao-stmload.h"
 #include "ao-selfload.h"
 #include "cc.h"
 #include "ao-stmload.h"
 #include "ao-selfload.h"
@@ -65,13 +64,10 @@ get_uint16_sl(stlink_t *sl, uint32_t addr)
 }
 
 static uint16_t
 }
 
 static uint16_t
-get_uint16(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
+get_uint16(stlink_t *sl, uint32_t addr)
 {
        uint16_t        result;
 {
        uint16_t        result;
-       if (cc)
-               result = ao_self_get_uint16(cc, addr);
-       else
-               result = get_uint16_sl(sl, addr);
+       result = get_uint16_sl(sl, addr);
        printf ("read 0x%08x = 0x%04x\n", addr, result);
        return result;
 }
        printf ("read 0x%08x = 0x%04x\n", addr, result);
        return result;
 }
@@ -109,14 +105,11 @@ get_uint32_sl(stlink_t *sl, uint32_t addr)
  * alignment
  */
 static uint32_t
  * alignment
  */
 static uint32_t
-get_uint32(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
+get_uint32(stlink_t *sl, uint32_t addr)
 {
        uint32_t        result;
 
 {
        uint32_t        result;
 
-       if (cc)
-               result = ao_self_get_uint32(cc, addr);
-       else
-               result = get_uint32_sl(sl, addr);
+       result = get_uint32_sl(sl, addr);
        printf ("read 0x%08x = 0x%08x\n", addr, result);
        return result;
 }
        printf ("read 0x%08x = 0x%08x\n", addr, result);
        return result;
 }
@@ -130,10 +123,10 @@ get_uint32(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
  * places this at 0x100 from the start of the rom section
  */
 static int
  * places this at 0x100 from the start of the rom section
  */
 static int
-check_flashed(stlink_t *sl, struct cc_usb *cc)
+check_flashed(stlink_t *sl)
 {
 {
-       uint16_t        romconfig_version = get_uint16(sl, cc, AO_ROMCONFIG_VERSION);
-       uint16_t        romconfig_check = get_uint16(sl, cc, AO_ROMCONFIG_CHECK);
+       uint16_t        romconfig_version = get_uint16(sl, AO_ROMCONFIG_VERSION);
+       uint16_t        romconfig_check = get_uint16(sl, AO_ROMCONFIG_CHECK);
 
        if (romconfig_version != (uint16_t) ~romconfig_check) {
                fprintf (stderr, "Device has not been flashed before\n");
 
        if (romconfig_version != (uint16_t) ~romconfig_check) {
                fprintf (stderr, "Device has not been flashed before\n");
@@ -143,7 +136,6 @@ check_flashed(stlink_t *sl, struct cc_usb *cc)
 }
 
 static const struct option options[] = {
 }
 
 static const struct option options[] = {
-       { .name = "stlink", .has_arg = 0, .val = 'S' },
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
        { .name = "cal", .has_arg = 1, .val = 'c' },
        { .name = "tty", .has_arg = 1, .val = 'T' },
        { .name = "device", .has_arg = 1, .val = 'D' },
        { .name = "cal", .has_arg = 1, .val = 'c' },
@@ -154,23 +146,17 @@ static const struct option options[] = {
 
 static void usage(char *program)
 {
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--stlink] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
+       fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
        exit(1);
 }
 
 void
        exit(1);
 }
 
 void
-done(stlink_t *sl, struct cc_usb *cc, int code)
+done(stlink_t *sl, int code)
 {
 {
-       if (cc) {
-/*             cc_usb_printf(cc, "a\n"); */
-               cc_usb_close(cc);
-       }
-       if (sl) {
-               stlink_reset(sl);
-               stlink_run(sl);
-               stlink_exit_debug_mode(sl);
-               stlink_close(sl);
-       }
+       stlink_reset(sl);
+       stlink_run(sl);
+       stlink_exit_debug_mode(sl);
+       stlink_close(sl);
        exit (code);
 }
 
        exit (code);
 }
 
@@ -207,15 +193,13 @@ main (int argc, char **argv)
        int                     was_flashed = 0;
        struct ao_hex_image     *load;
        int                     tries;
        int                     was_flashed = 0;
        struct ao_hex_image     *load;
        int                     tries;
-       struct cc_usb           *cc = NULL;
-       int                     use_stlink = 0;
        char                    *tty = NULL;
        int                     success;
        int                     verbose = 0;
        struct ao_sym           *file_symbols;
        int                     num_file_symbols;
 
        char                    *tty = NULL;
        int                     success;
        int                     verbose = 0;
        struct ao_sym           *file_symbols;
        int                     num_file_symbols;
 
-       while ((c = getopt_long(argc, argv, "T:D:c:s:Sv:", options, NULL)) != -1) {
+       while ((c = getopt_long(argc, argv, "T:D:c:s:v:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -233,9 +217,6 @@ main (int argc, char **argv)
                        if (serial_end == optarg || *serial_end != '\0')
                                usage(argv[0]);
                        break;
                        if (serial_end == optarg || *serial_end != '\0')
                                usage(argv[0]);
                        break;
-               case 'S':
-                       use_stlink = 1;
-                       break;
                case 'v':
                        verbose++;
                        break;
                case 'v':
                        verbose++;
                        break;
@@ -264,154 +245,86 @@ main (int argc, char **argv)
        if (ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
                fprintf(stderr, "Cannot find required symbols\n");
 
        if (ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
                fprintf(stderr, "Cannot find required symbols\n");
 
-       if (use_stlink) {
-               /* Connect to the programming dongle
-                */
+       /* Connect to the programming dongle
+        */
        
        
-               for (tries = 0; tries < 3; tries++) {
-                       if (device) {
-                               sl = stlink_v1_open(50);
-                       } else {
-                               sl = stlink_open_usb(50);
+       for (tries = 0; tries < 3; tries++) {
+               if (device) {
+                       sl = stlink_v1_open(50);
+               } else {
+                       sl = stlink_open_usb(50);
                
                
-                       }
-                       if (!sl) {
-                               fprintf (stderr, "No STLink devices present\n");
-                               done (sl, NULL, 1);
-                       }
-
-                       if (sl->chip_id != 0)
-                               break;
-                       stlink_reset(sl);
-                       stlink_close(sl);
-                       sl = NULL;
                }
                if (!sl) {
                }
                if (!sl) {
-                       fprintf (stderr, "Debugger connection failed\n");
-                       exit(1);
+                       fprintf (stderr, "No STLink devices present\n");
+                       done (sl, 1);
                }
 
                }
 
-               /* Verify that the loaded image fits entirely within device flash
-                */
-               if (load->address < sl->flash_base ||
-                   sl->flash_base + sl->flash_size < load->address + load->length) {
-                       fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
-                                load->address, load->address + load->length);
-                       done(sl, NULL, 1);
-               }
+               if (sl->chip_id != 0)
+                       break;
+               stlink_reset(sl);
+               stlink_close(sl);
+               sl = NULL;
+       }
+       if (!sl) {
+               fprintf (stderr, "Debugger connection failed\n");
+               exit(1);
+       }
 
 
-               /* Enter debugging mode
-                */
-               if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
-                       stlink_exit_dfu_mode(sl);
-
-               if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
-                       stlink_enter_swd_mode(sl);
-       } else {
-               int     is_loader;
-               int     tries;
-
-               for (tries = 0; tries < 3; tries++) {
-                       char    *this_tty = tty;
-                       if (!this_tty)
-                               this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
-                       if (!this_tty)
-                               this_tty = cc_usbdevs_find_by_arg(device, "MegaMetrum");
-                       if (!this_tty)
-                               this_tty = getenv("ALTOS_TTY");
-                       if (!this_tty)
-                               this_tty="/dev/ttyACM0";
-
-                       cc = cc_usb_open(this_tty);
-
-                       if (!cc)
-                               exit(1);
-                       cc_usb_printf(cc, "v\n");
-                       is_loader = 0;
-                       for (;;) {
-                               char    line[256];
-                               cc_usb_getline(cc, line, sizeof(line));
-                               if (!strncmp(line, "altos-loader", 12))
-                                       is_loader = 1;
-                               if (!strncmp(line, "software-version", 16))
-                                       break;
-                       }
-                       if (is_loader)
-                               break;
-                       printf ("rebooting to loader\n");
-                       cc_usb_printf(cc, "X\n");
-                       cc_usb_close(cc);
-                       sleep(1);
-                       cc = NULL;
-               }
-               if (!is_loader) {
-                       fprintf(stderr, "Cannot switch to boot loader\n");
-                       exit(1);
-               }
-#if 0
-               {
-                       uint8_t check[256];
-                       int     i = 0;
-
-                       ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
-                       for (;;) {
-                               uint8_t block[256];
-                               putchar ('.');
-                               if (++i == 40) {
-                                       putchar('\n');
-                                       i = 0;
-                               }
-                               fflush(stdout);
-                               ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
-                               ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
-                               if (memcmp(block, check, 256) != 0) {
-                                       fprintf (stderr, "read differed\n");
-                                       exit(1);
-                               }
-                       }
-               }
-#endif
+       /* Verify that the loaded image fits entirely within device flash
+        */
+       if (load->address < sl->flash_base ||
+           sl->flash_base + sl->flash_size < load->address + load->length) {
+               fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
+                        load->address, load->address + load->length);
+               done(sl, 1);
        }
 
        }
 
+       /* Enter debugging mode
+        */
+       if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
+               stlink_exit_dfu_mode(sl);
+
+       if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
+               stlink_enter_swd_mode(sl);
+
        /* Go fetch existing config values
         * if available
         */
        /* Go fetch existing config values
         * if available
         */
-       was_flashed = check_flashed(sl, cc);
+       was_flashed = check_flashed(sl);
 
        if (!serial) {
                if (!was_flashed) {
                        fprintf (stderr, "Must provide serial number\n");
 
        if (!serial) {
                if (!was_flashed) {
                        fprintf (stderr, "Must provide serial number\n");
-                       done(sl, cc, 1);
+                       done(sl, 1);
                }
                }
-               serial = get_uint16(sl, cc, AO_SERIAL_NUMBER);
+               serial = get_uint16(sl, AO_SERIAL_NUMBER);
                if (!serial || serial == 0xffff) {
                        fprintf (stderr, "Invalid existing serial %d\n", serial);
                if (!serial || serial == 0xffff) {
                        fprintf (stderr, "Invalid existing serial %d\n", serial);
-                       done(sl, cc, 1);
+                       done(sl, 1);
                }
        }
 
        if (!cal && AO_RADIO_CAL && was_flashed) {
                }
        }
 
        if (!cal && AO_RADIO_CAL && was_flashed) {
-               cal = get_uint32(sl, cc, AO_RADIO_CAL);
+               cal = get_uint32(sl, AO_RADIO_CAL);
                if (!cal || cal == 0xffffffff) {
                        fprintf (stderr, "Invalid existing rf cal %d\n", cal);
                if (!cal || cal == 0xffffffff) {
                        fprintf (stderr, "Invalid existing rf cal %d\n", cal);
-                       done(sl, cc, 1);
+                       done(sl, 1);
                }
        }
 
        if (!ao_editaltos(load, serial, cal))
                }
        }
 
        if (!ao_editaltos(load, serial, cal))
-               done(sl, cc, 1);
+               done(sl, 1);
 
        /* And flash the resulting image to the device
         */
 
        /* And flash the resulting image to the device
         */
-       if (cc)
-               success = ao_self_write(cc, load);
-       else
-               success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
+
+       success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
                
        if (!success) {
                fprintf (stderr, "\"%s\": Write failed\n", filename);
                
        if (!success) {
                fprintf (stderr, "\"%s\": Write failed\n", filename);
-               done(sl, cc, 1);
+               done(sl, 1);
        }
 
        }
 
-       done(sl, cc, 0);
+       done(sl, 0);
 }
 }
diff --git a/ao-tools/ao-usbload/Makefile.am b/ao-tools/ao-usbload/Makefile.am
new file mode 100644 (file)
index 0000000..144e25d
--- /dev/null
@@ -0,0 +1,16 @@
+if LIBSTLINK
+
+bin_PROGRAMS=ao-usbload
+
+AM_CFLAGS=-I$(top_srcdir)/ao-tools/lib $(LIBUSB_CFLAGS)
+AO_STMLOAD_LIBS=$(top_builddir)/ao-tools/lib/libao-tools.a
+
+ao_usbload_DEPENDENCIES = $(AO_STMLOAD_LIBS)
+
+ao_usbload_LDADD=$(AO_STMLOAD_LIBS) $(LIBUSB_LIBS) -lelf
+
+ao_usbload_SOURCES=ao-usbload.c ao-usbload.h
+
+man_MANS = ao-usbload.1
+
+endif
diff --git a/ao-tools/ao-usbload/ao-usbload.1 b/ao-tools/ao-usbload/ao-usbload.1
new file mode 100644 (file)
index 0000000..4816592
--- /dev/null
@@ -0,0 +1,81 @@
+.\"
+.\" Copyright © 2009 Keith Packard <keithp@keithp.com>
+.\"
+.\" This program is free software; you can redistribute it and/or modify
+.\" it under the terms of the GNU General Public License as published by
+.\" the Free Software Foundation; either version 2 of the License, or
+.\" (at your option) any later version.
+.\"
+.\" This program 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
+.\" General Public License for more details.
+.\"
+.\" You should have received a copy of the GNU General Public License along
+.\" with this program; if not, write to the Free Software Foundation, Inc.,
+.\" 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+.\"
+.\"
+.TH AO-LOAD 1 "ao-usbload" ""
+.SH NAME
+ao-usbload \- flash a program to an ARM-based AltOS device
+.SH SYNOPSIS
+.B "ao-usbload"
+[\-T \fItty-device\fP]
+[\--tty \fItty-device\fP]
+[\-D \fIaltos-device\fP]
+[\--device \fIaltos-device\fP]
+[\--cal \fIradio-calibration\fP]
+[\--serial \fserial-number\fP]
+\fIfile.elf\fP or \fIfile.ihx\fP
+.SH DESCRIPTION
+.I ao-usbload
+loads the specified .elf or .ihx file into the target device flash
+memory via the AltOS boot loader, using either existing serial number
+and radio calibration values or taking either of those from the
+command line.
+.SH OPTIONS
+.TP
+\-T tty-device | --tty tty-device
+This selects which tty device the debugger uses to communicate with
+the target device. The special name 'BITBANG' directs ao-dbg to use
+the cp2103 connection, otherwise this should be a usb serial port
+connected to a suitable cc1111 debug node.
+.TP
+\-D AltOS-device | --device AltOS-device
+Search for a connected device. This requires an argument of one of the
+following forms:
+.IP
+TeleMega:2
+.br
+TeleMega
+.br
+2
+.IP
+Leaving out the product name will cause the tool to select a suitable
+product, leaving out the serial number will cause the tool to match
+one of the available devices.
+.TP
+\-s serial-number | --serial serial-number
+This programs the device serial number into the image. If no serial
+number is specified, then the existing serial number, if any, will be
+read from the device.
+.TP
+\-c radio-calibration | --cal radio-calibration This programs the
+radio calibration value into the image for hardware which doesn't have
+any eeprom storage for this value. If no calibration value is
+specified, an existing calibration value will be used. The value here
+can be computed given the current radio calibration value, the
+measured frequency and the desired frequency:
+.IP
+       cal' = cal * (desired/measured)
+.IP
+The default calibration value is 7119667.
+.SH USAGE
+.I ao-usbload
+reads the specified .elf file into memory, edits the image to
+customize it using the specified serial number and radio calibration
+values. It then connects to the debug dongle and writes the program to
+the target device flash memory.
+.SH AUTHOR
+Keith Packard
diff --git a/ao-tools/ao-usbload/ao-usbload.c b/ao-tools/ao-usbload/ao-usbload.c
new file mode 100644 (file)
index 0000000..9e32b2b
--- /dev/null
@@ -0,0 +1,294 @@
+/*
+ * Copyright © 2012 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#include <err.h>
+#include <fcntl.h>
+#include <gelf.h>
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sysexits.h>
+#include <unistd.h>
+#include <getopt.h>
+#include <string.h>
+#include <stdbool.h>
+#include "ao-elf.h"
+#include "ccdbg.h"
+#include "cc-usb.h"
+#include "cc.h"
+#include "ao-usbload.h"
+#include "ao-selfload.h"
+#include "ao-verbose.h"
+#include "ao-editaltos.h"
+
+static uint16_t
+get_uint16(struct cc_usb *cc, uint32_t addr)
+{
+       uint16_t        result;
+       result = ao_self_get_uint16(cc, addr);
+       printf ("read 0x%08x = 0x%04x\n", addr, result);
+       return result;
+}
+
+/*
+ * Read a 32-bit value from the target device with arbitrary
+ * alignment
+ */
+static uint32_t
+get_uint32(struct cc_usb *cc, uint32_t addr)
+{
+       uint32_t        result;
+
+       result = ao_self_get_uint32(cc, addr);
+       printf ("read 0x%08x = 0x%08x\n", addr, result);
+       return result;
+}
+
+/*
+ * Check to see if the target device has been
+ * flashed with a similar firmware image before
+ *
+ * This is done by looking for the same romconfig version,
+ * which should be at the same location as the linker script
+ * places this at 0x100 from the start of the rom section
+ */
+static int
+check_flashed(struct cc_usb *cc)
+{
+       uint16_t        romconfig_version = get_uint16(cc, AO_ROMCONFIG_VERSION);
+       uint16_t        romconfig_check = get_uint16(cc, AO_ROMCONFIG_CHECK);
+
+       if (romconfig_version != (uint16_t) ~romconfig_check) {
+               fprintf (stderr, "Device has not been flashed before\n");
+               return 0;
+       }
+       return 1;
+}
+
+static const struct option options[] = {
+       { .name = "tty", .has_arg = 1, .val = 'T' },
+       { .name = "device", .has_arg = 1, .val = 'D' },
+       { .name = "cal", .has_arg = 1, .val = 'c' },
+       { .name = "serial", .has_arg = 1, .val = 's' },
+       { .name = "verbose", .has_arg = 1, .val = 'v' },
+       { 0, 0, 0, 0},
+};
+
+static void usage(char *program)
+{
+       fprintf(stderr, "usage: %s [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
+       exit(1);
+}
+
+void
+done(struct cc_usb *cc, int code)
+{
+/*     cc_usb_printf(cc, "a\n"); */
+       cc_usb_close(cc);
+       exit (code);
+}
+
+static int
+ends_with(char *whole, char *suffix)
+{
+       int whole_len = strlen(whole);
+       int suffix_len = strlen(suffix);
+
+       if (suffix_len > whole_len)
+               return 0;
+       return strcmp(whole + whole_len - suffix_len, suffix) == 0;
+}
+
+int
+main (int argc, char **argv)
+{
+       char                    *device = NULL;
+       char                    *filename;
+       Elf                     *e;
+       char                    *serial_end;
+       unsigned int            serial = 0;
+       char                    *serial_ucs2;
+       int                     serial_ucs2_len;
+       char                    serial_int[2];
+       unsigned int            s;
+       int                     i;
+       int                     string_num;
+       uint32_t                cal = 0;
+       char                    cal_int[4];
+       char                    *cal_end;
+       int                     c;
+       int                     was_flashed = 0;
+       struct ao_hex_image     *load;
+       int                     tries;
+       struct cc_usb           *cc = NULL;
+       char                    *tty = NULL;
+       int                     success;
+       int                     verbose = 0;
+       struct ao_sym           *file_symbols;
+       int                     num_file_symbols;
+
+       while ((c = getopt_long(argc, argv, "T:D:c:s:v:", options, NULL)) != -1) {
+               switch (c) {
+               case 'T':
+                       tty = optarg;
+                       break;
+               case 'D':
+                       device = optarg;
+                       break;
+               case 'c':
+                       cal = strtoul(optarg, &cal_end, 10);
+                       if (cal_end == optarg || *cal_end != '\0')
+                               usage(argv[0]);
+                       break;
+               case 's':
+                       serial = strtoul(optarg, &serial_end, 10);
+                       if (serial_end == optarg || *serial_end != '\0')
+                               usage(argv[0]);
+                       break;
+               case 'v':
+                       verbose++;
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+
+       ao_verbose = verbose;
+
+       if (verbose > 1)
+               ccdbg_add_debug(CC_DEBUG_BITBANG);
+
+       filename = argv[optind];
+       if (filename == NULL)
+               usage(argv[0]);
+
+       if (ends_with (filename, ".elf")) {
+               load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
+       } else if (ends_with (filename, ".ihx")) {
+               load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
+       } else
+               usage(argv[0]);
+
+       if (ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
+               fprintf(stderr, "Cannot find required symbols\n");
+
+       {
+               int     is_loader;
+               int     tries;
+
+               for (tries = 0; tries < 3; tries++) {
+                       char    *this_tty = tty;
+                       if (!this_tty)
+                               this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
+                       if (!this_tty)
+                               this_tty = cc_usbdevs_find_by_arg(device, "MegaMetrum");
+                       if (!this_tty)
+                               this_tty = getenv("ALTOS_TTY");
+                       if (!this_tty)
+                               this_tty="/dev/ttyACM0";
+
+                       cc = cc_usb_open(this_tty);
+
+                       if (!cc)
+                               exit(1);
+                       cc_usb_printf(cc, "v\n");
+                       is_loader = 0;
+                       for (;;) {
+                               char    line[256];
+                               cc_usb_getline(cc, line, sizeof(line));
+                               if (!strncmp(line, "altos-loader", 12))
+                                       is_loader = 1;
+                               if (!strncmp(line, "software-version", 16))
+                                       break;
+                       }
+                       if (is_loader)
+                               break;
+                       printf ("rebooting to loader\n");
+                       cc_usb_printf(cc, "X\n");
+                       cc_usb_close(cc);
+                       sleep(1);
+                       cc = NULL;
+               }
+               if (!is_loader) {
+                       fprintf(stderr, "Cannot switch to boot loader\n");
+                       exit(1);
+               }
+#if 0
+               {
+                       uint8_t check[256];
+                       int     i = 0;
+
+                       ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
+                       for (;;) {
+                               uint8_t block[256];
+                               putchar ('.');
+                               if (++i == 40) {
+                                       putchar('\n');
+                                       i = 0;
+                               }
+                               fflush(stdout);
+                               ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
+                               ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
+                               if (memcmp(block, check, 256) != 0) {
+                                       fprintf (stderr, "read differed\n");
+                                       exit(1);
+                               }
+                       }
+               }
+#endif
+       }
+
+       /* Go fetch existing config values
+        * if available
+        */
+       was_flashed = check_flashed(cc);
+
+       if (!serial) {
+               if (!was_flashed) {
+                       fprintf (stderr, "Must provide serial number\n");
+                       done(cc, 1);
+               }
+               serial = get_uint16(cc, AO_SERIAL_NUMBER);
+               if (!serial || serial == 0xffff) {
+                       fprintf (stderr, "Invalid existing serial %d\n", serial);
+                       done(cc, 1);
+               }
+       }
+
+       if (!cal && AO_RADIO_CAL && was_flashed) {
+               cal = get_uint32(cc, AO_RADIO_CAL);
+               if (!cal || cal == 0xffffffff) {
+                       fprintf (stderr, "Invalid existing rf cal %d\n", cal);
+                       done(cc, 1);
+               }
+       }
+
+       if (!ao_editaltos(load, serial, cal))
+               done(cc, 1);
+
+       /* And flash the resulting image to the device
+        */
+       success = ao_self_write(cc, load);
+               
+       if (!success) {
+               fprintf (stderr, "\"%s\": Write failed\n", filename);
+               done(cc, 1);
+       }
+
+       done(cc, 0);
+}
diff --git a/ao-tools/ao-usbload/ao-usbload.h b/ao-tools/ao-usbload/ao-usbload.h
new file mode 100644 (file)
index 0000000..1ba9a97
--- /dev/null
@@ -0,0 +1,36 @@
+/*
+ * Copyright © 2013 Keith Packard <keithp@keithp.com>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; version 2 of the License.
+ *
+ * This program 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
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License along
+ * with this program; if not, write to the Free Software Foundation, Inc.,
+ * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
+ */
+
+#ifndef _AO_STMLOAD_H_
+#define _AO_STMLOAD_H_
+
+#include "ao-elf.h"
+
+#define AO_BOOT_APPLICATION_BASE       0x08001000
+
+extern struct ao_sym ao_symbols[];
+
+extern int ao_num_symbols;
+extern int ao_num_required_symbols;
+
+void
+ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256]);
+
+void
+ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256]);
+
+#endif /* _AO_STMLOAD_H_ */
index bf801744271dafe94aa2f73780de70c08a572456..dea71396539971e5f029353c44c145eb311b6d58 100644 (file)
@@ -407,6 +407,7 @@ ao-tools/ao-dumpflash/Makefile
 ao-tools/ao-edit-telem/Makefile
 ao-tools/ao-dump-up/Makefile
 ao-tools/ao-elftohex/Makefile
 ao-tools/ao-edit-telem/Makefile
 ao-tools/ao-dump-up/Makefile
 ao-tools/ao-elftohex/Makefile
+ao-tools/ao-usbload/Makefile
 ao-utils/Makefile
 src/Version
 ])
 ao-utils/Makefile
 src/Version
 ])