BLE蓝牙模块HC-08和BLE-103驱动
- 驱动代码
- 使用方法
需要注意的是,两款蓝牙芯片都不是默认波特率为115200,都是以AT指令进行配置修改,所以驱动做了修改,自动切换自身波特率进行通讯,修改波特率参数后,调回115200。
驱动主要做了以下工作:
1、修改蓝牙名称
2、修改波特率到115200
3、配置打包机制
4、设置从站模式
驱动代码
/**
* @file Bluetooth_Port.c
*
* @date 2021-02-26
*
* @author aron566
*
* @copyright Copyright (c) 2021 aron566 <aron566@163.com>.
*
* @brief 蓝牙接口初始化
*
* @details 1、与APP通讯使用20Bytes打包机制
*
* @version V1.0
*/
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
/* Private includes ----------------------------------------------------------*/
#include "Bluetooth_Port.h"
#include "Uart_Port.h"
/** Private typedef ----------------------------------------------------------*/
/** Private macros -----------------------------------------------------------*/
/*BLE配置*/
#define USE_BLE_103_DEV 1 /**< 使用有人模块*/
#define ENABLE_BLE_DEBUG 0 /**< 启用BLE调试模式*/
#define SET_CMD_TIMEOUT_MAX 4U /**< 4s判定设置超时*/
#define USE_BAUD_REATE_VALUE 115200U /**< 设置波特率*/
#define BUF_SIZE_MAX 64U /**< 最大单次接收AT响应长度*/
/*模块AT指令集*/
#if USE_BLE_103_DEV
/*有人WH_BLE-103*/
#define DEFAULT_BAUD_RATE 57600U /**< 默认波特率*/
#define BLE_DEVICE_NAME "aidiWH" /**< 设定蓝牙名称*/
#define SLAVE_MODE_CMD "AT+MODE=S\r\n" /**< 设置模块为从站模式*/
#define MASTER_MODE_CMD "AT+MODE=M\r\n" /**< 设置模块为主站模式*/
#define BROADCAST_MODE_CMD "AT+MODE=B\r\n" /**< 设置模块为广播模式*/
#define DEV_DISCONNECT_CMD "AT+DISCONN\r\n"
#define CHECK_CONNECT_STA_CMD "AT+LINK?\r\n"
#define DEV_REBOOT_CMD "AT+Z\r\n" /**< 控制模块重启*/
#define SET_BAUD_RATE_CMD "AT+UART=%u,8,0,0\r\n"/**< 设置波特率*/
#define SET_BAUD_RETURN_STR "\r\n+UART:%u,8,0,0\r\nOK\r\n"
#define PACKAGE_MAXPUT "OFF\r\n" /**< 小包模式/100Bytes每包*/
#define PACKAGE_TIME "30\r\n" /**< 打包间隔时间>=30 value <=100*/
#define SEND_POWER "8\r\n" /**< 发送功率*/
#define SET_UUID_AA "AA0003CDD000001000800000805F9B0131\r\n"/**< 主服务UUID 返回:0003CDD0-00001000-8000-00805F9B0131*/
#define SET_UUID_BB "BB0003CDD100001000800000805F9B0131\r\n"/**< 串口读服务,一个 Notify 服务 返回:0003CDD1-00001000-8000-00805F9B0131*/
#define SET_UUID_CC "CC0003CDD200001000800000805F9B0131\r\n"/**< 串口写服务,一个 WriteWithoutResponse 服务 返回:0003CDD2-00001000-8000-00805F9B0131*/
#else
/*HC-08*/
#define DEFAULT_BAUD_RATE 9600U /**< 默认波特率*/
#define BLE_DEVICE_NAME "aidiHC" /**< 设定蓝牙名称*/
#define SET_BAUD_RATE_CMD "AT+BAUD=%u,N"
#define SET_BAUD_RETURN_STR "OK%u,NONE"
#endif
/** Private constants --------------------------------------------------------*/
/** Public variables ---------------------------------------------------------*/
/** Private variables --------------------------------------------------------*/
static Uart_Dev_Handle_t *uart_opt_handle = NULL;/**< 串口操作句柄*/
static char AT_Cmd_Str[BUF_SIZE_MAX];
static char AT_Reply_Str[BUF_SIZE_MAX];
/** Private function prototypes ----------------------------------------------*/
static bool Get_Result_State(const char *Cmd_Str, const char *Result_Str, uint32_t Time_Out_Sec);
static bool Set_Baudrate(uint32_t BaudRate);
static void Set_Self_Baudrate(uint32_t BaudRate);
static void Bluetooth_Port_Reset(void);
#if USE_BLE_103_DEV
static void Ble_103_Dev_Init(void);
#else
static void Ble_HC_08_Init(void);
#endif
/** Private user code --------------------------------------------------------*/
/** Private application code -------------------------------------------------*/
/*******************************************************************************
*
* Static code
*
********************************************************************************
*/
/**
******************************************************************
* @brief 设置自身通讯波特率
* @param [in]BaudRate 波特率
* @return None.
* @author aron566
* @version V1.0
* @date 2021-02-26
******************************************************************
*/
static void Set_Self_Baudrate(uint32_t BaudRate)
{
Uart_Port_Set_Baudrate(UART_NUM_6, BaudRate);
}
/**
******************************************************************
* @brief 设置蓝牙通讯波特率
* @param [in]BaudRate 波特率
* @return true 设置成功.
* @author aron566
* @version V1.0
* @date 2021-02-26
******************************************************************
*/
static bool Set_Baudrate(uint32_t BaudRate)
{
static uint8_t Retry_Cnt = 0;
/*设置波特率*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, SET_BAUD_RATE_CMD, BaudRate);
snprintf(AT_Reply_Str, BUF_SIZE_MAX, SET_BAUD_RETURN_STR, BaudRate);
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == true)
{
return true;
}
/*New Baudrate Retry*/
if(Retry_Cnt > 0)
{
return false;
}
Set_Self_Baudrate(BaudRate);
Retry_Cnt++;
Set_Baudrate(BaudRate);
return false;
}
/**
******************************************************************
* @brief 发送字符串命令等待返回结果
* @param [in]Cmd_Str 命令字符串.
* @param [in]Result_Str 预期等待返回字符串.
* @param [in]Time_Out_Sec 预期等待时间.
* @return true 返回成功.
* @author aron566
* @version V1.0
* @date 2021-03-18
******************************************************************
*/
static bool Get_Result_State(const char *Cmd_Str, const char *Result_Str, uint32_t Time_Out_Sec)
{
uint32_t len = 0;
uint8_t temp_buf[BUF_SIZE_MAX] = {0};
uint32_t Start_Sec = Timer_Port_Get_Current_Time(TIMER_SEC);
uint32_t Compare_Len = strlen(Result_Str);
while(1)
{
/*发送AT读指令*/
Uart_Port_Transmit_Data(uart_opt_handle, (uint8_t *)Cmd_Str, strlen(Cmd_Str), 100);
HAL_Delay(500);
/*等待回复*/
len = CQ_skipInvaildU8Header(uart_opt_handle->cb, Result_Str[0]);
if(len < Compare_Len)
{
/*检测超时*/
if((Timer_Port_Get_Current_Time(TIMER_SEC) - Start_Sec) >= Time_Out_Sec)
{
CQ_ManualOffsetInc(uart_opt_handle->cb, len);
return false;
}
continue;
}
len = len > BUF_SIZE_MAX?BUF_SIZE_MAX:len;
CQ_ManualGetData(uart_opt_handle->cb, temp_buf, len);
if(memcmp(temp_buf, Result_Str, Compare_Len) == 0)
{
CQ_ManualOffsetInc(uart_opt_handle->cb, len);
return true;
}
else
{
/*检测超时*/
if((Timer_Port_Get_Current_Time(TIMER_SEC) - Start_Sec) >= Time_Out_Sec)
{
CQ_ManualOffsetInc(uart_opt_handle->cb, len);
return false;
}
CQ_ManualOffsetInc(uart_opt_handle->cb, 1);
}
}
}
/**
******************************************************************
* @brief 蓝牙复位
* @param [in]None.
* @return None.
* @author aron566
* @version V1.0
* @date 2021-04-02
******************************************************************
*/
static void Bluetooth_Port_Reset(void)
{
#if USE_BLE_103_DEV
HAL_GPIO_WritePin(BLE_NRST_GPIO_Port, BLE_NRST_Pin, GPIO_PIN_RESET);
HAL_Delay(1);
HAL_GPIO_WritePin(BLE_NRST_GPIO_Port, BLE_NRST_Pin, GPIO_PIN_SET);
#else
#endif
}
/**
******************************************************************
* @brief 初始化BLE103蓝牙
* @param [in]None.
* @return None.
* @author aron566
* @version V1.0
* @date 2021-03-18
******************************************************************
*/
#if USE_BLE_103_DEV
static void Ble_103_Dev_Init(void)
{
/*唤醒*/
Bluetooth_Port_Wakeup();
/*复位*/
Bluetooth_Port_Reset();
/*调节至设备默认波特率*/
uint8_t Error_Cnt = 0;
Set_Self_Baudrate(DEFAULT_BAUD_RATE);
while(1)
{
/*进入AT指令设置模式*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "%s", "+++a");
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "a+ok");
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == true)
{
break;
}
else
{
if(Error_Cnt > 1)
{
return;
}
Set_Self_Baudrate(USE_BAUD_REATE_VALUE);
Error_Cnt++;
}
}
/*设置设备名称*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+NAME=%s%04X\r\n", BLE_DEVICE_NAME, HAL_GetUIDw0());
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+NAME:"BLE_DEVICE_NAME);
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
/*设置蓝牙波特率*/
if(Set_Baudrate(USE_BAUD_REATE_VALUE) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
/*设置打包机制*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+MAXPUT=%s", PACKAGE_MAXPUT);
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+MAXPUT:"PACKAGE_MAXPUT);
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
/*设置发送功率*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+TPL=%s", SEND_POWER);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+TPL:");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
/*设置打包时间*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+UARTTM=%s", PACKAGE_TIME);
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+UARTTM:"PACKAGE_TIME);
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
// /*设置设备UUIDAA*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+UUID=%s", SET_UUID_AA);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "0003CDD0");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
// /*设置设备UUIDBB*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+UUID=%s", SET_UUID_BB);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "0003CDD1");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
//
// /*设置设备UUIDCC*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+UUID=%s", SET_UUID_CC);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "0003CDD2");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
// /*设置从站模式*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "%s", SLAVE_MODE_CMD);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+MODE:Slave\r\nOK\r\n");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
/*清空缓冲区*/
HAL_Delay(10);
CQ_emptyData(uart_opt_handle->cb);
/*设置自身波特率*/
Set_Self_Baudrate(USE_BAUD_REATE_VALUE);
/*设置模块重启--Deprecated: Use The Set Mode To Reboot Device*/
// snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "%s", DEV_REBOOT_CMD);
// snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+RST:OK\r\nOK\r\n");
// /*发送设置*/
// if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
// {
//#if ENABLE_BLE_DEBUG
// while(1);
//#endif
// }
/*退出命令模式*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "%s", "AT+ENTM\r\n");
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "\r\n+ENTM:OK\r\nOK\r\n");
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
}
#else
/**
******************************************************************
* @brief 初始化BLE HC-08蓝牙
* @param [in]None.
* @return None.
* @author aron566
* @version V1.0
* @date 2021-03-18
******************************************************************
*/
static void Ble_HC_08_Init(void)
{
/*调节至设备默认波特率*/
Set_Self_Baudrate(DEFAULT_BAUD_RATE);
/*设置蓝牙波特率*/
if(Set_Baudrate(USE_BAUD_REATE_VALUE) == true)
{
Set_Self_Baudrate(USE_BAUD_REATE_VALUE);
}
/*设置设备名称*/
snprintf(AT_Cmd_Str, BUF_SIZE_MAX, "AT+NAME=%s%04X%04X", BLE_DEVICE_NAME, HAL_GetUIDw0(), HAL_GetUIDw1());
snprintf(AT_Reply_Str, BUF_SIZE_MAX, "%s", "OKsetNAME");
/*发送设置*/
if(Get_Result_State(AT_Cmd_Str, AT_Reply_Str, SET_CMD_TIMEOUT_MAX) == false)
{
#if ENABLE_BLE_DEBUG
while(1);
#endif
}
}
#endif
/** Public application code --------------------------------------------------*/
/*******************************************************************************
*
* Public code
*
********************************************************************************
*/
/**
******************************************************************
* @brief 蓝牙唤醒
* @param [in]None
* @return None.
* @author aron566
* @version V1.0
* @date 2021-04-02
******************************************************************
*/
void Bluetooth_Port_Wakeup(void)
{
#if USE_BLE_103_DEV
HAL_GPIO_WritePin(BLE_WAKEUP_GPIO_Port, BLE_WAKEUP_Pin, GPIO_PIN_RESET);
HAL_Delay(1500);
HAL_GPIO_WritePin(BLE_WAKEUP_GPIO_Port, BLE_WAKEUP_Pin, GPIO_PIN_SET);
#else
#endif
/*清空缓冲区*/
CQ_emptyData(uart_opt_handle->cb);
}
/**
******************************************************************
* @brief 蓝牙接口初始化
* @param [in]None
* @return None.
* @author aron566
* @version V1.0
* @date 2021-02-26
******************************************************************
*/
void Bluetooth_Port_Init(void)
{
#if USE_BLE_103_DEV
/*初始化WH-BLE_103设备*/
uart_opt_handle = Uart_Port_Get_Handle(UART_NUM_6);
if(uart_opt_handle == NULL)
{
printf("get uart opt handle faild.\n");
return;
}
Ble_103_Dev_Init();
#else
/*初始化HC-08设备*/
uart_opt_handle = Uart_Port_Get_Handle(UART_NUM_2);
if(uart_opt_handle == NULL)
{
printf("get uart opt handle faild.\n");
return;
}
Ble_HC_08_Init();
#endif
}
#ifdef __cplusplus ///<end extern c
}
#endif
/******************************** End of file *********************************/
/**
* @file Bluetooth_Port.h
*
* @date 2021-02-26
*
* @author aron566
*
* @brief 蓝牙接口初始化
*
* @version V1.0
*/
#ifndef BLUETOOTH_PORT_H
#define BLUETOOTH_PORT_H
#ifdef __cplusplus ///<use C compiler
extern "C" {
#endif
/** Includes -----------------------------------------------------------------*/
#include <stdint.h> /**< need definition of uint8_t */
#include <stddef.h> /**< need definition of NULL */
#include <stdbool.h>/**< need definition of BOOL */
#include <stdio.h> /**< if need printf */
#include <stdlib.h>
#include <string.h>
#include <limits.h> /**< need variable max value */
/** Private includes ---------------------------------------------------------*/
/** Private defines ----------------------------------------------------------*/
/** Exported typedefines -----------------------------------------------------*/
/** Exported constants -------------------------------------------------------*/
/** Exported macros-----------------------------------------------------------*/
/** Exported variables -------------------------------------------------------*/
/** Exported functions prototypes --------------------------------------------*/
/*蓝牙接口初始化*/
void Bluetooth_Port_Init(void);
/*蓝牙唤醒*/
void Bluetooth_Port_Wakeup(void);
#ifdef __cplusplus ///<end extern c
}
#endif
#endif
/******************************** End of file *********************************/
使用方法
- 在驱动中可以看到
Uart_Port_Get_Handle(UART_NUM_2);
这样的接口,这是获取串口操作的句柄,在这个句柄中带有串口接收数据缓冲队列,移植方法参考我的博客 - 还有超时检测的机制,所以需要提供当前时间的接口
Timer_Port_Get_Current_Time(TIMER_SEC)
- 在实际测试使用中BLE-103即有人模块没有HC-08的数据传输快,咨询过有人,据说数据以20的整数倍进行发送会好,实测并没有。HC-08的模块在开机的一瞬间电流较大,10mA左右,单独使用串口工具供电类似于CH34x这种,可能直接会软件挂掉(发送数据,串口工具tx指示灯不闪烁),需要注意。
void main(void)
{
/*初始化硬件*/
/*串口操作初始化*/
Uart_Port_Init();
/*初始化定时器*/
/*初始化蓝牙*/
Bluetooth_Port_Init();
}