5.4.6 USB CDC Peripheral Transparent UART

This application demonstrates a combined USB and Bluetooth Low Energy (BLE) peripheral solution implemented using the PIC32-BZ6 Curiosity Board. It integrates a USB Communication Device Class (CDC) virtual COM port interface and BLE connectivity, providing seamless bidirectional data exchange between a USB host (such as a PC terminal application) and a remote BLE central device (mobile phone).

The application works in two primary data-flow modes:

  • USB-to-BLE: Data entered from a USB host via the CDC COM port is received by the PIC32-BZ6 device and then transmitted wirelessly using BLE to a connected central device.
  • BLE-to-USB: Data received wirelessly via BLE from the connected central device is relayed by the PIC32-BZ6 device over the USB CDC interface to the USB host.

This solution was implemented using Microchip’s MPLAB X Integrated Development Environment (IDE) and Harmony software framework enabling handling of simultaneous USB and BLE communications

Users can choose to either run the precompiled Application Example hex file provided on the PIC32-BZ6 Curiosity board and experience the demo.

Hardware Required

S. No. Tool Quantity
1PIC32-BZ6 Curiosity Board1
2USB cable1
3Android/iOS Smartphone1

SDK Setup

Refer to Getting Started with Software Development from Related Links.

Software

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

Smartphone App

Microchip Bluetooth Data iOS/Android app available in stores

Programming the Precompiled Hex File or Application Example

Using MPLAB® X IPE:

  1. Import and program the precompiled hex file: <Discover Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications \ usb_cdc_peripheral_trp_uart \precompiled_hex\ usb_cdc_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: <Discover Path>\wireless_apps_pic32_bz6\apps\peripheral_applications\usb_cdc_peripheral_trp_uart\firmware\usb_cdc_peripheral_trp_uart.X
  3. 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

This demonstration illustrates a dual-interface USB and BLE peripheral solution implemented on the PIC32-BZ6 Curiosity Board. It highlights the integration of Bluetooth® Low Energy (BLE) peripheral functionality built on top of a USB CDC peripheral, facilitating seamless two-way communication between a USB host and a BLE central device.

A simple block diagram of the demo is shown below:

The application uses Microchip’s BLE Transparent UART Profile and Service, allowing data to be transparently exchanged over the BLE connection without additional formatting or parsing. USB data received via the CDC virtual COM port is immediately relayed over BLE using notifications, while incoming data from the BLE central device is directly forwarded to the USB host, showcasing efficient bidirectional communication.

Testing

  1. Connect a Type-C cable between the DBG USB (J100) port on the Curiosity board and the PC to power up the board.
  2. The jumper (J102) must be open for USB device applications.
  3. Use the Target USB (J103) connector on the board to connect the USB Device to the USB Host PC.
  4. RGB_LED (D800) turning Green indicates USB Device Configuration Set Complete event (the USB device functionality has been activated by the USB Host).
  5. Program the precompiled hex file or application example as mentioned.
  6. Open Tera Term:
    • Set the “Serial Port” to USB Serial Device.
    • Speed to 115200.
    Note: The PIC32-BZ6 board features an MCP2200 chip that provides both USB-to-UART and PKOB4 communication ports. When USB is enabled, three COM ports typically appear. Among these, the USB-to-UART port will always have a lower COM number than the PKOB4 port. For example, in my case, COM39 is the USB-to-UART port, COM40 is the PKOB4 port, and COM41 represents the actual USB interface. Therefore, for testing purposes, COM41 should be selected.
    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.
  7. Press the NMCLR Button on the Curiosity board.
  8. Open the Microchip Bluetooth Data (MBD) app on your smartphone and select “BLE UART” sub app.
  9. Select “PIC32CXBZ”.
  10. Press Scan for advertisements. A device with the name “pic32cx-bz6” will appear. Select the device and establish connection.
  11. Upon connection the serial terminal will display the “Connected” log.
  12. In MBD, select the “Text Mode” from the bottom tab and start exchanging data bidirectionally between the serial terminal and mobile application.
  13. Console log.
Note: The PIC32-BZ6 board features an MCP2200 chip that provides both USB-to-UART and PKOB4 communication ports. When USB is enabled, three COM ports typically appear. Among these, the USB-to-UART port will always have a lower COM number than the PKOB4 port. For example, in my case, COM39 is the USB-to-UART port, COM40 is the PKOB4 port, and COM41 represents the actual USB interface. Therefore, for testing purposes, COM41 should be selected.

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 .mc4 and is located in the path “<Discover Path>\wireless_apps_pic32_bz6\apps

    \ble\advanced_applications\usb_cdc_peripheral_trp_uart\firmware\usb_cdc_peripheral_trp_uart.X”.

  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.
  2. Select Transparent Profile component in project graph and verify the following:

Verifying USB Device Layer Configuration

  1. Select USB Device Layer component in the Project Graph.

Verifying CDC Function Driver Instance 0 Configuration

  1. Select Instance 0 within the CDC Function Driver component in the Project Graph.

Verifying SERCOM Configuration

  1. Select the 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:

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.
Figure 5-264. Auto Generated Advertisement Data Format
Figure 5-265. AD Structures and Types
Table 5-79. Source Files
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

The initialization sequence, event driven mechanisms, and data path implementations between USB and BLE are described in detail in this section. The application supports bidirectional data transfer: data entered over the USB CDC interface from a USB host is received and transmitted over BLE to a central device (“USB to BLE”), and data received from a central BLE device gets forwarded to the USB host via the USB CDC interface (“BLE to Socket”).

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.

Initialization of USB and BLE

  • The following USB CDC variables are defined in the APP_DATA struct in app.h and initialized in the APP_Initialize function in app.c.
  • In the APP_Tasks, the APP_STATE_INIT function will initialize the BLE, start advertising, and open the USB device for reading and writing. If successful, the USB device layer event handler will be regestered as APP_USBDeviceEventHandler.
  • The APP_STATE_ERROR state is defined in app.h.
  • Once the USB is opened and the BLE stack is initialized, the system enters the APP_STATE_SERVICE_TASKS state, which handles BLE and custom events. Simultaneously, the registered USB event handlerwill capture the device layer USB related events. The custom events APP_MSG_USB_CB and APP_MSG_UART_CB are defined in app.h and are used in the data paths.

USB Device Layer Event Handler

  • The APP_USBDeviceEventHandler function was registered as the event handler in the initialization. This function captures USB device layer events, namely, when the device is configured, power to the USB is detected or removed, if the device was suspended or resumed or reset. This mechanism allows the USB to be reset and remain operational. Meaning if the TGT USB power is removed and reattached, then the device will continue to operate as usual.

    The initial operation will flow from USB_DEVICE_EVENT_POWER_DETECTED to USB_DEVICE_EVENT_CONFIGURED once power is given to the TGT USB, then some reset or power removal will allow the device to get configured again.

  • The USB_DEVICE_EVENT_POWER_DETECTED and USB_DEVICE_EVENT_POWER_REMOVED events will attach/detach the USB device.
  • The USB_DEVICE_EVENT_CONFIGURED event will register the CDC application event handler as APP_USBDeviceCDCEventHandler. The initial USB read will be scheduled as well.

Connected & Disconnected Events

  • In app_ble_handler.c, BLE_GAP_EVT_CONNECTED event will be generated when a BLE connection is established.
  • On connection, the 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
  • The custom event APP_MSG_USB_CB will be scheduled so that the “Connected” message will be sent to the USB host via the CDC interface. The message is copied to the appMsg.msgData buffer.
  • BLE_GAP_EVT_DISCONNECTED event will be generated when a BLE connection is established.
  • Advertising will begin upon disconnection.
  • Similar to the connected event, the APP_MSG_USB_CB event willbe scheduled so the “Disconnected” message can be sent to the USB host via the CDC interface.

USB CDC Application Layer Event Handler

  • The APP_USBDeviceCDCEventHandler function was registered as the event handler in the USB_DEVICE_EVENT_CONFIGURED event callback. Various events are handled in this function.

“BLE to USB” Data Path

  • The “BLE to Socket" data path is initiated when BLE packet is received from the connected central device. This packet is copied to the appMsg.msgData buffer and the custom event APP_MSG_USB_CB is scheduled to allow the packet to be sent to the USB host via the CDC interface.

  • The BLE packet is received in the BLE_TRSPS_EVT_RECEIVE_DATA event in app_trsps_handler.c.
  • The custom APP_MSG_USB_CB event will send the BLE packet over the USB CDC interface to the host.

“USB to BLE” Data Path

  • The “USB to BLE” data path is initiated when data is read on the USB CDC interface. The data is copied to the appMsg.msgData buffer and the custom event APP_MSG_UART_CB is scheduled to allow one byte of data to be sent to the central device via BLE.

  • The event USB_DEVICE_CDC_EVENT_READ_COMPLETE is raised when data is read on the CDC interface. The application will also schedule another USB_DEVICE_CDC_READ to allow the event to be raised again.
  • The custom APP_MSG_UART_CB event will send only one byte of the data to the central connected device via BLE.