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