5.2.7 BLE ANPS Application
This section explains how to enable the BLE Alert Notification Profile Server (ANPS) on the PIC32-BZ6 Curiosity board using the MCC. In this application example, BLE ANPS functions in the Central role and is paired with the BLE ANPC application which functions in the Peripheral role.
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.
It is recommended to follow the examples in sequence to understand the basic concepts before progressing to the advanced topics.
Recommended Readings
-
Getting Started with Application Building Blocks – See Building Block Examples from Related Links.
-
Getting Started with Peripheral Building Blocks – See Peripheral Devices from Related Links.
-
FreeRTOS and BLE Stack Setup – See Peripheral - FreeRTOS BLE Stack and App Initialize from Related Links.
-
BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
Hardware Requirement
S. No. |
Tool |
Quantity |
---|---|---|
1 |
PIC32-BZ6 Curiosity Board |
3 |
2 |
Micro USB cable |
2 |
SDK Setup
Refer to Getting Started with Software Development from Related Links.
Software Requirement
To install Tera Term tool, refer to the Tera Term web page in Reference Documentation from Related Links.
Smart phone App
None
Programming the Precompiled Hex File or Application Example
Using MPLAB® X IPE:
- Import and program the precompiled hex
file:
<Harmony Content Path>\wireless_apps_pic32_bz6
\apps\ble\advanced_applications\ble_anps_app\precompiled_hex\ble_anps_app.X.production.hex
. - Import and program the precompiled hex
file:
<Harmony Content Path>\wireless_apps_pic32_bz6
\apps\ble\advanced_applications\ble_anpc_app\precompiled_hex\ble_anpc_app.X.production.hex
. - 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:
-
Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
- Open and program the application example
ble_anps_app.X
located in: :<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\advanced_applications\ble_anps_app\firmware\ble_anps_app.X
. - Open and program the application example
ble_anpc_app.X
located in:<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\advanced_applications\ble_anpc_app\firmware\ble_anpc_app.X
. - For more details on how to find the Harmony Content Path, refer to Installing the MCC Plugin from Related Links.
Demo Description
This application demonstrates on simulating an ANPS on the Curiosity board. The on-board USR BTN1 and LED are used as user interface during the demo.
- Type of pressing on USR BTN1:
- Press: Press the button for less than 500 ms
- Long press: Press the button for more than 500 ms
- LED behavior:
- All LED’s are OFF
- Blue LED flashes one time every 3 seconds
- Blue LED flashes two times every 3 seconds
- Blue LED flashes two times every 1.5 seconds
Testing
Experience the demo using two PIC32-BZ6 Curiosity boards configured as Peripheral and Central device
This section assumes that a user has already programmed the
“ble_anps_app.X
” and “ble_anpc_app.X
” application on two
PIC32-BZ6 Curiosity boards.
- Board 1 – PIC32-BZ6 Curiosity Board Programmed with “
ble_anpc_app.X
”. Once programmed, the board’s LED will Flash once every 3 seconds and wait for a new connection within 60 seconds.- Open TeraTerm and configure as
mentioned below:Terminal Settings
- Baud Rate/Speed – 115200 (as configured in SERCOM configuration)
- Parity – None
- Data Bits – 8
- Stop Bits – 1
- Flow Control – None
Figure 5-241. Tera Term Settings For terminal setup configuration, go to Setup>Terminal and set the “New-line” of Receive to LF then click OK
For Serial port setup and connection, go to Setup>Serial PortNote: For more details on how to set the “Serial Port” and “Speed”, refer to COM Port Setup in Running a Precompiled Application Example from Related Links. - Reset the board (on-board Reset),
Upon Reset, “ANPC init” will be displayed in Tera Term.
Figure 5-242. Tera Term
- Open TeraTerm and configure as
mentioned below:
- Board 2 – PIC32-BZ6 Curiosity Board Programmed with “
ble_anps_app.X
”. Once programmed, the ANPS device will establish a new connection with the ANPC device within 60 seconds. - Once connection is established, the ANPC device stores the pairing data with the ANPS device and the ANPS device’s LED will Flash twice every 1.5 seconds.
- Press the USR BTN1 on the ANPS device to send a
new alert notification to the ANPC device and the following will be displayed in Tera
Term.
Figure 5-243. Tera Term - Press the USR BTN1 on the ANPS device again to
send an unread alert notification to the ANPC device and the following will be displayed
in Tera Term.
Figure 5-244. Tera Term - Unplug the USB cable of the ANPS device then plug it in again. The ANPS device will prepare to reconnect with the ANPC device within 30 seconds and once connection is established, the ANPS device’s LED will again blink twice every 1.5 seconds.
- Now, unplug the USB cable of the ANPS
device and program the ANPS precompiled
.hex
file or application example in another Curiosity board.Note: Since the ANPC device stored pairing data with the ANPS device, it will not yet attempt a connection with the ANPS Device 2. - Long press the USR BTN1 on the ANPC device so that it accepts a new connection. Once connection is established with the ANPS Device 2 within 60 seconds, the ANPS device’s LED will blink twice every 1.5 seconds.
Developing this Application from Scratch using MCC
-
Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.
- 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_pic32_bz6\apps\ble\advanced_applications\ble_anps_app\firmware\ble_anps_app.X\ble_anps_app.mc3".
.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 when prompted.
- Verify if the project graph window has
all the expected configuration. as illustrated in the following figure:
Figure 5-245. Project Graph
Verify MCC Component Settings
- Select Alert Notification Profile
component in project graph, to open component configuration and configure as illustrated
in the following figure.
Figure 5-246. Alert Notification Profile Configuration -
Select BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.
Figure 5-247. BLE Stack Configuration
Generating a 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
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.
initialization.c
app_ble.c
Source Files |
Usage |
---|---|
|
Application State machine, includes calls for Initialization of all BLE stack (GAP,GATT, SMP, L2CAP) related component configurations |
|
Source code for the BLE stack related component configurations, code related to
function calls from |
|
All GAP, GATT, SMP and L2CAP event handlers |
|
Handle ANPS event from application level |
|
Handle ANPS event from profile level |
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 inapp.c
Function Calls
MCC generates and adds the code to initialize the BLE Stack GAP, GATT, L2CAP and SMP in
APP_BleStackInit()
function.
-
APP_BleStackInit()
is the API that will be called inside the Applications Initial State --APP_STATE_INIT
inapp.c
User Application Development
Include:
- Include the user action. For more information, refer to User Action from Related Links.
- definitions.h must be included
in all the files where UART will be used to print debug information.Note: definitions.h is not specific to UART but instead must be included in all the application source files where any peripheral functionality will be exercised.
Starting Scanning
APP_ScanEnable(bPaired ? APP_ANPS_STATE_WITH_BOND_SCAN: APP_ANPS_STATE_SCAN)
app.c
Connected and Disconnected Events:
- All the possible GAP, GATT, SMP and L2CAP
event handlers are available in app_ble_handler.c. Users can implement their own
application code to denote Connection State here.
Figure 5-254. app_ble_handler.c
Initialize ANPS:
- ANPS functionality initialization is done
in
APP_BleStackInitAdvance()
.Figure 5-255. app_ble.c
Implement ANPS:
- ANPS is implemented in
ble_anps.c
andBLE_ANPS_BleEventHandler()
will handle all ANPS. events.ble_anps.c
Firmware FSM (Finite State Machine)
This application firmware implements an ANP server on the curiosity board.
APP_ANPS_STATE_IDLE
: application remains in the Idle mode.APP_ANPS_STATE_SCAN
: application is prepared to establish a new connection within timeout_scan seconds.APP_ANPS_STATE_WITH_BOND_SCAN
: application is prepared to reconnect within timeout_with_bond_scan seconds.APP_ANPS_STATE_CONNECTING
: application is currently in the process of establishing a connection.APP_ANPS_STATE_CONNECTED
: application is prepared to send commands notifications.
Variable |
Value |
Description |
---|---|---|
|
60 seconds |
The timeout of scanning without bond |
|
30 seconds |
The timeout of scanning with bond |
Firmware State |
LED Behavior |
---|---|
|
All LEDs are OFF |
|
Blue LED Flashes once every 3 seconds. (ON: 50 ms, OFF: 2950 ms) |
|
Blue LED Flashes twice every 3 seconds. (ON: 50 ms, OFF: 50 ms) |
|
Blue LED Flashes twice every 1.5 seconds. (ON: 50 ms, OFF: 150 ms) |