Вы находитесь на странице: 1из 71

ELT048 – Embedded Operational

Systems
Rodrigo Maximiano Antunes de
Almeida
rmaalmeida@gmail.com
@rmaalmeida
Universidade Federal de Itajubá
Creative Commons License

The work ”Embedded System Design: From Electronics to Microkernel


Development” of Rodrigo Maximiano Antunes de Almeida was licensed
with Creative Commons 3.0 – Attribution – Non Commercial – Share Alike
license.

Additional permission can be given by the author through direct contact


using the e-mail: rmaalmeida@gmail.com
Intro
Initial comments
• Classes
 Wed 7:00 to 9:30
 LEC II
 1st Test 13/09
 2nd Test 08/11
 Sub. Test 6/12 (or earlier)

U
UNN II F
FEE II
Embedded OS
• Really?

U
UNN II F
FEE II
Moore Law

U
UNN II F
FEE II
LPC800

U
UNN II F
FEE II
LPC800

U
UNN II F
FEE II
LPC800

U
UNN II F
FEE II
KL02

U
UNN II F
FEE II
KL02
• MKL02Z32CAF4RTR-
ND
 32K flash
 4k ram
 US$1,76

U
UNN II F
FEE II
Schedule
• 01) Intro / Pointers / • 08) Drivers Controller
Buffers • 09) Callbacks
• 02) void and function • 10) Preemptive Kernel,
pointers scheduler
• 03) Tasks • 11) Real time
• 04) Cooperative kernel • 12) Mutex &
• 05) Timing Semaphores
requirements • 13) Message Prsing
• 06) Drivers

U
UNN II F
FEE II
How to make an operating system
void main (void){
//variable declaration
kernel_project(1);
//initialization
concepts(2);
//hard-work
microkernel(3);
device_driver_controller(4);
}

U
UNN II F
FEE II
void kernel_project (float i){
what_is_a_kernel(1.1);
alternatives(1.2);
monolithic_vs_microkernel(1.3);
kernel_design_decisions(1.4);
this_course_decisions(1.5);
}
kernel_project(1);

what_is_a_kernel(1.1);

U
UNN II F
FEE II
kernel_project(1);

U
UNN II F
FEE II
kernel_project(1);

• Kernel tasks:
1. Manage and coordinate the processes execution using
“some criteria”
2. Manage the free memory and coordinate the processes
access to it
3. Intermediate the communication between the hardware
drivers and the processes

U
UNN II F
FEE II
kernel_project(1);
Interface Internal
System logic
actions routines

C/C++
GUI Extra libs
libraries

Kernel

Context switch
File system Drivers

Memory CPU I/O

U
UNN II F
FEE II
kernel_project(1);

Develop my own kernel?

Why?
kernel_project(1);

• Improve home design


• Reuse code more efficiently
• Full control over the source
• Specific tweeks to the kernel
 Faster context switch routine
 More control over driver issues (interrupts)

U
UNN II F
FEE II
kernel_project(1);

Develop my own kernel?

Why not?
kernel_project(1);

• Kernel overhead (both in time and memory)


• Free and paid alternatives
• Time intensive project
• Continuous development

U
UNN II F
FEE II
Kernel Project
• Source code size (millions of lines)
 FreeBSD – 8.8
 IOS – 80
 Linux (2.6.0) – 5.2
 Linux (3.6) – 15.9
 Debian 7.0 – 419
 Mac OS X 10.4 – 86
 OpenSolaris – 9.7
 Windows NT – 11
 Windows XP – 45
 Windows Vista – 66
U
UNN II F
FEE II
kernel_project(1);

• Alternatives
 Windows Embedded Compact®
 VxWorks®
 X RTOS®
 uClinux
 FreeRTOS
 BRTOS

U
UNN II F
FEE II
kernel_project(1);


Monolithic kernel versus microkernel


Linus Torvalds and Andrew Tanenbaum

U
UNN II F
FEE II
kernel_project(1);

• Kernel design decisions


 I/O devices management
 Process management
 System safety

U
UNN II F
FEE II
kernel_project(1);

