5.3.4 BLE and Touch Transparent UART

This section explains how to create a peripheral device and send Touch data over the Microchip proprietary Transparent UART Profile using a QT7 Xplained Pro Extension Board. The peripheral device is a PIC32-BZ6 Curiosity Board.

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.

Hardware Requirement

Table 5-73. Hardware Requirement
S. No. Tool Qty
1PIC32-BZ6 Curiosity Board1
2

QT7 Xplained Pro Extension Kit

1
3Micro USB cable1

SDK Setup

Refer to Getting Started with Software Development from Related Links.

Software Requirement

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

Smart phone App

  1. Microchip Bluetooth Data (MBD)

Programming the Precompiled Hex File or Application Example

Using MPLAB® X IPE:

  1. Import and program the precompiled hex file: <Harmony Content Path>\wireless_apps_pic32_bz6apps\ble\peripheral_applications\touch_peripheral_trp_uart\precompiled_hex\touch_peripheral_trp_uart.X.production.signed.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.

Using MPLAB® X IDE:

  1. Perform the following the steps mentioned in Running a Precompiled Example. For more information, refer to Running a Precompiled Application Example from Related Links.
  2. Open and program the application can_peripheral_trp_uart.X located in <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\ peripheral _applications\touch_peripheral_trp_uart\firmware\touch_peripheral_trp_uart.X.
  3. 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 the capability of the PIC32-BZ6 Curiosity Board to connect to a smart phone through Bluetooth Low Energy (BLE). The RGB LED status and intensity on the curiosity board can also be controlled by using the Touch button and slider of the connected QT7 Xplained Pro Extension Board.

Testing

  1. Using a micro USB cable, connect the DBG USB of the curiosity board to a PC. Likewise, connect the QT7 Xplained Pro Extension Board to the XPRO HEADER pins of the curiosity board. See connections below.
  2. Program the precompiled hex file or application example as mentioned.
  3. Open Tera Term:
    • Set the “Serial Port” to USB Serial Device.
    • Speed to 115200.
    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.
  4. Press the NMCLR button on the curiosity board to start advertisements. TeraTerm should display the following message.
  5. Open MBD on your smart phone and follow the following images.
  6. Upon connection, TeraTerm should display the following message.
  7. Press Button 1 on the QT7 Xplained Pro Extension Board to turn on the RGB LED on the curiosity board and press it again to turn it off. Button 1 LED should reflect this. The MBD and TeraTerm should also show the RGB LED Status.
  8. Press Button 1 to turn it on again and slide your fingers across the Slider of the QT7 Xplained Pro Extension Board. The lowest slider position will bring the RGB LED intensity low and the highest slider position will bring it high. The intensity is on a scale from 40 to 220. The 6 Slider LEDs should reflect this. The MBD and TeraTerm should also show the RGB LED Intensity value.
    Note: Changing the Slider position will not change the RGB LED intensity until it is turned on by Button 1.
  9. To disconnect the curiosity board and the MBD running on your smart phone, do either one of the following:
    1. Press the NMCLR button on the PIC32-BZ6 Curiosity Board.
    2. Go back to the “SCAN” page in MBD.
  10. Upon successful disconnection, PIC32WM-BZ6204UE automatically advertises again.

Developing the Application from Scratch using the MPLAB Code Configurator

Follow the steps below to build the application manually:
Note: It is recommended for the new users of the MPLAB Code Configurator to refer MPLAB® Code Configurator (MCC) User’s Guide in Reference Documentation from Related Links.
  1. Create a new harmony project. For more details, see Creating a New MCC Harmony Project from Related Links.
  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_pic32_bz6\apps\ble\peripheral_applications\touch_peripheral_trp_uart\firmware\touch_peripheral_trp_uart.X\peripheral_trp_uart.mc3.
  3. Accept dependencies or satisfiers when prompted.
  4. Verify if the Project Graph window has all the expected configuration.

Verifying Advertisement, Connection and Transparent UART Profile Configuration

  1. Select the BLE Stack component in the Project Graph and verify the following in the Configuration Options panel.
  2. Select Transparent Profile component in project graph and verify the following:

Verifying Touch Configuration

  1. Select Touch Configuration under Plugins in the Project Graph:
  2. Verify the following settings:
    1. where related pins are needed for Touch button and slider to work.
    2. where Driven Shield is identified in the same table.

Verifying SERCOM Configuration

  1. Select SERCOM0 component in the project graph and verify the following:

Verifying the Pin Configuration

  1. Select Pin Configuration under Plugins in the Project Graph:
  2. Verify the Pin Settings:
    where RC7, RC10 and RE0 are related to the RGB LED Service in the Project Graph

    and RB1, RB3, RB14, RA8, RA4, RE5 and RD2 are related to Touch slider and button LEDs.

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:
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. This initialization routine is automatically generated by the MCC.
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.
Autogenerated, Advertisement Data Format
Table 5-74. Source File
Source FilesUsage
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.cGAP, GATT, SMP and L2CAP event handlers
app_ble\app_trsps_handler.cAll transparent UART server related event handlers
ble_trsps.cAll transparent server functions for user application
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
  • ble_trsps.h: This header file associated with API’s and structures related to BLE Transparent Client functions for Application User

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
  • ble_trsps.h in app.c, BLE Transparent UART Server related API's are available here
  • osal/osal_freertos_extend.h in app_trsps_handler.c contain OSAL related APIs
  • 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.

Touch Timer Handler Modification

  • The touch timer handler in touch.c was modified to generate a custom event (APP_MSG_TOUCH_MEAS) when a touch measurement period elapses. This event is sent to the application queue for further processing.
  • This custom event is defined in app.h.

Touch Measurement Processing

  • The APP_Touch_Measurement function processes the touch data by calling the touch_process function. If a touch measurement is completed, it calls APP_Touch_Status_Display to update the LED status and intensity.

Updating and Transmitting LED Status and Intensity

  • The APP_Touch_Status_Display function updates the LED status and intensity based on the touch input. It also transmits the updated status and intensity to the connected BLE peripheral using BLE_TRSPS_SendData() API.
    Note: The scroller_position threshold values have been hard-coded according to the ADC values. Different boards may have slight variations, therefore these thresholds should be tuned to specific boards. If the LEDs are found to be not synchronized with the scroll, then these values need to be adjusted according to the ADC values, where an infinite loop checks each ADC and lights up the corresponding LEDs.

Starting Advertisement in app.c

  • BLE_GAP_SetAdvEnable(0x01, 0x00);

Connected & Disconnected Events

  • In app_ble_handler.c, BLE_GAP_EVT_CONNECTED event will be generated when a BLE connection is completed.

Connection Handler

  • Connection handle associated with the peer peripheral device needs to be saved for data exchange after a BLE connection
  • p_event->eventField.evtConnect.connHandle has this information
  • Start advertising upon disconnection

Note: Users can explore more BLE Advertisement functionalities using the BLE Stack APIs. For more information, refer to BLE Stack in Reference Documentation from Related Links.

Where to go from here

BLE Sensor App utilizes the Transparent UART building block, see BLE Sensor from Related Links.