os.c File Reference

A Real Time Operating System. More...

#include <avr/io.h>
#include <avr/interrupt.h>
#include "os.h"
#include "kernel.h"
#include "error_code.h"

Go to the source code of this file.

Defines

#define SAVE_CTX_TOP()
#define STACK_SREG_SET_I_BIT()
#define SAVE_CTX_BOTTOM()
#define SAVE_CTX()   SAVE_CTX_TOP();SAVE_CTX_BOTTOM();
 Push all the registers and SREG onto the stack.
#define RESTORE_CTX()
 Pop all registers and the status register.

Functions

int main ()
 main function provided by user application. The first task to run.
static void kernel_main_loop (void)
 The heart of the RTOS, the main loop where the kernel is entered and exited.
static void kernel_dispatch (void)
 The second part of the scheduler.
static void kernel_handle_request (void)
 The first part of the scheduler.
static void exit_kernel (void)
 The idle task does nothing but busy loop.
static void enter_kernel (void)
 All system calls eventually enter here.
void TIMER1_COMPA_vect (void)
 The interrupt handler for output compare interrupts on Timer 1.
static int kernel_create_task ()
 Kernel function to create a new task.
static void kernel_terminate_task (void)
 Kernel function to destroy the current task.
static void kernel_event_wait (void)
 Kernel function to place current task in a waiting queue.
static void kernel_event_signal (uint8_t is_broadcast, uint8_t and_next)
 Kernel function to signal waiting processes.
static void enqueue (queue_t *queue_ptr, task_descriptor_t *task_to_add)
 Add a task the head of the queue.
static task_descriptor_tdequeue (queue_t *queue_ptr)
 Pops head of queue and returns it.
static void kernel_update_ticker (void)
 Update the current time.
static void check_PPP_names (void)
 Validate the PPP array.
void OS_Init ()
 Setup the RTOS and create main() as the first SYSTEM level task.
static void _delay_25ms (void)
 Delay function adapted from <util/delay.h>.
void OS_Abort (void)
 Abort the execution of this RTOS due to an unrecoverable erorr.
int Task_Create (void(*f)(void), int arg, unsigned int level, unsigned int name)
void Task_Next ()
 The calling task gives up its share of the processor voluntarily.
void Task_Terminate ()
 The calling task terminates itself.
int Task_GetArg (void)
 Retrieve the assigned parameter.
EVENTEvent_Init (void)
 Initialize a new, non-NULL Event descriptor.
void Event_Wait (EVENT *e)
 Wait for the next occurrence of a signal on e. The calling process always blocks.
void Event_Signal (EVENT *e)
 Resume a single waiting task on e. It is a no-op if there is no waiting process.
void Event_Broadcast (EVENT *e)
 Resume ALL waiting tasks on e. It is a no-op if there is no waiting process.
void Signal_And_Next (EVENT *e)
 Resume a waiting task on e and at the same time relinquish the processor.
void Broadcast_And_Next (EVENT *e)
 Resume ALL waiting tasks on e and at the same time relinquish the processor.

Variables

const unsigned char PPP []
const unsigned int PT
static task_descriptor_tcur_task = NULL
static volatile uint16_t kernel_sp
static task_descriptor_t task_desc [MAXPROCESS+1]
static task_descriptor_tidle_task = &task_desc[MAXPROCESS]
static volatile kernel_request_t kernel_request
static volatile create_args_t kernel_request_create_args
static volatile int kernel_request_retval
static volatile EVENTkernel_request_event_ptr
static queue_t dead_pool_queue
static uint8_t num_events_created = 0
static queue_t rr_queue
static queue_t system_queue
static queue_t event_queue [MAXEVENT]
static uint8_t ticks_remaining = 0
static uint8_t slot_task_finished = 0
static int slot_name_index = 0
static task_descriptor_tname_to_task_ptr [MAXNAME+1]
static uint8_t name_in_PPP [MAXNAME+1]
static uint8_t volatile error_msg = ERR_RUN_1_USER_CALLED_OS_ABORT


Detailed Description

A Real Time Operating System.

Our implementation of the operating system described by Mantis Cheng in os.h.

Author:
Scott Craig

Justin Tanner

Definition in file os.c.


Define Documentation

 
#define SAVE_CTX_TOP (  ) 

Value:

asm volatile (\
    "push   r31             \n\t"\
    "in     r31,__SREG__    \n\t"\
    "cli                    \n\t"::);
It is important to keep the order of context saving and restoring exactly in reverse. Also, when a new task is created, it is important to initialize its "initial" context in the same order as a saved context.

Save r31 and SREG on stack, disable interrupts, then save the rest of the registers on the stack. In the locations this macro is used, the interrupts need to be disabled, or they already are disabled.

Definition at line 367 of file os.c.

 
#define STACK_SREG_SET_I_BIT (  ) 

Value:

asm volatile (\
    "ori    r31, 0x80        \n\t"::);

Definition at line 372 of file os.c.

 
#define SAVE_CTX_BOTTOM (  ) 

