STM32WB0 Bluetooth® LE – Serial communication

1. Serial communication profile

Serial communication applications are proprietary applications that are used to exchange the data entered on the UART of each board.

SerialCom applications are named BLE_SerialCom_Central and BLE_SerialCom_Peripheral: One board is configured as central using BLE_SerialCom_Central. The other board is configured as peripheral using BLE_SerialCom_Peripheral.

Both boards support data transfer by sending the L2CAP data through a COC (Connection Oriented Channel) link.

  • Open VT100 terminals on both client and server sides.
  • Connect to ST-LINK via the COM port @115200 bauds/s, 8 data bits, with no parity and 1 stop bit:
Bluetooth® Serial port configuration
STM32WBA SerialCom Profile


  • Configure the terminal with local echo settings:
Bluetooth® Terminal setup
STM32WB0 SerialCom Profile


  • No service is added.​

Once a Bluetooth® LE link is established, a Connection Oriented Channel (COC) is established.
Data are exchanged through the COC link.

The SerialCom projects provided within the STM32CubeWB0 MCU Package[1] are described below:

Bluetooth® LE SerialCom projects & STM32WB0
STM32WB0 SerialCom Profile


Bluetooth® LE SerialCom peripheral project & smartphone
STM32WB0 SerialCom Profile


Example of COC link establishment between a SerialCom central (STM32WB0 or Smartphone) & a SerialCom peripheral (STM32WB0)
STM32WB0 SerialCom Flow Diagram


Example of COC exchanged data between a SerialCom peripheral (STM32WB0) & a SerialCom central (STM32WB0 or smartphone)
STM32WB0 SerialCom Flow Diagram



1.1. On board buttons configuration

Button configuration for Bluetooth® Low Energy SerialCom applications on STM32WB0 nucleo boards
Application Condition B1 Click B1 Long Press B2 Click B2 Long Press B3 Click B3 Long Press
SerialCom peripheral Idle

Connected

/ Clear Sec db

Security request

/ / /
SerialCom central Idle

Connected

Starts scan then connects
/ Clear Sec db

Pairing request

/ -

Update connection interval

/

2. Requirements

2.1. Software and hardware requirements

For software and hardware requirements, refer to the STM32WB0 Build BLE project wiki page.

2.2. Collector applications compatibility

The STM32CubeWB0 SerialCom peripheral application is compatible with the following collector:

3. STM32WB0 SerialCom peripheral description

3.1. Project directory

The "BLE_SerialCom_Peripheral" application is available by downloading the STM32CubeWB0 MCU Package[1].

Refer to the How to Build a Bluetooth® LE project wiki page for information on the project directory.

3.2. Project description

3.2.1. Structure

The software project structure and its most important parts are described below.

SerialCom peripheral project structure
Connectivity WB0 SerialCom Periph structure.png
Connectivity yellow box.png
Main applicative part files
Connectivity green box.png
Bluetooth® Low Energy libraries
Connectivity pink box.png
Bluetooth® Low Energy stack modular configuration options and event dispatcher files

WARNING: Do not modify the files in the Middlewares folder


3.2.2. Application initialization

The application initialization steps are described below:

SerialCom peripheral project initialization
Connectivity WB0 SerialCom Periph init.png
puce1.png
Initialize the system (HAL, clocks, peripherals).
HAL_Init();
SystemClock_Config();
PeriphCommonClock_Config();
MX_GPIO_Init();
MX_RADIO_Init();
MX_RADIO_TIMER_Init();
MX_PKA_Init();
MX_APPE_Init();
puce2.png
Initialize the RNG, AES block, PKA:
HW_RNG_Init();
HW_AES_Init();
HW_PKA_Init();
APP_BLE_Init();
puce3.png
Initialize the Bluetooth® LE Host Stack
BLE_Init();
puce4.png
Initialize the Bluetooth® LE GATT level

Initialize the Bluetooth® LE GAP level

puce5.png
Initialize the Application/Profile context (services, characteristics)

Manage the COC link establishment

puce6.png
Initialize Rx UART to process data from the terminal

3.2.3. GAP and GATT initialization and interaction

The Bluetooth® LE SerialCom peripheral application initialization is done within app_ble.c:

  • Start 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 BLEEVT_Init() - ble_evt.c.
  • Manage the GAP event - BLEEVT_App_Notification().
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE provides information on the connection interval, the slave latency, and the supervision timeout.
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE provides information on the new connection.
    • HCI_DISCONNECTION_COMPLETE_EVT_CODE communicates with the application in the event of a link disconnection, and provides details about the reason.

