5.3.1 BLE and CAN-FD Transparent UART

This section explains how to explore the PIC32-BZ6 curiosity board can seamlessly integrate Controller Area Network Flexible Data-Rate (CAN-FD) and Bluetooth Low Energy (BLE) technologies. This section shows how to set up a peripheral device that can efficiently communicate CAN-FD messages between a CAN bus and a smartphone using the Microchip Bluetooth Data (MBD) app.

This demo shows that CAN and CAN-FD messages received by PIC32WM-BZ6204UE can be forwarded to a central device (smartphone with MBD app) via BLE. In addition, this demo provides a simple mechanism of parsing received BLE messages sent from MBD and structuring this data as a CAN or CAN-FD message to transmit it back to the CAN bus.

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-67. Hardware Requirement
S. No. Tool Qty
1PIC32-BZ6 Curiosity Board1
2

ATA6571 Click

1
3APGDT006 CAN Bus Analyzer FD Tool1
4DB9 Gender Changer female to female1
5USB 2.0 Cable A Male to C Male1
6USB 2.0 Cable A Male to Mini B Male1
7Null Modem 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.

  2. CAN Bus Analyzer FD Tool Driver and Wireshark Extcap:
    • APGDT006-driver-Oct2023.zip
    • APGDT006-wireshark-extcap-Mar2024.zip v4.2.6 64-bit
  3. Wireshark - v4.2.6 64-bit - See Wireshark in Reference Documentation from Related Links.
  4. CAN_fifo_buffer_test.py
  5. CAN-FD_standard_extended_id_test.py
  6. install_v1.zip

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\can_peripheral_trp_uart\precompiled_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 “can_peripheral_trp_uart.X” located in “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\ peripheral _applications\can_peripheral_trp_uart\firmware”.
  3. For more details on how to find the Harmony Content Path, refer to Installing the MCC Plugin from Related Links.

Demo Description

This demo presents three scenarios to demonstrate the flow of data:

  1. Scenario 1: PIC32WM-BZ6204UE can generate standard CAN, CAN-FD, and CAN-FD extended messages and transmit them to the CAN Bus Analyzer.
  2. Scenario 2: PIC32WM-BZ6204UE can receive CAN and CAN-FD extended messages from a CAN bus and forward these messages to a central device using a transparent service.
  3. Scenario 3: PIC32WM-BZ6204UE can receive and parse BLE packets, then structure the data to generate and send corresponding CAN or CAN-FD messages back to the CAN bus.

Take the above image to visualize how data is passed between MBD, PIC32WM-BZ6204UE, and the CAN Bus Analyzer. We demonstrate that data can flow in both directions: from the CAN Bus Analyzer to PIC32WM-BZ6204UE to MBD, and from MBD to PIC32WM-BZ6204UE to the CAN Bus Analyzer. TeraTerm serves as a user interface, presenting a menu of CAN/CAN-FD messages for PIC32WM-BZ6204UE to generate and send to the CAN Bus Analyzer. It also provides a trace log of transmitted and received messages, ensuring users can monitor the communication flow effectively.

We demonstrate this data flow using the following three scenarios:

Scenario 1:

  • Users will be presented with three options in TeraTerm to generate CAN, CAN-FD, or CAN-FD extended messages. PIC32WM-BZ6204UE will structure the CAN/CAN-FD frame and transmit it to the CAN Bus Analyzer.
  • To verify the generated and transmitted messages, users can use Wireshark to view the packets created by PIC32WM-BZ6204UE.

Scenario 2:

  • Users can input 1-64 bytes of data on the MBD app.
  • PIC32WM-BZ6204UE will receive this data through the transparent service, parse it, and structure it into a CAN message (1-8 bytes of user data) or a CAN-FD extended message (9-64 bytes of user data).
  • The generated CAN or CAN-FD message will then be transmitted to the CAN Bus Analyzer.

Scenario 3:

  • Users can utilize provided Python scripts to generate CAN or CAN-FD messages on the CAN Bus Analyzer.
  • PIC32WM-BZ6204UE will receive these messages and forward the data via BLE to a smartphone with the MBD app installed.

Once the demo application is programmed, PIC32WM-BZ6204UE will begin advertising. A central device, such as a smartphone, can scan for these advertisements and connect by searching for the name “pic32cx-bz6” on the Microchip Bluetooth Data (MBD) app.

Upon reset, the demo will print a message to the terminal indicating the name of the demo and will print “Advertising.” The terminal will also print a menu of options to generate CAN/CAN-FD messages. When a device connects, the terminal will show “Connected.” If the device disconnects, a “Disconnected” message will appear, and PIC32WM-BZ6204UE will resume advertising, prompting another “Advertising” message. Throughout the demo, the terminal will print the status of the sent or received messages to provide further insight.

