nrf52840 dongle flashing via Segger J-link and VSCode

HW:

  • J-link standard
  • nRF52840 Dongle 

SW Support Info:

IMPORTANT: The data below could contain sensitive or confidential information about your environment.
If you do not wish this to be seen by others, please make sure to remove it before sharing.

For help and support, visit the Nordic DevZone at https://devzone.nordicsemi.com/.

{
  "platform": {
    "os": "darwin",
    "osVersion": "Darwin Kernel Version 22.5.0: Thu Jun  8 22:22:20 PDT 2023; root:xnu-8796.121.3~7/RELEASE_ARM64_T6000",
    "osKernel": "22.5.0",
    "vscode": "1.81.1",
    "electron": "22.3.18",
    "node": "v16.17.1"
  },
  "system": {
    "date": "2023-08-27T23:08:53.167Z",
    "vscodeRoot": "/Applications/Visual Studio Code.app/Contents/Resources/app",
    "nrfConnectForDesktopInstalled": true,
    "vscodeUptime": "03:11:11",
    "osUptime": "436:31:14",
    "cpu": {
      "model": "Apple M1 Max",
      "speed": "24 MHz",
      "count": 5
    },
    "memory": {
      "total": "64.00 GB",
      "free": "0.29 GB"
    }
  },
  "workspace": {
    "name": "lights",
    "workspaceFile": null,
    "folders": [
      "/Users/josh/Code/hw/nrf/lights"
    ]
  },
  "extensions": {
    "internal": {
      "nordic-semiconductor.nrf-connect": {
        "version": "2023.7.129",
        "path": "/Users/josh/.vscode/extensions/nordic-semiconductor.nrf-connect-2023.7.129-darwin-arm64",
        "isActive": true
      },
      "nordic-semiconductor.nrf-terminal": {
        "version": "2023.7.47",
        "path": "/Users/josh/.vscode/extensions/nordic-semiconductor.nrf-terminal-2023.7.47",
        "isActive": true
      },
      "nordic-semiconductor.nrf-devicetree": {
        "version": "2023.7.56",
        "path": "/Users/josh/.vscode/extensions/nordic-semiconductor.nrf-devicetree-2023.7.56",
        "isActive": true
      },
      "nordic-semiconductor.nrf-kconfig": {
        "version": "2023.6.51",
        "path": "/Users/josh/.vscode/extensions/nordic-semiconductor.nrf-kconfig-2023.6.51",
        "isActive": true
      }
    },
    "external": {
      "marus25.cortex-debug": null,
      "ms-vscode.cpptools": "1.17.4",
      "ms-vscode.js-debug": "1.81.0",
      "ms-vscode.js-debug-companion": "1.1.2",
      "ms-vscode.vscode-js-profile-table": "1.0.3",
      "alefragnani.Bookmarks": "13.4.1",
      "ambooth.git-rename": "1.3.0",
      "bmalehorn.shell-syntax": "1.0.5",
      "chouzz.vscode-better-align": "1.4.2",
      "christian-kohler.path-intellisense": "2.8.4",
      "coder.coder-remote": "0.1.21",
      "CodeStream.codestream": "14.28.0",
      "DavidAnson.vscode-markdownlint": "0.51.0",
      "dbaeumer.vscode-eslint": "2.4.2",
      "donjayamanne.githistory": "0.6.20",
      "eamodio.gitlens": "14.2.1",
      "eg2.vscode-npm-script": "0.3.29",
      "emilast.LogFileHighlighter": "2.16.0",
      "esbenp.prettier-vscode": "10.1.0",
      "everettjf.filter-line": "2.0.1",
      "foxundermoon.shell-format": "7.2.5",
      "genieai.chatgpt-vscode": "0.0.8",
      "GitHub.copilot": "1.105.350",
      "Gruntfuggly.todo-tree": "0.0.226",
      "hoovercj.vscode-settings-cycler": "1.0.1",
      "jbockle.jbockle-format-files": "3.4.0",
      "KevinRose.vsc-python-indent": "1.18.0",
      "mhutchie.git-graph": "1.30.0",
      "mohsen1.prettify-json": "0.0.3",
      "ms-dotnettools.csharp": "2.0.413",
      "ms-dotnettools.vscode-dotnet-runtime": "1.7.1",
      "ms-python.isort": "2023.10.1",
      "ms-python.python": "2023.14.0",
      "ms-python.vscode-pylance": "2023.8.40",
      "ms-toolsai.jupyter": "2023.7.1002162226",
      "ms-toolsai.jupyter-keymap": "1.1.2",
      "ms-toolsai.jupyter-renderers": "1.0.17",
      "ms-toolsai.vscode-jupyter-cell-tags": "0.1.8",
      "ms-toolsai.vscode-jupyter-slideshow": "0.1.5",
      "ms-vscode-remote.remote-containers": "0.304.0",
      "ms-vscode-remote.remote-ssh": "0.102.0",
      "ms-vscode-remote.vscode-remote-extensionpack": "0.24.0",
      "ms-vscode.cpptools-extension-pack": "1.3.0",
      "ms-vscode.cpptools-themes": "2.0.0",
      "ms-vscode.remote-explorer": "0.4.1",
      "ms-vscode.remote-server": "1.4.3",
      "ms-vscode.test-adapter-converter": "0.1.8",
      "ms-vsliveshare.vsliveshare": "1.0.5883",
      "neptunedesign.vs-sequential-number": "1.1.0",
      "nick-rudenko.back-n-forth": "3.1.1",
      "njpwerner.autodocstring": "0.6.1",
      "PKief.material-icon-theme": "4.30.0",
      "quicktype.quicktype": "12.0.46",
      "rvest.vs-code-prettier-eslint": "5.1.0",
      "searKing.preview-vscode": "2.2.5",
      "shaharkazaz.git-merger": "0.4.1",
      "shd101wyy.markdown-preview-enhanced": "0.6.10",
      "streetsidesoftware.code-spell-checker": "2.20.5",
      "trond-snekvik.gnu-mapfiles": "1.1.0",
      "Trottero.dotnetwatchattach": "0.2.6",
      "twxs.cmake": "0.0.17",
      "Tyriar.vscode-terminal-here": "0.2.4",
      "vscode-icons-team.vscode-icons": "12.5.0",
      "vscodeshift.material-ui-snippets": "3.3.8",
      "YuTengjing.vscode-colorize-plus": "0.12.13",
      "ZainChen.json": "2.0.2"
    }
  },
  "tools": {
    "/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gcc": "0.16.0",
    "/Applications/SEGGER/JLink/JLinkExe": "7.92a",
    "/usr/local/bin/nrfjprog": "10.23.0",
    "nrfutil": "Not found",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/cmake": "3.20.5",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/west": "1.0.0",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/python3": "3.9.6",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/ninja": "1.10.2",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/gperf": "3.1",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/dtc": "1.6.1",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/gn": "2106",
    "/opt/nordic/ncs/toolchains/4ef6631da0/bin/git": "2.37.3",
    "gdbPath": "/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin/arm-zephyr-eabi-gdb"
  },
  "sdks": [
    {
      "version": "2.4.0",
      "path": "/opt/nordic/ncs/v2.4.0"
    },
    {
      "version": "2.4.1",
      "path": "/opt/nordic/ncs/v2.4.1"
    },
    {
      "version": "2.4.1",
      "path": "/Users/josh/Code/hw/nrf/lights"
    },
    {
      "version": "3.4.99",
      "path": "/Users/josh/zephyrproject"
    }
  ],
  "toolchains": [
    {
      "version": "2.4.0",
      "path": "/opt/nordic/ncs/toolchains/4ef6631da0"
    }
  ],
  "connectedDevices": [
    {
      "serialNumber": "000260117435",
      "boardVersion": "Unknown",
      "deviceFamily": "NRF52_FAMILY",
      "deviceVersion": null,
      "jlinkObFirmwareVersion": "J-Link V10 compiled Jan 30 2023 11:28:07"
    }
  ],
  "deviceProvider": "nrfutil",
  "config": {
    "nordic-semiconductor.nrf-connect": {
      "topdir": "${nrf-connect.sdk:2.4.1}",
      "toolchain": {
        "path": "${nrf-connect.toolchain:2.4.0}"
      },
      "ozonePath": "",
      "applications": [
        "${workspaceFolder}/blinky"
      ],
      "applicationOptions": {},
      "kconfig": {
        "interface": "kconfig",
        "executeInDefaultShell": false
      },
      "welcome": {
        "showOnStartup": true
      },
      "west": {
        "env": {
          "$base": "terminal"
        }
      },
      "boardRoots": [],
      "taskBindings": {},
      "debugging": {
        "flash": true,
        "bindings": {}
      },
      "activeAppFollowActiveEditor": true,
      "deviceProvider": "nrfutil"
    },
    "nordic-semiconductor.nrf-terminal": {
      "terminalMode": "character"
    }
  },
  "environment": {
    "westEnv": {
      "ELECTRON_RUN_AS_NODE": "1",
      "GIT_EXEC_PATH": "/opt/nordic/ncs/toolchains/4ef6631da0/Cellar/git/2.37.3/libexec/git-core",
      "HOME": "/Users/josh",
      "PATH": "/opt/nordic/ncs/toolchains/4ef6631da0/bin:/opt/nordic/ncs/toolchains/4ef6631da0/usr/bin:/opt/nordic/ncs/toolchains/4ef6631da0/usr/local/bin:/opt/nordic/ncs/toolchains/4ef6631da0/opt/bin:/opt/nordic/ncs/toolchains/4ef6631da0/opt/nanopb/generator-bin:/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/aarch64-zephyr-elf/bin:/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/x86_64-zephyr-elf/bin:/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin:/Users/josh/.nvm/versions/node/v18.12.1/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/josh/.pyenv/shims:/usr/local/opt/llvm/bin:/usr/local/texlive/2019/bin/x86_64-darwin:/Users/josh/Library/Android/sdk/platform-tools:/opt/homebrew/bin:/Users/josh/esp/esp-idf/tools:/Users/josh/bin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/josh/.cargo/bin:/Users/josh/.dotnet/tools:/opt/homebrew/opt/fzf/bin:/Users/josh/.nvm/versions/node/v18.12.1/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/josh/.pyenv/shims:/usr/local/opt/llvm/bin:/usr/local/texlive/2019/bin/x86_64-darwin:/Users/josh/Library/Android/sdk/platform-tools:/opt/homebrew/bin:/Users/josh/esp/esp-idf/tools:/Users/josh/bin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/josh/.cargo/bin:/Users/josh/.dotnet/tools:/opt/homebrew/opt/fzf/bin",
      "ZEPHYR_BASE": "/Users/josh/Code/hw/nrf/lights/zephyr",
      "ZEPHYR_TOOLCHAIN_VARIANT": "zephyr",
      "ZEPHYR_SDK_INSTALL_DIR": "/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk",
      "USER": "josh"
    },
    "inherited": {
      "ELECTRON_RUN_AS_NODE": "1",
      "HOME": "/Users/josh",
      "PATH": "/Users/josh/.nvm/versions/node/v18.12.1/bin:/opt/homebrew/Cellar/pyenv-virtualenv/1.2.1/shims:/Users/josh/.pyenv/shims:/usr/local/opt/llvm/bin:/usr/local/texlive/2019/bin/x86_64-darwin:/Users/josh/Library/Android/sdk/platform-tools:/opt/homebrew/bin:/Users/josh/esp/esp-idf/tools:/Users/josh/bin:/usr/local/bin:/opt/local/bin:/opt/local/sbin:/usr/local/bin:/System/Cryptexes/App/usr/bin:/usr/bin:/bin:/usr/sbin:/sbin:/Applications/Little Snitch.app/Contents/Components:/usr/local/share/dotnet:/opt/X11/bin:~/.dotnet/tools:/Library/Apple/usr/bin:/Library/Frameworks/Mono.framework/Versions/Current/Commands:/Applications/Wireshark.app/Contents/MacOS:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/local/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/bin:/var/run/com.apple.security.cryptexd/codex.system/bootstrap/usr/appleinternal/bin:/Users/josh/.cargo/bin:/Users/josh/.dotnet/tools:/opt/homebrew/opt/fzf/bin",
      "USER": "josh"
    },
    "toolchainPath": "/opt/nordic/ncs/toolchains/4ef6631da0",
    "toolchainBinPath": "/opt/nordic/ncs/toolchains/4ef6631da0/opt/zephyr-sdk/arm-zephyr-eabi/bin",
    "toolchainVersion": "2.4.0"
  },
  "terminal": {
    "defaultProfile": null
  }
}