The Event management is done by the Bluetoooth® Low Energy Event Dispatcher, ble_evt.c:

  • Initialize the number of the registered handler - BLEEVT_Init()
  • Manage events - BLE_STACK_Event()- from the Bluetooth® Low Energy Host Stack, and redirect them to the gap event handler - BLEEVT_App_Notification()

The application level of the SerialCom peripheral is done using coc_peripheral_app.c:

  • Initialize the application context.
  • Manage the COC link establishment.
  • Initialize Rx UART to process data from the terminal.


3.3. How to use the Bluetooth® Low Energy SerialCom Peripheral application

Once the BLE SerialCom Peripheral application is installed on the STM32WB0 platform, use the BLE SerialCom Central application on STM32WB0 platform or on a smartphone.

  • Open a VT100 terminal on the central and peripheral sides via the ST-Link Com port, @115200 bauds, 8 data bits, no parity and 1 stop bit:
Bluetooth® Serial Port Peripheral write data
STM32WB0 SerialCom Profile


Bluetooth® Serial Port Central receive data
STM32WB0 SerialCom Profile


  • Press B1 on the central to scan, connect and establish the COC link. When the link is ready, the CONNECTION READY message is displayed.
  • Enter data on UART, on the peripheral or central side. Check that the peer device receives data correctly.

Pressing B2 while not connected allows to clear the security database. Pressing B2 while connected allows to send peripheral security request.

There are two different modes to send messages from an STM32WB0: Send button or Real time.

On the terminal, when you type data using a keyboard (on either central or peripheral): when LF or CR is encountered, or data buffer is full (20 bytes), data is sent to the peer in L2CAP SDUs (Service Data Units).

3.4. UART debug trace

The debug log via UART interface ensures the application project is traced.

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         (1)
SerialCom Peripheral - Initialization phase Connected phase
==>> Start BLE_Init function  
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_srv_profile_init command
  Success: aci_gap_init command
  Static Random Bluetooth Address: f1:7e:3e:db:2d:97
  Success: Gap_profile_set_dev_name - Device Name
  Success: Gap_profile_set_appearance - Appearance
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_security_requirements command
  Success: aci_gap_configure_filter_accept_and_resolving_list command
==>> End BLE_Init function
==>> aci_gap_set_advertising_configuration - Success
==>> aci_gap_set_advertising_data - Success
==>> Success: Start Fast Advertising
>>== hci_le_connection_complete_event - Connection handle: 0x0801
     - Connection established with @:ea:a4:97:ab:53:79
     - Connection Interval:   50.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
BLE_SVC_L2CAP_Conn_Update(), Successfully
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout:   10000 ms

4. STM32WB0 SerialCom central example description

4.1. Project directory

The "BLE_SerialCom_Central" application is available by downloading the STM32CubeWB0 MCU Package[1]

Refer to the How to Build a Bluetooth® LE project wiki page for project directory information.

4.2. Project description

4.2.1. Structure

The software project structure and its most important parts are described below:

SerialCom central project structure
Connectivity WB0 SerialCom Central structure.png
Connectivity yellow box.png
Main applicative part files
Connectivity green box.png
Bluetooth® Low Energy libraries
Connectivity pink box.png
Bluetooth® Low Energy stack modular configuration options and event dispatcher files

WARNING: Do not modify the files in Middlewares folder


4.2.2. Application initialization

The different steps of the application initialization are described below:

SerialCom central project initialization
Connectivity WB0 SerialCom central init.png
puce1.png
Initialize the system (HAL, clocks, peripherals).
HAL_Init();
SystemClock_Config();
PeriphCommonClock_Config();
MX_GPIO_Init();
MX_RADIO_Init();
MX_RADIO_TIMER_Init();
MX_PKA_Init();
MX_APPE_Init();
puce2.png
Initialize the RNG, AES block, PKA:
HW_RNG_Init();
HW_AES_Init();
HW_PKA_Init();
APP_BLE_Init();
puce3.png
Initialize the Bluetooth® LE Host Stack
BLE_Init();
puce4.png
Initialize the Bluetooth® LE GATT level

Initialize the Bluetooth® LE GAP level

P2P_SERVER_APP_Init();
puce5.png
  • Reset the number of registered handler
puce6.png
  • Initialize Gatt Client Application
  • Manage COC link establishment
  • Initialize Rx UART to process data from terminal

