Browse Source

接收蓝牙数据并分包

master
zwsd 3 years ago
parent
commit
17faa9c83d
  1. 16
      .vscode/settings.json
  2. 183
      main/ble_spp_server_demo.c
  3. 36
      main/ble_spp_server_demo.h
  4. 79
      main/main.c
  5. 8
      sdkconfig
  6. 1463
      sdkconfig.old

16
.vscode/settings.json

@ -16,4 +16,20 @@
},
"terminal.integrated.defaultProfile.windows": "Windows PowerShell",
"files.autoGuessEncoding": true,
"files.associations": {
"string.h": "c",
"string_view": "c",
"regex": "c",
"*.tcc": "c",
"optional": "c",
"istream": "c",
"ostream": "c",
"ratio": "c",
"system_error": "c",
"array": "c",
"functional": "c",
"tuple": "c",
"type_traits": "c",
"utility": "c"
},
}

183
main/ble_spp_server_demo.c

@ -6,22 +6,27 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
#include "ble_spp_server_demo.h"
#include "driver/uart.h"
#include "esp_bt.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
#include "freertos/task.h"
#include "freertos/event_groups.h"
#include "esp_system.h"
#include "esp_log.h"
#include "freertos/task.h"
#include "nvs_flash.h"
#include "esp_bt.h"
#include "driver/uart.h"
#include "string.h"
#include "esp_gap_ble_api.h"
#include "esp_gatts_api.h"
#include "esp_bt_defs.h"
#include "esp_bt_main.h"
#include "ble_spp_server_demo.h"
static bleuart_t *s_ble_uart_init_struct;
static blerxcb_t s_cb;
static uint16_t table_conn_id_m;
static esp_gatt_if_t table_gatts_if_m;
static uint16_t table_handle_m;
#define GATTS_TABLE_TAG "GATTS_SPP_DEMO"
@ -49,8 +54,7 @@ static const uint8_t spp_adv_data[23] = {
/* Complete List of 16-bit Service Class UUIDs */
0x03, 0x03, 0xF0, 0xAB,
/* Complete Local Name in advertising */
0x0F,0x09, 'E', 'S', 'P', '_', 'S', 'P', 'P', '_', 'S', 'E', 'R','V', 'E', 'R'
};
0x0F, 0x09, 'E', 'S', 'P', '_', 'S', 'P', 'P', '_', 'S', 'E', 'R', 'V', 'E', 'R'};
static uint16_t spp_mtu_size = 23;
static uint16_t spp_conn_id = 0xffff;
@ -67,7 +71,9 @@ static uint8_t heartbeat_count_num = 0;
static bool enable_data_ntf = false;
static bool is_connected = false;
static esp_bd_addr_t spp_remote_bda = {0x0,};
static esp_bd_addr_t spp_remote_bda = {
0x0,
};
static uint16_t spp_handle_table[SPP_IDX_NB];
@ -110,19 +116,15 @@ typedef struct spp_receive_data_buff{
spp_receive_data_node_t *first_node;
} spp_receive_data_buff_t;
static spp_receive_data_buff_t SppRecvDataBuff = {
.node_num = 0,
.buff_size = 0,
.first_node = NULL
};
static spp_receive_data_buff_t SppRecvDataBuff = {.node_num = 0, .buff_size = 0, .first_node = NULL};
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param);
/* One gatt-based profile one app_id and one gatts_if, this array will store the gatts_if returned by ESP_GATTS_REG_EVT */
static struct gatts_profile_inst spp_profile_tab[SPP_PROFILE_NUM] = {
[SPP_PROFILE_APP_IDX] = {
.gatts_cb = gatts_profile_event_handler,
.gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
[SPP_PROFILE_APP_IDX] =
{
.gatts_cb = gatts_profile_event_handler, .gatts_if = ESP_GATT_IF_NONE, /* Not get the gatt_if, so initial is ESP_GATT_IF_NONE */
},
};
@ -169,83 +171,71 @@ static const uint8_t spp_heart_beat_ccc[2] = {0x00, 0x00};
#endif
/// Full HRS Database Description - Used to add attributes into the database
static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] =
{
static const esp_gatts_attr_db_t spp_gatt_db[SPP_IDX_NB] = {
// SPP - Service Declaration
[SPP_IDX_SVC] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ,
sizeof(spp_service_uuid), sizeof(spp_service_uuid), (uint8_t *)&spp_service_uuid}},
[SPP_IDX_SVC] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&primary_service_uuid, ESP_GATT_PERM_READ, sizeof(spp_service_uuid), sizeof(spp_service_uuid), (uint8_t *)&spp_service_uuid}},
// SPP - data receive characteristic Declaration
[SPP_IDX_SPP_DATA_RECV_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
[SPP_IDX_SPP_DATA_RECV_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
// SPP - data receive characteristic Value
[SPP_IDX_SPP_DATA_RECV_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_data_receive_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
SPP_DATA_MAX_LEN,sizeof(spp_data_receive_val), (uint8_t *)spp_data_receive_val}},
[SPP_IDX_SPP_DATA_RECV_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&spp_data_receive_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, SPP_DATA_MAX_LEN, sizeof(spp_data_receive_val),
(uint8_t *)spp_data_receive_val}},
// SPP - data notify characteristic Declaration
[SPP_IDX_SPP_DATA_NOTIFY_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}},
[SPP_IDX_SPP_DATA_NOTIFY_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}},
// SPP - data notify characteristic Value
[SPP_IDX_SPP_DATA_NTY_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_data_notify_uuid, ESP_GATT_PERM_READ,
SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}},
[SPP_IDX_SPP_DATA_NTY_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&spp_data_notify_uuid, ESP_GATT_PERM_READ, SPP_DATA_MAX_LEN, sizeof(spp_data_notify_val), (uint8_t *)spp_data_notify_val}},
// SPP - data notify characteristic - Client Characteristic Configuration Descriptor
[SPP_IDX_SPP_DATA_NTF_CFG] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
sizeof(uint16_t),sizeof(spp_data_notify_ccc), (uint8_t *)spp_data_notify_ccc}},
[SPP_IDX_SPP_DATA_NTF_CFG] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(spp_data_notify_ccc),
(uint8_t *)spp_data_notify_ccc}},
// SPP - command characteristic Declaration
[SPP_IDX_SPP_COMMAND_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
[SPP_IDX_SPP_COMMAND_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write}},
// SPP - command characteristic Value
[SPP_IDX_SPP_COMMAND_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
SPP_CMD_MAX_LEN,sizeof(spp_command_val), (uint8_t *)spp_command_val}},
[SPP_IDX_SPP_COMMAND_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&spp_command_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, SPP_CMD_MAX_LEN, sizeof(spp_command_val), (uint8_t *)spp_command_val}},
// SPP - status characteristic Declaration
[SPP_IDX_SPP_STATUS_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}},
[SPP_IDX_SPP_STATUS_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_notify}},
// SPP - status characteristic Value
[SPP_IDX_SPP_STATUS_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_status_uuid, ESP_GATT_PERM_READ,
SPP_STATUS_MAX_LEN,sizeof(spp_status_val), (uint8_t *)spp_status_val}},
[SPP_IDX_SPP_STATUS_VAL] = {{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_status_uuid, ESP_GATT_PERM_READ, SPP_STATUS_MAX_LEN, sizeof(spp_status_val), (uint8_t *)spp_status_val}},
// SPP - status characteristic - Client Characteristic Configuration Descriptor
[SPP_IDX_SPP_STATUS_CFG] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
sizeof(uint16_t),sizeof(spp_status_ccc), (uint8_t *)spp_status_ccc}},
[SPP_IDX_SPP_STATUS_CFG] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(spp_status_ccc),
(uint8_t *)spp_status_ccc}},
#ifdef SUPPORT_HEARTBEAT
// SPP - Heart beat characteristic Declaration
[SPP_IDX_SPP_HEARTBEAT_CHAR] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ,
CHAR_DECLARATION_SIZE,CHAR_DECLARATION_SIZE, (uint8_t *)&char_prop_read_write_notify}},
[SPP_IDX_SPP_HEARTBEAT_CHAR] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_declaration_uuid, ESP_GATT_PERM_READ, CHAR_DECLARATION_SIZE, CHAR_DECLARATION_SIZE,
(uint8_t *)&char_prop_read_write_notify}},
// SPP - Heart beat characteristic Value
[SPP_IDX_SPP_HEARTBEAT_VAL] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&spp_heart_beat_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
sizeof(spp_heart_beat_val), sizeof(spp_heart_beat_val), (uint8_t *)spp_heart_beat_val}},
[SPP_IDX_SPP_HEARTBEAT_VAL] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&spp_heart_beat_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(spp_heart_beat_val), sizeof(spp_heart_beat_val),
(uint8_t *)spp_heart_beat_val}},
// SPP - Heart beat characteristic - Client Characteristic Configuration Descriptor
[SPP_IDX_SPP_HEARTBEAT_CFG] =
{{ESP_GATT_AUTO_RSP}, {ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ|ESP_GATT_PERM_WRITE,
sizeof(uint16_t),sizeof(spp_data_notify_ccc), (uint8_t *)spp_heart_beat_ccc}},
[SPP_IDX_SPP_HEARTBEAT_CFG] = {{ESP_GATT_AUTO_RSP},
{ESP_UUID_LEN_16, (uint8_t *)&character_client_config_uuid, ESP_GATT_PERM_READ | ESP_GATT_PERM_WRITE, sizeof(uint16_t), sizeof(spp_data_notify_ccc),
(uint8_t *)spp_heart_beat_ccc}},
#endif
};
static uint8_t find_char_and_desr_index(uint16_t handle)
{
static uint8_t find_char_and_desr_index(uint16_t handle) {
uint8_t error = 0xff;
for (int i = 0; i < SPP_IDX_NB; i++) {
@ -257,8 +247,7 @@ static uint8_t find_char_and_desr_index(uint16_t handle)
return error;
}
static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data)
{
static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data) {
temp_spp_recv_data_node_p1 = (spp_receive_data_node_t *)malloc(sizeof(spp_receive_data_node_t));
if (temp_spp_recv_data_node_p1 == NULL) {
@ -284,8 +273,7 @@ static bool store_wr_buffer(esp_ble_gatts_cb_param_t *p_data)
return true;
}
static void free_write_buffer(void)
{
static void free_write_buffer(void) {
temp_spp_recv_data_node_p1 = SppRecvDataBuff.first_node;
while (temp_spp_recv_data_node_p1 != NULL) {
@ -300,8 +288,7 @@ static void free_write_buffer(void)
SppRecvDataBuff.first_node = NULL;
}
static void print_write_buffer(void)
{
static void print_write_buffer(void) {
temp_spp_recv_data_node_p1 = SppRecvDataBuff.first_node;
while (temp_spp_recv_data_node_p1 != NULL) {
@ -310,8 +297,7 @@ static void print_write_buffer(void)
}
}
void uart_task(void *pvParameters)
{
void uart_task(void *pvParameters) {
uart_event_t event;
uint8_t total_num = 0;
uint8_t current_num = 0;
@ -389,8 +375,7 @@ void uart_task(void *pvParameters)
vTaskDelete(NULL);
}
static void spp_uart_init(void)
{
static void spp_uart_init(void) {
uart_config_t uart_config = {
.baud_rate = 115200,
.data_bits = UART_DATA_8_BITS,
@ -411,8 +396,7 @@ static void spp_uart_init(void)
}
#ifdef SUPPORT_HEARTBEAT
void spp_heartbeat_task(void * arg)
{
void spp_heartbeat_task(void *arg) {
uint16_t cmd_id;
for (;;) {
@ -436,8 +420,7 @@ void spp_heartbeat_task(void * arg)
}
#endif
void spp_cmd_task(void * arg)
{
void spp_cmd_task(void *arg) {
uint8_t *cmd_id;
for (;;) {
@ -450,8 +433,7 @@ void spp_cmd_task(void * arg)
vTaskDelete(NULL);
}
static void spp_task_init(void)
{
static void spp_task_init(void) {
spp_uart_init();
#ifdef SUPPORT_HEARTBEAT
@ -463,8 +445,7 @@ static void spp_task_init(void)
xTaskCreate(spp_cmd_task, "spp_cmd_task", 2048, NULL, 10, NULL);
}
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param)
{
static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param_t *param) {
esp_err_t err;
ESP_LOGE(GATTS_TABLE_TAG, "GAP_EVT, event %d\n", event);
@ -483,8 +464,7 @@ static void gap_event_handler(esp_gap_ble_cb_event_t event, esp_ble_gap_cb_param
}
}
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
esp_ble_gatts_cb_param_t *p_data = (esp_ble_gatts_cb_param_t *)param;
uint8_t res = 0xff;
@ -544,7 +524,11 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
#ifdef SPP_DEBUG_MODE
esp_log_buffer_char(GATTS_TABLE_TAG, (char *)(p_data->write.value), p_data->write.len);
#else
uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len);
// uart_write_bytes(UART_NUM_0, (char *)(p_data->write.value), p_data->write.len);
if (s_ble_uart_init_struct->receive_data_processing_flag == false) {
s_cb(p_data->write.value, p_data->write.len);
}
#endif
} else {
// TODO:
@ -577,6 +561,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
case ESP_GATTS_STOP_EVT:
break;
case ESP_GATTS_CONNECT_EVT:
table_conn_id_m = param->connect.conn_id;
table_gatts_if_m = gatts_if;
table_handle_m = spp_handle_table[SPP_IDX_SPP_DATA_NTY_VAL];
spp_conn_id = p_data->connect.conn_id;
spp_gatts_if = gatts_if;
is_connected = true;
@ -587,6 +574,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
#endif
break;
case ESP_GATTS_DISCONNECT_EVT:
table_conn_id_m = 0;
table_gatts_if_m = 0;
table_handle_m = 0;
is_connected = false;
enable_data_ntf = false;
#ifdef SUPPORT_HEARTBEAT
@ -609,11 +599,9 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
ESP_LOGI(GATTS_TABLE_TAG, "The number handle =%x\n", param->add_attr_tab.num_handle);
if (param->add_attr_tab.status != ESP_GATT_OK) {
ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table failed, error code=0x%x", param->add_attr_tab.status);
}
else if (param->add_attr_tab.num_handle != SPP_IDX_NB){
} else if (param->add_attr_tab.num_handle != SPP_IDX_NB) {
ESP_LOGE(GATTS_TABLE_TAG, "Create attribute table abnormally, num_handle (%d) doesn't equal to HRS_IDX_NB(%d)", param->add_attr_tab.num_handle, SPP_IDX_NB);
}
else {
} else {
memcpy(spp_handle_table, param->add_attr_tab.handles, sizeof(spp_handle_table));
esp_ble_gatts_start_service(spp_handle_table[SPP_IDX_SVC]);
}
@ -624,9 +612,7 @@ static void gatts_profile_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_
}
}
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param)
{
static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_if, esp_ble_gatts_cb_param_t *param) {
ESP_LOGI(GATTS_TABLE_TAG, "EVT %d, gatts if %d\n", event, gatts_if);
/* If event is register event, store the gatts_if for each profile */
@ -652,11 +638,14 @@ static void gatts_event_handler(esp_gatts_cb_event_t event, esp_gatt_if_t gatts_
} while (0);
}
void ble_spp_server_demo_app_main(void)
{
void bleuart_reg_cb(blerxcb_t cb) { s_cb = cb; }
void ble_spp_server_demo_app_main(bleuart_t *bleuart) {
esp_err_t ret;
esp_bt_controller_config_t bt_cfg = BT_CONTROLLER_INIT_CONFIG_DEFAULT();
s_ble_uart_init_struct = bleuart;
// Initialize NVS
ret = nvs_flash_init();
if (ret == ESP_ERR_NVS_NO_FREE_PAGES || ret == ESP_ERR_NVS_NEW_VERSION_FOUND) {

36
main/ble_spp_server_demo.h

@ -6,9 +6,19 @@
CONDITIONS OF ANY KIND, either express or implied.
*/
/*
1.
2.()
3.
*/
#pragma once
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <stdbool.h>
/*
* DEFINES
@ -49,4 +59,28 @@ enum {
SPP_IDX_NB,
};
void ble_spp_server_demo_app_main(void);
typedef struct {
uint8_t *rxbuf;
size_t rxbufsize;
uint8_t *txbuf;
size_t txbufsize;
size_t rxpacketReceiveOvertime;
bool receive_data_processing_flag;
int maxClientNum;
const char *bleName;
} bleuart_t;
/*接收完整JSON*/
typedef void (*blerxcb_t)(uint8_t *rx, size_t rxsize);
void ble_spp_server_demo_app_main(bleuart_t *bleuart);
void bleuart_reg_cb(blerxcb_t cb);
void bleuart_send_packet(uint8_t *tx, size_t txsize);
/**
* @brief
*/
void bleuart_schedule();

79
main/main.c

@ -1,3 +1,5 @@
#include "driver/gpio.h"
#include "driver/timer.h"
#include "esp_log.h"
#include "esp_system.h"
#include "freertos/FreeRTOS.h"
@ -5,33 +7,76 @@
#include "freertos/task.h"
#include "nvs_flash.h"
#include "string.h"
#include "driver/gpio.h"
//
#include "ble_spp_server_demo.h"
#include "motor_drive.h"
#define ble_uart_tx_size 128
#define ble_uart_rx_size 128
#define Electric_relay1 25
#define Electric_relay2 26
static uint8_t bluetooth_tx_buffer[ble_uart_tx_size] = {0};
static uint8_t bluetooth_rx_buffer[ble_uart_rx_size] = {0};
#define GPIO_OUTPUT_PIN_SEL ((1ULL << Electric_relay1) | (1ULL << Electric_relay2))
uint8_t ble_rx_buffer_now_len = 0;
uint8_t ble_rx_buffer_off = 0;
uint32_t start_time = 0;
uint16_t modbus_uart_rx_off_before = 0;
void gpio_rgb_init() {
gpio_config_t gpio_grb_led_structer;
bleuart_t ble_uart_init_struct = {
.txbuf = bluetooth_tx_buffer,
.txbufsize = sizeof(bluetooth_tx_buffer),
.rxbuf = bluetooth_rx_buffer,
.rxbufsize = sizeof(bluetooth_rx_buffer),
.rxpacketReceiveOvertime = 200,
.receive_data_processing_flag = false,
.maxClientNum = 1,
.bleName = "yimei_ble",
};
void blerxcb(uint8_t *rxmessage, size_t rxsize) {
if ((rxsize + ble_rx_buffer_now_len) > ble_uart_init_struct.rxbufsize) {
ESP_LOGW("MAIN", "rx buffer overstep");
return;
}
for (int i = 0; i < rxsize; i++) {
ble_uart_init_struct.rxbuf[ble_rx_buffer_now_len++] = rxmessage[i];
ble_rx_buffer_off++;
start_time = esp_log_timestamp();
}
}
gpio_grb_led_structer.intr_type = GPIO_INTR_DISABLE;
gpio_grb_led_structer.mode = GPIO_MODE_OUTPUT;
gpio_grb_led_structer.pin_bit_mask = GPIO_OUTPUT_PIN_SEL;
gpio_grb_led_structer.pull_down_en = 0;
gpio_grb_led_structer.pull_up_en = 0;
void buffer_all_init() {
ble_rx_buffer_now_len = 0;
memset(ble_uart_init_struct.rxbuf, 0, ble_uart_init_struct.rxbufsize);
}
gpio_config(&gpio_grb_led_structer);
void wait_ble_uart_receive_ms(uint16_t wait_time_ms) {
start_time = esp_log_timestamp();
while ((esp_log_timestamp() - start_time) < wait_time_ms) {
}
}
void app_main(void) {
// ble_spp_server_demo_app_main();
gpio_rgb_init();
ble_spp_server_demo_app_main(&ble_uart_init_struct);
bleuart_reg_cb(blerxcb);
gpio_set_level(Electric_relay1,0);
gpio_set_level(Electric_relay2,0);
while (true) {
if (ble_rx_buffer_off != 0) {
modbus_uart_rx_off_before = ble_rx_buffer_off;
return;
wait_ble_uart_receive_ms(100);
if (ble_rx_buffer_off == modbus_uart_rx_off_before) {
ble_uart_init_struct.receive_data_processing_flag = true;
ESP_LOGI("MAIN", "%s", ble_uart_init_struct.rxbuf);
}
if (ble_uart_init_struct.receive_data_processing_flag == true) {
buffer_all_init();
ble_uart_init_struct.receive_data_processing_flag = false;
ble_rx_buffer_off = 0;
}
}
}
}

8
sdkconfig

@ -757,8 +757,8 @@ CONFIG_ESP_INT_WDT_CHECK_CPU1=y
CONFIG_ESP_TASK_WDT=y
# CONFIG_ESP_TASK_WDT_PANIC is not set
CONFIG_ESP_TASK_WDT_TIMEOUT_S=5
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU0 is not set
# CONFIG_ESP_TASK_WDT_CHECK_IDLE_TASK_CPU1 is not set
# CONFIG_ESP_PANIC_HANDLER_IRAM is not set
CONFIG_ESP_SYSTEM_CHECK_INT_LEVEL_5=y
# end of ESP System Settings
@ -1757,8 +1757,8 @@ CONFIG_INT_WDT_CHECK_CPU1=y
CONFIG_TASK_WDT=y
# CONFIG_TASK_WDT_PANIC is not set
CONFIG_TASK_WDT_TIMEOUT_S=5
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0=y
CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1=y
# CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU0 is not set
# CONFIG_TASK_WDT_CHECK_IDLE_TASK_CPU1 is not set
CONFIG_TIMER_TASK_STACK_SIZE=3584
CONFIG_SW_COEXIST_ENABLE=y
# CONFIG_ESP32_ENABLE_COREDUMP_TO_FLASH is not set

1463
sdkconfig.old
File diff suppressed because it is too large
View File

Loading…
Cancel
Save