5.1.1.7 BLE Transparent UART Central with LE Coded Phy

This document enables users create a central device and send/receive characters between two connected BLE devices over Microchip proprietary Transparent UART Profile. The central and peripheral devices using LE Coded Phy in this section are two WBZ351devices. The following instructions are applicable for a BLE Central device.

Users can choose to either run the precompiled Application Example hex file provided on the WBZ351 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-11. Hardware Prerequisites
S. No. Tool Quantity
1WBZ351Curiosity Board2
2Micro USB cable2

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.

Smartphone App

None

Programming the Precompiled Hex File or Application Example

Using MPLAB® X IPE:

  1. Central Device – Import and program the precompiled hex file: <Discover Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\central\profiles_services\central_trp_uart_codedPhy\hex.
  2. Peripheral Device – Import and program the precompiled hex file: <Discover Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\peripheral\profiles_services\peripheral_trp_uart_codedPhy\hex.
  3. 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. Central Device – Open and program the application central_trp_uart.X located in <Discover Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\central\profiles_services\central_trp_uart_codedPhy\firmware.
  3. Peripheral Device – Open and program the application peripheral_trp_uart.X located in <Discover Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\peripheral\profiles_services\peripheral_trp_uart_codedPhy\hex.
  4. For more details on how to find the Discover path, refer to Download Application Example from Discover in Running a Precompiled Application Example from Related Links.

Demo Description

Upon programming the demo application, central device (WBZ351) will start scanning for near by peripheral devices to connect. After a connection has been made data can be sent back and forth over UART between the two devices that are connected.

Demo will print start of the scanning “Scanning”,connected “Connected!” and disconnected “Disconnected” state on a terminal emulator like TeraTerm (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none) The Application Data sent to the connected peripheral device must be entered in the terminal emulator.

Testing

Users must use another WBZ351 Curiosity Board configured as BLE Transparent UART Central with LE Coded Phy, see BLE Transparent UART Central with LE Coded Phy from Related Links.

Demo Experience when using 2 WBZ351Curiosity boards configured as Peripheral and Central device. This section assumes that a user has already programmed the peripheral_trp_uart_codedPhy and central_trp_uart_codedPhy application on two WBZ351Curiosity Boards.

Board 1: WBZ351Curiosity Board with peripheral_trp_uart_codedPhy application Programmed

Board 2 : WBZ351Curiosity Board with central_trp_uart_codedPhy application Programmed

Board 1:
  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
  2. Reset the board. Upon reset, “Advertising” message is displayed on the TeraTerm.
Figure 5-71. Tera Term Setup
Board 2:
  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
  2. Reset the board. Upon reset, “Advertising” message is displayed on the TeraTerm.
  3. Upon finding peripheral device with public address {0xA1, 0xA2, 0xA3, 0xA4, 0xA5, 0xA6} message “Found Peer Node” will be displayed and a connection request will be initiated "Initiating connection".
Note: Scanner is configured to scan only for 100 seconds. The 100 seconds scanning time will restart after a connection. Ensure the peer device is advertising.

After connection establishment, both the peripheral device (Board1) and central device (Board2) will display “Connected!” message on respective terminal windows.

Users can now start sending data back and forth between the central and peripheral device using the terminal emulator. Characters entered on either terminals will immediately be sent to the peer devices

Figure 5-72. Tera Term Logs

Developing the Application from scratch using 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 .mc4 and is located in the path "<Discover Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\central\profiles_services\central_trp_uart_codedPhy\firmware\central_trp_uart.X". For more details on importing the component configuration, refer to Importing Existing App Example Configuration from Related Links.

    Note: Import and Export functionality of component configuration will help users to start from a known working setup of configuration
  3. Accept Dependencies or satisfiers, select Yes.

  4. Verify if the Project Graph window has all the expected configuration.

    Figure 5-73. Project Graph

Verifying Scanning ,Connection and Transparent Profile Configuration

  1. Select BLE Stack component in project graph.
    Figure 5-74. BLE Stacks
  2. Select Transparent Profile component in project graph.
    Figure 5-75. Transparent Profile

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

Figure 5-76. Project Directory

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.

Figure 5-77. initialization.c

The BLE stack initialization routine excuted during Application Initialization can be found in project files. This intitialization routine is automatically generated by the MCC. This call initializes and configures the GAP, GATT, SMP, L2CAP and BLE middleware layers.

Figure 5-78. app_ble.c
Table 5-12. Source Files
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.c

Source 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_ble\app_trspc_handler.cAll Transparent UART Client related Event handlers
ble_trspc.cAll Transparent Client Functions for user application
Note: app.c is autogenerated and has a state machine based Application code sample. The users can refer to this template while developing their application

Header Files

  • ble_gap.h : This header file contains BLE GAP functions and is automatically included in the app.c file

  • ble_trspc.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

  • Include the user action. For more information, refer to User Action from Related Links.
  • ble_trspc.h in app.c, BLE Transparent UART Server related API's are available here.

  • osal/osal_freertos_extend.h in app_trsps_handler.c, OSAL related API’s are available here

  • definitions.h in all the files where UART will be used to print debug information
    Note: definitions.h is not specific to just UART peripheral, instead it should be included in all application source files where peripheral functionality will be exercised.

Start Scanning

BLE_GAP_SetExtScanningEnable(BLE_GAP_SCAN_MODE_OBSERVER, &extScan );

This API is called in the Applications initialstate - APP_STATE_INIT in app.c. Scan duration is 100 secs

Scan Results and initiating a BLE connection

  • BLE_GAP_EVT_EXT_ADV_REPORT event is generated upon finding Adverstisements on legacy channels

  • BLE connection can be initiated by using the BLE_GAP_ExtCreateConnection

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

Transmit Data

  • Add APP_MSG_UART_CB to the generated APP_MsgId_T
    Figure 5-79. 
  • BLE_TRSPC_SendData(conn_hdl , 1, &data); is the API to be used for sending data towards the central device
    Note: The precompiled application example uses a UART callback to initiate the data transmission upon receiving a character on UART.

Example Implementation for Transmitting the received data over UART using the BLE_TRSPC_SendData API

Figure 5-80. 
Figure 5-81. .

Receive Data

  • BLE_TRSPC_EVT_RECEIVE_DATA is the event generated when data is sent from central device

  • Users need to use the BLE_TRSPC_GetDataLength(&data_len) API to extract the length of application data received

  • BLE_TRSPC_GetData(&conn_hdl, data); API is used to retrieve the data, conn_hdl is the value obtained from Connection Handler section

Note: BLE_TRSPC_Event_T p_event structure stores the information about BLE transparent UART callback functions

Example Implementation for printing the received data from peripheral device over UART

  /* TODO: implement your application code.*/
            uint16_t data_len;
            uint8_t *data;
            // Retrieve received data length
            BLE_TRSPC_GetDataLength(p_event->eventField.onReceiveData.connHandle, &data_len);
            while (data_len) {
                // Allocate memory according to data length
                data = OSAL_Malloc(data_len);
                if (data == NULL)
                    break;
                // Retrieve received data
                BLE_TRSPC_GetData(p_event->eventField.onReceiveData.connHandle, data);
                // Output received data to UART
                SERCOM0_USART_Write(data, data_len);
                // Free memory
                OSAL_Free(data);

                //check again
                data_len = 0;
                BLE_TRSPC_GetDataLength(p_event->eventField.onReceiveData.connHandle, &data_len);
            }
Note: The users can exercise various other BLE Advertisement functionalities using the BLE Stack APIs. For more information, refer to BLE Stack in Reference Documentation from Related Links.