Definition at line 375 of file os.c.

 
#define SAVE_CTX (  )     SAVE_CTX_TOP();SAVE_CTX_BOTTOM();

Push all the registers and SREG onto the stack.

Definition at line 412 of file os.c.

 
#define RESTORE_CTX (  ) 

Pop all registers and the status register.

Definition at line 417 of file os.c.


Function Documentation

int main (  ) 

main function provided by user application. The first task to run.

static void kernel_main_loop ( void   )  [static]

The heart of the RTOS, the main loop where the kernel is entered and exited.

The complete function is:

Loop

  1. Select and dispatch a process to run
  2. Exit the kernel (The loop is left and re-entered here.)
  3. Handle the request from the process that was running.
  4. End loop, go to 1.

Definition at line 141 of file os.c.

static void kernel_dispatch ( void   )  [static]

The second part of the scheduler.

Chooses the next task to run.

Definition at line 165 of file os.c.

static void kernel_handle_request ( void   )  [static]

The first part of the scheduler.

Perform some action based on the system call or timer tick. Perhaps place the current process in a ready or waitng queue.

Definition at line 205 of file os.c.

static void exit_kernel ( void   )  [static]

The idle task does nothing but busy loop.

This function is called by the kernel. Upon entry, we are using the kernel stack, on top of which is the address of the instruction after the call to exit_kernel().

Assumption: Our kernel is executed with interrupts already disabled.

The "naked" attribute prevents the compiler from adding instructions to save and restore register values. It also prevents an automatic return instruction.

Definition at line 95 of file os.c.

enter_kernel ( void   )  [static]

All system calls eventually enter here.

Assumption: We are still executing on cur_task's stack. The return address of the caller of enter_kernel() is on the top of the stack.

Definition at line 514 of file os.c.

TIMER1_COMPA_vect ( void   ) 

The interrupt handler for output compare interrupts on Timer 1.

Used to enter the kernel when a tick expires.

Assumption: We are still executing on the cur_task stack. The return address inside the current task code is on the top of the stack.

The "naked" attribute prevents the compiler from adding instructions to save and restore register values. It also prevents an automatic return instruction.

Definition at line 561 of file os.c.

static int kernel_create_task (  )  [static]

Kernel function to create a new task.

When creating a new task, it is important to initialize its stack just like it has called "enter_kernel()"; so that when we switch to it later, we can just restore its execution context on its stack.

See also:
enter_kernel

Definition at line 632 of file os.c.

static void kernel_terminate_task ( void   )  [static]

Kernel function to destroy the current task.

Definition at line 755 of file os.c.

static void kernel_event_wait ( void   )  [static]

Kernel function to place current task in a waiting queue.

Definition at line 770 of file os.c.

static void kernel_event_signal ( uint8_t  is_broadcast,
uint8_t  and_next 
) [static]

Kernel function to signal waiting processes.

Handles signals and broadcasts, with or without yielding. May cause current task to be suspended.

Definition at line 801 of file os.c.

static void enqueue ( queue_t queue_ptr,
task_descriptor_t task_to_add 
) [static]

Add a task the head of the queue.

Parameters:
queue_ptr the queue to insert in
task_to_add the task descriptor to add

Definition at line 885 of file os.c.

static task_descriptor_t* dequeue ( queue_t queue_ptr  )  [static]

Pops head of queue and returns it.

Parameters:
queue_ptr the queue to pop
Returns:
the popped task descriptor

Definition at line 910 of file os.c.

static void kernel_update_ticker ( void   )  [static]

Update the current time.

Perhaps move to the next time slot of the PPP.

Definition at line 929 of file os.c.

static void check_PPP_names ( void   )  [static]

Validate the PPP array.

Definition at line 971 of file os.c.

void OS_Init ( void   ) 

Setup the RTOS and create main() as the first SYSTEM level task.

Point of entry from the C runtime crt0.S.

Definition at line 1015 of file os.c.

static void _delay_25ms ( void   )  [static]

Delay function adapted from <util/delay.h>.

Definition at line 1085 of file os.c.

void OS_Abort ( void   ) 

Abort the execution of this RTOS due to an unrecoverable erorr.

Abort the execution of this RTOS due to an unrecoverable erorr.

See also:
GLOBAL ASSUMPTIONS.

Definition at line 1096 of file os.c.

int Task_Create ( void(*)(void)  f,
int  arg,
unsigned int  level,
unsigned int  name 
)

Parameters:
f a parameterless function to be created as a process instance
arg an integer argument to be assigned to this process instanace
level assigned scheduling level: SYSTEM, PERIODIC or RR
name assigned PERIODIC process name
Returns:
0 if not successful; otherwise non-zero.
See also:
Task_GetArg(), PPP[].
A new process is created to execute the parameterless function f with an initial parameter arg, which is retrieved by a call to Task_GetArg(). If a new process cannot be created, 0 is returned; otherwise, it returns non-zero. The created process will belong to its scheduling level. If the process is PERIODIC, then its name is a user-specified name to be used in the PPP[] array. Otherwise, name is ignored.
See also:
SCHEDULING POLICY

