1 /* simple wrapper around the stlink_flash_write function */
3 // TODO - this should be done as just a simple flag to the st-util command line...
10 #include "stlink-common.h"
22 static void usage(void)
24 puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size>");
25 puts("stlinkv2 command line: ./flash {read|write} path addr <size>");
28 static int get_opts(struct opts* o, int ac, char** av)
30 /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
31 /* stlinkv2 command line: ./flash {read|write} path addr <size> */
35 if (ac < 3) return -1;
40 if (strcmp(av[0], "read") == 0)
51 o->size = strtoul(av[i + 3], NULL, 10);
53 else if (strcmp(av[0], "write") == 0)
69 o->filename = av[i + 1];
70 o->addr = strtoul(av[i + 2], NULL, 16);
76 int main(int ac, char** av)
82 if (get_opts(&o, ac - 1, av + 1) == -1)
84 printf("invalid command line\n");
89 if (o.devname != NULL) /* stlinkv1 */
91 static const int scsi_verbose = 2;
92 sl = stlink_v1_open(o.devname, scsi_verbose);
93 if (sl == NULL) goto on_error;
97 sl = stlink_open_usb(100);
98 if (sl == NULL) goto on_error;
101 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
102 stlink_exit_dfu_mode(sl);
104 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
105 stlink_enter_swd_mode(sl);
109 if (o.do_read == 0) /* write */
111 err = stlink_fwrite_flash(sl, o.filename, o.addr);
114 printf("stlink_fwrite_flash() == -1\n");
120 err = stlink_fread(sl, o.filename, o.addr, o.size);
123 printf("stlink_fread() == -1\n");
132 if (sl != NULL) stlink_close(sl);