Porting to Nordic Semiconductor nRF52

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
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Porting to Nordic Semiconductor nRF52

Post by mattyt » Wed Apr 12, 2017 6:46 am

Just wondering about the status of the nRF52 port? It looks like it's active (I see commits as recent as within the past day) but I don't see a lot of discussion on the forums...

For reference I'm looking at: https://github.com/tralamazza/micropyth ... rf5_no_sdk

I also note that there's a 'nrf52' branch that looks to be using the Nordic SDK. What is the purpose of these two branches? What's the pros/cons to using one or the other?

I'm thinking of purchasing one of the Adafruit Feather nRF52 (https://www.adafruit.com/product/3406) boards and would love to be able to use Micropython on it.

User avatar
tralamazza
Posts: 3
Joined: Sat Jul 30, 2016 1:32 pm
Contact:

Re: Porting to Nordic Semiconductor nRF52

Post by tralamazza » Wed Apr 12, 2017 1:49 pm

Hi mattyt,

Your observation is correct. There isn't much talk about the nrf52 port. However, it does not mean that there is no activity ;)
The nrf52 branch that you found is the initial POC made by me (Daniel), to provide a REPL over Bluetooth LE. This POC depends on libraries and modules from the Nordic SDK.
The nrf5_no_sdk is the extensive port of micropython made by Glenn, and is in active development. It provides a port for both nrf51 and nrf52 at the same time, hence the name. It also provides a rewritten Bluetooth LE REPL which does not depend on SDK libraries (except the bluetooth stack itself and its headers). The port consist of a new HAL layer written from scratch, and machine modules for most of the peripherals in a working state. It also contains a new python module "ubluepy" for Bluetooth LE peripheral and central, which can be used to do bluetooth stuff in micropython.
It should be fairly simple to add support for the Adafruit Feather nRF52. I do not have the board myself, but it's basically a matter of mapping the pin layout and do a decision on whether preservation of the bootloader is needed or not (maybe a new linker script for this purpose?). Feel free to contribute things you feel are missing :)

Cheers,
Daniel & Glenn
Last edited by tralamazza on Thu Apr 13, 2017 9:41 am, edited 1 time in total.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Porting to Nordic Semiconductor nRF52

Post by mattyt » Thu Apr 13, 2017 8:50 am

Thanks for the update Daniel and Glenn! That clarifies the situation a lot. Seems like you guys have been busy!

I think I will purchase one of the Feather nRF52 boards - I haven't had a heap of spare time lately but I'd like to think I could find a little bit of time to help with a port...

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Porting to Nordic Semiconductor nRF52

Post by mattyt » Sun Apr 23, 2017 6:43 am

OK, so my Adafruit nRF52 Feather arrived this week! Nice bit of kit.

Looking at the Memory Map of the Feather as it's delivered, it seems that I have some work to port the nrf5 branch of Micropython to this device...

I'm assuming that the Micropython build is larger than 156KB? If so, I'm going to have to fairly dramatically overhaul the bootloader. The current system implements DFU - which is good - but writes it to Free space before copying it to Application. All great unless your application image is >156KB... They've also written the Nordic Bluetooth stack to the 'SoftDevice' section. Which isn't used for the nrf5 branch so will be wasted space.

Do you folks have a bootloader? I see linker layout files for nRF52 512KB/64KB devices which would be useful but I haven't stumbled on a generic bootloader to reuse.

If I have to change the bootloader I need to solder a header on to the board and use a programming device like a Segger. No big deal but more hoops to jump.

Anyway, this is all ok, I'm just thinking out loud. :)

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

Re: Porting to Nordic Semiconductor nRF52

Post by c45713 » Sun Apr 23, 2017 3:43 pm

Hi mattyt,

I'm glad to hear that you have received your new kit :)
If you haven't already got something up running, i will try to answer your comments and questions as best as i can. It would be great to get this board up running!

The repository name "nrf5_no_sdk" might also be a bit misleading. Softdevices are still used in some of the build targets providing Bluetooth LE functionality. However, this can be downloaded seperatly as it cannot be re-distributed as part of a github repo, license wise. Also, the nrf51/52 could be useful as a target without Softdevice as well, for proprietary 2.4Ghz protocols etc. Or even without radio at all. What the name however indicates is that the micropython port does not depend on an SDK. No components like libraries or HAL drivers has been used from a nordic SDK, making the port standalone.

By default, the nrf5_no_sdk is targetting pca10040, which is using the same chip variant as the feather; nrf52832. Hence, you can try to use some of the existing build targets for pca10040 as a starting point. The application with default configuration currently has these sizes:

