5.1.2.4 BLE Deep Sleep Advertising

This section explains how to enable “Deep Sleep” mode with BLE Advertisements on the PIC32-BZ6 Curiosity board using the MPLAB Code Configurator (MCC). In this application example, the Deep Sleep advertisement interval will be set to 1 second. This interval dictates the application Deep Sleep time.

Users can choose to either run the precompiled Application Example hex file provided on the PIC32-BZ6 Curiosity Board or follow the steps to develop the application from scratch.

These examples build upon one another. It is recommended to follow the examples in sequence to understand the basic concepts before progressing to the advanced topics.

Recommended Readings

  1. Getting Started with Application Building Blocks – See Building Block Examples from Related Links.

  2. Getting Started with Peripheral Building Blocks – See Peripheral Devices from Related Links.

  3. FreeRTOS and BLE Stack Setup – See Peripheral - FreeRTOS BLE Stack and App Initialize from Related Links.

  4. BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
  5. Low Power Notes - See Low Power Design on PIC32-BZ6 Devices from Related Links.

Hardware Required

Table 5-16. Hardware Prerequisites
S. No.ToolQuantity
1PIC32-BZ6 Curiosity Board1
2Micro USB cable1
3Android/iOS Smartphone1
4(Optional) Power Debugger(1)/Multimeter/Oscilloscope to measure power
Note:
  1. For more information, refer to Power Debugger in Reference Documentation from Related Links.
1

SDK Setup

Refer to Getting Started with Software Development from Related Links.

Software

To install Tera Term tool, refer to the Tera Term web page in the Reference Documentation from Related Links.

Smartphone App

Microchip Bluetooth® Data (MBD) iOS/Android app available in stores.

Programming the Precompiled Hex file or Application Example

Using MPLAB® X IPE:

  1. Import and program the precompiled hex file: <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\peripheral\deep_sleep_adv\precompiled_hex\ble_deep_sleep_adv.X.production.signed.hex.

  2. For detailed steps, refer to Programming a Device in MPLAB® IPE in Reference Documentation from Related Links.
    Note: Ensure to choose the correct Device and Tool information.

Using MPLAB® X IDE:

  1. Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
  2. Open and program the Application: <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\peripheral\ deep_sleep_adv\firmware\ble_deep_sleep_adv.X.
  3. For more details on how to find the Harmony Content Path, refer to Installing the MCC Plugin from Related Links.

Demo Description

This application example enables users to enter Deep Sleep mode while transmitting Connectable, Undirected BLE Advertisements. On power-on reset, the application will enter Deep Sleep mode. When USR BTN 1(SW801) is pressed, the device on-board GREEN LED starts blinking to denote the start of advertisements. The device will then enter Deep Sleep mode periodically based on the advertisement interval. The advertisement interval is set to 960 milliseconds for this example. When the device connects with the mobile app, the onboard LED starts to glow solid, which indicates that the connection is established and the device will enter Standby Sleep mode during Idle state.

Testing

  1. Using a micro USB cable, connect the Debug USB on the Curiosity board to a PC.
  2. Program the precompiled hex file or application example as mentioned. Upon flashing, there will be no indication on the board since the device entered Deep Sleep mode.
  3. Press USR BTN 1(SW801) available on the board to start Deep sleep Advertising and the RBG LED will start blinking.
  4. Launch the MBD mobile app and select the BLE Smart sub app to see the scan results. A device with the name “BLE_DSADV” will appear.
  5. After connecting the PIC32-BZ6 device with the MBD App, the RGB LED will glow solid.

Current Consumption Measurement

Hardware modification

Remove the resistors R925 and R777 on PIC32-BZ6 Curiosity Board.

Connect a Multimeter/Power Debugger to Power Measurement Header J205. Power on the Board. If using a Power Debugger, users can use Data Visualizer to measure the current consumption.

Current measured in Deep Sleep mode as per Wireless_ble v 1.4.0 and wireless_pic32cxbz_wbz V1.5.0 in PIC32-BZ6 Curiosity board is around 2.4 uA.

Current measured in Deep sleep + Advertising mode is around 130 uA average.

Users of this early adopter package should go through the known issues document and understand the limitations, if any, with the current low power mode implementation.

Developing this Application using MPLAB Code Configurator Import method

Follow the steps below to build the application manually:
Note: It is recommended for the new users of the MPLAB Code Configurator to refer MPLAB® Code Configurator (MCC) User’s Guide in Reference Documentation from Related Links.
  1. Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.

  1. Setup the basic components and configuration required to develop this application, import component configuration: <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\peripheral\deep_sleep_adv\firmware\ble_deep_sleep_adv.X\ble_deep_sleep_adv.mc3.
    Note: Import and Export functionality of the Harmony component configuration will help users to start from a known working setup of the MCC configuration.
  2. Accept dependencies or satisfiers when prompted.

  3. Verify if the Project Graph window has all the expected configuration.

Developing this Application from scratch using MPLAB Code Configurator

