Skip to content

Infineon/linux-example-btstack-bas

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

1 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

AIROC™ BTSTACK: Bluetooth® Battery Server (BAS) for Linux host

This code example demonstrates Bluetooth® Battery Server (BAS) using AIROC™ Wi-Fi & Bluetooth® combo chip.

Requirements

  • Programming language: C
  • Embedded Linux platforms (Arm® Cortex®-A Class processors) for host communications. Supported Embedded Linux host platforms:
    1. RPI CM4-lite IFX custom HW board from Infineon
    2. IMX8 nano uCOM board from Embedded Artists
  • AIROC™ BTSTACK library and Linux porting layer source code
  • AIROC™ Wi-Fi & Bluetooth® combo chip Bluetooth® Firmware file (.hcd).
  • Linux Host PC with Ubuntu 20.04

Supported toolchains (make variable 'TOOLCHAIN')

  • GNU Arm® GCC AArch64 v9.3.0 (GCC_ARM)

Supported AIROC™ Wi-Fi & Bluetooth® combo chip

Hardware setup

Set up the hardware required for this code example as follows:

Figure 1. Hardware setup block diagram

Software setup

Set up a cross compiler according to the target platform requirement along with CMake on the Linux host PC based on Ubuntu 20.04.

Note: These steps are for Arm® 64-based target platform. Review these steps before adding support for any other target.

  1. Open a terminal on the Linux host PC.

  2. Use the following command to install the cross compiler, build tools, and dependencies:

    sudo apt-get install git cmake gcc-aarch64-linux-gnu build-essential -y
  3. To use an iOS or Android smartphone as the BAS Locator, download the AIROC™ Bluetooth® Connect App. Scan the following QR codes from your mobile phone to download the app:

    Figure 2. Application QR code

Using the code example

This section describes the steps to be executed on the Linux host PC for compiling the code example.

  1. Create a directory under $HOME on the Linux host PC and switch to the created directory. Use the following commands for reference:

    mkdir $HOME/Linux_CE
    cd $HOME/Linux_CE
  2. Fetch the code example source code using the following command:

    git clone https://github.com/Infineon/linux-example-btstack-bas
  3. Clone the code example dependencies (BTSTACK library and Linux porting layer source code) using the following commands:

    git clone https://github.com/Infineon/btstack --branch release-v3.6.0
    git clone https://github.com/Infineon/bluetooth-linux.git --branch release-v1.0.0

    Creates three different directories after cloning the code example and its dependencies with the directory structure as follows:

    Figure 3. Code example directory structure

  4. Clone the Bluetooth® firmware using following command.

    git clone https://github.com/Infineon/combo-bluetooth-firmware.git

    User can choose appropriate Bluetooth® firmware for particular AIROC™ Wi-Fi & Bluetooth® combo chip from cloned "combo-bluetooth-firmware" directory.

  5. Create the build folder under the code example source folder and build the code example using the following commands:

    cd $HOME/Linux_CE/linux-example-btstack-bas
    mkdir build && cd build
    cmake -DCMAKE_C_COMPILER:PATH=<GCC_CROSS_COMPILER> ../ && make
    

    Where,

    • GCC_CROSS_COMPILER is the target cross compiler for GCC (generally /usr/bin/aarch64-linux-gnu-gcc for Arm® 64-based targets)

    The code example executable is generated under the build folder with the same name as the code example.

    For example, in this project, the "linux-example-btstack-bas" executable is generated at /home/$USER/Linux_CE/linux-example-btstack-bas/build.

Operation

