56
Einführung in Eingebettete Systeme Vorlesung 2 Bernd Finkbeiner 23/10/2013 [email protected] Prof. Bernd Finkbeiner, Ph.D. [email protected] 1

Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Embed Size (px)

Citation preview

Page 1: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Einführung in

Eingebettete Systeme

Vorlesung 2 Bernd Finkbeiner23/10/2013 [email protected]

Prof. Bernd Finkbeiner, [email protected]

1

Page 2: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Letzte Woche: Modelle

2

Ein Systemmodell (kurz: Modell) ist eine Abstraktion eines Systems, welche nur eine Menge ausgewählter, gerade interessierender Sachverhalte des betrachteten Systems aufweist.

[Tabeling 2006]

Ein komplexes System hat üblicherweise mehrere Modelle!

Page 3: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

3

Kommunikation,NebenläufigkeitSteuerung

Hardware

Software

A/D

D/A

Aktoren

Sensoren

Physikalische Umgebung

Physikalische Umgebung

Endliche Automaten

Page 4: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Endliche Automaten

Ein deterministischer Automat M=(I, O, S, s0, δ, λ)besteht aus den folgenden Komponenten:

I ist eine endliche, nicht-leere Menge von Eingabesymbolen (das Eingabealphabet),

O ist eine endliche, nicht-leere Menge von Ausgabesymbolen (das Ausgabealphabet)

S eine endliche, nicht-leere Menge von Zuständen, s0 ∈ S ist der Initialzustand, δ : S × I → S ist die Transitionsfunktion, λ ist die Ausgabefunktion.

4

Page 5: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

5

Analysemodell Konstruktionsmodell

Modell

System

Ana

lyse

Des

ign

Widerspruch:Modell fehlerhaft

Widerspruch:System fehlerhaft

[Tabeling 2006]

Page 6: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

3. Microcontroller

Ziele:

Überblick über die wesentlichen Bestandteileeines Microcontrollers

Erstellen einfacher Programme(insbesondere die Implementierung endlicher Automaten auf einem Microcontroller).

Page 7: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

7

Arduino

ATmega328

Page 8: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Microcontroller

Prozessorkern, Speicher, und Ein-Ausgabeschnittstellen auf einem Chip

Ziel ist, Steuerungs- oder Kommunikationsaufgaben mit möglichst wenigen Bausteinen zu lösen

Es existiert eine Vielzahl von Microcontrollern, organisiert in Microcontrollerfamilien (Familienmitglieder haben meist gleichen Prozessorkern aber unterschiedlichen Speicher und Schnittstellen)

8

Page 9: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

9[Brinkschulte/Ungerer, Mikrocontroller und Mikroprozessoren, 2002]

Page 10: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

von-Neumann Prinzip

Der Rechner ist zentral gesteuert und aus Funktionseinheiten aufgebaut (Zentraleinheit, Speicher, Ein-/Ausgabeeinheit).

Der Rechner ist nicht speziell auf ein zu bearbeitendes Problem zugeschnitten. Zur Lösung eines Problems wird ein eigenes Programm im Speicher abgelegt.("programmgesteuerter Universalrechner")

von-Neumann Architektur: Programme und Daten liegen im selben Speicher.

Harvard Architektur: Programme und Daten liegen in getrennten Speichern. (ATmega328 hat Harvard Architektur)

10

Page 11: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

ATmega328

5ATmega48A/PA/88A/PA/168A/PA/328/P [DATASHEET SUMMARY]8271GS–AVR–02/2013

2. OverviewThe ATmega48A/PA/88A/PA/168A/PA/328/P is a low-power CMOS 8-bit microcontroller based on the AVRenhanced RISC arch i tec ture . By execut ing power fu l ins t ruc t ions in a s ing le c lock cyc le , theATmega48A/PA/88A/PA/168A/PA/328/P achieves throughputs approaching 1 MIPS per MHz allowing the systemdesigner to optimize power consumption versus processing speed.

2.1 Block Diagram

Figure 2-1. Block Diagram

The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers aredirectly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in onesingle instruction executed in one clock cycle. The resulting architecture is more code efficient while achievingthroughputs up to ten times faster than conventional CISC microcontrollers.

PORT C (7)PORT B (8)PORT D (8)

USART 0

8bit T/C 2

16bit T/C 18bit T/C 0 A/D Conv.

InternalBandgap

AnalogComp.

SPI TWI

SRAMFlash

EEPROM

WatchdogOscillator

WatchdogTimer

OscillatorCircuits /

ClockGeneration

PowerSupervisionPOR / BOD &

RESET

VC

C

GN

D

PROGRAMLOGIC

debugWIRE

2

GND

AREF

AVCC

DAT

AB

US

ADC[6..7]PC[0..6]PB[0..7]PD[0..7]

6

RESET

XTAL[1..2]

