2017年4月19日 星期三

nRF5X: prevent bootloader from erased after programming

Board : nRF52832_xxac / SDK11/s132  (but not only these have the problem)

After add DFU service in application, we have to program bootloader. In nRFgo , the IROM must be look like this :


but after you compile and program your application again, and open nRFgo next time , you might see :
and that means your bootloader is erased by application.

That's because a mistake of project setting of application



this is project setting of SDK11/ble_app_template
the start point + size = 0x1c000 + 0x64000 = 0x7f000 = the end point.
So, the application is end at 0x7f000 in default.

But in the project setting of SDK 11/bootloader/ dual_bank_dfu_s132
It shows that bootloader start at 0x7a000.

So, if you program application again , it is necessary to erase bootloader for application space.


And the solution I suggest you is :


change application size  0x62000 ->0x5e000


0x1c000+ 0x5d000 = 0x79000 to prevent bootloader from erased after programming


Note:
let the application size smaller than the start point of bootloader for 0x2000 is more stable.

Reference :
https://devzone.nordicsemi.com/question/100609/sdk-12-bootloader-erased-after-programming/

nRF5x: how to use UICR as your signature

board: nRF52832_xxac_s132/ SDK11

After developing a firmware, you can use UICR as a mark to use .
because UICR can not be erased by DFU-OTA

STEP 1
copy file "uicr_config.h" at:
C:\..\nRF5x_SDK_11.0.0\examples\peripheral\uicr_config
to your project file.









STEP 2
open your project in IDE
add this in main.c :


//power by Agatha Kuan
//add uicr
#include "uicr_config.h"
then compile your project first.

STEP 3
open uicr_config.h
I add my family name like this:


// const uint32_t UICR_CLENR0    __attribute__((at(0x10001000))) __attribute__((used)) = 0xFFFFFFFF;
// const uint32_t UICR_RBPCONF   __attribute__((at(0x10001004))) __attribute__((used)) = 0xFFFFFFFF;
// const uint32_t UICR_XTALFREQ  __attribute__((at(0x10001008))) __attribute__((used)) = 0xFFFFFFFF;
const uint32_t UICR_ADDR_0x04 __attribute__((at(0x10001004))) __attribute__((used)) = 0x6e61754b;
//const uint32_t UICR_ADDR_0x80 __attribute__((at(0x1000108c))) __attribute__((used)) = 0x69617354;
//const uint32_t UICR_ADDR_0x84 __attribute__((at(0x10001084))) __attribute__((used)) = 0x6e656843;
//const uint32_t UICR_ADDR_0x88 __attribute__((at(0x10001090))) __attribute__((used)) = 0x53334955;
// const uint32_t UICR_ADDR_0x8C __attribute__((at(0x1000108C))) __attribute__((used)) = 0xFFFFFFFF;
// const uint32_t UICR_ADDR_0x90 __attribute__((at(0x10001090))) __attribute__((used)) = 0xFFFFFFFF;
// const uint32_t UICR_ADDR_0x94 __attribute__((at(0x10001094))) __attribute__((used)) = 0xFFFFFFFF;
// const uint32_t UICR_ADDR_0x98 __attribute__((at(0x10001098))) __attribute__((used)) = 0xFFFFF
then compile , program to your nRF52

STEP 4
open command line, type :


:: power by Agatha Kuan  2017.04.18

.\nrfjprog.exe --family NRF52 --memrd 0x10001004 --n 4

it shows:














be careful that the ascii shows in Little Endian
parameter "--n" write the length to read, "4" means 4 bytes.

Reference:
https://devzone.nordicsemi.com/documentation/nrf51/4.4.1/html/group__uicr__config__example.html

nRF52 avoid "DfuTarg": how to prevent from staying stuck in bootloader mode without starting the application

board: nRF52832_xxac_s132 nordic / SDK 11

While you try to add DFU  OTA service and flash softdevice, application ,and bootloader to your nRF52 (by nRFgo or command line tools),  you might see its BLE Advertising name as "Dfutarg". Like this :



and that means your application does not start. The device stays stuck in bootloader.

There are 2 ways to solve this problem:

First solution: program by command line tools and write memory

1. go to
C:\..\Nordic Semiconductor\nrf5x\bin
open command line, and write :


:: power by Agatha Kuan 20170419

.\nrfjprog.exe --family NRF52 --eraseall
.\nrfjprog.exe --family NRF52 --program %your_softdevice_name%
.\nrfjprog.exe --family NRF52 --program %your_application_name%
.\nrfjprog.exe --family NRF52 --program %your_bootloader_name%
.\nrfjprog.exe --family NRF52 --memwr 0x7f000 --val 1
.\nrfjprog.exe --family NRF52 --reset

then after it reset , the application will start.

Second solution : add patch file 

I provide you a patch file to force the device to start application after reset.
please download by this link :
https://drive.google.com/open?id=0ByqJaZSFoV7Hb190d1l4UjNzb28

and program by command line tools. Open command line and write:



:: power by Agatha Kuan 20170419

.\nrfjprog.exe --family NRF52 --eraseall
.\nrfjprog.exe --family NRF52 --program %your_softdevice_name%
.\nrfjprog.exe --family NRF52 --program %your_application_name%
.\nrfjprog.exe --family NRF52 --program %your_bootloader_name%
.\nrfjprog.exe --family NRF52 --program patch.hex
.\nrfjprog.exe --family NRF52 --reset

Note:
1.the patch.hex file can only use on nRF52832 SDK11
2.these 2 solution does not work after reset or DFU
3.if you want to use the first solution on nRF51, please see  here
(it is 0x3FC00 for nrf51)



Reference:

https://devzone.nordicsemi.com/question/22056/combining-sd-dfu-and-application-hex-and-programming/
https://devzone.nordicsemi.com/question/37880/initdat-file-for-dfu-gets-crc-error-correct-order-nrf-beacon-help/
https://devzone.nordicsemi.com/question/70738/bootloader-does-not-start-application/