SlideShare a Scribd company logo
Embedded System Design:
From Electronics
to Microkernel Development
Rodrigo Maximiano Antunes de Almeida
rmaalmeida@gmail.com
@rmaalmeida
Universidade Federal de Itajubá
Portal embarcados
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
Creative Commons License
Workshop schedule
• Hardware
Electronics introduction
Board development
• Firmware
Embedded programming
Peripheral access
• Kernel
Timing requirements
Device drivers
Hardware
Hardware concepts
• Electronics introduction
Schematics
Datasheets
Protoboard/breadboard
LED
Potentiometer
LCD
Microcontroller
• System design
Basic steps
Electronics introduction
• http://xkcd.com/730/
Schematics
• Way to represent the
components and its
connections
• Each component has
its own symbol
• Crossing wires only
are connected if
joined with a dot
Datasheets
• The main source of information concerning
electronics
• Presents
Electrical characteristics
Simplified schematics
Use example
Opcodes/API
Protoboard/Breadboard
LED
LED
Potentiometer
• Linear/Log
• Used as voltage
divisor
• Need an analog input
• Filter
Potentiometer
LCD Display
LCD Display
• Complete interface solution
Screen + Video card + Protocol + Data cable
• “Standard” HD44780
4/8 bits communication
3 control bits
DEF CON 23 - Rodringo Almeida - embedded system design from electronics
LCD Display
• Backlight
• Data connection
• Current consumption
• Power on time/routine
Microcontroller
• System-on-a-chip
Processor
Memory
Input/Output
peripherals
Communication
Safety components
Microcontroller
• Xtal configuration
• Reset pin
• DC needs
• Many peripherals on
the same pin
System desing
System design
• Steps on a generic electronic system design
Define the objective(s)
Choose the main components needed to achieve the
objective
Get the use example and recommendations from
component datasheet
Build the schematics
Simulation of HW elements
Board layout
System design
• Free CAD tools for electronics
Fritzing (fritzing.org)
Kicad
LTSpice
https://www.circuitlab.com/
System design
• Ultra fast workshop
Power source development
• Online circuit simulation + fritzing for layout
From concept to ready to manufacture in 10 min
System design
• Minimum circuit
components
Microcontroller
Voltage source
Input/Output as
needed
Clock source
Programmer
connection
Firmware
development
Firmware development
• Programmer
• IDE
• Basic concepts
CPU Architecture
HW configuration
Memory access
• First program (Led blinking)
• Second program (ADC read)
• Third program (LCD access)
Firmware tools
• Programmer
PICkit3
Can use ICSP
Can program a lot of
Microchip products
Also a debugger
Jtag equivalent
Firmware tools
Firmware tools
• IDE
MPLABX
• Based on Netbeans
• Compiler
SDCC
• Based on GCC
GPUtils
Embedded
programming
concepts
Becouse while(a==a); is not an infinite loop!
Embedded programming concepts
Embedded programming concepts
• Memory segmentation
Embedded programming concepts
• HW configuration
Some options must be set before the program start
This can only be accomplished by special instructions
Compiler datasheet
Embedded programming concepts
Embedded programming concepts
#pragma config MCLRE=ON
// Master Clear desabilitado
#pragma config FOSC=INTOSC_XT
// Oscilador c/ cristal externo HS
#pragma config WDT=OFF
// Watchdog controlado por
software
#pragma config LVP=OFF
// Sem programação em baixa tensão
#pragma config DEBUG=OFF
// Desabilita debug
#pragma config XINST=OFF
Embedded programming concepts
Embedded programming concepts
• Build a pointer to a specific memory address:
void main (void){
char *ptr;
//pointing to the port D
ptr = 0xF83;
//changing all outputs to high
*ptr = 0xFF;
}
Embedded programming concepts
• Building a header with all definitions
__near = sfr region
volatile = can change without program acknowledge
#define PORTD (*(volatile __near unsigned char*)0xF83)
#define TRISC (*(volatile __near unsigned char*)0xF94)
//this is not an infinite loop!
while( PORTD == PORTD);
Embedded programming concepts
• Bitwise operations
char mask;
mask = 1 << 2;
arg = arg | mask;
//one line
arg = arg | (1<<bit);
//using define
#define BitSet(arg,bit) ((arg) |= (1<<bit))
#define BitClr(arg,bit) ((arg) &= ~(1<<bit))
#define BitFlp(arg,bit) ((arg) ^= (1<<bit))
#define BitTst(arg,bit) ((arg) & (1<<bit))
Hands on
First lab
• Assemble the first circuit
• Open MPLABX IDE
configure SDCC and PICkit
• Create a project to
Blink a led
First lab
First Lab
#define PORTD (*(volatile __near unsigned char*)0xF83)
#define TRISD (*(volatile __near unsigned char*)0xF95)
void main(void) {
TRISD = 0x00;
for(;;){
PORTD ^= 0xFF;
//delay();
}
}
Second lab
• Using ADC potentiometer as input
Peripherals setup
• Analog to digital
converter
ADC setup
#define TRISA (*(volatile __near unsigned char*)0xF92)
#define ADCON2 (*(volatile __near unsigned char*)0xFC0)
#define ADCON1 (*(volatile __near unsigned char*)0xFC1)
#define ADCON0 (*(volatile __near unsigned char*)0xFC2)
#define ADRESL (*(volatile __near unsigned char*)0xFC3)
#define ADRESH (*(volatile __near unsigned char*)0xFC4)
void adInit(void) {
BitSet(TRISA, 0); //pin setup
ADCON0 = 0b00000001; //channel select
ADCON1 = 0b00001110; //ref = source
ADCON2 = 0b10101010; //t_conv = 12 TAD
}
ADC setup
unsigned int adRead(void){
unsigned int ADvalue;
BitSet(ADCON0,1); //start conversion
while(BitTst(ADCON0,1)); //wait
ADvalue = ADRESH; //read result
ADvalue <<= 8;
ADvalue += ADRESL;
return ADvalor;
}
Second lab
void main(void) {
unsigned int i;
unsigned int ad;
TRISD = 0x00;
adInit();
for (;;) {
ad = adRead();
PORTD = 0xff;
for (i = 0; i < ad; i++);
PORTD = 0x00;
for (i = ad; i < 1024; i++);
}
}
Second Lab
Third example
• Using a LCD as information output peripheral
LCD communication
• The data is always an 8 bit information
It may be split in two 4 bit data transfer
• The data may represent a character or a
command
They are distinguished by RS pin
• The data must be stable for "some time"
In this period the EN pin must be set
LCD communication
#define RS 6
#define EN 7
void delayMicroseconds(int ms) {
int i;
for (; ms > 0; ms--) {
for (i = 0; i < 30; i++);
}
}
void pulseEnablePin() {
BitClr(PORTC, EN);
delayMicroseconds(1);
// send a pulse to enable
BitSet(PORTC, EN);
delayMicroseconds(1);
BitClr(PORTC, EN);
}
LCD communication
void pushNibble(int value, int rs) {
PORTD = value;
if (rs) {
BitSet(PORTC, RS);
} else {
BitClr(PORTC, RS);
}
pulseEnablePin();
}
void pushByte(int value, int rs) {
int val_lower = value & 0x0F;
int val_upper = value >> 4;
pushNibble(val_upper, rs);
pushNibble(val_lower, rs);
}
LCD communication
void lcdCommand(int value) {
pushByte(value, 0);
delayMicroseconds(40);
}
void lcdChar(int value) {
pushByte(value, 1);
delayMicroseconds(2);
}
void lcdInit() {
BitClr(TRISC, EN);
BitClr(TRISC, RS);
TRISD = 0x0f;
delayMicroseconds(50);
commandWriteNibble(0x03);
delayMicroseconds(5);
commandWriteNibble(0x03);
delayMicroseconds(100);
commandWriteNibble(0x03);
delayMicroseconds(5);
commandWriteNibble(0x02);
delayMicroseconds(10);
//display config
lcdCommand(0x28); //4bits, 2 linhas, 5x8
lcdCommand(0x06); //incremental mode
lcdCommand(0x0c); //display on, cursor and blink off
lcdCommand(0x03); //clean internal variables
lcdCommand(0x80); //initial position
lcdCommand(0x01); //clear display
delayMicroseconds(2);
}
LCD communication
LCD communication
• The LCD can hold up
to 8 custom characters
• Each character is a 5*8
matrix
• Translating: 40*64 b/w
drawing area
Source: http://www.8051projects.net/lcd-interfacing/lcd-custom-character.php
LCD communication
LCD communication
void lcdDefconLogo(void) {
int i;
unsigned char defcon[] = {
0x00, 0x01, 0x03, 0x03, 0x03, 0x03, 0x01, 0x04,
0x0e, 0x1f, 0x04, 0x04, 0x1f, 0x0e, 0x11, 0x1f,
0x00, 0x10, 0x18, 0x18, 0x18, 0x18, 0x10, 0x04,
0x0c, 0x03, 0x00, 0x00, 0x00, 0x03, 0x0c, 0x04,
0x00, 0x00, 0x1b, 0x04, 0x1b, 0x00, 0x00, 0x00,
0x06, 0x18, 0x00, 0x00, 0x00, 0x18, 0x06, 0x02
};
lcdCommand(0x40);
for (i = 0; i < 8 * 6; i++) {
lcdChar(defcon[i]);
}
}
Third Laboratory
• Read ADC value and
present in LCD
Third Laboratory
Time to break;
Back @ 14:00
//afternoon topics
void main (void){
//variable declaration
kernel_project(1);
//initialization
concepts(2);
//hard-work
microkernel(3);
device_driver_controller(4);
}
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);
kernel_project(1);
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
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)
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
kernel_project(1);
• Alternatives
Windows Embedded Compact®
VxWorks®
X RTOS®
uClinux
FreeRTOS
BRTOS
kernel_project(1);
Monolithic kernel versus microkernel
Linus Torvalds and Andrew Tanenbaum
kernel_project(1);
• Kernel design decisions
I/O devices management
Process management
System safety
kernel_project(1);
• Our decisions:
Microkernel
Non-preemptive
Cooperative
No memory management
Process scheduled based on timer
Isolate drivers using a controller
void concepts (float i){
function_pointers(2.1);
structs(2.2);
circular_buffers(2.3);
temporal_conditions(2.4);
void_pointers(2.5);
}
concepts(2);
function_pointers(2.1);
concepts(2);
• Necessity:
Make an image editor
that can choose the
right function to call
• 1st Implementation
Use a option
parameter as a switch
operator
concepts(2);
image Blur(image nImg){}
image Sharpen(image nImg){}
image imageEditorEngine(image nImg, int opt){
image temp;
switch(opt){
case 1:
temp = Sharpen(nImg);
break;
case 2:
temp = Blur(nImg);
break;
}
return temp;
}
concepts(2);
• Function pointers
Work almost as a normal pointer
Hold the address of a function start point instead the
address of a variable
The compiler need no known the function signature
to pass the correct parameters and the return value.
Awkard declaration (it is best to use a typedef)
concepts(2);
//defining the type pointerTest
//it is a pointer to function that:
// receives no parameter
// returns no parameter
typedef void (*pointerTest)(void);
//Function to be called
void nop (void){ __asm NOP __endasm }
//creating an pointerTest variable;
pointerTest foo;
foo = nop;
(*foo)(); //calling the function via pointer
concepts(2);
Re-code the image editor engine using
function pointers
concepts(2);
image Blur(image nImg){}
image Sharpen(image nImg){}
typedef image (*ptrFunc)(image nImg);
//image editor engine
image imageEditorEngine(ptrFunc function,
image nImg){
image temp;
temp = (*function)(nImg);
return temp;
}
concepts(2);
• Good
New function
additions do not alter
the engine
The engine only needs
to be tested once
Can change the
function
implementations
dynamically
• Bad
More complex code
(function pointers are
not so easy to work
with)
Not all compilers
support function
pointers
concepts(2);
structs(2.2);
concepts(2);
// struct declaration
typedef struct{
unsigned short int age;
char name[51];
float weight;
}people;
• Structs are composed variables.
• Group lots of information as if they were one
single variable.
• A vector that each position stores a different
type
void main(void){
struct people myself = {26, "Rodrigo", 70.5};
myself.age = 27;
//using each variable from the struct
printf("Age: %dn", myself.age);
printf("Name: %sn", myself.name);
printf("Weight: %fn", myself.weight);
return 0;
}
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: %dn", myself->age);
printf("Name: %sn", myself->name);
printf("Weight: %fn", myself->weight);
return 0;
}
concepts(2);
concepts(2);
circular_buffers(2.3);
concepts(2);
• Circular Buffers
“Endless” memory spaces
Use FIFO aproach
Store temporary data
Can implemented using vectors or linked-lists
concepts(2);
• Vector implementation
Uses less space
Need special caution when cycling
Problem to differentiate full from empty
#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;
}
concepts(2);
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;
}
concepts(2);
temporal_conditions(2.4);
concepts(2);
In the majority part of embedded
systems, we need to guarantee
that a function will be executed
in a certain frequency. Some
systems may even fail if these
deadlines are not met.
concepts(2);
• To implement temporal conditions:
1. There must be a tick event that occurs with a
precise frequency
2. The kernel must be informed of the execution
frequency needed for each process.
3. The sum of process duration must “fit” within the
processor available time.
concepts(2);
• 1st condition:
Needs an internal timer that can generate an
interrupt.
• 2nd condition:
Add the information for each process when creating
it
• 3rd condition:
Test, test and test.
If fail, change chip first, optimize only on last case
concepts(2);
• Scheduling processes:
Using a finite timer to schedule will result in overflow
Example: scheduling 2 processes for 10 and 50
seconds ahead.
concepts(2);
• And if two process are to be called in the same
time?
concepts(2);
• Question:
From the timeline above (only the timeline) is P2 late
or it was scheduled to happen 55(s) from now?
concepts(2);
• Solution:
Use a downtime counter for each process instead of
setting a trigger time.
• Problem:
Each counter must be decremented in the interrupt
subroutine.
Is it a problem for your system?
concepts(2);
void_pointers(2.5);
concepts(2);
• Void pointers
Abstraction that permits to the programmer to pass
parameters with different types to the same function.
The function which is receiving the parameter must
know how to deal with it
It can not be used without proper casting!
concepts(2);
char *name = "Paulo";
double weight = 87.5;
unsigned int children = 3;
void main (void){
//its not printf, yet.
print(0, &name);
print(1, &weight);
print(2, &children);
}
concepts(2);
void print(int option; void *parameter){
switch(option){
case 0:
printf("%s",(char*)parameter);
break;
case 1:
printf("%f",*((double*)parameter));
break;
case 2:
printf("%d",*((unsigned int*)parameter));
break;
}
}
void microkernel (float i){
init_kernel(3.0);
for(int i=1; i<4; i++;)
{
kernel_example(3+i/10);
}
running_the_kernel(3.4);
}
microkernel(3);
init_kernel(3.0);
microkernel(3);
• The examples will use a minimum of hardware or
platform specific commands.
• Some actions (specifically the timer) needs
hardware access.
microkernel(3);
//first implementation
kernel_example(3.1);
microkernel(3);
//pointer function declaration
typedef void(*ptrFunc)(void);
//process pool
static ptrFunc pool[4];
• In this first example we will build the main part
of our kernel.
• It should have a way to store which functions are
needed to be executed and in which order.
• This will be done by a static vector of pointers to
function
microkernel(3);
void tst1(void){
printf("Process 1n");
}
void tst2(void){
printf("Process 2n");
}
void tst3(void){
printf("Process 3n");
}
• Each process is a function with the same
signature of ptrFunc
microkernel(3);
//kernel internal variables
ptrFunc pool[4];
int end;
//kernel function's prototypes
void kernelInit(void);
void kernelAddProc(ptrFunc newFunc);
void kernelLoop(void);
• The kernel itself consists of three functions:
One to initialize all the internal variables
One to add a new process
One to execute the main kernel loop
microkernel(3);
//kernel function's implementation
void kernelInit(void){
end = 0;
}
void kernelAddProc(ptrFunc newFunc){
if (end <4){
pool[end] = newFunc;
end++;
}
}
microkernel(3);
//kernel function's implementation
void kernelLoop(void){
int i;
for(;;){
//cycle through the processes
for(i=0; i<end; i++){
(*pool[i])();
}
}
}
microkernel(3);
//main loop
void main(void){
kernelInit();
kernelAddProc(tst1);
kernelAddProc(tst2);
kernelAddProc(tst3);
kernelLoop();
}
microkernel(3);
Simple?
microkernel(3);
//second implementation
//circular buffer and struct added
kernel_example(3.2);
microkernel(3);
• The only struct field is the function pointer.
Other fields will be added latter.
• The circular buffer open a new possibility:
A process now can state if it wants to be rescheduled
or if it is a one-time run process
In order to implement this every process must return
a code.
This code also says if there was any error in the
process execution
microkernel(3);
//return code
#define SUCCESS 0
#define FAIL 1
#define REPEAT 2
//function pointer declaration
typedef char(*ptrFunc)(void);
//process struct
typedef struct {
ptrFunc function;
} process;
process pool[POOL_SIZE];
microkernel(3);
char kernelInit(void){
start = 0;
end = 0;
return SUCCESS;
}
char kernelAddProc(process newProc){
//checking for free space
if ( ((end+1)%POOL_SIZE) != start){
pool[end] = newProc;
end = (end+1)%POOL_SIZE;
return SUCCESS;
}
return FAIL;
}
microkernel(3);
void kernelLoop(void){
for(;;){
//Do we have any process to execute?
if (start != end){
//check if there is need to reschedule
if (pool[start]->Func() == REPEAT){
kernelAddProc(pool[start]);
}
//prepare to get the next process;
start = (start+1)%POOL_SIZE;
}
}
}
microkernel(3);
void tst1(void){
printf("Process 1n");
return REPEAT;
}
void tst2(void){
printf("Process 2n");
return SUCCESS;
}
void tst3(void){
printf("Process 3n");
return REPEAT;
}
• Presenting the new processes
microkernel(3);
void main(void){
//declaring the processes
process p1 = {tst1};
process p2 = {tst2};
process p3 = {tst3};
kernelInit();
//Test if the process were added
if (kernelAddProc(p1) == SUCCESS){
printf("1st process addedn");}
if (kernelAddProc(p2) == SUCCESS){
printf("2nd process addedn");}
if (kernelAddProc(p3) == SUCCESS){
printf("3rd process addedn");}
kernelLoop();
}
microkernel(3);
//third implementation
//time conditions added
kernel_example(3.3);
microkernel(3);
//process struct
typedef struct {
ptrFunc
function;
int period;
int start;
} process;
• The first modification is to add one counter to
each process
microkernel(3);
void isr(void) __interrupt 1{
unsigned char i;
i = ini;
while(i!=fim){
if((pool[i].start)>(MIN_INT)){
pool[i].start--;
}
i = (i+1)%SLOT_SIZE;
}
}
• We must create an function that will run on each
timer interrupt updating the counters
microkernel(3);
char AddProc(process newProc){
//checking for free space
if ( ((end+1)%SLOT_SIZE) != start){
pool[end] = newProc;
//increment start timer with period
pool[end].start += newProc.period;
end = (end+1)%SLOT_SIZE;
return SUCCESS;
}
return FAIL;
}
• The add process function will be the responsible
to initialize correctly the fields
microkernel(3);
if (start != end){
//Finding the process with the smallest start
j = (start+1)%SLOT_SIZE;
next = start;
while(j!=end){
if (pool[j].start < pool[next].start){
next = j;
}
j = (j+1)%SLOT_SIZE;
}
//exchanging positions in the pool
tempProc = pool[next];
pool[next] = pool[start];
pool[start] = tempProc;
while(pool[start].start>0){
}//great place to use low power mode
if ( (*(pool[ini].function))() == REPEAT ){
AddProc(&(vetProc[ini]));
}
ini = (ini+1)%SLOT_SIZE;
}
microkernel(3);
running_the_kernel(3.4);
“My board's programming” also works =)
void dd_controler (float i){
device_driver_pattern(5.1);
controller_engine(5.2);
isr_abstract_layer(5.3);
driver_callback(5.4);
}
device_driver_controller(4);
device_driver_pattern(5.1);
device_driver_controller(4);
• What is a driver?
An interface layer that translate hardware to software
• Device driver standardization
Fundamental for dynamic drivers load
device_driver_controller(4);
• Parameters problem
The kernel must be able to communicate in the same
way with all drivers
Each function in each driver have different types and
quantities of parameters
• Solution
Pointer to void
device_driver_controller(4);
Driver example
Generic Device Driver
drvGeneric
-thisDriver: driver
-this_functions: ptrFuncDrv[ ]
-callbackProcess: process*
+availableFunctions: enum = {GEN_FUNC_1, GEN_FUNC_2 }
-init(parameters:void*): char
-genericDrvFunction(parameters:void*): char
-genericIsrSetup(parameters:void*): char
+getDriver(): driver*
driver
+drv_id: char
+functions: ptrFuncDrv[ ]
+drv_init: ptrFuncDrv
device_driver_controller(4);
controller_engine(5.2);
device_driver_controller(4);
• Device Driver Controller
Used as an interface layer between the kernel and
the drivers
Can “discover” all available drivers (statically or
dynamically)
Store information about all loaded drivers
Responsible to interpret the messages received from
the kernel
device_driver_controller(4);
char initDriver(char newDriver) {
char resp = FAIL;
if(dLoaded < QNTD_DRV) {
//get driver struct
drivers[dLoaded] = drvInitVect[newDriver]();
//should test if driver was loaded correcly
resp = drivers[dLoaded]->drv_init(&newDriver);
dLoaded++;
}
return resp;
}
device_driver_controller(4);
char callDriver(char drv_id, char func_id, void *p) {
char i;
for (i = 0; i < dLoaded; i++) {
//find the right driver
if (drv_id == drivers[i]->drv_id) {
return drivers[i]->func[func_id].func_ptr(p);
}
}
return DRV_FUNC_NOT_FOUND;
}
device_driver_controller(4);
void main(void) {
//system initialization
kernelInitialization();
initDriver(DRV_LCD);
callDriver(DRV_LCD, LCD_CHAR, 'D');
callDriver(DRV_LCD, LCD_CHAR, 'E');
callDriver(DRV_LCD, LCD_CHAR, 'F');
callDriver(DRV_LCD, LCD_CHAR, 'C');
callDriver(DRV_LCD, LCD_CHAR, '0');
callDriver(DRV_LCD, LCD_CHAR, 'N');
callDriver(DRV_LCD, LCD_CHAR, '@');
callDriver(DRV_LCD, LCD_CHAR, 'L');
callDriver(DRV_LCD, LCD_CHAR, 'A');
callDriver(DRV_LCD, LCD_CHAR, 'S');
}
device_driver_controller(4);
Where are the defines?
device_driver_controller(4);
enum {
LCD_COMMAND, LCD_CHAR, LCD_INTEGER, LCD_END
};
enum {
DRV_INTERRUPT, DRV_TIMER, DRV_LCD, DRV_END
};
• In order to simplify the design, each driver build
its function define enum.
• The controller builds a driver define enum
device_driver_controller(4);
isr_abstract_layer(5.3);
device_driver_controller(4);
//SDCC compiler way
void isr(void) interrupt 1{
thisInterrupt();
}
//C18 compiler way
void isr (void){
thisInterrupt();
}
#pragma code highvector=0x08
void highvector(void){
_asm goto isr _endasm
}
#pragma code
• Interrupts are closely related to hardware
• Each architecture AND compiler pose a different
programming approach
• How to hide this from programmer?
device_driver_controller(4);
//Inside drvInterrupt.c
//defining the pointer to use in ISR callback
typedef void (*intFunc)(void);
//store the pointer to ISR here
static intFunc thisInterrupt;
//Set interrupt function to be called
char setInterruptFunc(void *parameters) {
thisInterrupt = (intFunc) parameters;
return SUCESS;
}
device_driver_controller(4);
//Interrupt function set without knowing hard/compiler issues
void timerISR(void) {
callDriver(DRV_TIMER, TMR_RESET, 1000);
kernelClock();
}
void main (void){
kernelInit();
initDriver(DRV_TIMER);
initDriver(DRV_INTERRUPT);
callDriver(DRV_TIMER, TMR_START, 0);
callDriver(DRV_TIMER, TMR_INT_EN, 0);
callDriver(DRV_INTERRUPT, INT_TIMER_SET, (void*)timerISR);
callDriver(DRV_INTERRUPT, INT_ENABLE, 0);
kernelLoop();
}
device_driver_controller(4);
driver_callback(5.4);
device_driver_controller(4);
How to make efficient use of CPU peripherals
without using pooling or hard-coding the
interrupts?
device_driver_controller(4);
Callback functions
device_driver_controller(4);
• Callback functions resemble events in high level
programming
e.g.: When the mouse clicks in the button X, please
call function Y.
• The desired hardware must be able to rise an
interrupt
• Part of the work is done under interrupt context,
preferable the faster part
device_driver_controller(4);
Main
Process
Driver
Interrupt
Layer
ISRsetup
(&callbackPtr)
DataRequest (&callbackProc)
HW interrupt
isrPtr()
Kernel
kernelAddProc(&callback)
KernelLoop:
MainProc.()
KernelLoop: CallbackProc()
KernelInit: InitDrv()
Callback
Process
device_driver_controller(4);
//********** Excerpt from drvAdc.c **********
// called from setup time to enable ADC interrupt
// and setup ADC ISR callback
char enableAdcInterrup(void* parameters){
callDriver(DRV_INTERRUPT,INT_ADC_SET,(void*)adcISR);
BitClr(PIR1,6);
return FIM_OK;
}
//********** Excerpt from drvInterrupt.c **********
// store the pointer to the interrupt function
typedef void (*intFunc)(void);
static intFunc adcInterrupt;
// function to set ADC ISR callback for latter use
char setAdcInt(void *parameters) {
adcInterrupt = (intFunc)parameters;
return FIM_OK;
}
device_driver_controller(4);
//********** Excerpt from main.c **********
// Process called by the kernel
char adc_func(void) {
//creating callback process
static process proc_adc_callback = {adc_callback, 0, 0};
callDriver(DRV_ADC,ADC_START,&proc_adc_callback);
return REPEAT;
}
//********** Excerpt from drvAdc.c **********
//function called by the process adc_func (via drv controller)
char startConversion(void* parameters){
callBack = parameters;
ADCON0 |= 0b00000010; //start conversion
return SUCCESS;
}
device_driver_controller(4);
//********** Excerpt from drvInterrupt.c **********
//interrupt function
void isr(void) interrupt 1 {
if (BitTst(INTCON, 2)) { //Timer overflow
}
if (BitTst(PIR1, 6)) { //ADC conversion finished
//calling ISR callback stored
adcInterrupt();
}
}
//********** Excerpt from drvAdc.c **********
//ADC ISR callback function
void adcISR(void){
value = ADRESH;
value <<= 8;
value += ADRESL;
BitClr(PIR1,6);
kernelAddProc(callBack);
}
device_driver_controller(4);
//********** Excerpt from main.c **********
//callback function started from the kernel
char adc_callback(void) {
unsigned int resp;
//getting the converted value
callDriver(DRV_ADC,ADC_LAST_VALUE,&resp);
//changing line and printing on LCD
callDriver(DRV_LCD,LCD_LINE,1);
callDriver(DRV_LCD,LCD_INTEGER,resp);
return SUCCESS;
}
“Don't Reinvent
The Wheel,
Unless You Plan
on Learning More
About Wheels”
Jeff Atwood
Thanks!
Rodrigo Maximiano Antunes de Almeida
rmaalmeida@gmail.com
@rmaalmeida
Universidade Federal de Itajubá
Portal embarcados