CPU

11[www.atmel.com]

Page 12: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

12[Brinkschulte/Ungerer, Mikrocontroller und Mikroprozessoren, 2002]

Prozessorkern (Central Processing Unit, CPU)

Page 13: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Operationsprinzip

Zu jedem Zeitpunkt führt die CPU nur einen einzigen Befehl aus, und dieser kann (höchstens) einen Datenwert neu berechnen (Single Instruction - Single Data, SISD). Der Befehlsablauf folgt einer sequentiellen Befehlsfolge.

In der Interpretations-Phase wird aufgrund der durch den Befehlszähler angegebenen Adresse der Inhalt einer Speicherzelle geholt und als Befehl interpretiert.

In der Ausführungs-Phase wird aufgrund der im Befehl enthaltenen Adresse der Inhalt einer weiteren Speicherzelle geholt und als Datenwert verarbeitet.

13

Page 14: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

ATmega328

14

88161D–AVR–10/09

ATmega48PA/88PA/168PA/328P

6. AVR CPU Core

6.1 OverviewThis section discusses the AVR core architecture in general. The main function of the CPU coreis to ensure correct program execution. The CPU must therefore be able to access memories,perform calculations, control peripherals, and handle interrupts.

Figure 6-1. Block Diagram of the AVR Architecture

In order to maximize performance and parallelism, the AVR uses a Harvard architecture – withseparate memories and buses for program and data. Instructions in the program memory areexecuted with a single level pipelining. While one instruction is being executed, the next instruc-tion is pre-fetched from the program memory. This concept enables instructions to be executedin every clock cycle. The program memory is In-System Reprogrammable Flash memory.

The fast-access Register File contains 32 x 8-bit general purpose working registers with a singleclock cycle access time. This allows single-cycle Arithmetic Logic Unit (ALU) operation. In a typ-

FlashProgramMemory

InstructionRegister

InstructionDecoder

ProgramCounter

Control Lines

32 x 8GeneralPurpose

Registrers

ALU

Statusand Control

I/O Lines

EEPROM

Data Bus 8-bit

DataSRAM

Dire

ct A

ddre

ssin

g

Indi

rect

Add

ress

ing

InterruptUnit

SPIUnit

WatchdogTimer

AnalogComparator

I/O Module 2

I/O Module1

I/O Module n

[www.atmel.com]

Page 15: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Speicher

integrierter Festwert- und Schreiblesespeicher enthält Daten und Programme anders als beim PC ist Speicher meist knapp je nach Stückzahl der Anwendung unterschiedlicher Typ

des Festwertspeichers (ROM: Read Only Memory, PROM: Programmable Read Only Memory, EPROM: Erasable Programmable Read Only Memory, EEPROM: Electrically Erasable Programmable Read Only Memory, Flash)

15

Page 16: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

5ATmega48A/PA/88A/PA/168A/PA/328/P [DATASHEET SUMMARY]8271GS–AVR–02/2013

2. OverviewThe ATmega48A/PA/88A/PA/168A/PA/328/P is a low-power CMOS 8-bit microcontroller based on the AVRenhanced RISC arch i tec ture . By execut ing power fu l ins t ruc t ions in a s ing le c lock cyc le , theATmega48A/PA/88A/PA/168A/PA/328/P achieves throughputs approaching 1 MIPS per MHz allowing the systemdesigner to optimize power consumption versus processing speed.

2.1 Block Diagram

Figure 2-1. Block Diagram

The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers aredirectly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in onesingle instruction executed in one clock cycle. The resulting architecture is more code efficient while achievingthroughputs up to ten times faster than conventional CISC microcontrollers.

PORT C (7)PORT B (8)PORT D (8)

USART 0

8bit T/C 2

16bit T/C 18bit T/C 0 A/D Conv.

InternalBandgap

AnalogComp.

SPI TWI

SRAMFlash

EEPROM

WatchdogOscillator

WatchdogTimer

OscillatorCircuits /

ClockGeneration

PowerSupervisionPOR / BOD &

RESET

VC

C

GN

D

PROGRAMLOGIC

debugWIRE

2

GND

AREF

AVCC

DAT

AB

US

ADC[6..7]PC[0..6]PB[0..7]PD[0..7]

6

RESET

XTAL[1..2]

CPU

ATmega328 - Speicher

16[www.atmel.com]

Harvard Architektur Flash (32KB)

Programmspeicherpermanenter Speicher

SRAM (2KB)(Static Random Access Memory) Kurzfristige Datenflüchtiger Speicher

EEPROM (1KB)Langfristige Daten

Page 17: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Ein-/Ausgabekanäle

seriell oder parallel synchron oder asynchron Echtzeitkanäle:

17

Digitale Schnittstellen

DA/AD Wandler Anschluss analoger

Sensoren und Aktoren Auflösung und

