From c704bbd24b3da750ac07902c79d56687f1e0a72b Mon Sep 17 00:00:00 2001 From: OPTOC <9159397+optoc@user.noreply.gitee.com> Date: Wed, 20 Aug 2025 11:28:11 +0800 Subject: [PATCH] =?UTF-8?q?=E5=AE=9E=E7=8E=B0=E5=9F=BA=E6=9C=AC=E4=B8=B2?= =?UTF-8?q?=E5=8F=A3=E5=9B=9E=E4=BC=A0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- app/CMakeLists.txt | 6 +- app/app_main.c | 6 +- .../data_port/ble_spp/ble_spp_server_shell.c | 6 +- app/drivers/sertrf/device.c | 117 ++++++++++++++++ app/drivers/sertrf/device.h | 131 ++++++++++++++++++ app/drivers/sertrf/sertrf.c | 41 ++++++ app/drivers/sertrf/sertrf.h | 37 +++++ 7 files changed, 338 insertions(+), 6 deletions(-) create mode 100644 app/drivers/sertrf/device.c create mode 100644 app/drivers/sertrf/device.h create mode 100644 app/drivers/sertrf/sertrf.c create mode 100644 app/drivers/sertrf/sertrf.h diff --git a/app/CMakeLists.txt b/app/CMakeLists.txt index b1f857c..79ab790 100644 --- a/app/CMakeLists.txt +++ b/app/CMakeLists.txt @@ -7,7 +7,7 @@ list(APPEND incs "../components/system/source/shell") list(APPEND incs "drivers/data_port/usb-host") list(APPEND incs "drivers/data_port/ble_spp") list(APPEND incs "drivers/data_port/socket_inet") - +list(APPEND incs "drivers/sertrf") list(APPEND srcs "app_main.c") @@ -26,6 +26,10 @@ list(APPEND srcs "config/app_log.c") list(APPEND srcs "utils/crc.c") list(APPEND srcs "utils/sb_aes.c") + +list(APPEND srcs "drivers/sertrf/sertrf.c") +list(APPEND srcs "drivers/sertrf/device.c") + if(CONFIG_BUILD_BLE) list(APPEND srcs "drivers/data_port/ble_spp/ble_spp_server.c") list(APPEND srcs "drivers/data_port/ble_spp/ble_spp_server_shell.c") diff --git a/app/app_main.c b/app/app_main.c index 4e8dbb5..b89d35f 100644 --- a/app/app_main.c +++ b/app/app_main.c @@ -20,7 +20,7 @@ /* 驱动 */ #include "drivers/ws2812_spi/ws2812_spi.h" #include "drivers/ws2812_spi/ws2812_spi_shell.h" - +#include "drivers/sertrf/sertrf.h" /* 通用模块 */ #include "button/button.h" @@ -66,7 +66,9 @@ void work_app_main(void *arg) // lib_shell_register(); // 静态库接口命令 /* 初始化按键检测和控制 */ - btn_attach(&g_cfg_board->key_reset, _change_mode_event_button, EVENT_PRESS_UP | EVENT_PRESS_DOWN | EVENT_LONG_PRESS_HOLD); + // btn_attach(&g_cfg_board->key_reset, _change_mode_event_button, EVENT_PRESS_UP | EVENT_PRESS_DOWN | EVENT_LONG_PRESS_HOLD); + + sertrf_init(); /* 启动 shell */ os_thread_sleep(100); diff --git a/app/drivers/data_port/ble_spp/ble_spp_server_shell.c b/app/drivers/data_port/ble_spp/ble_spp_server_shell.c index 7675857..3ba3eb2 100644 --- a/app/drivers/data_port/ble_spp/ble_spp_server_shell.c +++ b/app/drivers/data_port/ble_spp/ble_spp_server_shell.c @@ -64,13 +64,13 @@ void ble_server_shell_register(void) sb_data_port_start(s_cm.port_ble_cmd); sb_data_port_start(s_cm.port_ble_val); - + // ble cmd通道读取 os_work_create(&s_cm.s_work_hdl_cmd, "work-read", _work_read_handler, s_cm.port_ble_cmd, OS_PRIORITY_LOWEST); os_work_submit(default_os_work_q_hdl, &s_cm.s_work_hdl_cmd, 0); - + // ble 数据通道读取 os_work_create(&s_cm.s_work_hdl_val, "work-read", _work_read_handler, s_cm.port_ble_val, OS_PRIORITY_LOWEST); os_work_submit(default_os_work_q_hdl, &s_cm.s_work_hdl_val, 0); - + // 速度 os_work_create(&s_cm.s_speed_work_hdl, "work-speed", _work_speed_handler, NULL, OS_PRIORITY_LOWEST); os_work_submit(default_os_work_q_hdl, &s_cm.s_speed_work_hdl, 0); } diff --git a/app/drivers/sertrf/device.c b/app/drivers/sertrf/device.c new file mode 100644 index 0000000..2a3ed68 --- /dev/null +++ b/app/drivers/sertrf/device.c @@ -0,0 +1,117 @@ +#include "device.h" +#include "config/board_config.h" + +uint8_t device_init(device_t *port) +{ + uint8_t res = DEVICE_OK; +#if UART_ENABLE + res = uart_init(&port->init_device); + if(res) + { + SYS_LOG_WRN("device start uart error"); + return DEVICE_UART_ERROR; + } +#endif +#if BLE_ENABLE + res = ble_init(&port->init_device); + if(res) + { + SYS_LOG_WRN("ble init error"); + return DEVICE_BLE_ERROR; + } +#endif +#if WIFI_ENABLE + res = wifi_init(&port->init_device); + if(res) + { + SYS_LOG_WRN("wifi init error"); + return DEVICE_WIFI_ERROR; + } +#endif + //默认选择串口 + res = embedded_device_choice(port, DATA_PORT_TYPE_UART); + if(res == DEVICE_EMBEDDED_ERROR) + { + SYS_LOG_ERR("embedded device choice error"); + } + + return DEVICE_OK; +} +uint8_t uart_init(init_device_t *port) +{ + // port->uart_port = sb_uart_port_bind(g_cfg_board->uart_fc.id, g_cfg_board->uart_fc.br, g_cfg_board->uart_fc.pin_txd.pin, g_cfg_board->uart_fc.pin_rxd.pin, g_cfg_board->uart_fc.irq_prior, 1024, 0, NULL); + port->uart_port = sb_uart_port_bind(1, 115200, 0, 10, 21, 1024, 0, NULL); + + if(port->uart_port == NULL) + { + SYS_LOG_WRN("uart init error"); + return 1; + } + if(sb_data_port_start(port->uart_port)) + { + SYS_LOG_WRN("uart start error"); + return 1; + } + return DEVICE_OK; +} + +uint8_t ble_init(init_device_t *port) +{ + return DEVICE_OK; +} + +uint8_t wifi_init(init_device_t *port) +{ + return DEVICE_OK; +} + +uint8_t embedded_device_choice(device_t *port, uint8_t type) +{ + //端口与PC一致 + if(port->pc_device_type == type) + { + SYS_LOG_WRN("pc Port consistency"); + return DEVICE_WRN_EMBEDDED_TYPE; + } + + port->embedded_device_type = type; + switch (port->embedded_device_type) + { + case DATA_PORT_TYPE_UART: + port->embedded_device = port->init_device.uart_port; + break; + case DATA_PORT_TYPE_WIFI: + port->embedded_device = port->init_device.wifi; + break; + case DATA_PORT_TYPE_BLE_CMD: + port->embedded_device = port->init_device.ble_spp_server_cmd; + break; + case DATA_PORT_TYPE_BLE_VAL: + port->embedded_device = port->init_device.ble_spp_server_val; + break; + default: + break; + } + + if(port->embedded_device == NULL) + { + SYS_LOG_WRN("embedded device choice error"); + return DEVICE_EMBEDDED_ERROR; + } + + return DEVICE_OK; +} + +int embedded_device_read(device_t *port, void *buffer, uint32_t length) +{ + return sb_data_port_read(port->embedded_device, buffer, length, 0); +} + +int embedded_device_write(device_t *port, void *buffer, uint32_t length) +{ + return sb_data_port_write(port->embedded_device, buffer, length, 0); +} +uint32_t embedded_device_get_rx_length(device_t *port) +{ + return sb_data_port_get_rx_length(port->embedded_device); +} \ No newline at end of file diff --git a/app/drivers/sertrf/device.h b/app/drivers/sertrf/device.h new file mode 100644 index 0000000..17187d8 --- /dev/null +++ b/app/drivers/sertrf/device.h @@ -0,0 +1,131 @@ +#pragma once + +#include "../data_port/uart/uart_port.h" +#include "../data_port/sb_data_port.h" +#include "../data_port/ble_spp/ble_spp_server.h" +#include "../data_port/ble_spp/ble_spp_client.h" +#include "../data_port/socket_inet/socket_inet_server_shell.h" +#include "../data_port/socket_inet/wifi.h" +#include "sys_log.h" + +#define UART_ENABLE 1 +#define BLE_ENABLE 0 +#define WIFI_ENABLE 0 + +enum DEVICE_ERROR +{ + DEVICE_OK = 0, + DEVICE_UART_ERROR, + DEVICE_BLE_ERROR, + DEVICE_WIFI_ERROR, + DEVICE_EMBEDDED_ERROR, + DEVICE_PC_ERROR, + + DEVICE_WRN_EMBEDDED_TYPE = 110, + DEVICE_WRN_PC_TYPE + +}; +enum DATA_PORT_TYPE +{ + DATA_PORT_TYPE_UART = 1, + DATA_PORT_TYPE_WIFI, + DATA_PORT_TYPE_BLE_CMD, + DATA_PORT_TYPE_BLE_VAL +}; +typedef struct +{ + sb_data_port_t* uart_port; + sb_data_port_t* ble_spp_server_cmd; + sb_data_port_t* ble_spp_server_val; + sb_data_port_t* ble_spp_client_cmd; + sb_data_port_t* ble_spp_client_val; + sb_data_port_t* wifi; +}init_device_t; + +typedef struct +{ + uint8_t embedded_device_type; + uint8_t pc_device_type; + sb_data_port_t* embedded_device; + sb_data_port_t* pc_device; + init_device_t init_device; + + // int (*embedded_read)(device_t *port, void *buffer, uint32_t length); + // int (*embedded_write)(device_t *port, const void *data, uint32_t size); + + +}device_t; + +/** + * @brief 初始化设备 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @retval 0 成功 + */ +uint8_t device_init(device_t *port); +/** + * @brief UART 初始化 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @retval 0 成功 + */ +uint8_t uart_init(init_device_t *port); +/** + * @brief 蓝牙 初始化 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @retval 0 成功 + */ +uint8_t ble_init(init_device_t *port); +/** + * @brief WIFI 初始化 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @retval 0 成功 + */ +uint8_t wifi_init(init_device_t *port); +/** + * @brief 嵌入式设备数据接口 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @param type 对应的数据接口类型 + * @retval 0 成功 + */ +uint8_t embedded_device_choice(device_t *port, uint8_t type); +/** + * @brief PC OR 手机数据接口 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @param type 对应的数据接口类型 + * @retval 0 成功 + */ +uint8_t pc_device_choice(device_t *port, uint8_t type); +/** + * @brief 嵌入式设备数据接口读操作 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @param buffer 读取数据缓存 + * @param length 读取数据长度 + * @retval < 0 操作失败或在指定的时间内未满足指定的操作长度 + * @retval >= 0 实际已缓存的长度 + */ +int embedded_device_read(device_t *port, void *buffer, uint32_t length); +/** + * @brief 嵌入式设备数据接口写操作 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @param data 写入数据 + * @param size 写入数据长度 + * @retval < 0 操作失败或在指定的时间内未满足指定的操作长度 + * @retval >= 0 实际已缓存的长度 + */ +int embedded_device_write(device_t *port, void *buffer, uint32_t length); +/** + * @brief 嵌入式设备数据接口写操作 + * + * @param port 由对应的驱动提供的绑定接口获得的句柄 + * @param data 写入数据 + * @param size 写入数据长度 + * @retval 缓存中可读取的字节数 + */ +uint32_t embedded_device_get_rx_length(device_t *port); \ No newline at end of file diff --git a/app/drivers/sertrf/sertrf.c b/app/drivers/sertrf/sertrf.c new file mode 100644 index 0000000..099b4d7 --- /dev/null +++ b/app/drivers/sertrf/sertrf.c @@ -0,0 +1,41 @@ +#include "sertrf.h" + + +sertrf_t sertrf; +void sertrf_init(void) +{ + uint8_t res = 0; + res = device_init(&sertrf.device); + if(!res) + { + SYS_LOG_WRN("device init error"); + } + + sertrf_start(); +} +void sertrf_start(void) +{ + os_thread_create(&sertrf.embedded_thread, + "embedded_thread", + embedded_thread, + NULL, + 4096, + 20); +} +void embedded_thread(void* arg) +{ + while(true) + { + uint32_t embedded_size = embedded_device_get_rx_length(&sertrf.device); + if(embedded_size > 0) + { + uint8_t data[embedded_size]; + embedded_device_read(&sertrf.device, data, embedded_size); + SYS_LOG_INF("data : %s", data); + embedded_device_write(&sertrf.device, data, embedded_size); + } + //需要添加一些延时,否则会卡死,导致看门狗复位 + // vTaskDelay(( TickType_t ) 1000 / configTICK_RATE_HZ); + os_thread_sleep(1); + } +} \ No newline at end of file diff --git a/app/drivers/sertrf/sertrf.h b/app/drivers/sertrf/sertrf.h new file mode 100644 index 0000000..4fd7eea --- /dev/null +++ b/app/drivers/sertrf/sertrf.h @@ -0,0 +1,37 @@ +#pragma once + +#include "device.h" + + +typedef struct +{ + device_t device; + os_thread_t embedded_thread; + +}sertrf_t; + +/** + * @brief 模块初始化 +*/ +void sertrf_init(void); + +/** + * @brief 模块启动 +*/ +void sertrf_start(void); + +/** + * @brief 模块停止 +*/ +void sertrf_stop(void); + +/** + * @brief 模块数据查看 +*/ +void sertrf_status(void); + +/** + * @brief embedded thread +*/ +void embedded_thread(void* arg); +