STM32WB Bluetooth® LE – AT Command Server Project

Under construction.png Coming soon

1. STM32WB - Bluetooth® Low Energy (BLE) AT Server Overview

This page describes the set of AT commands to control the STM32WB Series. AT commands are instructions used to control a modem. AT is the abbreviation of ATtention. For this concept, the Cortex®-M4 application microcontroller contains an AT project able to manage the concepts of GAP, GATT, Security and low-power. This document explains how to interface with the STM32WB Series microcontroller to manage multi Bluetooth® Low Energy applications handling (such as AT P2P server, AT Heart Rate) and GAP/GATT custom applications using AT instructions.

The BLE_AT_Server application is available by downloading STM32CubeWB[1] release

This project implements a set of AT commands used to build GAP/GATT custom server or client, peripheral or central application. It also implements P2P Server or Heart Rate applications that could be directly selected. The objective is to have an intermediate abstraction layer in order to allow a user to develop a basic application with a limited sets of commands to send.

The following sections contain the interface description, the AT commands definition, and the description of some use cases.


1.1. AT commands

The AT instruction set is a standard developed by Hayes to control modems. The command set consists in a series of short text strings for performing operations:

  • at GAP level such as advertising, discovery, connection, security.
  • at GATT level such as services and characteristics management and discovery, and operation on characteristics.

The AT instructions are transfered through Cortex®-M4 application microcontroller low-power UART (LPUART) without flow control.
The STM32WB Series microcontroller can be controlled either through a terminal emulator such as Tera Term or Termite, or through an embedded microcontroller AT client module.

AT instruction architecture
Connectivity Setup.png


On P-NUCLEO-WB55 board use PA2 (CN 10 pin 35) for LPUART1 TX and PA3 (CN10 pin 37) for LPUART1 RX:

P-NUCLEO-WB55 LPUART1
Connectivity STM32WB55 LPUART1.png


The terminal emulator must be configured with the following parameters:

• Baud rate: 9600 
• Data: 8 bits 
• Parity: none 
• Stop: 1 bit 
• Flow control: none
• Each frame is delimited by a \n character
Terminal emulator configuration
Connectivity settings.png


AT command is composed of SET and QUERY operations. A single operation can be executed at a time. The format of these operations is defined as follow :

  • SET operation:
    • AT+CMD=value
  • Response for SET operation :
    • OK or ERROR
  • QUERY operation:
    • AT+CMD?
  • Response for QUERY operation:
    • +CMD:value
    • OK

Event can be sent asynchronously by the server with following format:

  • <EVT=value


1.2. General commands

Initialization commands:

GAP Command Description Comments
BLE_RST Sets the default values
BLE_PUB_ADDR Initializes public address If needed, to be done before BLE_INIT
BLE_CFG_RAND_ADDR Initializes default random address If needed, to be done before BLE_INIT
BLE_RAND_ADDR Initializes random address If needed, to be done before BLE_INIT
BLE_NAME Sets the name of the device If needed, to be done before BLE_INIT
BLE_RF_POWER Sets the TX power level of the device Entered in dBm. See available values
BLE_INIT Initializes public and random addresses, TX power level, name of the device: default values are used if not defined with previous AT commands. Sets default init values for the security Default security values can be modified with BLE_IO_CAPA and BLE_SET_AUTHEN_REQ
BLE_IO_CAPA Sets IO capability Can be done before or after BLE_INIT
BLE_SET_AUTHEN_REQ Sets authentication requirement of the device Can be done before or after BLE_INIT
BLE_SVC Initializes a predefined service (P2P server or Heart Rate) BLE_SVC=1 creates service and characteristics for P2P_server service and starts advertising. BLE_SVC=2 creates service and characteristics for Heart_Rate service and starts advertising.


GAP commands:

