Hi all. We are writing a RTOS for an ATMega16, just getting the skeleton done. We're implementing some sort of time slicing system thus:
1. A background task is running.
2. After a set amount of time (in the realm of microseconds), an interrupt is triggered.
3. Within the ISR the "current task" is updated to the next one.
That's the idea anyway. Here's what we've written, our questions are below:
Code:
#include "config.h"
#include "pio.h"
#define TASK_DEFINE(INDEX, FUNCTION) (task_t){index, function)
#define MAX_TASKS 3
typedef struct task_struct {
uint8_t index;
void (*function)(void);
} task_t;
/* Set up the task structures. */
task_t task1;
task1.index = 1;
task1.function = &display_update;
task_t task2;
task2.index = 2;
task2.function = &sensor_sample;
task_t task3;
task3.index = 3;
task3.function = &piezo_update;
/* Create an array of task structures. */
task_t task_list[MAX_TASKS] = {task1, task2, task3};
task_t *current_task = &task_list[0];
/* These are the actual background tasks */
void
display_update(void)
{
// .. do something
}
void
sensor_sample(void)
{
// ..do something else
}
void
piezo_update(void)
{
// ..do something else
}
/* ISR */
SIGNAL(SIG_OUTPUT_COMPARE1A)
{
if (current_task < MAX_TASKS)
current_task++;
else
current_task = &task_list[0];
}
void
task_execute(void)
{
current_task->function;
}
void
main(void)
{
TIMSK = 0x10;
TCCR1B = 0x05 | 0x08; // set prescaler and waveform bits stuff resp.
OCR1A = 150;
sei();
while (1)
{
task_execute(current_task);
}
}
So the first task (display_update) will be running, then once the timer hits the magic value the ISR will be called, and it will update the "current task" to the next one. Ok, fine. But then won't the ISR simply return back to the function we were in before the ISR was called? I.e., straight back to display_update without ever getting to the next task?
It's almost like we need a mechanism in the ISR to tell it to go back to the start of the while(1) loop after it updates the task so that task_execute can be called again...
How could we go about this? Thanks for any replies, I know this is a convoluted question but we're really stuck...