4.3 HCI Application

Introduction

This document aims to provide a concise guide on developing Human-Computer Interaction (HCI) applications utilizing the Bluetooth® Low Energy (BLE) UART transport interface.

In the context of HCI with BLE UART, communication is established using the BLE protocol along with a Universal Asynchronous Receiver-Transmitter (UART) interface. BLE is a wireless communication standard designed for short-range communication between devices, offering lower power consumption than classic Bluetooth. UART is a hardware communication protocol that enables serial communication by transmitting data as a sequence of individual bits.

Combining BLE with UART allows for seamless and efficient data transmission between devices like microcontrollers, computers, and mobile phones without requiring a lot of power. This setup is particularly beneficial for applications where small data packets are exchanged frequently over moderate distances, such as in wearable devices, home automation systems and other IoT applications.

Thus, using BLE UART in HCI applications enables developers to create interfaces that can communicate wirelessly with user input and output devices, enhancing the user experience by integrating physical computing devices smoothly into human activities.

Users have the option to either execute the pre-built application example hex file on the WBZ351 Curiosity Board for an immediate demonstration or to delve into the detailed process of building the application from the ground up.

For an optimal learning experience, it is recommended to approach the examples sequentially, starting with fundamental concepts and gradually advancing to more complex topics.

Recommended Reads

Hardware Requirement

Tool

Quantity

WBZ351 Curiosity Board

1

Micro USB cable

1

FTDI Cable1

SDK Setup

Software Requirement

Programming the Precompiled Hex File or Application Example

Steps to programming the hex file using MPLAB X IPE:

  1. Import and program the precompiled hex file: <Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\HCI\wbz351_ble_uart_app\hex.
  2. For more details on the steps, go to Programming A Device.
    Note: Ensure to choose the correct device and tool information.

Steps to programming the Application using MPLAB X IDE:

  1. Follow the steps mentioned in Running a Precompiled Example.
  2. Open and program the application: HCI_application.X.production.x located in <Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\HCI\wbz351_ble_uart_app\firmware using MPLABX IDE.

For more details on how to find the Harmony Content Path, refer to Installing MCC Plugin.

Demo Description

This application example showcases the implementation of Human-Computer Interaction (HCI) capabilities. The testing phase of the application involves using a WBZ351 Curiosity Board as the host device, which is interfaced with a Linux desktop computer. Once the setup is correctly configured, communication with the BLE_UART is established through the transmission of HCI commands. This allows for the evaluation and demonstration of the application's HCI features in a controlled environment.

Developing the Application from Scratch Using the MPLAB Code Configurator

This section explains the steps required by a user to develop this application example from scratch using the MPLAB Code Configurator.

Note: It is recommended that new users of the MPLAB Code Configurator to go through this overview.
  1. Create a new MCC Harmony Project For more details, refer to Creating a New MCC Harmony Project.
  2. To setup the basic components and configuration required to develop this application, import component configuration:

    The imported file is of format .mc3 and is located in the path <Harmony Content Path>\wireless_apps_pic32cxbz3_wbz35\apps\ble\building_blocks\peripheral\legacy_ADV_Pathloss\firmware\legacy_ADV_Pathloss.XFor more details, refer to Import existing App Example Configuration.

    Note: Import and export functionality of the Harmony component configuration will help users to start from a known working setup of the MCC configuration.
  3. To accept dependencies or satisfiers, select “Yes”.
  4. Verify if the project graph window has all the expected configuration.

Verifying Configurations

Components Present in the Project Graph

  1. Select BLE_Stack component in project graph.
    1. Enable BLE Controller Only option by clicking the tick on the checkbox.
  2. Select Systemcomponent in project graph.
    1. Choose DIRECT in SERCOM0 Direct Pin Enable option.
  3. Select SERCOM0 component in project graph.
    1. Ensure that the following configuration has done.

Verifying the DMA Configuration

  1. Select the drop-down symbol for the Plug-ins option and select DMA Configuration.
  2. Select the Channel Number DMAC Channel 0 from the Active Channels List .
    1. Once you select DMAC Channel 0, the respective channel’s settings will populate the right side of the Active Channel List.
    2. Ensure that the settings match those shown in the following figure.
  3. Select the Channel Number DMAC Channel 1 from the Active Channels List .
    1. Once you select DMAC Channel 1, the respective channel’s settings will populate the right side of the Active Channel List.
    2. Ensure that the settings match those shown in the following figure.
  4. Enable Run Channel in Standby Mode for DMAC Channel 0 and 1 in the System Component Configuration.

NVIC Configuration

  1. Click the drop-down symbol for the Plugins option and select NVIC Configuration.
  2. Set DMA priority as 3 for DMAC_0_3 (DMA Channel 0..3).
    1. Tick the enable check box.
    2. Set the Priority to 3.
    3. Set the handler name to DMAC_0_3_InterruptHandler.

PIN Configurations

  1. Click the drop-down symbol for the Plug-ins option and select PIN Configuration.
  2. Configure RA5 and RA6 as GPIO pins and set these UART Rx/Tx pins to pull high.

Generating a Code

For more details on code generation, refer to MPLAB Code Configurator (MCC) Code Generation.

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.

