Explorar el Código

上传文件至 'inc'

Yun_Lib_Share hace 1 año
padre
commit
e8df2c81c1
Se han modificado 5 ficheros con 478 adiciones y 0 borrados
  1. 139 0
      inc/zf_list.h
  2. 150 0
      inc/zf_task.h
  3. 38 0
      inc/zf_task_schedule.h
  4. 68 0
      inc/zf_time.h
  5. 83 0
      inc/zf_timer.h

+ 139 - 0
inc/zf_list.h

@@ -0,0 +1,139 @@
+/**
+  *****************************************************************************
+  * @file    zf_list.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   简单列表的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_LIST_H__
+#define __ZF_LIST_H__
+
+#ifdef __cplusplus 
+extern "C" {
+#endif
+
+#include "stdbool.h"
+#include "stdint.h"
+
+/* 列表节点结构 */
+typedef struct _ListNode
+{
+    bool IsExternData;          /* 是否外部数据,是则销毁时不释放 */
+    uint8_t *pData;             /* 指向数据的指针 */
+    uint32_t Size;              /* 数据的大小 */
+    struct _ListNode *Next;     /* 指向下一个节点 */
+} ListNode;
+
+/* 列表结构 */
+typedef struct _List
+{
+    ListNode *pRootNode;        /* 指向根节点数据 */
+    uint32_t Count;             /* 节点个数 */
+    
+    /* 在尾端增加节点 */
+    bool (*Add)(struct _List * const pList, ListNode *pNode);
+    
+    /* 删除节点(释放空间) */
+    bool (*Delete)(struct _List * const pList, ListNode *pNode);
+    
+    /* 移除节点(不释放空间) */
+    bool (*Remove)(struct _List * const pList, ListNode *pNode);
+    
+    /* 返回指定索引处的节点的指针 */
+    bool (*GetElementAt)(struct _List * const pList, uint32_t index,
+        ListNode **ppNode);
+    
+    /* 返回数据区指向data的节点的指针 */
+    bool (*GetElementByData)(struct _List * const pList, void *pdata,
+        ListNode **ppNode);
+    
+    /* 返回指定索引处的节点的数据缓冲区的指针 */
+    void *(*GetElementDataAt)(struct _List * const pList, uint32_t index);
+    
+    /* 返回节点的索引 */
+    bool (*GetElementIndex)(struct _List * const pList, ListNode *pNode,
+        uint32_t *pIndex);
+    
+    /* 在指定索引处增加节点 */
+    bool (*AddElementAt)(struct _List * const pList, uint32_t index,
+        ListNode *pNode);
+    
+    /* 在指定索引处删除节点(释放空间) */
+    bool (*DeleteElementAt)(struct _List * const pList, uint32_t index);
+    
+    /* 在指定索引处移除节点(不释放空间) */
+    bool (*RemoveElementAt)(struct _List * const pList, uint32_t index);
+    
+    /* 清空列表(释放空间) */
+    bool (*Clear)(struct _List * const pList);
+    
+    /* 释放列表(释放空间) */
+    bool (*Dispose)(struct _List * const pList);
+} List;
+
+/* 创建列表(内部分配空间) */
+bool List_create(List **ppList);
+
+/* 在尾端增加节点 */
+bool List_add(List * const pList, ListNode *pNode);
+
+/* 删除节点(内部释放空间) */
+bool List_delete(List * const pList, ListNode *pNode);
+
+/* 移除节点(内部不释放空间) */
+bool List_remove(List * const pList, ListNode *pNode);
+
+/* 返回指定索引处的节点的指针 */
+bool List_getElementAt(List * const pList, uint32_t index,
+    ListNode **ppNode);
+
+/* 返回数据区指向data的节点的指针 */
+bool List_getElementByData(List * const pList, void *pdata,
+    ListNode **ppNode);
+
+/* 返回指定索引处的节点的数据缓冲区的指针 */
+void *List_getElementDataAt(List * const pList, uint32_t index);
+
+/* 返回节点的索引 */
+bool List_getElementIndex(List * const pList, ListNode *pNode,
+    uint32_t *pIndex);
+
+/* 在指定索引处增加节点 */
+bool List_addElementAt(List * const pList, uint32_t index,
+    ListNode *pNode);
+
+/* 在指定索引处删除节点(内部释放空间) */
+bool List_deleteElementAt(List * const pList, uint32_t index);
+
+/* 在指定索引处移除节点(不释放空间) */
+bool List_removeElementAt(List * const pList, uint32_t index);
+
+/* 清空列表(内部释放空间) */
+bool List_clear(List * const pList);
+
+/* 释放列表(内部释放空间) */
+bool List_dispose(List * const pList);
+
+/* 创建节点(内部分配空间,size=0表示使用外部数据) */
+bool List_mallocNode(ListNode **ppNode, void **ppData, uint32_t size);
+
+/* 释放节点(不释放外部创建的数据) */
+bool List_freeNode(ListNode *pNode);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_LIST_H__ */
+
+/******************************** END OF FILE ********************************/

