3.1.2.4 BLE Deep Sleep Advertising

Getting Started

Getting Started with Peripheral Building Blocks

BLE Deep Sleep Advertising

Introduction

This section helps users to enable the Deep sleep mode with BLE Advertisements on the WBZ451 Curiosity board using MPLAB Code Configurator(MCC). In this basic application example, the Deep Sleep advertisement interval will be set to 960 millisecond. The advertisement interval will dictate the application Deep sleep time.

Users can choose to run the precompiled application example .hex file on the WBZ451 Curiosity Board and experience the demo or go through the steps involved in developing this application from scratch.

Recommendation is to follow the examples in order,by learning the basic concepts first and then progressing to the more advanced topics.

Recommended Reading

Hardware Requirement

Table 3-12. Hardware Requirement
ToolQuantity
WBZ451 Curiosity Board1
Micro USB cable1
Power Debugger/Multimeter1

SDK Setup

Software Requirement

Smart phone App

  • Microchip Bluetooth Data (MBD)

Programming the Precompiled Hex File or Application Example

Programming the .hex File using MPLAB X IPE

  1. Precompiled .hex file is located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\peripheral\deep_sleep_adv\hex" folder
  2. For the instructions, refer to Programming A Device.
    Note: Users must choose the correct Device and Tool information.

Programming the Application using MPLAB X IDE

  1. Follow steps mentioned in the Running a Precompiled Example section
  2. Open and program the application example “ble_deep_sleep_adv.X” located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\peripheral\deep_sleep_adv\firmware" using MPLAB X IDE

    For more details on finding the Harmony content path, refer to Installing the MCC Plugin

Demo Description

This application example enables users to enter the Deep sleep mode while transmitting connectable, undirected BLE advertisements. On power on reset demo will enter “Deep Sleep Mode”, when the USR_BTN (SW2) is pressed the device on-board led (green) starts blinking which denotes start of advertisements, Device will enter the Deep sleep mode periodically based on the advertisement interval which is set to 960 milliseconds for this example. When device connects with the mobile App the on-board led start to glow solid, which indicates the connection established and the device will enter the Standby Sleep Mode during Idle state.

Testing

Connect the WBZ451 Curiosity board to PC, program the precompiled hex file or application example as mentioned. Upon flashing, there will not be any indication on the board, since the device enters Deep Sleep mode. User can press the USR_BTN (SW2) available on the board to start Deep Sleep Advertising. To connect to the WBZ451,
  1. User can open the MBD App on Smart phone to scan for Advertisements.
    Figure 3-80. BLE Connect
  2. Select BLE Connect feature in MBD App
  3. From the list of devices select “BLE_DSADV” device to connect
    Figure 3-81. Devices

Current Consumption Measurement

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

Current measured in the Deep sleep mode as per Wireless_ble v 1.1.0 and Wireless_pic32cxbz_wbz v1.2.0 in A2 version of the WBZ451 module is around 1.21 uA.
Figure 3-82. Current Consumption Measurement
Note: Users of this Early adapter package must go through the known issues document and understand the limitations if any with the current low power mode implementation.

Developing this Application from Scratch using MCC

This section explains the steps required by a user to develop this application example from scratch using MCC

Note: It is recommended that new users of MCC to go through the overview.
  1. Create a new MPLAB MCC Harmony Project. For more details, refer to Creating a New MCC Harmony Project

  2. Import component configuration -- This step helps users setup the basic components and configuration required to develop this application. The imported file is of format .mc3 and is located in the path "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\peripheral\deep_sleep_adv\firmware\ble_deep_sleep_adv.X".
    For more details on importing the component configuration , refer to Importing Existing App Example Configuration
    Note: Import and export functionality of Harmony component configuration will help users to start from a known working setup of MCC configuration
  3. Accept Dependencies or Satisfiers, select Yes
  4. Verify if the project graph window has all the expected configuration. as illustrated in the following figure:
    Figure 3-83. Project Graph

Verifying Deep Sleep, Advertisement, System Sleep and RTC Clock Source Configuration

  1. Select BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.

    Figure 3-84. BLE Stack Configuration
    Note: Advertising Interval Min can be modified to adjust Deep Sleep Advertising interval. Advertisement payload can be configured by user here.
  2. Select Clock Configuration.
    Figure 3-85. Select Clock Configuration
  3. Configure RTC Clock Configuration.
    Figure 3-86. 
  4. Select Device Support Configuration component from project graph to enable PMU Mode configuration as illustrated below.
    Figure 3-87. Device Support Configuration
  5. Configure LED GPIO Configuration,
    1. Select Pin Configuration Plugin, from the project graph as illustrated below.
      Figure 3-88. Pin Configuration
    2. From the “Pin Settings” tab and configure the pin
      Figure 3-89. GPIO Configuration
      Note: The above GPIO configuration is used for indication of the device state in this example and is optional. The configuration bits will be generated after user generates the code.

