STM32WB55 development (3)----Disconnect Bluetooth connection

Overview

In the embedded world, the STM32WB series of microcontrollers is known for its dual-core architecture and powerful wireless connectivity capabilities. In particular, its Bluetooth 5.0 functionality makes this microcontroller ideal for applications that require low power consumption and high-performance wireless connectivity. However, in practical applications, it is very critical to manage Bluetooth connections reasonably and effectively, especially when the device needs to disconnect or re-establish the connection.
This article will focus on the STM32WB platform and provide how to use its HAL library and Bluetooth stack to disconnect the Bluetooth connection. We will first understand how to get the current Bluetooth connection status through the aci_hal_get_link_status function, and then, we will use the hci_disconnect function to disconnect these connections.
I am currently taking ST courses. If you need samples, you can join the group and apply: 615061293.

Hardware preparation

First, you need to prepare a development board. What I prepared here is the WB55RG development board:
Insert image description here

Video teaching

https://www.bilibili.com/video/BV1U14y16712/

STM32WB55 development (3)----Disconnect Bluetooth connection

sample application

https://www.wjx.top/vm/OhcKxJk.aspx#

Source code download

https://download.csdn.net/download/qq_24312945/88324454

Select chip model

Insert image description here

Configure clock source

HSE and LSE are external high-speed clock and low-speed clock respectively. In this article, an external clock source is used, so the Crystal/Ceramic Resonator option is selected, as shown below:
Insert image description here

Configure clock tree

Insert image description here

RTC clock configuration

Insert image description here
RFWKP clock configuration

Insert image description here

Check the conditions for enabling STM32_WPAN

As you can see, RF, RTC, RCC, IPCC, and HSEM need to be turned on.
Insert image description here

Configure HSEM

The Hardware Semaphore (HSEM) module is used to manage shared access rights and resource synchronization between multiple processes.
Turn on HSEM as follows.
Insert image description here

Configure IPCC

The communication controller (IPCC) module is mainly used for signal message exchange between CPUs.
Turn it on as shown below.

Insert image description here

Configure RTC

Insert image description here

Start RF

Insert image description here

Turn on Bluetooth

Insert image description here

Configure as a custom template.
Insert image description here

Name the device

Insert image description here

Configure BLE GATT

Insert image description here

Configure SVC
Insert image description here

LED configuration

Looking at the schematic, we can see that PB0 is a blue LED and PB1 is a green LED.

Insert image description here

Configure PB0 and PB1 as output IO.

Insert image description here

Set project information

Insert image description here

Project file settings

Insert image description here

Reference documentation

https://wiki.st.com/stm32mcu/wiki/Connectivity:STM32WB_HeartRate
HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE event can determine whether the build is completed.

Insert image description here

SVCCTL_App_Notification

The SVCCTL_App_Notification function is a Bluetooth event handler used to respond to and handle various events that occur in the STM32WB BLE stack.
The main contents of event processing:

  1. Disconnect event (HCI_DISCONNECTION_COMPLETE_EVT_CODE)
  2. Meta event (HCI_LE_META_EVT_CODE)
  3. This part handles several sub-events, such as HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE (connection update completed) and HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE (new connection establishment completed).
  4. Vendor specific debug events (HCI_VENDOR_SPECIFIC_DEBUG_EVT_CODE)

"In the HCI_LE_CONNECTION_COMPLETE_SUBEVT_CODE event, we can determine whether the Bluetooth Low Energy (BLE) device has successfully established a new connection. In contrast, in the HCI_DISCONNECTION_COMPLETE_EVT_CODE event, we can confirm whether an existing connection has been disconnected. These two Events provide us with an important means of monitoring the status of BLE connections, allowing us to better manage the device's connection lifecycle."

Add an IO flip function in HCI_DISCONNECTION_COMPLETE_EVT_CODE.

      /* USER CODE BEGIN EVT_DISCONN_COMPLETE */
			HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_1);
      /* USER CODE END EVT_DISCONN_COMPLETE */

Insert image description here
Add an IO flip function in HCI_DISCONNECTION_COMPLETE_EVT_CODE.
Insert image description here

ACI_HAL_GET_LINK_STATUS

In the aci_hal_get_link_status function description, the Link_Status array it returns has 8 elements, which means that the device can manage up to 8 Bluetooth Low Energy (BLE) connections. Each element represents a possible BLE connection state.
Therefore, when you want to get the status of each BLE connection and perform some action based on it (such as disconnecting), you need to loop through these 8 possible connections.

