726cc22cafd0e0c1a8becbe4ef52ab03c5e1b547
[fw/altos] / ao-tools / ao-eeprom / ao-eeprom.c
1 /*
2  * Copyright © 2009 Keith Packard <keithp@keithp.com>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful, but
10  * WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12  * General Public License for more details.
13  *
14  * You should have received a copy of the GNU General Public License along
15  * with this program; if not, write to the Free Software Foundation, Inc.,
16  * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
17  */
18
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <unistd.h>
22 #include <getopt.h>
23 #include "cc-usb.h"
24
25 #define NUM_BLOCK       512
26
27 static const struct option options[] = {
28         { .name = "tty", .has_arg = 1, .val = 'T' },
29         { 0, 0, 0, 0},
30 };
31
32 static void usage(char *program)
33 {
34         fprintf(stderr, "usage: %s [--tty <tty-name>]\n", program);
35         exit(1);
36 }
37
38 int
39 main (int argc, char **argv)
40 {
41         struct cc_usb   *cc;
42         int             block;
43         uint8_t         bytes[32 * (2 + 8)];
44         uint8_t         *b;
45         int             i, j;
46         uint32_t        addr;
47         char            *tty = NULL;
48         int             c;
49
50         while ((c = getopt_long(argc, argv, "T:", options, NULL)) != -1) {
51                 switch (c) {
52                 case 'T':
53                         tty = optarg;
54                         break;
55                 default:
56                         usage(argv[0]);
57                         break;
58                 }
59         }
60         if (!tty)
61                 tty = getenv("ALTOS_TTY");
62         if (!tty)
63                 tty="/dev/ttyACM0";
64         cc = cc_usb_open(tty);
65         if (!cc)
66                 exit(1);
67         for (block = 0; block < NUM_BLOCK; block++) {
68                 cc_queue_read(cc, bytes, sizeof (bytes));
69                 cc_usb_printf(cc, "e %x\n", block);
70                 cc_usb_sync(cc);
71                 for (i = 0; i < 32; i++) {
72                         b = bytes + (i * 10);
73                         addr = block * 256 + i * 8;
74                         printf ("%06x", addr);
75                         for (j = 0; j < 8; j++) {
76                                 printf (" %02x", b[j+2]);
77                         }
78                         printf ("\n");
79                 }
80         }
81         cc_usb_close(cc);
82         exit (0);
83 }