142020 (make / no softdevice use, and starting at 0x0)
151920 (make SD=s132 - softdevice used, 1 peripheral and starting at 0x1f000)
152448 (make SD=s132 - softdevice used, 1 peripheral and Bluetooth REPL activated, starting at 0x1f000)

So, the SD=s132 compiled variants should fit just by a few bytes.

Do you have the address of application and bootloader on the Feather?

If the application is positioned on above the softdevice (assuming s132 - 0x1f000) and bootloader in top of flash, then it maps quite well to the pca10040. The nrf52832_512k_64k_s132.ld linker script will be used if SD=s132 is defined, offsetting to 0x1f000. Possible, creating the hex file that you want :)

Does the feather provide UART over USB?
If not, using the Bluetooth LE REPL should work out-of-the box even on this target board. Bluetooth REPL is activated in bluetooth_conf.h as MICROPY_PY_BLE_NUS (1).

When it comes to whether the repo contains any DFU implementation, the answer is sadly No. It would have been a very nice feature to provide. Probably even needed in the long run.

One additional note:
If you intend to flash using Segger, only use the "flash" Makefile target, and not the "sd" target as it the latter target will do an 'eraseall' on the chip. Unless you have access to the bootloader firmware i would suggest that for the first flashes you do it manually, to preserve the bootloader and softdevice. Examples for sector flashing of application can be located in the Makefile.

Cheers,
Glenn

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

Re: Porting to Nordic Semiconductor nRF52

Post by c45713 » Sun Apr 23, 2017 8:48 pm

I took a quick look at the bootloader hex file in the adafruit github repo, and this is what i found:
s132 v.2 - size 0x0 -> 0x1c000
bootloader 0x74000 -> 0x798c8

Currently the nrf5_no_sdk branch does not support the s132 v.2 softdevice, so i will add support for this. Should not be to hard. I'll rename the linkerscripts using softdevice to also include version numbers, and update the board makefiles. Then it should be possible to derive a new linkerscript which preserves the bootloader.

User avatar
mattyt
Posts: 410
Joined: Mon Jan 23, 2017 6:39 am

Re: Porting to Nordic Semiconductor nRF52

Post by mattyt » Mon Apr 24, 2017 3:55 pm

Hi Glenn,
c45713 wrote:If you haven't already got something up running, i will try to answer your comments and questions as best as i can. It would be great to get this board up running!
I'm keen to get it running too!
c45713 wrote:The repository name "nrf5_no_sdk" might also be a bit misleading. Softdevices are still used in some of the build targets providing Bluetooth LE functionality. However, this can be downloaded separately as it cannot be re-distributed as part of a github repo, license wise. Also, the nrf51/52 could be useful as a target without Softdevice as well, for proprietary 2.4Ghz protocols etc. Or even without radio at all. What the name however indicates is that the micropython port does not depend on an SDK. No components like libraries or HAL drivers has been used from a nordic SDK, making the port standalone.
Got it, that's clearer.
c45713 wrote:By default, the nrf5_no_sdk is targetting pca10040, which is using the same chip variant as the feather; nrf52832. Hence, you can try to use some of the existing build targets for pca10040 as a starting point. The application with default configuration currently has these sizes:

142020 (make / no softdevice use, and starting at 0x0)
151920 (make SD=s132 - softdevice used, 1 peripheral and starting at 0x1f000)
152448 (make SD=s132 - softdevice used, 1 peripheral and Bluetooth REPL activated, starting at 0x1f000)

So, the SD=s132 compiled variants should fit just by a few bytes.
A little tight! But it will simplify things to squeeze in to that section. Good to know it'll fit (for now!).
c45713 wrote:Do you have the address of application and bootloader on the Feather?

