简介
timeslice是一个时间片轮询框架,他是一个完全解耦的时间片轮询框架,他的使用非常方便,该项目一共有四个文件分别是tieslice的头文件和源文件以及list的头文件和源文件,tieslice是负责轮询任务,list是一个双向链表负责任务的管理,在Linux内核中使用非常广泛也很经典,该框架是参考rtt实时操作系统的侵入式链表实现的,本章文章是将该框架移植到stm32单片机上实验,使用也非常容易,单片机只需要启用一个定时器作为时钟即可;
本章使用环境:
stm32f407vet6
代码工程使用cubemx创建
项目代码
该项目的代码是我在微信公众号上看到的一个文章,代码并没有上传在github上,这里直接贴上源代码;
timeslice.h
#ifndef _TIMESLICE_H #define _TIMESLICE_H #include "list.h" typedef enum { TASK_STOP, TASK_RUN } IsTaskRun; typedef struct timesilce { unsigned int id; void (*task_hdl)(void); IsTaskRun is_run; unsigned int timer; unsigned int timeslice_len; ListObj timeslice_task_list; } TimesilceTaskObj; void timeslice_exec(void); void timeslice_tick(void); void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len); void timeslice_task_add(TimesilceTaskObj* obj); void timeslice_task_del(TimesilceTaskObj* obj); unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj); unsigned int timeslice_get_task_num(void); unsigned char timeslice_task_isexist(TimesilceTaskObj* obj); #endif
timeslice.c
#include "timeslice.h" static LIST_HEAD(timeslice_task_list); void timeslice_exec() { ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (task->is_run == TASK_RUN) { task->task_hdl(); task->is_run = TASK_STOP; } } } void timeslice_tick() { ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (task->timer != 0) { task->timer--; if (task->timer == 0) { task->is_run = TASK_RUN; task->timer = task->timeslice_len; } } } } unsigned int timeslice_get_task_num() { return list_len(×lice_task_list); } void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len) { obj->id = id; obj->is_run = TASK_STOP; obj->task_hdl = task_hdl; obj->timer = timeslice_len; obj->timeslice_len = timeslice_len; } void timeslice_task_add(TimesilceTaskObj* obj) { list_insert_before(×lice_task_list, &obj->timeslice_task_list); } void timeslice_task_del(TimesilceTaskObj* obj) { if (timeslice_task_isexist(obj)) list_remove(&obj->timeslice_task_list); else return; } unsigned char timeslice_task_isexist(TimesilceTaskObj* obj) { unsigned char isexist = 0; ListObj* node; TimesilceTaskObj* task; list_for_each(node, ×lice_task_list) { task = list_entry(node, TimesilceTaskObj, timeslice_task_list); if (obj->id == task->id) isexist = 1; } return isexist; } unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj) { return obj->timeslice_len; }
list.h
#ifndef _LIST_H #define _LIST_H #define offset_of(type, member) (unsigned long) &((type*)0)->member #define container_of(ptr, type, member) ((type *)((char *)(ptr) - offset_of(type, member))) typedef struct list_structure { struct list_structure* next; struct list_structure* prev; } ListObj; #define LIST_HEAD_INIT(name) {&(name), &(name)} #define LIST_HEAD(name) ListObj name = LIST_HEAD_INIT(name) void list_init(ListObj* list); void list_insert_after(ListObj* list, ListObj* node); void list_insert_before(ListObj* list, ListObj* node); void list_remove(ListObj* node); int list_isempty(const ListObj* list); unsigned int list_len(const ListObj* list); #define list_entry(node, type, member) \ container_of(node, type, member) #define list_for_each(pos, head) \ for (pos = (head)->next; pos != (head); pos = pos->next) #define list_for_each_safe(pos, n, head) \ for (pos = (head)->next, n = pos->next; pos != (head); \ pos = n, n = pos->next) #endif
list.c
#include "list.h" void list_init(ListObj* list) { list->next = list->prev = list; } void list_insert_after(ListObj* list, ListObj* node) { list->next->prev = node; node->next = list->next; list->next = node; node->prev = list; } void list_insert_before(ListObj* list, ListObj* node) { list->prev->next = node; node->prev = list->prev; list->prev = node; node->next = list; } void list_remove(ListObj* node) { node->next->prev = node->prev; node->prev->next = node->next; node->next = node->prev = node; } int list_isempty(const ListObj* list) { return list->next == list; } unsigned int list_len(const ListObj* list) { unsigned int len = 0; const ListObj* p = list; while (p->next != list) { p = p->next; len++; } return len; }
【嵌入式开源库】timeslice的使用,完全解耦的时间片轮询框架构(二)https://developer.aliyun.com/article/1472622