Devices used:

  • Device Under Test (DUT): One hardware platform (Linux host platform and AIROC™ Wi-Fi & Bluetooth® combo chip) is required that runs the BAS code example. Implements: "Battery Server", GATT Server, GAP Peripheral role

  • Testing device: Mobile (Android or iOS) with AIROC™ Bluetooth® Connect App. Used as: "Battery Server locator", GATT Client, GAP Central role

  1. Copy the code example executable, AIROC™ BTSTACK library, and Bluetooth® Firmware file from the Linux host PC to the target platform using SCP. For example, use the following commands:

    cd $HOME/Linux_CE/linux-example-btstack-bas/build
    scp linux-bas <TARGET_USER>@<TARGET_IP>:<TARGET_PATH>/.
    cd $HOME/Linux_CE/btstack/stack/COMPONENT_WICED_DUALMODE/COMPONENT_ARMv8_LINUX/COMPONENT_GCC
    scp libbtstack.so <TARGET_USER>@<TARGET_IP>:<TARGET_PATH>/.
    scp <FW_FILE.hcd> <TARGET_USER>@<TARGET_IP>:<TARGET_PATH>/.
    

    Where,

    • TARGET_USER is the user name of the target platform
    • TARGET_IP is the IP address of the target platform
    • TARGET_PATH is the path of the target platform
    • FW_FILE.hcd file is Bluetooth® Firmware file cloned in step-4 of Using the code example section.
  2. Take SSH console of target platform.

    ssh <TARGET_DEVICE_USER_NAME>@<TARGET_DEVICE_IP_ADDRESS>
  3. Add the udev rule in the target host board for HCI UART and GPIO to bypass root access. Use the following steps to create and set up a udev rule.

    Note: If you have root access, the following udev rules are not required; you can execute the code example with sudo permissions or by switching to the root user.

    1. Create a new .rules (for example, combo-chip-uart-port.rules) file under /etc/udev/rules.d/ directory for HCI UART. Use the following commands:

      IMX8Nano:

      echo "KERNEL==\"ttymxc0\"d,SYMLINK+=\"combo_chip_uart\",MODE=\"0666\"" | sudo tee /etc/udev/rules.d/combo-chip-uart-port.rules

      RPICM4:

      echo "KERNEL==\"ttyAMA0\",SYMLINK+=\"combo_chip_uart\",MODE=\"0666\"" | sudo tee /etc/udev/rules.d/combo-chip-uart-port.rules
    2. Create new .rules (for example, combo-chip-gpio-port.rules) for BT_REG_ON GPIO under /etc/udev/rules.d/. Use the following commands:

      IMX8Nano & RPICM4:

      1. Create a rule file using the following command.

        sudo vim /etc/udev/rules.d/combo-chip-gpio-port.rules.rules
      2. Add the following rules in created files:

        SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R $user:$group /sys/class/gpio/export /sys/class/gpio/unexport;'"
        SUBSYSTEM=="gpio*", PROGRAM="/bin/sh -c 'chown -R $user:$group /sys%p/direction /sys%p/value; chmod 660 /sys%p/direction /sys%p/value;'"
    3. Reboot the target device:

      sudo reboot

      Where,

      • ttymxc0 and ttyAMA0 are HCI UART ports for IMX8Nano and RPICM4 respectively
      • combo_chip_uart is a friendly name for the HCI UART port
      • 0666 is the permission mask to bypass the root access for HCI UART
  4. Execute the application by setting the paths of the AIROC™ BTSTACK library using the following command on the target platform:

    cd <TARGET_PATH>
    chmod +x <APP_NAME>
    LD_LIBRARY_PATH=$LD_LIBRARY_PATH:<BTSTACK_LIB_PATH>
    ./<APP_NAME> -c <COM_PORT> -b 3000000 -f 921600 -r <GPIOCHIPx> <REGONPIN> -n -p <FW_FILE_NAME>.hcd -d 112233221133

    Where,

    • TARGET_PATH is the path of the target platform where the code example application copied to
    • BTSTACK_LIB_PATH is the path of the AIROC™ BTSTACK library. Skip this if the AIROC™ BTSTACK library and code example application executable are in the same folder
    • /dev/ttymxc0 is the COM_PORT for IMX8Nano
    • /dev/ttyAMA0 is the COM_PORT for RPICM4
    • 3000000 is the HCI baud rate
    • 112233221133 is a device BD address
    • -r <GPIOCHIPx> <REGONPIN> -n is setting the GPIO control to enable autobaud for AIROC™ Wi-Fi + Bluetooth® combo chip
      • -r gpiochip5 0 -n For IMX8Nano
      • -r gpiochip0 3 -n For RPICM4
    • 921600 is the firmware download baud rate
    • .hcd is the firmware file to download (make sure to validate this firmware file file path)

    Note Currently, random BD addresses are used for testing.

  5. To test using the AIROC™ Bluetooth® Connect App, do the following (see equivalent AIROC™ Bluetooth® Connect App screenshots in Figure 4 and Figure 5):

    1. Turn ON Bluetooth® on your Android or iOS device.

    2. Launch the AIROC™ Bluetooth® Connect App.

    3. Start the "Battery Server" application on the Linux board as mentioned in Step 4 of the Operation section.

      The "BAS Target" application on the Linux board will start advertising. Look for the following prints on the Linux console:

      Advertisement State Change: BTM_BLE_ADVERT_UNDIRECTED_HIGH

      Advertising stops after 90 seconds if a connection has not been established.

    4. Swipe down on the AIROC™ Bluetooth® Connect App home screen on the mobile to start scanning for Bluetooth® LE peripherals; your device (“Battery Server”) appears in the AIROC™ Bluetooth® Connect App home screen. Select your device to establish a Bluetooth® LE connection.

    5. Select the "Battery Server" from the carousel view of the Mobile AIROC™ Bluetooth® Connect App.

      Figure 4. AIROC™ Bluetooth® Connect App device discovery

  6. Select Battery Service (see Figure 5) from the carousel view to check the battery levels.

    Figure 5. AIROC™ Bluetooth® Connect Battery Service App

  7. Tap Start Notify to get notifications of the changing battery level:

    Figure 6. Battery level notification

