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.
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 Connection from Related Links.
- See BLE Transparent UART from Related Links.
- BLE Software Specification – See MPLAB® Harmony Wireless BLE in Reference Documentation from Related Links.
Hardware Requirement
S. No. | Tool | Quantity |
---|---|---|
1 | PIC32-BZ6 Curiosity Board | 1 |
2 | LAN8720A PHY Daughter Board | 1 |
3 | Micro USB cable | 1 |
4 | Ethernet cable | 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
- Microchip Bluetooth Data (MBD)
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\peripheral_applications\tcpip_tcp_server_trp_uart\precompiled_hex\pic32wmbz6_curiosity_freertos.X.productio
. - 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
“
<Harmony Content Path>\wireless_apps_pic32_bz6
”.\apps\ble\peripheral_applications\tcpip_tcp_server_trp_uart\firmware\pic32wmbz6_curiosity_freertos.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 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
- 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.
- Program the precompiled hex file or application example as mentioned.
- Open Tera Term:
- Set the “Serial Port” to USB Serial Device.
- Speed to 115200.
- Press the NMCLR button on the curiosity board to start advertisements. TeraTerm should display the following message.
- Verify the TCP/IP Stack initialization console messages.
- Open MBD on your smart phone and follow
the following images.
- 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.
- 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.
- Send a message from client to the TCPIP
server and it can be displayed on the utility, Tera term and MBD app.
- Server will send back the received
message to client and will display on socketTest utility.
- Similarly, the MBD app can send a message to the client. The TCP/IP stack will then
forward the message to the client.
- To disconnect the Curiosity board and the MBD running on your smart phone, do either one
of the following:
- Press the NMCLR button on the PIC32WM-BZ6204UE Curiosity Board.
- Go back to the “SCAN” page in MBD.
- Upon successful disconnection, the PIC32WM-BZ6204UE Curiosity Board automatically advertises again.
Developing the Application from Scratch using the MPLAB Code Configurator
- 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\peripheral_applications\tcpip_tcp_server_trp_uart \firmware\ pic32wmbz6_curiosity_freertos.X \ tcpip_tcp_server_freertos.mc3
”. - Accept dependencies or satisfiers when prompted.
- Verify if the Project Graph window has
all the expected configuration
Verifying Advertisement, Connection and Transparent UART Profile Configuration
-
Select the BLE Stack component in the Project Graph and verify the following in the Configuration Options panel.
- Select Transparent Profile
component in project graph and verify the following:
Verifying TCP/IP Stack Configuration
-
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.
- 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.
- Application Layer Modules:
Verifying SERCOM Configuration
-
Select the SERCOM0 component in the project graph and verify the following:
Verifying the Pin Configuration
-
Select Pin Configuration under Plugins in the Project Graph:
- Verify the Pin Settings: where RC7, RC10 and RE0 are related to the RGB LED.
-
Ethernet PHY daughter board Header pin descriptions are shown below:
Table 5-76. Ethernet PHY Daughter Board Header Pin Description J908 and J909 Pin Pin on RMII header Pin Description of Ethernet PHY Daughter Board Header Pin on PIC32WM-BZ6204UE Module J908-1 TX_EN Ethernet Transmit Enable GMAC_GTXEN/RPC9 J908-2 TXDO Ethernet Transmit Data 0 GMAC_GTX0/RPC0 J908-3 TXD1 Ethernet Transmit Data 1 GMAC_GTX1/RPE1 J908-4 NC Not connected NC J908-5 NC Not connected NC J908-6 GND Ground GND J908-7 XTALI Clock output NC J908-8 CLK_IN Clock input GMAC_GREFCLKOUT/RPC1 J908-9 GND Ground GND J908-10 +3V3 Input power supply VDD J908-11 NC Not connected NC J908-12 NC Not connected NC J909-13 WAKE Wake CVD5/RMII_WAKE/DRP_CTRL/GFX_LCDD1/RPB1
(Not connected by default. Mount R724 to connect)
J909-14 NC Not connected NC J909-15 RXD1 Ethernet Receive Data 1 GMAC_GRX1/RPA13 J909-16 RXD0 Ethernet Receive Data 0 GMAC_GRX0/RPA14 J909-17 RX_ER Ethernet Receive Error GMAC_GRXER/RPC8 J909-18 CRS_DV Ethernet Rx Data Valid Input GMAX_GCRS_DV/GFX_GPIO1/RPE2 J909-19 MDC Ethernet Management Data Clock Output GMAC_GMDC/GFX_GPIO2/RPD7 J909-20 MDIO Ethernet Management Data Input Output GMAC_GMDIO/GFX_LCD_GPIO5/RPD6 J909-21 INT Interrupt output RED_LED/RMII_INT/GFX_PWM2/RPC7
(Not connected by default. Mount R726 to connect)
J909-22 RST System Reset RMII_RST/LBO/RPD0 J909-23 EN Enable RMII_EN/MIKRO2_PWM/GFX_IRQ3/RPC11
(Not connected by default. Mount R722 to connect)
J909-24 NC Not connected NC Note:- 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.
- Pin PE2, PD7, PD6 are shared between Ethernet RMII and Graphic Connector
Generating 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
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 |
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 fileble_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
inapp.c
User Application Development
Include
ble_trsps.h
inapp.c
, BLE Transparent UART Server related API's are available hereosal/osal_freertos_extend.h
inapp_trsps_handler.c
contain OSAL related APIsdefinitions.h
must be included in all the files where UART will be used to print debug informationNote: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
Where to go from here
BLE Sensor App utilizes the Transparent UART building block, see BLE Sensor from Related Links.