来源:ERYUESANHI
大家好,我是肖遥,今天分享单片机裸机程序框架的文章。
1
总程序架构的重要性
2
参考代码
1#include
2#include
3#include
4#include "timeslice.h"
5
6// 创建5个任务对象
7TimesilceTaskObj task_1, task_2, task_3, task_4, task_5;
8
9// 具体的任务函数
10void task1_hdl()
11{
12 printf(">> task 1 is running ...\n");
13}
14
15void task2_hdl()
16{
17 printf(">> task 2 is running ...\n");
18}
19
20void task3_hdl()
21{
22 printf(">> task 3 is running ...\n");
23}
24
25void task4_hdl()
26{
27 printf(">> task 4 is running ...\n");
28}
29
30void task5_hdl()
31{
32 printf(">> task 5 is running ...\n");
33}
34
35// 初始化任务对象,并且将任务添加到时间片轮询调度中
36void task_init()
37{
38 timeslice_task_init(&task_1, task1_hdl, 1, 10);
39 timeslice_task_init(&task_2, task2_hdl, 2, 20);
40 timeslice_task_init(&task_3, task3_hdl, 3, 30);
41 timeslice_task_init(&task_4, task4_hdl, 4, 40);
42 timeslice_task_init(&task_5, task5_hdl, 5, 50);
43 timeslice_task_add(&task_1);
44 timeslice_task_add(&task_2);
45 timeslice_task_add(&task_3);
46 timeslice_task_add(&task_4);
47 timeslice_task_add(&task_5);
48}
49
50
51// 开两个线程模拟在单片机上的运行过程
52void timeslice_exec_thread()
53{
54 while (true)
55 {
56 timeslice_exec();
57 }
58}
59
60void timeslice_tick_thread()
61{
62 while (true)
63 {
64 timeslice_tick();
65 Sleep(10);
66 }
67}
68
69int main()
70{
71 task_init();
72
73 printf(">> task num: %d\n", timeslice_get_task_num());
74 printf(">> task len: %d\n", timeslice_get_task_timeslice_len(&task_3));
75
76 timeslice_task_del(&task_2);
77 printf(">> delet task 2\n");
78 printf(">> task 2 is exist: %d\n", timeslice_task_isexist(&task_2));
79
80 printf(">> task num: %d\n", timeslice_get_task_num());
81
82 timeslice_task_del(&task_5);
83 printf(">> delet task 5\n");
84
85 printf(">> task num: %d\n", timeslice_get_task_num());
86
87 printf(">> task 3 is exist: %d\n", timeslice_task_isexist(&task_3));
88 timeslice_task_add(&task_2);
89 printf(">> add task 2\n");
90 printf(">> task 2 is exist: %d\n", timeslice_task_isexist(&task_2));
91
92 timeslice_task_add(&task_5);
93 printf(">> add task 5\n");
94
95 printf(">> task num: %d\n", timeslice_get_task_num());
96
97 printf("\n\n========timeslice running===========\n");
98
99 std::thread thread_1(timeslice_exec_thread);
100 std::thread thread_2(timeslice_tick_thread);
101
102 thread_1.join();
103 thread_2.join();
104
105
106 return 0;
107}
运行结果如下:
3
时间片轮询架构
其实该部分主要使用了面向对象的思维,使用结构体作为对象,并使用结构体指针作为参数传递,这样作可以节省资源,并且有着极高的运行效率。
1#ifndef _TIMESLICE_H
2#define _TIMESLICE_H
3
4#include "./list.h"
5
6typedef enum {
7 TASK_STOP,
8 TASK_RUN
9} IsTaskRun;
10
11typedef struct timesilce
12{
13 unsigned int id;
14 void (*task_hdl)(void);
15 IsTaskRun is_run;
16 unsigned int timer;
17 unsigned int timeslice_len;
18 ListObj timeslice_task_list;
19} TimesilceTaskObj;
20
21void timeslice_exec(void);
22void timeslice_tick(void);
23void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len);
24void timeslice_task_add(TimesilceTaskObj* obj);
25void timeslice_task_del(TimesilceTaskObj* obj);
26unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj);
27unsigned int timeslice_get_task_num(void);
28unsigned char timeslice_task_isexist(TimesilceTaskObj* obj);
29
30#endif
1#include "./timeslice.h"
2
3static LIST_HEAD(timeslice_task_list);
4
5void timeslice_exec()
6{
7 ListObj* node;
8 TimesilceTaskObj* task;
9
10 list_for_each(node, ×lice_task_list)
11 {
12
13 task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
14 if (task->is_run == TASK_RUN)
15 {
16 task->task_hdl();
17 task->is_run = TASK_STOP;
18 }
19 }
20}
21
22void timeslice_tick()
23{
24 ListObj* node;
25 TimesilceTaskObj* task;
26
27 list_for_each(node, ×lice_task_list)
28 {
29 task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
30 if (task->timer != 0)
31 {
32 task->timer--;
33 if (task->timer == 0)
34 {
35 task->is_run = TASK_RUN;
36 task->timer = task->timeslice_len;
37 }
38 }
39 }
40}
41
42unsigned int timeslice_get_task_num()
43{
44 return list_len(×lice_task_list);
45}
46
47void timeslice_task_init(TimesilceTaskObj* obj, void (*task_hdl)(void), unsigned int id, unsigned int timeslice_len)
48{
49 obj->id = id;
50 obj->is_run = TASK_STOP;
51 obj->task_hdl = task_hdl;
52 obj->timer = timeslice_len;
53 obj->timeslice_len = timeslice_len;
54}
55
56void timeslice_task_add(TimesilceTaskObj* obj)
57{
58 list_insert_before(×lice_task_list, &obj->timeslice_task_list);
59}
60
61void timeslice_task_del(TimesilceTaskObj* obj)
62{
63 if (timeslice_task_isexist(obj))
64 list_remove(&obj->timeslice_task_list);
65 else
66 return;
67}
68
69
70unsigned char timeslice_task_isexist(TimesilceTaskObj* obj)
71{
72 unsigned char isexist = 0;
73 ListObj* node;
74 TimesilceTaskObj* task;
75
76 list_for_each(node, ×lice_task_list)
77 {
78 task = list_entry(node, TimesilceTaskObj, timeslice_task_list);
79 if (obj->id == task->id)
80 isexist = 1;
81 }
82
83 return isexist;
84}
85
86unsigned int timeslice_get_task_timeslice_len(TimesilceTaskObj* obj)
87{
88 return obj->timeslice_len;
89}
4
底层侵入式双向链表
该链表是linux内核中使用十分广泛,也十分经典,其原理具体可以参考文章:
1#ifndef _LIST_H
2#define _LIST_H
3
4#define offset_of(type, member) (unsigned long) &((type*)0)->member
5#define container_of(ptr, type, member) ((type *)((char *)(ptr) - offset_of(type, member)))
6
7typedef struct list_structure
8{
9 struct list_structure* next;
10 struct list_structure* prev;
11} ListObj;
12
13#define LIST_HEAD_INIT(name) {&(name), &(name)}
14#define LIST_HEAD(name) ListObj name = LIST_HEAD_INIT(name)
15
16void list_init(ListObj* list);
17void list_insert_after(ListObj* list, ListObj* node);
18void list_insert_before(ListObj* list, ListObj* node);
19void list_remove(ListObj* node);
20int list_isempty(const ListObj* list);
21unsigned int list_len(const ListObj* list);
22
23#define list_entry(node, type, member) \
24 container_of(node, type, member)
25
26#define list_for_each(pos, head) \
27 for (pos = (head)->next; pos != (head); pos = pos->next)
28
29#define list_for_each_safe(pos, n, head) \
30 for (pos = (head)->next, n = pos->next; pos != (head); \
31 pos = n, n = pos->next)
32
33#endif
1#include "list.h"
2
3void list_init(ListObj* list)
4{
5 list->next = list->prev = list;
6}
7
8void list_insert_after(ListObj* list, ListObj* node)
9{
10 list->next->prev = node;
11 node->next = list->next;
12
13 list->next = node;
14 node->prev = list;
15}
16
17void list_insert_before(ListObj* list, ListObj* node)
18{
19 list->prev->next = node;
20 node->prev = list->prev;
21
22 list->prev = node;
23 node->next = list;
24}
25
26void list_remove(ListObj* node)
27{
28 node->next->prev = node->prev;
29 node->prev->next = node->next;
30
31 node->next = node->prev = node;
32}
33
34int list_isempty(const ListObj* list)
35{
36 return list->next == list;
37}
38
39unsigned int list_len(const ListObj* list)
40{
41 unsigned int len = 0;
42 const ListObj* p = list;
43 while (p->next != list)
44 {
45 p = p->next;
46 len++;
47 }
48
49 return len;
50}
版权声明:本文来源网络,免费传达知识,版权归原作者所有。如涉及作品版权问题,请联系我进行删除。
‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧ END ‧‧‧‧‧‧‧‧‧‧‧‧‧‧‧
关注我的微信公众号,回复“加群”按规则加入技术交流群。
点击“阅读原文”查看更多分享,欢迎点分享、收藏、点赞、在看。