Ver código fonte

增加任务管理 和 动态内存

kinve 9 meses atrás
pai
commit
0ffb9882ac

+ 85 - 0
stm32 任务管理/scheduler.c

@@ -0,0 +1,85 @@
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//        头文件区
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#include <stdio.h>
+#include "scheduler.h"
+#include "stm32f30x.h"
+
+static Task_Block_t task_block[TASK_NUM]; // 任务块 根据需求设定任务数量
+Task_Ctrl_t Task_Ctrl;                    // 任务控制块
+
+void SysTick_Handler(void)
+{
+    ISR_CallBlack(&Task_Ctrl); // 每次系统时钟中断,时钟周期加1
+}
+
+/**
+ * @author
+ * @brief 	任务添加函数
+ * @param  任务执行时间间隔
+ * @param  任务ID 同时表示任务优先级
+ * @param  任务主体函数
+ * @retval NULL
+ */
+void TaskAdd(uint8_t task_id, Task_Handler_t task, uint32_t on_time)
+{
+    if(task_id >= TASK_NUM){
+        return;
+    }
+    task_block[task_id].On_Time = on_time;
+    task_block[task_id].RunTime = 0;
+    task_block[task_id].Task_Handler = task;
+    task_block[task_id].TaskState = 0;
+}
+
+/*更新任务状态*/
+void UpdateTask(p_Task_Block_t Task_Block)
+{
+    Task_Block->RunTime = 0;
+    Task_Block->TaskState = 1;
+}
+
+/*任务初始化函数*/
+void TaskInit(void)
+{
+    Task_Ctrl.Update_State = UpdateTask;
+    Task_Ctrl.Task = task_block;
+
+    SysTick_Config(SystemCoreClock / 1000);
+}
+
+/*放在1ms中断中*/
+void ISR_CallBlack(p_Task_Ctrl_t Task_Ctrl)
+{
+    if (Task_Ctrl->Task != NULL)
+    {
+        for (uint8_t i = 0; i < TASK_NUM; i++)
+        {
+            Task_Ctrl->Task[i].RunTime++;
+            if (Task_Ctrl->Task[i].RunTime > Task_Ctrl->Task[i].On_Time)
+            {
+                if(Task_Ctrl->Update_State) {
+                    Task_Ctrl->Update_State(&Task_Ctrl->Task[i]);
+                }
+            }
+        }
+    }
+}
+
+void TaskStart()
+{
+    static uint8_t i = 0;
+    while (1)
+    {
+        for (i = 0; i < TASK_NUM; i++)
+        {
+            if (Task_Ctrl.Task[i].TaskState)
+            {
+                Task_Ctrl.Task[i].TaskState = 0;
+                if(Task_Ctrl.Task[i].Task_Handler) {
+                    Task_Ctrl.Task[i].Task_Handler();
+                }
+            }
+        }
+    }
+}

+ 48 - 0
stm32 任务管理/scheduler.h

@@ -0,0 +1,48 @@
+
+#ifndef __MY_TASK_H
+#define __MY_TASK_H
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "stm32f30x.h"
+
+/*任务id 数字越小优先级越高  TASK_NUM表示任务数量,务必放到末尾*/
+enum
+{
+    led_id = 0,
+    ymod_id,
+    TASK_NUM
+};
+
+typedef void (*Task_Handler_t)(void);
+/*任务块*/
+typedef struct
+{
+    uint32_t RunTime;           // 已运行时间
+    uint32_t On_Time;           // 任务执行时间间隔
+    uint8_t TaskState;          // 任务状态 1:可执行   0:空闲
+    Task_Handler_t Task_Handler; // 任务执行函数
+
+} Task_Block_t, *p_Task_Block_t;
+
+/*任务控制块*/
+typedef struct
+{
+    void (*Update_State)(p_Task_Block_t Task_Block); // 更新任务状态函数
+    p_Task_Block_t Task;                             // 任务块指针
+
+} Task_Ctrl_t, *p_Task_Ctrl_t;
+
+void TaskAdd(uint8_t task_id, Task_Handler_t task, uint32_t on_time);
+void TaskInit(void);
+void TaskStart();
+extern Task_Ctrl_t Task_Ctrl;
+void ISR_CallBlack(p_Task_Ctrl_t Task_Ctrl);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif

+ 85 - 0
stm32 动态内存/scheduler.c

@@ -0,0 +1,85 @@
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+//        头文件区
+//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+#include <stdio.h>
+#include "scheduler.h"
+#include "stm32f30x.h"
+
+static Task_Block_t task_block[TASK_NUM]; // 任务块 根据需求设定任务数量
+Task_Ctrl_t Task_Ctrl;                    // 任务控制块
+
+void SysTick_Handler(void)
+{
+    ISR_CallBlack(&Task_Ctrl); // 每次系统时钟中断,时钟周期加1
+}
+
+/**
+ * @author
+ * @brief 	任务添加函数
+ * @param  任务执行时间间隔
+ * @param  任务ID 同时表示任务优先级
+ * @param  任务主体函数
+ * @retval NULL
+ */
+void TaskAdd(uint8_t task_id, Task_Handler_t task, uint32_t on_time)
+{
+    if(task_id >= TASK_NUM){
+        return;
+    }
+    task_block[task_id].On_Time = on_time;
+    task_block[task_id].RunTime = 0;
+    task_block[task_id].Task_Handler = task;
+    task_block[task_id].TaskState = 0;
+}
+
+/*更新任务状态*/
+void UpdateTask(p_Task_Block_t Task_Block)
+{
+    Task_Block->RunTime = 0;
+    Task_Block->TaskState = 1;
+}
+
+/*任务初始化函数*/
+void TaskInit(void)
+{
+    Task_Ctrl.Update_State = UpdateTask;
+    Task_Ctrl.Task = task_block;
+
+    SysTick_Config(SystemCoreClock / 1000);
+}
+
+/*放在1ms中断中*/
+void ISR_CallBlack(p_Task_Ctrl_t Task_Ctrl)
+{
+    if (Task_Ctrl->Task != NULL)
+    {
+        for (uint8_t i = 0; i < TASK_NUM; i++)
+        {
+            Task_Ctrl->Task[i].RunTime++;
+            if (Task_Ctrl->Task[i].RunTime > Task_Ctrl->Task[i].On_Time)
+            {
+                if(Task_Ctrl->Update_State) {
+                    Task_Ctrl->Update_State(&Task_Ctrl->Task[i]);
+                }
+            }
+        }
+    }
+}
+
+void TaskStart()
+{
+    static uint8_t i = 0;
+    while (1)
+    {
+        for (i = 0; i < TASK_NUM; i++)
+        {
+            if (Task_Ctrl.Task[i].TaskState)
+            {
+                Task_Ctrl.Task[i].TaskState = 0;
+                if(Task_Ctrl.Task[i].Task_Handler) {
+                    Task_Ctrl.Task[i].Task_Handler();
+                }
+            }
+        }
+    }
+}

+ 48 - 0
stm32 动态内存/scheduler.h

@@ -0,0 +1,48 @@
+
+#ifndef __MY_TASK_H
+#define __MY_TASK_H
+#ifdef __cplusplus
+extern "C"
+{
+#endif
+
+#include "stm32f30x.h"
+
+/*任务id 数字越小优先级越高  TASK_NUM表示任务数量,务必放到末尾*/
+enum
+{
+    led_id = 0,
+    ymod_id,
+    TASK_NUM
+};
+
+typedef void (*Task_Handler_t)(void);
+/*任务块*/
+typedef struct
+{
+    uint32_t RunTime;           // 已运行时间
+    uint32_t On_Time;           // 任务执行时间间隔
+    uint8_t TaskState;          // 任务状态 1:可执行   0:空闲
+    Task_Handler_t Task_Handler; // 任务执行函数
+
+} Task_Block_t, *p_Task_Block_t;
+
+/*任务控制块*/
+typedef struct
+{
+    void (*Update_State)(p_Task_Block_t Task_Block); // 更新任务状态函数
+    p_Task_Block_t Task;                             // 任务块指针
+
+} Task_Ctrl_t, *p_Task_Ctrl_t;
+
+void TaskAdd(uint8_t task_id, Task_Handler_t task, uint32_t on_time);
+void TaskInit(void);
+void TaskStart();
+extern Task_Ctrl_t Task_Ctrl;
+void ISR_CallBlack(p_Task_Ctrl_t Task_Ctrl);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif