This section explains how to create a peripheral device and send/receive characters between
two connected BLE devices over Microchip proprietary Transparent UART Profile. Peripheral
device will be the PIC32-BZ6 Curiosity board and central device can
either be a Smart phone with Light Blue app or another 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 Transparent UART from
Related Links.
See BLE Connection from Related
Links
BLE Software Specification – See MPLAB® Harmony Wireless
BLE in Reference Documentation from Related Links.
Hardware Requirement
Table 5-20. Hardware Prerequisites
S. No.
Tool
Quantity
1
PIC32-BZ6 Curiosity Board
1
2
Micro USB cable
1
3
Android/iOS Smart phone
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
Light Blue iOS/Android app available in stores
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\building_blocks\Peripheral\profiles_services\peripheral_trp_uart\precompiled_hex\peripheral_trp_uart.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
peripheral_trp_uart.X located in <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\building_blocks\peripheral\profiles_services\peripheral_trp_uart\firmware\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 enables users to send data back and forth over UART between two connected
BLE devices. On Reset, demo application prints “Advertising” which denotes the start of
advertisements and then “Connected” when connection is established. Application data to be
sent to the connected central device (smart phone or another PIC32-BZ6 Curiosity board) is entered in a terminal emulator like Tera Term. Data
inputted in TeraTerm will be buffered and sent every 500 ms to allow for different data
communication behavior. Input data accumulated within the 500 ms will be sent in a single
BLE packet.
Testing
Using a Micro USB cable, connect the Debug USB on the Curiosity board to a PC. The
Active LED turns Green once connected to the PC.
Program the precompiled hex file or
application example as mentioned.
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
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.
Press the NMCLR button on the Curiosity
board.
Launch the Light Blue mobile app and
search for the device name “pic32cx-bz6” and press Connect. Users with an iOS
device may see the device name as “Microchip”.Figure 5-92. LightBlue®
App
To receive data from the PIC32-BZ6 Curiosity board (peripheral device) to the mobile app
(central device), select “UUID: 49535343-1E4D-4BD9-BA61-23C647249616” then select
Subscribe.Figure 5-93. LightBlue®
App
Figure 5-94. LightBlue®
App
Change the Data format to UTF-8 String in
the Light Blue mobile app then enter “test” on Tera Term. The same data must be displayed
in the app.
Note: User may not be able to see “test” on the Tera Term
as user types.
Figure 5-95. LightBlue®
App
To send data from the mobile app (central
device) to the PIC32-BZ6 Curiosity board (peripheral device),
select “UUID: 49535343-8841-43F4-A8D4-ECBE34729BB3”.Figure 5-96. LightBlue®
App
Change the Data format to UTF-8
String then enter “trp uart” and click Write. The same data must be
displayed in Tera Term (see the following figure).
Note: Data
inputted in TeraTerm will be buffered and sent every 500 ms. Input data accumulated
within the 500 ms will be sent in a single BLE packet.
Figure 5-97. LightBlue®
App
Note: Users can use another PIC32-BZ6
Curiosity board configured as BLE Transparent UART (central) instead of using a mobile
app. For more information, refer to BLE Transparent UART from Related Links.
Developing the Application from Scratch
using MCC
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.
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\building_blocks\peripheral\profiles_services\peripheral_trp_uart\firmware\peripheral_trp_uart.
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.
Note: Ensure that the macro
configUSE_TICKLESS_IDLE is set to 0. The macro is
available in FreeRTOSConfig.h file
#define configUSE_TICKLESS_IDLE 0
Figure 5-98. Project Graph
Verifying Advertisement,Connection and
Transparent UART Profile Configuration
Select BLE Stack component in project graph, to open component configuration and
configure as illustrated in the following figure.
Figure 5-99. BLE Stack Configuration
Note: If users cannot see the Configuration
Options panel in the right-hand side of the MPLAB X IDE, it might be minimized. Hover
the cursor towards the Configuration Options side tab and click the “dot” on the top
right-hand corner to pin it (see the following figure).Figure 5-100. Configuration Panel
.
Select Transparent Profile component in project graph, to open component configuration and
configure as illustrated in the following figure.
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
After generating the program source from the MCC interface by clicking Generate Code, the BLE
configuration source and header files can be found in the following project directories.Figure 5-101. Project File
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
files.Figure 5-102. 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 5-103. app_ble.c
Autogenerated, Advertisement Data FormatFigure 5-104. AD Structures and Types
Table 5-21. 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\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
Note:app.c is autogenerated and has a state machine
based application code. Users can use this template to develop their own application.
Header Files
ble_gap.h: Contains BLE GAP
functions and is automatically included in the app.c file.
ble_trsps.h: is associated with APIs 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, SMP and L2CAP 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
Include the user action. For more information, refer to User Action from Related
Links.
ble_trsps.h in
app.c, BLE Transparent UART Server related APIs.
osal/osal_freertos_extend.h in app_trsps_handler.c,
OSAL related APIs are available here.
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.
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 informationFigure 5-107. app_ble_handler.c
Transmit Data
Add “APP_MSG_UART_CB” and “APP_TIMER_SEND_UART_MSG”
to the generated APP_MsgID_T.
Figure 5-108. app.h
BLE_TRSPS_SendData(conn_hdl , 1, &data); is the API to be used for
sending data to the central device.
Note: The precompiled application
example uses a UART callback to initiate the data transmission upon receiving a
character in UART.
Example for transmitting over UART using the BLE_TRSPS_SendData()
API
#define UART_DATA_MAX 25
uint16_t conn_hdl;// connection handle info captured @BLE_GAP_EVT_CONNECTED event
uint16_t ret;
uint8_t uart_data;
uint8_t uartBuf[UART_DATA_MAX];
uint8_t uartBufNum;
void uart_cb(SERCOM_USART_EVENT event, uintptr_t context)
{
APP_Msg_T appMsg;
// If RX data from UART reached threshold (previously set to 1)
if( event == SERCOM_USART_EVENT_READ_THRESHOLD_REACHED )
{
// Read 1 byte data from UART
SERCOM0_USART_Read(&uart_data, 1);
appMsg.msgId = APP_MSG_UART_CB;
OSAL_QUEUE_Send(&appData.appQueue, &appMsg, 0);
}
}
void APP_SendUartData()
{
// Send the uartBuf to connected device through Transparent service
if(uartBufNum == 0)
return;
BLE_TRSPS_SendData(conn_hdl, uartBufNum, uartBuf);
memset(uartBuf, 0 , sizeof(uartBuf));
uartBufNum = 0;
}
void APP_UartCBHandler()
{
uartBuf[uartBufNum] = uart_data;
if(++uartBufNum == UART_DATA_MAX)
{
APP_TIMER_StopTimer(APP_TIMER_SEND_UART);
APP_SendUartData();
}
else
APP_TIMER_SetTimer(APP_TIMER_SEND_UART,APP_TIMER_500MS, false);
}
// Enable UART Read
SERCOM0_USART_ReadNotificationEnable(true, true);
// Set UART RX notification threshold to be 1
SERCOM0_USART_ReadThresholdSet(1);
// Register the UART RX callback function
SERCOM0_USART_ReadCallbackRegister(uart_cb, (uintptr_t)NULL);
APP_BleStackInit();
// Start Advertisement
BLE_GAP_SetAdvEnable(0x01, 0x00);
// Reset the uart buffer
memset(uartBuf, 0, sizeof(uartBuf));
uartBufNum = 0;
SERCOM0_USART_Write((uint8_t *)"Advertising\r\n",13);
Figure 5-109. app.c
Figure 5-110. app.c
else if(p_appMsg->msgId==APP_MSG_UART_CB)
{
// Pass BLE UART Data transmission target BLE UART Device handling
APP_UartCBHandler();
}
else if(p_appMsg->msgId== APP_TIMER_SEND_UART_MSG)
{
APP_SendUartData();
}
Figure 5-111. app.c
Receive Data
BLE_TRSPS_EVT_RECEIVE_DATA is
the event generated when data is sent from the central device
Use the
BLE_TRSPS_GetDataLength(p_event->eventField.onReceiveData.connHandle,
&data_len); API to extract the length of the application data received
BLE_TRSPS_GetData(p_event->eventField.onReceiveData.connHandle,
data); API is used to retrieve the data
Note:BLE_TRSPS_Event_T p_event structure stores the information about BLE
transparent UART callback functions.
Example for printing the received data from the central device over UART
/* TODO: implement your application code.*/
uint16_t data_len;
uint8_t *data;
// Retrieve received data length
BLE_TRSPS_GetDataLength(p_event->eventField.onReceiveData.connHandle, &data_len);
// Allocate memory according to data length
data = OSAL_Malloc(data_len);
if(data == NULL)
break;
// Retrieve received data
BLE_TRSPS_GetData(p_event->eventField.onReceiveData.connHandle, data);
// Output received data to UART
SERCOM0_USART_Write(data, data_len);
// Free memory
OSAL_Free(data);
Figure 5-112. app_trsps_handler.c
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 – This application utilizes the
Transparent UART building block, see BLE Sensor from Related Links.