1 /* simple wrapper around the stlink_flash_write function */
8 #include "stlink-common.h"
20 static void usage(void)
22 puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size>");
23 puts("stlinkv2 command line: ./flash {read|write} path addr <size>");
24 puts(" use hex format for addr and <size>");
27 static int get_opts(struct opts* o, int ac, char** av)
29 /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
30 /* stlinkv2 command line: ./flash {read|write} path addr <size> */
34 if (ac < 3) return -1;
39 if (strcmp(av[0], "read") == 0)
50 o->size = strtoul(av[i + 3], NULL, 16);
52 else if (strcmp(av[0], "write") == 0)
68 o->filename = av[i + 1];
69 o->addr = strtoul(av[i + 2], NULL, 16);
75 int main(int ac, char** av)
81 if (get_opts(&o, ac - 1, av + 1) == -1)
83 printf("invalid command line\n");
88 if (o.devname != NULL) /* stlinkv1 */
91 static const int scsi_verbose = 2;
92 sl = stlink_quirk_open(o.devname, scsi_verbose);
93 if (sl == NULL) goto on_error;
95 printf("not compiled for use with STLink/V1");
101 sl = stlink_open_usb(1);
102 if (sl == NULL) goto on_error;
105 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
106 stlink_exit_dfu_mode(sl);
108 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
109 stlink_enter_swd_mode(sl);
113 if (o.do_read == 0) /* write */
115 err = stlink_fwrite_flash(sl, o.filename, o.addr);
118 printf("stlink_fwrite_flash() == -1\n");
124 err = stlink_fread(sl, o.filename, o.addr, o.size);
127 printf("stlink_fread() == -1\n");
136 if (sl != NULL) stlink_close(sl);