GAP Command Description Comments
BLE_ADV_PARAM Sets advertising parameters
BLE_ADV Starts, stops advertising If no new defined advertising parameter, use of default parameters
BLE_CONN_INT Updates the connection interval To send after link establishment if needed
BLE_CLEAR_DB Clears the security database
BLE_PERIPH_SEC_REQ Sends a peripheral security request to the central To send after link establishment if needed
BLE_PAIRING_CONFIRM Allows the user to confirm or the numeric comparison value showed through BLE_EVT_VALUE_CONFIRM Used to answer BLE_EVT_VALUE_CONFIRM event
BLE_PASSKEY_RESP Sends the passkey that will be used during the pairing process, in response to BLE_EVT_PASSKEY_REQ Used to answer BLE_EVT_PASSKEY_REQ event


GATT commands:

GAP Command Description Comments
BLE_SVC Initialize a service BLE_SVC=3 to 5 creates a custom service
BLE_CHAR_ADD Add a characteristic Applicable to a custom service
BLE_NOTIF_VAL Sets the characteristic value to be notified value sent from the server to the client
BLE_INDIC_VAL Sets the characteristic value to be indicated value sent from the server to the client


GAP events:

GAP Command Description Comments
BLE_EVT_CONN After connection/disconnection
BLE_EVT_UPD_CONN After connection update
BLE_EVT_PASSKEY_REQ After pairing request (legacy pairing) Needs to be answered with BLE_PASSKEY_RESP
BLE_EVT_VALUE_CONFIRM After pairing request (Secure Connection) Needs to be answered with BLE_PAIRING_CONFIRM
BLE_EVT_PAIRING AT the end of pairing process Returns status and reason parameters


GATT events:

GAP Command Description Comments
BLE_EVT_WRITE After characteristic write Sent when a client writes a characteristic


2. Initialization commands

2.1. Initialization - reset - BLE_RST

SET operation: <1>

AT+BLE_RST=1
OK

QUERY operation: No query for this command, it will return an error.

2.2. Initialization - public address - BLE_PUB_ADDR

Public address is 6 bytes address.
SET operation: <address>

AT+BLE_PUB_ADDR=0x112233445566
OK

QUERY operation: <public address>

AT+BLE_PUB_ADDR?
+ BLE_PUB_ADDR: 0x112233445566
OK

2.3. Initialization – use of default random address – BLE_CFG_RAND_ADDR

SET operation: <1>

AT+BLE_CFG_RAND_ADDR=1
OK

QUERY operation: <config random address>

AT+BLE_CFG_RAND_ADDR?
+BLE_CFG_RAND_ADDR=1
OK

2.4. Initialization – random address – BLE_RAND_ADDR

Random address is 6 bytes address.
SET operation: <address>

AT+BLE_RAND_ADDR=0xFF11223344CC
OK

QUERY operation: <random address>

AT+BLE_RAND_ADDR?
+ BLE_RAND_ADDR: 0xFF11223344CC
OK

2.5. Initialization – name – BLE_NAME

Device name is a string of characters (19 characters max)
SET operation: <name>

AT+BLE_NAME=WB_ATCMD
OK

QUERY operation: <name>

AT+BLE_NAME?
+BLE_NAME=WB_ATCMD
OK

2.6. Initialization – Tx power – BLE_RF_POWER

Tx power value is between -40 dBm to 6 dBm (IC level). List of supported values in dBm:

-40	
-20,85	
-19,75	
-18,85	
-17,6	
-16,5	
-15,25	
-14,1	
-13,15	
-12,05	
-10,9
-9,9	
-8,85	
-7,8	
-6,9	
-5,9	
-4,95	
-4	
-3,15	
-2,45	
-1,8	
-1,3
-0,85	
-0,5	
-0,15	
0	
1	
2	
3	
4	
5	
6	

SET operation: <tx power>

AT+BLE_RF_POWER=1
OK

QUERY operation: <tx power>

AT+BLE_RF_POWER?
+ BLE_RF_POWER:1
OK

2.7. Initialization – BLE_INIT

BLE_INIT to be used in case of custom application. If AT+BLE_INIT=1 is the first sent command, initialization of the following parameters is done:
Public address, random address, device name, RF Tx power, security parameters.
The default values applied in this case are:

  • Public address: based on the company ID, the device ID and the Unique Device Number (UDN)
    • bit[47:24] : 24bits (OUI) equal to the company ID
    • bit[23:16] : Device ID.
    • bit[15:0] : The last 16bits from the UDN
  • Random address: based on the UDN and a defined value (0x0000ED6E) to fit the requirements of a random address.
  • Device name: AT SERVER
  • RF Tx power: -0.15 dBm
  • IO Capability: IO_CAP_KEYBOARD_DISPLAY (4)
  • Authentication requirements: no bonding mode (0), MITM (1), SC support supported but optional (1), no use of fixed pin (1), fixed pin (111111), public address (0)