More Related Content

PPTX
Embedded systems design @ defcon 2015
PDF
Embedded systems development Defcon 19
PPT
Uart VHDL RTL design tutorial
PPTX
Calculator design with lcd using fpga
PPT
VGA VHDL RTL design tutorial
PPTX
Lcd module interface with xilinx software using verilog
PPTX
ODP
Fpga creating counter with internal clock
Embedded systems design @ defcon 2015
Embedded systems development Defcon 19
Uart VHDL RTL design tutorial
Calculator design with lcd using fpga
VGA VHDL RTL design tutorial
Lcd module interface with xilinx software using verilog
Fpga creating counter with internal clock

What's hot (20)

PDF
I2c drivers
PPTX
Smart Lamp With a GSM Module SIM800L
PPTX
Part-2: Mastering microcontroller with embedded driver development
DOCX
Uart
ODP
FPGA Tutorial - LCD Interface
PDF
Design of VGA Controller using VHDL for LCD Display using FPGA
PPTX
Session1
PPT
Introduction to Android G Sensor I²C Driver on Android
TXT
PIC and LCD
DOCX
m.tech esd lab manual for record
PDF
Analog to Digital Converter
PDF
Micrcontroller iv sem lab manual
PDF
2014 ii c08t-sbc pic para ecg
PDF
LinuxCNC 入門簡介
PDF
VHDL Practical Exam Guide
PDF
I2C Subsystem In Linux-2.6.24
PPTX
Embedded System Programming on ARM Cortex M3 and M4 Course
PPT
Introduction to Vortex86DX2 Motion-Control Evaluation Board
DOC
Interface gsm module with pic
PDF
Embedded system lab work
I2c drivers
Smart Lamp With a GSM Module SIM800L
Part-2: Mastering microcontroller with embedded driver development
Uart
FPGA Tutorial - LCD Interface
Design of VGA Controller using VHDL for LCD Display using FPGA
Session1
Introduction to Android G Sensor I²C Driver on Android
PIC and LCD
m.tech esd lab manual for record
Analog to Digital Converter
Micrcontroller iv sem lab manual
2014 ii c08t-sbc pic para ecg
LinuxCNC 入門簡介
VHDL Practical Exam Guide
I2C Subsystem In Linux-2.6.24
Embedded System Programming on ARM Cortex M3 and M4 Course
Introduction to Vortex86DX2 Motion-Control Evaluation Board
Interface gsm module with pic
Embedded system lab work
Ad

