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:
Here is the result on my 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:
A photo of my USB-MiniJTAG-EX JTAG/SWD Debugger/Emulator (I removed the PCB from its plastic case):
Finally a photo with the SWD key connected to the Beacon kit:
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