Wandlungszeit sind die wichtigsten Größen

Analoge Schnittstellen

Echtzeiterweiterungder parallelen E/A-Kanäle

Kopplung mit einem Zeitgeber: Ein-/Ausgabezeitpunkt wird von Hardware bestimmt.

Page 18: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

5ATmega48A/PA/88A/PA/168A/PA/328/P [DATASHEET SUMMARY]8271GS–AVR–02/2013

2. OverviewThe ATmega48A/PA/88A/PA/168A/PA/328/P is a low-power CMOS 8-bit microcontroller based on the AVRenhanced RISC arch i tec ture . By execut ing power fu l ins t ruc t ions in a s ing le c lock cyc le , theATmega48A/PA/88A/PA/168A/PA/328/P achieves throughputs approaching 1 MIPS per MHz allowing the systemdesigner to optimize power consumption versus processing speed.

2.1 Block Diagram

Figure 2-1. Block Diagram

The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers aredirectly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in onesingle instruction executed in one clock cycle. The resulting architecture is more code efficient while achievingthroughputs up to ten times faster than conventional CISC microcontrollers.

PORT C (7)PORT B (8)PORT D (8)

USART 0

8bit T/C 2

16bit T/C 18bit T/C 0 A/D Conv.

InternalBandgap

AnalogComp.

SPI TWI

SRAMFlash

EEPROM

WatchdogOscillator

WatchdogTimer

OscillatorCircuits /

ClockGeneration

PowerSupervisionPOR / BOD &

RESET

VC

C

GN

D

PROGRAMLOGIC

debugWIRE

2

GND

AREF

AVCC

DAT

AB

US

ADC[6..7]PC[0..6]PB[0..7]PD[0..7]

6

RESET

XTAL[1..2]

CPU

ATmega328 - E/A

18[www.atmel.com]

14 digitaleAnschlüsse

8 analoge AnschlüsseAuflösung: 10 bit(= 1024 verschiedene Werte)

Richtung programmierbar

Page 19: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

5ATmega48A/PA/88A/PA/168A/PA/328/P [DATASHEET SUMMARY]8271GS–AVR–02/2013

2. OverviewThe ATmega48A/PA/88A/PA/168A/PA/328/P is a low-power CMOS 8-bit microcontroller based on the AVRenhanced RISC arch i tec ture . By execut ing power fu l ins t ruc t ions in a s ing le c lock cyc le , theATmega48A/PA/88A/PA/168A/PA/328/P achieves throughputs approaching 1 MIPS per MHz allowing the systemdesigner to optimize power consumption versus processing speed.

2.1 Block Diagram

Figure 2-1. Block Diagram

The AVR core combines a rich instruction set with 32 general purpose working registers. All the 32 registers aredirectly connected to the Arithmetic Logic Unit (ALU), allowing two independent registers to be accessed in onesingle instruction executed in one clock cycle. The resulting architecture is more code efficient while achievingthroughputs up to ten times faster than conventional CISC microcontrollers.

PORT C (7)PORT B (8)PORT D (8)

USART 0

8bit T/C 2

16bit T/C 18bit T/C 0 A/D Conv.

InternalBandgap

AnalogComp.

SPI TWI

SRAMFlash

EEPROM

WatchdogOscillator

WatchdogTimer

OscillatorCircuits /

ClockGeneration

PowerSupervisionPOR / BOD &

RESET

VC

C

GN

D

PROGRAMLOGIC

debugWIRE

2

GND

AREF

AVCC

DAT

AB

US

ADC[6..7]PC[0..6]PB[0..7]PD[0..7]

6

RESET

XTAL[1..2]

CPU

Zähler und Zeitgeber

wichtig für Echtzeitsteuerungen

Beispielanwendungen: Zählen von Ereignissen, Zeitmessung, Pulsweitemodulation, Frequenz- und Drehzahlmessung, Schrittmotorsteuerungen

ATmega328: drei Zeitgeber

19

Page 20: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Watchdog

„Wachhund“ zur Überwachung der Programmaktivitäten eines Mikrocontrollers

Programm muss in regelmäßigen Abständen Lebenszeichen liefern

Bleiben diese aus, so nimmt der Wachhund einen Fehler im Programmablauf an => Reset

20

Reset by watchdog timer

Page 21: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Unterbrechungen (Interrupts)

Unterbrechung des Programmablaufs bei Ereignissen

Schnelle, vorhersagbare Reaktion auf Ereignisse Insbesondere wichtig bei Echtzeitanwendungen Behandlung eines Ereignisses durch eine Interrupt-

Service-Routine Microcontroller kennen meist

externe Unterbrechungsquellen (Eingangssignale) und interne Unterbrechungsquellen (Zähler, Zeitgeber, E/A-Kanäle, ...)

21

Page 22: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

658161D–AVR–10/09

