STM32CubeWBA Bluetooth® LE Timing Profiling


1. Introduction

The goal of this wiki page is to describe how the STM32WBA works to meet RF timing requirements and how to profile the system and link layer timings. These timings need to be profiled to optimize the device power consumption and to avoid any RF missed event, ensuring they fit perfectly with each use case.

2. Overview

To schedule an RF event, the Bluetooth® LE Link Layer (LL) starts a timer based on the event anchor point and on the LL processes duration. When this timer expires, the Radio IRQ (HW signal) is fired to execute LL processes to manage the radio activity event (pre-event). After the radio activity, a second Radio IRQ is fired to execute the post-event LL processes. These LL processes are executed in two ISRs: the LL High ISR and the LL Low ISR.

In case of an RF event, the RADIO IRQ Handler is called, as this is the only interrupt line for the radio. The real time code is handled inside the RADIO IRQ Handler, which corresponds to the LL High ISR and the non-real time code is handled inside the HASH IRQ Handler, which corresponds to the LL Low ISR. The LL Low ISR is a SW ISR that uses an existing interrupt line and handler (HASH interrupt by default but it is configurable through RADIO_SW_LOW_INTR_NUM define in app_conf.h file).

Radio activity event management in run mode
Information
When the system goes back to idle after the HASH ISR execution, the system goes into STOP0 low power mode as it’s the lowest low power mode available when the radio IP is active.

The RADIO and the HASH ISRs have different priorities depending on when they are executed. The RADIO ISR priority changes during the execution of the HASH ISR and vice-versa.

  • The RADIO IRQ Handler is priority 0 when the radio is active and priority 5 when the radio is not active.
  • The HASH IRQ Handler can be either priority 5 or 15 depending on when the ISR is triggered.
Radio activity event management in run mode with interrupt priority

3. Link Layer timings

3.1. Run mode

In the following part, the focus is on the “pre-event” part and on link layer timings. Several timings are involved inside the link layer to manage the radio event:

  • The DRIFT_TIME corresponds to the time between the Radio IRQ and the beginning of the Link Layer event setup. It can be tuned and overwritten by the application.
  • The EXEC_TIME corresponds to the time required by the Link Layer to prepare the system to configure the next radio event. It can be tuned and overwritten by the application.
  • The PHY_WAKEUP_TIME corresponds to the time required to switch the PHY from sleep to active state and be ready for TX/RX packets. The value is set inside the link layer and can’t be modified.

The margin M1 between the end of Hash IRQ handler and the radio activity allows the application / system to preempt the LL processes. A preemption longer than the margin M1 duration leads to an event missed (no radio activity on this event).

These link layers timings depend on several parameters like the BLE scenario (advertising, connection, scan…), the system (low power mode, system clock, low speed clock) and the IDEs/project optimization, etc. That is why it is important to tune it based on each use case to be specific and optimize.

In the STM32CubeWBA[1] deliveries, default values are defined in the link layer which allow managing most of the use cases.
However, for the following configurations since STM32CubeWBA[1] v1.6.0, the DRIFT_TIME and the EXEC_TIME have been overwritten at system level:

  • For STM32CubeIDE Debug configuration as the optimization are set to none
  • In the case where LSI2 is used as 2.4 GHz RADIO sleep timer

By default, the margin M1 is around 200 us but it can vary depending on the use cases/configuration as explained above.

Radio activity event management in run mode with link layer timings

3.2. STOP1 low power mode

When STOP1 low power mode is used, an additional system process needs to be considered, it is called the STOP1 exit in the diagram above. The STOP1 exit allows restoring the clock configuration when the system wakes up from STOP1 low power mode and it is performed under critical section when low power manager is used. The critical section duration depends on the application, whether the system clock manager (SCM) is used or not, the clock used, the code optimization, etc., and delays the process of the RADIO IRQ handler. That is why the STOP1 exit duration needs to be considered in the DRIFT_TIME measurement.

When using low power mode (STOP or Standby), the link layer timings should be profiled in STOP1 low power mode as described in the following part.

