4.2.7 BLE ANPS Application

Getting Started

Getting Started with Peripheral Building Blocks

Introduction

This section helps users enable the BLE Alert Notification Profile Server (ANPS) on the WBZ351 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 run the precompiled application example .hex file on the WBZ351 Curiosity Board and experience the demo or go through the steps involved in developing this application from scratch.

These examples are incrementally structured upon one another. Recommendation is to follow the examples in order,by learning the basic concepts first and then progressing to the more advanced topics.

Recommended Reads

  1. BLE Software Specification
  2. FreeRTOS BLE App Initialize
  3. BLE ANPC Application

Hardware Requirement

Table 4-50. Hardware Requirement

Tool

Quantity

WBZ351 Curiosity Board

3

Micro USB cable

2

SDK Setup

Software Requirement

Smart phone App

  • None

Programming the Precompiled Hex File or Application Example

Programming the .hex File using MPLAB X IPE

  1. Import and program the precompiled .hex file located in "<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anps_app\hex\ble_anps_app.X.production.hex
  2. Import and program the precompiled .hex file located in "<Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anpc_app\hex\ble_anpc_app.X.production.hex
  3. For more details on the steps, go 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_anps_app.X" located in: : <Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anps_app\firmware\ble_anps_app.X using MPLAB X IDE.
  3. Open and program the application example "ble_anpc_app.X" located in: <Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anpc_app\firmware\ble_anpc_app.X using MPLAB X IDE.

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

Demo Description

This application demonstrates on simulating an ANPS on the Curiosity board. The on-board User button and LED are used as user interface during the demo.

  • Type of pressing on user button:
    • 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 WBZ351 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 WBZ351 Curiosity boards.

  1. Board 1 – WBZ351 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.
    1. 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 4-233. 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 Port

      Note: For more details on setting the “Serial Port” and “Speed”, refer to COM Port Setup.
    2. Reset the board (on-board Reset), Upon Reset, “ANPC init” will be displayed in Tera Term.
      Figure 4-234. Tera Term
  2. Board 2 – WBZ351 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.
  3. 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.
  4. Press the User button on the ANPS device to send a new alert notification to the ANPC device and the following will be displayed in Tera Term.
    Figure 4-235. Tera Term
  5. Press the User button 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 4-236. Tera Term
  6. 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.
  7. 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.
  8. Long press the User button 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

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_pic32cxbz3_wbz35\apps\ble\advanced_applications\ble_anps_app\firmware\ble_anps_app.X\ble_anps_app.mc3".
    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 4-237. Project Graph

Verify MCC Component Settings

  1. Select Alert Notification Profile component in project graph, to open component configuration and configure as illustrated in the following figure.
    Figure 4-238. Alert Notification Profile Configuration
  2. Select BLE Stack component in project graph, to open component configuration and configure as illustrated in the following figure.

    Figure 4-239. BLE Stack Configuration

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 the MCC interface by clicking Generate Code, the BLE configuration source and header files can then be found in the following project directories.
Figure 4-240. Project File
The OSAL, RF system and BLE system initialization routine executed during program initialization can be found in the project file. This initialization routine is automatically generated by the MCC.
Figure 4-241. initialization.c
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.
Figure 4-242. app_ble.c
Figure 4-243. Auto Generated Advertisement Data Format
Table 4-51. 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

src\app_ble\app_anps_handler.c

Handle ANPS event from application level

src\config\default\ble\profile_ble\ ble_anps\ble_anps.c

Handle ANPS event from profile level

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, L2CAP and SMP in APP_BleStackInit() function.

  • APP_BleStackInit() is the API that will be called inside the Applications Initial State -- APP_STATE_INIT in app.c

User Application Development

Include:

  • User action is required as mentioned User Action
  • 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 just 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)
Figure 4-244. 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 4-245. app_ble_handler.c

Initialize ANPS:

  • ANPS functionality initialization is done in APP_BleStackInitAdvance().
    Figure 4-246. app_ble.c

Implement ANPS:

  • ANPS is implemented in ble_anps.c and BLE_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.
Figure 4-247. Finite State Machine
Table 4-52. Time Out Parameters

Variable

Value

Description

timeout_scan

60 seconds

The timeout of scanning without bond

timeout_with_bond_scan

30 seconds

The timeout of scanning with bond

Table 4-53. LED vs. State

Firmware State

LED behavior

APP_ANPS_STATE_IDLE

All LEDs are OFF

APP_ANPS_STATE_SCAN

Blue LED Flashes once every 3 seconds. (ON: 50 ms, OFF: 2950 ms)

APP_ANPS_STATE_WITH_BOND_SCAN

Blue LED Flashes twice every 3 seconds. (ON: 50 ms, OFF: 50 ms)

APP_ANPS_STATE_CONNECTED

Blue LED Flashes twice every 1.5 seconds. (ON: 50 ms, OFF: 150 ms)