Thread: Writing a preemptive multitasking system

  1. #1
    Registered User
    Join Date
    Dec 2009
    Posts
    9

    Writing a preemptive multitasking system

    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...

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,659
    When the ISR is triggered, you switch to a supervisor stack, so you can do whatever code you need to do.

    When you're about to resume a task, simply select the stack (move the stack pointer) and do an "IRET" (or whatever return from ISR is).

    Each active task will have been interrupted by an ISR. The only difference is, it will be a different ISR event which resumes it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. beginner help!!!
    By robski in forum C++ Programming
    Replies: 4
    Last Post: 03-23-2010, 01:26 PM
  2. How to model a system?
    By ovid in forum C++ Programming
    Replies: 1
    Last Post: 03-05-2010, 09:18 AM
  3. Reliable UDP communication for merging events in a distributed system
    By dwks in forum Networking/Device Communication
    Replies: 1
    Last Post: 12-26-2009, 05:36 PM
  4. Opinions on custom system build
    By lightatdawn in forum Tech Board
    Replies: 2
    Last Post: 10-18-2005, 04:15 AM
  5. Problem With My Box
    By HaVoX in forum Tech Board
    Replies: 9
    Last Post: 10-15-2005, 07:38 AM