单片机时间片架构

1.Wos_List.h
 

/*
 * Wos_List.h
 *
 *  Created on: 2023/12/5
 *      Author: WZX
 */
 
 
#ifndef _WOS_LIST_H_
#define _WOS_LIST_H_


#define Wos_Offset_Of(type, member)             (unsigned long) &((type*)0)->member
#define Wos_Container_Of(ptr, type, member)     ((type *)((char *)(ptr) - Wos_Offset_Of(type, member)))


typedef struct list_structure
{
    struct list_structure *next;
    struct list_structure *prev;
} Wos_ListObj;


#define WOS_LIST_HEAD_INIT(name)    {&(name), &(name)}
#define WOS_LIST_HEAD(name)         Wos_ListObj name = WOS_LIST_HEAD_INIT(name)


void Wos_List_Init(Wos_ListObj *list);
void Wos_List_Insert_After(Wos_ListObj *list, Wos_ListObj *node);
void Wos_List_Insert_Before(Wos_ListObj *list, Wos_ListObj *node);
void Wos_List_Remove(Wos_ListObj *node);
int Wos_List_Isempty(const Wos_ListObj *list);
unsigned int Wos_List_Len(const Wos_ListObj *list);


#define Wos_List_Entry(node, type, member) \
	Wos_Container_Of(node, type, member)


#define Wos_List_For_Each(pos, head) \
	for (pos = (head)->next; pos != (head); pos = pos->next)


#define Wos_List_For_Each_Safe(pos, n, head) \
	for (pos = (head)->next, n = pos->next; pos != (head); \
    pos = n, n = pos->next)


#endif


2.Wos_List.C

/*
 * Wos_List.c
 *
 *  Created on: 2023/12/5
 *      Author: WZX
 */
 
 
#include "Wos_List.h"


/*********************************************************************
 * @fn      Wos_List_Init
 *
 * @brief   os初始化.
 *
 * @param   list:os任务列表
 *
 * @return  none
 */
void Wos_List_Init(Wos_ListObj *list)
{
    list->next = list->prev = list;
}


/*********************************************************************
 * @fn      Wos_List_Insert_After
 *
 * @brief   os列表头节点添加节点.
 *
 * @param   list:os任务列表
 *
 * @param	node:添加的节点
 *
 * @return  none
 */
void Wos_List_Insert_After(Wos_ListObj *list, Wos_ListObj *node)
{
    list->next->prev = node;
    node->next = list->next;

    list->next = node;
    node->prev = list;
}


/*********************************************************************
 * @fn      Wos_List_Insert_Before
 *
 * @brief   os列表尾节点添加节点.
 *
 * @param   list:os任务列表
 *
 * @param 	node:添加的节点
 *
 * @return  none
 */
void Wos_List_Insert_Before(Wos_ListObj *list, Wos_ListObj *node)
{
    list->prev->next = node;
    node->prev = list->prev;

    list->prev = node;
    node->next = list;
}


/*********************************************************************
 * @fn      Wos_List_Remove
 *
 * @brief   os任务列表删除节点.
 *
 * @param   node:添加的节点
 *
 * @return  none
 */
void Wos_List_Remove(Wos_ListObj *node)
{
    node->next->prev = node->prev;
    node->prev->next = node->next;

    node->next = node->prev = node;
}


/*********************************************************************
 * @fn      Wos_List_Isempty
 *
 * @brief   检测os任务列表是否空.
 *
 * @param   list:os任务列表
 *
 * @return  none
 */
int Wos_List_Isempty(const Wos_ListObj *list)
{
    return list->next == list;
}


/*********************************************************************
 * @fn      Wos_List_Len
 *
 * @brief   计算os列表的节点长度.
 *
 * @param   none
 *
 * @return  none
 */
unsigned int Wos_List_Len(const Wos_ListObj* list)
{
    unsigned int len = 0;
    const Wos_ListObj* p = list;
	
    while (p->next != list)
    {
        p = p->next;
        len++;
    }

    return len;
}


3.Wos_Timesilce.h

/*
 * Wos_Timesilce.h
 *
 *  Created on: 2023/12/5
 *      Author: WZX
 */
 
 
#ifndef _WOS_TIMESILCE_H_
#define _WOS_TIMESILCE_H_


#include "Wos_List.h"


#define Wos_Task_Name_MAX			10


typedef enum 
{
    Wos_TASK_STOP,
    Wos_TASK_RUN
} Wos_IsTaskRun;


typedef struct timesilce
{
	char name[Wos_Task_Name_MAX];
    unsigned int id;
    void (*task_hdl)(void *pvParameters);
    Wos_IsTaskRun is_run;
    unsigned int timer;
    unsigned int timeslice_len;
    Wos_ListObj timeslice_task_list;
} Wos_TimesilceTaskObj;


