Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Erase addr size / section of the flash memory with st-flash #1213

Merged
merged 8 commits into from
Jan 15, 2022
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions inc/stlink.h
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ int stlink_trace_enable(stlink_t* sl, uint32_t frequency);
int stlink_trace_disable(stlink_t* sl);
int stlink_trace_read(stlink_t* sl, uint8_t* buf, size_t size);
int stlink_erase_flash_mass(stlink_t* sl);
int stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size);
int stlink_write_flash(stlink_t* sl, stm32_addr_t address, uint8_t* data, uint32_t length, uint8_t eraseonly);
int stlink_parse_ihex(const char* path, uint8_t erased_pattern, uint8_t * * mem, size_t * size, uint32_t * begin);
uint8_t stlink_get_erased_pattern(stlink_t *sl);
Expand Down
24 changes: 15 additions & 9 deletions src/common.c
Original file line number Diff line number Diff line change
Expand Up @@ -2951,19 +2951,13 @@ int stlink_erase_flash_page(stlink_t *sl, stm32_addr_t flashaddr) {
return check_flash_error(sl);
}

int stlink_erase_flash_mass(stlink_t *sl) {
int err = 0;

// TODO: User MER bit to mass-erase WB series.
if (sl->flash_type == STLINK_FLASH_TYPE_L0 ||
sl->flash_type == STLINK_FLASH_TYPE_WB) {
int stlink_erase_flash_section(stlink_t *sl, stm32_addr_t base_addr, size_t size) {
// erase each page
int i = 0, num_pages = (int)(sl->flash_size / sl->flash_pgsz);

int i = 0, num_pages = (int)(size / sl->flash_pgsz);
Nightwalker-87 marked this conversation as resolved.
Show resolved Hide resolved
for (i = 0; i < num_pages; i++) {
// addr must be an addr inside the page
stm32_addr_t addr =
(stm32_addr_t)sl->flash_base + i * (stm32_addr_t)sl->flash_pgsz;
(stm32_addr_t)base_addr + i * (stm32_addr_t)sl->flash_pgsz;

if (stlink_erase_flash_page(sl, addr)) {
WLOG("Failed to erase_flash_page(%#x) == -1\n", addr);
Expand All @@ -2975,6 +2969,18 @@ int stlink_erase_flash_mass(stlink_t *sl) {
}

fprintf(stdout, "\n");
return 0;
}

int stlink_erase_flash_mass(stlink_t *sl) {
int err = 0;

// TODO: User MER bit to mass-erase WB series.
if (sl->flash_type == STLINK_FLASH_TYPE_L0 ||
sl->flash_type == STLINK_FLASH_TYPE_WB) {

stlink_erase_flash_section(sl, sl->flash_base, sl->flash_size);

} else {
wait_flash_busy(sl);
clear_flash_error(sl);
Expand Down
7 changes: 5 additions & 2 deletions src/st-flash/flash.c
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ static void cleanup(int signum) {

static void usage(void) {
puts("command line: ./st-flash [--debug] [--reset] [--connect-under-reset] [--hot-plug] [--opt] [--serial <serial>] [--format <format>] [--flash=<fsize>] [--freq=<kHz>] [--area=<area>] {read|write} [path] [addr] [size]");
puts("command line: ./st-flash [--debug] [--connect-under-reset] [--hot-plug] [--freq=<kHz>] [--serial <serial>] erase");
puts("command line: ./st-flash [--debug] [--connect-under-reset] [--hot-plug] [--freq=<kHz>] [--serial <serial>] erase [addr] [size]");
puts("command line: ./st-flash [--debug] [--freq=<kHz>] [--serial <serial>] reset");
puts(" <addr>, <serial> and <size>: Use hex format.");
puts(" <fsize>: Use decimal, octal or hex (prefix 0xXXX) format, optionally followed by k=KB, or m=MB (eg. --flash=128k)");
Expand Down Expand Up @@ -168,7 +168,10 @@ int main(int ac, char** av) {
goto on_error;
}
} else if (o.cmd == FLASH_CMD_ERASE) {
err = stlink_erase_flash_mass(sl);
if (o.size != 0 && o.addr >= sl->flash_base && (o.addr + o.size) <= (sl->flash_base + sl->flash_size))
err = stlink_erase_flash_section(sl, o.addr, o.size);
else
err = stlink_erase_flash_mass(sl);

if (err == -1) {
printf("stlink_erase_flash_mass() == -1\n");
Expand Down
19 changes: 18 additions & 1 deletion src/st-flash/flash_opts.c
Original file line number Diff line number Diff line change
Expand Up @@ -234,7 +234,24 @@ int flash_get_opts(struct flash_opts* o, int ac, char** av) {
return(-1);

case FLASH_CMD_ERASE: // no more arguments expected
if (ac != 0) { return(-1); }
if (ac != 0 && ac != 2) { return(-1); }
if (ac == 2) {
uint32_t address;
result = get_integer_from_char_array(av[0], &address);
if (result != 0) {
return bad_arg ("addr");
} else {
o->addr = (stm32_addr_t) address;
}

uint32_t size;
result = get_integer_from_char_array(av[1], &size);
if (result != 0) {
return bad_arg ("size");
} else {
o->size = (size_t) size;
}
}

break;

Expand Down