2 * Copyright © 2012 Keith Packard <keithp@keithp.com>
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; version 2 of the License.
8 * This program is distributed in the hope that it will be useful, but
9 * WITHOUT ANY WARRANTY; without even the implied warranty of
10 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
11 * General Public License for more details.
13 * You should have received a copy of the GNU General Public License along
14 * with this program; if not, write to the Free Software Foundation, Inc.,
15 * 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA.
29 #include "stlink-common.h"
34 #include "ao-stmload.h"
35 #include "ao-selfload.h"
36 #include "ao-verbose.h"
37 #include "ao-editaltos.h"
41 * Read a 16-bit value from the target device with arbitrary
45 get_uint16_sl(stlink_t *sl, uint32_t addr)
47 const uint8_t *data = sl->q_buf;
55 actual_addr = addr & ~3;
57 stlink_read_mem32(sl, actual_addr, 8);
63 result = data[off] | (data[off + 1] << 8);
68 get_uint16(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
72 result = ao_self_get_uint16(cc, addr);
74 result = get_uint16_sl(sl, addr);
75 printf ("read 0x%08x = 0x%04x\n", addr, result);
80 * Read a 32-bit value from the target device with arbitrary
84 get_uint32_sl(stlink_t *sl, uint32_t addr)
86 const uint8_t *data = sl->q_buf;
93 printf ("read 0x%x\n", addr);
95 actual_addr = addr & ~3;
97 stlink_read_mem32(sl, actual_addr, 8);
103 result = data[off] | (data[off + 1] << 8) | (data[off+2] << 16) | (data[off+3] << 24);
108 * Read a 32-bit value from the target device with arbitrary
112 get_uint32(stlink_t *sl, struct cc_usb *cc, uint32_t addr)
117 result = ao_self_get_uint32(cc, addr);
119 result = get_uint32_sl(sl, addr);
120 printf ("read 0x%08x = 0x%08x\n", addr, result);
125 * Check to see if the target device has been
126 * flashed with a similar firmware image before
128 * This is done by looking for the same romconfig version,
129 * which should be at the same location as the linker script
130 * places this at 0x100 from the start of the rom section
133 check_flashed(stlink_t *sl, struct cc_usb *cc)
135 uint16_t romconfig_version = get_uint16(sl, cc, AO_ROMCONFIG_VERSION);
136 uint16_t romconfig_check = get_uint16(sl, cc, AO_ROMCONFIG_CHECK);
138 if (romconfig_version != (uint16_t) ~romconfig_check) {
139 fprintf (stderr, "Device has not been flashed before\n");
145 static const struct option options[] = {
146 { .name = "stlink", .has_arg = 0, .val = 'S' },
147 { .name = "tty", .has_arg = 1, .val = 'T' },
148 { .name = "device", .has_arg = 1, .val = 'D' },
149 { .name = "cal", .has_arg = 1, .val = 'c' },
150 { .name = "serial", .has_arg = 1, .val = 's' },
151 { .name = "verbose", .has_arg = 1, .val = 'v' },
155 static void usage(char *program)
157 fprintf(stderr, "usage: %s [--stlink] [--verbose=<verbose>] [--device=<device>] [-tty=<tty>] [--cal=<radio-cal>] [--serial=<serial>] file.{elf,ihx}\n", program);
162 done(stlink_t *sl, struct cc_usb *cc, int code)
165 /* cc_usb_printf(cc, "a\n"); */
171 stlink_exit_debug_mode(sl);
178 ends_with(char *whole, char *suffix)
180 int whole_len = strlen(whole);
181 int suffix_len = strlen(suffix);
183 if (suffix_len > whole_len)
185 return strcmp(whole + whole_len - suffix_len, suffix) == 0;
189 main (int argc, char **argv)
195 unsigned int serial = 0;
208 struct ao_hex_image *load;
210 struct cc_usb *cc = NULL;
215 struct ao_sym *file_symbols;
216 int num_file_symbols;
218 while ((c = getopt_long(argc, argv, "T:D:c:s:Sv:", options, NULL)) != -1) {
227 cal = strtoul(optarg, &cal_end, 10);
228 if (cal_end == optarg || *cal_end != '\0')
232 serial = strtoul(optarg, &serial_end, 10);
233 if (serial_end == optarg || *serial_end != '\0')
248 ao_verbose = verbose;
251 ccdbg_add_debug(CC_DEBUG_BITBANG);
253 filename = argv[optind];
254 if (filename == NULL)
257 if (ends_with (filename, ".elf")) {
258 load = ao_load_elf(filename, &file_symbols, &num_file_symbols);
259 } else if (ends_with (filename, ".ihx")) {
260 load = ao_hex_load(filename, &file_symbols, &num_file_symbols);
264 if (ao_editaltos_find_symbols(file_symbols, num_file_symbols, ao_symbols, ao_num_symbols))
265 fprintf(stderr, "Cannot find required symbols\n");
268 /* Connect to the programming dongle
271 for (tries = 0; tries < 3; tries++) {
273 sl = stlink_v1_open(50);
275 sl = stlink_open_usb(50);
279 fprintf (stderr, "No STLink devices present\n");
283 if (sl->chip_id != 0)
290 fprintf (stderr, "Debugger connection failed\n");
294 /* Verify that the loaded image fits entirely within device flash
296 if (load->address < sl->flash_base ||
297 sl->flash_base + sl->flash_size < load->address + load->length) {
298 fprintf (stderr, "\%s\": Invalid memory range 0x%08x - 0x%08x\n", filename,
299 load->address, load->address + load->length);
303 /* Enter debugging mode
305 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
306 stlink_exit_dfu_mode(sl);
308 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
309 stlink_enter_swd_mode(sl);
314 for (tries = 0; tries < 3; tries++) {
315 char *this_tty = tty;
317 this_tty = cc_usbdevs_find_by_arg(device, "AltosFlash");
319 this_tty = cc_usbdevs_find_by_arg(device, "MegaMetrum");
321 this_tty = getenv("ALTOS_TTY");
323 this_tty="/dev/ttyACM0";
325 cc = cc_usb_open(this_tty);
329 cc_usb_printf(cc, "v\n");
333 cc_usb_getline(cc, line, sizeof(line));
334 if (!strncmp(line, "altos-loader", 12))
336 if (!strncmp(line, "software-version", 16))
341 printf ("rebooting to loader\n");
342 cc_usb_printf(cc, "X\n");
348 fprintf(stderr, "Cannot switch to boot loader\n");
356 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, check);
365 ao_self_block_write(cc, AO_BOOT_APPLICATION_BASE, block);
366 ao_self_block_read(cc, AO_BOOT_APPLICATION_BASE, block);
367 if (memcmp(block, check, 256) != 0) {
368 fprintf (stderr, "read differed\n");
376 /* Go fetch existing config values
379 was_flashed = check_flashed(sl, cc);
383 fprintf (stderr, "Must provide serial number\n");
386 serial = get_uint16(sl, cc, AO_SERIAL_NUMBER);
387 if (!serial || serial == 0xffff) {
388 fprintf (stderr, "Invalid existing serial %d\n", serial);
393 if (!cal && AO_RADIO_CAL && was_flashed) {
394 cal = get_uint32(sl, cc, AO_RADIO_CAL);
395 if (!cal || cal == 0xffffffff) {
396 fprintf (stderr, "Invalid existing rf cal %d\n", cal);
401 if (!ao_editaltos(load, serial, cal))
404 /* And flash the resulting image to the device
407 success = ao_self_write(cc, load);
409 success = (stlink_write_flash(sl, load->address, load->data, load->length) >= 0);
412 fprintf (stderr, "\"%s\": Write failed\n", filename);