void Wos_Timeslice_Exec(void);
void Wos_Timeslice_Tick(void);
void Wos_Timeslice_Task_Init(const char *name, Wos_TimesilceTaskObj *obj, \
							void (*task_hdl)(void *pvParameters), unsigned int id, unsigned int timeslice_len);
void Wos_Timeslice_Task_Add(Wos_TimesilceTaskObj *obj);
void Wos_Timeslice_Task_Del(Wos_TimesilceTaskObj *obj);

unsigned int Wos_Timeslice_Get_Task_Num(void);
unsigned char Wos_Timeslice_Task_Isexist(Wos_TimesilceTaskObj *obj);
unsigned int Wos_Timeslice_Get_Task_Timeslice_Len(Wos_TimesilceTaskObj *obj);

#endif


4.Wos_Timesilce.c

/*
 * Wos_Timesilce.c
 *
 *  Created on: 2023/12/5
 *      Author: WZX
 */
 
 
#include "Wos_Timesilce.h"
#include <stddef.h>
#include <string.h>


static WOS_LIST_HEAD(timeslice_task_list);


/*********************************************************************
 * @fn      Wos_Timeslice_Exec
 *
 * @brief   任务进程运行.
 *
 * @param   none
 *
 * @return  none
 */
void Wos_Timeslice_Exec(void)
{
    Wos_ListObj *node;
    Wos_TimesilceTaskObj *task;

    Wos_List_For_Each(node, &timeslice_task_list)
    {
        task = Wos_List_Entry(node, Wos_TimesilceTaskObj, timeslice_task_list);
        if(task->is_run == Wos_TASK_RUN)
        {
            task->task_hdl(NULL);
            task->is_run = Wos_TASK_STOP;
        }
    }
}


/*********************************************************************
 * @fn      Wos_Timeslice_Tick
 *
 * @brief   任务进程tick.
 *
 * @param   none
 *
 * @return  none
 */
void Wos_Timeslice_Tick(void)
{
    Wos_ListObj *node;
    Wos_TimesilceTaskObj *task;

    Wos_List_For_Each(node, &timeslice_task_list)
    {
        task = Wos_List_Entry(node, Wos_TimesilceTaskObj, timeslice_task_list);
        if(task->timer != 0)
        {
            task->timer--;
            if(task->timer == 0)
            {
                task->is_run = Wos_TASK_RUN;
                task->timer = task->timeslice_len;
            }
        }
    }
}


/*********************************************************************
 * @fn      Wos_Timeslice_Task_Init
 *
 * @brief   任务初始化.
 *
 * @param   obj:任务进程
 *
 * @param	task_hdl:任务函数指针
 *
 * @param	id:运行优先级
 *
 * @param	timeslice_len:运行时间
 *
 * @return  none
 */
void Wos_Timeslice_Task_Init(const char *name, Wos_TimesilceTaskObj *obj, \
							void (*task_hdl)(void *pvParameters), unsigned int id, unsigned int timeslice_len)
{
	if(obj == NULL)
	{
		return;
	}
	memset(obj, 0, sizeof(struct timesilce));
    strncpy(obj->name, name, Wos_Task_Name_MAX);
	
    obj->id = id;
    obj->is_run = Wos_TASK_STOP;
    obj->task_hdl = task_hdl;
    obj->timer = timeslice_len;
    obj->timeslice_len = timeslice_len;
	
	Wos_Timeslice_Task_Add(obj);
}


/*********************************************************************
 * @fn      Wos_Timeslice_Task_Add
 *
 * @brief   添加任务.
 *
 * @param   obj:任务进程
 *
 * @return  none
 */
void Wos_Timeslice_Task_Add(Wos_TimesilceTaskObj *obj)
{
    Wos_List_Insert_Before(&timeslice_task_list, &obj->timeslice_task_list);
}


/*********************************************************************
 * @fn      Wos_Timeslice_Task_Del
 *
 * @brief   删除任务进程.
 *
 * @param   obj:任务进程
 *
 * @return  none
 */
void Wos_Timeslice_Task_Del(Wos_TimesilceTaskObj *obj)
{
    if (Wos_Timeslice_Task_Isexist(obj))
	{
        Wos_List_Remove(&obj->timeslice_task_list);
	}
    else
	{
        return;
	}
}


/*********************************************************************
 * @fn      Wos_Timeslice_Get_Task_Num
 *
 * @brief   获取任务数量.
 *
 * @param   none
 *
 * @return  none
 */
unsigned int Wos_Timeslice_Get_Task_Num(void)
{
    return Wos_List_Len(&timeslice_task_list);
}


/*********************************************************************
 * @fn      Wos_Timeslice_Task_Isexist
 *
 * @brief   退出任务.
 *
 * @param   obj:任务节点
 *
 * @return  none
 */
