This repository has been archived on 2024-03-18. You can view files and clone it, but cannot push or open issues or pull requests.
game-ctrl-01/PontCoopScheduler.c

91 lines
1.9 KiB
C
Raw Permalink Normal View History

2024-03-07 15:51:44 +01:00
/*
* PontCoopScheduler.c
*
* Created on: 29.08.2016
* Author: wn
*/
#include <stdlib.h>
2024-03-08 13:12:55 +01:00
#include <msp430g2553.h>
2024-03-07 15:51:44 +01:00
#include "PontCoopScheduler.h"
tTask tasks[MAX_NUM_OF_TASKS];
void schInit() {
2024-03-08 13:12:55 +01:00
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
2024-03-07 15:51:44 +01:00
tasks[i].delay = 0;
tasks[i].period = 0;
tasks[i].run = 0;
tasks[i].exec = NULL;
tasks[i].handle = NULL;
}
}
void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period) {
2024-03-08 13:12:55 +01:00
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
2024-03-07 15:51:44 +01:00
if (tasks[i].exec == NULL) {
tasks[i].delay = delay;
tasks[i].period = period;
if (delay == 0 && period == 0) {
tasks[i].run = 1;
} else {
tasks[i].run = 0;
}
tasks[i].exec = exec;
tasks[i].handle = handle;
break;
}
}
}
2024-03-08 13:12:55 +01:00
/*
2024-03-07 15:51:44 +01:00
void schDel(void (*exec)(void *), void *handle) {
2024-03-08 13:12:55 +01:00
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
2024-03-07 15:51:44 +01:00
if ((tasks[i].exec == exec) && (tasks[i].handle == handle)) {
tasks[i].exec = NULL;
break;
}
}
}
2024-03-08 13:12:55 +01:00
*/
2024-03-07 15:51:44 +01:00
void schExec() {
2024-03-08 13:12:55 +01:00
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
// synchronize access to tasks[].run
__disable_interrupt();
2024-03-07 15:51:44 +01:00
if (tasks[i].exec != NULL && tasks[i].run > 0) {
tasks[i].run--;
2024-03-08 13:12:55 +01:00
// synchronize access to tasks[].run
// reenable interrupts before actually executing task
__enable_interrupt();
2024-03-07 15:51:44 +01:00
tasks[i].exec(tasks[i].handle);
if (tasks[i].period == 0) {
tasks[i].exec = NULL;
}
2024-03-08 13:12:55 +01:00
} else {
// synchronize access to tasks[].run
// reenable interrupts in case task is not yet executable
__enable_interrupt();
2024-03-07 15:51:44 +01:00
}
}
}
void schUpdate() {
2024-03-08 13:12:55 +01:00
for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
2024-03-07 15:51:44 +01:00
if (tasks[i].exec != NULL) {
if (tasks[i].delay == 0) {
tasks[i].delay = tasks[i].period;
tasks[i].run++;
} else {
tasks[i].delay--;
}
}
}
}