Summary

The first of several articles discussing the use of the AnyCloud BLE Stack to build advertising scanner/observers.

Story

A few summers ago while I was writing the WICED Bluetooth Academy book, I created a WICED based BLE advertising scanner.  Actually, I created a framework for the scanner and the remarkable summer intern I had finished the work.  That project has been sitting in the source code repository for the Bluetooth class, mostly only shown to face-to-face students.  This scanner is built using some of the original code combined with the new AnyCloud Bluetooth SDK.  It will act sort-of-like LightBlue or one of the other Bluetooth advertising scanners you might run on your phone, but with a serial console.

Sometime in the last few months we released the Bluetooth SDK for AnyCloud (things have been crazy and I have lost track of time)  This SDK has all of the stuff needed to add Bluetooth to your AnyCloud project using one of the Cypress Bluetooth/WiFi combo chips.  I had not had a chance to try it out, so I decided to build a Bluetooth project and then port the scanning code.

For this series of articles I will:

  1. Discuss an overview of Bluetooth Application Architecture (Part 1)
  2. Explain how to Start the AnyCloud Bluetooth Stack (Part 1) 
    1. Discuss the configuration structure: bt_platform_cfg_setting (Part 1)
    2. Discuss the configuration structure: wiced_bt_cfg_settings (Part 1)
    3. Discuss the Tasks created by the Bluetooth Stack (Part 1)
    4. Build the Basic Project (Part 2)
    5. Explain BLE Advertising - Scanner/Observer (Part 3)
    6. Explain BLE Advertising - Advertise (Part 3)
    7. How does scanning work in the AnyCloud Bluetooth Stack?  (Part 3)
    8. Add Observing to our Project  (Part 3)
    9. Program and Test the Project  (Part 3)
    10. Add the IoT Expert Bluetooth Utility Library (Part 4)
    11. Mulithreading Dangers (Part 4)
    12. Add a Queue to the Bluetooth Manager Thread (Part 4)
    13. Add a Timer to Process the Queue (Part 4)
    14. A Potential Threading Bug (Part 4)
    15. Add a Scan Off & On Command (Part 4)
    16. Create an Advertising Data Database Task (Part 5)
    17. Update the Advertising Database to Accept Submitted ADV Packets (Part 5)
    18. Update the Bluetooth Manager to Submit Adv Packets (Part 5)
    19. Create an Advertising Data Database (Part 5)
    20. Add a Command to Print the Database (Part 5)
    21. Refactor a silly duplication (Part 6)
    22. Update the print (Part 6)
    23. Add the "decode" command (Part 6)
    24. Add the decode packets functionality (Part 6)
    25. Program and test (Part 6)
    26. Design a new advertising database (Part 7)
    27. Update to the new database (Part 7)
    28. Update printing (Part 7)
    29. Update the advertising add (Part 7)
    30. Create a watch command (Part 7)
    31. Create a record command (Part 7)
    32. Create an erase command (Part 7)
    33. Update the filter data structure (Part 8)
    34. Fix the Add function (Part 8)
    35. Add the filter command (Part 8)
    36. Update the printing (Part 8)
    37. Fix a memory leak (Part 9)
    38. Add packet age (Part 9)
    39. Fix the printing, again (Part 9)
    40. Fix the add packet function (Part 9)
    41. Update the database structure for sort and purge (Part 10)
    42. Add the sort and purge commands (Part 10)
    43. Add the sort functionality (Part 10)
    44. Add the purge functionality (Part 10)
    45. The End & Commentary (Part 10)

    All of the code can be found at git@github.com:iotexpert/AnyCloudBLEScanner.git and https://github.com/iotexpert/AnyCloudBLEScanner.git

    There are git tags in place starting at part 5 so that you can look at just that version of the code.  "git tag" to list the tags.  And "git checkout part6" to look at the part 6 version of the code.

    Bluetooth Application Architecture

    Bluetooth applications are divided into these four pieces

    1. You user application which responds to events and sends messages from/to the Bluetooth host stack
    2. A Bluetooth Host Stack
    3. A Bluetooth Controller Stack
    4. The Bluetooth Radio

    These four pieces can be divided into multiple chips, as few as one or as many as four.  However, for this article, the project will be built to run on a PSoC 6 + CYW43012 WiFi/Bluetooth Combo chip.  Specifically:

    1. My scanner application running on the PSoC 6
    2. The Bluetooth Host Stack running on the PSoC 6
    3. The BlueTooth Controller Firmware running on the CYW43012
    4. A Bluetooth Radio on the CYW43012

    But how do they talk?  Simple, there is:

    1. A UART Host Controller Interface (HCI) between the two chips
    2. A GPIO to serve as a deep sleep wakeup from the 43012 –> PSoC 6
    3. A GPIO to serve as the bluetooth controller wakeup from the PSoC 6 –> 43012
    4. A GPIO to turn on the Bluetooth regulator from the PSoC 6 –> 43012

    Here is the block diagram from the CY8CKIT-062S2-43012 Kit Guide.  Notice that signals labeled UART and Control going between the PSoC 6 and the CYW43012.

    And when you read more deeply into the schematic you can see the signals labeled

    • BT_UART_TXD/RXD/CTS/RTS
    • BT_HOST_WAKE
    • BT_DEV_WAKE
    • BT_REG_ON

    How to Start the AnyCloud Bluetooth Stack

    To actually start the AnyCloud Bluetooth stack you will call two functions

    1. cybt_platform_config_init – that will setup the hardware interface to the CYW43012
    2. wiced_bt_stack_init that will:
      1. Start a task to manage the Host Controller Interface
      2. Download the controller firmware to the CYW43012
      3. Start a task to manage the host stack
      4. Initialize both the host and the controller
      5. Call you back when that is all done

    Here is an example from main.

    When you look at these two function calls, you will find that you need to provide three things:

    1. A platform hardware configuration structure called bt_platform_cfg_settings
    2. The Bluetooth stack configuration settings structure called wiced_bt_cfg_settings
    3. A management callback called app_bt_management_callback

    bt_platform_cfg_settings

    The purpose of the hardware configuration structure is to define the UART + parameters and the wakeup GPIOs.  Specifically, the hardware configuration structure defines the configuration of the host controller interface (hci)

    1. The HCI transport scheme (in this case UART)
    2. The pins of the UART
    3. Baud Rate
    4. Data Bits
    5. Stop Bits
    6. Parity
    7. Flow Control

    And the controller low power behavior (in the .controller_config member)

    This is a fairly standard configuration and I think that we should help you by providing this structure somewhere in the BSP.  But for now, you need to provide it (in an upcoming article I’ll update the IoT Expert Bluetooth Library to provide it).  Here is the specific structure that I will be using.

    wiced_bt_cfg_settings

    The Cypress WICED Bluetooth Stack has a boatload of configuration settings.  When you call the stack start function you need to provide all of those settings in a structure of type “wiced_bt_cfg_settings_t” which is actually a structure of structures.  There are several basic ways to set these settings

    • Start from scratch and build you own settings
    • Copy from an example project
    • Use the Bluetooth Configurator to generate the structure

    For the purposes of THIS project I started by copying the structure from on of the example projects and then modifying the three numbers that were relevant to me.  Specifically

    • max_simultanous_link – which I changed to 0 because this is simply a Bluetooth Observer
    • low_duty_scan_interval – how long in the window to listen for advertising packets
    • low_duty_scan_window – how wide the window of listening should be

    app_bt_management_callback

    The last thing that you need to provide is a management callback.  This function is called by the Bluetooth Stack when a “management event” occurs.  There is a big-long-list of enumerated events of type wiced_bt_management_event_t.  The events include things like the the stack started “BTM_ENABLED_EVENT”.  Each event may have data associated with the event which is passed to you in a pointer to wiced_bt_management_event_data_t.

    You typically deal with these events with a giant switch statement like this:

    Tasks

    The Bluetooth stack on the PSoC6 is operated with two tasks.  Specifically, when you call the wiced_bt_stack_init it will startup:

    1. CYBT_HCI_Task – a task that sends and receives HCI packets going to the Radio chip
    2. CY_BT_Task – a task that manages the Bluetooth Host Stack

    Here is print of the task list from my project:

     

    Now with the background in place, in the next article I will discuss Bluetooth advertising and how to build the observer project.

    Recommended Posts

    No comment yet, add your voice below!


    Add a Comment

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