ao-tools: Move ao-selfload into library
authorKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 17:46:13 +0000 (09:46 -0800)
committerKeith Packard <keithp@keithp.com>
Thu, 28 Nov 2013 17:46:13 +0000 (09:46 -0800)
This needs to be shared between ao-stmload and ao-usbload

Signed-off-by: Keith Packard <keithp@keithp.com>
ao-tools/ao-stmload/Makefile.am
ao-tools/ao-stmload/ao-selfload.c [deleted file]
ao-tools/ao-stmload/ao-stmload.c
ao-tools/ao-stmload/ao-stmload.h
ao-tools/lib/Makefile.am
ao-tools/lib/ao-selfload.c [new file with mode: 0644]
ao-tools/lib/ao-verbose.h

index 68b518f15b070af7d3d2f339cc17422562666804..9ed286cc33ec54b0f2262a3ba881fe78816bd230 100644 (file)
@@ -11,7 +11,7 @@ ao_stmload_DEPENDENCIES = $(AO_STMLOAD_LIBS)
 
 ao_stmload_LDADD=$(AO_STMLOAD_LIBS) $(LIBSTLINK_LIBS) $(LIBUSB_LIBS) -lelf
 
-ao_stmload_SOURCES=ao-stmload.c ao-stmload.h ao-selfload.c
+ao_stmload_SOURCES=ao-stmload.c ao-stmload.h
 
 man_MANS = ao-stmload.1
 
diff --git a/ao-tools/ao-stmload/ao-selfload.c b/ao-tools/ao-stmload/ao-selfload.c
deleted file mode 100644 (file)
index dee1c3c..0000000
+++ /dev/null
@@ -1,127 +0,0 @@
-/*
- * 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.
- */
-
-#include <stdio.h>
-#include <stdint.h>
-#include <stdlib.h>
-#include <sysexits.h>
-#include <unistd.h>
-#include <string.h>
-#include "cc.h"
-#include "cc-usb.h"
-#include "ccdbg.h"
-#include "ao-stmload.h"
-
-int    ao_self_verbose;
-
-#define TRACE(...) if (ao_self_verbose) printf (__VA_ARGS__)
-
-void
-ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
-{
-       int                     byte;
-       cc_usb_sync(cc);
-       cc_usb_printf(cc, "R %x\n", address);
-       for (byte = 0; byte < 0x100; byte++) {
-               block[byte] = cc_usb_getchar(cc);
-       }
-       TRACE ("\nread %08x\n", address);
-       for (byte = 0; byte < 0x100; byte++) {
-               TRACE (" %02x", block[byte]);
-               if ((byte & 0xf) == 0xf)
-                       TRACE ("\n");
-       }
-}
-
-void
-ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
-{
-       int                     byte;
-       cc_usb_sync(cc);
-       cc_usb_printf(cc, "W %x\n", address);
-       TRACE ("write %08x\n", address);
-       for (byte = 0; byte < 0x100; byte++) {
-               TRACE (" %02x", block[byte]);
-               if ((byte & 0xf) == 0xf)
-                       TRACE ("\n");
-       }
-       for (byte = 0; byte < 0x100; byte++) {
-               cc_usb_printf(cc, "%c", block[byte]);
-       }
-}
-
-struct ao_hex_image *
-ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
-{
-       struct ao_hex_image     *image;
-       int                     pages;
-       int                     page;
-       uint32_t                base = address & ~0xff;
-       uint32_t                bound = (address + length + 0xff) & ~0xff;
-
-       image = calloc(sizeof (struct ao_hex_image) + (bound - base), 1);
-       image->address = base;
-       image->length = bound - base;
-       pages = image->length / 0x100;
-       for (page = 0; page < pages; page++)
-               ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
-       return image;
-}
-
-int
-ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
-{
-       uint8_t         block[256];
-       uint8_t         check[256];
-       uint32_t        base, bound, length, address;
-       uint32_t        pages;
-       uint32_t        page;
-
-       base = image->address & ~0xff;
-       bound = (image->address + image->length + 0xff) & ~0xff;
-
-       address = base;
-       length = bound - base;
-
-       pages = length / 0x100;
-       printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
-       for (page = 0; page < pages; page++) {
-               uint32_t        start, stop;
-               address = base + page * 0x100;
-
-               if (address < image->address || address + 0x100 > image->address + image->length) {
-                       ao_self_block_read(cc, address, block);
-               }
-               start = address;
-               stop = address + 0x100;
-               if (start < image->address)
-                       start = image->address;
-               if (stop > image->address + image->length)
-                       stop = image->address + image->length;
-               memcpy(block + start - address, image->data + start - image->address, stop - start);
-               ao_self_block_write(cc, address, block);
-               ao_self_block_read(cc, address, check);
-               if (memcmp(block, check, 0x100) != 0) {
-                       fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
-                       return 0;
-               }
-               putchar('.'); fflush(stdout);
-       }
-       printf("done\n");
-       cc_usb_printf(cc,"a\n");
-       return 1;
-}
index a11d93de68974c1c0e417f8f69c44cd412536e00..71d1ec572409e6ebd54d71a7ea40e90db22869aa 100644 (file)
@@ -32,6 +32,8 @@
 #include "cc-usb.h"
 #include "cc.h"
 #include "ao-stmload.h"