Generating a Code

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

Files and Routines Automatically Generated by the MCC

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

Figure 3-90. Project Files
The OSAL, RF System, BLE System initialization routine executed during program initialization can be found in the project files. This initialization routine is automatically generated by the MCC.
Figure 3-91. initialization.c

The BLE stack initialization routine excuted during application initialization can be found in project files. This intitialization 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 the Sleep mode is used to accomplish this. RTC timer will be initialized after BLE stack initialization.

Figure 3-92. app_ble.c
Table 3-13. 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.cAll GAP, 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 application.

Header Files

  • ble_gap.h: This header file contains BLE GAP functions and is automatically included in the app.c file

Function Calls

MCC generates and adds the following code to initialize the BLE Stack GAP, GATT, L2CAP and SMP in APP_BleStackInit() and Deep Sleep Advertising Functionality in APP_BleDsadvStart(flag) function
  • APP_BleStackInit() and APP_BleDsadvStart(flag) are the API's that will be called inside the Applications Initial State – APP_STATE_INIT in app.c.

System Manual Configurations

The following code modifications must be done to utilize the deep sleep advertising functionality.

  1. initialization.c
    1. CLK_Initialize();
      This API call will be originally available in SYS_Initialize function and must be called part of _on_reset() function inside initialization.c file.
      Figure 3-93. initialization.c
    2. void SYS_Initialize (void* data)
      The below generated code must be removed from the SYS_Initialize function.
      Figure 3-94. SYS_Initialize
      Add the below code inside SYS_Initialize function.
      DEVICE_DeepSleepWakeSrc_T wakeSrc;
      DEVICE_GetDeepSleepWakeUpSrc(&wakeSrc);
      if (wakeSrc == DEVICE_DEEP_SLEEP_WAKE_NONE) //Initialize RTC if wake source is none(i.e power on reset)
      {
      RTC_Initialize();
      }
      Figure 3-95. SYS_Initialize
  2. startup_xc32.c
    1. PCHE_SetupRam()

      Copy the below code into the startup_xc32.c

      __attribute__((ramfunc, long_call, section(".ramfunc"),unique_section)) void PCHE_SetupRam(void)
      {
        // Set Flash Wait states and enable pre-fetch
        // clear PFMWS and ADRWS
        PCHE_REGS->PCHE_CHECON = (PCHE_REGS->PCHE_CHECON & (~(PCHE_CHECON_PFMWS_Msk | PCHE_CHECON_ADRWS_Msk | PCHE_CHECON_PREFEN_Msk)))
                                        | (PCHE_CHECON_PFMWS(1) | PCHE_CHECON_PREFEN(1));
       
        // write completion delay
        for(int i=1; i<10; i++)
        {
            asm ("NOP");
        }
      }
      Figure 3-96. startup_xc32.c

      Copy the below code into the Reset_Handler Handler Function after __pic32c_data_initialization() Function Call.

      if (!(DSU_REGS->DSU_DID & DSU_DID_REVISION_Msk))   //HW A0 version
         {
             PCHE_SetupRam();
         }
      Figure 3-97. startup_xc32.c
  3. heap_4.c
    1. Modify with the following code inside heap_4.c (available as part of FreeRTOS-->MemMang)

      static uint8_t ucHeap[ configTOTAL_HEAP_SIZE ]; --> Original Code
       
      static uint8_t  __attribute__((section (".bss.ucHeap"), noload)) ucHeap[ configTOTAL_HEAP_SIZE ]; -->Modified Code
      Figure 3-98. heap_4.c
  4. device_deep_sleep.c (optional)
    1. Configure the peripheral ports to minimize the current consumption. Those configurations must be placed in the function “Device_GpioConfig”.
      According to the WBZ451 Curiosity board pins assignment, the function provides example code to setup the GPIO pins and the peripheral. To setup the board configuration, refer to the configuration examples.
      Figure 3-99. device_deep_sleep.c
      The “Device_GpioConfig” is inside “DEVICE_EnterDeepSleep” to enter Deep sleep
      Figure 3-100. device_deep_sleep.c

User Application Development

Include

  • User action is required as mentioned here
  • app_ble_dsadv.h into app.c file
  • definitions.h in all the files where port pin macros are used.
    Note: definitions.h is not specific to just port peripheral, instead it must be included in all application source files where peripheral functionality will be exercised.

Enter Deep Sleep mode

  • DEVICE_EnterDeepSleep(false,0);

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

Start Deep Sleep Advertisement

  • APP_BleDsadvStart(false);

    This API can be called to start the Deep sleep Advertising.

    This API is called in the Applications initialstate – APP_STATE_INIT in app.c
    Figure 3-101. app.c

    Users can exercise various other BLE Advertisement functionalities by using BLE Stack API.