根据串行通信原理设计搭建了三方之间交互通信环境,通过协议相互收发数据,模块实现情况。该设计实现三方交互通信方式,数据传输灵活、方便、可靠,具有较高的实用性。
仅供参考
#include <stdint.h>
#include <string.h>
#include <stdbool.h>
#include "bsp_gpio.h"
#include "bsp_uart.h"
#include "log.h"
#include "business_gpio.h"
#include "business_function.h"
#include "app_main.h"
#include "uart_ble_module.h"
#if BS_POWER_SWITCH
#include "biz_power.h"
#endif
#if BS_NRF_BLE_SUPPORT
#include "biz_ble.h"
#endif
#define PROT_CMD_HEAD_PACK(BUFF, CMD, LEN) \
do \
{ \
BUFF[0] = 0x55; \
BUFF[1] = 0x56; \
BUFF[2] = CMD; \
BUFF[3] = LEN; \
}while(0); \
uint8_t data_temp[32] = {0};
static uint8_t get_checksum(uint8_t *data, uint8_t len)
{
uint16_t sum = 0;
for (uint8_t i = 0; i < len; i++)
{
sum += data[i];
}
return sum & 0x00FF;
}
static void send_data(uint8_t *data, uint16_t len)
{
if ((data[2] & 0x80) == 0x80)
{
biz_ble_data_send(data + 2, len - 2);
}
else
{
bsp_uart_send_nbyte(BOARD_UART_COMM, data, len);
}
}
static void response_get_device_info(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 8) | PROT_CMD_GET_DEVICE_INFO_RES, BLE_MAC_MAX_VAL + strlen(FIRMWARE_DATE));
temp_len = 4;
memcpy(&data_temp[4], get_ble_mac_addr(), BLE_MAC_MAX_VAL);
temp_len += BLE_MAC_MAX_VAL;
memcpy(&data_temp[temp_len], FIRMWARE_DATE, strlen(FIRMWARE_DATE));
temp_len += 6;
data_temp[temp_len] = get_checksum(data_temp + 2, temp_len - 2);
send_data(data_temp, temp_len + 1);
}
static void response_get_device_name(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 8) | PROT_CMD_GET_DEVICE_NAME_RES, strlen(PRODUCT_NAME));
temp_len = 4;
memcpy(data_temp + temp_len, PRODUCT_NAME, strlen(PRODUCT_NAME));
temp_len += strlen(PRODUCT_NAME);
*(data_temp + temp_len) = get_checksum(data_temp + 2, temp_len - 2);
send_data(data_temp, temp_len + 1);
}
static void response_bat_state(bool dir)
{
#if BS_POWER_SWITCH
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 8) | PROT_CMD_GET_BAT_SOC_RES, 2);
temp_len = 4;
data_temp[4] = get_bat_soc_val();
data_temp[5] = get_usb_state();
temp_len += 2;
*(data_temp + temp_len) = get_checksum(data_temp + 2, temp_len - 2);
send_data(data_temp, temp_len + 1);
#endif
}
static void response_ble_addr(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 7) | PROT_CMD_GET_BLE_ADDR_RES, BLE_MAC_MAX_VAL);
temp_len = 4;
memcpy(&data_temp[4], get_ble_mac_addr(), BLE_MAC_MAX_VAL);
temp_len += BLE_MAC_MAX_VAL;
data_temp[temp_len] = get_checksum(data_temp + 2 , temp_len - 2);
send_data(data_temp, temp_len + 1);
}
static void response_ble_enter_pair_mode(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 7) | PROT_CMD_BLE_IN_PAIRING_RES, 1);
temp_len = 4;
data_temp[4] = 0;
temp_len += 1;
*(data_temp + temp_len) = get_checksum(data_temp + 2, temp_len - 2);
send_data(data_temp, temp_len + 1);
LOG_I("<INFO> [ble_enter_pair]: %02x\r\n", data_temp[4]);
}
static void response_ble_enter_whitelist_pair_mode(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 7) | PROT_CMD_BLE_WHITELIST_PAIRING_REQ, 1);
temp_len = 4;
data_temp[4] = 0;
temp_len += 1;
*(data_temp + temp_len) = get_checksum(data_temp + 2, temp_len - 2);
send_data(data_temp, temp_len + 1);
LOG_I("<INFO> [ble_enter_pair]: %02x\r\n", data_temp[4]);
}
static void response_ble_connect_state(bool dir)
{
uint8_t temp_len = 0;
PROT_CMD_HEAD_PACK(data_temp, (dir << 7) | PROT_CMD_BLE_CONN_STATE_RES, 1);
temp_len = 4;
data_temp[4] = get_ble_conn_state();
temp_len += 1;
*(data_temp + temp_len) = get_checksum(data_temp, temp_len);
send_data(data_temp, temp_len + 1);
LOG_I("<INFO> [ble_conn_ste]: %02x\r\n", data_temp[4]);
}
static void response_mcu_to_mp(uint8_t *data, uint16_t len)
{
#if BLE_NUS_ENABLED && BS_NRF_BLE_SUPPORT
biz_ble_data_send(data, len);
#endif
}
static void response_mp_to_mcu(uint8_t *data, uint16_t len)
{
bsp_uart_send_nbyte(BOARD_UART_COMM, data, len);
}
static void prot_dispose_data(uint8_t *data, uint16_t len)
{
LOG_D("<DEBUG> [nrf_ble]: %x %x\r\n", ((uint8_t)(data[0] << 1) >> 1), data[0]);
switch(((uint8_t)(data[0] << 1 )>> 1))
{
case PROT_CMD_GET_DEVICE_INFO_REQ:
response_get_device_info((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_GET_DEVICE_INFO_REQ\r\n");
break;
case PROT_CMD_GET_DEVICE_NAME_REQ:
response_get_device_name((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_GET_DEVICE_NAME_REQ\r\n");
break;
case PROT_CMD_GET_BAT_SOC_REQ:
response_bat_state((data[0] & 0x80));
break;
case PROT_CMD_GET_BLE_ADDR_REQ:
response_ble_addr((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_GET_BLE_ADDR_REQ\r\n");
break;
case PROT_CMD_BLE_IN_PAIRING_RES:
response_ble_enter_pair_mode((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_BLE_IN_PAIRING_REQ %d\r\n", data[4]);
break;
case PROT_CMD_BLE_WHITELIST_PAIRING_REQ:
response_ble_enter_whitelist_pair_mode((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_BLE_WHITELIST_PAIRING_REQ %d\r\n", data[4]);
break;
case PROT_CMD_BLE_CONN_STATE_REQ:
response_ble_connect_state((data[0] & 0x80));
LOG_D("<DEBUG> [nrf_ble]: PROT_CMD_BLE_CONN_STATE_REQ %d\r\n", data[4]);
break;
case PROT_CMD_MCU_TO_MP:
response_mcu_to_mp(data, len);
LOG_D("<DEBUG> PROT_CMD_MCU_TO_MP \r\n");
break;
case PROT_CMD_MP_TO_MCU:
response_mp_to_mcu(data, len);
LOG_D("<DEBUG> PROT_CMD_MP_TO_MCU \r\n");
break;
default:
break;
}
if ((data[0] & 0x80) != 0x80)
{
bsp_uart_reset_rxbuff(BOARD_UART_COMM);
}
}
void uart_ble_module_prot_dispose_data(uint8_t *data, uint16_t len)
{
if (data[0] != 0x55)
{
bsp_uart_reset_rxbuff(BOARD_UART_COMM);
return;
}
if (len == 2 && data[1] != 0x56)
{
bsp_uart_reset_rxbuff(BOARD_UART_COMM);
return;
}
if (len < data[3] + 5)
{
return;
}
uint8_t checksum = 0;
checksum = get_checksum(data + 2, len - 3);
if (checksum != data[len - 1])
{
LOG_E("<ERR> [prot_uart]: check fail %02x|%02x\r\n", checksum, data[len - 1]);
bsp_uart_reset_rxbuff(BOARD_UART_COMM);
return;
}
prot_dispose_data(data + 2, len - 3);
}
void nus_ble_module_prot_dispose_data(uint8_t *data, uint16_t len)
{
if (len < data[1] + 3)
{
return;
}
uint8_t checksum = 0;
checksum = get_checksum(data, len - 1);
if (checksum != data[len - 1])
{
LOG_E("<ERR> [prot_nus]: check fail %02x|%02x\r\n", checksum, data[len - 1]);
bsp_uart_reset_rxbuff(BOARD_UART_COMM);
return;
}
prot_dispose_data(data, len - 1);
}
头文件:
#ifndef __PROTOCOL_UART_BLE_MODULE_H
#define __PROTOCOL_UART_BLE_MODULE_H
#include <stdint.h>
#include <stdbool.h>
#include "business_function.h"
#define PROT_CMD_GET_DEVICE_INFO_REQ 0X01
#define PROT_CMD_GET_DEVICE_INFO_RES 0x02
#define PROT_CMD_GET_DEVICE_NAME_REQ 0x03
#define PROT_CMD_GET_DEVICE_NAME_RES 0x04
#define PROT_CMD_GET_BLE_VER_REQ 0x07
#define PROT_CMD_GET_BLE_VER_RES 0x08
#define PROT_CMD_GET_BAT_SOC_REQ 0x09
#define PROT_CMD_GET_BAT_SOC_RES 0x0A
#define PROT_CMD_GET_CHARG_REQ 0X0B
#define PROT_CMD_GET_CHARG_RES 0X0C
#define PROT_CMD_GET_BLE_ADDR_REQ 0X0D
#define PROT_CMD_GET_BLE_ADDR_RES 0X0E
#define PROT_CMD_BLE_IN_PAIRING_REQ 0X0F
#define PROT_CMD_BLE_IN_PAIRING_RES 0X10
#define PROT_CMD_BLE_WHITELIST_PAIRING_REQ 0X11
#define PROT_CMD_BLE_WHITELIST_PAIRING_RES 0X12
#define PROT_CMD_BLE_CONN_STATE_REQ 0X13
#define PROT_CMD_BLE_CONN_STATE_RES 0X14
#define PROT_CMD_BLE_SLEEP_REQ 0X15
#define PROT_CMD_CLEAR_BLE_PAIR_REQ 0X16
#define PROT_CMD_BLE_IN_DTM_MODE_REQ 0X17
#define PROT_CMD_BLE_IN_FIXED_FREQUENCY_REQ 0X18
#define PROT_CMD_SET_LED_STATE_REQ 0X19
#define PROT_CMD_SET_LED_STATE_RES 0X1A
#define PROT_CMD_GET_LED_STATE_REQ 0X1B
#define PROT_CMD_GET_LED_STATE_RES 0X1C
#define PROT_CMD_SET_BAT_INFO_REQ 0X1D
#define PROT_CMD_SET_BAT_INFO_RES 0X1F
#define PROT_CMD_MCU_TO_MP 0X70
#define PROT_CMD_MP_TO_MCU 0x71
#define PROT_CMD_BLE_HID_DATA_REQ 0x74
void uart_ble_module_prot_dispose_data(uint8_t *data, uint16_t len);
void nus_ble_module_prot_dispose_data(uint8_t *data, uint16_t len);
#endif
|