+ 150 - 0
inc/zf_task.h

@@ -0,0 +1,150 @@
+/**
+  *****************************************************************************
+  * @file    zf_task.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   任务管理的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_TASK_H__
+#define __ZF_TASK_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdint.h"
+#include "stdbool.h"
+#include "zf_list.h"
+#include "zf_timer.h"
+#include "zf_event.h"
+
+/* 用户任务优先级1-31,数字越小,优先级越高 */
+
+/* 任务最高优先级(用户不可用) */
+#define TASK_HIGHEST_PRIORITY EVENT_HIGHEST_PRIORITY
+
+/* 任务最低优先级(用户不可用) */
+#define TASK_LOWEST_PRIORITY EVENT_LOWEST_PRIORITY
+
+/* 开启任务调度 */
+#define TASK_SCHEDULE_ENABLE() Task_scheduleSwitch(true)
+
+/* 关闭任务调度 */
+#define TASK_SCHEDULE_DISABLE() Task_scheduleSwitch(false)
+
+/* 获取任务调度标志 */
+#define TASK_IS_SCHEDULE_ON() mIsScheduleOn
+
+/* 获取任务系统运行标志 */
+#define TASK_IS_SYSTEM_RUN() mIsTaskSystemRun
+
+/* 获取空闲任务 */
+#define TASK_GET_IDLE_TASK() pIdleTask
+
+/* 获取空闲任务事件处理器 */
+#define TASK_GET_IDLE_TASK_HANDLER() pIdleTaskEventHandler
+
+/* 获取任务列表 */
+#define TASK_GET_TASK_LIST() Task_getTaskList()
+
+/* 获取任务数量 */
+#define TASK_GET_TASK_COUNT() TASK_GET_TASK_LIST()->Count
+
+/* 创建任务定时器 */
+#define TASK_TIMER_CREATE(ppTimer_) do                     \
+{                                                          \
+    Timer_create(ppTimer_);                                \
+    if (*ppTimer_ != NULL)                                 \
+    {                                                      \
+        *ppTimer_->pEventHandler = pIdleTaskEventHandler;  \
+    }                                                      \
+} while(0)
+
+typedef void (*ITaskProcess)(void *pArg); /* 程序任务类型 */
+
+/* 任务状态 */
+typedef enum _TaskState
+{
+    TASK_STATE_STOP = 0,          /* 停止 */
+    TASK_STATE_RUNNING            /* 运行 */
+} TaskState;
+
+/* 任务结构 */
+typedef struct _Task
+{
+    uint32_t *pStkPtr;            /* 堆栈指针 */
+    uint32_t *pStkBase;           /* 堆栈基地址 */
+    uint32_t StkSize;             /* 堆栈大小 */
+    uint32_t DelayTime;           /* 任务延时时间(系统周期) */
+    uint8_t Priority;             /* 任务优先级 */
+    uint8_t State;                /* 任务状态 */
+    uint32_t RunTime;             /* 任务总运行时间(系统周期) */
+    
+    /* 开始任务 */
+    bool (*Start)(struct _Task * const pTask);
+    
+    /* 停止任务 */
+    bool (*Stop)(struct _Task * const pTask);
+    
+    /* 销毁任务 */
+    void (*Dispose)(struct _Task * const pTask);
+    
+    /* 延时任务 */
+    bool (*Delay)(struct _Task * const pTask, uint32_t tick);
+} Task;
+
+extern Task *pTopPriorityTask;              /* 最高优先级任务 */
+extern Task *pCurrentTask;                  /* 当前任务 */
+extern Task *pIdleTask;                     /* 空闲任务 */
+extern EventHandler *pIdleTaskEventHandler; /* 空闲任务事件处理器 */
+extern bool mIsScheduleOn;                  /* 任务调度开的标志 */
+extern bool mIsTaskSystemRun;               /* 任务系统是否开始的标志 */
+
+/* 获取任务列表 */
+List *Task_getTaskList(void);
+
+/* 创建任务 */
+bool Task_create(Task **ppTask, ITaskProcess taskProcess, void *pArg,
+    uint8_t priority, uint32_t stkSize);
+
+/* 开始任务 */
+bool Task_start(Task * const pTask);
+
+/* 停止任务 */
+bool Task_stop(Task * const pTask);
+
+/* 销毁任务 */
+void Task_dispose(Task * const pTask);
+
+/* 延时任务 */
+void Task_delay(struct _Task * const pTask, uint32_t tick);
+
+/* 任务调度开关 */
+void Task_scheduleSwitch(bool on);
+
+/* 任务调度 */
+void Task_schedule(void);
+
+/* 任务时间更新 */
+void Task_timeUpdate(void);
+
+/* 运行所有任务,程序不返回 */
+void Task_run(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_TASK_H__ */
+
+/******************************** END OF FILE ********************************/

+ 38 - 0
inc/zf_task_schedule.h

@@ -0,0 +1,38 @@
+/**
+  *****************************************************************************
+  * @file    zf_task_schedule.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   任务调度算法的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_TASK_SCHEDULE_H__
+#define __ZF_TASK_SCHEDULE_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdint.h"
+#include "stdbool.h"
+#include "zf_task.h"
+
+/* 获取最高优先级任务 */
+Task *Task_getTopPriorityTask(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_TASK_SCHEDULE_H__ */
+
+/******************************** END OF FILE ********************************/

+ 68 - 0
inc/zf_time.h

@@ -0,0 +1,68 @@
+/**
+  *****************************************************************************
+  * @file    zf_time.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   系统时间的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_TIME_H__
+#define __ZF_TIME_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdbool.h"
+#include "stdint.h"
+
+/* 系统滴答周期(ms) */
+#define ZF_TICK_PERIOD 1
+
+/* 获取系统滴答数 */
+#define ZF_SYSTICK() ZF_getSystemTick()
+
+/* 获取系统时间(ms) */
+#define ZF_SYSTIME_MS() ZF_getSystemTimeMS()
+
+/* 系统延时(ms) */
+#define ZF_DELAY_MS(ms_) do                            \
+{                                                      \
+    if (ms_ % ZF_TICK_PERIOD)                          \
+    {                                                  \
+        ZF_delayTick((ms_ / ZF_TICK_PERIOD) + 1);      \
+    }                                                  \
+    else                                               \
+    {                                                  \
+        ZF_delayTick(ms_ / ZF_TICK_PERIOD);            \
+    }                                                  \
+} while(0)
+
+/* 获取系统滴答数 */
+uint32_t ZF_getSystemTick(void);
+
+/* 获取系统时间(ms) */
+uint32_t ZF_getSystemTimeMS(void);
+
+/* 系统延时 */
+void ZF_delayTick(uint32_t tick);
+
+/* 系统滴答程序(需挂在硬件的时间中断里边) */
+void ZF_timeTick (void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_TIME_H__ */
+
+/******************************** END OF FILE ********************************/

+ 83 - 0
inc/zf_timer.h

@@ -0,0 +1,83 @@
+/**
+  *****************************************************************************
+  * @file    zf_timer.h
+  * @author  Zorb
+  * @version V1.0.0
+  * @date    2018-06-28
+  * @brief   软件定时器(精度1ms)的头文件
+  *****************************************************************************
+  * @history
+  *
+  * 1. Date:2018-06-28
+  *    Author:Zorb
+  *    Modification:建立文件
+  *
+  *****************************************************************************
+  */
+
+#ifndef __ZF_TIMER_H__
+#define __ZF_TIMER_H__
+
+#ifdef __cplusplus
+extern "C" {
+#endif
+
+#include "stdint.h"
+#include "stdbool.h"
+#include "zf_event.h"
+
+/* 定时器处理程序 */
+typedef void (*ITimerProcess)(void);
+
+/* 定时器结构 */
+typedef struct _Timer
+{
+    uint8_t Priority;               /* 事件优先级 */
+    uint32_t Interval;              /* 时间间隔(ms) */
+    uint32_t AlarmTime;             /* 定时到达时间 */
+    bool IsAutoReset;               /* 重复运行(默认开) */
+    bool IsRunning;                 /* 是否正在运行(默认关) */
+    /* 事件的处理者,事件将推送到处理者的队列 */
+    /* 不设置处理者则本地执行(挂载Timer_process的地方) */
+    EventHandler *pEventHandler;
+    /* 处理事件 */
+    ITimerProcess TimerProcess;
+    
+    /* 开始定时器 */
+    void (*Start)(struct _Timer * const pTimer);
+    
+    /* 关闭定时器 */
+    void (*Stop)(struct _Timer * const pTimer);
+    
+    /* 重新运行定时器 */
+    void (*Restart)(struct _Timer * const pTimer);
+    
+    /* 销毁定时器(释放空间) */
+    bool (*Dispose)(struct _Timer * const pTimer);
+} Timer;
+
+/* 创建定时器(内部分配空间) */
+bool Timer_create(Timer ** ppTimer);
+
+/* 开始定时器 */
+void Timer_start(Timer * const pTimer);
+
+/* 关闭定时器 */
+void Timer_stop(Timer * const pTimer);
+
+/* 重新运行定时器 */
+void Timer_restart(Timer * const pTimer);
+
+/* 销毁定时器(释放空间) */
+bool Timer_dispose(Timer * const pTimer);
+
+/* 后台运行程序(放在1ms内的循环里边) */
+void Timer_process(void);
+
+#ifdef __cplusplus
+}
+#endif
+
+#endif /* __ZF_TIMER_H__ */
+
+/******************************** END OF FILE ********************************/