If the public address, the random address, the name or the tx output power needs to be changed, send associated AT commands, BLE_PUB_ADDR, BLE_RAND_ADDR, BLE_NAME, BLE_RF_POWER before BLE_INIT.

If IO capability, authentication requirements parameters need to be changed, send BLE_IO_CAPA, BLE_SET_AUTHEN_REQ before or after BLE_INIT out of any link establishment.

SET operation: <1>
Use default BD address, default name, default Tx Power if no new parameter has been defined. Default initialization in case security (pairing) is started after link establishment. These values could be changed when no link is established with BLE_IO_CAPA and BLE_SET_AUTHEN_REQ (commands described later).If BLE_PUB_ADDR, BLE_RAND_ADDR, BLE_NAME or BLE_RF_POWER have been sent previously, the new parameters are taken into account.

AT+BLE_INIT=1
OK

QUERY operation: <public_addr>, <name>, <tx power>

AT+BLE_INIT?
+BLE_INIT:0x0080e12600c2,AT server,-0.15
OK

2.8. Initialization – IO capability - BLE_IO_CAPA

IO capability values :

  • 0: IO_CAP_DISPLAY_ONLY
  • 1: IO_CAP_DISPLAY_YES_NO
  • 2: IO_CAP_KEYBOARD_ONLY
  • 3: IO_CAP_NO_INPUT_NO_OUTPUT
  • 4: IO_CAP_KEYBOARD_DISPLAY


SET operation: <io_capability>

AT+BLE_IO_CAPA=1
OK

QUERY operation: <io_capability>

AT+BLE_IO_CAPA?
+BLE_IO_CAPA:1
OK

2.9. Initialization – authentication requirement - BLE_SET_AUTHEN_REQ

Sets authentification requirement of the device :

bonding_mode:
  • 0: no bonding mode’
  • 1: bonding mode
mitm:
  • 0: no MITM
  • 1: MITM
sc_support:
  • 0: SC pairing not supported
  • 1: SC pairing supported but optional
  • 2: SC pairing mandatory
use_fixed_pin:
  • 0: use a fixed pin
  • 1: do not use a fixed pin
fixed_pin:
  • 0 to 999999

SET operation: <bonding_mode>, <mitm>, <sc_support>, <use_fixed_pin>, <fixed_pin>

AT+BLE_SET_AUTHEN_REQ=0,0,1,1,0000
OK

QUERY operation: <bonding_mode>, <mitm>, <sc_support>, <use_fixed_pin>, <fixed_pin>,<identity_address_type>

AT+BLE_SET_AUTHEN_REQ?
+BLE_SET_AUTHEN_REQ:0,0,1,1,0x0,0
OK

2.10. Initialization commands – P2P_server or Heart rate service creation – BLE_SVC

If BLE_SVC=1 or BLE_SVC=2 is sent, no need to send BLE_INIT.
BLE_SVC=1 creates service and characteristics for P2P_server service and starts advertising.
BLE_SVC=2 creates service and characteristics for Heart_Rate service and starts advertising.
Use a phone with ST BLE Sensor application (Available on Play store for Android [2] and Apple store for IOS [3]) to connect to P2P server or Heart rate application.
Services available :

  • 0: no service
  • 1: P2P service
  • 2: Heart Rate


SET operation: <svc_id>

  • Creates P2P_server service and starts advertising
AT+BLE_SVC=1
OK
  • Creates Heart Rate service and starts advertising
AT+BLE_SVC=2
OK

QUERY operation: <svc_id> Returns the active service.

AT+BLE_SVC? 
+BLE_SVC:2
 OK

3. GAP commands

3.1. GAP command - Advertising set parameters – BLE_ADV_PARAM

Sets advertising parameters of the device :

