Home | pfodApps/pfodDevices | WebStringTemplates | Java/J2EE | Unix | Torches | Superannuation | | About Us
 

Forward Logo (image)      

BLE in Arduino
BlackMagic Probe Programmer

by Matthew Ford 6th January 2021 (originally posted 14th December 2018)
© Forward Computing and Control Pty. Ltd. NSW Australia
All rights reserved.

Using the BlackMagic Probe
to Program BLE devices
with Arduino


Introduction

This tutorial describes an alternative nRF52 Arduino based programmer using the BlackMagic Probe. A programmer based on the less expensive Particle Debugger is described on the main page, Easy Very Low Power BLE in Arduino

Quick Start

Wire up the programmer, install the low power support and use the free pfodDesigner to create a custom control menu/data logger and generate the low power sketch for pfodApp to connect to and display the controls and chart and log the data.

Boards and Programmers

Although Sandeepmistry's original nRF5 Arduino add-on supports a number of nRF51/52 boards, this project only supports nRF52832 chips. To get the lowest power you need to use a board with just the nRF52832 chip since any extra devices like accelerometers use more power.

The test device is a RedBear Nano V2. This is small enough to be used for real devices. The tutorial will be updated later to also cover using stripped down modules like the RedBear BLE Module or the SkyLab Bluetooth Module SKB369 Although the Nano V2 board includes a low power regulator, an external regulator is used so that the bare chip boards can be programmed also. This external regulator is also needed to ensure start up from very low current sources.

In this tutorial the nRF52832 chip is programmed using the general purpose BlackMagic Probe. This probe also supports low level debugging if you ever decide to delve deeper into the Nordic SKD code, but all debugging done in this project just uses Arduino Serial print statements.

Tutorial Outline

Building the programming/test board
Installing the low power support for the nRF52832 in Arduino
How to Code for Low Power
Delays are evil. Use timers instead.
Measuring the Supply Current – Blink_millisDelay.ino

Building the programming / testing board

The project use a BlackMagic V2.1 Probe to program and debug (via Arduino Serial prints) the nRF52832 chip. The BlackMagic Probe also supports GDB single step source level debugging, but that was not used in developing this low power library and you should be able to use the usual Arduino print statements to do any debugging you need for your sketch.

There is the schematic for the programming / testing board (pdf version).

Parts List

Approximate cost as at Dec 2018, ~US$122 including NanoV2, USB cables and USB supply (plus shipping)

BlackMagic Probe V2.1 ~ US$60
USB Cable - A/MicroB – 3ft ~US$4 for the BlackMagic Probe
SWD (2x5 1.27mm) Cable Breakout Board ~US$2 for BlackMagic programming cable