ATmega48PA/88PA/168PA/328P

.org 0x1C000x1C00 jmp RESET ; Reset handler

0x1C02 jmp EXT_INT0 ; IRQ0 Handler

0x1C04 jmp EXT_INT1 ; IRQ1 Handler

... ... ... ;

0x1C32 jmp SPM_RDY ; Store Program Memory Ready Handler

;

0x1C33 RESET: ldi r16,high(RAMEND); Main program start

0x1C34 out SPH,r16 ; Set Stack Pointer to top of RAM

0x1C35 ldi r16,low(RAMEND)

0x1C36 out SPL,r160x1C37 sei ; Enable interrupts

0x1C38 <instr> xxx

11.4 Interrupt Vectors in ATmega328P

Table 11-6. Reset and Interrupt Vectors in ATmega328P

VectorNo.Program

Address(2) Source Interrupt Definition

1 0x0000(1) RESET External Pin, Power-on Reset, Brown-out Reset and Watchdog System Reset

2 0x0002 INT0 External Interrupt Request 0

3 0x0004 INT1 External Interrupt Request 1

4 0x0006 PCINT0 Pin Change Interrupt Request 0

5 0x0008 PCINT1 Pin Change Interrupt Request 1

6 0x000A PCINT2 Pin Change Interrupt Request 2

7 0x000C WDT Watchdog Time-out Interrupt

8 0x000E TIMER2 COMPA Timer/Counter2 Compare Match A

9 0x0010 TIMER2 COMPB Timer/Counter2 Compare Match B

10 0x0012 TIMER2 OVF Timer/Counter2 Overflow

11 0x0014 TIMER1 CAPT Timer/Counter1 Capture Event

12 0x0016 TIMER1 COMPA Timer/Counter1 Compare Match A

13 0x0018 TIMER1 COMPB Timer/Coutner1 Compare Match B

14 0x001A TIMER1 OVF Timer/Counter1 Overflow

15 0x001C TIMER0 COMPA Timer/Counter0 Compare Match A

16 0x001E TIMER0 COMPB Timer/Counter0 Compare Match B

17 0x0020 TIMER0 OVF Timer/Counter0 Overflow

18 0x0022 SPI, STC SPI Serial Transfer Complete

19 0x0024 USART, RX USART Rx Complete

20 0x0026 USART, UDRE USART, Data Register Empty

21 0x0028 USART, TX USART, Tx Complete

22 0x002A ADC ADC Conversion Complete

668161D–AVR–10/09

ATmega48PA/88PA/168PA/328P

Notes: 1. When the BOOTRST Fuse is programmed, the device will jump to the Boot Loader address at reset, see ”Boot Loader Sup-port – Read-While-Write Self-Programming, ATmega88PA, ATmega168PA and ATmega328P” on page 277.

2. When the IVSEL bit in MCUCR is set, Interrupt Vectors will be moved to the start of the Boot Flash Section. The address of each Interrupt Vector will then be the address in this table added to the start address of the Boot Flash Section.

Table 11-7 on page 66 shows reset and Interrupt Vectors placement for the various combina-tions of BOOTRST and IVSEL settings. If the program never enables an interrupt source, theInterrupt Vectors are not used, and regular program code can be placed at these locations. Thisis also the case if the Reset Vector is in the Application section while the Interrupt Vectors are inthe Boot section or vice versa.

Note: 1. The Boot Reset Address is shown in Table 26-7 on page 289. For the BOOTRST Fuse “1” means unprogrammed while “0” means programmed.

The most typical and general program setup for the Reset and Interrupt Vector Addresses inATmega328P is:

Address Labels Code Comments

0x0000 jmp RESET ; Reset Handler

0x0002 jmp EXT_INT0 ; IRQ0 Handler

0x0004 jmp EXT_INT1 ; IRQ1 Handler

0x0006 jmp PCINT0 ; PCINT0 Handler

0x0008 jmp PCINT1 ; PCINT1 Handler

0x000A jmp PCINT2 ; PCINT2 Handler

0x000C jmp WDT ; Watchdog Timer Handler

0x000E jmp TIM2_COMPA ; Timer2 Compare A Handler

0x0010 jmp TIM2_COMPB ; Timer2 Compare B Handler

0x0012 jmp TIM2_OVF ; Timer2 Overflow Handler

0x0014 jmp TIM1_CAPT ; Timer1 Capture Handler

0x0016 jmp TIM1_COMPA ; Timer1 Compare A Handler

0x0018 jmp TIM1_COMPB ; Timer1 Compare B Handler

0x001A jmp TIM1_OVF ; Timer1 Overflow Handler

0x001C jmp TIM0_COMPA ; Timer0 Compare A Handler

0x001E jmp TIM0_COMPB ; Timer0 Compare B Handler

23 0x002C EE READY EEPROM Ready

