From bca497ffc71047563efdfda4a3ac8c9a06da10f1 Mon Sep 17 00:00:00 2001 From: zwsd Date: Fri, 22 Jul 2022 15:10:06 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0=E4=BA=86=E4=B8=B2=E5=8F=A3?= =?UTF-8?q?=E5=88=9D=E5=A7=8B=E5=8C=96?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- .vscode/settings.json | 8 +++++++- main/CMakeLists.txt | 1 + main/ble_uart.c | 27 +++++++++++++++++++++++++++ main/ble_uart.h | 9 +++++++++ main/main.c | 9 ++++++++- 5 files changed, 52 insertions(+), 2 deletions(-) create mode 100644 main/ble_uart.c create mode 100644 main/ble_uart.h diff --git a/.vscode/settings.json b/.vscode/settings.json index fabb43d..466cf40 100644 --- a/.vscode/settings.json +++ b/.vscode/settings.json @@ -25,6 +25,12 @@ "timer_u.h": "c", "task.h": "c", "string.h": "c", - "motor_drive.h": "c" + "motor_drive.h": "c", + "stdio.h": "c", + "stdlib.h": "c", + "esp_system.h": "c", + "esp_log.h": "c", + "ble_uart.h": "c", + "uart.h": "c" }, } \ No newline at end of file diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index 2753f64..7a8b1d0 100644 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -5,6 +5,7 @@ idf_component_register(SRCS # "timer_u.c" "ble_parse_data.c" "motor_drive.c" + "ble_uart.c" INCLUDE_DIRS # "../dep/" ".") diff --git a/main/ble_uart.c b/main/ble_uart.c new file mode 100644 index 0000000..1a8e031 --- /dev/null +++ b/main/ble_uart.c @@ -0,0 +1,27 @@ +#include "ble_uart.h" +#define buffer_size 1024 +uint8_t data[128]; +int length = 0; + +void ble_uart_init(uart_port_t uart_num, int tx_io_num, int rx_io_num) { + uart_config_t uart_config = { + .baud_rate = 115200, + .data_bits = UART_DATA_8_BITS, + .parity = UART_PARITY_DISABLE, + .stop_bits = UART_STOP_BITS_1, + .flow_ctrl = UART_HW_FLOWCTRL_DISABLE, + .source_clk = UART_SCLK_APB, + }; + ESP_ERROR_CHECK(uart_driver_install(uart_num, buffer_size * 2, 0, 0, NULL, 0)); + + ESP_ERROR_CHECK(uart_param_config(uart_num, &uart_config)); + + ESP_ERROR_CHECK(uart_set_pin(uart_num, tx_io_num, rx_io_num, UART_PIN_NO_CHANGE, UART_PIN_NO_CHANGE)); +} + +void ble_receive(uart_port_t uart_num) { + ESP_ERROR_CHECK(uart_get_buffered_data_len(uart_num, (size_t*)&length)); + if (length != 0) { + length = uart_read_bytes(uart_num, data, length, 100); + } +} diff --git a/main/ble_uart.h b/main/ble_uart.h new file mode 100644 index 0000000..585fd87 --- /dev/null +++ b/main/ble_uart.h @@ -0,0 +1,9 @@ +#pragma once +#include +#include +#include + +#include "driver/uart.h" + +void ble_uart_init(uart_port_t uart_num, int tx_io_num, int rx_io_num); +void ble_receive(uart_port_t uart_num); \ No newline at end of file diff --git a/main/main.c b/main/main.c index d1dfbac..70b38c8 100644 --- a/main/main.c +++ b/main/main.c @@ -8,6 +8,7 @@ #include "string.h" // #include "ble_spp_server_demo.h" +#include "ble_uart.h" #include "motor_drive.h" #include "timer_u.h" @@ -51,7 +52,9 @@ void app_main(void) { constructor_bluetooth_processer(&s_bluetooth_processer); ble_spp_server_demo_app_main(&s_bluetooth_processer); timer_group_init(TIMER_GROUP_0, TIMER_0, false, timer_group0_interval_num, timer_interval_ms); - motor_drive_uart_init(); + // motor_drive_uart_init(); + // ble_uart_init(UART_NUM_2, 4, 5); + // char* test_str = "This is a test string.\n"; while (true) { bluetooth_gatts_try_process_data(); @@ -64,6 +67,10 @@ void app_main(void) { ESP_LOGI("test", "info log ok\n"); s_bluetooth_processer.motor_drive_turn_flag = false; } + + // uart_write_bytes(UART_NUM_2, (const char*)test_str, strlen(test_str)); + // ets_delay_us(1000000); + // ble_receive(UART_NUM_2); } return;