BLE POC Tracking System – Implementation Notes – Programming the NRF51822 BLE Chip

I’ve recently completed an Honours Degree in Software Development and as part of this I spent a few months off and on working on a proof of concept tracking system utilising Bluetooth Low Energy (BLE) in order to track small asset tags. I believe this system could quite easily be scaled up to an industrial scale with further work but don’t personally plan to follow this up in the near future due to working on other projects. As such I figured I’d release a lot of what I have and hopefully it can help somebody else.

If you do something cool with this then please get in touch, otherwise do with it all as you wish.

This post is one of eight scheduled to be released over the next few weeks, all posts in this series may be found by selecting the tag below.

This post functions as a log of the steps required to implement this project in a practical manner, the goal is that somebody else with minimal knowledge may be able to follow it.

These set up steps may be specific to my configuration in certain cases, I have attempted to record all assumed setup below –

  • Using Raspberry PI 4

Some of the information found in this post was posted in the past due to not being able to find a single OpenOCD config for a Raspberry Pi 4 –

Programming the NRF51822 BLE Chip

The NRF51822 is a Bluetooth Low Energy chip which will be programmed to take in any serial BLE broadcasts and converts it to serial UART data that the ESP8266 WiFi chip can understand and convert it to MQTT.

First, we need to connect the NRF51822 chip to a Raspberry PI so that we can flash it with the required software.

Connect NRF51822 to Pi

Connect the NRF51822 chip to the Raspberry PI using Dupont connectors as this allows for easily plugging and unplugging the various connections.

The following pins needs to be connected to allow flashing the chip from the Raspberry PI –

Figure 9 Shows the relevant pins for connecting NRF51822 to Raspberry Pi
Figure 9 Shows the relevant pins for connecting NRF51822 to Raspberry Pi
NRF51822Raspberry PI 4
GND (Ground)GND Pin 6
VDD (Power)  3.3V Pin 1
SCLK/SWCLK (Input)GPIO 25 Pin 22
SDD/SWDIO (Output)GPIO 24 Pin 18
Connection Table

OpenOCD Setup

OpenOCD is an open-source on-chip debugger which is used within this project to reprogram the various chips using the Raspberry PI.

First, we need to install the latest OpenOCD repository, to do this use the following commands in a console –

sudo apt-get install git autoconf libtool make pkg-config telnet libusb-1.0-0-dev libusb-1.0-0
git clone git:// openocd
cd openocd/
./configure --enable-sysfsgpio --enable-bcm2835gpio
sudo make install

Once this has completed, we then need to set up a proper interface config file that will work for the Raspberry PI 4, we will need to first make a copy of one of the original config files and then modify it as required.

Creating a new Raspberry Pi 4 Interface Config

  1. Open the console.
  2. Type “cd /usr/local/share/openocd/scripts/interface” to navigate to the interface’s directory.
  3. Then type “sudo cp raspberrypi2-native.cfg raspberrypi4-native.cfg” which will copy the original PI 2 config file which we will use as a base config.
  4. Finally type “sudo nano raspberrypi4-native.cfg” to open the new config file in a text editor.

We will then need to modify the file so that it is identical to the example below –

# Config for using Raspberry Pi 4's expansion header
# This is best used with a fast enough buffer but also
# is suitable for direct connection if the target voltage
# matches RPi's 3.3V and the cable is short enough.
adapter driver bcm2835gpio
bcm2835gpio_peripheral_base 0xFE000000
# Transition delay calculation: SPEED_COEFF/khz - SPEED_OFFSET
# These depend on system clock, calibrated for stock 1000MHz
# bcm2835gpio_speed SPEED_COEFF SPEED_OFFSET
bcm2835gpio_speed_coeffs 236181 60
# Each of the SWD lines need a gpio number set: swclk swdio
bcm2835gpio_swd_nums 25 24

Once this is complete, we should be able to start OpenOCD and create a connection to the device.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.