24 0x002E ANALOG COMP Analog Comparator

25 0x0030 TWI 2-wire Serial Interface

26 0x0032 SPM READY Store Program Memory Ready

Table 11-6. Reset and Interrupt Vectors in ATmega328P (Continued)

VectorNo.Program

Address(2) Source Interrupt Definition

Table 11-7. Reset and Interrupt Vectors Placement in ATmega328P(1)

BOOTRST IVSEL Reset Address Interrupt Vectors Start Address

1 0 0x000 0x002

1 1 0x000 Boot Reset Address + 0x0002

0 0 Boot Reset Address 0x002

0 1 Boot Reset Address Boot Reset Address + 0x0002

22[www.atmel.com]

Page 23: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Ruhebetrieb (Standby Mode)

Ruhebetrieb zur Reduktion des Energieverbrauchs Energievorrat (oder Wärmeemission) oft beschränkt Abschaltung nicht benötigter Peripheriekomponenten

und Festwertspeicher Erhaltungsspannung am Schreib-/Lesespeicher ATmega328: 6 verschiedene “Sleep modes”

23

Page 24: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

24

Programmierung

Page 25: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

25

16 Embedded Systems Engineering 1

#include ... /* Hier werden fertige Programmdateien eingebunden, die z.B. Deklarationen enthalten */ char a; /* Globale Variable */ int b; main() /* Main() muss immer vorhanden sein und kann auch Parameter sowie Rückgabewert beinhalten */ { char c; /* Funktions-lokale Variablen */ .../* Jetzt folgt der Algorithmus, der z.B. den Aufruf weiterer Funktionen beinhalten kann */ }

Bild 2.10 Aufbau eines Programms in C

2.4.2 Beispielprogramm Zur weiteren Darstellung sei ein Beispielprogramm und dessen Übersetzung im Detail diskutiert. Listing 2.1 zeigt den Sourcecode, geschrieben in C. Das Programm selbst hat wenig Gehalt, hier werden drei char-Variablen deklariert, von denen zweien ein konstanter Initialwert, der bei jedem Durchgang um 1 erhöht wird, zugewiesen wird und der dritten dann die Summe der beiden ersten Variab-len. Zwei wichtige Details sollen an diesem Beispiel gezeigt werden: Ein (beispiel-hafter) Tool-Fluss und ein Assemblerbeispiel.

#include <stdio.h> int main(void) { char a = 3, b = 4, c; while(1) { a++, b++; c = a + b; printf( "%d ", c ); } }

Listing 2.1 Beispielprogramm zur Addition zweier Zahlen, geschrieben in C 2 Kurzeinführung zu Mikrocontrollern und zum ATmega8 17

Der erste Teil der Übersetzung führt vom C-Code zum Assemblercode mit Zusatz-informationen. In Listing 2.2 ist der Assemblercode (im Auszug) gezeigt, der sich aus dem obigen Beispielcode ergibt. Hier sind die Variablen a, b und c noch in symbolischer Form enthalten, ebenso die im Programm benutzte Funktion printf().

0000012a <main>: 12a: cf 93 push r28 12c: df 93 push r29 12e: 00 d0 rcall .+0 ; 0x130 <main+0x6> 130: cd b7 in r28, 0x3d ; 61, SP low 132: de b7 in r29, 0x3e ; 62, SP high (r28/r29 == Y) 134: 83 e0 ldi r24, 0x03 ; 3 (Wert für a) 136: 89 83 std Y+1, r24 ; Variable a, auf Stack 138: 84 e0 ldi r24, 0x04 ; 4 (Wert für b) 13a: 8a 83 std Y+2, r24 ; Variable b, auf Stack 13c: 99 81 ldd r25, Y+1 ; a 13e: 8a 81 ldd r24, Y+2 ; b 140: 89 0f add r24, r25 ; a + b 142: 8b 83 std Y+3, r24 ; Ergebnis in c, auf Stack 144: 8b 81 ldd r24, Y+3 ; c 146: 28 2f mov r18, r24 ; c in r18 148: 30 e0 ldi r19, 0x00 ; 0 in r19 14a: 00 d0 rcall .+0 ; 0x14c <main+0x22> 14c: 0f 92 push r0 14e: 8d b7 in r24, 0x3d ; 61, SP low 150: 9e b7 in r25, 0x3e ; 62, SP high 152: 01 96 adiw r24, 0x01 ; SP-Kopie + 1 154: 40 e0 ldi r20, 0x00 ; 0 156: 52 e0 ldi r21, 0x02 ; 2 158: fc 01 movw r30, r24 ; r24/25 in r30/31 (Z) 15a: 51 83 std Z+1, r21 ; Parameter auf Stack 15c: 40 83 st Z, r20 ; 15e: fc 01 movw r30, r24 ; 160: 33 83 std Z+3, r19 ; Kopie von c auf Stack 162: 22 83 std Z+2, r18 ; 164: 0e 94 b9 00 call 0x172 ; <printf> aufrufen 168: 0f 90 pop r0 ; Stack bereinigen 16a: 0f 90 pop r0 16c: 0f 90 pop r0 16e: 0f 90 pop r0 170: e5 cf rjmp .-54 ; 0x13c <main+0x12>