SparkFun USB Mini-B Breakout (or Adafruit's Mini-B version) ~US$2 (OR you can use a Micro-B break out board and matching cable instead)
USB cable - A/MiniB – 3ft ~US$4 to suit SparkFun' Mini-B breakout board

USB supply ~ US$7 to power the nRF52832 chip via the breakout board and MAX8881EUT33 regulator

MAX8881EUT33+T 3v3 Digikey MAX8881EUT33+TCT-ND ~US$2
SparkFun SOT23 to DIP Adapter ~US$1

Redbear NanoV2 ~ US$17

2 x 10uF 25V Ceramic capacitors e.g. Digikey 445-7705-1-ND ~ US$2 mounted between tracks on copper side of vero board
1 x 0.1uF 50V Ceramic capacitor e.g. Digikey 478-10836-1-ND ~ US$0.5 mounted between tracks on copper side of vero board
1 x 0.1uF Ceramic capacitor e.g. Digikey 478-2472-ND ~ US$0.5
1 x 330R 1/4W 1% resistor e.g. Digikey S330CACT-ND ~US$0.1
1 x 1K5 1/4W 1% resistor e.g. Digikey RNF14FTD1K50CT-ND ~US$0.1
470uF 25V Low ESR capacitor e.g. Digikey 399-6127-ND ~ US$1
2 x 6 pin and 2 x 5pin female headers sockets e.g. Sparkfun PRT-11269 ~US$2 cut down the 8pin header to use for the BlackMagic programming breakout board
6 x 6 pin male header pins e.g. Sparkfun PRT-00116 ~US$1.5
female to female jumper e.g. Adafruit ID: 1950 ~US$2
3mm x 12mm nylon screws, e.g. Jaycar HP0140 ~AUD$3
3mm x 12mm nylon tapped spacers, e.g. Jaycar HP0924 ~AUD$10
Vero board (strip copper) e.g. Jaycar HP9540 ~AUD$5
plastic sheet to insulate bottom of vero board e.g. cut out of plastic lid

The programmer/test board was constructed on vero board. The MAX8881 regulator provides 3.3V from the 5V USB supply. The Nano V2 has its own on board regulator, but if you want to be able to program bare modules like the SkyLab that don't include a regulator then you will need the MAX8881, or similar. The MAX8881 has a supply current of 3.5uA (typical), similar to the Nano V2 on-board regulator. When running your low power BLE project from a very low current source, then the Power OK (POK) and Shutdown (SHDN) pins on the MAX8881 can be used to hold off the nRF52 until the 470uF supply capacitor has charged up enough current to supply the chip's start up current surge.

In this circuit, the 470uF low ESR (Equivalent Series Resistance) is used to filter the chip's current pulses when it transmits. This allows you to use a multimeter to get a good idea of the average supply current. The supply current is measured across two shunt resistors in the GND line. For high currents, short out terminals 1 to 3 to remove the resistors. For medium currents short out terminals 2 to 3 to remove the 1K5 resistor. For the very low currents, that will be achieved in this tutorial, leave the terminals 1,2,3 open. The multimeter, or oscilloscope, is connected across the Current Monitor Test Points. If using an oscilloscope, connect the GND clip lead to the USB GND side of the shunt resistors.

The BlackMagic Probe is connected to the NanoV2 via a programming/GDB debugging header board on ribbon cable and via TX/RX serial connections for Arduino Serial debugging. The BlackMagic Probe does not supply power to the Nano V2, but when connected does draw some current from the Nano V2 supply, so when making very low current measurements, you need to unplug the programming header board and remove the four (4) Serial links (Vref, Tx, Rx, GND)

The BlackMagic Probe uses the Vref connection to sense the supply voltage level of the chip being programmed. In this board the NanoV2 was plugged in for ease of programming and then transferring to the final circuit. In general to program your nRF52832, you need some way of connecting four (4) leads (Vref, SWCLK, SWDIO, GND) to the chip to be programmed.

Installing the low power support for the nRF52832 in Arduino

This project builds on the Sandeepmistry's Arduino Core for Nordic Semiconductor nRF5 based boards and his BLEPeripheral library, but has been modified to add low-power support and to only work with Nordic's nRF52 series. It was tested using a Redbear NanoV2, nRF52832 board. The modified library provides a number of low power utilities, like sleep, lp_timer and lp_comparator. It also provides a general purpose low power Nordic UART BLE service, lp_BLESerial, which works with Nordic's free apps and with pfodApp. The free pfodDesigner app can be used to generate a low power Arduino sketch that will display a custom menu or graphical UI using pfodApp on your Android mobile with no Android programming required. See the pfodDesigner tutorials for more details on creating menus, sub-menus, charts and graphical UI's.

If you don't want to use pfodApp, you can still use the pfodDesigner to design a menu and then program the menu cmds into UART control in Nordic's nRF Toolbox. This project also uses Nordic's nRF UART v2.0 app for testing.

Download and Install the Arduino IDE

Here Arduino version 1.8.7 is used on a Windows 7 PC.

Install Sandeepmistry's Arduino Core for Nordic Semiconductor nRF5 based boards

1. Start the Arduino IDE
2. Go into File → Preferences
3. Add https://sandeepmistry.github.io/arduino-nRF5/package_nRF5_boards_index.json as an "Additional Board Manager URL"
4. Open the Boards Manager from the Tools -> Board menu and install "Nordic Semiconductor nRF5 Boards". This project used version 0.6.0. Other version will have their own set of features and bugs.

NOTE: During installation it takes the Arduino IDE a few minutes to extract the tools after they have been downloaded, please be patient.

Install the BlackMagic Probe driver

This project uses the BlackMagic Probe which needs a driver installed if you are using Windows 7, 8 or 9.

1. Download .inf file drivers from blacksphere github
2. Plugin Black Magic Probe
3. Point the installer to the folder containing blackmagic.inf

You should now have two COM ports. On Window 7, click the Windows button (bottom left of your screen) and type Device Manager. Click on “Device Manager” in list.

If you have previously programmed a Teensy board, this driver install will not work. You need to unplug the BlackMagic Probe, right click on the Com ports and uninstall the existing drivers, the plug the BlackMagic Probe back in and right click in device manager on BlackMagic GDB server port and choose update driver software and browse to the directory where you have downloaded (and unzipped) the BlackMagic.inf file, click next and when prompted let windows install the driver. Do the same for the UART Com port.

Install the pfod_lp_nrf52 hardware support.

1. Download the pfod_lp_nrf52.zip file.
2. Start the Arduino IDE, Open the File → Preferences window an at the bottom find the directory where the
preferences.txt file is stored. In Window's 7 you can click on that path to open the directory in the Explorer.
3. Open the
packages sub-directory and then the sandeepmistry sub-directory which contains the hardware and tools directories.
4. Delete the
hardware directory.
5. Unzip pfod_lp_nrf52.zip to the
sandeepmistry directory to install the pfod low power support. This will install the modified hardware directory.
6. Close and restart the Arduino IDE.
7. Open the Tools → Board and scroll down to find the
pfod low power nRF52832 boards

Note: the boards with * against them do not use the selected Programmer. Instead they use their own pre-configured programmer. For example if you select the board *RedBear BLE Nano 2, and then use the IDE upload Arrow button, Arduino will try and program the board using RedBear DAP-Link programmer rather than the Black Magic Probe programmer. The Arduino IDE menu Sketch → Upload Using Programmer can be used to override this configuration.

Here we are using the BLENano2 via Selected Programmer board, and selecting Black Magic Probe (GDB) as the programmer.

Connecting the BlackMagic Probe to the nRF52832 chip.

In order to program the nRF52832 you need to power it and connect the BlackMagic Probe VDD, GND, SWCLK and SWDIO pins to the chip. For debugging via Arduino print statements you also need to connect TX and RX to a pair of UART pins on the chip.

The small development board built above provides the 3.3V power, current measurement shunt resistors and serial debugging and can quickly completely disconnect the BlackMagic Probe. The BlackMagic Probe also provides code level debugging via this board, but it is not used in this project.

Programming the Nordic Softdevice

Before you can run a sketch you need to first load a 'softdevice' into the nRF52832 chip. The project uses the s132 softdevice, s132_nrf52_2.0.1_softdevice.hex (http://www.nordicsemi.com/eng/content/download/95151/1606944/file/s132_nrf52_2.0.0.zip) The pfod_lp_nrf52.zip already includes s132, so you don't need to download it from the Nordic website. However you still need to program it into your nRF53832 chip.

Plug in the NanoV2 module and the BlackMagic programming cables and Serial jumpers and plug in the USB power supply to power up the NanoV2. Make sure the Current Shunt Shorting link in in place so the current sense resistors do not limit the supply volts while programming.

Start the Arduino IDE and make sure BlackMagic Probe (GDB) is the selected programmer and that the COM port is lower numbered one, i.e. COM113

Note Use the nRF52 Flash SoftDevice option half way down the Tools menu. Do not use the Burn Bootloader option at the bottom. The soft device flashed is the one selected under the Softdevice: The pfod_lp_nrf52.zip has pre-configured S132 as the softdevice for all the nRF52832 Boards in the pfod low power nRF52832 menu section.

NOTE: Arduino sometimes looses track of the COM ports. If you have problems uploading your sketch, close Arduino, un-plug the USB cable from the computer, restart Arduino and plug the USB back in.

How to Code for Low Power

The trick to getting a really low power solution is to do nothing most of the time, use high value external pull-up/pull-down resistors on inputs and don't have any extra components.

Normally in Arduino you put all your action code in the loop() method which is then called repeatedly by Arduino, but this means most of the time the processor is just spinning around doing nothing.

Consider the loop() method in the Arduino Blink example (File → Examples → 01.Basic → Blink)

int led = 13;

// the setup routine runs once when you press reset:
void setup() {                
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);     
}

// the loop routine runs over and over again forever:
void loop() {
  digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
  delay(1000);               // wait for a second
  digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
  delay(1000);               // wait for a second
}

Arduino just runs this loop() code for ever, setting the led on, delaying for 1sec and then turning it off.

Delays are evil. Use timers instead.

Looking inside the delay() method you will see

void delay( uint32_t ms ) {
  if ( ms == 0 ) {
    return ;
  }
  uint32_t start = millis() ;
  do {
  } while ( millis() - start < ms ) ; 
}

The do{ }while loop just spins using up processor time, and power, until the millis() counter has incremented by ms This is just a waste of time and prevents your sketch dealing with any triggers / inputs that occur.

Don't use delay()

For normal Arduino coding you should use a timer library, like millisDelay. See How to code Timers and Delays in Arduino for all the details.

Rewriting Blink.ino using millisDelay gives Blink_millisDelay.ino

#include <millisDelay.h>
// Pin 13 has an LED connected on most Arduino boards, including NanoV2
int led = 13;
bool ledOn = false;
millisDelay ledDelay;
const unsigned long DELAY_TIME = 1000; // mS == 1sec

// the setup routine runs once when you press reset:
void setup() {
  // initialize the digital pin as an output.
  pinMode(led, OUTPUT);
  ledDelay.start(DELAY_TIME);
}

// the loop routine runs over and over again forever:
void loop() {
  // do other stuff here
  if (ledDelay.isFinished()) {
    ledDelay.repeat(); // start a repeat the delay
    ledOn = !ledOn; // toggle state
    if (ledOn) {
      digitalWrite(led, HIGH);   // turn the LED on (HIGH is the voltage level)
    } else {
      digitalWrite(led, LOW);    // turn the LED off by making the voltage LOW
    }
  }
}

Blink_millisDelay does not stop your loop() from running, rather it just checks each time if the ledDelay has timed out.

Upload the Blink_millisDelay.ino sketch on to the NanoV2, via the BlackMagic Probe.

Sometimes Arduino looses connection to the programming COM port. In that case try unplugging the BlackMagic USB cable from the computer and plug it in again. If that does not work, close the Arduino IDE and restart it.

Measuring the Supply Current

Having programmed the Blink_millisDelay.ino sketch, lets measure the supply current.

To measure the supply current

Current shunt shorting link inserted while programming the NanoV2


1) With the current shunt shorting link inserted, upload the sketch onto the NanoV2.
2) Remove the USB cable supplying the Nano (black cable above) to reset the nRF52 (the NanoV2) after the programming is finished. This is necessary in order to measure the correct supply current.
3) Remove the BlackMagic programming header and the Serial links (as shown above)
4) Move the current shunt shorting link so that it shorts out the 1K5 resistor leaving only the 330R resistor in the ground line. For very low current measurements just remove the link completely
5) Temporally short out the 330R resistor while re-connecting the USB power, then remove the temporary short to measure the voltage drop across the 330R resistor.

