Flashing NRF using pyOCD

Discussion and questions about boards that can run MicroPython but don't have a dedicated forum.
Target audience: Everyone interested in running MicroPython on other hardware.
User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Flashing NRF using pyOCD

Post by devnull » Mon Dec 30, 2019 2:24 pm

Hi;

I have built the firmware for the NRF52 and also dowloaded the bluetooth stack for the device.

Using STLink V2, I have flashed both files:

Code: Select all

BTFILE="s140_nrf52_6.1.1_softdevice.hex"
FWFILE="pca10056-191230-42e45bd.bin"

pyOCD flash $BTFILE --target nrf52                                                  
[====================] 100%
0009050:INFO:loader:Erased 155648 bytes (38 sectors), programmed 155648 bytes (38 pages), skipped 0 bytes (0 pages) at 20.12 kB/s

pyOCD flash $FWFILE --target nrf52                                                  
[====================] 100%
0013961:INFO:loader:Erased 155648 bytes (38 sectors), programmed 155648 bytes (38 pages), skipped 294912 bytes (72 pages) at 34.15 kB/s
pac@pacMacBook nrf %

However I have no REPL when connecting via Serial Adapter to the UART.

Is my flash procedure correct ? - there were no errors, however I have nothing at all at the REPL ?!

I suspect that I need to specify the start address but have no idea what they would be !

c45713
Posts: 51
Joined: Fri Dec 09, 2016 7:09 pm

Re: Flashing NRF using pyOCD

Post by c45713 » Mon Dec 30, 2019 9:18 pm

I'm a bit puzzled why the log shows that you programmed 155648 in both cases.

I'm not sure if this solves the problem, however *.bin files do not contain any position address to know where to put it in flash. So you probably need to define the address on where to put the binary image, or use a hex file where this is defined in the file itself.

You can try to define the region where to flash it.
pyOCD flash -h:

Code: Select all

  -a ADDR, --base-address ADDR
                        Base address used for the address where to flash a
                        binary. Defaults to start of flash.
The correct address for application on top of SoftDevice s140 v6.1.1 is 0x00026000.

In other words, try out these commands:

Code: Select all

pyOCD flash $BTFILE --target nrf52 -a 0x0
pyOCD flash $FWFILE --target nrf52 -a 0x26000

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Tue Dec 31, 2019 12:38 am

Thanks;

I tried that, but still nothing out of the uart.

I also tried changing the target to nrf52840 and nrf52 and using a .hex file for the firmware file, but nothing at all from the uart.

This is a device that I successfully flashed about a year ago using openocd, however that will no longer run with the current version, and so are now using pyOCD.

I also tried a second device just in case the first one was bricked by all the wrong flashing, but it behaves the same.

If I don't "--erase chip" during the btstack upload, then the firmware uploads both fail at about 60%.

This is using .hex files

Code: Select all

pyOCD flash $BTFILE --erase chip --target nrf52
[====================] 100%
0005711:INFO:loader:Erased chip, programmed 155648 bytes (38 pages), skipped 0 bytes (0 pages) at 37.02 kB/s

pyOCD flash $FWFILE1 --target nrf52            
[====================] 100%
0009621:INFO:loader:Erased 159744 bytes (39 sectors), programmed 159744 bytes (39 pages), skipped 0 bytes (0 pages) at 20.23 kB/s

pyOCD flash $BTFILE --erase chip --target nrf52840
[====================] 100%
0005514:INFO:loader:Erased chip, programmed 155648 bytes (38 pages), skipped 0 bytes (0 pages) at 36.73 kB/s

pyOCD flash $FWFILE1 --target nrf52840            
[====================] 100%
0008974:INFO:loader:Erased 159744 bytes (39 sectors), programmed 159744 bytes (39 pages), skipped 0 bytes (0 pages) at 20.15 kB/s
and openocd fails:

Code: Select all

