STM32WB Bluetooth® LE – Heart Rate Sensor Project

1. Heart Rate Profile presentation

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.
Heart Rate Sensor is the device that measures heart rate data and other information, it can be defined as a GAP peripheral and GATT server implemented with the Heart Rate Service [3]

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

Bluetooth® LE Heart Rate Profile overview
Connectivity Heart-Rate-Profile-Overview.png


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

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

2. Heart Rate Profile interaction

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 Heart Rate Sensor:
    • Measures the heart rate and exposes it via the Heart Rate Service.
    • 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.

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



Once Bluetooth® LE connection is established and the notification enabled:

  • Heart Rate measurement provided to remote every 1 s.
  • Connection interval may be changed by the application.

3. Requirements

3.1. Software and system requirements

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

  • IAR Embedded Workbench for ARM (EWARM) toolchain V8.20.2
  • RealView Microcontroller Development Kit (MDK-ARM) toolchain V5.31
  • STM32CubeIDE toolchain V1.7.0 [5].


Programmer:

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

3.2. Hardware requirements

P-NUCLEO-WB55 [7] or STM32WB5MM-DK Discovery Kit [8] is necessary to install the application.

Hardware platforms illustration
Connectivity hardware-presentation.png


3.3. Smartphone application

BLE_HeartRate application works with ST BLE Sensor and ST BLE ToolBox applications.

4. STM32WB Heart Rate Sensor example description

4.1. Project directory

The "BLE_HeartRate" application is available by downloading STM32CubeWB MCU Package[9].

Heart Rate project directory
Connectivity heartRate.png

4.2. Project description

4.2.1. Structure

Software project structure with the most important parts:

Heart Rate project structure
Connectivity HR project structure.png
Connectivity yellow box.png
Main applicative part files
Connectivity blue box.png
Bluetooth® LE Stack API Interface
Connectivity dark blue box.png
Services management
Connectivity green box.png
System commands
Connectivity pink box.png
Transport Layer Interface

WARNING: Do not modify the files in Middlewares folder


4.2.2. Application initialization

The different steps of the application initialization are described below:

Heart Rate project initialization
Connectivity HR Application Initialization.png
puce1.png
  • Initialize the system (HAL, Clock, Power Mode, RTC IP, Timer Server)
  • Infinite loop for run mode
puce2.png
  • Initialize the BSP (LEDs, buttons…), Transport Layer Interface, Debugger
  • Wait for initialization done
puce3.png
  • Initialize the Bluetooth® LE App – GAP level
  • Manage GAP events (advertising,scanning)
puce4.png
  • Initialize the Bluetooth® LE App – GATT level
  • Initialize the Bluetooth® LE Stack
  • Manage GATT events
puce5.png
  • Register Event Handler
  • Initialize and manages services
  • Update characteristic
puce6.png
  • Initialize the context
  • Manage Heart Rate Service notification

4.2.3. GAP and GATT initialization and interaction

puce3.png

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

  • Start 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()
    • EVT_LE_CONN_COMPLETE - provides information of the connection interval, slave latency, supervision timeout
    • EVT_LE_CONN_UPDATE_COMPLETE - provides the new information of the connection
    • EVT_DISCONN_COMPLETE - informs the application about the link disconnection and the reason
    • EVT_ENCRYPT_CHANGE - informs the application weather the link is encrypted
puce4.png

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

  • Call the Initialization function of all the developed services - SVCCTL_SvcInit()
    • Heart Rate Service - HRS_Init() - hrs.c
    • Device Information service - DIS_Init() - dis.c
  • Register Services Event Handler - SVCCTL_RegisterSvcHandler(...)
  • Manage events - SVCCTL_UserEvtRx ()- from the BLE Host Stack and redirect them to the services registered - HearRate_Event_Handler() - or gap event handler - SVCCTL_App_Notification