entry_nb:
  • 1: 0,20ms,30ms,0
  • 2: 0,180ms,200ms,0
  • 3: 0,1s,2.5s,0
  • 0, <adv_type>, <adv_int_min>, <adv_int_max>
adv_type:
  • 0: connectable undirected advertising
  • 1: connectable directed advertising
  • 2: scannable undirected advertising
  • 3: not connectable undirected advertising
adv_int_min:
  • From 20 ms to 10240 ms (must be multiple of 0.625 ms)
adv_int_max:
  • From 20 ms to 10240 ms (must be multiple of 0.625 ms)

SET operation: <entry_nb>

AT+BLE_ADV_PARAM=2
OK
AT+BLE_ADV_PARAM=0,0,32,48
OK

QUERY operation: <entry_nb> <adv_type>, <adv_int_min>, <adv_int_max>, <own_addr_type>

AT+BLE_ADV_PARAM?
+BLE_ADV_PARAM:2,0,180.0,200.0,0
OK
AT+BLE_ADV_PARAM?
+BLE_ADV_PARAM:0,0,32.0,48.0,0
OK

3.2. GAP command - Start/stop advertising – BLE_ADV

Start/stop advertising:

  • 0: stop advertising
  • 1: start advertising

If no param entered, use default values (config 1)
If predefined config, use (1,2 or 3)
If param entered, use the new parameters

SET operation: <enable>

AT+BLE_ADV=1
OK

QUERY operation: <enable>

AT+BLE_ADV?
+BLE_ADV:1
OK

3.3. GAP command - Connection update – BLE_CONN_INT

Connection interval values :

*conn_int_min:  From 7,5 ms to 4000 ms (must be multiple of 1.25 ms)
*conn_int_max: From 7,5 ms to 4000 ms (must be multiple of 1.25 ms) 

SET operation: <conn_int_min>,<conn_int_max>

AT+BLE_CONN_INT=125,150 
OK

QUERY operation: <conn_int_min>,<conn_int_max>

AT+BLE_CONN_INT?	 
+BLE_CONN_INT:125.00,150.00
OK

3.4. GAP command - Clear the security database – BLE_CLEAR_DB

SET operation: <1>

AT+BLE_CLEAR_DB = 1
OK	

QUERY operation: No query for this command, it will return an error.

3.5. GAP command - Sends a peripheral security request to the central – BLE_PERIPH_SEC_REQ

SET operation: <1>

AT+BLE_PERIPH_SEC_REQ = 1
OK

QUERY operation: No query for this command, it will return an error.

3.6. GAP command - Confirms or not the numeric comparison value in Secure Connection - BLE_PAIRING_CONFIRM

Confirms or not the numeric comparison value in Secure Connection:

  • 0: To not confirm the numeric comparison value
  • 1: To confirm the numeric comparison value

SET operation: <confirm_yes_no>

AT+BLE_PAIRING_CONFIRM=1
OK

QUERY operation: No query for this command, it will return an error.

On reception of BLE_EVT_VALUE_CONFIRM, send AT+BLE_PAIRING_CONFIRM=<confirm_yes_no>

TEST TEST

3.7. GAP command - Sends the passkey that will be used during the pairing process in Legacy pairing - BLE_PASSKEY_RESP

Passkey value is between 0 and 999999.
SET operation: <passkey>

AT+BLE_PASSKEY_RESP=0000
OK

QUERY operation: No query for this command, it will return an error.

On reception of BLE_EVT_PASSKEY_REQ, send AT+BLE_PASSKEY_RESP=<passkey>

When the pairing is complete, reception of BLE_EVT_PAIRING With 2 parameters: status, reason.

Status:
  • 0: pairing status success
  • 1: pairing status timeout
  • 2: pairing failed
  • 3: encryption failed
Reason:
  • 1: passkey entry failed
  • 2: OOB not available
  • 3: authentication request cannot be met
  • 4: confirm value failed
  • 5: pairing not supported
  • 6: insufficient encryption key size
  • 7: command not supported
  • 8: unspecified reason
  • 9: very early next attempt
  • 10: invalid parameters
  • 11: SC DHKEY check failed
  • 12: SC numeric comparison failed


