diff --git a/main/APP/README.md b/main/APP/README.md index e69de29..0d410b5 100644 --- a/main/APP/README.md +++ b/main/APP/README.md @@ -0,0 +1,2 @@ +每个APP调用不同的service来组成不同的功能 +module间通过消息队列进行功能 \ No newline at end of file diff --git a/main/APP/main_app/main_app.c b/main/APP/main_app/main_app.c index 5bcca77..e14d1ae 100644 --- a/main/APP/main_app/main_app.c +++ b/main/APP/main_app/main_app.c @@ -33,15 +33,19 @@ #include "main_common.h" #include "shell_port.h" +#include "imp_msg_queue.h" +#include "ext_trans_service.h" + /* define --------------------------------------------------------------------*/ /* typedef -------------------------------------------------------------------*/ /* variables -----------------------------------------------------------------*/ -static uint8_t print_en = 0; +static uint8_t print_en = 0; +static imp_msg_queue_t* msg_q_handle = NULL; /* Private function(only *.c) -----------------------------------------------*/ -uint8_t _set_print_en(int argc, char **argv) +uint8_t _set_print_en(int argc, char** argv) { if (argc < 2) { cdc_printf("too few argv\r\n"); @@ -50,14 +54,50 @@ uint8_t _set_print_en(int argc, char **argv) print_en = en_v; return 0; } + +uint8_t _set_send_msg_to(int argc, char** argv) +{ + if (argc < 3) { + cdc_printf("too few argv: cmd sendtoid value_num\r\n"); + } + uint16_t send_to_id = (uint16_t)atoi(argv[1]); + int value_num = atoi(argv[2]); + + imp_msg_item_t msg_item = { 0 }; + msg_item.msg_data = value_num; + imp_msg_queue_send_msg(msg_q_handle, send_to_id, &msg_item); + cdc_printf("send from %d to %d value %d ok\r\n", msg_item.send_id, + msg_item.recv_id, msg_item.msg_data); + return 0; +} + +uint8_t _main_task_wake_up_services() +{ + xTaskCreate(imp_ext_trans_service_task, + imp_main_task_table[IMP_TASK_ID_EXT_TRANS_SERVICE_TASK], 2048, + NULL, 10, NULL); + return 0; +} + /* Exported functions --------------------------------------------------------*/ uint8_t imp_main_app_task() { - int i = 0; + uint16_t i = 0; + msg_q_handle = imp_msg_queue_create_handle(IMP_TASK_ID_MAIN_TASK); + imp_msg_item_t msg_item = { 0 }; + + _main_task_wake_up_services(); + while (1) { if (print_en) { - cdc_printf("hello %d\r\n", i++); + cdc_printf("hello %d\r\n", i); + } + + if (i++ % 4 == 0) { + msg_item.msg_data = i; + imp_msg_queue_send_msg( + msg_q_handle, IMP_TASK_ID_EXT_TRANS_SERVICE_TASK, &msg_item); } vTaskDelay(1000 / portTICK_PERIOD_MS); @@ -67,6 +107,8 @@ uint8_t imp_main_app_task() SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), main_pe, _set_print_en, set print value); +SHELL_EXPORT_CMD(SHELL_CMD_PERMISSION(0) | SHELL_CMD_TYPE(SHELL_TYPE_CMD_MAIN), + main_msg_to, _set_send_msg_to, send msg to other task); /* * EOF diff --git a/main/APP/main_app/main_app.h b/main/APP/main_app/main_app.h index f6bce4d..6082536 100644 --- a/main/APP/main_app/main_app.h +++ b/main/APP/main_app/main_app.h @@ -29,6 +29,7 @@ extern "C" { /* Includes ------------------------------------------------------------------*/ #include +#include "main_common.h" /* define --------------------------------------------------------------------*/ /* typedef -------------------------------------------------------------------*/ diff --git a/main/CMakeLists.txt b/main/CMakeLists.txt index ba97d06..5360c41 100755 --- a/main/CMakeLists.txt +++ b/main/CMakeLists.txt @@ -12,6 +12,8 @@ idf_component_register( "utilities/imp_util_ring_queue/" "utilities/imp_types/" "APP/main_app/" + "components/msq_queue" + "services/ext_trans_service" EXCLUDE_SRCS "utilities/usb_cherry/CherryUSB/class/cdc/usbh_cdc_acm.c" @@ -34,6 +36,8 @@ idf_component_register( "utilities/imp_util_ring_queue/" "APP/main_app/" "utilities/imp_types/" + "components/msq_queue" + "services/ext_trans_service" LDFRAGMENTS "utilities/letter_shell/port/esp-idf/shell.lf" diff --git a/main/components/README.md b/main/components/README.md new file mode 100644 index 0000000..ca38801 --- /dev/null +++ b/main/components/README.md @@ -0,0 +1 @@ +component是 \ No newline at end of file diff --git a/main/components/msq_queue/imp_msg_queue.c b/main/components/msq_queue/imp_msg_queue.c new file mode 100644 index 0000000..7df4959 --- /dev/null +++ b/main/components/msq_queue/imp_msg_queue.c @@ -0,0 +1,124 @@ +/** + * @file imp_msg_queue.c + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2024-12-04 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2024-12-04 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ +#include "imp_msg_queue.h" +#include "main_common.h" +#include +#include +/* define --------------------------------------------------------------------*/ +/* typedef -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ + +/// @brief 消息队列的freeRTOS句柄 +static QueueHandle_t sg_imp_msg_queue = NULL; + +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ + +uint8_t imp_msg_queue_init() +{ + sg_imp_msg_queue = + xQueueCreate(IMP_MSG_QUEUE_MAX_ITEM_COUNT, sizeof(imp_msg_item_t)); + if (sg_imp_msg_queue != NULL) { + cdc_printf("msg queue init ok!\n"); + } else { + cdc_printf("msg queue init failed!\n"); + return 1; + } + return 0; +} + +imp_msg_queue_t* imp_msg_queue_create_handle(uint16_t task_id) +{ + if (NULL == sg_imp_msg_queue) { + cdc_printf("msg queue create failed!\n"); + return NULL; + } + imp_msg_queue_t* ptr = (imp_msg_queue_t*)malloc(sizeof(imp_msg_queue_t*)); + ptr->is_init = 1; + ptr->msg_queue_handle = sg_imp_msg_queue; + ptr->task_id = task_id; + ptr->task_name = imp_main_task_table[task_id]; + return ptr; +} + +uint8_t imp_msg_queue_recv_msg(imp_msg_queue_t* handle, imp_msg_item_t* msg, + uint16_t timeout) +{ + if (NULL == sg_imp_msg_queue || NULL == handle) { + cdc_printf("handle or msg queue is NULL\n"); + return 1; + } + BaseType_t msg_queue_return; + imp_msg_item_t item = { 0 }; + + msg_queue_return = xQueuePeek(handle->msg_queue_handle, &item, timeout); + if (msg_queue_return != pdTRUE) { + return 2; + } else { + // read succeed + if (item.recv_id == handle->task_id) { + xQueueReceive(handle->msg_queue_handle, msg, 0); + if (msg->recv_id == handle->task_id) { + return 0; + } + return 3; + } + } + return 4; +} + +uint8_t imp_msg_queue_send_msg(imp_msg_queue_t* handle, uint16_t recv_id, + imp_msg_item_t* msg) +{ + if (NULL == sg_imp_msg_queue || NULL == handle) { + cdc_printf("handle or msg queue is NULL\n"); + return 1; + } + BaseType_t msg_queue_return; + + msg->send_id = handle->task_id; + msg->recv_id = recv_id; + msg_queue_return = xQueueSend(handle->msg_queue_handle, msg, portMAX_DELAY); + + return 0; +} + +uint8_t imp_msg_queue_destroy_handle(imp_msg_queue_t* handle) +{ + if (NULL == sg_imp_msg_queue || NULL == handle) { + cdc_printf("handle or msg queue is NULL\n"); + return 1; + } + + handle->is_init = 0; + handle->msg_queue_handle = NULL; + handle->task_name = NULL; + handle->task_id = 0; + free(handle); + return 0; +} + +/* + * EOF + */ \ No newline at end of file diff --git a/main/components/msq_queue/imp_msg_queue.h b/main/components/msq_queue/imp_msg_queue.h new file mode 100644 index 0000000..9c6f0f8 --- /dev/null +++ b/main/components/msq_queue/imp_msg_queue.h @@ -0,0 +1,174 @@ +/** + * @file imp_msg_queue.h + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2024-12-03 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2024-12-03 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __IMP_MSG_QUEUE_H__ +#define __IMP_MSG_QUEUE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ + +#include +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "freertos/queue.h" + +/* define --------------------------------------------------------------------*/ + +#define IMP_MSG_QUEUE_MAX_ITEM_COUNT (20) +/* typedef -------------------------------------------------------------------*/ + +/// @brief 消息队列的消息内容 +typedef struct __imp_msg_item_t__ +{ + uint16_t send_id; + uint16_t recv_id; + uint16_t msg_type; + union { + uint32_t msg_data; + void* msg_data_ptr; + }; + union { + uint32_t msg_reserve; + uint32_t msg_data_len; + }; +} imp_msg_item_t; + +/// @brief 消息队列的句柄 +typedef struct __imp_msg_queue_t__ +{ + uint8_t is_init; + /// @brief 唯一的值代表不同的Task + uint16_t task_id; + char* task_name; + /// @brief 通过create 获取消息队列句柄 + QueueHandle_t msg_queue_handle; +} imp_msg_queue_t; + +/* variables -----------------------------------------------------------------*/ +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ + +/** + * + * @brief 初始化消息队列 + * @return uint8_t + * @date 2024-12-04 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2024-12-04 Alvin Young 新建 + *
+ */ +uint8_t imp_msg_queue_init(); + +/** + * + * @brief 获取一个消息队列控制句柄 + * @param [in] task_id + * @return imp_msg_queue_t* + * @date 2024-12-04 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2024-12-04 Alvin Young 新建 + *
+ */ +imp_msg_queue_t* imp_msg_queue_create_handle(uint16_t task_id); + +/** + * + * @brief 接收消息 + * @param [in] handle + * @param [in] msg + * @return uint8_t + * @date 2024-12-04 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2024-12-04 Alvin Young 新建 + *
+ */ +uint8_t imp_msg_queue_recv_msg(imp_msg_queue_t* handle, imp_msg_item_t* msg, + uint16_t timeout); + +/** + * + * @brief 发送消息 + * @param [in] handle + * @param [in] recv_id + * @param [in] msg + * @return uint8_t + * @date 2024-12-04 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2024-12-04 Alvin Young 新建 + *
+ */ +uint8_t imp_msg_queue_send_msg(imp_msg_queue_t* handle, uint16_t recv_id, + imp_msg_item_t* msg); + +/** + * + * @brief 销毁一个消息队列控制句柄 + * @param [in] handle + * @return uint8_t + * @date 2024-12-04 + * @author Alvin Young (impressionyang@outlook.com) + * + * @details + * @note + * @par 修改日志: + * + *
Date Author Description + *
2024-12-04 Alvin Young 新建 + *
+ */ +uint8_t imp_msg_queue_destroy_handle(imp_msg_queue_t* handle); + +#ifdef __cplusplus +} +#endif +#endif //__IMP_MSG_QUEUE_H__ + +/* + * EOF + */ \ No newline at end of file diff --git a/main/main.c b/main/main.c index 0a38b8a..c08b037 100755 --- a/main/main.c +++ b/main/main.c @@ -20,6 +20,7 @@ #include "shell_port.h" #include "main_app.h" #include "main_common.h" +#include "imp_msg_queue.h" static void _init_esp() { @@ -47,14 +48,14 @@ void app_main(void) esp_chip_info_t chip_info; uint32_t flash_size; esp_chip_info(&chip_info); - cdc_printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", CONFIG_IDF_TARGET, - chip_info.cores, - (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", - (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", - (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", - (chip_info.features & CHIP_FEATURE_IEEE802154) - ? ", 802.15.4 (Zigbee/Thread)" - : ""); + cdc_printf("This is %s chip with %d CPU core(s), %s%s%s%s, ", + CONFIG_IDF_TARGET, chip_info.cores, + (chip_info.features & CHIP_FEATURE_WIFI_BGN) ? "WiFi/" : "", + (chip_info.features & CHIP_FEATURE_BT) ? "BT" : "", + (chip_info.features & CHIP_FEATURE_BLE) ? "BLE" : "", + (chip_info.features & CHIP_FEATURE_IEEE802154) + ? ", 802.15.4 (Zigbee/Thread)" + : ""); unsigned major_rev = chip_info.revision / 100; unsigned minor_rev = chip_info.revision % 100; @@ -64,12 +65,13 @@ void app_main(void) return; } - cdc_printf("%" PRIu32 "MB %s flash\r\n", flash_size / (uint32_t)(1024 * 1024), - (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" - : "external"); + cdc_printf("%" PRIu32 "MB %s flash\r\n", + flash_size / (uint32_t)(1024 * 1024), + (chip_info.features & CHIP_FEATURE_EMB_FLASH) ? "embedded" + : "external"); cdc_printf("Minimum free heap size: %" PRIu32 " bytes\r\n", - esp_get_minimum_free_heap_size()); + esp_get_minimum_free_heap_size()); // for (int i = 10; i >= 0; i--) { // printf("Restarting in %d seconds...\n", i); @@ -77,6 +79,8 @@ void app_main(void) // } cdc_printf("start run app:\r\n"); + + imp_msg_queue_init(); xTaskCreate(imp_main_app_task, "main", 2048, NULL, 10, NULL); while (1) { diff --git a/main/main_common.c b/main/main_common.c new file mode 100644 index 0000000..5e1f535 --- /dev/null +++ b/main/main_common.c @@ -0,0 +1,39 @@ +/** + * @file main_common.c + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2024-12-04 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2024-12-04 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ +#include "main_common.h" +/* define --------------------------------------------------------------------*/ +/* typedef -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ + +/// @brief 所有Task的命名集合,名字长度不能超过configMAX_TASK_NAME_LEN = 16 +char* imp_main_task_table[] = { + "idle", + "main_task", + "ext_trans_task", +}; +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ +/* + * EOF + */ \ No newline at end of file diff --git a/main/main_common.h b/main/main_common.h index 0e15478..d0bc67d 100644 --- a/main/main_common.h +++ b/main/main_common.h @@ -29,13 +29,21 @@ extern "C" { /* Includes ------------------------------------------------------------------*/ /* define --------------------------------------------------------------------*/ + +#define IMP_TASK_ID_IDLE (0) +#define IMP_TASK_ID_MAIN_TASK (1) +#define IMP_TASK_ID_EXT_TRANS_SERVICE_TASK (2) + /* typedef -------------------------------------------------------------------*/ /* variables -----------------------------------------------------------------*/ + +extern char* imp_main_task_table[]; + /* Private function(only *.c) -----------------------------------------------*/ /* Exported functions --------------------------------------------------------*/ -extern void cdc_acm_msc_init(); -extern int cdc_printf(const char* fmt, ...); +extern void cdc_acm_msc_init(); +extern int cdc_printf(const char* fmt, ...); extern signed short cdc_usb_read_bytes(char* data, unsigned short len); extern signed short cdc_usb_writ_bytes(char* data, unsigned short len); diff --git a/main/services/README.md b/main/services/README.md new file mode 100644 index 0000000..f19bbab --- /dev/null +++ b/main/services/README.md @@ -0,0 +1 @@ +service是某个专门的功能集合,具备多线程资源,他自己就是一个线程,通过调动不同的component进行特定的事项 \ No newline at end of file diff --git a/main/services/ext_trans_service/ext_trans_service.c b/main/services/ext_trans_service/ext_trans_service.c new file mode 100644 index 0000000..893d489 --- /dev/null +++ b/main/services/ext_trans_service/ext_trans_service.c @@ -0,0 +1,59 @@ +/** + * @file ext_trans_service.c + * @author Alvin Young (impressionyang@outlook.com) + * @brief + * @version 0.1 + * @date 2024-12-04 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2024-12-04 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +/* Includes ------------------------------------------------------------------*/ + +#include "ext_trans_service.h" +#include "freertos/FreeRTOS.h" +#include "freertos/task.h" +#include "imp_msg_queue.h" + +/* define --------------------------------------------------------------------*/ +/* typedef -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ + +static imp_msg_queue_t* msg_q_handle = NULL; +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ + +uint8_t imp_ext_trans_service_task() +{ + int i = 0; + msg_q_handle = + imp_msg_queue_create_handle(IMP_TASK_ID_EXT_TRANS_SERVICE_TASK); + imp_msg_item_t msg_item = { 0 }; + uint8_t msg_read_ret = 0; + while (1) { + msg_read_ret = imp_msg_queue_recv_msg(msg_q_handle, &msg_item, 0); + if (!msg_read_ret) { + // read succeed + cdc_printf("read msg ok from %d to %d value: %d\r\n", + msg_item.send_id, msg_item.recv_id, msg_item.msg_data); + } + vTaskDelay(100 / portTICK_PERIOD_MS); + } + return 0; +} + +/* + * EOF + */ \ No newline at end of file diff --git a/main/services/ext_trans_service/ext_trans_service.h b/main/services/ext_trans_service/ext_trans_service.h new file mode 100644 index 0000000..3b80889 --- /dev/null +++ b/main/services/ext_trans_service/ext_trans_service.h @@ -0,0 +1,51 @@ +/** + * @file ext_trans_service.h + * @author Alvin Young (impressionyang@outlook.com) + * @brief 用来界定外部串口输出的内容是字符流还是控制流的,需要完成的操作为分割控制流 + * 和字符流,控制流交给控制模块同时切断cdc_printf的功能,字符流则释放cdc_printf的打 + * 印内容权限 + * @version 0.1 + * @date 2024-12-03 + * + * _ _ + * (_)_ _ ___ _______ ___ ___ (_)__ ___ __ _____ ____ ___ _ + * / / ' \/ _ \/ __/ -_|_-<(_- + * Date Version Author Description + * 2024-12-03 v1.0 Alvin Young 首次创建 + * + * + */ +/* Define to prevent recursive inclusion -------------------------------------*/ +#ifndef __EXT_TRANS_SERVICE_H__ +#define __EXT_TRANS_SERVICE_H__ + +#ifdef __cplusplus +extern "C" { +#endif + +/* Includes ------------------------------------------------------------------*/ + +#include +#include "main_common.h" +/* define --------------------------------------------------------------------*/ +/* typedef -------------------------------------------------------------------*/ +/* variables -----------------------------------------------------------------*/ +/* Private function(only *.c) -----------------------------------------------*/ +/* Exported functions --------------------------------------------------------*/ + +uint8_t imp_ext_trans_service_task(); + +#ifdef __cplusplus +} +#endif +#endif //__EXT_TRANS_SERVICE_H__ + +/* + * EOF + */ \ No newline at end of file