This section is intended to showcase the complete application development from scratch including the individual components and their dependency inclusions.
Note: It is recommended for the new users of the MPLAB Code Configurator to refer MPLAB® Code Configurator (MCC) User’s Guide in Reference Documentation from Related Links.
  1. Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.

  2. In the previous step, the BLE Stack component is included. Accept all the component auto-activations and attachment auto-connect confirmations.
  3. Include the RCON Component from the peripheral category.
  4. Include the Transparent Service and Transparent Profile component located at : Harmony-> Wireless->Drivers->BLE.

  5. Establish the link between the Transparent Service and Profile components.
  6. Click on the Transparent Profile component. In the configuration option, enable server role.

  7. Click on the BLE Stack component. In the configuration option, enable Sleep mode.
  8. This action will trigger the auto inclusion of the RTC component. Accept the activations and auto-connect confirmations.
  9. Final Project graph with all the required component.

Verify Advertisement and Component Configuration

This section details the configuration changes needed for each component in accordance with the application use case implementation.

  1. Select the BLE_Stack component in the project graph.
    Note: Advertising Interval Min can be modified to adjust Deep Sleep Advertising interval.
    Tip: Advertisement payload can be configured by user here.
  2. Select Clock Configuration.
  3. Configure RTC clock configuration.
  4. Configure LED GPIO Configuration.
    Note: The above GPIO configuration is used for indicating the device state in this example and is optional.
    Tip: The configuration bits will be generated after user Generates Code.
  5. Configure RTOS configuration by increasing the Total heap size from the default value.

Generating Code

For more details on code generation, refer to MPLAB Code Configurator (MCC) Code Generation from Related Links.

Files and Routines Automatically generated by the MCC

After generating the program source from the MCC interface by clicking Generate Code, the BLE configuration source and header files can then be found in the following project directories.

Initialization routines for OSAL, RF System, and BLE System are auto-generated by the MCC. See OSAL Libraries Help in Reference Documentation from Related Links. Initialization routine executed during program initialization can be found in the project file.

The BLE stack initialization routine executed during Application Initialization can be found in project files. This initialization routine is automatically generated by the MCC. This call initializes and configures the GAP, GATT, SMP, L2CAP and BLE middleware layers.

During system sleep, clock (system PLL) will be disabled and system tick will be turned off. FreeRTOS timer needs to be compensated for the time spent in sleep. RTC timer which works in sleep mode is used to accomplish this. RTC timer will be initialized after BLE stack initialization.

Table 5-17. Source Files
Source FilesUsage
app.cApplication State machine, includes calls for Initialization of all BLE stack (GAP,GATT, SMP, L2CAP) related component configurations
app_ble.cSource code for the BLE stack related component configurations, code related to function calls from app.c
app_ble_handler.cGAP, GATT, SMP and L2CAP event handlers
app_ble_dsadv.cSource code for utilizing the deep sleep advertising functionality
device_deep_sleep.cSource code for deep sleep and wake-up related system configurations
Note: app.c is autogenerated and has a state machine-based application code sample. Users can use this template to develop their own application.

Header Files

  • ble_gap.h contains BLE GAP functions and is automatically included in app.c

Function Calls

  • MCC generates and adds the code to initialize the BLE Stack GAP, GATT, SMP and L2CAP in APP_BleStackInit() and the Deep Sleep Advertising functionality in APP_BleDsadvStart(flag)
  • APP_BleStackInit() and APP_BleDsadvStart(flag) APIs are called in the Applications Initial State APP_STATE_INIT in app.c

Manual Application Code Edit

This section talks about the mandatory code modifications that need to be incorporated in order to implement the Deep Sleep Advertising functionality.

Step 1: app_user_edits.c

  • Some of the Harmony 3 generated files cannot be fully configured by the MPLAB Code Configurator. This file contains the instructions for the user to modify these files.

    Follow the instruction mentioned in app_user_edits.c and after completing the required edits, comment out or remove the #error line.

User Application Development

Include

    • definitions.h must be included in all the files where port pin macros are used.
    Note: definitions.h is not specific to port peripheral but instead must be included in all the application source files where any peripheral functionality will be exercised.

Enter Deep Sleep mode

  • DEVICE_EnterDeepSleep(false,0);

    This API can be called to put the device to Deep Sleep mode.

Start Deep Sleep Advertisement

  • APP_BleDsadvStart(false);

    This API can be called in APP_STATE_INIT of app.c to start the Deep Sleep Advertising.

    Note: Users can explore more BLE Advertisement functionalities using the BLE Stack APIs. For more information, refer to BLE Stack in Reference Documentation from Related Links.

Application Use Case Implementation

This application implementation is purely user choice. It is designed in such a way that the device enters into Deep Sleep mode upon power up and when SW2 is pressed, the device starts the Deep Sleep Advertisements based on the fixed advertisement interval. Device then wakes up from Deep Sleep in regular intervals to start an advertisement and go back to sleep again.

This scenario is implemented by modifying the default generated code inside the switch case in app.c. User can implement their desired application workflow by making use of the available function calls.

Known Issue

System Sleep implementation source (device_sleep.c) and header (device_sleep.h) files may sometimes not be included when generating the project through MCC so follow the below steps to avoid this.

Step1: Uncheck and re-enable the Enable Sleep Mode and Enable Deep Sleep Advertising option inside the BLE Stack component in the project graph as shown in the figure below and accept the dependencies requested.

Step2: Click the Force Update on All option and press Generate

Where to Go from Here

See BLE Connection from Related Links.