Creating firmware for Bluetooth LE devices with mbed

This tutorial shows how to create a basic virtual UART-over-Bluetooth LE device using VisualGDB and the ARM mbed framework. Before you begin, install the latest VisualGDB and update the mbed package to 5.4.2 or later via Tools->VisualGDB Package Manager.

In this tutorial we will use a Nordic NRF52_DK board and and Android phone to connect to the board via Bluetooth LE.

  1. Start Visual Studio and open the VisualGDB Embedded Project Wizard:01-newprj
  2. Select “Create a new project -> MSBuild -> Embedded Binary”:02-prjtype
  3. On the next page select “Use ARM mbed” and pick your board from the list. We will use the Nordic NRF52_DK board, however the same steps should work for any other nRF5x-based board supported by mbed:03-deviceIt is recommended to select “Newlib-nano” as the C library and check the “provide default stubs for system calls” checkbox to reduce the binary size.
  4. On the “Sample Selection” page pick the “Bluetooth LE UART Console” sample and press “Next”:04-sample
  5. Choose a debug method corresponding to your target. Nordic development boards have an integrated Segger J-Link that can be configured as shown below:05-debugNote that J-Link won’t automatically detect the exact device name from the mbed target name (like nRF52_DK), so it needs to be selected manually.
  6. Press “Finish” to create the project build it with Ctrl-Shift-B:06a-build
  7. If the build fails due to inline variable declarations in C files, open VS Project Properties and switch the C language standard to C99:06-c99
  8. Connect your Nordic board and press F5 to start debugging. On your Android device launch the Nordic UART app and connect to the virtual UART device:07-ble-devices
  9. Once you connect to the device, you should see the “ping” messages sent by the firmware:08-ping
  10. Modify the periodicCallback() function as follows to replace the “ping” messages with “hello” messages having increasing numbers:
    int g_Counter = 0;
     
    void periodicCallback(void)
    {
        led1 = !led1;
        if (uart)
        {
            char tmp[32];
            sprintf(tmp, "Hello %d\n", g_Counter++);
            uart->writeString(tmp);
        }
    }

    Then press F5 to start debugging:09-newcode

  11. Reconnect to the board from your Android device and see how the displayed messages have changed:10-hello
  12. If you try setting a breakpoint in the periodicCallback() function, it will trigger, but you won’t be able to continue debugging as the nRF5x watchdog will trigger an internal error: 11-bkpt
  13. To avoid this, we recommend using alternate debugging techniques like Live Variables (right-click on g_Counter to plot its value in real-time) and real-time watch that can show the executed functions in real time:12-livevar
  14. Open VisualGDB Project Properties and go to the Embedded Frameworks page. You will see various mbed libraries and features (like BLE or LWIP) displayed as embedded frameworks. Checking the corresponding frameworks will automatically add them to your project: 13-frameworks
  15. You can also easily retarget an mbed project to a different platform or edit settings like baud rate via the first page of VisualGDB project properties:dev