STM32WBA Bluetooth® LE – Heart Rate Sensor

Revision as of 16:23, 27 February 2023 by Registered User (→‎UART debug trace)

1. Heart Rate Profile

Heart Rate Profile (HRP) [1] is a Generic Attribute Profile (GATT) based low-energy profile defined by the Bluetooth® Special Interest Group[2]
The HRP, widely used in fitness applications, defines the communication between a GATT-server of a Heart Rate Sensor device, such as a wrist band, and a GATT-client Collector device, such as a smartphone or tablet.

  • The Heart Rate Sensor:
    • Measures the heart rate and exposes it via the Heart Rate Service[3]
    • Contains the Device Information Service, which contains information for example about the manufacturer of the device.
    • Is the GATT server.
  • The Heart Rate Collector:
    • Accesses the information exposed by the Heart Rate Sensor and can for example display it to the end user or store it on nonvolatile memory for later analysis.
    • Is the GATT client.

This wiki page is the description of the Heart Rate Sensor project provided within the STM32CubeWBA MCU Package[4]

Bluetooth® LE Heart Rate Profile & STM32WBA
STM32WBA Heart Rate Profile


The table below describes the structure of Heart Rate Sensor services:

Bluetooth® LE Heart Rate Service specification
Service Characteristic Mode UUID size
Heart Rate Service 0x180D
Heart Rate Measurement Notify 0x2A37 23
Body Sensor Location Read 0x2A38 1
Heart Rate Control Point Write 0x2A39 1
Device Information Service 0x180A
Manufacturer Name String Read 0x2A29 32


Heart Rate Profile is a combination of a Heart Rate Collector and a Heart Rate Sensor to connect and exchange data in different applications.

The GAP - Generic Access Profile defines and manages advertising and connection.

Collector Central device and Sensor Peripheral device
Connectivity HR GAP Interaction.png


The GATT - Generic Attribute Profile defines and manages in/out data exchange.

Collector GATT client device and Sensor GATT service device
Connectivity HR GATT Interaction.png


Example of flow diagram between STM32WBA & ST BLE Toolbox
STM32WBA HR Flow Diagram


1.1. Advertising Data

At start up, Heart rate Sensor application starts Fast Advertising(80ms/100ms).
Data advertised are composed as follow:

Heart Rate Advertising packet
Description Length AD Type Value
Device Name 6 0x09 HR_XX (XX: last byte of BD address)
Service UUID 3 0x03 HRS
Manufacturer Data 15 0xFF See table below
Flags 2 0x01 0x06
(GeneralDiscoverable, BrEdrNotSupported)



Manufacturer data are encoded following STMicroelectronics BlueST SDK v2 as described below:

STMicroelectronics Manufacturer Advertising data
Byte Index 0 1 2-3 4 5 6 7 8 9 10-15
Function Length Manufacturer ID Company BlueST SDK Version Device ID Firmware ID Option 1 Option 2 Option 3 Device Address
Value 0x0F 0xFF 0x0030 STMicro 0x02 0x8B Nucleo-WBA 0x89 - HR Sensor 0x00 0x00 0x00 0x08E12Axxxx


Advertising is switched to Low Power advertising (1s/2.5s) after 60s

1.2. On board buttons configuration

HR Sensor Application Nucleo-WBA buttons configuration
Application Condition B1 Click B1 Long Press B2 Click B2 Long Press B3 Click B3 Long Press
HR Sensor Idle

Connected

Adv start

Toggle PHY(1M/2M)

/ Clear Sec db

Slave Sec Req

/ -

L2CAP CONN REQ

/

2. Requirements

2.1. Software and system requirements

The software required are the following (minimum IDEs version):

  • IAR Embedded Workbench for ARM (EWARM) toolchain V9.20.1, plus a patch available in Utilities/PC_Software/EWARMv8_STM32WBAx_V1.2.zip
  • RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.37, plus a patch available in Utilities/PC_Software/Keil.STM32WBAx_DFP.1.0.0.zip
  • STM32CubeIDE toolchain V1.12.0 [5].

Programmer:

  • STM32CubeProgrammer[6] : To flash the board with an already generated binary

2.2. Hardware requirements

NUCLEO-WBA5 [7] is necessary to install the application.

Hardware platform illustration
Nucleo-WBA5


2.3. Collector applications compatible

The STM32CubeWBA Heart Rate Sensor project is compatible with the following collector:

3. STM32WB Heart Rate Sensor example description

3.1. Project directory

The "BLE_HeartRate" application is available by downloading STM32CubeWBA MCU Package[8].