Debugging

You can debug the example using a generic Linux debugging mechanism such as the following:

  • Debugging by logging: Add prints in the application and check them during the execution.

  • Debugging using GDB: See the GDB man page for more details.

Design and implementation

Roles implemented:

A Bluetooth® LE GATT Server for Battery Service

Battery Service is used to simulate the battery level, which changes continuously from 100 to 0 percent in steps defined by the BATTERY_LEVEL_CHANGE macro. It has a default value of 2 percent. On a periodic timer, notifications are sent to the client

  • CE code example (implementation under test): BAS Target, GATT Server, and GAP Peripheral role

  • Testing mobile device with AIROC™ Bluetooth® Connect App: BAS Locator, GATT Client, and GAP Central role

Source file details

Files Description of Files
app/main.c Implements the main function which takes the user command-line inputs
app/bas.c Implements the battery server functionalities
app_bt_utils/app_bt_utils.c Contains utility functions such as functions to print error codes, status, etc, in a user-understandable format
app_bt_utils/app_bt_utils.h Header file corresponding to app_bt_utils.c
app_bt_config/bas_server_cfg.c Contains Bluetooth® GATT database
app_bt_config/bas_server_gap.c Contains Bluetooth® GAP parameters
app_bt_config/bas_server_gatt_db.c Contains Bluetooth® stack configuration parameters
include/bas.h Battery Server Header file

Resources and settings

Table 1. Application resources

Resource Alias/object Purpose
UART HCI UART is used for HCI communication with the host system

Related resources

Resources Links
Device documentation CYW5557x
AIROC™ BTSTACK library AIROC™ BTSTACK library
Linux porting layer source code Linux porting layer source code

Other resources

Infineon provides a wealth of data at www.infineon.com to help you select the right device, and quickly and effectively integrate it into your design.

Document history

Document title: CE237174 - AIROC™ BTSTACK: Bluetooth® Battery Server (BAS) for Linux host

Version Description of change
1.0.0 New code example