unsigned char Wos_Timeslice_Task_Isexist(Wos_TimesilceTaskObj *obj)
{
    unsigned char isexist = 0;
    Wos_ListObj *node;
    Wos_TimesilceTaskObj *task;

    Wos_List_For_Each(node, &timeslice_task_list)
    {
        task = Wos_List_Entry(node, Wos_TimesilceTaskObj, timeslice_task_list);
        if (obj->id == task->id)
		{
            isexist = 1;
		}
    }

    return isexist;
}


/*********************************************************************
 * @fn      Wos_Timeslice_Get_Task_Timeslice_Len
 *
 * @brief   获取任务长度.
 *
 * @param   obj:任务节点
 *
 * @return  none
 */
unsigned int Wos_Timeslice_Get_Task_Timeslice_Len(Wos_TimesilceTaskObj *obj)
{
    return obj->timeslice_len;
}


5.例程

在1ms的定时器里面加入Wos_Timeslice_Tick函数
 

/**
  * @brief This function handles System tick timer.
  */
void SysTick_Handler(void)
{
  /* USER CODE BEGIN SysTick_IRQn 0 */
	Wos_Timeslice_Tick();
  /* USER CODE END SysTick_IRQn 0 */
  HAL_IncTick();
  /* USER CODE BEGIN SysTick_IRQn 1 */

  /* USER CODE END SysTick_IRQn 1 */
}

在main的while(1)里面添加Wos_Timeslice_Exec函数

  while (1)
  {
    /* USER CODE END WHILE */

    /* USER CODE BEGIN 3 */
	  Wos_Timeslice_Exec();
  }

注册函数

/*
 * Task_SystemTick.c
 *
 *  Created on: 2024/5/28
 *      Author: WZX
 */


#include "Task_System.h"
#include "Wos_Timesilce.h"
#include "dAutoInit.h"


#define TASK_1MS_TIME				1
#define TASK_1MS_PRIO				0 
Wos_TimesilceTaskObj				task1ms_handle;

#define TASK_10MS_TIME				10
#define TASK_10MS_PRIO				1 
Wos_TimesilceTaskObj 				task10ms_handle;

#define TASK_20MS_TIME				20
#define TASK_20MS_PRIO				2 
Wos_TimesilceTaskObj 				task20ms_handle;

#define TASK_50MS_TIME				50
#define TASK_50MS_PRIO				3 
Wos_TimesilceTaskObj 				task50ms_handle;

#define TASK_100MS_TIME				100
#define TASK_100MS_PRIO				4 
Wos_TimesilceTaskObj 				task100ms_handle;


/*********************************************************************
 * @fn      taskInit
 *
 * @brief   任务初始化.
 *
 * @param   none
 *
 * @return  none
 */
static void taskInit(void)
{
	Cmd_Debug_Init();
	Cmd_UART_Init();
	Cmd_Queue_Init();
	Cmd_Button_Init();
	Cmd_RunLED_Init();
	
	Wos_Timeslice_Task_Init("1ms",		&task1ms_handle, 	task1ms,	TASK_1MS_PRIO, 		TASK_1MS_TIME);
	Wos_Timeslice_Task_Init("10ms", 	&task10ms_handle, 	task10ms, 	TASK_10MS_PRIO, 	TASK_10MS_TIME);
	Wos_Timeslice_Task_Init("20ms", 	&task20ms_handle, 	task20ms, 	TASK_20MS_PRIO, 	TASK_20MS_TIME);
	Wos_Timeslice_Task_Init("50ms", 	&task50ms_handle, 	task50ms, 	TASK_50MS_PRIO, 	TASK_50MS_TIME);
	Wos_Timeslice_Task_Init("100ms", 	&task100ms_handle, 	task100ms, 	TASK_100MS_PRIO, 	TASK_100MS_TIME);
}
__Function_AUTOInit(taskInit);


/*********************************************************************
 * @fn      task1ms
 *
 * @brief   1ms任务.
 *
 * @param   none
 *
 * @return  none
 */
void task1ms(void *pvParameters)
{

}


/*********************************************************************
 * @fn      task10ms
 *
 * @brief   10ms任务.
 *
 * @param   none
 *
 * @return  none
 */
void task10ms(void *pvParameters)
{
	Task_UART();     
}


/*********************************************************************
 * @fn      task20ms
 *
 * @brief   20ms任务.
 *
 * @param   none
 *
 * @return  none
 */
void task20ms(void *pvParameters)
{
	Task_Button();
}   


/*********************************************************************
 * @fn      task50ms
 *
 * @brief   50ms任务.
 *
 * @param   none
 *
 * @return  none
 */
void task50ms(void *pvParameters)
{

}


/*********************************************************************
 * @fn      task100ms
 *
 * @brief   100ms任务.
 *
 * @param   none
 *
 * @return  none
 */
void task100ms(void *pvParameters)
{
	Task_RunLED();
	Task_Debug();
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值