From 468e4084f2005048f59e438f34a2b6a8474f9490 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ga=C3=ABtan=20Harter?= Date: Fri, 15 Mar 2019 18:49:00 +0100 Subject: [PATCH] tools/openocd.sh: probe the board for real flash address on binary flash Some boards have a configuration of the flash bank with an address of 0 when it actually starts as 0x08000000 but openocd relies on probing the hardware at runtime. This now allows to first probe the board to get the actual value. If probing fail for any reason, return the value from the configuration. This can happen when the board is unreachable so at least give a valid output instead of an error. This will allow correct flash detection on for example the `stm32f3` and `stm32l4` which have a configured address of 0. https://github.com/gnu-mcu-eclipse/openocd/blob/4a6f93c96146f3c06e61d89377d76e84915af202/tcl/target/stm32f3x.cfg#L64 https://github.com/gnu-mcu-eclipse/openocd/blob/4a6f93c96146f3c06e61d89377d76e84915af202/tcl/target/stm32l4x.cfg#L51 --- dist/tools/openocd/openocd.sh | 38 ++++++++++++++++++++++++++++++++--- 1 file changed, 35 insertions(+), 3 deletions(-) diff --git a/dist/tools/openocd/openocd.sh b/dist/tools/openocd/openocd.sh index c59a1939a2..41b5adacb1 100755 --- a/dist/tools/openocd/openocd.sh +++ b/dist/tools/openocd/openocd.sh @@ -184,17 +184,49 @@ _split_banks() { }' } -# Outputs bank info on different lines without the '{}' -_flash_list() { +_flash_list_raw() { # Openocd output for 'flash list' is # .... # {name nrf51 base 0 size 0 bus_width 1 chip_width 1} {name nrf51 base 268439552 size 0 bus_width 1 chip_width 1} # .... + # + # Before printing the flash list, try to init and probe the board + # to get the actual address. + # Some openocd configuration put an address of 0 and rely on probing to + # find the real flash address like 0x08000000 + # + # If it does not work, fallback to only query the configured value + # + # Probing can fail when the board is in a non flashable state or + # maybe probing would need a different init procedure. + # At least, currently fallback to returning the configured value + + # Probe the real value + sh -c "${OPENOCD} \ + ${OPENOCD_ADAPTER_INIT} \ + -f '${OPENOCD_CONFIG}' \ + -c 'init' \ + -c 'flash probe 0' \ + -c 'flash list' \ + -c 'shutdown'" 2>&1 && return + + # Fallback to return the value stored in openocd + echo "WARN: Failed to probe board flash." >&2 + echo "WARN: Falling back to using the openocd configuration value." >&2 sh -c "${OPENOCD} \ ${OPENOCD_ADAPTER_INIT} \ -f '${OPENOCD_CONFIG}' \ -c 'flash list' \ - -c 'shutdown'" 2>&1 | _split_banks + -c 'shutdown'" 2>&1 +} + +# Outputs bank info on different lines without the '{}' +_flash_list() { + # .... + # name nrf51 base 0 size 0 bus_width 1 chip_width 1 + # name nrf51 base 268439552 size 0 bus_width 1 chip_width 1 + # .... + _flash_list_raw | _split_banks } # Print flash address for 'bank_num' num defaults to 1