openocd -f interface/stlink-v2.cfg -f target/nrf52.cfg -c init -c "reset init" -c halt -c "program $BTFILE verify" -c "program $FWFILE1 verify" -c reset -c exit 
Open On-Chip Debugger 0.10.0
Licensed under GNU GPL v2
For bug reports, read
	http://openocd.org/doc/doxygen/bugs.html
Info : auto-selecting first available session transport "hla_swd". To override use 'transport select <transport>'.
Info : The selected transport took over low-level target control. The results might differ compared to plain JTAG/SWD
adapter speed: 10000 kHz
Info : Unable to match requested speed 10000 kHz, using 4000 kHz
Info : Unable to match requested speed 10000 kHz, using 4000 kHz
Info : clock speed 4000 kHz
Info : STLINK v2 JTAG v35 API v2 SWIM v7 VID 0x0483 PID 0x3748
Info : using stlink api v2
Info : Target voltage: 3.231196
Info : nrf52.cpu: hardware has 6 breakpoints, 4 watchpoints
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000a80 msp: 0x20000400
target halted due to debug-request, current mode: Thread 
xPSR: 0x01000000 pc: 0x00000a80 msp: 0x20000400
** Programming Started **
embedded:startup.tcl:476: Error: ** Programming Failed **
in procedure 'program' 
in procedure 'program_error' called at file "embedded:startup.tcl", line 532
at file "embedded:startup.tcl", line 476

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Tue Dec 31, 2019 11:20 am

Spent 2 days on this, and nothing at all from the UART.

I have triple-checked the connections and never get any errors, but not a whisper from the UART :-(

Code: Select all

BTFW61="micropython/ports/nrf/drivers/bluetooth/s140_nrf52_6.1.1/s140_nrf52_6.1.1_softdevice.hex";
HOLYHEX="micropython/ports/nrf/build-holyiot_18010-s140/firmware.hex";
HOLYBIN="micropython/ports/nrf/build-holyiot_18010-s140/firmware.bin";
ADDR="0x26000";
TGT="nrf52840";

pyocd cmd -c "reset init" --target $TGT;
pyocd cmd -c "halt" --target $TGT
pyocd erase --mass-erase --target $TGT;
pyocd flash $BTFW61 --target $TGT;
pyocd flash $HOLYBIN --target $TGT --erase sector -a $ADDR;
pyocd cmd -c "reset" --target $TGT;
Resetting target
Successfully halted device
0000868:INFO:eraser:Mass erasing device...
0001079:INFO:eraser:Successfully erased.
[====================] 100%
0008778:INFO:loader:Erased 155648 bytes (38 sectors), programmed 155648 bytes (38 pages), skipped 0 bytes (0 pages) at 20.07 kB/s
[====================] 100%
0008632:INFO:loader:Erased 159744 bytes (39 sectors), programmed 159744 bytes (39 pages), skipped 0 bytes (0 pages) at 20.25 kB/s
Resetting target

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Sat Jan 04, 2020 7:51 am

@c45713 i am now programming using gdb and black magic probe.

Code: Select all

./gdb-nrf52.sh                          
Target voltage: 3.3V
Available Targets:
No. Att Driver
 1      ARM Cortex-M
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
warning: while parsing target memory map (at line 1): Required element <memory> is missing
0x00026000 in ?? ()
Loading section .sec1, size 0xb00 lma 0x0
Loading section .sec2, size 0xf000 lma 0x1000
Loading section .sec3, size 0x10000 lma 0x10000
Loading section .sec4, size 0x5de8 lma 0x20000
Start address 0x0, load size 153832
Transfer rate: 50 KB/sec, 979 bytes/write.
Loading section .sec1, size 0xa000 lma 0x26000
Loading section .sec2, size 0x10000 lma 0x30000
Loading section .sec3, size 0xc9b4 lma 0x40000
Start address 0x26000, load size 158132
Transfer rate: 50 KB/sec, 982 bytes/write.
[Inferior 1 (Remote target) detached]
I am loading both the btstack file and the micropython file in hex format, and to me it looks like it is successful.

I have set the RX/TX pins and disabled hardware flow control in the mpconfigport.h file:

Code: Select all

// UART config
#define MICROPY_HW_UART1_RX         (22)    // P0.22   (p0=0.31, p1=32-63) Default:8
#define MICROPY_HW_UART1_TX         (32)    // P1.00   (p0=0.31, p1=32-63) Default:6

// UART FLOW CONTROL
#define MICROPY_HW_UART1_HWFC       (0)   // Hardware Flow Control
Yet I still get no REPL and have not enabled the bluetooth REPL.

Is it possible to use gdb / black magic probe to detect whether micropython is actuall running using the gdb / back magic probe debugger as I am now unsure whet the device is bricked, whether it not programmed correctly, or whether there is an issue with the uart configration?

Any suggestions / help would be appreciated :-)