Radio activity event management in STOP1 mode with link layer timings

3.3. Requirements

  • All the time combined that the system spends in interrupt service routines (ISRs) with a priority equal to RADIO_INTR_PRIO_HIGH value (set to 0 by default) or lower must be less than 40 microseconds to ensure proper system performance and timing.
  • All the time combined that the system spends in interrupt service routines (ISRs) with a priority equal to RADIO_INTR_PRIO_LOW value (set to 5 by default) or lower must be less than margin M1 duration to ensure proper system performance and timing.

Note: A lower numerical priority value indicates a higher level of priority.

4. Link Layer timings profiling

The following part explains how to profile the link layer timings and how to set these new timings. As soon as the device goes into at least STOP low power mode, the link layer timings must be profiled in STOP1; otherwise, they are be profiled in RUN mode.

4.1. GPIOs configuration to measure Link Layer timings

To profile the DRIFT_TIME and the EXEC_TIME, the following GPIOs are needed:

  • RADIO_IRQ DTB – PA6: CN3 pin 30 on NUCLEO-WBA55CG and NUCLEO-WBA65RI
  • TX_ON DTB – PA1: CN3 pin 34 on NUCLEO-WBA55CG and NUCLEO-WBA65RI

To enable the DTB signals, set the following preprocessor directive in app_conf.h file to 1. By default RADIO_IRQ and TX_ON are enabled as soon as DTBs are enabled:

#define CFG_RT_DEBUG_DTB                 (1)

Some debug GPIOs of the RT Debug module are also needed. To enable the module set the following preprocessor directive in app_conf.h file to 1:

#define CFG_RT_DEBUG_GPIO_MODULE                              (1)

Then, enable the following GPIOs and choose the GPIO you want to use in debug_config.h file:

#define USE_RT_DEBUG_PROFILE_END_DRIFT_TIME                   (1)
#define GPIO_DEBUG_PROFILE_END_DRIFT_TIME                     {GPIOA, GPIO_PIN_0}  // CN3 pin 38 on NUCLEO-WBA55CG

For STM32CubeWBA v1.5.0 and older version:

#define USE_RT_DEBUG_SCHDLR_EXEC_EVNT_PROFILE                (1)
#define GPIO_DEBUG_SCHDLR_EXEC_EVNT_PROFILE                  {GPIOB, GPIO_PIN_15} // CN4 pin 21 on NUCLEO-WBA55CG

For STM32CubeWBA v1.6.0 and newer version:

#define USE_RT_DEBUG_SCHDLR_EXEC_EVNT_TRACE                  (1)
#define GPIO_DEBUG_SCHDLR_EXEC_EVNT_TRACE                    {GPIOB, GPIO_PIN_5} // CN4 pin 35 on NUCLEO-WBA55CG

Additional GPIOs can be used for better understanding:

#define USE_RT_DEBUG_LLWCC_CMN_HG_ISR                        (1)
#define GPIO_DEBUG_LLWCC_CMN_HG_ISR                          {GPIOB, GPIO_PIN_2} // CN4 pin 3 on NUCLEO-WBA55CG
#define USE_RT_DEBUG_LLHWC_CMN_LW_ISR                        (1)
#define GPIO_DEBUG_LLHWC_CMN_LW_ISR                          {GPIOA, GPIO_PIN_12} // CN4 pin 17 on NUCLEO-WBA55CG


4.2. How to measure Link Layer timings

Depending on the device role, the timings must be measured:

  • During an advertising event for broadcaster role
  • Before a scan window for observer role
  • During a connection event for peripheral and central role

In case of multiple role, the worst timings must be considered. Once the GPIOs are configured and the scenario is executed, first identify the RF activity of the scenario using TX_ON GPIO and then identify the preceding RADIO_IRQ to perform the timings measurement.

4.2.1. DRIFT_TIME profiling