These messages can be viewed using a terminal emulator like TeraTerm with the following settings:
  • Speed: 115200
  • Data: 8-bit
  • Parity: none
  • Stop bits: 1 bit
  • Flow control: none

Testing Setup

  1. Hardware setup:
    1. Plug in the ATA6571 into the mikroBUS1 port on the PIC32-BZ6 Curiosity Board.
    2. Plug in the DB9 Female-Female Gender Changer into the CAN0 port on the CAN Bus Analyzer FD Tool.
    3. Either use a standard DB9 cable to connect the ATA6571 DB9 port to the DB9 Female-Female Gender Changer (as shown in the image) OR directly plug in the ATA6571 DB9 port into the DB9 Female-Female Gender Changer.
    4. After these connections are made, plug in the USB-C cable into the DBG USB port on the PIC32-BZ6 Curiosity Board. Then plug in the USB Mini B cable into the J4 on the CAN Bus Analyzer FD Tool.
  2. Wireshark setup:
    1. Install Wireshark.
      1. Important: Make sure to install Wireshark v4.2.6 64-bit.
    2. Download the Wireshark extcap zip from CAN Bus Analyzer FD Tool Driver and Wireshark Extcap (APGDT006-wireshark-extcap-Mar2024.zip).
    3. Unzip files in APGDT006-wireshark-extcap-Mar2024.zip and paste it in C:\Program Files\Wireshark\extcap.
    4. Important: Wireshark is only able to receive/capture messages. Additionally, the python scripts (which transmits data) cannot be running while Wireshark is open/capturing data.
    5. For more information on installation, refer to CAN-Bus-Analyzer-FD-Users-Guide.pdf chapter 4.2. The pdf can be downloaded from CAN BUS ANALYZER FD TOOL.
  3. Adding Python support to APGDT006:
    1. Setup Python.
      1. Navigate to Python Releases for Windows | Python.org.
      2. Install Python.
        1. Important: Make sure to install Python v3.9.0 64-bit.
    2. Install files:
      1. Unzip the install_v1.zip folder located in “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\can_peripheral_trp_uart\tools” folder to extract files.
      2. Install File “mba-1.3.0” whl, to do this browse to the Python Install Folder. In the folder path type “cmd” .
      3. Once In The Command Window Type: python -m pip install C:\(FILE DIRECTORY) \mba-1.3.0-py3-none-any.whl (If Done Correctly Your Screen Will Look Like This).
      4. To Double Check If the Install Went Thorough Enter This in the Command Window: pip list.
      5. Correct mba bug before executing a python script with the APGDT006 tool. Browse to the mba folder within the python install folder.
      6. Open mba.py.
        Replace line 491:
        With this:
        Note: If this step is NOT taken, a user would see the following error when attempting to execute a script.
    3. Install cba.fd Driver:
      1. Download the driver install zip from CAN Bus Analyzer FD Tool Driver and Wireshark Extcap (APGDT006-driver-Oct2023.zip).
      2. Unzip the folder.
      3. Connect the CBA-FD hardware to the laptop.
      4. Go to device manager and install the driver. After it is installed, the device manager should look like this.
      5. Reboot PC.
    4. Execute the python test:
      1. From within the python folder run this script “CAN-FD_pythonTest.py” with python.
      2. After running the script your command window must look like this.
      3. You will now see 5 blue LED lights on the board illuminated.
      4. This indicates the Python setup has been successfully completed.
Note: The “install” zip file contains a testing script (CAN-FD_pythonTest.py) which is only to be used during the initial Python setup. The scripts used later in this demo should not be confused with this testing script.
Important: Users MUST do above steps to make sure environment works as expected. Ensure to run step 3d again after unplugging and plugging CAN bus analyzer.

Testing