Refer to How to Build a BLE Project wiki page for project directory information.

3.2. Project description

3.2.1. Structure

Software project structure with the most important parts:

Heart Rate project structure
Connectivity WBA HeartRate Archi.png
Connectivity yellow box.png
Main applicative part files
Connectivity dark blue box.png
Services management
Connectivity green box.png
BLE libraries
Connectivity pink box.png
Link Layer System integration files

WARNING: Do not modify the files in Middlewares folder


3.2.2. Application initialization

The different steps of the application initialization are described below:

Heart Rate project initialization
Connectivity WBA HeartRate Initialization.png
puce1.png
  • Initialize the system (HAL, clocks, peripherals)
  • Infinite loop for run mode
puce2.png
  • Initialize the BSP, Power Mode, trace, memory manager, NVM
  • Wait for initialization done
puce3.png
  • Initialize the Bluetooth® LE Host Stack
puce4.png
  • Initialize the Bluetooth® LE GATT level
  • Initialize the Bluetooth® LE GAP level
puce5.png
  • Reset the number of registered handler
puce6.png
  • Initialize the context
  • Manage Heart Rate Service notification
puce7.png
  • Register Service Handler
  • Update services and characteristics

3.2.3. GAP and GATT initialization and interaction

puce3.png

and puce4.png The Bluetooth LE Heart Rate Sensor application initialization is done within app_ble.c

  • Initialize the BLE stack - initialize the device as peripheral - configure and start advertising: ADV parameters, local name, UUID - APP_BLE_init()
  • Call the services controller initialization SVCCTL_Init() - svc_ctl.c
  • Manage the GAP event - SVCCTL_App_Notification()
    • HCI_LE_CONNECTION_COMPLETE - provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE- provides the new information of the connection
    • HCI_DISCONNECTION_COMPLETE - informs the application about the link disconnection and the reason
puce5.png

The Services management is done by the service controller, svc_ctl.c

  • Initialize the number of registered handler - SVCCTL_Init()
  • Manage events - SVCCTL_UserEvtRx()- from the BLE Host Stack and redirect them to the gap event handler - SVCCTL_App_Notification
puce6.png

The application level of the Heart Rate Sensor is done with hrs_app.c:

  • Initialization of the services:
    • Heart Rate Service - HRS_Init() - hrs.c
  • Initialization of the context of the application
    • Measurement value flags support
    • Body sensor location - HRS_BODY_SENSOR_LOCATION_HAND
  • Receive notification from the Heart Rate Service - HRS_Notification()
  • When Heart rate measurement characteristics are enabled by the remote, simulate every 1 s the Heart Rate Measure (HRS_APP_Measurement) and increase the energy expended to transfer to the remote device (collector) - HRS_UpdateValue()
puce7.png

The Heart Rate Service hrs.c manages the specification of the service:

  • Service Init - HRS_Init()
    • Register Heart Rate Event Handle to Service Controller - SVCCTL_RegisterSvcHandler(HRS_EventHandler);
    • Initialize Service UUID – add Heart Rate service as Primary services
      • Initialize Heart rate measurement characteristic
      • Initialize Body Sensor location characteristic
  • Manage the GATT event from BLE Stack - HRS_EventHandler()
    • ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE
      • Reception of a Write Command: HR Control Point Characterictic Value
        • Send an aci_gatt_write_response() with an OK or KO status.
        • Notify the application to Reset Energy Expended - HRS_Notification(HRS_RESET_ENERGY_EXPENDED_EVT)
    • ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE
      • Reception of an attribute modification - HR Measurement Characteristics Description Value : ENABLE or DISABLE Notification
        • Notify application of the Measurement Notification - HRS_Notification(HRS_NOTIFICATION_ENABLED/DISABLED)
Heart Rate Sensor software module interaction
Connectivity WBA HeartRate module interaction with puces.png

3.3. Build and install

Follow the steps described in Bluetooth® LE Build and Install Application page, applying them for STM32WBA BLE_HeartRate project.

3.4. How to use

Once the BLE Heart Rate application is installed on the STM32WBA platform, launch ST BLE Sensor or ST BLE ToolBox smartphone application. Then, scan and connect the device called HR_XX (where XX is replaced by the last byte of the BD address) to the application.
Once the Bluetooth® LE connection is established and the notification enabled by the smartphone:

  • Heart Rate measurement is provided to remote every 1 s.
    • thanks to the timer server and the task manager