c45713
Posts: 51
Joined: Fri Dec 09, 2016 7:09 pm

Re: Flashing NRF using pyOCD

Post by c45713 » Sat Jan 04, 2020 10:18 am

I replaced the Segger firmware on my pca10056 with a DAPLink version https://armmbed.github.io/DAPLink/?boar ... c-nRF51-DK in order to try out your commands running pyOCD. Looks like it works for me, the initial ones and the latest commands. So, i also believe that you have the firmware flashed. Do you have any chance to toggle a LED or or a free pin and measure it from main.c to confirm we hit the main()?

For example put something like this in main.c before entering the REPL:

Code: Select all

nrf_gpio_cfg_output(14);
nrf_gpio_pin_set(14);

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Sat Jan 04, 2020 12:24 pm

Thanks so much for spending the time to do that.

I bought this black magic probe a few months ago and never used it, so this seemed a good opportunity, before that I was using a cheap chines STLink V2.

I will try the led enable tomorrow and let you know what happens.

On a similar subject, I use the new method for frozen files, during compile I saw that the files were compiled by MPC but they did not appear as frozen modules when I programmed my PCA10059 dongle, looks like nrf is still using the previous folder method ?

User avatar
jimmo
Posts: 2754
Joined: Tue Aug 08, 2017 1:57 am
Location: Sydney, Australia
Contact:

Re: Flashing NRF using pyOCD

Post by jimmo » Sun Jan 05, 2020 5:09 am

FWIW I have successfully used a Black Magic Probe to flash a Particle Xenon (nrf52840). (I added the FLASHER=bmp support to the nrf Makefile)

What does your ./gdb-nrf52.sh look like?
c45713 wrote:
Sat Jan 04, 2020 10:18 am
Is it possible to use gdb / black magic probe to detect whether micropython is actuall running using the gdb / back magic probe debugger as I am now unsure whet the device is bricked, whether it not programmed correctly, or whether there is an issue with the uart configration?
Yes -- see what code is running when you break into the debugger.
devnull wrote:
Sat Jan 04, 2020 12:24 pm
On a similar subject, I use the new method for frozen files, during compile I saw that the files were compiled by MPC but they did not appear as frozen modules when I programmed my PCA10059 dongle, looks like nrf is still using the previous folder method ?
Yes, unfortunately the NRF port hasn't been updated to use manifests yet.

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Sun Jan 05, 2020 5:33 am

Hi again;

Is this where it should be added:

Code: Select all

soft_reset:

    led_init();

    led_state(1, 1); // MICROPY_HW_LED_1 aka MICROPY_HW_LED_RED

    mp_stack_set_top(&_ram_end);

    // Stack limit should be less than real stack size, so we have a chance
    // to recover from limit hit.  (Limit is measured in bytes.)
    mp_stack_set_limit((char*)&_ram_end - (char*)&_heap_end - 400);

    machine_init();

    gc_init(&_heap_start, &_heap_end);

    mp_init();
    mp_obj_list_init(mp_sys_path, 0);
    mp_obj_list_append(mp_sys_path, MP_OBJ_NEW_QSTR(MP_QSTR_)); // current dir (or base dir of the script)
    mp_obj_list_init(mp_sys_argv, 0);


    // ### ADDED PORT ENABLE HERE
    nrf_gpio_cfg_output(10);
    nrf_gpio_pin_set(10);

    pyb_set_repl_info(MP_OBJ_NEW_SMALL_INT(0));

    readline_init0();
