1 #include <twr_scheduler.h>
2 #include <twr_system.h>
21 void application_idle();
22 void application_error(twr_error_t code);
26 memset(&_twr_scheduler, 0,
sizeof(_twr_scheduler));
37 for (*task_id = 0; *task_id <= _twr_scheduler.max_task_id; (*task_id)++)
39 if (_twr_scheduler.pool[*task_id].task != NULL)
41 if (_twr_scheduler.tick_spin >= _twr_scheduler.pool[*task_id].tick_execution)
45 _twr_scheduler.pool[*task_id].task(_twr_scheduler.pool[*task_id].param);
57 if (_twr_scheduler.pool[i].task == NULL)
59 _twr_scheduler.pool[i].tick_execution = tick;
60 _twr_scheduler.pool[i].task = task;
61 _twr_scheduler.pool[i].param = param;
63 if (_twr_scheduler.max_task_id < i)
65 _twr_scheduler.max_task_id = i;
72 application_error(TWR_ERROR_NOT_ENOUGH_TASKS);
81 application_error(TWR_ERROR_INVALID_PARAMETER);
83 _twr_scheduler.pool[task_id].task = NULL;
85 if (_twr_scheduler.max_task_id == task_id)
89 if (_twr_scheduler.max_task_id == 0)
94 _twr_scheduler.max_task_id--;
96 }
while (_twr_scheduler.pool[_twr_scheduler.max_task_id].task == NULL);
102 return _twr_scheduler.current_task_id;
107 return _twr_scheduler.tick_spin;
114 application_error(TWR_ERROR_INVALID_PARAMETER);
116 _twr_scheduler.pool[task_id].tick_execution = 0;
123 application_error(TWR_ERROR_INVALID_PARAMETER);
125 _twr_scheduler.pool[task_id].tick_execution = tick;
132 application_error(TWR_ERROR_INVALID_PARAMETER);
134 _twr_scheduler.pool[task_id].tick_execution = _twr_scheduler.tick_spin + tick;
141 application_error(TWR_ERROR_INVALID_PARAMETER);
143 _twr_scheduler.pool[task_id].tick_execution =
twr_tick_get() + tick;
148 _twr_scheduler.pool[_twr_scheduler.current_task_id].tick_execution = 0;
153 _twr_scheduler.pool[_twr_scheduler.current_task_id].tick_execution = tick;
158 _twr_scheduler.pool[_twr_scheduler.current_task_id].tick_execution = _twr_scheduler.tick_spin + tick;
163 _twr_scheduler.pool[_twr_scheduler.current_task_id].tick_execution =
twr_tick_get() + tick;
void twr_scheduler_run(void)
Run task scheduler (this call never ends)
void twr_scheduler_plan_current_from_now(twr_tick_t tick)
Schedule current task to tick relative from now.
void twr_scheduler_plan_current_relative(twr_tick_t tick)
Schedule current task to tick relative from current spin.
void twr_scheduler_plan_current_absolute(twr_tick_t tick)
Schedule current task to absolute tick.
void twr_scheduler_plan_absolute(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to absolute tick.
void twr_scheduler_init(void)
Initialize task scheduler.
void twr_scheduler_unregister(twr_scheduler_task_id_t task_id)
Unregister specified task.
void twr_scheduler_plan_from_now(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from now.
size_t twr_scheduler_task_id_t
Task ID assigned by scheduler.
void twr_scheduler_plan_now(twr_scheduler_task_id_t task_id)
Schedule specified task for immediate execution.
void twr_scheduler_plan_current_now(void)
Schedule current task for immediate execution.
twr_tick_t twr_scheduler_get_spin_tick(void)
Get current tick of spin in which task has been run.
#define TWR_SCHEDULER_MAX_TASKS
Maximum number of tasks.
void twr_scheduler_plan_relative(twr_scheduler_task_id_t task_id, twr_tick_t tick)
Schedule specified task to tick relative from current spin.
twr_scheduler_task_id_t twr_scheduler_get_current_task_id(void)
Get task ID of currently executing task.
twr_scheduler_task_id_t twr_scheduler_register(void(*task)(void *), void *param, twr_tick_t tick)
Register task in scheduler.
#define TWR_TICK_INFINITY
Maximum timestamp value.
twr_tick_t twr_tick_get(void)
Get absolute timestamp since start of program.
uint64_t twr_tick_t
Timestamp data type.