Académique Documents
Professionnel Documents
Culture Documents
User Guide
M.Verlinden
Version 1.0
Real time kernel for the AVR
This tiny AVR kernel with documentation is free of charge. Copyright (C)
2001 by Mark Verlinden. All rights reserved.
1 Introduction.............................................................................................................4
1.1 Usage...............................................................................................................4
2 Features...................................................................................................................5
2.1 Supported ........................................................................................................5
2.2 To do’s ............................................................................................................5
2.3 Additional features...........................................................................................5
2.4 Known issues...................................................................................................5
3 Kernel overview ......................................................................................................6
4 Breaking complexity into easier understandable tasks..............................................6
5 System level definitions and declarations.................................................................7
6 Scheduling...............................................................................................................8
6.1 Re-entrant and thread safe..............................................................................10
7 Context switching..................................................................................................12
8 Memory management ............................................................................................14
9 Semaphore.............................................................................................................15
10 Queues...............................................................................................................15
11 Timers ...............................................................................................................16
12 Messages ...........................................................................................................16
13 Pipe lines...........................................................................................................16
14 List ....................................................................................................................17
1 Introduction
This kernel is a powerful, C-written real-time kernel. Atak
is short for a (or another) tiny AVR kernel used in the
rest of this document. This kernel is highly portable,
where just two issues remain to be changed in order to port
this kernel to another mcu, namely: The context switching
and interrupt handling.
1.1 Usage
There is not one particular reason I made this kernel.
I made it to increase my knowledge within my line of work
as embedded software engineer. Or maybe a bit of lets see I
if can do this kind of thing, plus making a good base
system for my robotic ambitions and last but not least, I
love everything that has to do with programming, especially
embedded!
2 Features
2.1 Supported
• AVR based kernel, but can be easily ported
• Pre-emptive scheduling
• Task management (Create, suspend and resume)
• Message queues
• Semaphores
• Static task block control
• Delays
• Re-entrant and thread safe kernel services
• Optimized for speed
2.2 To do’s
• Reduce RAM usage by specifying stack for each task
• Priority scheduling
• Dynamic stack/heap management
• Only supported by CodevisionAVR C Compiler 1.0.1.4b
• Tested only on 8515 in combination with the STK200
• Timers (Will be added in version 1.1)
• (Re)Scheduling from interrupts
• Port to AVR GCC
• Pipes
3 Kernel overview
Some important descriptions of kernel elements:
}TCB;
while(1)
{
// do something
}
}
6 Scheduling
Pre-emptive
• tasks are scheduled and the scheduler decides who runs
• less efficient in coding size, but more powerful
• more responsive to fast execution
• latency is much lower
• kernel functions should be re-entrant
Figure 2 Example,
Task A wants to change a, but Task B will do this first after sudden rescheduling.
7 Context switching
Each task has a Task Control Block assigned and holds all
information concerning task switching. The entry point is
used to pin point the task (by saving the program counter)
and its argument. The context holds the current program
counter in that context and the registers, which are saved
during a context switch. The data stack is used to
dynamically store local variables, passing functions
parameters and saving registers and SREG during interrupt
service routine. The stack is used for storing the
functions return address. The delay is used for setting a
delay time on a task.
8 Memory management
This is my solution:
BEGIN_CRITICAL_SECTION();
for(int counter=0;x < system_counter; x++){
back = current;
currentTask = current->nextTask;
if(current->task_Status == SLEEPING &&
current->nextTask == back){
current->nextTask = back->nextTask;
free deleted task workspace!
}
}
END_CRITICAL_SECTION();
Note: this will be added in version 1.1
9 Semaphore
The structure of a semaphore definition is:
10 Queues
The structure of a queue definition is:
With this queue pointers items can be stored and acts like
a FIFO buffer. This queue is protected by semaphores. When
a task uses a queue that is already being used it is
suspended by the semaphore. This also occurs when the queue
is full and a task wants to add an item.
Note: Should be defined globally, this way every tasks can use it.
11 Timers
Timers are provided by the kernel, which holds a list of
created timers. These timers can be started and stopped or
trigger a task to wake up from the sleep status.
12 Messages
The structure of a message definition is:
With the message tasks can sent data to each other. Usually
the message are used with a queue, making it a message
queue. A task can read from this queue and get the
appropriate messages meant for the task.
13 Pipe lines
The structure of a pipe line definition is:
Note: Should be defined globally, this way every tasks can use it.
Not supported in v1.0
14 List
typedef struct TLIST_ITEM{
struct TLIST_ITEM *next;
struct TLIST_ITEM *previous;
void* owner;
}LIST_ITEM;
headRunningList = headRunningList->next;
ownerTCB = headRunningList->owner;
currentRunningTask = (TCB*) ownerTCB;
The kernel has several lists for the TCB’s, called the
sleeping, running, delayed and destroyed lists. Suspending
a task will remove the task in question from the running
list and then added to the sleeping list.
Resuming a task will remove the task from the sleeping list
and then added to the running list at the end. This way the
task switching always is done on the running list. There is
no need to search for tasks that could be run. Another
solution would be one list where the just the status is
checked while context switching. But this way the context
switch time could differ and we don’t want that. This is a
design consideration for speed and response time of the
kernel. The implementation though isn’t that big and also
uses link lists with pointers, which is very efficient for
speed consideration. These lists also simplify the resume
and suspend services. It is a neat solution and not
especially bigger in code than other solutions.
Usage:
Sleeping list
Running list
Destroyed list
Delayed list
Timer list