4.2.3. GAP and GATT initialization and interaction

The Bluetooth LE SerialCom central application initialization is done within app_ble.c

  • Start the Bluetooth® LE stack - initialize the device as peripheral - configure and start advertising: ADV parameters, local name, UUID - APP_BLE_init()
  • Call the services controller initialization BLEEVT_Init() - ble_evt.c.
  • Manage the GAP event - BLEEVT_App_Notification() - ble_evt.c
    • HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE - provides information of the connection interval, slave latency, supervision timeout
    • HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE - provides new information about the connection
    • HCI_DISCONNECTION_COMPLETE_EVT_CODE - informs the application about the link disconnection, and the reason

Event management is done by the Bluetoooth® Low Energy Event Dispatcher, ble_evt.c

  • Initialize the number of registered handler - BLEEVT_Init()
  • Manage events - BLE_STACK_Event() - from the Bluetooth® Low Energy Host Stack and redirect them to the gap event handler - BLEEVT_App_Notification()

The application level of the SerialCom central is done with gatt_client_app.c:

  • Initialize Gatt Client Application
  • Manage COC link establishment
  • Initialize Rx UART to process data from terminal

4.3. How to use the Bluetooth® Low Energy SerialCom central application

Once the BLE SerialCom Central application is installed on the STM32WB0 platform, use BLE SerialCom Peripheral application on other STM32WB0.

  • Open a VT100 terminal on central and peripheral sides via the ST-Link Com Port (Connection is displayed as ST Link), @115200 bauds, 8 data bits, no parity and 1 stop bit:
  • Press B1 on the central side to scan, connect and establish COC. Once the link is ready, the CONNECTION READY message is displayed.
  • Enter data on the central or the peripheral side, and check that data are correctly received on peer device.


In terminal, when you type data using keyboard (on either Central or Peripheral): when LF or CR is encountered or data buffer is full (20 bytes), data are sent to the peer in L2CAP SDUs (Service Data Units).

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

Pressing B3 while connected allows to send connection update.

4.4. UART debug trace

Traces are enabled within the project, in app_conf.h, as described below:

/**
 * Enable or Disable traces in application
 */
#define CFG_DEBUG_APP_TRACE         (1)
SerialCom Central - Initialization phase Press B1. The connected phase starts.
==>> Start BLE_Init function  
  Success: aci_hal_set_tx_power_level command
  Success: aci_gatt_srv_profile_init command
  Success: aci_gap_init command
  Static Random Bluetooth Address: ea:a4:97:ab:53:79
  Success: Gap_profile_set_dev_name - Device Name
  Success: Gap_profile_set_appearance - Appearance
  Success: aci_gap_set_io_capability command
  Success: aci_gap_set_security_requirements command
  Success: aci_gap_configure_filter_accept_and_resolving_list command
==>> End BLE_Init function
-- CENTRAL INITIALIZED

** START GENERAL DISCOVERY (SCAN) **
--- ST MANUFACTURER data BlueSTv2
  Serial Port Server, db addr 0xF1:7E:3E:DB:2D:97
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
-- GAP_GENERAL_DISCOVERY_PROC completed
Create connection to Serial Port Server
==>> aci_gap_set_scan_configuration - Success
==>> aci_gap_set_connection_configuration Success , result: 0x00
  wait for event HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE
HCI_LE_META_EVT: 0x14
>>== hci_le_connection_complete_event - Connection handle: 0x0801
     - Connection established with @:f1:7e:3e:db:2d:97
     - Connection Interval:   50.00 ms
     - Connection latency:    0
     - Supervision Timeout: 5000 ms
>>== ACI_GAP_PROC_COMPLETE_VSEVT_CODE
GATT services discovery
  Success: aci_l2cap_connection_parameter_update_resp command
  Success: aci_hal_set_radio_activity_mask command
  Fail   : aci_l2cap_connection_parameter_update_resp command
ACI_ATT_READ_BY_GROUP_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  1/2 short UUID=0x1801, handle [0x0001 - 0x000A], GENERIC_ATTRIBUTE_SERVICE_UUID found
  2/2 short UUID=0x1800, handle [0x000B - 0x000F], GAP_SERVICE_UUID found
HCI_VENDOR_EVT: 0x0C0A
HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
PROC_GATT_CTL_DISC_ALL_PRIMARY_SERVICES services discovered Successfully

