altoslib: Fetch target device config for Fire Igniter npyro value
[fw/altos] / ao-tools / ao-rawload / ao-rawload.c
index 5f7708fd2f8c93ac4b1d8da40fefc0baaa71b2ca..17ed73cae20e5ac58a7176ba8583459da22b7e07 100644 (file)
  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
  */
 
+#include <unistd.h>
+#include <getopt.h>
 #include "ccdbg.h"
+#include "cc.h"
+
+static const struct option options[] = {
+       { .name = "tty", .has_arg = 1, .val = 'T' },
+       { .name = "device", .has_arg = 1, .val = 'D' },
+       { .name = "run", .has_arg = 0, .val = 'r' },
+       { 0, 0, 0, 0},
+};
+
+static void usage(char *program)
+{
+       fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--run] file.ihx\n", program);
+       exit(1);
+}
 
 int
 main (int argc, char **argv)
@@ -24,14 +40,34 @@ main (int argc, char **argv)
        struct ccdbg    *dbg;
        uint8_t         status;
        uint16_t        pc;
-       struct hex_file *hex;
-       struct hex_image *image;
-       char *filename;
-       FILE *file;
+       struct ao_hex_file      *hex;
+       struct ao_hex_image *image;
+       char            *filename;
+       FILE            *file;
+       char            *tty = NULL;
+       char            *device = NULL;
+       int             c;
+       int             run = 0;
 
-       filename = argv[1];
+       while ((c = getopt_long(argc, argv, "rT:D:", options, NULL)) != -1) {
+               switch (c) {
+               case 'T':
+                       tty = optarg;
+                       break;
+               case 'D':
+                       device = optarg;
+                       break;
+               case 'r':
+                       run = 1;
+                       break;
+               default:
+                       usage(argv[0]);
+                       break;
+               }
+       }
+       filename = argv[optind];
        if (filename == NULL) {
-               fprintf(stderr, "usage: %s <filename.ihx>\n", argv[0]);
+               usage(argv[0]);
                exit(1);
        }
        file = fopen(filename, "r");
@@ -39,18 +75,20 @@ main (int argc, char **argv)
                perror(filename);
                exit(1);
        }
-       hex = ccdbg_hex_file_read(file, filename);
+       hex = ao_hex_file_read(file, filename);
        fclose(file);
        if (!hex)
                exit (1);
-       image = ccdbg_hex_image_create(hex);
+       image = ao_hex_image_create(hex);
        if (!image) {
                fprintf(stderr, "image create failed\n");
                exit (1);
        }
 
-       ccdbg_hex_file_free(hex);
-       dbg = ccdbg_open();
+       ao_hex_file_free(hex);
+       if (!tty)
+               tty = cc_usbdevs_find_by_arg(device, "TIDongle");
+       dbg = ccdbg_open(tty);
        if (!dbg)
                exit (1);
 
@@ -69,12 +107,14 @@ main (int argc, char **argv)
        } else {
                printf("Cannot load code to 0x%04x\n",
                       image->address);
-               ccdbg_hex_image_free(image);
+               ao_hex_image_free(image);
                ccdbg_close(dbg);
                exit(1);
        }
-       ccdbg_set_pc(dbg, image->address);
-       ccdbg_resume(dbg);
+       if (run) {
+               ccdbg_set_pc(dbg, image->address);
+               ccdbg_resume(dbg);
+       }
        ccdbg_close(dbg);
        exit (0);
 }