In this article, I’ll share my first expirience of getting started with Thread protocol running on cheap module with Nordic nRF52840 chip inside.
It is pretty easy to start experimenting with Thread on Nordic devboard, but using cheap module from China can be a little tricky…
What is a Thread?
First question that you may have already – what is this all for? There is ESP8266/ESP32 modules that are cheap, easy start programming, and they offer Internet WiFi connectivity just out-of-the box, so why we need other chips with other protocols?
Well, simplified answer is – Thread mutch more match requirements of small connected devices than WiFi. Nordic nRF52 needs significally less power than ESP32 (about 10-12 mA vs about 80 mA), and Thread is a mesh network – it means that you don’t need to have direct connection from every device to central router – distant devices can reach router through several “hops” on other devices located close to the router.
You can read more about Thread on Openthread site.
For this project, I’ve used Skylab SKB-501 module. This module is declared as Bluetooth module, but it is ok because it have nRF52840 chip inside, so we can just erase chip and load our own (Thread) firmware.
Here is links and description of things, that you’ll need:
- SKB-501 module itself. On moment of writing this article, it was probably cheepest way to get nRF52840(except buying chips and soldering them on your board).
- USB-UART converter to link your device to PC.
- ST-Link adapter. You can probably use anoter SWD/JTAG adapter, I’ve used ST-Link(v2) just because I had one on my STM32F4 Discovery board.
- Nordic PCA10056 devboard. You’ll need it only once, for resetting nRF52 chip on SKB-501 module. Maybe you’ll able to do this without expensive Nordic devboard, but I don’t tried to do reset via ST-Link just because I had only one SKB-501 and I’ve resetted it with Devboard already. Maybe I’ll order some more SKB-501’s in future and update this article…
- Small protoryping board.
Assembling here is very simple: you just need to connect power, UART lanes (for communication with PC) and SWD lanes (for programming module itself) to SKB-501 module.
UART and power lanes should be connected to USB-UART converter. Please make sure to connect power to 3.3V of converter, not ot 5V!
Connection of SKB-501 to USB-UART is very simple:
(USB-UART) – (SKB-501)
RX (input) – P0.06
TX (output) – P0.08
GND – GND
3.3V – VCC (through diode, to not power up USB-UART from batteries, if you’ll use them)
For SWD, I’ve used two 2-pin headers, but you can use 4-pin. Pins is – SWCLK, SWDIO, reset and GND. Actually, I’ve never used reset pin, so you can omit it.
Also, I’ve soldered 6,3V, 680 uF electrolytic capacitor between GND and VCC of Nordic chip, and connected 2 pin header to that capacitor for connecting batteries.
After soldering, connect USB-UART converter to USB port of PC. If all soldered correctly, you’ll see Bluetooth device with name nRF52 appear. You can use “nRF connect” Android software for scanning Bluetooth devices.
Preparing required software
To compile firmware and work with nRF52 chip, we will need a set of software. Let’s install/prepare it:
OpenOCD – is free software that will allow us to “talk” with nRF52840’s SWD interface via STLink adapter.
While writing this article, I’ve tried several versions of OpenOCD, and here is one that works with Nordic chips and STLink:
#git clone https://github.com/ntfreak/openocd.git
nrfjprog and Jtag libs
To unlock chip with Nordic’s devboard, we will need nrfjprog software. Please download it from Nordic site, and then unpack archives.
Resetting nRF52 chip
Method 1 – using Nordic 10056 board.
First, connect your board to Nordic’s devboard P20 connector.
Connections should be:
(your board) – (Nordic board)
SWDIO – SWDIO (4)
SWCLK – SWDCLK(5)
GND – GND_DETECT (8)
+3.3V – VTG (3)
RESET – RESET (7)
Please note, that connecting +3.3V to Nordic’s devboard is essential for Nordic’s internal debugger – with this connection, it recognizes that some external thing is connected, and it switches from on-board nRF52840 to external one.
When you’re done with connections, run:
Thing is – “–recover” command erasing all non-volatile memory and removes readback protection bit. After this procedure, our chip can be programmed/debugged with OpenOCD.
Method 2 – using OpenOCD (Without Nordic devboard).
While method 1 is reliable and easy, it is have drawback – you’ll need expencive Nordic’s devboard to use nrfjprog. Theoretically, same thing can be done with OpenOCD. But, when I’ve tried it, it dosn’t work for me, so I’am putting it here more like placeholder to update it later, when I’ll finally able to do this.
So, theoretically, you just need to erase all chip:
#../src/openocd -d2 -f interface/stlink-v2.cfg -f target/nrf52.cfg -c init -c 'reset init' -c halt -c 'nrf5 mass_erase'
When chip is resetted, we can compile our Thread example, and upload firmware to board.
Compiling Openthread firmware for nRF52840
There is a really good manual at Openthread’s site, and here I’ll be compile firmware according to that manual.
We will just need to do little customizations – disable hardware flow control for UART. Without this, our board will not communicate via UART.
So, firstly, let’s clone code:
#git clone https://github.com/openthread/openthread
Now, edit the configuration file:
In that file, you’ll need to completely remove this string:
#define UART_HWFC_ENABLED 1
After ediding config, you can build firmware:
#docker pull openthread/environment:latest
#docker run -it --rm -v $PWD:/openthread-fw-new/ openthread/environment bash
#make -f examples/Makefile-nrf52840
After build, prepare .hex file for loading:
#arm-none-eabi-objcopy -O ihex ./output/nrf52840/bin/ot-cli-ftd ./ot-cli.ftd.hex
Now, we can exit Docker, connect and flash our board.
Copy hex file to some place with short path:
cp ./ot-cli.ftd.hex /tmp
Ok, now we have unlocked Nordic board and firmware – it is time to flash it!
First, connect your board to STLink. I’ve using STLink from STM32F4 discovery board, but any STLink should be good here.
Connections is very simple:
(Your board) – (STLink)
SWDCLK – SWCLK
SWDIO – SWDIO
GND – GND
Please note, we don’t connect reset pin here.
Also, your nRF52840 should be powered, obviously, you can connect USB-UART converter on your board to USB port to get 3.3V from it, or connect batteries to your board.
Now, let’s flash it:
<OpenOCD_Dir> – is a directory where you’re compiled OpenOCD for Nordic in previous steps.
#../src/openocd -d2 -f interface/stlink-v2.cfg -f target/nrf52.cfg -c init -c 'reset init' -c halt -c 'nrf5 mass_erase' -c 'program /tmp/ot-cli.ftd.hex verify exit'
If all will be good, you’ll see lots of debug messages, and something like this at end of the output:
** Programming Finished **
** Verify Started **
** Verified OK **
If openocd fails with errors like “open failed”, try to run it under root user.
Now, you can connect to your board with picocom. First, disconnect your board from USB, STLink and batteries, and wait for couple seconds. Then, connect USB-UART converter to your PC, and run:
#picocom -b 115200 /dev/ttyUSB0
Press enter several times. If you’ll see “>” at begining of new string – this is it! You’re succesfully flashed Thread CLI to your board!
You can type “scan” for list of networks in visible range (most likely for today, it will be empty), or type “help” to see what comands are available.
So, this is it for now.
In future, I’ve planning to make some sensors and light devices for my smart home on this Nordic’s nRF52840’s.
I hope, you’re endjoyed this article and you’re have some curiosity in Thread and other wireless protocols after reading this post.