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>");
26 puts(" use hex format for addr and <size>");
29 static int get_opts(struct opts* o, int ac, char** av)
31 /* stlinkv1 command line: ./flash {read|write} /dev/sgX path addr <size> */
32 /* stlinkv2 command line: ./flash {read|write} path addr <size> */
36 if (ac < 3) return -1;
41 if (strcmp(av[0], "read") == 0)
52 o->size = strtoul(av[i + 3], NULL, 16);
54 else if (strcmp(av[0], "write") == 0)
70 o->filename = av[i + 1];
71 o->addr = strtoul(av[i + 2], NULL, 16);
77 int main(int ac, char** av)
84 if (get_opts(&o, ac - 1, av + 1) == -1)
86 printf("invalid command line\n");
91 if (o.devname != NULL) /* stlinkv1 */
93 sl = stlink_v1_open(50);
94 if (sl == NULL) goto on_error;
99 sl = stlink_open_usb(50);
100 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);
110 if (o.do_read == 0) /* write */
112 if ((o.addr >= sl->flash_base) &&
113 (o.addr < sl->flash_base + sl->flash_size))
114 err = stlink_fwrite_flash(sl, o.filename, o.addr);
115 else if ((o.addr >= sl->sram_base) &&
116 (o.addr < sl->sram_base + sl->sram_size))
117 err = stlink_fwrite_sram(sl, o.filename, o.addr);
120 printf("stlink_fwrite_flash() == -1\n");
126 err = stlink_fread(sl, o.filename, o.addr, o.size);
129 printf("stlink_fread() == -1\n");
140 stlink_exit_debug_mode(sl);