+#include "ao-selfload.h"
+#include "ao-verbose.h"
 
 #define AO_USB_DESC_STRING             3
 
@@ -249,13 +251,13 @@ static const struct option options[] = {
        { .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 = 0, .val = 'v' },
+       { .name = "verbose", .has_arg = 1, .val = 'v' },
        { 0, 0, 0, 0},
 };
 
 static void usage(char *program)
 {
-       fprintf(stderr, "usage: %s [--stlink] [--verbose] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
+       fprintf(stderr, "usage: %s [--stlink] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
        exit(1);
 }
 
@@ -316,7 +318,7 @@ main (int argc, char **argv)
        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:Sv:", options, NULL)) != -1) {
                switch (c) {
                case 'T':
                        tty = optarg;
@@ -346,7 +348,7 @@ main (int argc, char **argv)
                }
        }
 
-       ao_self_verbose = verbose;
+       ao_verbose = verbose;
 
        if (verbose > 1)
                ccdbg_add_debug(CC_DEBUG_BITBANG);
index 744dfa75cac04e243fe2e71c05462dba6deacef5..1ba9a9775143e1c675de12521efd840c11757946 100644 (file)
@@ -33,12 +33,4 @@ 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]);
 
-struct ao_hex_image *
-ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length);
-
-int
-ao_self_write(struct cc_usb *cc, struct ao_hex_image *image);
-
-extern int ao_self_verbose;
-
 #endif /* _AO_STMLOAD_H_ */
index ca32e121826224373c2125c4c5e10d7948d47246..5f47c08984d975e01d32033722749694a2dd3f03 100644 (file)
@@ -44,5 +44,7 @@ libao_tools_a_SOURCES = \
        ao-hex.h \
        ao-elf.c \
        ao-elf.h \
+       ao-selfload.c \
+       ao-selfload.h \
        ao-verbose.c \
        ao-verbose.h
