task #5
20
srv/task.c
20
srv/task.c
@ -11,6 +11,7 @@
|
|||||||
#include "task.h"
|
#include "task.h"
|
||||||
#include "../drv/stk.h"
|
#include "../drv/stk.h"
|
||||||
#include "error.h"
|
#include "error.h"
|
||||||
|
#include "debug.h"
|
||||||
|
|
||||||
|
|
||||||
//--local definitions-----------------------------------------------------------
|
//--local definitions-----------------------------------------------------------
|
||||||
@ -21,7 +22,6 @@ struct TaskContext {
|
|||||||
Task function;
|
Task function;
|
||||||
uint32_t delay_ms;
|
uint32_t delay_ms;
|
||||||
uint8_t state;
|
uint8_t state;
|
||||||
uint8_t priority;
|
|
||||||
};
|
};
|
||||||
|
|
||||||
void task_callback(void);
|
void task_callback(void);
|
||||||
@ -39,16 +39,18 @@ void task_start_scheduler(void)
|
|||||||
{
|
{
|
||||||
while (1) {
|
while (1) {
|
||||||
uint32_t delay = UINT32_MAX;
|
uint32_t delay = UINT32_MAX;
|
||||||
for (uint8_t priority = 0; priority < UINT8_MAX; ++priority) {
|
|
||||||
|
|
||||||
bool task_runned = false;
|
bool task_runned = false;
|
||||||
for (uint8_t i = 0; i < task_nb; ++i) {
|
for (uint8_t i = 0; i < task_nb; ++i) {
|
||||||
if (task_list[i].function != nullptr) {
|
if (task_list[i].function != nullptr) {
|
||||||
if (task_list[i].priority == priority) {
|
|
||||||
if (task_list[i].delay_ms == 0) {
|
if (task_list[i].delay_ms == 0) {
|
||||||
task_runned = true;
|
task_runned = true;
|
||||||
|
debug_trace("%u", 1);
|
||||||
|
debug_trace("%u", i + 2);
|
||||||
struct TaskReturn ret
|
struct TaskReturn ret
|
||||||
= task_list[i].function(task_list[i].state);
|
= task_list[i].function(task_list[i].state);
|
||||||
|
debug_trace("%u", i + 2);
|
||||||
|
debug_trace("%u", 1);
|
||||||
if (ret.state == _TASK_COUNT_EXIT) {
|
if (ret.state == _TASK_COUNT_EXIT) {
|
||||||
task_list[i].function = nullptr;
|
task_list[i].function = nullptr;
|
||||||
} else {
|
} else {
|
||||||
@ -61,14 +63,14 @@ void task_start_scheduler(void)
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (task_runned) {
|
if (task_runned) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
if (delay > 0) {
|
if (delay > 0) {
|
||||||
|
debug_trace("%u", 1);
|
||||||
|
debug_trace("%u", 0);
|
||||||
stk_configure(delay * 1000, task_callback);
|
stk_configure(delay * 1000, task_callback);
|
||||||
stk_start();
|
stk_start();
|
||||||
__asm("wfi");
|
__asm("wfi");
|
||||||
@ -83,13 +85,16 @@ void task_start_scheduler(void)
|
|||||||
task_list[i].delay_ms -= delay;
|
task_list[i].delay_ms -= delay;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
debug_trace("%u", 0);
|
||||||
|
debug_trace("%u", 1);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
//never returns
|
//never returns
|
||||||
}
|
}
|
||||||
|
|
||||||
void task_start(Task task, uint8_t priority)
|
void task_start(Task task)
|
||||||
{
|
{
|
||||||
for (uint8_t i = 0; i < MAX_TASK_NB; ++i) {
|
for (uint8_t i = 0; i < MAX_TASK_NB; ++i) {
|
||||||
if (task_list[i].function == nullptr) {
|
if (task_list[i].function == nullptr) {
|
||||||
@ -97,7 +102,6 @@ void task_start(Task task, uint8_t priority)
|
|||||||
task_list[i].function = task;
|
task_list[i].function = task;
|
||||||
task_list[i].delay_ms = 0;
|
task_list[i].delay_ms = 0;
|
||||||
task_list[i].state = 0;
|
task_list[i].state = 0;
|
||||||
task_list[i].priority = priority;
|
|
||||||
|
|
||||||
++task_nb;
|
++task_nb;
|
||||||
return;
|
return;
|
||||||
@ -133,9 +137,9 @@ bool task_is_running(Task task)
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
//--local functions-------------------------------------------------------------
|
//--local functions-------------------------------------------------------------
|
||||||
|
|
||||||
void task_callback(void) {
|
void task_callback(void) {
|
||||||
stk_stop();
|
stk_stop();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@ -48,7 +48,7 @@ typedef struct TaskReturn(*Task)(uint8_t);
|
|||||||
|
|
||||||
void task_start_scheduler(void);
|
void task_start_scheduler(void);
|
||||||
|
|
||||||
void task_start(Task task, uint8_t priority);
|
void task_start(Task task);
|
||||||
void task_stop(Task task);
|
void task_stop(Task task);
|
||||||
bool task_is_running(Task task);
|
bool task_is_running(Task task);
|
||||||
|
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user