For the Blink_millisDelay.ino, sketch the supply current is about 6mA (The led on the NanoV2 draws a fraction of a mA). The voltage measured across the 330R current shunt resistor is ~2V. The voltage measured across the 330R current shunt resistor is ~2V, So the supply current is
2 Volts / 330 ohms == 0.006 Amps (i.e. 6mA)

The 2V across the current shunt resistor means there is only ~3V to run the NanoV2 . If the supply is much higher, i.e. 8mA, there not enough voltage left to power the chip. This sets the upper limit on the supply current that can be measured with a 330R shunt resistor.

NOTE: If the voltmeter reads 2.6V across the current measuring resistors than the nRF52 chip is not running, but is stuck in start up due to insufficient supply current.

Issue with NRF51 unexpectedly entering debug mode

NOTE: The nRF52 can un-expectedly enter debug mode while running, due to noise on the SWCLK line, resulting in a few mA of supply current instead of 100uA. See Issue with NRF51 unexpectedly entering debug mode
The solution appears to be to add a small value resistor say 470R and a small capacitor say 1nF in parallel between SWCLK pin and GND as close to the chip as possible and to disconnect any long traces connecting to SWCLK, after you have programmed the chip.
As a last resort you might even consider connecting a spare GPIO on the nRF51822 directly to SWCLK, and set the spare GPIO to output low after power up. This will prevent re-programming, so you might want to have some means for the application to release the pinOR cut the board connection between the GPIO and SWCLK