Similar to DEF CON 23 - Rodringo Almeida - embedded system design from electronics (20)

PPTX
Presentation
PPT
Introduction to Blackfin BF532 DSP
PPT
Picmico
PPTX
131080111003 mci
PPTX
Microcontroller from basic_to_advanced
PPTX
Rig nitc [autosaved] (copy)
PPTX
Track c-High speed transaction-based hw-sw coverification -eve
PPTX
embedded system and AVR
PDF
Expanding The Micro Blaze System
PPTX
Embedded system
PPT
Embedded c programming22 for fdp
PPTX
ESD -DAY 24.pptx
PPTX
System Software
PPT
chapter 4
PPTX
Microcontroller
PDF
PPTX
EMBEDDED SYSTEM BASICS
PPTX
Programming A Robot Using
PDF
Microcontroladores: Programación con microcontrolador PIC
PDF
Programming with PIC microcontroller
Presentation
Introduction to Blackfin BF532 DSP
Picmico
131080111003 mci
Microcontroller from basic_to_advanced
Rig nitc [autosaved] (copy)
Track c-High speed transaction-based hw-sw coverification -eve
embedded system and AVR
Expanding The Micro Blaze System
Embedded system
Embedded c programming22 for fdp
ESD -DAY 24.pptx
System Software
chapter 4
Microcontroller
EMBEDDED SYSTEM BASICS
Programming A Robot Using
Microcontroladores: Programación con microcontrolador PIC
Programming with PIC microcontroller
Ad

