Programming an nRF52 on a Mac

 

Several months ago, I went to promotional conference for a microcontroller on a whim. I’d heard about it the night before, and after some investigation I found out I could get a free development board out of it. Did somebody say free??? Yeah, that’s me. So I got the board and promptly did nothing with it, because electronics development chains are still the apple of Windows’ eye (see what I did there?)

Devboard

Anyway, if you want to develop for a Nordic nRF52 Dev Kit on a Mac, I’m going to show you how to setup your machine, how to start a quick project, and load it onto the board.

There are 3 steps we’re going to go through:

  1. Getting the development tools installed
  2. Getting and updating the SDK
  3. Building and installing an example project.

My hope is that this is enough to get anyone developing for the board. I won’t be covering debug tools (because I haven’t figured out how to yet) or more general programming things like How do I write a Makefile?, or How do I utilize the SoftDevice to get Bluetooth? I probably don’t know the answers to these questions, but this is what’s worked for me*.

1. Getting the Development tools installed

The nRF52832 (that’s the chip we’re working with) is made by a company called Nordic Semiconductor. It’s an ARM CoreM4f. That’s a type of ARM processor. That means that the tools we use to make programs for our computers (which are mostly x86) won’t work. Nordic’s first choice is Keil, which is an ARM compiler that’s REALLY expensive and doesn’t run on a Mac (two strikes)

Instead, we’re going to use GCC, a reliable, open source compiler. There are lots of pages about installing ARM-GCC on Macs, even via Homebrew (which is awesome), but they’re all pretty far out of date, so we’ll have to do it by hand:

First, download the most recent gcc-arm-none-eabi. For me it was the release from May 3rd, 2016. Once you have it, extract it out into a directory in /usr/local (You don’t actually have to put the compiler here, but it seemed the most appropriate place to me.)


aeiche $ mkdir /usr/local/gcc-arm aeiche $ tar -xjf gcc-arm-none-eabi-5_3-2016q1-20160330-mac.tar.bz2 -C /usr/local/gcc-arm

Beyond the compiler, we need a tool to get the compiled program onto the chip itself. Nordic has a download for this one as well. The program is called nrfjprog, and you can download it here (look for the nRF5x Command Line Tools – OS X link.)†

After you download that, expand the zip file and inside you’ll find the nrfjprog directory. Move that directory into that same /usr/local directory you made earlier. This is for convenience, you can put it wherever you want.

Lastly, you’ll want to add the nrfjprog program to your path. This will allow you to use it from anywhere. We do this by adding it to a .bash_profile file. If you don’t already have one, make one in with any plain text editor. Be sure to include the . at the front.

We need a line that will add nrfjprog to our path. We do this (in bash) by exporting the PATH variable and adding on our directory. If our .bash_profile already looks like this:


export PATH=/usr/local/bin:/usr/local/sbin:$PATH

Then we just need to add the directory path


export PATH=/usr/local/bin:/usr/local/sbin:/usr/local/nrfjprog:$PATH

Be sure to separate each path with a colon :. Now you should be able to close your

Next (I know, why is there so much!?) We need to install the appropriate drivers for the NRF52 Dev Kit. This one is easy though (especially on a Mac)

Go to https://www.segger.com/jlink-software.html, and download the Software and documentation pack for Mac OS X. It’s a pkg file, open it, install, and you’re done!

Now, as a sanity-check, open a terminal and type nrfjprog --version

If you’ve done everything right, you should see the following (or something similar):


aeiche $ nrfjprog --version nrfjprog version: 8.5.0 JLinkARM.dll version: 5.12g

Congratulations! You have the compiler and flasher installed. That was pretty easy, right?

2. Installing the SDK

The nRF5x chips have an SDK that makes developing for them much much easier. Instead of having to slog through a lot of setup, Nordic has already done that work for you so you can do the fun stuff. The current SDK is the nRF5 SDK. This applies to the 52 series as well as the older 51 chips. You can find it here under the downloads tab. Download that. As of this writing, the SDK is version 11.

You can put the SDK where-ever you like. I keep mine in a directory off my home called Development/nRF52/ When I expanded the download, the name was nRF5_SDK_11.0.0_89a8197, I just renamed it SDK. It shouldn’t make a difference. 

Once you have it where you want it, we’re going to make a couple of simple changes. In the SDK you should be able to find the file components/toolchain/gcc/Makefile.posix. This file is going to tell the SDK where to find a couple of components.

Now, in your terminal get the version of arm-gcc:


aeiche $ /usr/local/gcc_arm/gcc-arm-none-eabi-5_3-2016q1/bin/arm-none-eabi-gcc --version arm-none-eabi-gcc (GNU Tools for ARM Embedded Processors) 5.3.1 20160307 (release) [ARM/embedded-5-branch revision 234589] Copyright (C) 2015 Free Software Foundation, Inc. This is free software; see the source for copying conditions. There is NO warranty; not even for MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.

Now open the Makefile.posix you saw earlier. Here, we’ll want to update the GNU_INSTALL_ROOT and the GNU_VERSION to whatever we just got. My SDKs file said this:


