Sunday, July 29, 2018

Circuit Python on Nordic nRF52

Background

I've been using Python for about 5 years now for light scripting utilities that I use for make embedded development easy (e.g. parsers, serial to tcp bridges, etc.).  I really enjoy using Python because its incredibly easy to use, but I never really understood what was going on underneath the hood.  As a bare-metal embedded developer it has always bothered me to not understand how things are working under the hood, since that affects my decision making on developing appropriate solutions to a problem. 

So recently I stumbled upon Circuit Python from AdaFruit and was tempted to buy one until I found that it supported a Nordic nRF52 PCA10040 that I had lying around at work unused.  Before starting, I read the Circuit Python guide to figure out what I was getting myself into and realized it was really simple to get started.  So today is the day I deep dive a bit into the underpinnings of python.  But first step is to put Circuit Python into the nRF52. 

The Setup


The steps are described cleary in their documentation, but I had to figure out a few things to make it work on the nRF52 PCA10040 which I highlight in GREEN.  So here are my steps:
  • Following the steps in 
  • Build their cross tools
    • make -C mpy-cross
  • Now we build the nRF52 target
    • cd ports/nrf
    • make BOARD=pca10040
      • However this caused an error and suggested that I needed to install the ble_stack
        • ../drivers/bluetooth/download_ble_stack.sh
    • make BOARD=pca10040 SD=s132

Installing

Problems

Now this is when I ran into problems.  It looks like you need to specify the value for BOOT_SETTINGS_ADDR, which I had no idea what they would be:
  • make BOARD=pca10040 BOOT_SETTINGS_ADDR=<????> flash 
So instead I commented out the following from the Makefile since I wasn't doing any fancy booting:
# Also update to bootloader settting to validate application and skip checksum ( app valid = 0x0001, crc = 0x0000 )
flash: $(BUILD)/$(OUTPUT_FILENAME).hex
    nrfjprog --program $< --sectorerase -f $(MCU_VARIANT)
    #nrfjprog --erasepage $(BOOT_SETTING_ADDR) -f $(MCU_VARIANT)
    #nrfjprog --memwr $(BOOT_SETTING_ADDR) --val 0x00000001 -f $(MCU_VARIANT)
    nrfjprog --reset -f $(MCU_VARIANT)

Now I can run with no issues
  • make BOARD=pca10040 flash

Running Circuilt Python

Now that it installed successfully, I had to test whether REPL works.  But then I couldn't find a serial device to communicate to.  After some google searching, it looks like the nRF52 PCA10040 uses the on board SEGGER JLink as a UART bridge under /dev/ttyACM0.   Unlike the FTDI implementation of /dev/ttyUSBx which I could have used minicom or putty, I needed to use a different terminal program that supports ttyACMx like screen

So installing screen and then connecting to it
  • sudo apt-get install screen
  • sudo screen /dev/ttyACM0 115200
SUCCESS, I'm able to finally talk to REPL.  Now my next step is to find a