Getting started with the Heart Rate Sensor within STM32WBA Bluetooth® Low Energy

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 process 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 that includes 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. It can display it to the end user, or store it on a nonvolatile memory for later analysis.
    • Is the GATT client.

The description of the Heart Rate Sensor project provided within the STM32CubeWBA MCU Package[4] is the subject of this article.

Bluetooth® Low Energy Heart Rate Profile & STM32WBA
STM32WBA Heart Rate Profile


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

Bluetooth® Low Energy 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


The Heart Rate Profile is a combination of a Heart Rate Collector and a Heart Rate Sensor. The goal is 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 & STBLEToolbox
STM32WBA HR Flow Diagram


1.1. Advertising Data

At startup, the Heart Rate Sensor application starts a 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 0x180D (HRS)
Manufacturer Data 15 0xFF See table below
Flags 2 0x01 0x06
(GeneralDiscoverable, BrEdrNotSupported)



The 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 Board ID Firmware ID Option 1 Option 2 Option 3 Device Address
Value 0x0F 0xFF 0x0030 STMicroelectronics 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

Button configuration for Bluetooth® Low Energy HeartRate Application on Nucleo-WBA55CG boards
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 Hardware requirements

For software and hardware requirements refer to STM32WBA Build BLE Project wiki page.

2.2. Collector applications compatible

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

3. STM32WB Heart Rate Sensor example description

3.1. Project directory

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

Refer to How to Build a Bluetooth® Low Energy project Wiki page for project directory information.

3.2. Project description

3.2.1. Structure

Below, a 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
Bluetooth® Low Energy libraries
Connectivity pink box.png
Link Layer System integration files

WARNING: Do not modify the files in Middleware folder


3.2.2. Application initialization

The different steps of the application initialization are described below:

Heart Rate project initialization
Error creating thumbnail: File missing
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® Low Energy Host Stack.
puce4.png
  • Initialize the Bluetooth® Low Energy GATT level.
  • Initialize the Bluetooth® Low Energy GAP level.
puce5.png
  • Reset the number of registered handlers.
puce6.png
  • Initialize the context.
  • Manage the Heart Rate Service notification.
puce7.png
  • Register Service Handler.
  • Update the services and characteristics.

3.2.3. GAP and GATT initialization and interaction

Heart Rate Sensor software module interaction
Connectivity WBA HeartRate module interaction with puces.png
puce3.png

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

  • Initialize the Bluetooth® Low Energy 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 handlers - SVCCTL_Init().
  • Manage events - SVCCTL_UserEvtRx()- from the Bluetooth® Low Energy 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()
    • Registers Heart Rate Event Handle to Service Controller - SVCCTL_RegisterSvcHandler(HRS_EventHandler).
    • Initializes Service UUID – add Heart Rate service as Primary services.
      • Initializes Heart rate measurement characteristic.
      • Initializes Body Sensor location characteristic.
  • Manages the GATT event from the Bluetooth® Low Energy Stack - HRS_EventHandler().
    • ACI_GATT_WRITE_PERMIT_REQ_VSEVT_CODE
      • Reception of a Write Command: HR Control Point Characteristic Value
        • Sending of an aci_gatt_write_response() with an OK or KO status
        • Notification to 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)

3.3. How to use the Bluetooth® Low Energy Heart Rate application

Once the Bluetooth® Low Energy Heart Rate application is installed on the STM32WBA platform, launch the 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® Low Energy connection is established and the notification enabled by the smartphone:

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

The Bluetooth® Low Energy Heart Rate application supports the pairing procedure.

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

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

  • Pressing B1 while connected allows to switch between 2 Mbit/s and 1 Mbit/s PHY.

The Bluetooth® Low Energy 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.4. Low-power optimization

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.5. UART debug trace

Thanks to the debug log via UART interface , 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)

A 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 Bluetooth® Low Energy Heart Rate application is installed on the STM32WBA platform, launch the STBLEToolbox smartphone application. The application starts scanning and enables seeing advertisement data, or to connect to the device. Once the device is connected, bpm and kcal measurements are displayed in real time.

Bluetooth® Low Energy Heart Rate application on STBLEToolbox
Connectivity WBA HeartRate Toolbox scan.jpg
Connectivity WBA HeartRate Toolbox adv.jpg
Connectivity WBA HeartRate Toolbox connected.jpg


5. References