From b27fc87089e113a97b0352e8c0e3affa9220f6f9 Mon Sep 17 00:00:00 2001 From: RB Date: Sun, 27 Apr 2025 00:30:45 +0800 Subject: [PATCH] =?UTF-8?q?=E6=B7=BB=E5=8A=A0user?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- User/bsp/.gitkeep | 0 User/bsp/bsp.h | 17 +++++++ User/component/.gitkeep | 0 User/component/user_math.c | 12 +++++ User/component/user_math.h | 20 ++++++++ User/device/.gitkeep | 0 User/module/.gitkeep | 0 User/task/.gitkeep | 0 User/task/init.c | 39 ++++++++++++++++ User/task/user_task.c | 34 ++++++++++++++ User/task/user_task.h | 94 ++++++++++++++++++++++++++++++++++++++ 11 files changed, 216 insertions(+) create mode 100644 User/bsp/.gitkeep create mode 100644 User/bsp/bsp.h create mode 100644 User/component/.gitkeep create mode 100644 User/component/user_math.c create mode 100644 User/component/user_math.h create mode 100644 User/device/.gitkeep create mode 100644 User/module/.gitkeep create mode 100644 User/task/.gitkeep create mode 100644 User/task/init.c create mode 100644 User/task/user_task.c create mode 100644 User/task/user_task.h diff --git a/User/bsp/.gitkeep b/User/bsp/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/User/bsp/bsp.h b/User/bsp/bsp.h new file mode 100644 index 0000000..19e162c --- /dev/null +++ b/User/bsp/bsp.h @@ -0,0 +1,17 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#define BSP_OK (0) +#define BSP_ERR (-1) +#define BSP_ERR_NULL (-2) +#define BSP_ERR_INITED (-3) +#define BSP_ERR_NO_DEV (-4) + +#define SIGNAL_BSP_USB_BUF_RECV (1u << 0) + +#ifdef __cplusplus +} +#endif diff --git a/User/component/.gitkeep b/User/component/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/User/component/user_math.c b/User/component/user_math.c new file mode 100644 index 0000000..0d0ff8c --- /dev/null +++ b/User/component/user_math.c @@ -0,0 +1,12 @@ +// /* +// 自定义的数学运算。 +// */ + +// #include "user_math.h" +// #include + + +// float Adc_to_Distance(uint16_t adc_data) { +// // return (float)((adc_data * 1000) / 4095); +// return (float)((adc_data * 1000) / 16000000.0f); // 使用浮点数进行计算 +// } \ No newline at end of file diff --git a/User/component/user_math.h b/User/component/user_math.h new file mode 100644 index 0000000..3e623b1 --- /dev/null +++ b/User/component/user_math.h @@ -0,0 +1,20 @@ +// /* +// 自定义的数学运算。 +// */ + +// #pragma once + + +// #include +// #include + +// #ifndef M_PI +// #define M_PI 3.14159265358979323846f +// #endif + +// /** +// * @brief 用于计算ADC数据对应的距离(mm) +// * @param adc_data +// * @return +// */ +// float Adc_to_Distance(uint16_t adc_data); \ No newline at end of file diff --git a/User/device/.gitkeep b/User/device/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/User/module/.gitkeep b/User/module/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/User/task/.gitkeep b/User/task/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/User/task/init.c b/User/task/init.c new file mode 100644 index 0000000..7e787d0 --- /dev/null +++ b/User/task/init.c @@ -0,0 +1,39 @@ +/* + 初始化任务 +*/ + +/* Includes ----------------------------------------------------------------- */ +#include "task\user_task.h" +#include "device\can.h" +/* Private typedef ---------------------------------------------------------- */ +/* Private define ----------------------------------------------------------- */ +/* Private macro ------------------------------------------------------------ */ +/* Private variables -------------------------------------------------------- */ + +/* Private function --------------------------------------------------------- */ +/* Exported functions ------------------------------------------------------- */ + +/** + * \brief 初始化 + * + * \param argument 未使用 + */ +void Task_Init(void *argument) { + (void)argument; /* 未使用argument,消除警告 */ + + osKernelLock(); // 锁定内核,防止任务切换 + + // 创建任务,确保任务创建成功 + task_runtime.thread.disp = osThreadNew(Task_Disp, NULL, &attr_disp); + task_runtime.thread.can = osThreadNew(Task_Can, NULL, &attr_can); + task_runtime.thread.monitor = osThreadNew(Task_Monitor, NULL, &attr_monitor); + task_runtime.thread.pc = osThreadNew(Task_PC, NULL, &attr_pc); + + //创建消息队列 + task_runtime.msgq.can.feedback.sick = osMessageQueueNew(2u, sizeof(CAN_t), NULL); + task_runtime.msgq.pc = osMessageQueueNew(2u, sizeof(CAN_t), NULL); + + osKernelUnlock(); // 解锁内核 + osThreadTerminate(osThreadGetId()); // 任务完成后结束自身 +} + diff --git a/User/task/user_task.c b/User/task/user_task.c new file mode 100644 index 0000000..7d79402 --- /dev/null +++ b/User/task/user_task.c @@ -0,0 +1,34 @@ +#include "task/user_task.h" + +Task_Runtime_t task_runtime; + +// 定义任务属性 +const osThreadAttr_t attr_init = { + .name = "Task_Init", + .priority = osPriorityRealtime, + .stack_size = 256 * 4, +}; + +const osThreadAttr_t attr_can = { + .name = "Task_Can", + .priority = osPriorityRealtime, + .stack_size = 128 * 4, +}; + +const osThreadAttr_t attr_disp = { + .name = "Task_Disp", + .priority = osPriorityRealtime, + .stack_size = 1024 * 4, +}; + +const osThreadAttr_t attr_monitor = { + .name = "Task_Monitor", + .priority = osPriorityRealtime, + .stack_size = 128 * 4, +}; + +const osThreadAttr_t attr_pc = { + .name = "Task_PC", + .priority = osPriorityRealtime, + .stack_size = 128 * 4, +}; diff --git a/User/task/user_task.h b/User/task/user_task.h new file mode 100644 index 0000000..e2b47df --- /dev/null +++ b/User/task/user_task.h @@ -0,0 +1,94 @@ +#pragma once + +#ifdef __cplusplus +extern "C" { +#endif + +#include +#include "FreeRTOS.h" +#include "task.h" + +// 定义任务运行时结构体 +typedef struct { + /* 各任务,也可以叫做线程 */ + struct { + osThreadId_t can; /* CAN任务 */ + osThreadId_t disp; /* ADC任务 */ + osThreadId_t pc; /* PC任务 */ + osThreadId_t monitor; /* 监控任务 */ + } thread; + + struct { + struct { + struct { + osMessageQueueId_t sick; + } output; + struct { + osMessageQueueId_t sick; + } feedback; + } can; + + osMessageQueueId_t pc; /* PC消息队列 */ + } msgq; + + + struct { + uint32_t can; /* CAN使用 */ + uint32_t disp; /* ADC使用 */ + uint32_t pc; /* PC使用 */ + uint32_t monitor; /* 监控使用 */ + } heap_water_mark; /* heap使用 */ + + struct { + float can; /* CAN任务运行频率 */ + float disp; /* ADC任务运行频率 */ + float pc; /* PC任务运行频率 */ + float monitor; /* 监控任务运行频率 */ + } freq; /* 任务运行频率 */ + + struct { + uint32_t can; /* CAN任务运行时间 */ + uint32_t disp; /* ADC任务运行时间 */ + uint32_t pc; /* PC任务运行时间 */ + uint32_t monitor; /* 监控任务运行时间 */ + } last_up_time; /* 任务最近运行时间 */ + } Task_Runtime_t; + + + +// 任务频率和初始化延时 +#define TASK_FREQ_CAN (100u) +#define TASK_FREQ_DISP (1u) +#define TASK_FREQ_MONITOR (1u) +#define TASK_FREQ_PC (100u) + + +#define TASK_INIT_DELAY_INFO (500u) + +// 任务句柄 +typedef struct { + osThreadId_t can; + osThreadId_t disp; + osThreadId_t monitor; + osThreadId_t pc; +} Task_Handles_t; + +extern Task_Runtime_t task_runtime; + + +extern const osThreadAttr_t attr_init; + +extern const osThreadAttr_t attr_can; +extern const osThreadAttr_t attr_disp; +extern const osThreadAttr_t attr_monitor; +extern const osThreadAttr_t attr_pc; + +void Task_Init(void *argument); +void Task_Can(void *argument); +void Task_Disp(void *argument); +void Task_Monitor(void *argument); +void Task_PC(void *argument); + +#ifdef __cplusplus +} +#endif