This section assumes that user has completed the setup mentioned in the Testing Setup section.

  1. Scenario 1:
    This scenario presents the data flow from PIC32WM-BZ6204UE to the CAN BUS Analyzer. Users interact with TeraTerm to generate CAN/CAN-FD frames on PIC32WM-BZ6204UE.
    Scenario 1 (i.e., PIC32WM-BZ6204UE -> CAN Bus Analyzer):
    1. Have all boards connected as in Hardware Setup above.
    2. Open a terminal emulator like Tera Term and select the COM port@ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none) .
    3. Reset PIC32WM-BZ6204UE and you will see the following menu of actions that can be performed:
    4. Start capturing on Wireshark
      1. Open up Wireshark.
      2. Click on the icon next to the Microchip CAN BUS Analyzer interface.
      3. Configure the following:
        1. Tick the Send Acknowledgment box then press Save.
      4. Double click on the Microchip CAN Bus Analyzer interface to start capturing data.
    5. In TeraTerm, press 0 then 1 then 2.
    6. If successful, TeraTerm and Wireshark would look like this.
  2. Scenario 2:
    This scenario shows the data flow from MBD to PIC32WM-BZ6204UE to the CAN Bus Analyzer.
    Scenario 2 (i.e., MBD on smartphone -> PIC32WM-BZ6204UE -> CAN Bus Analyzer):
    1. Have all boards connected as in Hardware Setup above.
    2. Open a terminal emulator like Tera Term and select the COM port@ (Speed: 115200, Data: 8-bit, Parity: none, stop bits: 1 bit, Flow control: none) as in step 1.b.
    3. Open MBD on your smartphone. Navigate to BLE UART then PIC32CXBZ and search for “pic32cx-bz6.” Connect to the device and select the transparent option. From here you can enable the “Display Data” checkbox to begin viewing received data.
      Note: Some iOS users may not find the device “pic32cx-bz6” in the “PIC32CXBZ” page in MBD. In this case, users can select “BM70” on MBD and follow the same instructions.
    4. Follow step 1.d to setup Wireshark.
    5. Send between 1 and 8 bytes of data on MBD (e.g. “123”).
      1. If successful, TeraTerm and Wireshark will look like this:
        Note: When PIC32WM-BZ6204UE receives a message between 1 and 8 bytes, it will structure a standard CAN message with ID of 0x555 and will append 0x00 to any of the remaining 8 bytes.
    6. Send between 9 and 64 bytes of data on MBD (e.g. “123456789”).
      1. If successful, TeraTerm and Wireshark will look like this:
      Note: When PIC32WM-BZ6204UE receives a message between 9 and 64 bytes, it will structure an extended CAN-FD message with ID of 0x100000A5 and will append 0x00 to any of the remaining 64 bytes.
  3. Scenario 3:
    This scenario shows the data flow from the CAN Bus Analyzer to PIC32WM-BZ6204UE to MBD.

    Scenario 3 (i.e., CAN Bus Analyzer -> PIC32WM-BZ6204UE -> MBD on smartphone):

    1. Follow steps 2.a through 2.c to establish a BLE connection.
    2. Do not capture data on Wireshark for the remaining steps.
    3. Run CAN_fifo_buffer_test.py located in “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\can_peripheral_trp_uart\tools” folder
      Note: Wireshark cannot be capturing data at the same time you use the python script to transmit data.
      1. If successful, command prompt and TeraTerm will look like this:
      2. The message will also be seen on MBD as follows:
      3. You can also play around with the message IDs in the script to test message capture in RX FIFO0, RX Buffer and RX FIFO1, respectively.
    4. Run CAN-FD_standard_extended_id_test.py located in “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\peripheral_applications\can_peripheral_trp_uart\tools” folder:

      1. If successful, command prompt and TeraTerm would look like this:
      2. The message will also be seen on MBD as follows:
      3. You can also play around with the message IDs in the script to test standard and extended ID filtering, respectively.

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\advanced_applications\can_peripheral_trp_uart\firmware\can_peripheral_trp_uart.X”.

  3. Accept dependencies or satisfiers when prompted.
  4. Verify if the Project Graph window has all the expected configuration.
Note: Make sure that the macro “configUSE_TICKLESS_IDLE” is set to 0. The macro is available in FreeRTOSConfig.h file
#define configUSE_TICKLESS_IDLE 0

Verifying Advertisement, Connection and Transparent UART Profile Configuration

  1. Select the BLE Stack component in the Project Graph.
  2. Select Transparent Profile component in the Project Graph.

Verifying CAN1 Configuration

  1. Select CAN1 component in the Project Graph.

Verifying SERCOM Configuration

  1. Select SERCOM0 component in the Project Graph.

Verifying the Pin Configuration

  1. Select Pin Configuration under Plugins in the Project Graph:
  2. Verify the Pin Settings:
    1. RA10 and RB10 must have this configuration for ATA6571 to operate in normal mode.
      See this Pinout Diagram of the ATA6571 and mikroBUS:

      Pin RA10 corresponds to the CS pin on the mikroBUS which is the EN pin on ATA6571.

      Pin RB10 corresponds to the AN pin on the mikroBUS which is the STB pin on ATA6571.

      These are the operating modes of the ATA6571:
      From this schematic, we can see that AN is connected to NSTBY and INH is NC. Hence, we must set RA10 and RB10 to be high so that the ATA6571 can operate in normal mode.

Files and Routines Automatically generated by the MCC

After generating the program source from the MCC interface (by clicking Generate Code), the BLE configuration source can then 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.C.

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

Figure 5-259. AD Structures and Types
Table 5-68. 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() API is called in the Applications Initial State APP_STATE_INIT in app.c is the API that will be called inside the Applications Initial State APP_STATE_INIT in app.c

