synchronize scheduler, now it works

This commit is contained in:
Wolfgang Hottgenroth 2024-03-08 13:12:55 +01:00
parent 8da88e96c2
commit a4adf6ac27
5 changed files with 20 additions and 34 deletions

View File

@ -7,6 +7,7 @@
#include <stdlib.h> #include <stdlib.h>
#include <msp430g2553.h>
#include "PontCoopScheduler.h" #include "PontCoopScheduler.h"
@ -15,7 +16,7 @@ tTask tasks[MAX_NUM_OF_TASKS];
void schInit() { void schInit() {
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
tasks[i].delay = 0; tasks[i].delay = 0;
tasks[i].period = 0; tasks[i].period = 0;
tasks[i].run = 0; tasks[i].run = 0;
@ -25,7 +26,7 @@ void schInit() {
} }
void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period) { void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period) {
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
if (tasks[i].exec == NULL) { if (tasks[i].exec == NULL) {
tasks[i].delay = delay; tasks[i].delay = delay;
tasks[i].period = period; tasks[i].period = period;
@ -41,23 +42,34 @@ void schAdd(void (*exec)(void *), void *handle, uint32_t delay, uint32_t period)
} }
} }
/*
void schDel(void (*exec)(void *), void *handle) { void schDel(void (*exec)(void *), void *handle) {
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
if ((tasks[i].exec == exec) && (tasks[i].handle == handle)) { if ((tasks[i].exec == exec) && (tasks[i].handle == handle)) {
tasks[i].exec = NULL; tasks[i].exec = NULL;
break; break;
} }
} }
} }
*/
void schExec() { void schExec() {
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
// synchronize access to tasks[].run
__disable_interrupt();
if (tasks[i].exec != NULL && tasks[i].run > 0) { if (tasks[i].exec != NULL && tasks[i].run > 0) {
tasks[i].run--; tasks[i].run--;
// synchronize access to tasks[].run
// reenable interrupts before actually executing task
__enable_interrupt();
tasks[i].exec(tasks[i].handle); tasks[i].exec(tasks[i].handle);
if (tasks[i].period == 0) { if (tasks[i].period == 0) {
tasks[i].exec = NULL; tasks[i].exec = NULL;
} }
} else {
// synchronize access to tasks[].run
// reenable interrupts in case task is not yet executable
__enable_interrupt();
} }
} }
} }
@ -65,7 +77,7 @@ void schExec() {
void schUpdate() { void schUpdate() {
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) { for (uint16_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
if (tasks[i].exec != NULL) { if (tasks[i].exec != NULL) {
if (tasks[i].delay == 0) { if (tasks[i].delay == 0) {
tasks[i].delay = tasks[i].period; tasks[i].delay = tasks[i].period;
@ -76,13 +88,3 @@ void schUpdate() {
} }
} }
} }
uint8_t schTaskCnt() {
uint8_t cnt = 0;
for (uint8_t i = 0; i < MAX_NUM_OF_TASKS; i++) {
if (tasks[i].exec != NULL){
cnt++;
}
}
return cnt;
}

4
led.c
View File

@ -21,7 +21,7 @@ void ledBlueOff() {
} }
void ledExec(void *args) { void ledExec(void *args) {
static int i = 0; static uint16_t i = 0;
if (i == 0) { if (i == 0) {
ledGreenOff(); ledGreenOff();
@ -41,7 +41,7 @@ void ledInit() {
ledBlueOff(); ledBlueOff();
// schAdd(ledExec, NULL, 0, 50); schAdd(ledExec, NULL, 0, 500);
} }

2
main.c
View File

@ -29,7 +29,7 @@ int main() {
ledInit(); ledInit();
displayDriverInit(); displayDriverInit();
canvasInit(); canvasInit();
displayTestInit(); displayTestInit();
__enable_interrupt(); __enable_interrupt();

14
time.c
View File

@ -1,30 +1,16 @@
#include <msp430g2553.h> #include <msp430g2553.h>
#include <stdint.h>
#include "time.h" #include "time.h"
#include "PontCoopScheduler.h" #include "PontCoopScheduler.h"
volatile uint32_t timestamp;
void __attribute__ ((interrupt (TIMER0_A0_VECTOR))) ta0_isr() { void __attribute__ ((interrupt (TIMER0_A0_VECTOR))) ta0_isr() {
timestamp++;
schUpdate(); schUpdate();
} }
void timeInit() { void timeInit() {
timestamp = 0;
TACCR0 = 32; TACCR0 = 32;
TACCTL0 = CCIE; TACCTL0 = CCIE;
TACTL = MC_1 | ID_0 | TASSEL_1 | TACLR; TACTL = MC_1 | ID_0 | TASSEL_1 | TACLR;
} }
uint32_t getMillis() {
return timestamp;
}
void ms_active_delay(uint16_t delay) {
uint32_t start = timestamp;
while (start + delay > timestamp);
}

2
time.h
View File

@ -4,8 +4,6 @@
#include <stdint.h> #include <stdint.h>
void timeInit(); void timeInit();
uint32_t getMillis();
void ms_active_delay(uint16_t delay);