I tested a 1nF between SWCLK and GND and was unable to program the chip with the capacitor in place, but the chip continued to run and could be connected to. Even after removing the capacitor, the chip would not program, either with BlackMagic Probe or RedBear's DAP V1.5 programmer. Re-flashing the Softdevice worked and made the chip programmable again. So do not add the suppression capacitor/resistor until after you have finished programming. My final solution is to connect a wire from SWCLK to GND, to ground it, after programming.

nRF52 Low Power Optimizations

You will see in the on-line nRF52 forums references to a) enabling the nRF52 DC to DC converter and b) disabling the Serial UART, to reduce supply current.
None of the sketches here use either of these optimizations.
The DC to DC converter option requires extra components, which the NanoV2 has but, which 'bare' nRF52 boards, like SkyLab, will not, so these sketches don't use it. As for disabling the UART, with this library, provided your sketch does not call Serial.begin(), the UART does not use any noticeable current, compared to the 100nA already being used. Finally, testing with current limited supplies shows that either of these optimizations result is higher start up current. See Very Low Power BLE, Part 2 – Power Supplies (under construction) for the details.

Conclusion

This tutorial has shown how to build a nRF52 programmer for Arduino using the BlackMagic Probe. The main page, Easy Very Low Power BLE in Arduino, uses the less expensive Particle Debugger and covers low power programming of the nRF52.

Supply currents of less than 100uA are easily achievable while keeping the chip active to make connections and send/receive data.

The free pfodDesigner lets you design menus/sub-menus, plot and log data and then generate the low power Arduino sketch for you. Connecting with pfodApp displays the menus and data while the nRF52 chip uses <100uA

No Android programming is required. pfodApp handles all of that.
No Arduino coding is required. The free pfodDesignerV2 generates complete low power sketches.

Roll Your Own BLE Service

In most case the general Nordic BLE UART service is all that you will need to collect data and control your device. However you can use the underlying BLEPeripheral class to define your own services and characteristics, but you will also need to code an Andriod / iOS app to recognize it. If you want to use one of the 40 or more 'standard' BLE services , for which apps are already available, you will need to code that service and characteristics and format the data, in your sketch. Covering these services is beyond the scope of this tutorial.



AndroidTM is a trademark of Google Inc. For use of the Arduino name see http://arduino.cc/en/Main/FAQ


The General Purpose Android/Arduino Control App.
pfodDevice™ and pfodApp™ are trade marks of Forward Computing and Control Pty. Ltd.




Forward home page link (image)

Contact Forward Computing and Control by
©Copyright 1996-2020 Forward Computing and Control Pty. Ltd. ACN 003 669 994