I welcome you to this tutorial. In this tutorial I will show two things. Tutorial is separated to two parts. As first I will show how to develop application for nRF52840 MCU presented on Arduino Nano 33 BLE Sense without Arduino IDE and using tools offered my MCU vendor Nordic Semiconductor. In second part I will show how to debug this app in IDE using Raspberry Pi as a replacement for quite expensive JTAG/SWD probe. In first part I show how to develop application and in second part I show how to debug it. This tutorial is part of my RoadTest review about Arduino Nano 33 BLE Sense. All my opinions about this Arduino, sensors, MCU, documentation and software are presented in chapters with “Review” at the beginning of name. This tutorial is based on my experiences gained when roadtesting this Arduino. Following Table of Contents contains links to all chapters of my review.
Table of Contents
- Introduction
- Review of Development Board
- Review of Onboard Sensors
- Review of Microcontroller and BLE Module
- Review of Software
- Review of Documentation
- Tutorial 01: Accessing Sensor Values
- Tutorial 02: nRF52840 Application without Arduino IDE
- Part 1 - Developing nRF52840 Application (this article)
Tutorial 02, Part 1 – Developing nRF52840 Application
Installing Required Tools
We will need two things to install. First is development IDE. Nordic says that they support many IDEs, but opposite is true. Many IDEs support Nordic chips. Nordic says that preferred IDE is Segger Embeded Studio (SES), so I have used this IDE. Second thing to install is nRF5 SDK which is library for driving peripherals of nRF52840 microcontroller.
You can download SES from https://www.segger.com/products/development-tools/embedded-studio/. SES is proprietary IDE but in comparison with many other proprietary IDEs it allows free development of non-commercial solutions for free without any limits. Nordic also negotiated special license with Segger and you can develop even commercial apps with SES for free. You need register for receiving this license. Then you can develop even commercial application for Nordic chips using SES but only non-commercial apps for non-Nordic platforms. If you pay for license, you can develop whatever you want.
When you download and activate SES, you must add support for Nordic chips. Go to Tools > Package Manager ….
Scroll or search for Nordic and click nRF CPU Support Package. If you see No Action in nRF row, double click it. When you set this row to Install, proceed by Next.
Confirm summary page by Next.
Wait and confirm installation success.
Now you can create empty project, but it is very hard to import SDK into this empty project. I will do this differently.
Installing Nordic nRF5 SDK
First, you need to download nRF5 SDK from https://www.nordicsemi.com/Software-and-tools/Software/nRF5-SDK/Download#infotabs . On this webpage you download nRF5 SDK with some Soft Devices bundled. This Soft Devices are implementation of some wireless stacks and you can use them, but in this tutorial, I will not need any soft device.
Downloaded ZIP will contains other ZIPs. Extract nRF5_SDK to some folder on your disk. Path to this library is very important and it will be hardcoded on many places in your project. I moved extracted folder directly to C:\ for simplification.
In this folder there are folder named examples and this contains projects. There is no empty template. You must integrate sources from library to your app yourself. For simplification I recommend copy some example nearest to your need. In my case it will be UART example stored in examples/peripheral/uart folder.
Creating project and integrating nRF5 SDK
First, Create folder for your project somewhere on your disk. You need to copy following files to your new empty folder.
- Copy main.c file from examples/peripheral/uart from nRF5 SDK folder to root folder of your project. We will modify main.c later.
- Copy examples\peripheral\uart\pca10056\blank\ses\flash_placement.xml from nRF5 SDK folder to root folder of your project.
- Copy project file examples\peripheral\uart\pca10056\blank\ses\uart_pca10056.emProject from nRF5 SDK folder to your project.
- Rename uart_pca10056.emProject to name of project. I will use HelloUartWorld as project name in this tutorial.
- Copy examples\peripheral\uart\pca10056\blank\config\sdk_config.h from nRF5 SDK folder to root folder of your project.
pca10056 is development board with nRF52840. It is ideal source of some configurations and project settings because this board target the same MCU which is on Arduino Nano 33 BLE Sense.
We need to make multiple changes in HelloUartWorld.emProject file. I recommend using some editor like Notepad++ or vim with Find and Replace support for applying following changes to file.
- Change solution and project name in header to HelloUartWorld.
- Replace ../../../../../.. by path to your nRF5 SDK folder. In my case it is C:/nRF5_SDK_17.0.2_d674dde. Remember to do not include trailing / in both strings when replacing them.
- Remove ../../../config; from c_user_include_directories value
- Remove ../../..; from c_user_include_directories value
- Replace ../config; to.;
- Replace ../../../main.c to main.c
- Replace ../config/sdk_config.h to sdk_config.h
Now replace code of main.c to following code
#include <stdbool.h> #include <stdint.h> #include <stdio.h> #include "app_uart.h" #include "nrf_uart.h" #include "nrf_delay.h" #include "nrf.h" void UART_EventHandler(app_uart_evt_t * p_event) { if (p_event->evt_type == APP_UART_COMMUNICATION_ERROR || p_event->evt_type == APP_UART_FIFO_ERROR) { while (1) { __WFI(); } } } void UART_PutString(char* string) { while (*string) { while (app_uart_put(*string) != NRF_SUCCESS); string++; } } int main(void) { uint32_t status; app_uart_comm_params_t uartConfig; uartConfig.tx_pin_no = 33; uartConfig.rx_pin_no = 40; uartConfig.rts_pin_no = 0; uartConfig.cts_pin_no = 0; uartConfig.use_parity = false; uartConfig.flow_control = APP_UART_FLOW_CONTROL_DISABLED; uartConfig.baud_rate = NRF_UART_BAUDRATE_115200; APP_UART_FIFO_INIT(&uartConfig, 256, 256, UART_EventHandler, APP_IRQ_PRIORITY_LOWEST, status); if (status) { while (1) { __WFI(); } } while (1) { UART_PutString("Hello RoadTest!\r\n"); nrf_delay_ms(100); } }
Code is very easy. It initializes UART and prints “Hello RoadTest!” 10 times per second. It configures RX to port P1.08 which is D12 on Arduino and TX to P1.01 which is D11 on Arduino. As you can see both pins are not dedicated to UART but on nRF52840 you can map any feature to any pin you want. Now you can build code by pressing F7. At the bottom of window, you can see message that compilation was successful and utilization of FLASH and RAM memories.
Compiler created new Output\Debug\Exe folder for you and placed compiled elf and hex files there for you. We will need elf file in second part of this tutorial.
If you need to use more than UART in your app (for example you want to use UART, SPI and BLE at once) you must right click project, add existing sources and select .c files from nRF SDK folder which you need to use.
This is all for this tutorial. In second part I will show how to setup Raspberry Pi as JATG/SWD debug probe, how to start debug server on them and how to connect SES to this debug server.
My opinions about this complicated process, library, and SES you can find in chapter “Review of Software”.