Professional Documents
Culture Documents
Abstract
In this paper we present a solution to implement a real time operating system kernel for
AVR ATMEGA family. We present our solution and provide source code which can be
tested. This kernel can be used in applications which require more responses in same time
to external stimulus.
Hardware
21 stack
22
Functions adress
23
48 SREG
49 R31
.
. Registers
.
79 R1
80 R0
89 temp_g
.
.
. Software
93 kg stack
94 jg
95 ig
96
Pointer Y
97
98
Pointer SP
99
Each task has its own hardware stack. Usually in hardware stack are saved current
program memory address when is call a function. When return from function this address
gives the next instruction. We use a hardware stack for each task so we can call function
inside the task loop.
Registers are used inside the task for local variable, function parameters and return
values storing. Registers are also used as local operators. If we use a C compiler may be
isn’t obvious their rules but if we use a disassembly in debug mode will be surprise by
the number of their appearance behind the C instructions.
The explication is very simple: a C instruction is decompose by the compiler in real
AVR instruction, and many AVR instruction use registers as operands. In addition, some
registers, called pointers, are used to save the local context when a function is called.
For example, when describe a function, in this case called “temp”, we write in C:
void temp()
{
int temp_g;
//disable LCD
MA_WritePort_IO(MA_PORTG,0x00,0x04);//0xx
//
MA_WritePort_IO(MA_PORTA,0x00,0xFF);//data=0
MA_SetPortIn_IO(MA_PORTA,0xFF);//
//configure LCD command
MA_WritePort_IO(MA_PORTG,0x02,0x07);//010//read configuration
for(temp_g=0;temp_g<50;temp_g++);//temp
MA_WritePort_IO(MA_PORTG,0x04,0x04);//1xx
……………………………………………………………………..
In disassembly we see:
void temp()
{
temp:
00008C 939A ST -Y,R25
00008E 938A ST -Y,R24
MA_WritePort_IO(MA_PORTG,0x00,0x04);//0xx
000090 E024 LDI R18,0x04
000092 E010 LDI R17,0x00
000094 E100 LDI R16,0x10
000096 940E 022C CALL MA_WritePort_IO
MA_WritePort_IO(MA_PORTA,0x00,0xFF);//data=0
00009A EF2F LDI R18,0xFF
00009C E010 LDI R17,0x00
00009E E00A LDI R16,0x0A
0000A0 940E 022C CALL MA_WritePort_IO
MA_SetPortIn_IO(MA_PORTA,0xFF);//
0000A4 EF1F LDI R17,0xFF
0000A6 E00A LDI R16,0x0A
0000A8 940E 01A1 CALL MA_SetPortIn_IO
MA_WritePort_IO(MA_PORTG,0x02,0x07);//010//
Software stack contain global variables used in a task. Global variables have address
reserved in RAM memory. But if we wish to use a global variable in two different tasks
we will encounter same problems like local variables: one task modify variable value for
another. To avoid this we use the following schema:
Global variables
current task
(ig, jg, mg…)
Step1 Step n
Step 2
To illustrate the portability of MYOS we integrate the kernel in more application with
different ATMEGA MCU.
In Project\Example\IAR is source code for application with MYOS for ATMEGA128
compiled with IAR – application used in messenger design.
We build two different applications using MYOS for ATMega128 and for ATMega32
and put running together and communicate using RS232. This system is simulated using
Proteus (LabCenter) (see window capture bellow while running):