puce5.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(HearRate_Event_Handler);
    • Initialize Service UUID – add Heart Rate service as Primary services
      • Initialize Heart rate measurement characteristic
      • Initialize Body Sensor location characteristic
  • Update Heart Rate measurement characteristic - HRS_UpdateChar()
  • Update Body Sensor location characteristic Value - HRS_UpdateChar()
  • Manage the GATT event from BLE Stack - HeartRate_Event_Handler()
    • 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 HeartRate-Project.png


puce6.png

The application level of the Heart Rate Sensor is done with hrs_app.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 (HRSAPP_Measurement) and increase the energy expended to transfer to the remote device (collector) - HRS_UpdateChar()

4.3. Build and install

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

4.4. How to use

Once the BLE Heart Rate application is installed on the STM32WB platform, launch ST BLE Sensor or ST BLE ToolBox smartphone application. Then, scan and connect the device called HRSTM 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

  • SW1 allows to clear the security database
  • SW2 allows to request pairing

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

  • SW3 allows to switch to 2 Mbits or 1 Mbits PHY

4.5. Optimize low power

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

  • no debug trace
  • no debugger

The evaluation boards include the components to support the SMPS (switched mode power supply) integrated in microcontrollers.

  • P-NUCLEO-WB55 pack including a USB dongle and a Nucleo-64 board with STM32WB55 MCU
  • NUCLEO-WB15CC
  • STM32WB5MM-DK Discovery

It is possible to enable the feature within app_ble.h

/**
 * SMPS supply
 * SMPS not used when Set to 0
 * SMPS used when Set to 1
 */
#define CFG_USE_SMPS    1

4.6. 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:

/**
 * When set to 1, the traces are enabled in the BLE services
 */
#define CFG_DEBUG_BLE_TRACE     1
/**
 * Enable or Disable traces in application
 */
#define CFG_DEBUG_APP_TRACE     1
Heart Rate Sensor - Initialization phase Connected Phase
Wireless Firmware version 1.13.0
Wireless Firmware build 5
FUS version 1.2.0
>>== SHCI_SUB_EVT_CODE_READY

>>== WIRELESS_FW_RUNNING 
>>== DBGMCU_GetRevisionID= 2001 

==>> 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:26:ff:eb
  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: 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

Device Information Service (DIS) is added Successfully 000C
Manufacturer Name Characteristic Added Successfully  000D 
Model Number String Characteristic Added Successfully  000F 
Heart Rate Service (HRS) is added Successfully 0011
Heart Rate Measurement Characteristic Added Successfully  0012 
Sensor Location Characteristic Added Successfully  0015 
Control Point Characteristic Added Successfully  0017 
==>> aci_gap_set_discoverable - Success
==>> Success: Start Fast Advertising
>>== HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - Connection handle: 0x801
     - Connection established with Central: @:72:16:9d:9e:d5:8e
     - 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
==>> hci_le_read_phy - Success
==>> PHY Param  TX= 1, RX= 1 
==>> hci_le_set_phy PHY Param  TX= 2, RX= 2 - Success

==>> HCI_LE_PHY_UPDATE_COMPLETE_SUBEVT_CODE - status ok 
==>> hci_le_read_phy - Success 
==>> PHY Param  TX= 2, RX= 2

5. Heart Rate Collector - smartphone application

5.1. ST BLE Sensor application

Once the BLE Heart Rate application is installed on the STM32WB platform, launch the ST BLE Sensor smartphone application. Click on the interface Connect one Device. Then your device should be visible and connectable by clicking on it. Now bpm measurement is displayed in real time.

BLE Heart Rate application on ST BLE Sensor
Connectivity Heart-Rate-screen-cap-1.jpg
Connectivity Heart-Rate-application-2.jpg
Connectivity Heart-Rate-App-3.jpg


5.2. ST BLE ToolBox application

Once the BLE Heart Rate application is installed on the STM32WB 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 ST BLE ToolBox HR Scan.jpg
Connectivity ST BLE ToolBox HR adv.jpg
Connectivity ST BLE ToolBox HR.jpg


With the Discovery kit platform, the Heart Rate is also visible from the board LCD screen, and the heart pulse is illustrated below:

Heart Rate on DK illustration
Connectivity heart-rate-DK.png

6. References