• Our decisions:
 Microkernel
 Non-preemptive
 Cooperative
 No memory management
 Process scheduled based on timer
 Isolate drivers using a controller

U
UNN II F
FEE II
Practical Activities
• Boards
 NEO201 – Pic18f4550
 LPCxpresso base board – LPC1100
• Development environment
 MPLABX
 Eclipse

U
UNN II F
FEE II
void concepts (float i){
pointers(2.1);
structs(2.2);
circular_buffers(2.3);
temporal_conditions(2.4);
void_pointers(2.5);
}
concepts(2);

pointers(2.1);

U
UNN II F
FEE II
Pointers

https://xkcd.com/
U
UNN II F
FEE II
Pointers
• They are variables that store the address (location)
of memory.
 In these memory addresses it is possible to enter values.
• The type of the value placed in the memory pointed
by the address is defined in the declaration of the
pointer.
 The type that tells the compiler how much memory is
needed to store the values.
• A pointer variable points to a variable of a certain
type (char, int, float, double, ...).

U
UNN II F
FEE II
Pointers
• It is necessary in the • Sintax:
declaration of a pointer, • type *variableName;
to specify for which
type of variable it will
point.
• Pointers are declared
with a * before the
variable name.

U
UNN II F
FEE II
Pointers
• aux, temp, and pont are int *aux;
variables that store float *temp;
memory addresses and char *pont;

not variable of the types


int, float, or char.
• * is used when you
want to access the value
that is in the memory
location and not the
memory address.

U
UNN II F
FEE II
Pointers

U
UNN II F
FEE II
Pointers
• Operator &: • Operator *:
 Always gets the address  The * operator does the
of a variable opposite of the &
 Since pointers are also operator.
variables, they also  Given a pointer, the *
occupy memory. operator accesses the
 You can get the pointer content pointed to by it.
address and have
pointers to pointers
(multiples *).

U
UNN II F
FEE II
Ponteiros
• Operator &: • Operator *:
 “get the address of”  “dereference”
 “saved in”  “through”
 “pointed by”

U
UNN II F
FEE II
Pointers
#include <stdio.h>
int main(int argc, char *argv[]){
int x=10;
int *p1=&x; //ponteiro para um inteiro
printf("x = %d\n\n", x);
*p1=20; //ou p1[0]=20;
printf("p1 = %u\n", p1);
printf("x = %d\n", x);
printf("*p1 = %d\n", *p1);
printf("p1[0] = %d\n\n", p1[0]);
return 0;
} //end main
U
UNN II F
FEE II
Parameter passing
• C-language functions can receive multiple
parameters and can return one.
• The method of sending the parameters can be done
through the stack or through registers.
• The stack allows several parameters to be passed at
once
• The registers have a lower overhead, therefore
being faster and with smaller code size

U
UNN II F
FEE II
Parameter passing
• When sending, the parameters are copied to a
temporary variable, so that changes in its value are
lost after the end of the function.
• The initial variable is not changed
• To avoid this situation it is common to pass the
parameter by reference.
• When passing by reference, the address of the
variable is passed instead the value. Therefore the
function can change the value of the original
variable.
U
UNN II F
FEE II
Parameter passing
void incrementa(int a){
a += 1;
}

int main(int argc, char *argv[]){


int x = 10;

incrementa(x);

return 0;
}

U
UNN II F
FEE II
Rotina Principal Função