4. GATT commands

4.1. GATT command - Custom service creation - BLE_SVC

For the creation of a custom service, the command BLE_INIT is mandatory. Use a phone with ST BLE Toolbox to connect with WB55 and a custom service. Available on Play store for Android and Apple store for IOS.

SET: <svc_id>, <svc_uuid_type>, <uuid_16b>, <max_attr_record>

svc_id:
  • 0: no service
  • 3 to 5: custom service
svc_uuid_type:
  • 1: 16 bits
  • 2: 128 bits
uuid_16b: 
  • 16 bits service uuid
max_attr_record: 
  • 2 * nb_of_char + 1

QUERY: <svc_id>

If svc_uuid_type = 1, a range of UUID values has been pre-allocated, it is the Bluetooth Base UUID and has the value 00000000-0000-1000-8000- 00805F9B34FB.   If svc_uuid_type = 2, the user enters 16 bits service uuid. The 128 bits service uuid is completed in BLE_AT_SERVER application with the following value 00000000-CC7A-482A-984A-7F2ED5B3E58F.


Example1:
  • AT+BLE_INIT=1
  • OK
  • AT+BLE_SVC=3,1,0xAABB,10
  • AT+BLE_ADV=1
  • OK
Connectivity Custom service1.png
Example2:
  • AT+BLE_INIT=1
  • OK
  • AT+BLE_SVC=3,2,0xAABB,10
  • AT+BLE_ADV=1
  • OK
Connectivity Custom service2.png
AT_SERVER: default complete local name
00:80:E1:26:F6:5B: default public BD address

Up to three custom services could be defined.

4.2. GATT command - Characteristic creation - BLE_CHAR_ADD

SET: <svc_id>, <char_id>, <char_uuid_type>, <char_uuid_16b>, <char_value_len>, <char_prop>, <sec_permission>, <gatt_evt_mask>

svc_id: 
  • 3: service id on which characteristic is added
char_id: 
  • 1 to 5: characteristic id
char_uuid_type:
  • 1: 16 bits
  • 2: 128 bits

char_uuid_16b:

  • 16 bits char uuid
char_value_len:
  • 1 to 245
char_prop:
  • 0x00: CHAR_PROP_NONE
  • 0x01: CHAR_PROP_BROADCAST
  • 0x02: CHAR_PROP_READ
  • 0x04: CHAR_PROP_WRITE_WITHOUT_RESP
  • 0x08: CHAR_PROP_WRITE
  • 0x10: CHAR_PROP_NOTIFY
  • 0x20: CHAR_PROP_INDICATE
  • 0x40: CHAR_PROP_SIGNED_WRITE
  • 0x80: CHAR_PROP_EXT
sec_permission:
  • 0x00: None
  • 0x01: AUTHEN_READ
  • 0x02: AUTHOR_READ
  • 0x04: ENCRY_READ
  • 0x08: AUTHEN_WRITE
  • 0x10: AUTHOR_WRITE
  • 0x20: ENCRY_WRITE
gatt_evt_mask:
  • 0x00: GATT_DONT_NOTIFY_EVENTS
  • 0x01: GATT_NOTIFY_ATTRIBUTE_WRITE
  • 0x02: GATT_NOTIFY_WRITE_REQ_AND_WAIT_FOR_APPL_RESP
  • 0x04: GATT_NOTIFY_READ_REQ_AND_WAIT_FOR_APPL_RESP

QUERY: No query for this command, it will return an error.

If char_uuid_type = 1, a range of UUID values has been pre-allocated, it is the Bluetooth Base UUID and has the value 00000000-0000-1000-8000- 00805F9B34FB

If char_uuid_type = 2, the user enters 16 bits characteristic uuid. The 128 bits service uuid is completed in BLE_AT_SERVER application with the following value 00000000-8E22-4541-9D4C-21EDAE82ED19.

Example 1:
  • AT+BLE_CHAR_ADD=3,1,1,0x3344,2,26,0,1
  • OK
Connectivity Custom char1.png
Example 2:
  • AT+BLE_CHAR_ADD=3,1,2,0x3344,2,10,0,1
  • OK
