tirsdag den 23. juli 2019

BLE from scratch Part 1: Basic build setup

Preface

It has been some time since I last did a post on this blog - there has been way too many things going on for me to focus on this hobby, but finally things have cleared up.

I have for a long time wanted to dive more into BLE development - there are a lot of cool things that can be done with a cheap radio and an app. For this endeavour I have chosen to buy the nRF52-DK from Nordic Semiconductor. I have worked professionally with devices from Nordic before (I'm looking at you ReSound LiNX) and they are well documented and easy to use, so that is an obvious choice for me.

Nordic have teamed up with Segger to provide a free unlimited version of the Segger Embedded Studio also for commercial use. Although this is a hobby project, free and unlimited for all and also built on CLANG/LLVM is too good to pass, so that will be the tool chain I will be focusing on in the coming posts

Nordic provides lots and lots of examples on how to build software for their devices, but I would like to start a project from scratch and then add functionality as needed. This is for several reasons:


  • Starting with an example and modifying that doesn't really bring the understanding of how things work and are put together. That understanding is needed when you want do debug stuff
  • I am not a big fan of board support packages - they carry complexity that are in many cases not needed.
  • Although powerful, the Nordic devices don't have endless RAM and Flash ressources so I would like to be able to run a lean ship
I will be storing the source code and projects for this endeavour on https://github.com/bdpedersen/ble-from-scratch

Quick spec

I will not be building the entire BLE stack from scratch - that is too time consuming. Instead I will be using what is called a SoftDevice in Nordic lingo. Basically a prebuilt binary that you can interface with using SVC and task with various BLE and basic system tasks. The SoftDevice I will be using is the S112 - the simplest of the SoftDevices that Nordic offers. On top of this I will build a simple application with the following features:

  • One service to control a LED and read a button state with:
    • One writeable characteristic that controls the LED
    • One read/notify characteristic that reads the state of the button
The application will end up supporting bonding with LE secure connections as well as persistence of bonds. It will run with the loweste possible power consumption allowed by the Nordic Device while still supporting IO.

Prerequisites for building the code in this blog

Downloads:
  • The latest version of the Nordic nRF5 SDK. Documentation can be found here
  • Segger Embedded Studio, which can be found here. During installation you will be asked to acquire a license - just state that you use a Nordic device in this step.

I extracted the two Nordic files to c:\nrf. 

... And now to some action

To set up a project from scratch in SES, choose "New Project..." and then choose a C/C++ executable for Nordic Semiconductor nRF. Choose a proper name and location for your project, then press Next.

In the target setup make sure that the Target processor matches your target. In the case of the nRF52-DK, the default nRF52832_xxAA is the correct one. In "Additional output format" choose to create a hex file. Adjust heap size if needed, but set stack size to at least 1536 bytes (2048 is safe) as this is needed by the softdevice, and press Next.

For additional files to add to the project, you can choose to leave out the Segger RTT files if you don't need realtime logging and debugging in your app, but otherwise the defaults are fine, so just press Next.

Finally create the two configurations, debug and release by pressing Next.

You can actually build and run this project, just make sure the nRF52-DK is connected to USB and on, and then choose "build -> build and run". If you then choose "Debug -> Go" the debug terminal will show up and runningn the application will show the Hello World prints via RTT

Part 2 can be found here

Ingen kommentarer:

Send en kommentar