GNU_INSTALL_ROOT := /usr/local/gcc-arm-none-eabi-4_9-2015q1 GNU_VERSION := 4.9.3 GNU_PREFIX := arm-none-eabi

and I changed it to this:


GNU_INSTALL_ROOT := /usr/local/gcc_arm/gcc-arm-none-eabi-5_3-2016q1/ GNU_VERSION := 5.3.1 GNU_PREFIX := arm-none-eabi

Boom! That’s 2 steps down! You’re making real progress now! Well done!

3. Building and flashing an example project.

You made it to the fun stuff! This one is going to be quick, because all we’re going to do is compile and install something that already exists.

There are lots and lots of example applications included in the SDK. It’s really great. My preferred starter app is blinky. In the SDK, you should be able to find it at examples/peripheral/blinky. It’s a simple application that will blink all four LEDs on the nRF52 board in sequence. In this directory you’ll find the source, license, an already compiled hex file, and several directories that begin with pca. The pca directories are board configurations. My Dev Kit has a sticker on it that kindly tells me which board I’m looking it.

sticker
If we dive further into the board directories, we find the following:


aeiche $ ls pca10040 blank s132

These two directories are virtually identical. The difference is that that s132 version is meant to be compiled and flashed alongside a SoftDevice, which is a really cool way Nordic uses to provide bluetooth for your application while letting you have more fun. We’re going to ignore it however, and just go the old fashioned route, choosing blank. Navigate down to blinky/pca10040/blank/armgcc/

Here is where the magic happens. Time to make our app! Type:


aeiche $ make

You should get something like:

aeiche$ make
rm -rf _build
echo Makefile
Makefile
mkdir _build
Compiling file: system_nrf52.c
Compiling file: main.c
Compiling file: nrf_delay.c
Assembly file: gcc_startup_nrf52.s
Linking target: nrf52832_xxaa.out
Preparing: nrf52832_xxaa.bin
Preparing: nrf52832_xxaa.hex

text data bss dec hex filename
2408 112 28 2548 9f4 _build/nrf52832_xxaa.out

It built! Now lets install it.

aeiche $ make flash

Now you should get soemthing like:

aeiche$ make flash
Linking target: nrf52832_xxaa.out
Preparing: nrf52832_xxaa.bin
Preparing: nrf52832_xxaa.hex

text data bss dec hex filename
2408 112 28 2548 9f4 _build/nrf52832_xxaa.out

Flashing: _build/nrf52832_xxaa.hex
nrfjprog --program _build/nrf52832_xxaa.hex -f nrf52 --chiperase
Parsing hex file.
Erasing code and UICR flash areas.
Applying system reset.
Checking that the area to write is not protected.
Programing device.
nrfjprog --reset -f nrf52
Applying system reset.
Run.

And if everything worked, you should be looking at BLINKING LEDS, OH MY GOSH, THIS IS THE BEST DAY EVAR!

Next Steps

At some point in the future, I intend to write a full article on how to write a custom program to get people started. In the meantime, here’s a helpful hint: In several of the subdirectories in examples you can find projects labeled template_project. These are basic projects that you can copy and start building. Start adding code to the main.c file and you’re off to the races

*As of this writing.

†According to the internet you can do this via homebrew in a more automated approach. I’d already written this when I discovered it. Either way should work.

