319 lines
9.1 KiB
C
Executable File
319 lines
9.1 KiB
C
Executable File
/****************************************************************************
|
||
** BLE.c
|
||
**
|
||
** Copyright (c) Siargo Ltd. 2020
|
||
** All Rights Reserved.
|
||
**
|
||
** BY DMY
|
||
**
|
||
** 蓝牙无线通讯模块
|
||
**
|
||
**
|
||
** 历史文件
|
||
** 2026-03-09 V1000
|
||
*****************************************************************************/
|
||
|
||
/* Includes ----------------------------------------------------------------*/
|
||
#include "../main/SystemInclude.h"
|
||
/* -------------------定义变量--------------------------------------*/
|
||
extern DMA_HandleTypeDef DMA_Handle_UartRx , DMA_Handle_UartTx ;
|
||
|
||
static u8 BLE_resp = BLE_WAIT;
|
||
|
||
uint8_t Series[12]={0,1,2,3,4,5,6,7,8,9,10,11};
|
||
uint8_t BLE_count;
|
||
uint8_t BLE_flag;
|
||
|
||
u8 BLE_at_response_check(u8 *resp, u8 check_resp)
|
||
{// 等待处理模组返回的信息
|
||
u8 count = 0;
|
||
while(*resp != check_resp)
|
||
{
|
||
delay_ms(10);
|
||
count++;
|
||
if(count == 100) //等待1s,如果仍然没有更新,则表示模组没有返回,可能卡死了
|
||
return 1;
|
||
}
|
||
return 0;
|
||
}
|
||
|
||
u8 BLE_at_cmd_write(char *pbuf)
|
||
{
|
||
BLE_resp = BLE_WAIT;
|
||
BLE_SendData((u8*)pbuf, strlen(pbuf));
|
||
return BLE_at_response_check(&BLE_resp, BLE_OK);
|
||
}
|
||
|
||
u8 BLE_at_rec_process(char *inbuf, u8 data_len)// 处理模组传给MCU的数据,根据需求自行填写
|
||
{
|
||
int i = 0;
|
||
u8 resp;
|
||
if (strstr(inbuf,"ERROR\r"))
|
||
{
|
||
resp = BLE_ERROR;
|
||
} else if(strstr(inbuf,"OK\r")) {
|
||
resp = BLE_OK;
|
||
} else {
|
||
resp = BLE_OTHER;
|
||
}
|
||
return resp;
|
||
}
|
||
|
||
void BLE_lp_exit(void)
|
||
{
|
||
LHL_GPIO_WritePin(BLE_CTS_GPIO, BLE_CTS_PIN, GPIO_PIN_SET);
|
||
delay_ms(1);
|
||
LHL_GPIO_WritePin(BLE_CTS_GPIO, BLE_CTS_PIN, GPIO_PIN_RESET);
|
||
delay_ms(1);
|
||
}
|
||
void BLE_lp_enter(void)
|
||
{
|
||
BLE_at_cmd_write("AT+SLEEP=0,1\r\n");
|
||
|
||
}
|
||
void BLE_metering_packet_send(void)
|
||
{
|
||
char tx_buf[256];
|
||
BLE_lp_exit();
|
||
BLE_at_cmd_write(tx_buf);
|
||
BLE_at_cmd_write("AT+ADS=1,0,1000\r\n");
|
||
}
|
||
void BLE_identification_packet_send(void)
|
||
{
|
||
char tx_buf[256];
|
||
BLE_lp_exit();
|
||
|
||
BLE_at_cmd_write(tx_buf);
|
||
BLE_at_cmd_write("AT+ADS=1,0,100\r\n");
|
||
}
|
||
void BLE_beacon_check(void)
|
||
{
|
||
BLE_count++;
|
||
if(BLE_count == 236)
|
||
BLE_flag = BLE_FLAG_ID;// 计时到,标记发送 ID 包
|
||
else if(BLE_count == 240){
|
||
BLE_count = 0;;// 计时到,重置计数器
|
||
BLE_flag = BLE_FLAG_METER;// 标记发送计量包
|
||
}
|
||
else
|
||
;
|
||
}
|
||
void BLE_beacon_done(void)
|
||
{
|
||
if(BLE_flag == BLE_FLAG_METER){
|
||
BLE_metering_packet_send();
|
||
BLE_flag = 0;
|
||
}
|
||
else if(BLE_flag == BLE_FLAG_ID){
|
||
BLE_identification_packet_send();
|
||
BLE_flag = 0;
|
||
}
|
||
else
|
||
;
|
||
}
|
||
/**
|
||
* @brief 清除接收标志
|
||
*/
|
||
static inline void ClearRxFlag(void)
|
||
{
|
||
comState.state.ReceivedData = 0;
|
||
}
|
||
|
||
/******蓝牙部分*************************************************************************************************************/
|
||
void BLE_init(void)
|
||
{
|
||
GPIO_InitTypeDef GPIO_InitStruct;
|
||
// CTS 输入模式 (用于流控检测)
|
||
GPIO_InitStruct.Pin = BLE_CTS_PIN;
|
||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||
GPIO_InitStruct.Current = GPIO_CURRENT_2mA;
|
||
GPIO_InitStruct.Pull = GPIO_PULLUP;
|
||
GPIO_InitStruct.SchmittTrigger = ENABLE;
|
||
GPIO_InitStruct.Alternate = GPIOx_AF_GPIO; // // AF为普通IO
|
||
LHL_GPIO_Init(BLE_CTS_GPIO, &GPIO_InitStruct);
|
||
|
||
// RST
|
||
GPIO_InitStruct.Pin = BLE_RST_PIN;
|
||
GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP;
|
||
GPIO_InitStruct.Current = GPIO_CURRENT_2mA;
|
||
GPIO_InitStruct.Pull = GPIO_NOPULL;
|
||
GPIO_InitStruct.SchmittTrigger = ENABLE;
|
||
GPIO_InitStruct.Alternate = GPIOx_AF_GPIO; // // AF为普通IO
|
||
LHL_GPIO_Init(BLE_RST_GPIO, &GPIO_InitStruct);
|
||
|
||
//3. 默认拉高复位脚
|
||
LHL_GPIO_WritePin(BLE_CTS_GPIO, BLE_CTS_PIN, GPIO_PIN_RESET);
|
||
LHL_GPIO_WritePin(BLE_RST_GPIO, BLE_RST_PIN, GPIO_PIN_SET);
|
||
delay_ms(1000);
|
||
|
||
char tx_buf[256];
|
||
BLE_at_cmd_write("+++"); // 进入 AT 指令模式
|
||
// 设置蓝牙名称:SIARGO + 序列号 (Hex格式)
|
||
sprintf(tx_buf, "AT+NAME=1,53494152%x%x%x%x%x%x%x%x%x%x\r\n", Series[1], Series[2], Series[3],
|
||
Series[4], Series[5], Series[6], Series[7], Series[8], Series[9], Series[10] );
|
||
BLE_at_cmd_write(tx_buf);
|
||
// 发送 AT+EXIT 退出 AT 模式,进入透传模式
|
||
// 此后,模块将不再解析 AT 指令,所有串口数据将直接透传给 MCU
|
||
BLE_at_cmd_write("AT+EXIT\r\n");
|
||
|
||
}
|
||
void BLE_HardwareReset(void)
|
||
{
|
||
// 拉低复位
|
||
LHL_GPIO_WritePin(BLE_RST_GPIO, BLE_RST_PIN, GPIO_PIN_RESET);
|
||
delay_ms(BLE_RESET_HOLD_TIME);
|
||
|
||
// 拉高释放
|
||
LHL_GPIO_WritePin(BLE_RST_GPIO, BLE_RST_PIN, GPIO_PIN_SET);
|
||
delay_ms(BLE_RESET_WAIT_TIME);
|
||
}
|
||
|
||
void BLE_SendData(uint8_t *pData, uint16_t len)
|
||
{
|
||
if (pData == NULL || len == 0) return;
|
||
// 停止之前传输
|
||
// LHL_DMA_Stop(&DMA_Handle_UartTx);
|
||
// // 拷贝并发送
|
||
// memcpy(comState.TxdData, pData, len);
|
||
MBBuf.TxdLen = len;
|
||
MBBuf.TxPointer =pData ;
|
||
StartDMAForTxdMainUartData();
|
||
}
|
||
|
||
|
||
void BLEModBusReturnDevice(void)
|
||
{
|
||
MBBuf.RxPointer[0] = 0x01; // 从机地址
|
||
MBBuf.RxPointer[1] = 0x2B; // 功能码 (自定义)
|
||
MBBuf.RxPointer[2] = 0x0E; // 长度
|
||
MBBuf.RxPointer[3] = 0x01;
|
||
MBBuf.RxPointer[4] = 0x01;
|
||
MBBuf.RxPointer[5] = 0xFF;
|
||
MBBuf.RxPointer[6] = 0x05;
|
||
MBBuf.RxPointer[7] = 0x03;
|
||
MBBuf.RxPointer[8] = 0x00;
|
||
MBBuf.RxPointer[9] = 0x0D;
|
||
// 硬编码字符串 "SIARGO-MF5700"
|
||
MBBuf.RxPointer[10] = 0x53;//S
|
||
MBBuf.RxPointer[11] = 0x49; //I
|
||
MBBuf.RxPointer[12] = 0x41;//A
|
||
MBBuf.RxPointer[13] = 0x52;//R
|
||
MBBuf.RxPointer[14] = 0x47;//G
|
||
MBBuf.RxPointer[15] = 0x4F;//O
|
||
MBBuf.RxPointer[16] = 0x2D;//-
|
||
MBBuf.RxPointer[10] = 0x56; // V
|
||
MBBuf.RxPointer[11] = 0x4D; // M
|
||
MBBuf.RxPointer[12] = 0x31; // 1
|
||
MBBuf.RxPointer[13] = 0x30; // 0
|
||
MBBuf.RxPointer[14] = 0x30; // 0
|
||
MBBuf.RxPointer[15] = 0x30; // 0
|
||
MBBuf.RxPointer[23] = 0x02;//XULEIHAO (类型号?)
|
||
MBBuf.RxPointer[24] = 0x0C; // 序列号长度
|
||
MBBuf.RxPointer[25] = Series[0]; // 填充10位序列号
|
||
MBBuf.RxPointer[26] = Series[1];
|
||
MBBuf.RxPointer[27] = Series[2];
|
||
MBBuf.RxPointer[28] = Series[3];
|
||
MBBuf.RxPointer[29] = Series[4];
|
||
MBBuf.RxPointer[30] = Series[5];
|
||
MBBuf.RxPointer[31] = Series[6];
|
||
MBBuf.RxPointer[32] = Series[7];
|
||
MBBuf.RxPointer[33] = Series[8];
|
||
MBBuf.RxPointer[34] = Series[9];
|
||
MBBuf.RxPointer[35] = Series[10];
|
||
MBBuf.RxPointer[36] = Series[11];
|
||
MBBuf.RxPointer[37] = 0x02;
|
||
MBBuf.RxPointer[38] = 0x04;
|
||
// MBBuf.RxPointer[39] = MAJ_VERSION; // 固件版本
|
||
// MBBuf.RxPointer[40] = SUB_VERSION;
|
||
// MBBuf.RxPointer[41] = REV_VERSION;
|
||
// MBBuf.RxPointer[42] = BUD_VERSION;
|
||
MBBuf.RxPointer[39] = 0x0a; // 固件版本
|
||
MBBuf.RxPointer[40] = 0x0b;
|
||
MBBuf.RxPointer[41] = 0x0c;
|
||
MBBuf.RxPointer[42] = 0x0d;
|
||
MBBuf.RxPointer[43] = 0x03;
|
||
MBBuf.RxPointer[44] = 0x01;
|
||
MBBuf.RxPointer[45] = 0x00;
|
||
MBBuf.RxPointer[46] = 0x04;
|
||
MBBuf.RxPointer[47] = 0x01;
|
||
MBBuf.RxPointer[48] = 0x00;
|
||
|
||
MBBuf.TxdLen =49;
|
||
}
|
||
|
||
void BLEModBusReturnData(void)
|
||
{
|
||
|
||
u8 tx_Length;
|
||
|
||
tx_Length = 0;
|
||
MBBuf.RxPointer[tx_Length++] = 0x01;
|
||
MBBuf.RxPointer[tx_Length++] = 0x42;
|
||
MBBuf.RxPointer[tx_Length++] = 0x0c;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x3A;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x3C;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x3E;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x37;
|
||
//MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
// MBBuf.RxPointer[tx_Length++] = 0x48;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x50;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x51;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x90;
|
||
|
||
MBBuf.RxPointer[2] = tx_Length - 3;
|
||
MBBuf.TxdLen=tx_Length;
|
||
|
||
}
|
||
|
||
void BLEModBusReturnparameter(void)
|
||
{
|
||
u8 tx_Length;
|
||
|
||
tx_Length = 0;
|
||
MBBuf.RxPointer[tx_Length++] = 0x01;
|
||
MBBuf.RxPointer[tx_Length++] = 0x43;
|
||
MBBuf.RxPointer[tx_Length++] = 0x12;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x83;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x85;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x8B;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x96;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x98;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x9A;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0x9C;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0xF2;
|
||
MBBuf.RxPointer[tx_Length++] = 0x00;
|
||
MBBuf.RxPointer[tx_Length++] = 0xF0;
|
||
MBBuf.RxPointer[2] = tx_Length - 3;
|
||
MBBuf.TxdLen=tx_Length;
|
||
}
|
||
|
||
/**
|
||
* @brief 启动蓝牙初始化流程
|
||
* @note 此函数只做硬件复位和设置开始标志,不会阻塞
|
||
*/
|
||
void BLE_Application_Init(void)
|
||
{
|
||
/* ==================== 第 1 步:硬件初始化 ==================== */
|
||
// 1.1 调用底层驱动初始化 GPIO (CTS/RTS/RST)
|
||
BLE_init();
|
||
//硬件复位
|
||
// LHL_GPIO_WritePin(BLE_RST_GPIO, BLE_RST_PIN, GPIO_PIN_RESET);
|
||
}
|
||
|
||
|