If the application is positioned on above the softdevice (assuming s132 - 0x1f000) and bootloader in top of flash, then it maps quite well to the pca10040. The nrf52832_512k_64k_s132.ld linker script will be used if SD=s132 is defined, offsetting to 0x1f000. Possible, creating the hex file that you want :)
According to the Memory Map, the application is located at 0x1F000. The bootloader you've already found (0x74000) though, if I understand, the first-stage bootloader is at 0 (the MBR).
c45713 wrote:Does the feather provide UART over USB?
If not, using the Bluetooth LE REPL should work out-of-the box even on this target board. Bluetooth REPL is activated in bluetooth_conf.h as MICROPY_PY_BLE_NUS (1).
Adafruit do provide UART over USB - it's used to program the board (using nfrutil or from the Arduino IDE). But it's good to know that BLE REPL is available...
c45713 wrote:When it comes to whether the repo contains any DFU implementation, the answer is sadly No. It would have been a very nice feature to provide. Probably even needed in the long run.
Maybe we can (modify and) use the Adafruit version? Though they do have a disclaimer stating that OTA DFU "is not actively support nor recommended by Adafruit, and we are still working on making this as safe as possible". I guess this means they're working on it.
c45713 wrote:One additional note:
If you intend to flash using Segger, only use the "flash" Makefile target, and not the "sd" target as it the latter target will do an 'eraseall' on the chip. Unless you have access to the bootloader firmware i would suggest that for the first flashes you do it manually, to preserve the bootloader and softdevice. Examples for sector flashing of application can be located in the Makefile.
I don't have a Segger handy (though I'm sure I can find one at work I could borrow) so I was hoping to use the current Adafruit system to flash Micropython onto the board. I think it should work?

By the way, Adafruit do supply the bootloader (binary only) and provide instructions on how to flash it.

Thanks for the info, I'll keep working on it whenever I have a little time spare! If you can make the changes to use s132 and we change the linker script then we should be good..?

Cheers,
Matt

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

Re: Porting to Nordic Semiconductor nRF52

Post by c45713 » Mon Apr 24, 2017 9:50 pm

Hi Matt,

I have updated the branch to support the s132 v.2.0.1. Renamed the linker scripts and created a new one to offset the application to 0x1C000 which is the end of the v.2 softdevice. However, you might be right that the real offset is 0x1f000. The only way to figure this one out is to try to upload an image of each variant and see where the bootloader jumps, either to 0x1c000 or to 0x1f000.

I also added a template board for the bluefruit_feather, without updating any pin layout (UART, LEDS etc). For now its just a copy of the pca10040.
Currently, the image will be offset to 0x1c000 using s132 v2.0.1 linker script, and can be build using this command:
make BOARD=bluefruit_feather SD=s132

The next step would be to create a firmware package for DFU of either of these images (offset 0x1c000 or 0x1f000). The question i have now, is whether the bootloader uses signed images... I will try out some things tomorrow and see where it leads.

Cheers,
Glenn

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

Re: Porting to Nordic Semiconductor nRF52

Post by c45713 » Tue Apr 25, 2017 8:45 pm

Hi Matt,

A short update on my experiments.

It's getting closer! :)

I managed to get the bootloader to jump to a working micropython Bluetooth REPL application (https://github.com/adafruit/Adafruit_nR ... master/bin). It turns out after all that this bootloader jumps to 0x1c000 and not 0x1f000 as described in the https://learn.adafruit.com/bluefruit-nr ... memory-map image.

Not yet manged to do the firmware upgrade over serial, as i have some issues getting the bootloader into serial mode on my board (not a feather board). So for now i have forced in the micropython app into the device using segger, emulating a serial dfu transfer.

Also, i've updated the template for the bluefruit nrf52 feather with latest linker script that works and updated the board name to feather52. Hence, new command to generate the hex:
make BOARD=feather52 SD=s132

For creating the dfu package i tried to use tools in the this repo: https://github.com/adafruit/Adafruit_nR ... util-0.5.2, but became blocked by UART issues when attempting to transferring it.

Could you try to generate a package using this tool and see if you get serial dfu up running. You could probably test using a hex file configured with bluetooth REPL. Or, update the board/feather52/mpconfigboard.h with currect pins for UART, to see if UART REPL is working?

I'm seeing different documentation on UART RX/TX pin numbers. Without having the board to verify, its quite hard to do a guess :)
TX=6 / RX=8 (https://cdn-learn.adafruit.com/assets/a ... 1488978189)
TX=8 / RX=6 (https://learn.adafruit.com/bluefruit-nr ... ice-pinout)

Cheers,
Glenn

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

Re: Porting to Nordic Semiconductor nRF52

Post by c45713 » Tue Apr 25, 2017 10:34 pm

Should work now. :D I found the correct UART RX/TX pins. Also got the serial upload to work on my side. I believe the max size is even less than 156kb for serial upload. I'll start looking into how to get ubluepy module fit inside the DFU image next.

If you install the nrfutiils from the previous post, you should be able perform these steps to get it up running with UART REPL (no softdevice usage, but still offset to 0x1c000). Modify the boards/feather52/mpconfigboard.mk to fit your UART port. Then issue the following commands:

make BOARD=feather52
make BOARD=feather52 dfu-gen
make BOARD=feather52 dfu-flash

Post Reply