void main(void){
int x = 10;

incrementa(x);
void incrementa(int a){
a+=1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10

inc(x); void inc(int a){


a += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10
a=10
inc(x); void inc(int a){
a += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10
a=11
inc(x); void inc(int a){
a += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10
descartado
inc(x); void inc(int a){
a += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
void incrementaptr(int* a){
(*a) += 1;
}
int main(int argc, char *argv[]){
int x = 10;
incrementaptr(&x);
return 0;
} //end main

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10

incptr(&x); void incptr


(int* a){
(*a) += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10
temp=&x
incptr(&x); void incptr
(int* a){
(*a) += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=10
a=&x
incptr(&x); void incptr
(int* a){
(*a) += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=11
a=&x
incptr(&x); void incptr
(int* a){
*
(*a) += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Rotina Principal Função Variáveis

void main(void)
{
int x = 10; x=11
descartado
incptr(&x); void incptr
(int* a){
(*a) += 1;
return;
}
return 0;
}

U
UNN II F
FEE II
Parameter passing
• Passing parameters by reference allows a function
to "return" more than one parameter at a time.
• This is done by passing a variable via reference so
that the function can write to it.

U
UNN II F
FEE II
//a e b são dados de entrada
//d e r são dados de saída

void div(int a, int b, int* d, int* r){


(*d) = a / b;
(*r) = a % b;
return;
}

void main(void){
int d, r;
div(10, 3, &d, &r);
//d = 3 e r = 1;
return;
}

U
UNN II F
FEE II
concepts(2);

structs(2.2);

U
UNN II F
FEE II
concepts(2);

• Structs are composed variables.


• Group lots of information as if they were one single
variable.
• A vector that each position stores a different type

// struct declaration
typedef struct{
unsigned short int age;
char name[51];
float weight;
}people;

U
UNN II F
FEE II
concepts(2);

void main(void){
struct people myself = {26, "Rodrigo", 70.5};

myself.age = 27;

//using each variable from the struct


printf("Age: %d\n", myself.age);
printf("Name: %s\n", myself.name);
printf("Weight: %f\n", myself.weight);

return 0;
}

U
UNN II F
FEE II
concepts(2);
// struct declaration
typedef struct{
unsigned short int *age;
char *name[51];
float *weight;
}people;

void main(void){
struct people myself = {26, "Rodrigo", 70.5};
//using each variable from the struct
printf("Age: %d\n", myself->age);
printf("Name: %s\n", myself->name);
printf("Weight: %f\n", myself->weight);
return 0;
}
Structs
typedef struct{
• The access of the char nome[20];
internal members of int idade;
}t_aluno;
structures, pointed by
pointers, can actually be t_aluno *a1;
done through the (*a1).nome = "Joao";
operator -> (*a1).idade = 18;

a1->nome = "Joao";
a1->idade = 18;

U
UNN II F
FEE II
concepts(2);

circular_buffers(2.3);

U
UNN II F
FEE II
concepts(2);

• Circular Buffers
 “Endless” memory spaces
 Use FIFO aproach
 Store temporary data
 Can implemented using vectors or linked-lists

U
UNN II F
FEE II
concepts(2);

• Vector implementation
 Uses less space
 Need special caution when cycling
 Problem to differentiate full from empty

U
UNN II F
FEE II
[0]
[4] [1]

[3] [2]
Buffer Vazio

ini=0

? ? ? ? ?
[0] [1] [2] [3] [4]

fim=0
Adicionando 2 elementos

ini=0

'A' 'B' ? ? ?
[0] [1] [2] [3] [4]

fim=2
Removendo 1 elemento

ini=1

'A' 'B' ? ? ?
[0] [1] [2] [3] [4]

fim=2
Adicionando 2 elementos

ini=1

'A' 'B' 'C' 'D' ?


[0] [1] [2] [3] [4]

fim=4
Adicionando 1 elemento

ini=1

'A' 'B' 'C' 'D' 'E'


[0] [1] [2] [3] [4]

fim=0
concepts(2);

#define CB_SIZE 10

int circular_buffer[CB_SIZE];

int index=0;

for(;;){
//do anything with the buffer
circular_buffer[index] = index;
//increment the index
index = (index+1)%CB_SIZE;
}

U
UNN II F
FEE II
concepts(2);

#define CB_SIZE 10
int circular_buffer[CB_SIZE];
int start=0, end=0;

char AddBuff(int newData)


{
//check if there is space to add a number
if ( ((end+1)%CB_SIZE) != start)
{
circular_buffer[end] = newData;
end = (end+1)%CB_SIZE;
return SUCCESS;
}
return FAIL;
}
Exercise
• Implement a circular buffer
 Use a 10-position vector
• Each element of the vector is a structure with two
variables
 char * ProcessName
 int Time
• Create one function to add new elements and one to
remove the oldest elements.

U
UNN II F
FEE II

Вам также может понравиться