Listing 2.2 Assemblerübersetzung des Programm aus Listing 2.1 für den Atmel AVR (ohne Optimierung, Auszug aus .lss-File)

Das Programm selbst folgt exakt dem C-Sourcecode, weil keine Optimierung zu-gelassen ist. So werden die Variablen, die in einem späteren Teil der Übersetzung auf Speicheradressen abgebildet werden, mit konstanten Werten belegt, dann die Variable a in den Akkumulator A geladen und mit dem Inhalt der Variable b addiert. Das Additionsergebnis im Akkumulator wird zuletzt in die c entsprechende Speicherstelle geschrieben, und dann beginnt die Ausgabe.

18 Embedded Systems Engineering 1

Die Ausgabe nutzt die im C-Standard definierte printf()-Funktion und wird mit zwei Parametern aufgerufen, die beide auf dem Stack landen (Formatierung, Wert von c) Der Output dieser Phase in ein Object-File (*.o), in dem die feststehenden Teile des Listfiles in Binärcode übersetzt sind. Der anschließend arbeitende Linker bindet dann die genutzten Funktionen aus einer Bibliothek ein (link) und gibt den Symbolen konkrete Adressen (locate). Das Ergebnis ist dann z.B. ein Hex-File (Listing 2.3), das dann als Input für ein Programm zur Übertragung auf den Mikrocontroller dient.

:100100000BBF02C007900D92A430B107D9F712E0DF :10011000A4E0B2E001C01D92AA30B107E1F70E944D :1001200095000C9472030C940000CF93DF9300D0E1 :10013000CDB7DEB783E0898384E08A8399818A81A1 :10014000890F8B838B81282F30E000D00F928DB7E1 :100150009EB7019640E052E0FC0151834083FC01D0 :10016000338322830E94B9000F900F900F900F905D :10017000E5CFCF93DF93CDB7DEB7FE013696619121 :1001800071918091060290910702AF010E94CB000D :10019000DF91CF9108952F923F924F925F926F928D :1001A0007F928F929F92AF92BF92CF92DF92EF9207 :1001B000FF920F931F93CF93DF93CDB7DEB72D97A9 :1001C0000FB6F894DEBF0FBECDBF3C017D876C87B4 :1001D0005A01FC0117821682838181FFC8C12E015A :1001E0000894411C511CF3019381EC85FD8593FD1E :1001F000859193FF8191FD87EC87882309F4B3C132 :10020000853241F493FD859193FF8191FD87EC87C1 :10021000853229F490E0B3010E94E802E4CFFF2484 :10022000EE2410E01032B0F48B3269F08C3228F4F6 :10023000803251F0833271F40BC08D3239F080334B :1002400049F411602CC01260146029C0186027C0E6 :10025000106125C017FD2EC0282F20532A3098F496 :1002600016FF08C08F2D880FF82EFF0CFF0CF80E1C :10027000F20E15C08E2D880FE82EEE0CEE0CE80E57 :10028000E20E10620CC08E3221F416FD6CC11064B7 :1002900006C08C3611F4106802C0883659F4EC851B :1002A000FD8593FD859193FF8191FD87EC878823E0 :1002B00009F0B8CF982F9554933018F0905293309E

Listing 2.3 Hex-File (Auszug) für das Programm in Listing 2.1, Zielsystem Atmel AVR

Verwendet man anstatt char- nun int-Variablen, so werden diese als Variablen mit 16-bit-Breite interpretiert und der Assemblercode ändert sich. Jetzt werden alle Verknüpfungen mit 16 Bit durchgeführt, einschließlich der Wertzuweisung von Konstanten an die Variablen.

Modell

C Programm

Assembler Programm

Binärcode

manuell/Codegenerator

Compiler

Linker

Page 26: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Arduino Programme

C Programme (mit Bibliotheken) Arduino nennt Programme “Sketches” Programmstruktur:

26

Header Deklarationen, Einbinden von Bibliotheken, etc.

setup()wird einmal ausgeführt bei Programmstart

loop()wird immer wieder ausgeführt sobald das Ende erreicht wurde

Page 27: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Beispielprogramm: Blink

