4.1.2.2 Bluetooth®LE Extended Advertisements

This section helps users to enable extended advertisements (1M, 2M, Coded PHY - 125 kbps) on the PIC32WM-BW1 Curiosity Board. This example enables users to send application data using extended advertisements. Extended Advertisements are used to send more data than what legacy advertisements allow and also has long range functionality when using Coded PHY. Use of Extended Advertisements also enables the users to select between different PHYs (1M, 2M and LE Coded) which are not permitted when using legacy advertisements. In BLE, a peripheral or broadcaster always starts with advertisements. Advertisement packets enable a central or observer to discover a peripheral or broadcaster.

Extended Advertisements implemented in this example can be used for sending more data (2M PHY) or do range tests (Coded PHY). For testing these features, a scanner application can be used which is configured to scan the extended advertisement packets.

To save power, low power mode can be enabled. If conditions are met, then the PIC32WM-BW1 will enter into Sleep mode. The following sections discuss it in detail.

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

Hardware Requirement

Table 4-9. Hardware Requirements
Tool Qty
PIC32WM-BW1 Curiosity Board2
USB Type-C cable2

Software Requirement

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

Programming the Precompiled Hex File or Application Example

Programming the hex file using MPLABX IPE

  1. Import and program the Precompiled Hex file: “<Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_ext_adv\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.

Programming the application using MPLABX IDE

  1. Follow steps mentioned in the Running a Precompiled Application Example section from Related Links.
  2. Open and program the Application: “<Harmony Content Path>\wireless_apps_pic32_bw1\apps\ble\peripheral\peripheral_ext_adv\firmware\ext_adv.X

For more details on finding the Harmony Content Path, refer to Installing the MCC Plugin section from Related Links.

Testing

  1. Users must use two PIC32WM-BW1 boards configured as BLE Extended Advertiser and BLE Extended Advertisement Scanner.
  2. Program PIC32WM-BW1 Curiosity Board 1 with BLE Extended Advertisement and open Tera Term configured with following settings through Terminal Settings:
    1. Baud rate/ Speed – 115200
    2. Parity – None
    3. Data bits – 8
    4. Stop bits – 1
    5. Flow Control – None
  3. Reset the board. Upon Reset, “Ext Adv Enabled” will be displayed. This indicates successful start of Coded PHY Advertisements and are configured to be sent every 2 seconds.
  4. Program PIC32WM-BW1 Curiosity Board 2 with BLE Scanning Extended Advertisement application and open Tera Term configured with following settings through Terminal Settings:
    1. Baud rate/ Speed – 115200
    2. Parity – None
    3. Data bits – 8
    4. Stop bits – 1
    5. Flow Control – None
  5. Reset the board. Upon reset, “ExtAdv Scan Enable Success” displays on the terminal. Application data (“Microchip”) is transmitted in Auxiliary packets (ADV_AUX_IND) and ADV_EXT_IND carries the Auxiliary Packet Information (ADV_AUX_IND) for the scanner to collect the data.
  6. “Microchip” is displayed on the screen as soon as PIC32WM-BW1 Module performs an extended advertisement scan.
    Figure 4-58. Extended Advertisement Enabled
Note: The Low Power Enable power numbers have not been measured for PIC32CXBZ2 demo application on PIC32WM-BW1.

Project Graph

This section explains how MCC Project graph should look like and component configurations.

  1. Verify if the Project Graph window has all the expected configuration as illustrated in the following figure.
Figure 4-59. Project Graph

MCC Component Configuration

  1. Enable low power mode by selecting Low Power Enable in BLE Stack configuration. This must add RTC module component
    Figure 4-60. BLE Stack Configuration
  2. Then click on Windows>MPLAB Code Configurator v5>Harmony>Clock Configuration
    Figure 4-61. Clock Configuration

Verify Advertisement Configuration

  1. Select the BLE Stack component in the Project Graph and configure the following in the Configuration Options panel

    Figure 4-62. BLE Stack Configuration

Configuring UART

For more details on UART Configuration, refer to UART Hello World section 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.
Figure 4-63. Project File Structure
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-64. 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.

Auto generated Advertisement Data Format

Source Files Usage
app.cApplication State machine, includes calls for Initialization of all BLE stack (GAP,GATT, SMP, L2CAP) related component configurations
app_ble\app_ble.cSource Code for the BLE stack related component configurations, code related to function calls from app.c
app_ble\app_ble_handler.cAll GAP, GATT, SMP and L2CAP Event handlers
app_user_edits.cUser Code Change instruction
Note: app.c is auto generated 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()
  • APP_BleStackInit() is the API that will be called inside the Applications Initial State APP_STATE_INIT in app.c
  • RTC_Timer32Start() defined in plib_rtc_timer.c calls to start the RTC Timer clock to facilitate low power mode

User Application Development

  1. Include

    • User Action is required
    • 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
  2. Starting Extended Advertisement in app.c

    // Enable Ext Adv
    BLE_GAP_ExtAdvEnableParams_T extAdvEnableParam;
    extAdvEnableParam.advHandle = 0x01; // Adv Set - 0x01
    extAdvEnableParam.duration = 0;
    extAdvEnableParam.maxExtAdvEvts = 0; // Enable Cont ADV
    ret = BLE_GAP_SetExtAdvEnable(true, 0x01, &extAdvEnableParam);
    if (ret == MBA_RES_SUCCESS)
    SERCOM0_USART_Write((uint8_t *)"Ext Adv Enabled\r\n", 17);

    This API is called in the applications initial state APP_STATE_INIT in app.c. Parameters for the extended advertisement must be chosen. Below is an example.

    Figure 4-65. app.c
  3. GPIO_LED_Initialize() in app.c
    uint16_t ret;
    void GPIO_LED_Initialize ( void )
    {
    CFG_REGS->CFG_CFGCON0CLR = CFG_CFGCON0_JTAGEN_Msk;
    /* PORTA Initialization */
    /* PORTB Initialization */
    /* PPS Input Remapping */
    /* PPS Output Remapping */
    /* PA */
    GPIOA_REGS->GPIO_TRISSET = 0xFFFF; //Set all pins as input
    /* PB */
    GPIOB_REGS->GPIO_ANSELSET = 0x0040; //PB6 ANSEL for Temp sensor
    GPIOB_REGS->GPIO_TRISSET = 0xFFFF; //Set all pins as input
    GPIOB_REGS->GPIO_CNPUSET = 0xF886; //Pull up: PRB 1, 2, 7, 11, 12 , 13 ,14 , 15
    GPIOB_REGS->GPIO_CNPDSET = 0x0029; //Pull down RB0,3,5 for LED
    }