More from Felipe Prado (20)

PDF
DEF CON 24 - Sean Metcalf - beyond the mcse red teaming active directory
PDF
DEF CON 24 - Bertin Bervis and James Jara - exploiting and attacking seismolo...
PDF
DEF CON 24 - Tamas Szakaly - help i got ants
PDF
DEF CON 24 - Ladar Levison - compelled decryption
PDF
DEF CON 24 - Clarence Chio - machine duping 101
PDF
DEF CON 24 - Chris Rock - how to overthrow a government
PDF
DEF CON 24 - Fitzpatrick and Grand - 101 ways to brick your hardware
PDF
DEF CON 24 - Rogan Dawes and Dominic White - universal serial aBUSe remote at...
PDF
DEF CON 24 - Jay Beale and Larry Pesce - phishing without frustration
PDF
DEF CON 24 - Gorenc Sands - hacker machine interface
PDF
DEF CON 24 - Allan Cecil and DwangoAC - tasbot the perfectionist
PDF
DEF CON 24 - Rose and Ramsey - picking bluetooth low energy locks
PDF
DEF CON 24 - Rich Mogull - pragmatic cloud security
PDF
DEF CON 24 - Grant Bugher - Bypassing captive portals
PDF
DEF CON 24 - Patrick Wardle - 99 problems little snitch
PDF
DEF CON 24 - Plore - side -channel attacks on high security electronic safe l...
PDF
DEF CON 24 - Six Volts and Haystack - cheap tools for hacking heavy trucks
PDF
DEF CON 24 - Dinesh and Shetty - practical android application exploitation
PDF
DEF CON 24 - Klijnsma and Tentler - stargate pivoting through vnc
PDF
DEF CON 24 - Antonio Joseph - fuzzing android devices
DEF CON 24 - Sean Metcalf - beyond the mcse red teaming active directory
DEF CON 24 - Bertin Bervis and James Jara - exploiting and attacking seismolo...
DEF CON 24 - Tamas Szakaly - help i got ants
DEF CON 24 - Ladar Levison - compelled decryption
DEF CON 24 - Clarence Chio - machine duping 101
DEF CON 24 - Chris Rock - how to overthrow a government
DEF CON 24 - Fitzpatrick and Grand - 101 ways to brick your hardware
DEF CON 24 - Rogan Dawes and Dominic White - universal serial aBUSe remote at...
DEF CON 24 - Jay Beale and Larry Pesce - phishing without frustration
DEF CON 24 - Gorenc Sands - hacker machine interface
DEF CON 24 - Allan Cecil and DwangoAC - tasbot the perfectionist
DEF CON 24 - Rose and Ramsey - picking bluetooth low energy locks
DEF CON 24 - Rich Mogull - pragmatic cloud security
DEF CON 24 - Grant Bugher - Bypassing captive portals
DEF CON 24 - Patrick Wardle - 99 problems little snitch
DEF CON 24 - Plore - side -channel attacks on high security electronic safe l...
DEF CON 24 - Six Volts and Haystack - cheap tools for hacking heavy trucks
DEF CON 24 - Dinesh and Shetty - practical android application exploitation
DEF CON 24 - Klijnsma and Tentler - stargate pivoting through vnc
DEF CON 24 - Antonio Joseph - fuzzing android devices