BLE Heart Rate application support the pairing procedure

  • Pressing B2 while not connected allows to clear the security database
  • Pressing B2 while connected allows to request pairing

With the WBA5x.Nucleo board, it is also possible to change RF PHY modulation

  • Pressing B1 while connected allows to switch between 2 Mbits and 1 Mbits PHY

BLE Heart Rate application allows other actions on button:

  • Pressing B1 while not connected allows to restart the fast advertising
  • Pressing B3 while connected allows to update the connection interval

3.5. Optimize low power

The project is delivered with the full system low-power enabled:

  • no debug trace
  • no debugger

It is possible to enable/disable the Low Power feature within app_conf.h

/******************************************************************************
 * Low Power
 *
 *  When CFG_FULL_LOW_POWER is set to 1, the system is configured in full
 *  low power mode. It means that all what can have an impact on the consumptions
 *  are powered down.(For instance LED, Access to Debugger, Etc.)
 *
 *  When CFG_FULL_LOW_POWER is set to 0, the low power mode is not activated
 *
 ******************************************************************************/
#define CFG_FULL_LOW_POWER       (1)



3.6. UART debug trace

Thanks to the debug log via UART interface LINK TO UPDATE?, it is possible to trace the application project.

To enable the traces within the project, enable them within app_conf.h as described below:

/**
 * Enable or Disable traces in application
 */
#define CFG_DEBUG_APP_TRACE         (0)

Debugger can also be supported by enabling it within app_conf.h as described below:

/**
 * User interaction
 * When CFG_LED_SUPPORTED is set, LEDS are activated if requested
 * When CFG_BUTTON_SUPPORTED is set, the push button are activated if requested
 * When CFG_DBG_SUPPORTED is set, the debugger is activated
 */
[...]
#define CFG_DBG_SUPPORTED                       (1)
Heart Rate Sensor - Initialization phase Connected Phase
==>> Start Ble_Hci_Gap_Gatt_Init function
  Success: hci_reset command
  Success: aci_hal_write_config_data command - CONFIG_DATA_PUBADDR_OFFSET
  Public Bluetooth Address: 00:80:e1:2a:7c:ea
  Success: aci_hal_write_config_data command - CONFIG_DATA_IR_OFFSET
  Success: aci_hal_write_config_data command - CONFIG_DATA_ER_OFFSET
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_init command
  Success: aci_gap_init command
  Success: aci_gatt_update_char_value - Device Name
  Success: aci_gatt_update_char_value - Appearance
  Success: hci_le_set_default_phy command
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_authentication_requirement command
  Success: aci_gap_configure_whitelist command
==>> End Ble_Hci_Gap_Gatt_Init function

Services and Characteristics creation
  Success: aci_gatt_add_service command: HRS
  Success: aci_gatt_add_char command   : HRME
  Success: aci_gatt_add_char command   : BSL
  Success: aci_gatt_add_char command   : HRCP
  Success: aci_gatt_update_char_value BSL command
  Success: aci_gatt_add_service command: DIS
  Success: aci_gatt_add_char command   : MANS
  Success: aci_gatt_add_char command   : MNBS
  Success: aci_gatt_add_char command   : SNS
  Success: aci_gatt_add_char command   : HRS
  Success: aci_gatt_add_char command   : FRS
  Success: aci_gatt_update_char_value MANS command
  Success: aci_gatt_update_char_value MNBS command
  Success: aci_gatt_update_char_value SNS command
  Success: aci_gatt_update_char_value HRS command
  Success: aci_gatt_update_char_value FRS command
End of Services and Characteristics creation

==>> aci_gap_set_discoverable - Success
==>> Success: Start Advertising
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x0001
     - Connection established with @:62:f1:e7:ab:a6:18
     - Connection Interval:   48.75 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   7.50 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   48.75 ms
     - Connection latency:    0
     - Supervision Timeout:   5000 ms
ACI_GATT_ATTRIBUTE_MODIFIED_VSEVT_CODE HRS_NOTIFICATION_ENABLED

4. Heart Rate Collector - smartphone application

4.1. ST BLE ToolBox application

Once the BLE Heart Rate application is installed on the STM32WBA platform, launch the ST BLE ToolBox smartphone application. The application starts scanning and allows to see advertisement data or to connect to the device. Once the device is connected bpm and kcal measurements are displayed in real time.

BLE Heart Rate application on ST BLE ToolBox
Connectivity WBA HeartRate Toolbox scan.jpg
Connectivity WBA HeartRate Toolbox adv.jpg
Connectivity WBA HeartRate Toolbox connected.jpg


5. References