32 comments
  1. Thanks for this fantastic setup guide! Exactly what I needed. Looking forward to your article about writing a custom program.

  2. Thanks for the tutorial. I have followed your steps. Everything worked. But while flashing the blinky example, I am getting the following error. My Development kit is connected to USB port. Any help on how to debug this issue? Thanks. Babu
    nrfjprog –program _build/nrf52832_xxaa.hex -f nrf52 –chiperase
    ERROR: There is no debugger connected to the PC.
    make: *** [flash] Error 41

    • Hi Babu,

      This error looks reminiscent of not having the Jlink tools installed. I would make sure you installed the JLink tools from Segger and try again. It’s the Software and Documentation Pack: https://www.segger.com/j-link-software.html
      nrfjprog utilizes the libraries provided by segger for the JLink onboard the Development Kit.

      • Thanks, Aaron. I have already downloaded the software. When I use JLinkExe, I get the following error.

        /Applications/SEGGER/JLink/JLinkExe ; exit;
        SEGGER J-Link Commander V5.12g (Compiled May 27 2016 16:55:14)
        DLL version V5.12g, compiled May 27 2016 16:55:08

        Connecting to J-Link via USB…FAILED: Can not connect to J-Link via USB.

        • Hmm – that is unexpected. First things first: Are you sure the board is plugged into your Mac, and that the switch on the board is “on”?

          • Thanks, Aaron. Looks like the board I received is defective. I am getting a replacement in a week time. I will give it a try after receiving the new board and let you know.

  3. Hi Aaron,
    Thank you so much for making it an absolute no-brainer journey to start nRF52 development on the Mac. I am sure it saved me tens of hours to trial and error to get things set up. I am on Sierra and there was absolutely no issue encountered. I’ve got now next to me 4 happily blinking LEDs 🙂 🙂
    //Ed

  4. Hello,

    thanks for the article. Really helped me today to successfully make installation on my macbook.

    One question, which IDE is best for developing apps on nrf52? I’m new to this so I’m looking for best options.

    • Hi Tarik,

      I’m glad the article helped. The best IDE depends a lot on what you want to do. Kiel is the go-to recommended Nordic solution, but it’s expensive and Windows-only.

      Personally, I use Atom for writing code, and compile and deploy via the command line. I don’t have a solution yet for debugging on-the-fly. If you find something that works well for you, please let me know.

      • I’ll maybe go Visual Studio Code just because it’s more friendly to C.
        But I use ATOM too as my web development tool and it’s awesome 😀

    • Additionally – I will note that one IDE I particularly like (though it’s not a perfect match) is QtCreator. It has a “Bare Metal” kit that you can use for deploying to devices.

  5. Thanks very much for this guide–it’s been really useful. I ran into one problem at the end where I had the error “ERROR: There is no debugger connected to the PC. make: *** [flash] Error 41”, but changing the micro usb cable that I was using fixed this. One thing that I noticed is that when I was connected to the board via the dud cable the power LED kept flashing; the LED was steady after it wa connected properly. Thanks 🙂

  6. WTF!?! My brain just exploded. This is quite possibly the most easy-to-use and well-written tutorial on getting ANY microcontroller set up right from opening the box to flashing the application. You sir, are my hero of the day. Well done.

  7. Hi Aaron,

    Thank you very much for detailed instruction for “Programming an nRF52 on a Mac. I got it work but had a small issue :

    nrfjprog 9.3.1 could not find libjlinkarm.dylib (6.12a) unless the library is in the same folder where you run nrfjprog.

    My Macbook Air has OSX Yosemite 10.10.5 – I can run “make” or “nrfjprog” only if I copied libjlinkarm.dylib into the same folder that I execute the make or nrfjprog.

    I set up PATH to run nrfjprog from anywhere but I could not tell nrfjprog to find the libjlinkarm.dylib

    I will get error message

    “ERROR: JLinkARM DLL not found. Please reinstall latest JLinkARM.”

    I appreciate any suggestion to fix this issue.

    Thank you.

  8. “We do this by adding it to a .bash_profile file” this is where the tutorial ended for me. No explanation what it is and how to do it. You just assume we all know that and keep going.

    Whats really amazing here, is that there are people who still think writing software in this way ( using command line and machines syntax) is the right way in 2017, because it feels more “pro”.

    I was so happy till I found out that this is not a real tutorial for having a real tool, but just to show that you know how to program a chip.

    Wish one day you will grow up and understand that humans should have tools for humans, even for writing machine code.

    • I can see how that wasn’t obvious. The .bash_profile is a file that OS X uses to save preferences in the terminal.

      It’s not clear to me what you were expecting. This tutorial was written to fill in a gap that exists in terms of loading code onto an NRF52 dev board from a Mac. I didn’t write it out this way to be “more pro”, or seem overly technical, these are the tools that exist, and the libraries that are provided by Nordic. Can you clarify what you mean by a “real tool” or a real tutorial?

      I agree that it would be wonderful to have tools that are easy and obvious for all kinds of people, but we’re limited by what manufacturers provide us.

  9. There are many other tools for humans, such as Segger for mac, Eclipse, Xcode(embedded) and so on.

    Anyway thanks for the work although even if I want I still can’t use it because of that .bash_profile thing. Maybe I am not “technical” enough.

  10. Hi Aaron,

    Many thanks for this post. It did work from the first time after I tried quite a few other posts without success.
    Could you please give us some insight on how to flash with a soft device?

    Many thanks,
    Cristian

  11. Thanks Aaron! I think this is probably the most concise guide I’ve seen for a microcontroller. There’s a few things I’m wondering though. (1) I have an nRF51 in addition to my nRF52. Are there any major changes in the set-up process for the nRF51? (2) Do you have any suggestions on what to do differently for flashing/config if I want to play with Bluetooth LE on these chips?

    • Thank you for your kind words. They’re much appreciated. You shouldn’t have to do much to get it to work on an nRF51 – the only difference is going to be the various linked header files. Those will take care of all the heavy lifting of compiling with the right stuff for the chipset. Nordic did a good job of abstracting that stuff out so you can just write code. Potentially you could even write the same code for both chips if the file will point everything to the right pins.
      As for getting the BT to work, that’s unfortunately a little more than I’ve learned at this point. IIRC, it’s all a soft device load. Nordic has some tutorials that show how to load the soft device – after that I believe you just see it as a port to write to/read from. It’s been a while since I’ve looked at it though.

    • Thanks Mark! And thank you for including this useful command for flashing the soft device, I think one of the things that really attracts people to the NRF5* series is the BT and the lack of needing to write your own drivers.

Add Comment

Required fields are marked *. Your email address will not be published.