The DRIFT_TIME can be profiled as described below:

  • Start the measurement at the rising edge of the RADIO_IRQ signal
  • Stop the measurement at the edge of the DBG_IO_PROFILE_END_DRIFT_TIME signal

Once the DRIFT_TIME is measured in microseconds, the value will be computed to have the number of cycles required for the DRIFT_TIME, using the following formula:

V_DRIFT_TIME = CEIL(T_DRIFT_TIME / T_CYCLE) + V_MARGIN

With:

  • V_DRIFT_TIME: Value to set in the Link Layer and corresponds to the number of cycles required for the DRIFT_TIME.
  • T_DRIFT_TIME: DRIFT_TIME duration measured previously in microseconds.
  • T_CYCLE: Duration of the Radio sleep timer clock cycle (~30.52us)
  • V_MARGIN: Number of cycles added to allow preemption of Link Layer process. It is up to the application to choose the margin depending on the interrupt duration which can preempt link layer process.

For example, if the T_DRIFT_TIME is equal to 208 us and 200 us (7 cycles) of margin is required, V_DRIFT_TIME will be equal to 14 cycles:

V_DRIFT_TIME = CEIL(208 / 30.52) + 7 = 7 + 7 = 14 cycles

4.2.2. EXEC_TIME profiling

With STM32CubeWBA v1.5.0 and older versions

The EXEC_TIME can be profiled as described below:

  • Start the measurement at the rising edge of the DBG_IO_SCHDLR_EXEC_EVNT_PROFILE
  • Stop the measurement at the falling edge of the DBG_IO_ SCHDLR_EXEC_EVNT_PROFILE

Once the value is measured in microseconds, the value will be computed to have the number of cycles required for the EXEC_TIME, using the following formula:

EXEC TIME = CEIL( EXEC PROFILED / T_CYCLE)

With:

  • EXEC PROFILED: Value measured in microseconds
  • T_CYCLE: the duration of the Radio sleep timer clock cycle (~30.52us)

For example, if EXEC PROFILED is equal to 639 us, the EXEC TIME is equal to 21 cycles:

EXEC TIME = CEIL( 639 / 30.52) = 21 cycles

Link layers profiling during a connection event using STM32CubeWBA v1.5.0
With STM32CubeWBA v1.6.0 and v1.6.1

The EXEC_TIME can be profiled as described below:

  • Start the measurement at the rising edge of the DBG_IO_SCHDLR_EXEC_EVNT_TRACE
  • Stop the measurement at the falling edge of the DBG_IO_SCHDLR_EXEC_EVNT_TRACE

Once the value is measured in microseconds, the value will be computed to have the number of cycles required for the EXEC_TIME, using the following formula:

EXEC TIME = CEIL( EXEC PROFILED / T_CYCLE) – PHY_WAKEUP_TIME + 2

With:

  • EXEC PROFILED: Value measured in microseconds
  • T_CYCLE: Duration of the Radio sleep timer clock cycle (~30.52us)
  • PHY_WAKEUP_TIME equals to 15 for STM32WBA5x and 17 for STM32WBA6x in STM32CubeWBA v1.6.0

For example, if EXEC PROFILED is equal to 617 us on STM32WBA5x, the EXEC TIME is equal to 8 cycles:

EXEC TIME = CEIL( 617 / 30.52) – 15 + 2 = 21 - 15 + 2 = 8 cycles


Link layers profiling during a connection event using STM32CubeWBA v1.6.0
With STM32CubeWBA v1.7.0 and newer version

The EXEC_TIME can be profiled as described below:

  • Start the measurement at the rising edge of the DBG_IO_SCHDLR_EXEC_EVNT_TRACE
  • Stop the measurement at the falling edge of the DBG_IO_SCHDLR_EXEC_EVNT_TRACE

Once the value is measured in microseconds, the value will be computed to have the number of cycles required for the EXEC_TIME, using the following formula:

EXEC TIME = CEIL( EXEC PROFILED / T_CYCLE)

With:

  • EXEC PROFILED: Value measured in microseconds
  • T_CYCLE: Duration of the Radio sleep timer clock cycle (~30.52us)

