X-Git-Url: https://git.gag.com/?a=blobdiff_plain;f=flash%2Fmain.c;h=24287456b41e283abc6624711267532b4de83fd9;hb=9e842c3aa51780280120915722802356f605aff1;hp=e474174377b1ac154d17cfaf96e46823f087e551;hpb=ebaf5ac5be01306be8d806bfaa33834d9615552c;p=fw%2Fstlink diff --git a/flash/main.c b/flash/main.c index e474174..2428745 100644 --- a/flash/main.c +++ b/flash/main.c @@ -1,5 +1,7 @@ /* simple wrapper around the stlink_flash_write function */ +// TODO - this should be done as just a simple flag to the st-util command line... + #include #include @@ -7,10 +9,10 @@ #include #include "stlink-common.h" - +enum st_cmds {DO_WRITE = 0, DO_READ = 1, DO_ERASE = 2}; struct opts { - unsigned int do_read; + enum st_cmds cmd; const char* devname; const char* filename; stm32_addr_t addr; @@ -20,7 +22,10 @@ struct opts static void usage(void) { puts("stlinkv1 command line: ./flash {read|write} /dev/sgX path addr "); + puts("stlinkv1 command line: ./flash /dev/sgX erase"); puts("stlinkv2 command line: ./flash {read|write} path addr "); + puts("stlinkv2 command line: ./flash erase"); + puts(" use hex format for addr and "); } static int get_opts(struct opts* o, int ac, char** av) @@ -30,38 +35,52 @@ static int get_opts(struct opts* o, int ac, char** av) unsigned int i = 0; - if (ac < 3) return -1; + if (ac < 1) return -1; /* stlinkv2 */ o->devname = NULL; - if (strcmp(av[0], "read") == 0) - { - o->do_read = 1; - - /* stlinkv1 mode */ - if (ac == 5) - { - o->devname = av[1]; - i = 1; - } - - o->size = strtoul(av[i + 3], NULL, 10); - } - else if (strcmp(av[0], "write") == 0) + if (strcmp(av[0], "erase") == 0) { - o->do_read = 0; + o->cmd = DO_ERASE; /* stlinkv1 mode */ - if (ac == 4) + if (ac == 2) { o->devname = av[1]; i = 1; } } - else - { - return -1; + else { + if (ac < 3) return -1; + if (strcmp(av[0], "read") == 0) + { + o->cmd = DO_READ; + + /* stlinkv1 mode */ + if (ac == 5) + { + o->devname = av[1]; + i = 1; + } + if (ac > 3) + o->size = strtoul(av[i + 3], NULL, 16); + } + else if (strcmp(av[0], "write") == 0) + { + o->cmd = DO_WRITE; + + /* stlinkv1 mode */ + if (ac == 4) + { + o->devname = av[1]; + i = 1; + } + } + else + { + return -1; + } } o->filename = av[i + 1]; @@ -77,6 +96,7 @@ int main(int ac, char** av) struct opts o; int err = -1; + o.size = 0; if (get_opts(&o, ac - 1, av + 1) == -1) { printf("invalid command line\n"); @@ -86,19 +106,15 @@ int main(int ac, char** av) if (o.devname != NULL) /* stlinkv1 */ { -#if CONFIG_USE_LIBSG - static const int scsi_verbose = 2; - sl = stlink_quirk_open(o.devname, scsi_verbose); + sl = stlink_v1_open(50); if (sl == NULL) goto on_error; -#else - printf("not compiled for use with STLink/V1"); - goto on_error; -#endif + sl->verbose = 50; } else /* stlinkv2 */ { - sl = stlink_open_usb(10); + sl = stlink_open_usb(50); if (sl == NULL) goto on_error; + sl->verbose = 50; } if (stlink_current_mode(sl) == STLINK_DEV_DFU_MODE) @@ -107,11 +123,29 @@ int main(int ac, char** av) if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) stlink_enter_swd_mode(sl); - stlink_reset(sl); - - if (o.do_read == 0) /* write */ + if (o.cmd == DO_WRITE) /* write */ { - err = stlink_fwrite_flash(sl, o.filename, o.addr); + if ((o.addr >= sl->flash_base) && + (o.addr < sl->flash_base + sl->flash_size)) { + err = stlink_fwrite_flash(sl, o.filename, o.addr); + if (err == -1) + { + printf("stlink_fwrite_flash() == -1\n"); + goto on_error; + } + } + else if ((o.addr >= sl->sram_base) && + (o.addr < sl->sram_base + sl->sram_size)) + err = stlink_fwrite_sram(sl, o.filename, o.addr); + if (err == -1) + { + printf("stlink_sram_flash() == -1\n"); + goto on_error; + } + } + else if (o.cmd == DO_ERASE) + { + err = stlink_erase_flash_mass(sl); if (err == -1) { printf("stlink_fwrite_flash() == -1\n"); @@ -120,6 +154,12 @@ int main(int ac, char** av) } else /* read */ { + if ((o.addr >= sl->flash_base) && + (o.addr < sl->flash_base + sl->flash_size)) + o.size = sl->flash_size; + else if ((o.addr >= sl->sram_base) && + (o.addr < sl->sram_base + sl->sram_size)) + o.size = sl->sram_size; err = stlink_fread(sl, o.filename, o.addr, o.size); if (err == -1) { @@ -132,7 +172,11 @@ int main(int ac, char** av) err = 0; on_error: - if (sl != NULL) stlink_close(sl); + if (sl != NULL) + { + stlink_exit_debug_mode(sl); + stlink_close(sl); + } return err; }