© Cypress Semiconductor Corporation, 2023. This document is the property of Cypress Semiconductor Corporation, an Infineon Technologies company, and its affiliates ("Cypress"). This document, including any software or firmware included or referenced in this document ("Software"), is owned by Cypress under the intellectual property laws and treaties of the United States and other countries worldwide. Cypress reserves all rights under such laws and treaties and does not, except as specifically stated in this paragraph, grant any license under its patents, copyrights, trademarks, or other intellectual property rights. If the Software is not accompanied by a license agreement and you do not otherwise have a written agreement with Cypress governing the use of the Software, then Cypress hereby grants you a personal, non-exclusive, nontransferable license (without the right to sublicense) (1) under its copyright rights in the Software (a) for Software provided in source code form, to modify and reproduce the Software solely for use with Cypress hardware products, only internally within your organization, and (b) to distribute the Software in binary code form externally to end users (either directly or indirectly through resellers and distributors), solely for use on Cypress hardware product units, and (2) under those claims of Cypress’s patents that are infringed by the Software (as provided by Cypress, unmodified) to make, use, distribute, and import the Software solely for use with Cypress hardware products. Any other use, reproduction, modification, translation, or compilation of the Software is prohibited.
TO THE EXTENT PERMITTED BY APPLICABLE LAW, CYPRESS MAKES NO WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, WITH REGARD TO THIS DOCUMENT OR ANY SOFTWARE OR ACCOMPANYING HARDWARE, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. No computing device can be absolutely secure. Therefore, despite security measures implemented in Cypress hardware or software products, Cypress shall have no liability arising out of any security breach, such as unauthorized access to or use of a Cypress product. CYPRESS DOES NOT REPRESENT, WARRANT, OR GUARANTEE THAT CYPRESS PRODUCTS, OR SYSTEMS CREATED USING CYPRESS PRODUCTS, WILL BE FREE FROM CORRUPTION, ATTACK, VIRUSES, INTERFERENCE, HACKING, DATA LOSS OR THEFT, OR OTHER SECURITY INTRUSION (collectively, "Security Breach"). Cypress disclaims any liability relating to any Security Breach, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any Security Breach. In addition, the products described in these materials may contain design defects or errors known as errata which may cause the product to deviate from published specifications. To the extent permitted by applicable law, Cypress reserves the right to make changes to this document without further notice. Cypress does not assume any liability arising out of the application or use of any product or circuit described in this document. Any information provided in this document, including any sample design information or programming code, is provided only for reference purposes. It is the responsibility of the user of this document to properly design, program, and test the functionality and safety of any application made of this information and any resulting product. "High-Risk Device" means any device or system whose failure could cause personal injury, death, or property damage. Examples of High-Risk Devices are weapons, nuclear installations, surgical implants, and other medical devices. "Critical Component" means any component of a High-Risk Device whose failure to perform can be reasonably expected to cause, directly or indirectly, the failure of the High-Risk Device, or to affect its safety or effectiveness. Cypress is not liable, in whole or in part, and you shall and hereby do release Cypress from any claim, damage, or other liability arising from any use of a Cypress product as a Critical Component in a High-Risk Device. You shall indemnify and hold Cypress, including its affiliates, and its directors, officers, employees, agents, distributors, and assigns harmless from and against all claims, costs, damages, and expenses, arising out of any claim, including claims for product liability, personal injury or death, or property damage arising from any use of a Cypress product as a Critical Component in a High-Risk Device. Cypress products are not intended or authorized for use as a Critical Component in any High-Risk Device except to the limited extent that (i) Cypress’s published data sheet for the product explicitly states Cypress has qualified the product for use in a specific High-Risk Device, or (ii) Cypress has given you advance written authorization to use the product as a Critical Component in the specific High-Risk Device and you have signed a separate indemnification agreement.
Cypress, the Cypress logo, and combinations thereof, WICED, ModusToolbox, PSoC, CapSense, EZ-USB, F-RAM, and Traveo are trademarks or registered trademarks of Cypress or a subsidiary of Cypress in the United States or in other countries. For a more complete list of Cypress trademarks, visit www.infineon.com. Other names and brands may be claimed as property of their respective owners.