For example, if EXEC PROFILED is equal to 617 us on STM32WBA5x, the EXEC TIME is equal to 21 cycles:

EXEC TIME = CEIL( 617 / 30.52) = 21 cycles


Link layers profiling during a connection event using STM32CubeWBA v1.7.0

4.3. How to set the new timings

Once the values have been computed, the following API can be used to set the new timings:

ll_sys_config_BLE_schldr_timings(DRIFT_TIME, EXEC_TIME);

With:

  • DRIFT_TIME: Drift time value in number of cycles
  • EXEC_TIME: Exec time value in number of cycles

This API is already called in ll_sys_reset function in ll_sys_if.c file with the drift_time and exec_time variables as parameters, these variables can be overwritten in ll_sys_reset_1 user section with the computed values.

5. Standby low power mode timings

Before starting to profile timings regarding standby low power mode, the link layer timings must be profiled (DRIFT_TIME and EXEC_TIME), refer to Link Layer timings profiling part.

When standby low power mode is used, additional processes need to be considered: they are called the standby exit and the LL deep sleep exit in the diagram below. When exiting from standby, the system needs to wake up earlier to restore the CPU context, the clock configuration, to reinitialize the peripherals and to exit from deep sleep the RF IP. The radio sleep timer used to schedule the radio IRQ is reprogrammed to consider the duration of standby exit and LL deep sleep exit processes. These duration are considered through CFG_LPM_STDBY_WAKEUP_TIME preprocessor directive in app_conf.h file. This preprocessor directive is equal to the sum of standby exit duration (duration X) and deep sleep exit duration (duration Y).

The standby exit is performed under critical section and its duration depends on the application: the clock used, if the SCM is used or not, the number of peripheral to restore… The LL deep sleep exit depends only on the system clock frequency.

In Bluetooth® LE applications available in STM32CubeWBA[1] package, the CFG_LPM_STDBY_WAKEUP_TIME preprocessor directive is set at a default value equal to 1500 us when standby is enabled (CFG_LPM_STDBY_SUPPORTED set to 1). The default value has been set to 1500 to support all the configuration and include some margin, to reduce the device power consumption this value should be profiled and set to the right value according to the application.

Radio activity event optimize in Standby mode

If the value in CFG_LPM_STDBY_WAKEUP_TIME preprocessor directive is:

  • Too small: This means that the value is underestimated. It will reduce the margin M1 and then it can lead to miss the radio activity event if all the margin is consumed.
  • Too big: This means that the value is overestimated. It will increase the device power consumption, the system goes back in STOP1 low power mode and then wakes up again to manage the radio activity as shown in the diagram below:
Radio activity event not optimize in Standby mode

That is why the standby exit and the LL deep sleep exit duration must be profiled. Find out in the next chapters how to profile and configure them.

6. Standby low power mode timings profiling

With STM32CubeWBA v1.5.0 and older version

6.1. With STM32CubeWBA v1.5.0 and older version

6.1.1. GPIOs configuration to measure Standby timings

To profile Standby timings, the following GPIOs are needed:

  • RADIO_IRQ DTB – PA6: CN3 pin 30 on NUCLEO-WBA55CG and NUCLEO-WBA65RI

To enable the DTB signals, set the following preprocessor directive in app_conf.h file to 1. By default RADIO_IRQ and TX_ON are enabled as soon as DTBs are enabled:

#define CFG_RT_DEBUG_DTB                 (1)

Some debug GPIOs of the RT Debug module are also needed. To enable the module set the following preprocessor directive in app_conf.h file to 1:

#define CFG_RT_DEBUG_GPIO_MODULE                              (1)

Then, enable the following GPIOs and choose the GPIO you want to use in debug_config.h file:

