5.3.5 BLE and TCP/IP TCP Server Transparent UART

This section explains how to create a peripheral device and a TCP/IP TCP server on the PIC32-BZ6 Curiosity board. The setup will enable users to send and receive data from a Central device which can be a smart phone with MBD (Microchip Bluetooth Data) application to another TCP/IP client.

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.

Hardware Requirement

Table 5-75. Hardware Prerequisites
S. No. Tool Quantity
1PIC32-BZ6 Curiosity Board1
2LAN8720A PHY Daughter Board1
3Micro USB cable1
4Ethernet cable1

SDK Setup

Refer to Getting Started with Software Development from Related Links.

Software Requirement

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

Smart phone App

  1. Microchip Bluetooth Data (MBD)

Programming the Precompiled Hex File or Application Example

Using MPLAB® X IPE:

  1. Import and program the precompiled hex file: <Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\tcpip_tcp_server_trp_uart\precompiled_hex\pic32wmbz6_curiosity_freertos.X.productio.
  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 “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\tcpip_tcp_server_trp_uart\firmware\pic32wmbz6_curiosity_freertos.X”.
  3. 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 PIC32WM-BZ6204UE module to connect to a smartphone via Bluetooth Low Energy (BLE). It showcases the use of the MPLAB Harmony TCP API to implement a TCP server on port 9760. The MBD application can communicate with a client application running on a host computer (such as SocketTest or PacketSender) that is connected to the TCP server on the PIC32WM-BZ6204UE. For this demonstration, the SocketTest utility is used. A simple block diagram of the demo is shown below:

Testing

  1. Connect the USB Type-C cable between DEBUG USB port on the board and host PC. Connect LAN8720 PHY Daughter board to PIC32-BZ6 curiosity board as shown below. Establish a connection between the router/switch with the Curiosity board through the RJ45 connector, using the Ethernet cable.
  2. Program the precompiled hex file or application example as mentioned.
  3. Open Tera Term:
    • Set the “Serial Port” to USB Serial Device.
    • Speed to 115200.
    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.
  4. Press the NMCLR button on the curiosity board to start advertisements. TeraTerm should display the following message.
  5. Verify the TCP/IP Stack initialization console messages.
  6. Open MBD on your smart phone and follow the following images.
  7. After the BLE connection is established, verify the TCP/IP stack messages on the console. You can type the “netinfo” command to display more details, as shown below.
  8. For TCP Server test, a TCP Client application is required to run on the host computer (SocketTest, PacketSender etc). In this demonstration, we use SocketTest utility.
  9. Send a message from client to the TCPIP server and it can be displayed on the utility, Tera term and MBD app.
  10. Server will send back the received message to client and will display on socketTest utility.
  11. Similarly, the MBD app can send a message to the client. The TCP/IP stack will then forward the message to the client.
  12. To disconnect the Curiosity board and the MBD running on your smart phone, do either one of the following:
    1. Press the NMCLR button on the PIC32WM-BZ6204UE Curiosity Board.
    2. Go back to the “SCAN” page in MBD.
  13. Upon successful disconnection, the PIC32WM-BZ6204UE Curiosity Board automatically advertises again.

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 .mc3 and is located in the path “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\tcpip_tcp_server_trp_uart \firmware\ pic32wmbz6_curiosity_freertos.X \ tcpip_tcp_server_freertos.mc3”.
  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 and verify the following in the Configuration Options panel.
  2. Select Transparent Profile component in project graph and verify the following:

Verifying TCP/IP Stack Configuration

  1. Select TCP/IP Configurator under Plugins in the Project Graph. For more details of TCP/IP Configuration plugin, refer to TCP/IP Configuration in Reference Documentation from Related Links.
  2. TCP/IP configuration for PIC32WM-BZ6204UE module is detailed below.

    The Application Layer modules enabled in the demo are as follows:

    • Application Layer Modules:
      • ANNOUNCE to discover the Microchip devices within a local network.
      • DHCP Client to discover the IPv4 address from the nearest DHCP Server.
      • DNS Client provides DNS resolution capabilities to the stack.

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:
    where RC7, RC10 and RE0 are related to the RGB LED.
  3. Ethernet PHY daughter board Header pin descriptions are shown below:
    Table 5-76. Ethernet PHY Daughter Board Header Pin Description J908 and J909
    PinPin on RMII headerPin Description of Ethernet PHY Daughter Board HeaderPin on PIC32WM-BZ6204UE Module
    J908-1TX_ENEthernet Transmit EnableGMAC_GTXEN/RPC9
    J908-2TXDOEthernet Transmit Data 0GMAC_GTX0/RPC0
    J908-3TXD1Ethernet Transmit Data 1GMAC_GTX1/RPE1
    J908-4NCNot connectedNC
    J908-5NCNot connectedNC
    J908-6GNDGroundGND
    J908-7XTALIClock outputNC
    J908-8CLK_INClock inputGMAC_GREFCLKOUT/RPC1
    J908-9GNDGroundGND
    J908-10+3V3Input power supplyVDD
    J908-11NCNot connectedNC
    J908-12NCNot connectedNC
    J909-13WAKEWake

    CVD5/RMII_WAKE/DRP_CTRL/GFX_LCDD1/RPB1

    (Not connected by default. Mount R724 to connect)

    J909-14NCNot connectedNC
    J909-15RXD1Ethernet Receive Data 1GMAC_GRX1/RPA13
    J909-16RXD0Ethernet Receive Data 0GMAC_GRX0/RPA14
    J909-17RX_EREthernet Receive ErrorGMAC_GRXER/RPC8
    J909-18CRS_DVEthernet Rx Data Valid InputGMAX_GCRS_DV/GFX_GPIO1/RPE2
    J909-19MDCEthernet Management Data Clock OutputGMAC_GMDC/GFX_GPIO2/RPD7
    J909-20MDIOEthernet Management Data Input OutputGMAC_GMDIO/GFX_LCD_GPIO5/RPD6
    J909-21INTInterrupt output

    RED_LED/RMII_INT/GFX_PWM2/RPC7

    (Not connected by default. Mount R726 to connect)

    J909-22RSTSystem ResetRMII_RST/LBO/RPD0
    J909-23ENEnable

    RMII_EN/MIKRO2_PWM/GFX_IRQ3/RPC11

    (Not connected by default. Mount R722 to connect)

    J909-24NCNot connectedNC
    Note:
    1. These are Peripheral Pin Select (PPS) pins. The user can configure them for any of the supported peripheral functions based on the end user application.
    2. Pin PE2, PD7, PD6 are shared between Ethernet RMII and Graphic Connector

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.
Autogenerated, Advertisement Data Format
Table 5-77. Source File
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

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.

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

Transmit Data

  • Example for transmitting over UART using the BLE_TRSPS_SendData() API

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 utilizes the Transparent UART building block, see BLE Sensor from Related Links.