BLE application with nRF51822: Firmware flashing

In the last post we have developed our first led blinking application, now we will see how to flash the firmware in the nRF51822 memory. You can use different technics to upgrade the firmware, either by using a SWD key or by using the pre installed DFU bootloader supporting OTA update (update over the air), shipped by default with the nRF51822 Smart Beacon Kit. In this post I’ll only explain how to use a SWD key to flash and debug your program.

First if you don’t have any SWD key compatible with Jlink, you can buy one for only 13$ on the following website : http://www.hotmcu.com/usbminijtagex-jlink-jtagswd-debuggeremula%E2%80%8Btor-p-46.html?cPath=3_25. The first thing you need to do is soldering 4 wires on the Smart Beacon Kit in order to have access to the nRF51822’s SWD port:

nrf51 swd jtag pinout

Here is the result on my Beacon Kit:

bluetooth low energy nrf51 beacon kit

You now have to connect your key to the SWD port of the Beacon kit. The connection depends of the key you are using but the pinout should be as following:

segger jtag swd pinout

A photo of my USB-MiniJTAG-EX JTAG/SWD Debugger/Emula​tor (I removed the PCB from its plastic case):

jtag swd usb programer arm

Finally a photo with the SWD key connected to the Beacon kit:

bluetooth nrf51 beacon kit jtag swd

The application we will use to flash our kit is Jlink Debugger developed by Segger, here is the link where you can download this software: https://www.segger.com/jlink-software.html

Once you’ve downloaded and installed Jlink Debugger, you should have in the terminal path JLinkExe, if it’s not the case please be sure JLinkExe binary directory is present in the PATH variable of your computer.

We are now ready to flash the led blinking firmware into the nRF51’s memory. Place the battery in your Smart Beacon kit battery holder and connect your SWD key to your computer.

        $ cd nrf51/
        $ JLinkExe
        $ J-Link> device nrf51
        $ J-Link> r
        $ J-Link> w4 4001e504 2
        $ J-Link> w4 4001e50c 1
        $ J-Link> w4 4001e514 1
        $ J-Link> w4 4001e504 0
        $ J-Link> r
        $ J-Link> loadbin led_blinking/led_blinking.bin 0x00000000
        $ J-Link> r

By typing the command JLinkExe you will enter into the Jlink terminal. From this terminal you’ll be able to flash, execute and debug your program. The command “device nrf51” allows to tell the debugger which target we will use. The command “r” allows to reset the target. Then we need to erase the whole flash memory, to do that we directly wrote at specific register address. If you look in the reference manual you will see that the NVMC config register, NVMC eraseall register and NVMC eraseuicr register are respectively located at 0x4001e504, 0x4001e50c and 0x4001e514 adresses. Writing the value 2 in NVMC config register enables the erase command. Writing the value 1 in NVMC eraseall register starts the chip erase. Writing the value 1 in NVMC eraseuicr register starts uicr erase (I’ll explain in another post what is the purpose of the UICR on the nRF51). Finally by writing the value 0 in NVMC config register we set back the chip memory in read only mode. After having erased the chip memory we can flash our firmware by typing the “loadbin” command following by two arguments, the first one is the firmare binary path and the second one the memory address at which the firmware will be placed, in our case 0x00000000.

  • To execute the firmware we just need to type the following command:
        $ J-Link> g

You should now see the green led blinking on the Smart Beacon kit ;-). The “g” command allows you to run the firmware on the target. If you want to halt the target to read some reasons you can type “h”. By typing “?” command in the Jlink terminal you’ll be abe to see all the commands supported by the target.

Since it’s pretty annoying to type all these commands before being able to execute and test a firmware, we can place all these commands in a file which will be passed as argument to Jlink.

        $ cd nrf51/
        $ mkdir sh
        $ cd sh/
        $ touch nrf51_erase.sh
  • Add the following commands in nrf51_erase.sh file:
device nrf51822
r
w4 4001e504 2
Sleep 200
w4 4001e50c 1
Sleep 200
w4 4001e514 1
Sleep 200
w4 4001e504 0
Sleep 200
r

To use this script during JLinkExe start, you just have to type the following command in a terminal:

        $ cd nrf51/
        $ JLinkExe sh/nrf51_erase.sh   
Advertisements

2 thoughts on “BLE application with nRF51822: Firmware flashing

  1. Mike March 29, 2016 / 9:24 am

    This series of posts are excellent, the best I have seen for the Nordic. I found your presentation style perfect, bringing the device up step by step with just the right level of detail for me.

    Many thanks!

    Like

    • Jocelyn Masserot March 29, 2016 / 1:09 pm

      Thanks Mike! Glad to hear you liked this series of posts. I’m currently working on the next posts, hopefully they will be released soon.

      Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s