User Application Development

Include
  • ble_trsps/ble_trsps.h in app.c, BLE Transparent UART Server related API's are available here
  • stdio.h in app.c and app_trsps_handler.c
  • peripheral/sercom/usart/plib_sercom0_usart.h in app_ble_handler.c and app_trsps_handler.c
  • osal/osal_freertos_extend.h in app_trsps_handler.c
  • app.h in app_trsps_handler.c
  • Include the user action. For more information, refer to User Action from Related Links.
  • 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 UART but instead must be included in all the application source files where any peripheral functionality will be exercised.

Configure CAN1 and USART and Start Advertisement

CAN1

CAN1_MessageRAMConfigSet(Can1MessageRAM);

CAN1_RxFifoCallbackRegister(CAN_RX_FIFO_0, APP_CAN_RxFifo0Callback, APP_STATE_CAN_RECEIVE);

CAN1_RxFifoCallbackRegister(CAN_RX_FIFO_1, APP_CAN_RxFifo1Callback, APP_STATE_CAN_RECEIVE);

CAN1_RxBuffersCallbackRegister(APP_CAN_RxBufferCallback, APP_STATE_CAN_RECEIVE);

USART

SERCOM0_USART_ReadNotificationEnable(true, true);

SERCOM0_USART_ReadThresholdSet(1);

SERCOM0_USART_ReadCallbackRegister(uart_cb, (uintptr_t)NULL);

Start Advertising

BLE_GAP_SetAdvEnable(0x01, 0);

Connected and 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

CAN Functions
  • Many of the CAN functions are located in the app.c file. These functions include:
  • CAN callback functions, responsible for error checking and maintaining the CAN state machine:
    • void APP_CAN_TxFifoCallback(uintptr_t context)
    • void APP_CAN_RxBufferCallback(uint8_t bufferNumber, uintptr_t context)
    • void APP_CAN_RxFifo0Callback(uint8_t numberOfMessage, uintptr_t context)
    • void APP_CAN_RxFifo1Callback(uint8_t numberOfMessage, uintptr_t context)
  • CAN helpers:
    • uint8_t CANLengthToDlcGet(uint8_t length)
    • static uint8_t CANDlcToLengthGet(uint8_t dlc)
  • Display menu to terminal:
    • static void display_menu(void)
  • Print and transmit the received CAN message to MBD:
    • void print_and_tx_message(uint8_t numberOfMessage, CAN_RX_BUFFER *rxBuf, uint8_t rxBufLen, uint8_t rxFifoBuf)
  • General function used to send a CAN/CAN-FD message:
    • void sendCANMessage(uint32_t id, bool extended, uint8_t datalen, uint8_t* databuf)
  • Print the status of the Tx or Rx event to the terminal:
    • void print_status(APP_CAN_STATES temp_state)
  • Refer to “<Harmony Content Path>\wireless_apps_pic32_bz6\apps\ble\advanced_applications\can_peripheral_trp_uart\firmware\src\app.c to see their full implementation.

Transmit CAN/CAN-FD Message to CAN Bus Analyzer FD Tool

  • Add “APP_MSG_UART_CB” to the generated APP_MsgId_T.
  • The UART data will be captured in the uart_cb() function.
  • The function responsible for transmitting CAN/CAN-FD messages is sendCANMessage().
    • The functions APP_UARTCBHandler and APP_TrspsEvtHandler() are responsible for gathering the data to be transmitted and they call sendCANMessage to structure and send the CAN message.
  • When data is inputted into TeraTerm, the data is captured by UART. From there the data ends up in the APP_UartCBHandler(). This function creates a data buffer for either a standard CAN message, CAN-FD message, or CAN-FD extended message and calls sendCANMessage depending on the 3 options that the user chose.
  • When the user inputs custom CAN data into MBD, the message is received by the transparent service. The BLE_TRSPS_EVT_RECEIVE_DATA event is generated upon receiving a BLE packet. This function structures a data buffer for either a standard CAN message or a CAN-FD extended message depending on the length of data inputted by the user. This CAN message is filled with user defined data in contrast to the 3 options of predefined data used in the UART callback.

Receive CAN/CAN-FD Messages and Send to MBD

  • The CAN Rx callback functions call the print_and_tx_message() function to print the received data to the terminal and transmit the message to MBD.
  • The print_and_tx_message() is used to queue messages to be sent over the transparent service and print the CAN data to the terminal.
  • Add “APP_MSG_CAN_EVT” and “APP_MSG_UART_CB” to the generated APP_MsgId_T.
  • BLE_TRSPS_SendData(); is the API to be used for sending data towards the central device.
Note: The precompiled application example uses a print_and_tx_message() function to initiate the data transmission upon receiving a CAN message.
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.