0% found this document useful (0 votes)
36 views

ELT048 - Embedded Operational Systems: Rodrigo Maximiano Antunes de Almeida @rmaalmeida Universidade Federal de Itajubá

The document discusses pointers and parameter passing in C programming. Pointers store the address of a memory location and can be used to access values in memory. Parameters can be passed by value, where a copy is made, or by reference, where the address is passed to allow modification of the original variable. Passing by reference avoids changes being lost after a function call.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
36 views

ELT048 - Embedded Operational Systems: Rodrigo Maximiano Antunes de Almeida @rmaalmeida Universidade Federal de Itajubá

The document discusses pointers and parameter passing in C programming. Pointers store the address of a memory location and can be used to access values in memory. Parameters can be passed by value, where a copy is made, or by reference, where the address is passed to allow modification of the original variable. Passing by reference avoids changes being lost after a function call.
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 56

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
Bureaucracy
Initial comments
• Classes
 Mon 7:55 to 9:45 – B4.2.06
 Mon 13:30(P1) 15:45 (P2) - LEC II
 1st Test 20/04
 2nd Test 08/06
 Sub. Test 22/06

U
UNN II F
FEE II
Schedule
1. Intro / Pointers / Buffers 1. FreeRTOS
2. Function pointers / tasks 2. Heap Memory
/ kernel 3. Task Management
3. Scheduler / Cooperative 4. Queues
kernel / priorities
(RMS) 5. Resource Management
4. Timing requirements 6. *Soft Timers & Int
5. Drivers 7. 2nd Test
6. Drivers Controller 8. Substitutive test
7. 1st Test
8. Preemptive Kernel,
U
UNscheduler
N II F
FEE II
Labs Schedules
• L1 – Circular buffer & function pointers
• L2 – Kernel + Priorities
• L3 – Kernel + Timing
• L4 – Drivers
• L5 – FreeRTOS
• L6 – Tasks Management
• L7 – Queues
• L8 – Mutexes & Semaphores

U
UNN II F
FEE II
Fast Intro

AKA why embedded OS?


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
The OS
What is a kernel?
What is a kernel?

U
UNN II F
FEE II
What is a kernel?
• Kernel tasks:
 Manage and coordinate the processes execution using
“some criteria”
 Manage the free memory and coordinate the processes
access to it
 Intermediate the communication between the hardware
drivers and the processes

U
UNN II F
FEE II
What is a kernel?
Interface Internal
System logic
actions routines

C/C++
GUI Extra libs
libraries

Kernel

Context switch
File system Drivers

Memory CPU I/O


Develop my own
kernel?
Why?
Develop my own kernel?
• 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
Develop my own
kernel?
Why not?
Develop my own kernel?
• 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
Develop my own kernel?
• Alternatives
 Windows Embedded Compact®
 VxWorks®
 X RTOS®
 uClinux
 FreeRTOS
 BRTOS

U
UNN II F
FEE II
Programming
concepts
Pointers

https://xkcd.com/
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
Pointers
• 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
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
temp=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
Structs
Structs
• 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
Structs

void main(void){
people someone = {26, "Name", 70.5};

someone.age = 27;

//using each variable from the struct


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

return 0;
}
Structs
• The access of the
typedef struct{
internal members of char nome[20];
structures, pointed by int idade;
pointers, can actually be int *p_dado;
}t_aluno;
done through the
operator -> t_aluno *a1;

(*a1).idade = 18;
a1->idade = 18;

U
UNN II F
FEE II
Structs
void main(void){
int dado = 10;
t_aluno aluno = {"Nome",17,&dado};
t_aluno *a1;
a1 = &aluno;

(*a1).idade = 18;
printf("Age: %d\n", (*a1).idade);
a1->idade = 19;
printf("Age: %d\n", a1->idade);

*(aluno.p_dado) = 11;
printf("dado: %d\n", dado);
//aluno->p_dado = 12; //erro
//printf("dado: %d\n", dado);
*((*a1).p_dado) = 12;
printf("dado: %d\n", dado);
*(a1->p_dado) = 13;
printf("dado: %d\n", dado);
return 0;
}

You might also like