4 Copyright 2008, 2010 Michel Pollet <buserror@gmail.com>
6 This file is part of simavr.
8 simavr is free software: you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation, either version 3 of the License, or
11 (at your option) any later version.
13 simavr is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with simavr. If not, see <http://www.gnu.org/licenses/>.
32 extern avr_kind_t * avr_kind[];
34 void display_usage(char * app)
36 printf("usage: %s [-t] [-g] [-m <device>] [-f <frequency>] firmware\n", app);
37 printf(" -t: run full scale decoder trace\n"
38 " -g: listen for gdb connection on port 1234\n"
39 " -ff: Loads next .hex file as flash\n"
40 " -ee: Loads next .hec file as eeprom\n"
41 " Supported AVR cores:\n");
42 for (int i = 0; avr_kind[i]; i++) {
44 for (int ti = 0; ti < 4 && avr_kind[i]->names[ti]; ti++)
45 printf("%s ", avr_kind[i]->names[ti]);
51 int main(int argc, char *argv[])
53 elf_firmware_t f = {{0}};
58 uint32_t loadBase = AVR_SEGMENT_OFFSET_FLASH;
59 int trace_vectors[8] = {0};
60 int trace_vectors_count = 0;
63 display_usage(basename(argv[0]));
65 for (int pi = 1; pi < argc; pi++) {
66 if (!strcmp(argv[pi], "-h") || !strcmp(argv[pi], "-help")) {
67 display_usage(basename(argv[0]));
68 } else if (!strcmp(argv[pi], "-m") || !strcmp(argv[pi], "-mcu")) {
70 strcpy(name, argv[++pi]);
72 display_usage(basename(argv[0]));
73 } else if (!strcmp(argv[pi], "-f") || !strcmp(argv[pi], "-freq")) {
75 f_cpu = atoi(argv[++pi]);
77 display_usage(basename(argv[0]));
78 } else if (!strcmp(argv[pi], "-t") || !strcmp(argv[pi], "-trace")) {
80 } else if (!strcmp(argv[pi], "-ti")) {
82 trace_vectors[trace_vectors_count++] = atoi(argv[++pi]);
83 } else if (!strcmp(argv[pi], "-g") || !strcmp(argv[pi], "-gdb")) {
85 } else if (!strcmp(argv[pi], "-ee")) {
86 loadBase = AVR_SEGMENT_OFFSET_EEPROM;
87 } else if (!strcmp(argv[pi], "-ff")) {
88 loadBase = AVR_SEGMENT_OFFSET_FLASH;
89 } else if (argv[pi][0] != '-') {
90 char * filename = argv[pi];
91 char * suffix = strrchr(filename, '.');
92 if (suffix && !strcasecmp(suffix, ".hex")) {
93 if (!name[0] || !f_cpu) {
94 fprintf(stderr, "%s: -mcu and -freq are mandatory to load .hex files\n", argv[0]);
97 struct ihex_chunk_t chunk[4];
98 int cnt = read_ihex_chunks(filename, chunk, 4);
100 fprintf(stderr, "%s: Unable to load IHEX file %s\n",
104 printf("Loaded %d section of ihex\n", cnt);
105 for (int ci = 0; ci < cnt; ci++) {
106 if (chunk[ci].baseaddr < (1*1024*1024)) {
107 f.flash = chunk[ci].data;
108 f.flashsize = chunk[ci].size;
109 f.flashbase = chunk[ci].baseaddr;
110 printf("Load HEX flash %08x, %d\n", f.flashbase, f.flashsize);
111 } else if (chunk[ci].baseaddr >= AVR_SEGMENT_OFFSET_EEPROM ||
112 chunk[ci].baseaddr + loadBase >= AVR_SEGMENT_OFFSET_EEPROM) {
114 f.eeprom = chunk[ci].data;
115 f.eesize = chunk[ci].size;
116 printf("Load HEX eeprom %08x, %d\n", chunk[ci].baseaddr, f.eesize);
120 elf_read_firmware(filename, &f);
126 strcpy(f.mmcu, name);
130 avr_t * avr = avr_make_mcu_by_name(f.mmcu);
132 fprintf(stderr, "%s: AVR '%s' now known\n", argv[0], f.mmcu);
136 avr_load_firmware(avr, &f);
138 printf("Attempted to load a bootloader at %04x\n", f.flashbase);
139 avr->pc = f.flashbase;
142 for (int ti = 0; ti < trace_vectors_count; ti++)
143 if (avr->vector[trace_vectors[ti]])
144 avr->vector[trace_vectors[ti]]->trace++;
146 // even if not setup at startup, activate gdb if crashing
147 avr->gdb_port = 1234;
149 avr->state = cpu_Stopped;