BLE application with nRF51822: Softdevice

One advantage of using Nordic Semiconductor BLE solution is Softdevice. Softdevice is a BLE stack running on the nRF51 chip, allowing you to develop a custom BLE application really easily. Sofdevice binary is provided free of charge with the nRF51 chip, a bunch of different public APIs are provided to let your application set up and communicate with it. Different declinations of Sofdevice are available depending the features you need for your project. In our case we will use S110 (BLE stack only). To get BLE working with our current project set up, we need to create a new linker script since Softdevice has to be stored at a specific memory location.

  • nRF51 Softdevice memory map:

nrf51822 bluetooth softdevice s110 memory map

To work properly Softdevice needs a part of the flash memory and a part of the RAM memory as well. So we will create a new linker script based on the first one we already developed:

        $ cd nrf51/linker_script/
        $ cp nrf51_ld_script.ld nrf51_sd_ld_script.ld

We now have to do some modifications in nrf51_sd_ld_script.ld file in order to share the memory. In Softdevice specifications document we can retrieve information regarding the memory usage when S110 is enabled.

nrf51822 bluetooth stack softdevice memory usage

As described below S110 + MBR need 96kB of Flash memory and 8kB of RAM memory. So the RAM starting point for our application is now 0x20002000 (0x20000000 + 8kB), therefore, the total RAM available for our application is not 16kB anymore but 8kB. Same for the Flash memory, the new starting point for our application is now 0x00180000 (0x00000000 + 96kB), so, the amount of Flash available is now 160kB. Below the modifications to do accordingly the specifications:

        MEMORY
        {
             RAM (xrw) : ORIGIN = 0x20000000 + 8k, LENGTH = 16k - 8k
             FLASH (rx) : ORIGIN = 0x00000000 + 96k, LENGTH = 256k - 96k
        }

That’s it for the linker script. We’re gonna create a new project based on the led blinking sources.

        $ cd nrf51/
        $ cd led_blinking/
        $ make clean         // In order to do a clean copy, remove all the object files 
        $ cd ../
        $ cp -r led_blinking/ sd_app

We need to do some modifications in the Makefile to call the correct ld script, change the project name and add few mandatory flags.

# linker script directory
LDSCRIPT = ../linker_script/nrf51_sd_ld_script.ld   
# Project name
PROJECT_NAME = sd_app
# Drivers
USE_DRV_NOSD := n
USE_LIB_SCHEDULER := y
USE_SD110 := y
USE_BLE_COMMON := y
USE_LIB_TRACE := y
USE_DRV_BLE_FLASH := y
USE_LIB_TIMER := y
...
CPFLAGS = -mcpu=cortex-m0 ...
CPFLAGS += -std=gnu99
...
# Define target processor
CPFLAGS += -D$(TARGET)
CPFLAGS	+= -DBLE_STACK_SUPPORT_REQD 

Now, compile your project and look at the generated sd_app.map file, you will see that the “.isr_vector” section is now starting at 0x180000, same for the “.bss” section which is now starting at 0x20002000.

Some modifications have been done in sdk.mk as well, in order to fix few include issues. You can find all these modifications at the following link:

https://github.com/jocelynmass/nrf51/blob/master/sdk/sdk.mk

Now, we need to flash Softdevice S110 (the MBR section is already included in S110) in the nrf51 flash memory. Start by copying the stack hex file in a new directory named stack_img.

        $ cd nrf51/
        $ mkdir stack_img
        $ cp sdk/nRF51_SDK_9.0.0_2e23562/components/softdevice/s110/hex/s110_softdevice.hex stack_img/

Then we’re gonna create a new JLink script file (based on nrf51_erase.sh script) that will help us to upload Sofdevice in flash memory.

        $ cd nrf51/sh/
        $ cp nrf51_erase.sh nrf51_load_sd.sh

In nrf51_load_sd.sh add the following lines:

loadfile stack_img/s110_softdevice.hex 0
r 

Finally we’ll check that our blinking led application is still working with Sofdevice loaded in the flash memory. Be careful to correctly load your application at address 0x18000, if not you’ll most likely corrupt Sofdevice.

        $ cd nrf51/
        $ JLinkExe sh/nrf51_load_sd.sh
        J-Link>loadbin sd_app/sd_app.bin 0x18000
        J-Link>r
        J-Link>g

You should see the green led blinking now. I know, nothing really exciting yet but that means we can now start using BLE ;-). In the next post we’ll see how to advertise some data with BLE.

Advertisements

Leave a Reply

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

WordPress.com Logo

You are commenting using your WordPress.com 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 )

Google+ photo

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

Connecting to %s