27[http://arduino.cc/en/Tutorial/Blink]

/* Blink Turns on an LED on for one second, then off for one second, repeatedly. */ // Pin 13 has an LED connected on most Arduino boards.// give it a name:int led = 13;

// the setup routine runs once when you press reset:void setup() { // initialize the digital pin as an output. pinMode(led, OUTPUT); }

// the loop routine runs over and over again forever:void loop() { digitalWrite(led, HIGH); // turn the LED on (HIGH is the voltage level) delay(1000); // wait for a second digitalWrite(led, LOW); // turn the LED off by making the voltage LOW delay(1000); // wait for a second}

Page 28: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

arduino.cc

28

Page 29: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

29

Implementierung eines Mealy-Automaten

k0

k1

k2

k3

0/P

1/P

0/P 1/Ö

f1

f2

f3

0/P

1/P

1/P0/P

1/S0/S

0/P 1/P1/P

0 1

k0 k1 / P f1 / P

k1 f2 / P k2 / P

k2 k3 / P f3 / P

k3 k0 / S k0 / Ö

f1 f2 / P f2 / P

f2 f3 / P f3 / P

f3 k0 / S k0 / S

Page 30: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Schritt 1: Repräsentation von Zuständen und Eingabesymbolen

Zustände und Symbole können aufgezählt werden,wir können sie deshalb durch ganze Zahlen (integers) darstellen:

30

#define STATE_k0 0#define STATE_k1 1#define STATE_k2 2...#define STATE_f3 6

int current_state;

#define INPUT_0 0#define INPUT_1 1

int current_input;

0 1

k0 k1 / P f1 / P

k1 f2 / P k2 / P

k2 k3 / P f3 / P

k3 k0 / S k0 / Ö

f1 f2 / P f2 / P

f2 f3 / P f3 / P

f3 k0 / S k0 / S

Page 31: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Schritt 2: Funktionen für Ausgaben

Ausgaben entsprechen bestimmten Aktionen (z.B. an einer Schnittstelle). Wir implementieren diese Aktionen als Funktionen.

31

void action_P(){ digitalWrite(led, HIGH); delay(1000); digitalWrite(led, LOW); delay(1000); }

void action_S() {...}

void action_OE() {...}

0 1

k0 k1 / P f1 / P

k1 f2 / P k2 / P

k2 k3 / P f3 / P

k3 k0 / S k0 / Ö

f1 f2 / P f2 / P

f2 f3 / P f3 / P

f3 k0 / S k0 / S

Page 32: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Schritt 3: Initialzustand

Der Initialzustand wird in setup() gesetzt.

32

void setup() { current_state = STATE_k0;}

k0

k1

k2

k3

0/P

1/P

0/P 1/Ö

f1

f2

f3

0/P

1/P

1/P0/P

1/S0/S

0/P 1/P1/P

Page 33: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Schritt 4: Transitionen

Für jede Eingabe wird, in Abhängigkeit vom Zustand, die entsprechende Ausgabeaktion durchgeführt und der neue Zustand gesetzt.

33

void loop() { current_input = get_new_input();

switch(current_state) { case STATE_k0 : if (current_input==INPUT_0) { action_P(); current_state = STATE_k1; } else if (current_input==INPUT_1) { action_P(); current_state = STATE_f1; } case STATE_k1 : ... ... }}

0 1

k0 k1 / P f1 / P

k1 f2 / P k2 / P

k2 k3 / P f3 / P

k3 k0 / S k0 / Ö

f1 f2 / P f2 / P

f2 f3 / P f3 / P

f3 k0 / S k0 / S

Page 34: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

4. StateCharts

Erstellen von kompakten, lesbaren Modellen für diskrete Steuerungenmit Hilfe von Hierarchie und Variablen

Bewusstsein für semantische Schwierigkeiten (insbesondere Superstep Semantik)

Ziele:

Page 35: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

StateCharts – Automaten mit zusätzlichen Konstrukten

Hierarchie Variablen mit komplexen Datentypen Timer

35

Achtung: Bedeutung (Semantik) von StateCharts nicht einheitlich (mehr dazu später).

Referenzsemantik: Statemate StateFlow Semantik UML ...

Page 36: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Beispiel: Anrufbeantworter

incl.

36

Page 37: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Hierarchie

Superzustand (superstate)

Unterzustände (substates)

Wenn Zustand S aktivist, dann ist auch einer der Unterzustände aktiv

(A oder B oder ..)

37

Page 38: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Zustände

Zustände, die nicht aus weiteren Zuständen bestehen, heissen Basiszustände.

Zustände die aus weiteren Zuständen bestehen heissen Superzustände.

Die Superzustände eines Zustands sind seine Ahnen. Superzustände können OR-Superzustände oder AND-

Superzustände sein. Wann immer ein OR-Superzustand aktiv ist, ist auch genau einer seiner Unterzustände aktiv.

Ahne von E

38

Page 39: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Hierarchie

Die Hierarchie kann durch einen Baum repräsentiert werden, in dem die Basiszustände als Blätter auftreten.

Statechart SC

SC

S Z

A B C D E

• Transitionen sind zwischen allen Hierarchie-Ebenen möglich

• Wenn ein Basiszustand aktiv ist, dann sind alle seine Ahnen aktiv.

39

Page 40: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Transitionen zu Superstates

Durch die Transition von Z nach S wird der Superzustand S aktiv. Zusätzlich muss ein Basiszustand aktiv werden.

➤ Default State Mechanismus

➤ History Mechanismus

40

Page 41: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Beispiel: Digitalcamera

41[http://www.boost.org/doc/libs/1_41_0/libs/statechart/doc/tutorial.htm]

Page 42: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Default Konnektoren

Der gefüllte Kreis heißt “default Konnektor” und kennzeichnet den “default state”, der aktiv wird, wenn der Superzustand aktiv wird.

Der Konnektor ist selbst kein Zustand.

Die interne Struktur von S wird gegenüber der Außenwelt verborgen.

42

Page 43: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History Konnektoren

Durch die Transition von Z nach S wird der Zustand aktiv, der aktiv war als S zuletzt verlassen wurde.

Falls S zum ersten Mal besucht wird, wird der default state aktiv.

43

Page 44: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History und Default Konnektoren

Die History und Default State Konnektoren können in verschiedenen Hierarchieebenen unterschiedlich eingesetzt werden.

44

Page 45: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History und Deep History

S

DC CO

ID OP

SL FA

Default states

Active states H

History connectors speichern Zustände die auf der gleichen Hierarchiestufe wie der History Konnektor angesiedelt sind.

45

Page 46: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History und Deep History

S

DC CO

ID OP

SL FA

Default states

Active states H

speichert OP

Welcher Zustand wird aktiv nach

SENSOR_DISCONNECTED, SENSOR_CONNECTED?

46

Page 47: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History und Deep History

S

DC CO

ID OP

SL FA

Default states

Active states H*

Deep history Konnektor H* speichert den aktuellen Basiszustand

*

47

Page 48: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

History und Deep History

S

DC CO

ID OP

SL FA

Default states

Active states H*

remembers OP, FA

What state is entered after sequence

SENSOR_DISCONNECTED, SENSOR_CONNECTED?

*

48

Page 49: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

AND Superzustände Alle (direkten) Unterzustände eines aktiven AND-

Superzustands sind aktiv; Beispiel:

49

Page 50: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

AND-Superzustände

Beispiel für aktive Zustände:

AND-Zustände können in normalen Automaten nur durch Produktzustände nachgebildet werden

➤ Strukturelle Information geht verloren ➤ Produktautomat ist viel größer

answ.

off on

l-m. k-m.

K.w. K.p.

Default states

Active states

L.w. L.p.

AND-super-state

50

Page 51: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Eintritt und Austritt aus AND-Superzuständen

Sowohl Line-monitoring als auch key-monitoring werden durch key-on and key-off betreten bzw. verlassen.

incl.

51

Page 52: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Variablen mit komplexen DatentypenProblem der klassischen Automaten:

Sowohl Kontrolle als auch Daten werden durch explizite Zustände dargestellt.Hier:

Getypte Variablen (z.B. integers, reals, strings, records) speichern Daten Der Zustand setzt sich zusammen aus den explizit dargestellten Kontrollzuständen

und den Variablenbelegungen Terminologie:

• graphisch dargestellter Kontrollzustand = Zustand• graphisch dargestellter Kontrollzustand + Variablenbelegung = Status

52

Page 53: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Beispiel: Alarmuhr

[Harel: “StateCharts: A visual formalism for complex systems”. Science of Computer Programming, 1987]

P1 = alarm1_enabled ∧ (alarm2_disabled ∨ T1≠T2)P2 = alarm2_enabled ∧ (alarm1_disabled ∨ T1≠T2)

P = alarm1_enabled ∧ alarm2_enabled ∧ T1=T2

53

Page 54: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Allgemeine Form der Kantenmarkierungen

Bedeutung: Transition kann genommen werden, wenn Ereignis stattgefunden

hat und Bedingung wahr ist. Wenn Transition genommen wird, dann wird die Aktion

ausgeführt.Bedingung:

Bedingung bezieht sich auf die VariablenAktion:

Zuweisung und Generierung von Ereignis

Beispiel: a & [x = 1023] / overflow; x:=0

Ereignis [Bedingung] / Aktion

54

Page 55: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Timer

Timer können direkt in StateCharts modelliert werden.

Spezielle Kanten beschreiben Timeouts.

Falls Ereignis a nicht innerhalb von 20 ms eintritt während das System im linken Zustand ist, dann tritt das

Timeout-Ereignis ein.55

Page 56: Einführung in Eingebettete Systeme - react.uni-saarland.de · Counter Control Lines 32 x 8 General Purpose Registrers ALU Status and Control I/O Lines EEPROM Data Bus 8-bit Data

Beispiel

56