-
Notifications
You must be signed in to change notification settings - Fork 0
/
esch.c
144 lines (123 loc) · 3.46 KB
/
esch.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/**
* @file esch.c
* @author Sergey Tkachenko ([email protected])
* @brief
* @version 0.0.1
* @date 2021-05-09
*
* @copyright Copyright (c) 2021
*
*/
#include "esch.h"
#include <string.h>
// static uint8_t esch_tick_flag_get();
static esch_task_t task_pool[ESCH_TASK_NUM + 1];
static volatile uint32_t tick = 0;
// static volatile uint8_t tick_flag = 0;
void esch_init()
{
memset(task_pool, 0, sizeof(task_pool));
for (uint16_t task_prio = 0; task_prio < (ESCH_TASK_NUM + 1); task_prio++) {
task_pool[task_prio].priority = task_prio;
task_pool[task_prio].function = NULL;
if (task_prio == ESCH_TASK_NUM) {
strcpy(task_pool[task_prio].name, "ESCH_IDLE");
task_pool[ESCH_TASK_NUM].interval = -1;
}
}
esch_port_init();
}
esch_task_t* esch_task_create(const char* name, uint32_t interval, uint16_t prio, esch_task_fn func, void* arg)
{
// check if desired priority is out of bounds
if (prio >= ESCH_TASK_NUM) {
return NULL;
} else {
// check if function with same priority was already defined in the pool
if (task_pool[prio].function) {
return NULL;
} else {
strcpy(task_pool[prio].name, name);
task_pool[prio].elapsed = 0;
task_pool[prio].priority = prio;
task_pool[prio].interval = interval;
task_pool[prio].function = func;
#if ESCH_USE_PROFILER == 1
task_pool[prio].profiler.exec_mcutick_count = 0;
#endif
if (arg) {
task_pool[prio].arg = arg;
}
}
}
return &task_pool[prio];
}
void esch_task_delete(esch_task_t* task_handler)
{
if (task_handler) {
for (uint16_t i = 0; i < ESCH_TASK_NUM; i++) {
if (task_handler == &task_pool[i]) {
task_pool[i].function = NULL;
break;
}
}
}
}
esch_task_t* esch_idle_task_register_callback(esch_task_fn func)
{
task_pool[ESCH_TASK_NUM].function = func;
task_pool[ESCH_TASK_NUM].interval = 0;
return &task_pool[ESCH_TASK_NUM];
}
void esch_task_interval_set(esch_task_t* task_handle, uint32_t interval)
{
for (uint16_t task_prio = 0; task_prio < ESCH_TASK_NUM; task_prio++) {
if (task_handle == &task_pool[task_prio]) {
task_pool[task_prio].interval = interval;
}
}
}
void esch_tick_advance()
{
tick++;
}
uint32_t esch_tick_get()
{
return tick;
}
void esch_run()
{
tick = 0;
for (;;) {
for (uint16_t i = 0; i < (ESCH_TASK_NUM + 1); i++) {
esch_task_t* active_task = &task_pool[i];
if (active_task->function) {
volatile uint32_t tick_curr = tick;
uint32_t time_delta = (tick_curr - active_task->elapsed);
if (time_delta >= active_task->interval) {
#if ESCH_USE_PROFILER == 1
esch_profiler_clear();
#endif
active_task->function(active_task->arg);
active_task->elapsed = tick;
#if ESCH_USE_PROFILER == 1
active_task->profiler.exec_mcutick_count = esch_profiler_count_get();
#endif
}
}
}
#if ESCH_USE_PROFILER == 1
asm("nop");
#endif
// tick_flag = 0;
// while (!esch_tick_flag_get()) { }
}
}
inline uint32_t esch_ms_to_ticks(uint32_t msec)
{
return msec * ESCH_TICK_FREQ / 1000U;
}
// static uint8_t esch_tick_flag_get()
// {
// return tick_flag;
// }