#define USE_RT_DEBUG_LOW_POWER_STANDBY_MODE_EXIT              (1)
#define GPIO_DEBUG_LOW_POWER_STANDBY_MODE_EXIT                {GPIOA, GPIO_PIN_2}   // CN3 pin 32 on NUCLEO-WBA55CG 
#define USE_RT_DEBUG_SCHDLR_HNDL_NXT_TRACE                    (1)
#define GPIO_DEBUG_SCHDLR_HNDL_NXT_TRACE                      {GPIOB, GPIO_PIN_9}  // CN4 pin 23 on NUCLEO-WBA55CG
#define USE_RT_DEBUG_LLHWC_CMN_LW_ISR                         (1)
#define GPIO_DEBUG_LLHWC_CMN_LW_ISR                           {GPIOA, GPIO_PIN_12} // CN4 pin 17 on NUCLEO-WBA55CG

Additional GPIOs can be used for better understanding:

#define USE_RT_DEBUG_LLWCC_CMN_HG_ISR                        (1)
#define GPIO_DEBUG_LLWCC_CMN_HG_ISR                          {GPIOB, GPIO_PIN_2} // CN4 pin 3 on NUCLEO-WBA55CG
Standby timings profiling

6.1.2. How to measure Standby timings

Once the GPIOs are configured, the CFG_LPM_STDBY_WAKEUP_TIME can be profiled by adding the duration of the following processes:

  • Standby Exit (Duration X):
    • Start the measurement on the rising edge of the DTB Radio IRQ
    • Stop the measurement on the falling edge of the Low power standby exit: GPIO_DEBUG_LOW_POWER_STANDBY_MODE_EXIT
  • Deep Sleep exit (Duration Y):
    • Start the measurement on the rising edge of the HASH IRQ Handler : GPIO_DEBUG_LLHWC_CMN_LW_ISR
    • Stop the measurement on the rising edge of handle next event : GPIO_DEBUG_SCHDLR_HNDL_NXT_TRACE
Standby timings profiling

6.1.3. How to set Standby timings

Once the value has been measured, add the duration of standby exit process and LL Deep Sleep exit process:

CFG_LPM_STDBY_WAKEUP_TIME = Standby exit duration + LL Deep Sleep exit duration

If using STM32CubeMX, set the value in us through STM32_WPAN interface in configuration panel. Else set the value directly in the code through CFG_LPM_STDBY_WAKEUP_TIME preprocessor directive in app_conf.h file.

With STM32CubeWBA v1.6.0 and v1.6.1

6.2. With STM32CubeWBA v1.6.0 and v1.6.1

6.2.1. GPIOs configuration to measure Standby timings

To profile Standby timings, the following GPIOs are needed:

  • RADIO_IRQ DTB – PA6: CN3 pin 30 on NUCLEO-WBA55CG and NUCLEO-WBA65RI

To enable the DTB signals, set the following preprocessor directive in app_conf.h file to 1. By default RADIO_IRQ and TX_ON are enabled as soon as DTBs are enabled:

#define CFG_RT_DEBUG_DTB                 (1)

Some debug GPIOs of the RT Debug module are also needed. To enable the module set the following preprocessor directive in app_conf.h file to 1:

#define CFG_RT_DEBUG_GPIO_MODULE                              (1)

Then, enable the following GPIOs and choose the GPIO you want to use in debug_config.h file:

#define USE_RT_DEBUG_LOW_POWER_STANDBY_MODE_EXIT              (1)
#define GPIO_DEBUG_LOW_POWER_STANDBY_MODE_EXIT                {GPIOA, GPIO_PIN_2}   // CN3 pin 32 on NUCLEO-WBA55CG

An additional GPIO needs to be added manually to measure the link layer deep sleep exit duration:

  • Choose the GPIO you want to use (here PB0 for example) and initialize it in MX_GPIO_Init function in main.c file as below:
GPIO_InitTypeDef GPIO_InitStruct = {0};
GPIO_InitStruct.Pin = GPIO_PIN_0;
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
GPIO_InitStruct.Pull = GPIO_NOPULL;
GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_HIGH;
HAL_GPIO_Init(GPIOB, &GPIO_InitStruct);
  • Surround the call to ll_intf_cmn_le_set_dp_slp_mode function with the GPIO in ll_sys_dp_slp_exit function in ll_sys_dp_slp.c file:
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_SET);
cmd_status = ll_intf_cmn_le_set_dp_slp_mode(DEEP_SLEEP_DISABLE);
HAL_GPIO_WritePin(GPIOB, GPIO_PIN_0, GPIO_PIN_RESET);
  • Add the following include in ll_sys_dp_slp.c file:
#include "stm32wbaxx_hal.h"

Additional GPIOs can be used for better understanding:

#define USE_RT_DEBUG_LLHWC_CMN_LW_ISR                        (1)
#define GPIO_DEBUG_LLHWC_CMN_LW_ISR                          {GPIOA, GPIO_PIN_12} // CN4 pin 17 on NUCLEO-WBA55CG
#define USE_RT_DEBUG_LLWCC_CMN_HG_ISR                        (1)
#define GPIO_DEBUG_LLWCC_CMN_HG_ISR                          {GPIOB, GPIO_PIN_2} // CN4 pin 3 on NUCLEO-WBA55CG
Radio activity event management in Standby mode

6.2.2. How to measure Standby timings

Once the GPIOs are configured, the CFG_LPM_STDBY_WAKEUP_TIME can be profiled by adding the duration of the following processes:

  • Standby Exit (Duration X):
    • Start the measurement on the rising edge of the DTB Radio IRQ
    • Stop the measurement on the falling edge of the Low power standby exit: GPIO_DEBUG_LOW_POWER_STANDBY_MODE_EXIT
  • Deep Sleep exit (Duration Y):
    • Start the measurement on the rising edge of LL_Deep_Sleep_exit
    • Stop the measurement on the falling edge of LL_Deep_Sleep_exit
Radio activity event management in Standby mode

6.2.3. How to set Standby timings

Once the value has been measured, add the duration of standby exit process and LL Deep Sleep exit process:

CFG_LPM_STDBY_WAKEUP_TIME = Standby exit duration + LL Deep Sleep exit duration

When using STM32CubeMX, set the value in us through STM32_WPAN interface in configuration panel. Else set the value directly in the code through CFG_LPM_STDBY_WAKEUP_TIME preprocessor directive in app_conf.h file.

With STM32CubeWBA v1.7.0 and newer version

6.3. With STM32CubeWBA v1.7.0 and newer version

Since STM32CubeWBA[1] v1.7.0, an embedded solution has been put in place to automatically profile the standby wakeup time, defined by the CFG_LPM_STDBY_WAKEUP_TIME value. This time correspond to the sum of standby exit duration and the link layer deep sleep exit duration.

At the first wakeup from standby, the default value set in CFG_LPM_STDBY_WAKEUP_TIME is used as standby wakeup time. During this first wakeup, the standby exit duration is profiled using the SysTick. Then, this value is computed by adding the link layer deep sleep exit duration considered as a fixed value (LL_DEEPSLEEP_EXIT_TIME_US) and the time needed by the device to exit from standby (DEVICE_WAKEUP_STANDBY_TIME_US). Once it is done, the computed value is set as standby wakeup time with APP_SYS_SetWakeupOffset API.

In some cases, especially when using a real-time operating system (RTOS), an extra time is added to consider the additional code in critical section added by the RTOS framework.

The default value set in CFG_LPM_STDBY_WAKEUP_TIME is used only for the first wakeup from standby. The profiling is done only once at the first wakeup from standby. At the end of the profiling, before the end of the low power critical section, the application SysTick configuration is restored, making the process transparent to the application.

These automatic profiling can be enabled or disabled through CFG_LPM_WAKEUP_TIME_PROFILING define in app_conf.h file. When the profiling is disabled, the default value set in CFG_LPM_STDBY_WAKEUP_TIME is used. In this case, the wakeup time can be manually profiled using GPIOs (refer to the With_STM32CubeWBA_v1-6-0_and_v1-6-1 section here).

7. References