I have built and re-flashed using this mod on p0.10 and have now used a brand new board on a new breakout board that I made this afternoon, all breakout pins checked and correctly connected, no shorts pin to pin etc and the device programs successfully.

Pressing the reset button (to ground) P0.18 the P0.10 LED does not light during boot, I see no activity whatsoever :-(

User avatar
devnull
Posts: 473
Joined: Sat Jan 07, 2017 1:52 am
Location: Singapore / Cornwall
Contact:

Re: Flashing NRF using pyOCD

Post by devnull » Sun Jan 05, 2020 12:28 pm

Sorry @Jimmo, did not see your post.

This is my gdb-nrf52.sh file:

Code: Select all

#!/bin/sh

GDB="/Users/pac/gcc-arm-none-eabi-9-2019-q4-major/bin/arm-none-eabi-gdb"
BTFW61="/Users/pac/uPython/build/micropython/ports/nrf/fw/s140_nrf52_6.1.1/s140_nrf52_6.1.1_softdevice.hex";
HOLYA="/Users/pac/uPython/build/micropython/ports/nrf/fw/holyiot_18010-191231-42e45bd.hex";
HOLYB="/Users/pac/uPython/build/micropython/ports/nrf/fw/holyiot_18010-200101-42e45bd.hex";

$GDB -ex "target extended-remote /dev/cu.usbmodem79AD7EA61" -ex "monitor tpwr enable" -ex "monitor swdp_scan" -ex "attach 1" -ex "set mem inaccessible-by-default off" -ex "load $BTFW61" -ex "load $HOLYB" -batch;
Output:

Code: Select all

./gdb-nrf52.sh                                                                                                                    
Target voltage: 3.3V
Available Targets:
No. Att Driver
 1      ARM Cortex-M
warning: No executable has been specified and target does not support
determining executable automatically.  Try using the "file" command.
warning: while parsing target memory map (at line 1): Required element <memory> is missing
0x0003a404 in ?? ()
Loading section .sec1, size 0xb00 lma 0x0
Loading section .sec2, size 0xf000 lma 0x1000
Loading section .sec3, size 0x10000 lma 0x10000
Loading section .sec4, size 0x5de8 lma 0x20000
Start address 0x0, load size 153832
Transfer rate: 51 KB/sec, 979 bytes/write.
Loading section .sec1, size 0xa000 lma 0x26000
Loading section .sec2, size 0x10000 lma 0x30000
Loading section .sec3, size 0xc9b4 lma 0x40000
Start address 0x26000, load size 158132
Transfer rate: 51 KB/sec, 982 bytes/write.
[Inferior 1 (Remote target) detached]
It all seems to complete successfully, but I have no output from the device whatsoever. How exactly can I enter debugging and see if anything is running, as I have zero experiebce with debuggers, never used one before !

I have connected a LED to P0.10 and ground and added the following into main.c in the nrf port:

Code: Select all

soft_reset:
    ...
    //PAC P0.10 LED On
    nrf_gpio_cfg_output(10);
    nrf_gpio_pin_set(10);

    pyb_set_repl_info(MP_OBJ_NEW_SMALL_INT(0));

    readline_init0();
Afrer progrming, the LED does not power on, if I press the reset button p0.18 > GND to do a reset, nothing happens (does p0.18 need to be defined as the reset pin ?), I have put the LUD on each pin and can see nothing whatsoever on any pin, this is the 3rd device I have tried and this third one is brand new.

This is the device ( http://www.holyiot.com/tp/2019042516322180424.pdf ) , I succeeded with this about a year ago, but now cannot get anything at all !

Post Reply