DISCOVER_ALL_CHARS ConnHdl=0x0801 ALLServiceHandle[0x0001 - 0x000F]
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 3
    1/3 short UUID=0x2A05, Properties=0x0020, CharHandle [0x0002 - 0x0003], GATT SERVICE_CHANGED_CHARACTERISTIC_UUID charac found
    2/3 short UUID=0x2B29, Properties=0x000A, CharHandle [0x0005 - 0x0006]
    3/3 short UUID=0x2B2A, Properties=0x0002, CharHandle [0x0007 - 0x0008]
HCI_VENDOR_EVT: 0x0C06
>>== HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE
     - Connection Interval:   45.00 ms
     - Connection latency:    0
     - Supervision Timeout:   10000 ms
==>> Start Connection Oriented Channel
  Success: aci_l2cap_cos_connection_req command
ACI_ATT_READ_BY_TYPE_RESP_VSEVT_CODE - ConnHdl=0x0801
  ConnHdl=0x0801, number of value pair = 3
    1/3 short UUID=0x2B3A, Properties=0x0002, CharHandle [0x0009 - 0x000A]
    2/3 short UUID=0x2A00, Properties=0x0002, CharHandle [0x000C - 0x000D], GAP DEVICE_NAME charac found
    3/3 short UUID=0x2A01, Properties=0x0002, CharHandle [0x000E - 0x000F], GAP APPEARANCE charac found
HCI_VENDOR_EVT: 0x0C06
>>== ACI_L2CAP_COS_CONNECTION_RESP_VSEVT_CODE
Channel_Type: 0
Peer_MTU: 64
Peer_MPS: 64
Initial_Credits:1
Result: 0x00
CID_Count: 1
==>> Connection Oriented Channel established
  MTU exchanged size = 247
  Success: set data length command
HCI_VENDOR_EVT: 0x0C03
HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
All characteristics discovered Successfully

DISCOVER_ALL_CHAR_DESCS [0x0001 - 0x000F]
ACI_ATT_FIND_INFO_RESP_VSEVT_CODE - ConnHdl=0x0801
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0002
  UUID=0x2A05, handle=0x0003, found GATT SERVICE_CHANGED_CHARACTERISTIC_UUID
Descriptor UUID=0x2902, handle=0x0002-0x0003-0x0004, Service Changed found
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0005
  UUID=0x2B29, handle=0x0006
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0007
  UUID=0x2B2A, handle=0x0008
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x0009
  UUID=0x2B3A, handle=0x000A
PRIMARY_SERVICE_UUID=0x2800 handle=0x000B
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000C
  UUID=0x2A00, handle=0x000D, found GAP DEVICE_NAME_UUID
reset - UUID & handle - CHARACTERISTIC_UUID=0x2803 CharStartHandle=0x000E
  UUID=0x2A01, handle=0x000F, found GAP APPEARANCE_UUID
HCI_VENDOR_EVT: 0x0C04
HCI_VENDOR_EVT: 0x0C10
All characteristic descriptors discovered Successfully

HCI_VENDOR_EVT: 0x0C11
HCI_VENDOR_EVT: 0x0C10
 ServiceChangedCharDescHdl =0x0004
All notifications enabled Successfully

5. STM32WB0 SerialCom peripheral with ST BLE Toolbox smartphone app

Required: phone with Android 10 or newer.

ST BLE ToolBox is a user-friendly application which interacts with and debugs ST Bluetooth®LE devices.

ST BLE Toolbox screenshots about the COC feature
Connectivity Screenshots Toolbox COC.png

On the ST BLE Toolbox, when clicking on a device corresponding to an ST board with a firmware ID SerialCom Peripheral (0x87), a "COC" button appears (next to the "Connect" button).

After accepting the pairing with the device (which is mandatory), a special page for COC appears. This means the L2CAP socket has been created and you can now receive and send data via the L2CAP channel.

Messages sent by the board will immediately appear in the chat.

Two different modes can be used to send messages from the phone: Send button and Real time.

  • With the Send button mode, you can type your message in the text field at the bottom of the chat, then sending this message by clicking on the "Send" button.
  • With the Real time mode, each character that you write is immediately sent.

You can erase all messages by clicking on "Clear".

Data format:
When sending the message "hello", what is actually sent is 0x 05 00 68 65 6C 6C 6F.

  • 0x 05 00 is the length of the message in little endian (05 00 in little endian => 00 05 => 5 => number of letters of "hello").
  • 0x 68 65 6C 6C 6F is "hello" in ascii ("h" = 0x68, "e" = 0x65, "l" = 0x6C, "o" = 0x6F).

6. References