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 sl = stlink_v1_open(100);
92 if (sl == NULL) goto on_error;
96 sl = stlink_open_usb(100);
97 if (sl == NULL) goto on_error;
100 if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE)
101 stlink_exit_dfu_mode(sl);
103 if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE)
104 stlink_enter_swd_mode(sl);
108 if (o.do_read == 0) /* write */
110 err = stlink_fwrite_flash(sl, o.filename, o.addr);
113 printf("stlink_fwrite_flash() == -1\n");
119 err = stlink_fread(sl, o.filename, o.addr, o.size);
122 printf("stlink_fread() == -1\n");
131 if (sl != NULL) stlink_close(sl);