From 8c36e07cbefc41751807d9e327c116d3755cc6aa Mon Sep 17 00:00:00 2001 From: Uwe Bonnes Date: Thu, 19 Jan 2012 14:59:02 +0100 Subject: [PATCH] Make stlink_erase_flash_mass device dependant and implement mass erase for L1 by consecutive page erase Allow to erase the device with the flash --- flash/main.c | 71 ++++++++++++++++++++++++++++++--------------- src/stlink-common.c | 58 +++++++++++++++++++++++------------- 2 files changed, 86 insertions(+), 43 deletions(-) diff --git a/flash/main.c b/flash/main.c index 0f3040d..387a335 100644 --- a/flash/main.c +++ b/flash/main.c @@ -9,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; @@ -22,7 +22,9 @@ 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 "); } @@ -33,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) + if (strcmp(av[0], "erase") == 0) { - o->do_read = 1; + o->cmd = DO_ERASE; /* 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->do_read = 0; - - /* 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]; @@ -107,7 +123,7 @@ int main(int ac, char** av) if (stlink_current_mode(sl) != STLINK_DEV_DEBUG_MODE) stlink_enter_swd_mode(sl); - if (o.do_read == 0) /* write */ + if (o.cmd == DO_WRITE) /* write */ { if ((o.addr >= sl->flash_base) && (o.addr < sl->flash_base + sl->flash_size)) @@ -121,6 +137,15 @@ int main(int ac, char** av) goto on_error; } } + else if (o.cmd == DO_ERASE) + { + err = stlink_erase_flash_mass(sl); + if (err == -1) + { + printf("stlink_fwrite_flash() == -1\n"); + goto on_error; + } + } else /* read */ { err = stlink_fread(sl, o.filename, o.addr, o.size); diff --git a/src/stlink-common.c b/src/stlink-common.c index 87dfc02..5cbf7fe 100644 --- a/src/stlink-common.c +++ b/src/stlink-common.c @@ -1053,26 +1053,44 @@ int stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr) } int stlink_erase_flash_mass(stlink_t *sl) { - /* wait for ongoing op to finish */ - wait_flash_busy(sl); - - /* unlock if locked */ - unlock_flash_if(sl); - - /* set the mass erase bit */ - set_flash_cr_mer(sl); - - /* start erase operation, reset by hw with bsy bit */ - set_flash_cr_strt(sl); - - /* wait for completion */ - wait_flash_busy(sl); - - /* relock the flash */ - lock_flash(sl); - - /* todo: verify the erased memory */ - + if (sl->chip_id == STM32_CHIPID_F4) { + DLOG("(FIXME) Mass erase of STM32F4\n"); + } + else if (sl->chip_id == STM32_CHIPID_L1_MEDIUM) { + /* erase each page */ + int i = 0, num_pages = sl->flash_size/sl->flash_pgsz; + for (i = 0; i < num_pages; i++) { + /* addr must be an addr inside the page */ + stm32_addr_t addr = sl->flash_base + i * sl->flash_pgsz; + if (stlink_erase_flash_page(sl, addr) == -1) { + WLOG("Failed to erase_flash_page(%#zx) == -1\n", addr); + return -1; + } + fprintf(stdout,"\rFlash page at %5d/%5d erased", i, num_pages); + fflush(stdout); + } + } + else { + /* wait for ongoing op to finish */ + wait_flash_busy(sl); + + /* unlock if locked */ + unlock_flash_if(sl); + + /* set the mass erase bit */ + set_flash_cr_mer(sl); + + /* start erase operation, reset by hw with bsy bit */ + set_flash_cr_strt(sl); + + /* wait for completion */ + wait_flash_busy(sl); + + /* relock the flash */ + lock_flash(sl); + + /* todo: verify the erased memory */ + } return 0; }