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.
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.
- See BLE Connection from Related Links.
- See BLE Transparent UART from Related Links.
- BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
Hardware Requirement
S. No. | Tool | Qty |
---|---|---|
1 | PIC32-BZ6 Curiosity Board | 1 |
2 | 1 | |
3 | Micro USB cable | 1 |
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
- Microchip Bluetooth Data (MBD)
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\peripheral_applications\touch_peripheral_trp_uart\precompiled_hex\touch_peripheral_trp_uart.X.production.signed.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
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
- 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
- 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.
- Program the precompiled hex file or application example as mentioned.
- Open Tera Term:
- Set the “Serial Port” to USB Serial Device.
- Speed to 115200.
- Press the NMCLR button on the curiosity
board to start advertisements. TeraTerm should display the following message.
- Open MBD on your smart phone and follow
the following images.
- Upon connection, TeraTerm should display
the following message.
- 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.
- 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.
- To disconnect the curiosity board and the
MBD running on your smart phone, do either one of the following:
- Press the NMCLR button on the PIC32-BZ6 Curiosity Board.
- Go back to the “SCAN” page in MBD.
- Upon successful disconnection, PIC32WM-BZ6204UE automatically advertises again.
Developing the Application from Scratch using the MPLAB Code Configurator
- 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\peripheral_applications\touch_peripheral_trp_uart\firmware\touch_peripheral_trp_uart.X\peripheral_trp_uart.mc3
. - Accept dependencies or satisfiers when prompted.
- Verify if the Project Graph window has
all the expected configuration.
Verifying Advertisement, Connection and Transparent UART Profile Configuration
-
Select the BLE Stack component in the Project Graph and verify the following in the Configuration Options panel.
- Select Transparent Profile
component in project graph and verify the following:
Verifying Touch Configuration
-
Select Touch Configuration under Plugins in the Project Graph:
- Verify the following settings:
-
-
where related pins are needed for Touch button and slider to work.
-
-
-
where Driven Shield is identified in the same table.
-
-
-
Verifying SERCOM Configuration
-
Select SERCOM0 component in the project graph and verify the following:
Verifying the Pin Configuration
-
Select Pin Configuration under Plugins in the Project Graph:
- 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.
Generating 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
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\app_ble.c | Source Code for the BLE stack related component configurations, code related to function calls from app.c |
app_ble\app_ble_handler.c | GAP, GATT, SMP and L2CAP event handlers |
app_ble\app_trsps_handler.c | All transparent UART server related event handlers |
ble_trsps.c | All transparent server functions for user application |
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 fileble_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
inapp.c
User Application Development
ble_trsps.h
inapp.c
, BLE Transparent UART Server related API's are available hereosal/osal_freertos_extend.h
inapp_trsps_handler.c
contain OSAL related APIsdefinitions.h
must be included in all the files where UART will be used to print debug informationNote: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
Where to go from here
BLE Sensor App utilizes the Transparent UART building block, see BLE Sensor from Related Links.