STM32WBA Bluetooth® LE Stack Integration

Revision as of 15:18, 30 May 2023 by Registered User (→‎Bluetooth® LE stack initialization)

1. Introduction

This wiki describes to an application developer how to integrate the STM32WBAxx Bluetooth® LE stack library.
It covers only the Bluetooth® LE stack library integration, and it does not include the Link Layer integration aspects.

Acronyms definitions
Acronym Definition
HCI Host Controller interface
ACI Application Controller Interface
N/A Not Applicable

2. Bluetooth® LE Stack Overview

2.1. Release contents

2.1.1. Folder structure

The Bluetooth® LE stack delivery contains 3 folders as following:

Bluetooth® LE library folder structure


  • “doc” folder contains the current document and “STM32WBA_BLE_Wireless_Interface.html” which describes the Bluetooth® LE Application Commands Interface (ACI) and the Host Commands Interface (HCI).
  • “include” folder contains the header files of the Bluetooth® LE stack library interface. These files contain some definitions of constants and types and the declarations of ACI and HCI functions. It also contains the declarations of some external modules’ functions used by the Bluetooth® LE stack (for more details see section 4.2)
  • “lib” folder contains the 4 Bluetooth® LE stack library variants described in the following section.

2.1.2. Library variants

The Bluetooth® LE stack library is delivered in 4 variants:

  • 2 variants containing the Bluetooth® LE host stack:
    • Full stack (stm32wba_ble_stack_full.a)
    • Basic stack (stm32wba_ble_stack_basic.a)
  • 2 variants without the Bluetooth® LE host stack (controller only):
    • Link Layer Only stack (stm32wba_ble_stack_llo.a)
    • Link Layer Only Basic stack (stm32wba_ble_stack_llobasic.a)

Here are the 4 Bluetooth® LE stack variants details:

  • Full stack (stm32wba_ble_stack_full.a) includes the ST LL Controller and the Host Stack, and contains all the legacy stack supported features plus the extended advertising, GATT caching, ACI HCI flow control, isochronous support for audio, L2CAP connection oriented channels.

Note: This Bluetooth® LE stack variant can be configured to run in controller only mode (host stack is bypassed). See the parameter “options” of the initialization structure BleStack_init_t parameters in section 3.1 to enable/disable the controller only mode.

  • Basic stack (stm32wba_ble_stack_basic.a) contains only basic supported features or Bluetooth® LE legacy features without extended advertising, neither GATT caching, nor ACI HCI flow control, nor isochronous support, nor L2CAP connection oriented channels. The Host Stack is included, and it supports all the basic GATT, GAP and L2CAP features.
  • Link Layer Only stack (stm32wba_ble_stack_llo.a) contains all the features supported by the full stack but doesn’t include the Host Stack (GATT, GAP and L2CAP features).
  • Link Layer Only Basic stack (stm32wba_ble_stack_llobasic.a) contains all the features supported by the basic stack but doesn’t include the Host Stack (GATT, GAP and L2CAP features).

For more information, please refer to “STM32WBA_BLE_Wireless_Interface.html” to know which ACI and HCI command/event is supported by each stack configuration.
The table below details the supported features for each Bluetooth® LE stack variants:

Bluetooth® LE stack variants
Configuration FULL = Host Stack + ST LL Controller (stm32wba_ble_stack_full) BASIC = Host Stack + ST LL Controller Basic Feature (stm32wba_ble_stack_basic) LL_ONLY = ST LL Controller Only (stm32wba_ble_stack_llo) LL_ONLY_BASIC = ST LL Controller Only Basic Feature (stm32wba_ble_stack_llobasic)
Link Layer library dependency LinkLayer_BLE_Full_lib LinkLayer_BLE_Basic_lib LinkLayer_BLE_Full_lib LinkLayer_BLE_Basic_lib
Advertising Certified Certified Certified Certified
Scanning Certified Certified Certified Certified
Slave Connection Certified Certified Certified Certified
Data length extension Certified Certified Certified Certified
Privacy Certified Certified Certified Certified
LE Encryption Certified Certified Certified Certified
Legacy Pairing, LE secure connections Certified Certified Certified Certified
Master Connection Certified Certified Certified Certified
2Mbit Certified Certified Certified Certified
Long Range Certified Certified Certified Certified
Channel Selection Algorithm #2 Certified Certified Certified Certified
Direct Test Mode Certified Certified Certified Certified
Config HCI only Certified Not Supported Certified Certified
Extended Advertising Certified Not Supported Certified Not Supported
Periodic Advertising Certified Not Supported Certified Not Supported
AOA/AOD Enabled Not Supported Enabled Not Supported
Periodic Sync Transfer Certified Not Supported Certified Not Supported
Connected Isochronous Enabled Not Supported Enabled Not Supported
Isochronous Broadcaster Enabled Not Supported Enabled Not Supported
Isochronous Synchronized Enabled Not Supported Enabled Not Supported
LE Power Control Enabled Not Supported Enabled Not Supported
GATT Client Certified Certified N/A N/A
Enhanced ATT Enabled Not Supported N/A N/A
LE L2CAP Connection Oriented channel Certified Not Supported N/A N/A
GATT Caching Enabled Not Supported N/A N/A
Number of Links 20 20 20 20

Note: The features “Certified” are the supported features that have been officially certified. The feature “Enabled” are the supported features that have not been certified yet.

2.2. Real-Time environment

The Bluetooth® LE stack library is independent from the real-time software environment as well as any real-time resources.
The functions of this library used during run time are:

  • The Bluetooth® LE stack commands (HCI, ACI...),
  • The Bluetooth® LE stack process,
  • The Bluetooth® LE stack callback functions called by the link layer or the platform software (PKA, Timer...).

All these functions must be called from the same level of execution, i.e., from the same task when using a real-time operating system or from the main loop in a bare metal environment.

2.3. Library dependencies

The Bluetooth® LE stack library depends on (i.e. it must be linked with the following libraries):

  • STM32WBAxx Link Layer libraries (c.f. table Bluetooth® LE stack variants in section 2.1.2 for Link Layer library configuration dependency).
  • Standard C library:
    • Only basic memory functions are used (memcpy, memset, memmove, memcmp)

2.4. Library compilation options

The BLE stack library was compiled with the following options in IAR® environment:

  • General options:
    • Byte order: Little-endian
    • FPU: VFPv5 single precision
  • C/C++ Compilation options
    • Extra compilation options (--aeabi && --guard_calls)
      • --aeabi: Used to generate AEABI-compliant object code
      • --guard_calls: Used mandatory for AEABI-compliant
    • Plain 'char' is unsigned.

As mentioned above, the BLE stack library is AEABI compliant; The advantage of adhering to AEABI is that the BLE stack library can be linked with any other AEABI-compliant module, even modules produced by tools provided by other vendors than IAR®.
For the enum type, the IAR® C/C++ compiler will use the smallest type required to hold enum constants, preferring signed rather than unsigned.

2.5. Application compilation options

These compilation options are compulsory:

  • Plain 'char' is unsigned
  • Short enum:
    • For IAR® compiler, this option is set by default
    • Keil µvision® environment with ARM® CC compiler, the following option shall be added: -fshort-enums

In Keil µvision® environment with ARM® CC compiler, the 2 following linker options shall be added:

  • --diag_suppress 6654
  • --diag_suppress 6775

In CubeIDE environment with GCC compiler, there is no need to add specific compiler options or linker options.

3. Bluetooth® LE stack user interface

In order to use the main functions of the Bluetooth® LE stack library, the user application code needs to include “blestack.h”.
In addition, to be able to directly call Bluetooth® LE commands (see section 3.3.2), the application needs to also include “blecore.h”.

3.1. Bluetooth® LE stack initialization

BleStack_Init function
Functions Parameters Return Value
BleStack_Init BleStack_init_t* init_params_p tBleStatus

BleStack_Init: The Bluetooth® LE stack initialization routine. This function is used to define the memory and configure the Bluetooth® LE stack.
All BleStack_Init parameters are described below:

BleStack_Init function's parameters
BleStack_init_t parameters Definition Value
uint16_t numAttrRecord Maximum number of attribute records related to all the required characteristics (excluding the services) that can be stored in the GATT database, for the specific Bluetooth® LE user application. Value = <number of user attributes> + 9
Min value = 9
Max value: depending on the GATT database defined by the user application.
uint16_t numAttrServ Defines the maximum number of services that can be stored in the GATT database. Note that the GAP and GATT services are automatically added at initialization so this parameter must be the number of user services increased by two. Value = <number of user services> + 2
Min value = 2
Max value: depending on the GATT database defined by the user application.
uint16_t attrValueArrSize Size of the storage area for the attribute values.
By default, two services are present and must be included, with dedicated characteristics:
  • Generic access service: service UUID 0x1800, with its three mandatory characteristics:
    • Device name UUID 0x2A00.
    • Appearance UUID 0x2A01.
    • Peripheral preferred connection parameters. UUID 0x2A04.
  • Generic attribute service. UUID 0x1801, with one optional characteristic:
    • Service changed UUID 0x2A05.

Each characteristic contributes to the attrValueArrSize value as follows:

  • Characteristic value length plus:
    • 5 bytes if characteristic UUID is 16 bits
    • 19 bytes if characteristic UUID is 128 bits
    • 2 bytes if characteristic has a server configuration descriptor
    • 2 bytes * NUM_OF_LINKs if the characteristic has a client configuration descriptor
    • 2 bytes if the characteristic has extended properties
Depends on the number of attributes used by the application.
uint8_t numOfLinks Maximum number of simultaneous connections that the device will support. Min value: 1

Max value: 20

uint8_t prWriteListSize Maximum number of supported “prepare ATT write request”. Value defined by the macro:

DIVC (max_char_size, default_att_mtu - 5) * 2
max_char_size: Maximum characteristic’s value size
BLE_DEFAULT_ATT_MTU = 23

uint8_t mblockCount Number of allocated memory blocks for the Bluetooth® LE stack. Value defined by the macro:

BLE_MBLOCKS_CALC (PREP_WRITE_LIST_SIZE, MAX_ATT_MTU, NUM_LINKS) + MBLOCK_COUNT_MARGIN)
With:

  • PREP_WRITE_LIST_SIZE defined by the parameter prWriteListSize.
  • MAX_ATT_MTU defined by the attMtu parameter.
  • NUM_LINKS defined by the numOfLinks parameter.
  • MBLOCK_COUNT_MARGIN the memory margin to take.
uint16_t attMtu Maximum ATT MTU size supported. Min value: 23

Max value: 512

uint16_t max_coc_mps Used in APIs in l2cap_coc.c to process with a L2CAP connection, send/receive data…
Maximum value of the connection-oriented channel Maximum Payload Size.
Min value: 23

Max value: 248

uint8_t max_coc_nbr Used in APIs in l2cap_coc.c to process with a L2CAP connection, send/receive data…
Maximum number of connection-oriented channels.
Min value: 0

Max value: 64

uint8_t max_coc_initiator_nbr Used in APIs in l2cap_coc.c to process with a L2CAP connection, send/receive data…
Maximum number of connection-oriented channels in initiator mode.
Min value:0

Max value: max_coc_nbr

uint8_t* bleStartRamAddress Start address of the RAM buffer allocated for Bluetooth® LE stack library.
It must be a 32bit aligned RAM area.
uint32_t total_buffer_size Size of the RAM buffer allocated for Bluetooth® LE stack library. Value defined by the macro:

BLE_TOTAL_BUFFER_SIZE (NUM_LINKS, MBLOCK_COUNT)
With:

  • NUM_LINKS: the maximum number of links defined by the parameter numOfLinks.
  • MBLOCK_COUNT: the number of allocated memory blocks, defined by the parameter mblockCount.
uint8_t* bleStartRamAddress_GATT Start address of the RAM buffer allocated for GATT database.
It must be a 32bit aligned RAM area.
uint32_t total_buffer_size_GATT Size of the RAM buffer allocated for GATT database. Value defined by the macro:

BLE_TOTAL_BUFFER_SIZE_GATT (NUM_ATTR_RECORD, NUM_ATTR_SERV, ATTR_VALUE_ARR_SIZE)
With:

  • NUM_ATTR_RECORD: defined by the parameter numAttrRecord.
  • NUM_ATTR_SERV: defined by the parameter numAttrServ.
  • ATTR_VALUE_ARR_SIZE: defined by the parameter attrValueArrSize.
uint16_t options Options flags. Bitmap of the "BLE_OPTIONS_..." definitions:
  • bit 0:   1: LL only  0: LL + host
  • bit 1:   1: no service change desc.  0: with service change desc.
  • bit 2:   1: device name Read-Only  0: device name R/W
  • bit 3:   1: extended adv supported  0: extended adv not supported
  • bit 5:   1: Reduced GATT db in NVM  0: Full GATT db in NVM
  • bit 6:   1: GATT caching is used  0: GATT caching is not used
  • bit 7:   1: LE Power Class 1  0: LE Power Class 2-3
  • bit 8:   1: appearance Writable  0: appearance Read-Only
  • bit 9:   1: Enhanced ATT supported  0: Enhanced ATT not supported
  • other bits: reserved
BLE_OPTIONS_LL_ONLY = 0x0001U,
BLE_OPTIONS_NO_SVC_CHANGE_DESC = 0x0002U,
BLE_OPTIONS_DEV_NAME_READ_ONLY = 0x0004U,
BLE_OPTIONS_EXTENDED_ADV = 0x0008U,
BLE_OPTIONS_REDUCED_DB_IN_NVM = 0x0020U,
BLE_OPTIONS_GATT_CACHING = 0x0040U,
BLE_OPTIONS_POWER_CLASS_1 = 0x0080U,
BLE_OPTIONS_APPEARANCE_WRITABLE = 0x0100U,
BLE_OPTIONS_ENHANCED_ATT = 0x0200U
uint32_t debug Debug flags BLE_DEBUG_RAND_ADDR_INIT = 0x00000010UL

3.2. Bluetooth® LE stack process

3.3. Bluetooth® LE stack modes

3.3.1. Transparent Mode

3.3.2. Direct mode for commands and data sending

3.3.3. Bluetooth® LE Asynchronous ACI/HCI events and data reception

4. Bluetooth® LE Stack Porting

4.1. Stack Porting

4.2. Bluetooth® LE platform functions

4.2.1. NVM functions

4.2.2. Timer functions

4.2.3. AES functions

4.2.4. PKA functions

4.2.5. RNG functions

5. References



6. Wiki syntax examples

Header text Header text Header text
Example Example Example
Example Example Example
Example Example Example
Bluetooth® LE Health Thermometer Service specification
Service Characteristic Property UUID size +1
Health Thermometer Service 0x1809 +1
Temperature Measurement Indicate 0x2A1C 13
Temperature Type Read 0x2A1D 1
Intermediate Temperature Notify 0x2A1E 13
Measurement Interval Read,Write,Indicate 0x2A21 2
Device Information Service 0x180A
Manufacturer Name String Read 0x2A29 32
Model Number String Read 0x2A24 32
System ID Read 0x2A23 8
ST Bluetooth® LE example project directory
WBA BLE application folder.png



ST Bluetooth® LE example project directory
WBA BLE application folder.png


Bluetooth®

texte - “doc” folder contains the current document and “STM32WBA_BLE_Wireless_Interface.html” which describes the BLE Application Commands Interface (ACI) and the Host Commands Interface (HCI).

- “include” folder contains the header files of the BLE stack library interface. These files contain some definitions of constants and types and the declarations of ACI and HCI functions. It also contains the declarations of some external modules’ functions used by the BLE stack (for more details see section 4.2)

- “lib” folder contains the 4 BLE stack library variants described in the following section.

BLA BLA BLA  

ST Bluetooth® LE pour Nidhal
WBA BLE application folder.png
/**
******************************************************************************
* @file    STM32WB_P2P_Server.ino
* @author  WBL BLE Application Team
* @brief   Arduino STM32WB P2P Server Application (Custom STM)
******************************************************************************