altos/test: Adjust CRC error rate after FEC fix
[fw/altos] / ao-tools / ao-rawload / ao-rawload.c
1 /*
2  * Copyright © 2008 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 <unistd.h>
20 #include <getopt.h>
21 #include "ccdbg.h"
22 #include "cc.h"
23
24 static const struct option options[] = {
25         { .name = "tty", .has_arg = 1, .val = 'T' },
26         { .name = "device", .has_arg = 1, .val = 'D' },
27         { .name = "run", .has_arg = 0, .val = 'r' },
28         { 0, 0, 0, 0},
29 };
30
31 static void usage(char *program)
32 {
33         fprintf(stderr, "usage: %s [--tty <tty-name>] [--device <device-name>] [--run] file.ihx\n", program);
34         exit(1);
35 }
36
37 int
38 main (int argc, char **argv)
39 {
40         struct ccdbg    *dbg;
41         struct ao_hex_file      *hex;
42         struct ao_hex_image *image;
43         char            *filename;
44         FILE            *file;
45         char            *tty = NULL;
46         char            *device = NULL;
47         int             c;
48         int             run = 0;
49
50         while ((c = getopt_long(argc, argv, "rT:D:", options, NULL)) != -1) {
51                 switch (c) {
52                 case 'T':
53                         tty = optarg;
54                         break;
55                 case 'D':
56                         device = optarg;
57                         break;
58                 case 'r':
59                         run = 1;
60                         break;
61                 default:
62                         usage(argv[0]);
63                         break;
64                 }
65         }
66         filename = argv[optind];
67         if (filename == NULL) {
68                 usage(argv[0]);
69                 exit(1);
70         }
71         file = fopen(filename, "r");
72         if (!file) {
73                 perror(filename);
74                 exit(1);
75         }
76         hex = ao_hex_file_read(file, filename);
77         fclose(file);
78         if (!hex)
79                 exit (1);
80         image = ao_hex_image_create(hex);
81         if (!image) {
82                 fprintf(stderr, "image create failed\n");
83                 exit (1);
84         }
85
86         ao_hex_file_free(hex);
87         if (!tty)
88                 tty = cc_usbdevs_find_by_arg(device, "TIDongle");
89         dbg = ccdbg_open(tty);
90         if (!dbg)
91                 exit (1);
92
93         ccdbg_add_debug(CC_DEBUG_FLASH);
94
95         ccdbg_debug_mode(dbg);
96         ccdbg_halt(dbg);
97         if (image->address == 0xf000) {
98                 printf("Loading %d bytes to execute from RAM\n",
99                        image->length);
100                 ccdbg_write_hex_image(dbg, image, 0);
101         } else if (image->address == 0x0000) {
102                 printf("Loading %d bytes to execute from FLASH\n",
103                        image->length);
104                 ccdbg_flash_hex_image(dbg, image);
105         } else {
106                 printf("Cannot load code to 0x%04x\n",
107                        image->address);
108                 ao_hex_image_free(image);
109                 ccdbg_close(dbg);
110                 exit(1);
111         }
112         if (run) {
113                 ccdbg_set_pc(dbg, image->address);
114                 ccdbg_resume(dbg);
115         }
116         ccdbg_close(dbg);
117         exit (0);
118 }