Connectivity Custom char2.png

4.3. GATT command - Setting of the characteristic value to notify - BLE_NOTIF_VAL

SET: <svc_id>, <char_id>, <char_val>

svc_id: 
  • 1 to 5: service id corresponding to the characteristic to be notified
char_id: 
  • 1 to 5: characteristic id to be notified
char_val:
  • value to be notified

QUERY: No query for this command, it will return an error.

Example:
  • AT+BLE_NOTIF_VAL=2,1,70 – sets HeartRate characteristic value to 70

By default, Energy expended value is set to 0, it is not displayed on phone.

  • AT+BLE_NOTIF_VAL=1,2,1 – sets Button characteristic status, 0 and 1 are displayed alternatively (char_val = 0 has no effect)

4.4. GATT command - Setting of the characteristic value to indicate - BLE_INDIC_VAL

SET: <svc_id>, <char_id>, <char_val>

svc_id: 
  • 1 to 5: service id corresponding to the characteristic to be notified
char_id: 
  • 1 to 5: characteristic id to be notified
char_val:
  • value to be notified

QUERY: No query for this command, it will return an error.

Example:
  • AT+BLE_INDIC_VAL=3,1,10

5. Events generation

An event is an information received either from the remote device either from the local device to inform the application about status change or data received.

Connectivity Events generation.png

6. Security

The BLE security model includes 5 security features:

Pairing: process for creating one or more shared secret keys. Bonding: act of storing the keys created during pairing for use in subsequent connections in order to form a trusted device pair. Device authentication: verification that the two devices have the same keys. Encryption: provides message confidentiality. Message integrity: protects against fake messages.(4 bytes Message Integrity Check MIC)

BLE uses 2 security levels:

  • Legacy Pairing – Short Temporary Key (STK). STK will be created to encrypt connection. Then, if bonding, LTK will be used for subsequent connections.
  • Secure Connection – Long Term Key (LTK). LTK will be created to encrypt connection.

6.1. Legacy pairing and no fixed pin (initiated by the central)

Default initialization values can be changed when no link is established with: BLE_IO_CAPA=4 BLE_SET_AUTHEN_REQ=0,1,0,1,111111 (fixed pin is not used)

Connectivity LP nofixedpin1.png
Connectivity LP nofixedpin2.png

6.2. Legacy pairing and fixed pin (111111) (initiated by the central)

Default initialization values can be changed when no link is established with:

BLE_IO_CAPA=0
BLE_SET_AUTHEN_REQ=0,0,0,0,111111 (fixed pin = 111111 is used)
Connectivity LP fixedpin1.png
Connectivity LP fixedpin2.png

6.3. Secure connection (initiated by the central)

Use of default values when BLE_INIT is sent:

io_capability = 4
bonding_mode = 0
mitm = 1
sc_support = 1
use_fixed_pin = 1
fixed_pin = 111111
identity_address_type = 0
Connectivity SecureConnection1.png
Connectivity SecureConnection2.png

7. Flow sequences for examples


7.1. BLE_SVC=1 P2P_server

Connectivity P2P server.png

7.2. BLE_SVC=2 Heart Rate

Connectivity HeartRate.png

7.3. BLE_SVC=3 Custom service

Add a characteristic with properties equal to write, read, notify
Add a characteristic with properties equal to write, read, indicate
Connectivity newcustom.png
Write characteristics, enable notification and indication
Connectivity writechar1.png
Connectivity writechar2.png
Initialization sequence with specific parameters, change of connection interval
Connectivity conninterval1.png
Connectivity conninterval2.png
Initialization sequence with random address defined by the user
Connectivity random addr1 1.png
Connectivity random addr1 2.png
Initialization sequence with default random address (CFG_STATIC_RANDOM_ADDRESS defined in app_conf.h)
Connectivity random addr2 1.png
Connectivity random addr2 2.png
Initialization sequence with default random address generated with random generator (CFG_STATIC_RANDOM_ADDRESS not defined in app_conf.h)
Connectivity random addr3 1.png
Connectivity random addr3 2.png
Initialization sequence with a new name
Connectivity name1.png
Connectivity name2.png

8. References