Insert image description here

There is a description of this function in ble_hci_le.h. To use this function to obtain the handle of the current connection and subsequently disconnect, you can do this:
declare two arrays to store the connection status and connection handle returned by the function.
Call the aci_hal_get_link_status function to obtain these values.
Loop through the connection status array, looking for any connection marked as connected (for example, with a value of 0x02 or 0x05).
For each connected state, obtain the corresponding connection handle from the connection handle array and use the hci_disconnect function to disconnect.

Insert image description here

Add the ble_hci_le.h file in main.c.

/* USER CODE BEGIN Includes */

#include "ble_hci_le.h"
/* USER CODE END Includes */

Add variables in main.c.

/* USER CODE BEGIN 0 */
uint8_t connect_flag=0;//连接成功标志位
uint32_t connect_num=0;//断开连接计数器

/* USER CODE END 0 */

By referencing the connect_flag variable in app_ble.c, you can set the flag to 1 after the connection is successful.

/* USER CODE BEGIN Includes */
extern uint8_t connect_flag;//连接成功标志位
/* USER CODE END Includes */

You can add a flag definition in the HCI_LE_CONNECTION_UPDATE_COMPLETE_SUBEVT_CODE (connection update complete) event and set connect_flag to 1.

          /* USER CODE BEGIN HCI_EVT_LE_CONN_COMPLETE */
					HAL_GPIO_TogglePin(GPIOB,GPIO_PIN_0);
					connect_flag=1;
          /* USER CODE END HCI_EVT_LE_CONN_COMPLETE */

Insert image description here
After a delay of about 5S in the main program, disconnect the connection, traverse the connection status array, and find any connection marked as connected (for example, the value is 0x02 or 0x05).
For each connected state, obtain the corresponding connection handle from the connection handle array and use the hci_disconnect function to disconnect.

  /* Init code for STM32_WPAN */
  MX_APPE_Init();

  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    
    
    /* USER CODE END WHILE */
    MX_APPE_Process();

    /* USER CODE BEGIN 3 */
		//检查连接标志,如果连接成功,则进行处理
		if(connect_flag)
		{
    
    
			if(connect_num<5000)
				connect_num++;
			else
			{
    
    
				connect_num=0;
				// 初始化连接句柄变量
				uint16_t Connection_Handle1=0;
				
				// 定义存储连接状态和连接句柄的数组
				uint8_t linkStatus[8];
				uint16_t linkHandles[8];
				tBleStatus status;
				// 获取当前的连接状态
				status = aci_hal_get_link_status(linkStatus, linkHandles);
				// 检查是否成功获取连接状态
				if (status == BLE_STATUS_SUCCESS) {
    
    
						// 遍历所有可能的连接
						for (int i = 0; i < 8; i++) {
    
    
								// 判断当前连接是否处于Peripheral或Central角色
								if (linkStatus[i] == 0x02 || linkStatus[i] == 0x05) {
    
     
										// 断开与此连接句柄关联的连接
										hci_disconnect(linkHandles[i], 0x13); // 使用适当的断开原因
								}
						}
					}
				// 重置连接标志
				connect_flag=0;
			}
		}
		HAL_Delay(1);
		
  }
  /* USER CODE END 3 */

hci_disconnect

This is a function implementation of the Bluetooth HCI (Host Controller Interface) command, named hci_disconnect. HCI is an interface defined in the Bluetooth specification that allows communication between the host and the Bluetooth controller.
Insert image description here

There is a description of this function in ble_hci_le.h. To use this function to terminate an existing Bluetooth connection, you can do this: The
corresponding parameters are:
Connection_Handle: This is an identifier indicating the connection to be disconnected. The valid value range is 0x0000 to 0x0EFF.
Reason: This is a parameter indicating the reason for terminating the connection. The following are possible causes and their values:
0x05: Authentication Failure
0x13: Remote User Terminated Connection
0x14: Remote Device Terminated Connection due to Low Resources )
0x15: Remote Device Terminated Connection due to Power Off
0x1A: Unsupported Remote Feature
0x3B: Unacceptable Connection Parameters
so 0x13 can be used Perform Bluetooth disconnection.
Insert image description here

Results demonstration

Establish a connection with the STM32WB through the mobile phone, and then wait to see whether the handle can be obtained correctly.
Insert image description here

Insert image description here

Guess you like

Origin blog.csdn.net/qq_24312945/article/details/132793962