Definition at line 1177 of file os.c.

void Task_Next ( void   ) 

The calling task gives up its share of the processor voluntarily.

Voluntarily relinquish the processor.

Definition at line 1203 of file os.c.

void Task_Terminate ( void   ) 

The calling task terminates itself.

Terminate the calling process

When a process returns, i.e., it executes its last instruction in the associated function/code, it is automatically terminated.

Definition at line 1220 of file os.c.

int Task_GetArg ( void   ) 

Retrieve the assigned parameter.

Retrieve the assigned parameter.

See also:
Task_Create().

Definition at line 1236 of file os.c.

EVENT* Event_Init ( void   ) 

Initialize a new, non-NULL Event descriptor.

Returns:
a non-NULL Event descriptor if successful; NULL otherwise.

Definition at line 1256 of file os.c.

void Event_Wait ( EVENT e  ) 

Wait for the next occurrence of a signal on e. The calling process always blocks.

Parameters:
e an Event descriptor

Definition at line 1280 of file os.c.

void Event_Signal ( EVENT e  ) 

Resume a single waiting task on e. It is a no-op if there is no waiting process.

Parameters:
e an Event descriptor
See also:
Event_Wait()

Definition at line 1301 of file os.c.

void Event_Broadcast ( EVENT e  ) 

Resume ALL waiting tasks on e. It is a no-op if there is no waiting process.

Parameters:
e an Event descriptor
See also:
Event_Wait()

Definition at line 1322 of file os.c.

void Signal_And_Next ( EVENT e  ) 

Resume a waiting task on e and at the same time relinquish the processor.

Parameters:
e an Event descriptor
This is equivalent to "Event_Signal( e ); Task_Next()" in concept. The fundamental difference is that these two operations are performed as an indivisible unit. So conceptually, the calling task resumes another waiting task and gives up its share of the processor simultaneously.
See also:
Event_Signal(), Task_Next()

Definition at line 1348 of file os.c.

void Broadcast_And_Next ( EVENT e  ) 

Resume ALL waiting tasks on e and at the same time relinquish the processor.

Parameters:
e an Event descriptor
This is equivalent to "Event_Broadcast( e ); Task_Next()" in concept.
See also:
Event_Broadcast(), Task_Next()

Definition at line 1371 of file os.c.


Variable Documentation

const unsigned char PPP[]

PPP and PT defined in user application.

const unsigned int PT

PPP and PT defined in user application.

task_descriptor_t* cur_task = NULL [static]

The task descriptor of the currently RUNNING task.

Definition at line 32 of file os.c.

volatile uint16_t kernel_sp [static]

Since this is a "full-served" model, the kernel is executing using its own stack.

Definition at line 35 of file os.c.

task_descriptor_t task_desc[MAXPROCESS+1] [static]

This table contains all task descriptors, regardless of state, plus idler.

Definition at line 38 of file os.c.

task_descriptor_t* idle_task = &task_desc[MAXPROCESS] [static]

The special "idle task" at the end of the descriptors array.

Definition at line 41 of file os.c.

volatile kernel_request_t kernel_request [static]

The current kernel request.

Definition at line 44 of file os.c.

volatile create_args_t kernel_request_create_args [static]

Arguments for Task_Create() request.

Definition at line 47 of file os.c.

volatile int kernel_request_retval [static]

Return value for Task_Create() request.

Definition at line 50 of file os.c.

volatile EVENT* kernel_request_event_ptr [static]

Argument and return value for Event class of requests.

Definition at line 53 of file os.c.

queue_t dead_pool_queue [static]

Number of tasks created so far

Definition at line 56 of file os.c.

uint8_t num_events_created = 0 [static]

Number of events created so far

Definition at line 59 of file os.c.

queue_t rr_queue [static]

The ready queue for RR tasks. Their scheduling is round-robin.

Definition at line 62 of file os.c.

queue_t system_queue [static]

The ready queue for SYSTEM tasks. Their scheduling is first come, first served.

Definition at line 65 of file os.c.

queue_t event_queue[MAXEVENT] [static]

An array of queues for tasks waiting on events.

Definition at line 68 of file os.c.

uint8_t ticks_remaining = 0 [static]

time remaining in current slot

Definition at line 71 of file os.c.

uint8_t slot_task_finished = 0 [static]

Indicates if periodic task in this slot has already run this time

Definition at line 74 of file os.c.

int slot_name_index = 0 [static]

Index of name of task in current slot in PPP array. An even number from 0 to 2*(PT-1).

Definition at line 77 of file os.c.

task_descriptor_t* name_to_task_ptr[MAXNAME+1] [static]

The task descriptor for index "name of task"

Definition at line 80 of file os.c.

uint8_t name_in_PPP[MAXNAME+1] [static]

The names that appear in PPP

Definition at line 83 of file os.c.

uint8_t volatile error_msg = ERR_RUN_1_USER_CALLED_OS_ABORT [static]

Error message used in OS_Abort()

Definition at line 86 of file os.c.


Generated on Tue Oct 23 21:49:51 2007 for RTOS by  doxygen 1.5.1