I can successfully program the dongle using nRFConnect. However, when I try to program with J-Link via VSCode, I get the following error:

Flashing build to 260117435
/bin/sh -c west flash -d /Users/josh/Code/hw/nrf/lights/blinky/build --skip-rebuild --dev-id 260117435

-- west flash: using runner nrfjprog
-- runners.nrfjprog: Flashing file: /Users/josh/Code/hw/nrf/lights/blinky/build/zephyr/zephyr.hex
[error] [ Client] - Encountered error -90: Command read_device_info executed for 12 milliseconds with result -90
[error] [ Client] - Encountered error -90: Command read_memory_descriptors executed for 11 milliseconds with result -90
[error] [ Worker] - Access protection is enabled, can't read device version.
[error] [ Worker] - Can't read memory descriptors, ap-protection is enabled.
[error] [ Client] - Encountered error -90: Command erase_file executed for 23 milliseconds with result -90
[ ###### ] 0.000s | Erase file - Check image [ ##### ] 0.000s | Check image validity - Initialize devic[ ########## ] 0.000s | Check image validity - Check region 0 s[ ############### ] 0.000s | Check image validity - block 1 of 2 [error] [ nRF52] - Failed while detecting device memory block protection status!
[error] [ nRF52] - Failed while erasing device. -90: Access protection is enabled, can't read device version.
[error] [ Worker] - Access protection is enabled, can't read device version.
ERROR: The operation attempted is unavailable due to readback protection in
ERROR: your device. Please use --recover to unlock the device.
NOTE: For additional output, try running again with logging enabled (--log).
NOTE: Any generated log error messages will be displayed.
ERROR: runners.nrfjprog: Flashing failed because the target must be recovered.
To fix, run "west flash --recover" instead.
Note: this will recover and erase all flash memory prior to reflashing.
FATAL ERROR: command exited with status 24: nrfjprog --program /Users/josh/Code/hw/nrf/lights/blinky/build/zephyr/zephyr.hex --sectoranduicrerase --verify -f NRF52 --snr 260117435

I then run west flash --recover and I can connect to the device. However, when I successfully flash, the program never runs (simple blinky example). If I revert back to factory dfu bootloader and reflash with nRFConnect, it works again... however, I can't debug/access using the jlink.

Goal: be able to keep DFU bootloader, while also able to flash/debug using J-Link in VSCode... any help much appreciated!

Parents Reply Children
  • Hi,

    We have two separate SDKs, the nRF5 SDK(old) and nRF Connect SDK(new).
    These come with different default bootloaders:
    nRF5 SDK: nRF5 Bootloader
    nRF Connect SDK: MCUboot

    nRF5 Bootloader

    The dongles come pre-programmed with the nRF5 Bootloader.
    If you want this bootloader, you must program it using the nRF5 SDK, or at least use a hex from the nRF5 SDK. I beleive you should find pre-compiled hex files inside the nRF5 SDK examples folder.
    Then if you build with CONFIG_BOARD_HAS_NRF5_BOOTLOADER=y and the dongle board, it will automatically set FLASH_LOAD_OFFSET to make your hex-file start after the nRF5 bootloader.  It also reserves that space using the partition manager.
    With this, you should be able to flash your application using J-Link, as long as you do not erase everything:

    nrfjprog --sectorerase --program build/zephyr/zephyr.hex

    Or you could just use the nRF Programmer app / nrfutil to upload your new firmware using DFU.

    MCUboot

    The alternative is of course just to use MCUboot instead, which will nicely integrate with the nRF Connect SDK, and lets you program using J-Link and West to program without issue.
    See official docs on bootloaders or my unofficial explanations.

    Did this make sense?

    Regards,
    Sigurd Hellesvik

  • Thanks for the detailed response. I'm amazed at how much documentation you guys have, it's really great. I also really appreciated your "unofficial" documentation.

    I'll give this a shot when I get time (seems like quite a bit of up front work to learn and implement), and let you know if I have anymore questions. FYI I can flash and run with no nRF bootloader, so that's my working solution for the time being

  • parksj10 said:
    Thanks for the detailed response. I'm amazed at how much documentation you guys have, it's really great. I also really appreciated your "unofficial" documentation.

    And thank you for the kind words

    parksj10 said:
    FYI I can flash and run with no nRF bootloader, so that's my working solution for the time being

    Yep, for developing the application, you don't really need a bootloader, as you can use an external debugger(The other chip on your DK) to program the nRF chip.
    The use-case for a bootloader and DFU is mostly to enable you to program the devices in the field, when you do not have an external bootloader available.
    Bootloaders also have some security benefits, which I guess you can learn about if you google something along the lines of "Why use an immutable bootloader microcontrollers".

    parksj10 said:
    I'll give this a shot when I get time

    Good luck!

  • Hi  ,

    I've been giving this a shot as I come back around to the project (got very sidetracked on an I2S implementation).

    You said

    The alternative is of course just to use MCUboot instead, which will nicely integrate with the nRF Connect SDK, and lets you program using J-Link and West to program without issue.

    However, I'm not quite figuring out how to do this. Do I need to do a one-time flash of the MCUboot loader? When I build an NCS project, is this not included directly? Do you know if there's a way to make this included in the compiled binary so that when anyone on the project builds/flashes, it automatically insures the devices has the MCU bootloader? Maybe you have links for where to figure this out? I was looking in the Zephyr docs (https://docs.zephyrproject.org/latest/boards/arm/nrf52840dongle_nrf52840/doc/index.html) and they were saying you need to modify the .dts files for the boards, which I would like to avoid doing...

  • The default method here is to enable MCUboot in a project, and then the project will be build as two parts: MCUboot and the application. Both these will be merged into merged.hex, which is automatically flashed to the nRF chip when you flash it.

    parksj10 said:
    Maybe you have links for where to figure this out? I was looking in the Zephyr docs (https://docs.zephyrproject.org/latest/boards/arm/nrf52840dongle_nrf52840/doc/index.html) and they were saying you need to modify the .dts files for the boards, which I would like to avoid doing...

    See official docs on bootloaders or my unofficial explanations.

    The nRF Connect SDK use the Partition Manager and Multi-Image builds to automatically handle a lot of this, so you would generally not need to do any of that yourself.

Related