User Application Development

  1. Include
    • User action is required as mentioned in User Action
    • definitions.h in all the files where UART will be used to print debug information
      Note: The 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.
  2. In the App.h file, reduce the msgData array size in the APP_Msg_T structure.
    Modify the size of msgData to 32 bytes from the default size of 256 bytes. This change is recommended because when the App task sends a message, it disables all interrupts. A smaller message size can reduce the duration of the critical section.
  3. How to process received HCI commands and ACL packets:
    • Define the UART packet type – The HCI MCC configuration is transport independent and requires the creation of the packet type definition when deciding to use UART transport.
      HCI Packet TypeHCI Packet Indicator
      HCI Command Packet0x01
      HCI ACL Data Packet0x02
      HCI Synchronous Data Packet0x03
      HCI Event Packet0x04
      HCI ISO Data Packet0x05
    Example:
  4. Parse incoming UART data to identify the packet type and the corresponding format to determine the packet length. This is crucial to ensure the receipt of the complete packet (refer to the Reference section for the HCI packet format).
    Example of receiving a packet:
    1. Invoke DMAC_ChannelTransfer() with a block size of 4 bytes to capture the first 4 bytes, which include the packet type and header.
    2. Verify the packet type and extract the length (note that the buffer size is currently 251 bytes; the upper byte is always 0x00, so extracting the first byte of length from the ACL packet is sufficient).
    3. Use DMAC_ChannelTransfer() again to collect the remaining data based on the length specified in the packet header.
    4. Once a complete HCI packet is received via the UART DMA RX callback, send a message containing the command or ACL packet to the App layer.
    5. Depending on the packet type, invoke HCI_Cmd() or HCI_AclTx() API to pass the received packet to the controller.
  5. How to process and output HCI events and ACL packets:
    1. Implement the code to output packets via UART in the APP_BleStackEvtHandler() section of app_ble_hci.c File
  6. Handle HCI vendor commands by parsing and processing them in the APP_BleStackEvtHandler() section of app_ble_hci.c file.

Use the HCI_AppCmdComplEvt() or HCI_AppCmdStatusEvt() APIs to respond to vendor commands.

Different BLE Stack Memory Configuration

There are three build option examples on how to reduce memory size if it is a concern; otherwise, use the default setting to maintain full functionality.

Feature

Define

Default Setting

Common Memory Size

Note

Baud Rate

Central role high throughput

APP_CEN_SINGLE_HIG_THROUGH

Disable

16KB

ACL Tx buffer number = 12

921600

Central role low throughput

APP_CEN_SINGLE_LOW_THROUGH

Disable

14KB

ACL Tx buffer number = 4

115200

Peripheral role low throughput

APP_PER_SINGLE_LOW_THROUGH

Disable

14KB

ACL Tx buffer number = 4

115200

Full function

Enable

28KB

ACL Tx buffer number = 12

115200

  • The values for common memory size mentioned above are obtained from the BLE Memory Usage. If additional features are added, please refer to this section and increase the common memory size (Data memory) in initialization.c.
  • When using high throughput for the Central, set the UART baud rate to 921600; otherwise, set it to 115200 in the plib_sercom0_usart.c file.

Rework HCI Interface for WBZ351 Curiosity Board

The WBZ351 must be reworked to disconnect the chip from the MCP2200.

Reworks Steps:

1.Remove R1 and R2.

2.Add a Jumper in R1 and R2.

3.Add an external pin for TP9 and TP10.

Note:
  • When we need to input the UART data in TP9(RXD) and TP10(TXD), we need to remove R1 and R2 jumper to disconnect MCP2200.
  • If we use TP9 and TP10 to input UART data and do not removing the R1 and R2 jumpers, the UART data will be affected by the MCP2200, and the data may be incorrect.
  • When we need to update HEX file, we need to connect the R1 and R2 jumper.
Table 4-55. Pin to Pin connection

FTDI

WBZ351

VCCIO

X

TXD

RX

RXD

TX

RTS

CTS

GND

GND

CTS

RTS

References

HCI Command Packet Format:
Figure 4-249. HCI Command Packet Format

Op_Code: Size: 2 octets

ValueParameter Description
0xXXXX

OGF Range (6 bits): 0x00-0x3F (0x3F reserved for vendor-specific debug commands)

OCF Range (10 bits): 0x0000-0x03FF

Parameter_Total_Length: Size: 1 octet

ValueParameter Description
0xXX

Lengths of all of the parameters contained in this packet measured in octets. (N.B.: total length of parameters, not number of parameters)

HCI ACL Packet Format:
Figure 4-250. HCI ACL Packet Format

Handle: Size: 12 bits

ValueParameter Description
0xXXX

Connection_Handle to be used for transmitting a data packet or segment over a Primary Controller.

Logical_Link_Handle to be used for transmitting a data packet over an AMP Controller.

On the receiving side (AMP Controller to Host), the least significant 8 bits of the Handle is the Physical_Link_Handle and the most significant 4 bits are reserved. (This is because certain AMP Controllers might not be able to provide the logical link context on the receiving side, and the Host does not require it.)

Range: 0x000-0xEFF (0xF00 - 0xFFF Reserved for future use)

Packet_Boundary_Flag: Size: 2 Bits
ValueParameter DescriptionACL-UAMP-ULE-U
0x00

First non-automatically flushable packet of Higher Layer Message (start of a non- automatically flushable L2CAPPOU) from Host to Controller. Shall not be used on a BR/EOR Controller from Controller to Host.

Host to Controller

Allowed

Not allowed

Allowed

Controller to Host

Not allowed (except during loop- back)

Not allowedNot allowed
01

Continuing fragment of Higher Layer Message

Host to ControllerAllowedNot allowedAllowed
Controller to HostAllowedNot allowedAllowed
10First automatically flushable packet of Higher Layer Message (start of an automatically flushable L2CAP PDU)Host to ControllerAllowedNot allowedNot allowed
Controller to HostAllowedNot allowedAllowed
11A complete L2CAP PDU. Automatically flushableHost to ControllerAllowedAllowedNot allowed
Controller to HostNot allowedAllowedNot allowed