4. Dr.
Amr
Talaat
Embedded
Systems
Embedded Systems
According to forecasts, future of IT c
haracterized by terms such as
Disappearing computer,
Ubiquitous computing,
Pervasive computing,
Ambient intelligence,
Post-PC era,
Cyber-physical systems.
Internet of Things
Basic technologies:
Embedded Systems
Communication technologies
5. Dr.
Amr
Talaat
Embedded
Systems
Embedded Systems & Cyber-Physical Systems
“Dortmund“ Definition: [Peter Marwedel]
Embedded systems are information processing systems
embedded into a larger product
Berkeley: [Edward A. Lee]:
Embedded software is software integrated with physical
processes. The technical problem is managing time and
concurrency in computational systems.
Definition: Cyber-Physical (cy-phy) Systems (CPS)
are integrations of computation with physical
processes [Edward Lee, 2006].
13. Dr.
Amr
Talaat
Embedded
Systems
13
Some common characteristics of ES
Dependability
Single-functioned (dedicated System)
Executes a single program, repeatedly
Tightly-constrained (Efficient)
Low cost, low power, small, fast, etc.
Reactive and real-time
Continually reacts to changes in the system’s
environment
Must compute certain results in real-time with
out delay
14. Dr.
Amr
Talaat
Embedded
Systems
Dependability
ES Must be dependable,
• Reliability R(t) = probability of system working
correctly provided that is was working at t=0
• Maintainability M(d) = probability of system working
correctly d time units after error occurred.
• Availability A(t): probability of system working at time t
• Safety: no harm to be caused
• Security: confidential and authentic communication
Even perfectly designed systems can fail if the assumptions about the
workload and possible errors turn out to be wrong.
Making the system dependable must not be an after-thought, it must
be considered from the very beginning
16. Dr.
Amr
Talaat
Embedded
Systems
Real-time constraints
Many ES must meet real-time constraints
A real-time system must react to stimuli from the con
trolled object (or the operator) within the time interv
al dictated by the environment.
For real-time systems, right answers arriving too late
are wrong.
“A real-time constraint is called hard, if not me
eting that constraint could result in a catastrop
he“ [Kopetz, 1997].
All other time-constraints are called soft.
A guaranteed system response has to be explained w
ithout statistical arguments
18. Dr.
Amr
Talaat
Embedded
Systems
Reactive & hybrid systems
Typically, ES are reactive systems:
“A reactive system is one which is in continual interacti
on with is environment and executes at a pace determi
ned by that environment“ [Bergé, 1995]
Behavior depends on input and current state.
automata model appropriate,
model of computable functions inappropriate.
Hybrid systems
(analog + digital parts).
20. Dr.
Amr
Talaat
Embedded
Systems
Design Challenge
Time-to-prototype: the time needed to build a working v
ersion of the system
Time-to-market: the time required to develop a system to
the point that it can be released and sold to customers
NRE cost (Non-Recurring Engineering cost):
The one-time monetary cost of designing the syst
em
Flexibility: the ability to change the functionality
of the system without incurring heavy NRE cost
21. Dr.
Amr
Talaat
Embedded
Systems
Design challenge – optimizing design metrics
Obvious design goal:
Construct an implementation with desired fun
ctionality
Key design challenge:
Simultaneously optimize numerous design me
trics
Design metric
A measurable feature of a system’s impleme
ntation
Optimizing design metrics is a key challenge
22. Dr.
Amr
Talaat
Embedded
Systems
Design metric competition -- improving one
may worsen others
Expertise with both soft
ware and hardware is
needed to optimize
design metrics
Not just a hardware or soft
ware expert, as is common
A designer must be comfor
table with various technolo
gies in order to choose the
best for a given application
and constraints
Size
Performance
Power
NRE cost
24. Dr.
Amr
Talaat
Embedded
Systems
Design Questions
How much Power is needed?
Is power an issue?
Average Current needed?
Max. Transient Current
Do I need communications/Networking module?
How far? (Distance Travelled)
Authorized Frequencies
Antenna Size
Required Power
It is 1 device or in a network?
Do I need routing?
…………
27. Dr.
Amr
Talaat
Embedded
Systems
IOT EXTRA Design Challenges
Larger Scale: Scalability, the ability of a network to support the
increase of its limiting parameters. We discuss the first four scal
ability issues here.
Large Network Size: In the Internet of Things, we are tal
king about interaction with thousands of devices in one pl
ace.
Massive Number of Events: A significant challenge is pos
ed by the enormous number of events generated by obje
cts.
Mobility Rate: Higher mobility rate causes more breakag
e of links and causes more routing information becoming
out-of-date.
Heterogeneous Devices: In the Internet of Things there i
s a wide variety of hardware and devices, in all shapes an
d sizes.
28. Dr.
Amr
Talaat
Embedded
Systems
IOT EXTRA Design Challenges (cont.)
Spontaneous Interaction: Sudden interactions happen as the
objects move around and come into other objects’ wireless rang
e. This leads to the spontaneous generation of events.
Zero Infrastructure: In the Internet of Things setting, devices
need to discover each other as well as the resources provided by
other devices in the surroundings. The challenge here is that the
re is no fixed infrastructure to manage resource publication, disc
overy and communication.
31. Dr.
Amr
Talaat
Embedded
Systems
Controller
Four important factors for the controller
Number of transistors -> size, cost, power
Number of clock cycles -> power
Time to MARKET-> cost, acceptance
Nonrecurring engineering cost (NRE) -> cost, a
cceptance
Ideal: Minimize all factors at the same time!
32. Dr.
Amr
Talaat
Embedded
Systems
Tasks of the controller
Running of (real time) data processing and com
munication protocols
Perform and control the application program
Energy management of the node
Different operation modes available (active, i
dle, listen, sleep, etc.)
33. Dr.
Amr
Talaat
Embedded
Systems
Embedded processors (MicroControllers )
Programmed once by manufacturer of system
Executes a single program (or a limited suite) wi
th few parameters
Task-specific
can be optimized for specific application
Interacts with environment in many ways
direct sensing and control of signal wires
communication protocols to environment and
other devices
real-time interactions and constraints
34. Dr.
Amr
Talaat
Embedded
Systems
Microprocessors and Microcontrollers
• A microprocessor unit (MPU) is a processor on
one silicon chip.
• Microcontrollers are used in embedded
computing.
• A Microcontroller unit (MCU) is a
microprocessor with some added circuitry on
one silicon chip.
38. Dr.
Amr
Talaat
Embedded
Systems
Introduction to Embedded Systems & Microcontrollers
CSEN701: Embedded Systems 38
MCU Types
• MCUs can be classified according:
• MCU bits (e.g. 8-bit, 16-bit, 32-bit)
• Instruction Set Architecture (ISA)
40. Dr.
Amr
Talaat
Embedded
Systems
Instruction Set Architecture
• Instruction Set Architecture (ISA) is the part of the
computer architecture related to programming, including
the native data types, instructions, registers, addressing
modes, memory architecture, interrupt and exception
handling, and external I/O.
• The ISA defines:
• Operations that the processor can execute,
• The mechanism of Data Transfer and how to access data,
• Control Mechanisms (branch, jump, etc)
• “Contract” between programmer/compiler and hardware
• The main ISAs are CISC and RISC:
• CISC stands for Complex Instruction Set Computer
• RISC stands for Reduced Instruction Set Computer
41. Dr.
Amr
Talaat
Embedded
Systems
The CISC Approach
• Emphasis on hardware
• Includes multi-clock Instructions
• Has a large amount of different (in length) and
• complex instructions
• The philosophy behind it is that hardware is always
faster than software, therefore one should make a
powerful instruction-set,
• Memory-to-memory:
• "LOAD" and "STORE“ are incorporated in instructions
• Small code sizes (use less number of instructions),
• Requires more transistors
42. Dr.
Amr
Talaat
Embedded
Systems
The RISC Approach
• RISC processors only use simple instructions that can be executed
within one clock cycle.
• Because there are more lines of code, more RAM is needed to store
the assembly level instructions. The compiler must also perform
more work to convert a high-level language statement into code of
this form.
• Because each instruction requires only one clock cycle to execute,
the entire program will execute in approximately the same amount
of time as the multi-cycle "MULT" command.
• These RISC "reduced instructions" require less transistors of
hardware space than the complex instructions, leaving more room
for general purpose registers.
• Because all of the instructions execute in a uniform amount of time
• (i.e. one clock), pipelining is possible.
• It’s also called : Load-Store Architecture
43. Dr.
Amr
Talaat
Embedded
Systems
Microcontroller Features
Processor speed: Fundamental measure of pro
cessing rate of device
Value of interest is in MIPS, not MHz
Supply voltage/current: Measure of the amount
of power required to run the device
Multiple modes (sleep, idle, etc)
45. Dr.
Amr
Talaat
Embedded
Systems
RAM: Random Access Memory
• Volatile or Non-Permanent Memory, i.e., data is lost after the
removal of power,
• Can be Written too Many Times,
• It is a general purpose memory which is typically used for
storing user data, temporary or Changeable Data
• Most microcontrollers have some amount of internal RAM.
Generally, 1 kbytes (“Embedded” in MCUs) is a common
amount, although some microcontrollers have more, some
less.
46. Dr.
Amr
Talaat
Embedded
Systems
It is electrically-erasable-and-programmable.
Internally, they are similar to EPROMs, but the erase
operation is accomplished electrically, rather than by
exposure to ultraviolet light. Any byte within an
EEPROM may be erased and rewritten.– Erased by
higher voltage.
The primary tradeoff for this improved
functionality is higher cost, though write cycles are
also significantly longer than writes to a RAM.
EEPROM are typically used to store permanent data
EEPROM: Electrically Erasable Programmable Read-Only Memory
47. Dr.
Amr
Talaat
Embedded
Systems
Flash Memory
• Flash memory devices are
• high density,
• low cost,
• nonvolatile,
• fast (to read, but not to write), and
• electrically reprogrammable.
• Write/Erase large blocks of bytes
• Read bytes
• EEPROM is similar to flash memory (sometimes called flash
EEPROM).
• The principal difference is that
• EEPROM requires data to be written or erased one byte at a time
whereas flash memory allows data to be written or erased in
blocks.
• Flash memory is typically used in MCUs for storing program code
51. Dr.
Amr
Talaat
Embedded
Systems
Introduction to ARM
Acorn Computers Ltd. (Cambridge, England) Nov.
1990
First called Acorn RISC Machine, then Advanced R
ISC Machine
Based on RISC architecture work done at UCal Be
rkley and Stanford
ARM only sells licenses for its core architect
ure design
Optimized for low power & performance
VersatileExpress board with Cortex-A9 (ARMv7) c
ore will be “emulated” using Linaro builds.
This also means some things may not work. You’v
e been warned.
52. Dr.
Amr
Talaat
Embedded
Systems
ARM architecture versions
Architecture Family
ARMv1 ARM1
ARMv2 ARM2, ARM3
ARMv3 ARM6, ARM7
ARMv4 StrongARM, ARM7TDMI, ARM9TDMI
ARMv5 ARM7EJ, ARM9E, ARM10E, Xscale
ARMv6 ARM11, ARM Cortex-M
ARMv7 ARM Cortex-A, ARM Cortex-M, ARM Cortex-R
ARMv8 Not available yet. Will support 64-bit addressing +
data
52
56. Dr.
Amr
Talaat
Embedded
Systems
Microcontroller Ports and Pins
The communication channels
through which information
flows into or out of the
microcontroller
Ex. PORTB
Pins PB0 – PB7
May not be contiguous
Often bi-directional
C
57. Dr.
Amr
Talaat
Embedded
Systems
Port Pin Data Directionality
Input
When you want to take information from the e
xternal world (sensors) into the MCU
Output
When you want to change the state of somethi
ng outside the MCU (turn a motor on or off, e
tc.)
Pins default to input direction on power-up
or reset
Your program can set or change the direct
ionality of a pin at any time
59. Dr.
Amr
Talaat
Embedded
Systems
Setting the Pin Data Direction
Arduino
pinMode(pin_no., dir)
Ex. Make Arduino pin 3 (PD3) an output
pinMode(3, OUTPUT);
pinMode(PIN_D3, OUTPUT); // with me
106.h
Note: one pin at a time
Suppose you wanted Arduino pins 3, 5, and
7 (PD3, PD5, and PD7) to be outputs?
Is there a way to make them all outputs at t
he same time?
Yes! Answer coming later…
60. Dr.
Amr
Talaat
Embedded
Systems
Pin Voltages
Microcontrollers are fundamentally digital device
s. For digital IO pins:
Information is ‘coded’ in two discrete states:
HIGH or LOW (logic: 1 or 0)
Voltages
TTL
5 V (for HIGH)
0 V (for LOW)
3.3 V CMOS
3.3 V (for HIGH)
0 V (for LOW)
61. Dr.
Amr
Talaat
Embedded
Systems
Pin Used as an Output
Turn on an LED, which is co
nnected to pin Arduino pin 0
(PD0) (note the resistor!)
What should the data directio
n be for pin 0 (PD0)?
pinMode(____, ____);
Turn on the LED
digitalWrite(PIN_LED,HIGH);
Turn off the LED
digitalWrite(PIN_LED,LOW);
ATmega328
Arduino
pin 0
(PD0)
62. Dr.
Amr
Talaat
Embedded
Systems
Pins as Inputs and Pull-up Resistors - 1
Using a switch as a sensor
Ex. Seat belt sensor
Detect the switch state
What should the data directio
n be for Arduino pin 3 (PD3)?
pinMode(____, ____);
What will the voltage be on P
D3 when the switch is closed?
What will the voltage be on P
D3 when the switch is open?
Indeterminate!
ATmega328
Arduino
pin 3
(PD3)
SPST
momentary
63. Dr.
Amr
Talaat
Embedded
Systems
Pins as Inputs and Pull-up Resistors - 2
Switch as a sensor, cont.
Make the voltage on the pin d
eterminate by turning on the
pull-up resistor for PD3
Assuming PD3 is an input:
digitalWrite(PIN_SWITCH,HIGH); tur
ns on the “pull-up” resistor
pinMode(PIN_SWITCH,INPUT_PULLUP);
What will the voltage on PD3 be
when the switch is open?
VTG
What will the voltage on PD3 be
when the switch is closed?
ATmega328
PD3
1
VTG= +5V
0
64. Dr.
Amr
Talaat
Embedded
Systems
Pins as Inputs and Pull-up Resistors - 3
Switch as a sensor, cont.
To turn off the pull-up resist
or
Assuming PD3 is an input
:
digitalWrite(PIN_SWITCH,LOW); tur
ns the “pull-up” resistor off
ATmega328
PD3
VTG= +5V
0
1
65. Dr.
Amr
Talaat
Embedded
Systems
Pins as Inputs and Pull-up Resistors - 4
Possibility of ‘weak drive’ when
pull-up resistor is turned on
Pin set as an input with a pul
l-up resistor turned on can s
ource a small current
Remember this!
ATmega328
PD3
VTG= +5V
0
1
iweak
66. Dr.
Amr
Talaat
Embedded
Systems
Example 1
Arduino approach Alternate approach
Make Arduino pins 3, 5, and 7 (PD3, PD5, and
PD7) to be outputs
pinMode(3, OUTPUT);
pinMode(5, OUTPUT);
pinMode(7, OUTPUT);
DDRD = 0b10101000;
or
DDRD = 0xA8;
or
DDRD | = 1<<PD7 | 1<<PD5 | 1<<PD3;
More on this coming soon!
Or if me106.h is used:
pinMode(PIN_D3, OUTPUT);
pinMode(PIN_D5, OUTPUT);
pinMode(PIN_D7, OUTPUT);
67. Dr.
Amr
Talaat
Embedded
Systems
Example 2
Arduino approach Alternate approach
Make pins Arduino pins 0 and 1 (PD0 and PD1)
inputs, and turn on pull-up resistors
pinMode(0, INPUT);
pinMode(1, INPUT);
digitalWrite(0, HIGH);
digitalWrite(1, HIGH);
DDRD = 0; // all PORTD pins inputs
PORTD = 0b00000011;
or
PORTD = 0x03;
or better yet:
DDRD & = ~(1<<PD1 | 1<<PD0);
PORTD | = (1<<PD1 | 1<<PD0);
More on this coming soon!
Or if me106.h is used:
pinMode(PIN_D0, INPUT);
pinMode(PIN_D1, INPUT);
digitalWrite(PIN_D0, HIGH);
digitalWrite(PIN_D1, HIGH);
68. Dr.
Amr
Talaat
Embedded
Systems
Structure of an Arduino Program
An arduino program == ‘ske
tch’
Must have:
setup()
loop()
setup()
configures pin modes an
d registers
loop()
runs the main body of th
e program forever
like while(1) {…}
Where is main() ?
Arduino simplifies things
Does things for you
/* Blink - turns on an LED for DELAY_ON msec,
then off for DELAY_OFF msec, and repeats
BJ Furman rev. 1.1 Last rev: 22JAN2011
*/
#define LED_PIN 13 // LED on digital pin 13
#define DELAY_ON 1000
#define DELAY_OFF 1000
void setup()
{
// initialize the digital pin as an output:
pinMode(LED_PIN, OUTPUT);
}
// loop() method runs forever,
// as long as the Arduino has power
void loop()
{
digitalWrite(LED_PIN, HIGH); // set the LED on
delay(DELAY_ON); // wait for DELAY_ON msec
digitalWrite(LED_PIN, LOW); // set the LED off
delay(DELAY_OFF); // wait for DELAY_OFF msec
}
69. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 1
‘Reading a pin’
Write some lines of C code for t
he Arduino to determine a cour
se of action if the seat belt has
been latched (switch closed).
If latched, the ignition should be e
nabled through a call to a function
ig_enable().
If not latched, the ignition should b
e disabled through a call to a functi
on ig_disable()
Write pseudocode first
ATmega328
PD3
70. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 1 Pseudocode
‘Reading a pin’
Pseudocode:
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THE
N
call function ig_enable()
ELSE
call function ig_disable()
ATmega328
PD3
VTG= +5V
0
1
71. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 1 Code
‘Reading a pin’
Pseudocode:
Set up PD3 as an input
Turn on PD3 pull-up resistor
Read voltage on Arduino pin 3 (PIN_D3)
IF PIN_D3 voltage is LOW (latched), THEN
call function ig_enable()
ELSE
call function ig_disable()
ATmega328
PD3
VTG= +5V
0
1
#define PIN_SWITCH 3
#define LATCHED LOW
pinMode(PIN_SWITCH,INPUT_PULLUP);
belt_state = digitalRead(PIN_SWITCH);
if (belt_state == LATCHED)
{ ig_enable(); }
else
{ ig_disabled(); }
One way
(snippet, not full program)
72. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 2
‘Reading from and writing t
o a pin’
Write some lines of C code fo
r the Arduino to turn on a la
mp (PD2) and buzzer (PD3) i
f the key is in the ignition (P
D0 closed), but seat belt is n
ot latched (PD1 open)
(diagram shows only one of the
two switches, but both are simil
ar)
Pseudocode first
ATmega328
PD0, PD1
PD2
PD3
73. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 2 Pseudocode
Pseudocode:
Set up data direction of pins
Make PD0 and PD1 inputs
Turn on pull up resistors for PD0 and PD1
Make PD2 and PD3 outputs
Loop forever
IF key is in ignition THEN
IF belt is latched, THEN
Turn off buzzer
Turn off lamp
ELSE
Turn on lamp
Turn on buzzer
ELSE
Turn off buzzer
Turn off lamp
ATmega328
PD0, PD1
VTG= +5V
0
1
PD2
PD3
74. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 2 (Arduino style code)
#define PIN_IGNITION 0
#define PIN_SEATBELT 1
#define PIN_LED 2
#define PIN_BUZZER 3
#define SEATBELT_LATCHED LOW
#define KEY_IN_IGNITION LOW
#define LED_ON HIGH
#define LED_OFF LOW
#define BUZZER_ON HIGH
#define BUZZER_OFF LOW
void setup()
{
pinMode(PIN_IGNITION, INPUT_PULLUP); // key switch
pinMode(PIN_SEATBELT, INPUT_PULLUP); // belt latch
switch
pinMode(PIN_LED, OUTPUT); // lamp
pinMode(PIN_BUZZER, OUTPUT); // buzzer
}
void loop()
{ /* see next page for code */}
ATmega328
PD0, PD1
VTG= +5V
0
1
PD2
PD3
75. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 2 (Arduino style code)
/* see previous page for code before loop() */
void loop()
{
int key_state = digitalRead(PIN_IGNITION);
int belt_state = digitalRead(PIN_SEATBELT);
if (key_state == KEY_IN_IGNITION)
{
if (belt_state == SEATBELT_LATCHED)
{
digitalWrite(PIN_BUZZER, BUZZER_OFF);
digitalWrite(PIN_LED, LED_OFF);
}
else // key is in ignition, but seatbelt NOT
latched
{
digitalWrite(PIN_BUZZER, BUZZER_ON);
digitalWrite(PIN_LED, LED_ON);
}
else // key is NOT in ignition
{
digitalWrite(PIN_BUZZER, BUZZER_OFF);
digitalWrite(PIN_LED, LED_OFF);
}
}
}
ATmega328
PD0, PD1
VTG= +5V
0
1
PD2
PD3
76. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 3 (Register style code)
/* NOTE: #defines use predefined PORT pin numbers for ATmega328 */
#define PIN_IGNITION PD0
#define PIN_SEATBELT PD1
#define PIN_LED PD2
#define PIN_BUZZER PD3
#define SEATBELT_LATCHED LOW
#define KEY_IN_IGNITION LOW
#define LED_ON HIGH
#define LED_OFF LOW
#define BUZZER_ON HIGH
#define BUZZER_OFF LOW
#define _BIT_MASK( bit ) ( 1 << (bit) ) // same as _BV( bit)
void setup()
{
PORTD = 0; // all PORTD pullups off
DDRD = _BIT_MASK(PIN_LED) | _BIT_MASK(PIN_BUZZER); // LED and buzzer
PORTD | = _BV(PIN_IGNITION) | _BV(PIN_SEATBELT); // pullups for switches
}
/* See next page for loop() code */
ATmega328
PD0, PD1
VTG= +5V
0
1
PD2
PD3
77. Dr.
Amr
Talaat
Embedded
Systems
Digital IO – Practice 3 (Register style code)
/* see previous page for setup() code */
void loop()
{
uint8_t current_PORTD_state, key_state, belt_state;
current_PORTD_state = PIND; // snapshot of PORTD pins
key_state = current_PORTD_state & _BV(PIN_IGNITION);
belt_state = current_PORTD_state & _BV(PIN_SEATBELT);
if (key_state == KEY_IN_IGNITION)
{
if (belt_state == SEATBELT_LATCHED)
{
PORTD & = ~( _BV(PIN_LED) | _BV(PIN_BUZZER) );
}
else
{
PORTD | = ( _BV(PIN_LED) | _BV(PIN_BUZZER) );
}
}
else
{
PORTD & = ~( _BV(PIN_LED) | _BV(PIN_BUZZER) );
}
}
ATmega328
PD0, PD1
VTG= +5V
0
1
PD2
PD3