diff --git a/ao-tools/lib/ao-selfload.c b/ao-tools/lib/ao-selfload.c
new file mode 100644 (file)
index 0000000..bf036f3
--- /dev/null
@@ -0,0 +1,124 @@
+/*
+ * 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.
+ */
+
+#include <stdio.h>
+#include <stdint.h>
+#include <stdlib.h>
+#include <sysexits.h>
+#include <unistd.h>
+#include <string.h>
+#include "ao-hex.h"
+#include "ao-selfload.h"
+#include "ao-verbose.h"
+
+#define TRACE(...) ao_printf(AO_VERBOSE_SELF, __VA_ARGS__)
+
+static void
+ao_self_block_read(struct cc_usb *cc, uint32_t address, uint8_t block[256])
+{
+       int                     byte;
+       cc_usb_sync(cc);
+       cc_usb_printf(cc, "R %x\n", address);
+       for (byte = 0; byte < 0x100; byte++) {
+               block[byte] = cc_usb_getchar(cc);
+       }
+       TRACE ("\nread %08x\n", address);
+       for (byte = 0; byte < 0x100; byte++) {
+               TRACE (" %02x", block[byte]);
+               if ((byte & 0xf) == 0xf)
+                       TRACE ("\n");
+       }
+}
+
+static void
+ao_self_block_write(struct cc_usb *cc, uint32_t address, uint8_t block[256])
+{
+       int                     byte;
+       cc_usb_sync(cc);
+       cc_usb_printf(cc, "W %x\n", address);
+       TRACE ("write %08x\n", address);
+       for (byte = 0; byte < 0x100; byte++) {
+               TRACE (" %02x", block[byte]);
+               if ((byte & 0xf) == 0xf)
+                       TRACE ("\n");
+       }
+       for (byte = 0; byte < 0x100; byte++) {
+               cc_usb_printf(cc, "%c", block[byte]);
+       }
+}
+
+struct ao_hex_image *
+ao_self_read(struct cc_usb *cc, uint32_t address, uint32_t length)
+{
+       struct ao_hex_image     *image;
+       int                     pages;
+       int                     page;
+       uint32_t                base = address & ~0xff;
+       uint32_t                bound = (address + length + 0xff) & ~0xff;
+
+       image = calloc(sizeof (struct ao_hex_image) + (bound - base), 1);
+       image->address = base;
+       image->length = bound - base;
+       pages = image->length / 0x100;
+       for (page = 0; page < pages; page++)
+               ao_self_block_read(cc, image->address + page * 0x100, image->data + page * 0x100);
+       return image;
+}
+
+bool
+ao_self_write(struct cc_usb *cc, struct ao_hex_image *image)
+{
+       uint8_t         block[256];
+       uint8_t         check[256];
+       uint32_t        base, bound, length, address;
+       uint32_t        pages;
+       uint32_t        page;
+
+       base = image->address & ~0xff;
+       bound = (image->address + image->length + 0xff) & ~0xff;
+
+       address = base;
+       length = bound - base;
+
+       pages = length / 0x100;
+       printf ("Write %08x %d pages: ", address, length/0x100); fflush(stdout);
+       for (page = 0; page < pages; page++) {
+               uint32_t        start, stop;
+               address = base + page * 0x100;
+
+               if (address < image->address || address + 0x100 > image->address + image->length) {
+                       ao_self_block_read(cc, address, block);
+               }
+               start = address;
+               stop = address + 0x100;
+               if (start < image->address)
+                       start = image->address;
+               if (stop > image->address + image->length)
+                       stop = image->address + image->length;
+               memcpy(block + start - address, image->data + start - image->address, stop - start);
+               ao_self_block_write(cc, address, block);
+               ao_self_block_read(cc, address, check);
+               if (memcmp(block, check, 0x100) != 0) {
+                       fprintf(stderr, "Block at 0x%08x doesn't match\n", address);
+                       return 0;
+               }
+               putchar('.'); fflush(stdout);
+       }
+       printf("done\n");
+       cc_usb_printf(cc,"a\n");
+       return 1;
+}
index 45a0559cb9aa4bb56defee59618a3c48155b74ec..26c2fe41974ce8e7121518ad8756228238bafd04 100644 (file)
@@ -24,6 +24,7 @@
 uint32_t       ao_verbose;
 
 #define AO_VERBOSE_EXE 1
+#define AO_VERBOSE_SELF        2
 
 void
 ao_printf(uint32_t verbose, const char *format, ...);