Recently uploaded (20)

PDF
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
PDF
Zenith AI: Advanced Artificial Intelligence
PPTX
Tartificialntelligence_presentation.pptx
PDF
Unlocking AI with Model Context Protocol (MCP)
PDF
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
PDF
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
PPTX
cloud_computing_Infrastucture_as_cloud_p
PPTX
A Presentation on Artificial Intelligence
PDF
A comparative study of natural language inference in Swahili using monolingua...
PDF
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
PDF
August Patch Tuesday
PDF
A comparative analysis of optical character recognition models for extracting...
PDF
Heart disease approach using modified random forest and particle swarm optimi...
PDF
Getting Started with Data Integration: FME Form 101
PDF
project resource management chapter-09.pdf
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PDF
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
PDF
MIND Revenue Release Quarter 2 2025 Press Release
PPTX
OMC Textile Division Presentation 2021.pptx
PDF
Hindi spoken digit analysis for native and non-native speakers
From MVP to Full-Scale Product A Startup’s Software Journey.pdf
Zenith AI: Advanced Artificial Intelligence
Tartificialntelligence_presentation.pptx
Unlocking AI with Model Context Protocol (MCP)
Microsoft Solutions Partner Drive Digital Transformation with D365.pdf
DASA ADMISSION 2024_FirstRound_FirstRank_LastRank.pdf
cloud_computing_Infrastucture_as_cloud_p
A Presentation on Artificial Intelligence
A comparative study of natural language inference in Swahili using monolingua...
Transform Your ITIL® 4 & ITSM Strategy with AI in 2025.pdf
August Patch Tuesday
A comparative analysis of optical character recognition models for extracting...
Heart disease approach using modified random forest and particle swarm optimi...
Getting Started with Data Integration: FME Form 101
project resource management chapter-09.pdf
Building Integrated photovoltaic BIPV_UPV.pdf
ENT215_Completing-a-large-scale-migration-and-modernization-with-AWS.pdf
MIND Revenue Release Quarter 2 2025 Press Release
OMC Textile Division Presentation 2021.pptx
Hindi spoken digit analysis for native and non-native speakers

DEF CON 23 - Rodringo Almeida - embedded system design from electronics