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>");
26 static int get_opts(struct opts* o, int ac, char** av)
28 /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
29 /* stlinkv2 command line: ./flash {read|write} path addr <size> */
33 if (ac < 3) return -1;
38 if (strcmp(av[0], "read") == 0)
49 o->size = strtoul(av[i + 3], NULL, 10);
51 else if (strcmp(av[0], "write") == 0)
67 o->filename = av[i + 1];
68 o->addr = strtoul(av[i + 2], NULL, 16);
74 int main(int ac, char** av)
80 if (get_opts(&o, ac - 1, av + 1) == -1)
82 printf("invalid command line\n");
87 if (o.devname != NULL) /* stlinkv1 */
90 static const int scsi_verbose = 2;
91 sl = stlink_quirk_open(o.devname, scsi_verbose);
92 if (sl == NULL) goto on_error;
94 printf("not compiled for use with STLink/V1");
100 sl = stlink_open_usb(1);
101 if (sl == NULL) goto on_error;
104 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
105 stlink_exit_dfu_mode(sl);
107 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
108 stlink_enter_swd_mode(sl);
112 if (o.do_read == 0) /* write */
114 err = stlink_fwrite_flash(sl, o.filename, o.addr);
117 printf("stlink_fwrite_flash() == -1\n");
123 err = stlink_fread(sl, o.filename, o.addr, o.size);
126 printf("stlink_fread() == -1\n");
135 if (sl != NULL) stlink_close(sl);