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.
Programming the Precompiled Hex
File or Application Example
Programming the .hex File using MPLAB X IPE
Precompiled .hex
file is located in "<Harmony Content Path>\wireless_apps_pic32cxbz2_wbz45\apps\ble\building_blocks\peripheral\deep_sleep_adv\hex"
folder
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
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,
User can open the MBD App on Smart phone to scan for Advertisements.
Select BLE Connect
feature in MBD App
From the list of devices
select “BLE_DSADV” device to connect
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.
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.
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".
Note: Import and export functionality of Harmony
component configuration will help users to start from a known
working setup of MCC configuration
Accept Dependencies or
Satisfiers, select Yes
Verify if the project graph
window has all the expected configuration. as illustrated in the following
figure:
Verifying Deep Sleep, Advertisement, System
Sleep and RTC Clock Source Configuration
Select BLE Stack component in project graph, to open component
configuration and configure as illustrated in the following figure.
Note: Advertising Interval Min can be modified to
adjust Deep Sleep Advertising interval. Advertisement payload can be
configured by user here.
Select Clock
Configuration.
Configure RTC Clock
Configuration.
Select Device Support
Configuration component from project graph to enable PMU Mode
configuration as illustrated below.
Configure LED GPIO
Configuration,
Select Pin Configuration Plugin, from the project graph as
illustrated below.Figure 3-88. Pin Configuration
From the “Pin Settings” tab and configure the pinFigure 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.
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 Files
Usage
app.c
Application State machine, includes calls for Initialization of
all BLE stack (GAP,GATT, SMP, L2CAP) related component
configurations
app_ble.c
Source Code for the BLE stack related component configurations,
code related to function calls from app.c
app_ble_handler.c
All GAP, GATT, SMP and L2CAP Event handlers
app_ble_dsadv.c
Source Code for utilizing the deep sleep advertising
functionality
device_deep_sleep.c
Source 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.
initialization.c
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
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();
}
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
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.cFigure 3-101. app.c
Users can exercise various other BLE Advertisement
functionalities by using BLE Stack API.