Eingebettete Systeme - Halbleiterbauelemente.de

Werbung
Overview
Praktikum Embedded Systems
•
•
•
•
Wintersemester 2012/2013
Software development with Linux
Using Multitasking in Microcontrollers
URL: http://hlb-labor.de/lehre
Dipl. Informatiker Gregor Rebel
1
Praktikum Embedded Systems v1.1s
Chapter 1
Software development
under Linux OS
Dipl. Informatiker Gregor Rebel
2
Praktikum Embedded Systems v1.1s
Softwareentwicklung mit Linux
•
•
•
•
•
•
•
Was ist Linux?
Das Linux Dateisystem
Aufsetzen des Betriebssystems
Installation von Softwarepaketen
Werkzeuge
ToolChain_STM32
Versionskontrollsysteme
Dipl. Informatiker Gregor Rebel
3
Praktikum Embedded Systems v1.1s
Historie von Linux
1991 begann Linus Torvalds in Helsinki (Finnland) mit der Entwicklung einer
Terminal-Emulation, um unter anderem seinen eigenen Computer besser zu
verstehen. Mit der Zeit merkte er, dass sich das System immer mehr zu einem
Betriebssystem entwickelte und kündigte es daraufhin in der UsenetThemengruppe für das Betriebssystem Minix, comp.os.minix an.[3] Im
September desselben Jahres sollte das System dann auf einem Server den
Interessierten zur Verfügung gestellt werden. Da der damalige FTP-ServerAdministrator Ari Lemmke mit den von Torvalds vorgesehenen Namen Freax
oder Buggix nicht einverstanden war, stellte jener es stattdessen in einem
Verzeichnis mit dem Namen Linux zur Verfügung. Torvalds widersetzte sich
anfangs dieser Namensgebung, gab seinen Widerstand aber schnell auf, da er
nach eigener Aussage eingestehen musste, dass Linux einfach ein besserer
Name war.
Linux wurde zu dieser Zeit noch unter einer eigenen Lizenz veröffentlicht, welche
die kommerzielle Nutzung verbot. Schnell merkte Torvalds aber, dass diese
hinderlich war, und entschied sich dazu, allen Entwicklern deutlich mehr Freiraum
zu geben. Er und seine Mitautoren stellten daraufhin im Januar 1992 Linux unter
die GNU GPL[4]. Somit konnte man Linux in GNU integrieren und dies als das
erste freie Betriebssystem vertreiben. Dieser Schritt machte das System für eine
noch größere Zahl von Entwicklern interessanter, da es für diese die
Modifizierung und Verbreitung vereinfachte. 1996 kündigte Torvalds an, dass er
einen Pinguin als Maskottchen für Linux haben wolle, und schrieb einen
Wettbewerb aus, aus dem schließlich der populäre Tux hervorging.
Quelle: http://de.wikipedia.org/wiki/Linux#Historische_Entwicklung
Dipl. Informatiker Gregor Rebel
4
Praktikum Embedded Systems v1.1s
Was ist Linux?
•
•
•
•
•
•
•
•
Kernel
Das Programm init
Shells
Benutzerverwaltung
Grafische Benutzeroberfläche
Anwendungen
Distributionen
Community
Dipl. Informatiker Gregor Rebel
5
Praktikum Embedded Systems v1.1s
Der Linux Kernel
• Oberboss im System
• Lizensiert unter GNU General Public License 2
• Komponenten
– Speicherverwaltung
– Gerätetreiber
– Multitasking Scheduler
– Interprozesskommunikation
– Paketfilter (Firewall)
• Quelle: http://kernel.org/
Dipl. Informatiker Gregor Rebel
6
Praktikum Embedded Systems v1.1s
GNU General Public License 2
• Copyleft Prinzip
– Lizenznehmer:
Alle abgeleiteten Werke auch GPL 2
– Autor:
Darf Werk auch unter anderen Lizenzen
veröffentlichen
• Lizenznehmer muss Quelltext veröffentlichen!
• Mehrere Gerichtsverfahren gegen Firmen
z.B. 2006 Verfahren gegen D-Link
http://dejure.org/dienste/vernetzung/rechtsprechung?Text=2-6+O+224/06
Dipl. Informatiker Gregor Rebel
7
Praktikum Embedded Systems v1.1s
Das Programm /sbin/init
• Erstes Programm bei jedem Systemstart
• Startet alle weiteren Programme
• verwaltet Runlevel
0. System anhalten
1. Einzelbenutzer (meistens root)
2. Mehrbenutzer ohne Netzwerk
3. Mehrbenutzer mit textueller Oberfläche
4. Reserviert/ Unbenutzt
5. Mehrbenutzer mit grafischer Oberfläche
6. Rechner wird neugestartet
●
Befehle (als Superuser)
> runlevel
> init <N>
gibt aktuellen Runlevel aus
schaltet in Runlevel <N>
Dipl. Informatiker Gregor Rebel
8
Praktikum Embedded Systems v1.1s
Das Linux Dateisystem
•
•
•
•
•
•
Dateisystem ist Zentralbibliothek des Rechners
Linux Philosophie: Alles ist eine Datei!
Laufwerke immer in Ordner eingehängt
/ ist die Wurzel von allem
alles andere darunter
Zugriff auf Hardware über
virtuelle Dateien
• Zugriffsrechte/ Schutzbits
• Versteckte Dateien
Dipl. Informatiker Gregor Rebel
9
Praktikum Embedded Systems v1.1s
Laufwerke in Ordnern
• keine Laufwerkbuchstaben!
• Einhängen eines Laufwerks mittels mount
– Benötigt oft Superuser Rechte (sudo)
– sudo mount /dev/sda2 /media/home
• Anzeige der Mountpoints mittels df oder mount
> df -h
Dateisystem
rootfs
devtmpfs
tmpfs
/dev/sda1
/dev/sda2
Dipl. Informatiker Gregor Rebel
Größe Benut
20G 18G
984M 228K
990M 1,4M
20G 18G
54G 46G
Verf Ben%% Eingehängt auf
2,9G 86% /
984M 1% /dev
988M 1% /dev/shm
2,9G 86% /
5,2G 90% /home
10
Praktikum Embedded Systems v1.1s
/ ist die Wurzel
• Immer ein Laufwerk in / eingehängt => Bei Neuinstallation reicht eine /-Partition
• alle anderen Datenträger in Unterordnern
• /-Verzeichnis (Beispiel openSuSE)
/bin
Kommandos für root und normale Benutzer
/boot Kernel und für Bootvorgang benötigte Dateien
/dev
Virtuelles Dateisystem devFS enthält Dateien für Zugriff auf Hardwaregeräte
/etc
Systemkonfiguration (globale Einstellungen für alle Benutzer)
/home Jeder Benutzer für gewöhnlich ein eigenes Verzeichnis
/lib
Gemeinsam genutzte Programmbibliotheken
/media Dynamische Datenträger wie z.B. USB-Sticks, CDROM Laufwerke, ...
/opt
Optional installierte Software (z.B. QtSDK)
/proc Virtuelles Dateisystem mit Informationen zum laufenden System
/root Das Heimatverzeichnis des Superusers
/sbin Befehle zur Systemverwaltung (nur mit Superuser Rechten benutzbar)
/sys
Virtuelles Dateisystem sysFS eine erweiterte Darstellung von devFS (/dev)
/usr
Die meisten Anwendungen sind hier installiert
/var
Variable Informationsdateien und Datenbanken
Quelle: http://fibel.org/linux/lfo-0.6.0/node63.html
Dipl. Informatiker Gregor Rebel
11
Praktikum Embedded Systems v1.1s
Der Ordner /dev
• Hardwaregeräte in Form von Dateien sichtbar
• Vom Kernel mittels devtmpfs automatisch gefüllt
• Beispieleinträge
– Ein Eintrag pro Festplatte und pro Partition
• /dev/sda Festplatte 1
• /dev/sda1 Festplatte 1 Partition 1
– Serielle Schnittstellen
• /dev/ttyS0
• /dev/ttyUSB0
– /dev/null
Dipl. Informatiker Gregor Rebel
interne serielle Schnittstelle
erster USB-Seriell Wandler
schluckt alle eingehenden Zeichen
12
Praktikum Embedded Systems v1.1s
Zugriffsrechte/ Schutzbits
• Für jede Datei drei Gruppen
– (E)
– (G)
– (A)
Eigentümer
Gruppe
Andere
• Drei Arten von Schutzbits
r darf lesen
w darf schreiben
x darf ausführen
• Schutzbits in Textdarstellung EEEGGGAAA
> ls -l
-rw-r--r-- 1 gregor users
43 11. Okt 13:35 Quellen.txt
> chmod g+w Quellen.txt
> ls -l
-rw-rw-r-- 1 gregor users
43 11. Okt 13:35 Quellen.txt
Dipl. Informatiker Gregor Rebel
13
Praktikum Embedded Systems v1.1s
Versteckte Dateien
• Dateien/ Ordner die mit Punkt beginnen
– Normalerweise nicht angezeigt
– Enthalten oft Einstellungen
– Bei Kopier- /Verschiebeaktionen ignoriert
• Punkt Einträge anzeigen
> ls -al
-rw-r--r-- 1 gregor gregor 3973 2011-10-12 09:30 .springrc
drwx------ 2 gregor gregor
480 2011-08-24 04:18 .ssh
• Punkt Einträge kopieren
– cp -R \.* Ziel/
– Im Dateiimanager versteckte Dateien anzeigen
Dipl. Informatiker Gregor Rebel
14
Praktikum Embedded Systems v1.1s
Aufsetzen des Betriebssystems
• Wir machen uns die Hände schmutzig!
• Beispiel anhand Distribution Xubuntu
→ http://xubuntu.org/
• Installation in VirtualBox Emulator
→ http://virtualbox.org/
Dipl. Informatiker Gregor Rebel
15
Praktikum Embedded Systems v1.1s
Linux Distro herunterladen
• 32 Bit Version (x86) optimal für Virtualisierung
– weniger Speicherbedarf
– einfacher zu emulieren
• 64 Bit Version (x64) optimal bei nativer Installation
Dipl. Informatiker Gregor Rebel
16
Praktikum Embedded Systems v1.1s
Virtuelle Maschine erstellen
Dipl. Informatiker Gregor Rebel
17
Praktikum Embedded Systems v1.1s
Namen vergeben
Dipl. Informatiker Gregor Rebel
18
Praktikum Embedded Systems v1.1s
1GB RAM mindestens
Dipl. Informatiker Gregor Rebel
19
Praktikum Embedded Systems v1.1s
Virtual Disk Image
Dipl. Informatiker Gregor Rebel
20
Praktikum Embedded Systems v1.1s
Bootfestplatte erzeugen
Dipl. Informatiker Gregor Rebel
21
Praktikum Embedded Systems v1.1s
Feste Größe ist schneller
Dipl. Informatiker Gregor Rebel
22
Praktikum Embedded Systems v1.1s
20GB reichen zunächst
Dipl. Informatiker Gregor Rebel
23
Praktikum Embedded Systems v1.1s
Zeit für einen Kaffee
Dipl. Informatiker Gregor Rebel
24
Praktikum Embedded Systems v1.1s
ISO Image als virtuelle CD
Dipl. Informatiker Gregor Rebel
25
Praktikum Embedded Systems v1.1s
Start von virtueller CD
Dipl. Informatiker Gregor Rebel
26
Praktikum Embedded Systems v1.1s
Maus befreien: Strg Rechts
Dipl. Informatiker Gregor Rebel
27
Praktikum Embedded Systems v1.1s
Inklusive Online Updates
Dipl. Informatiker Gregor Rebel
28
Praktikum Embedded Systems v1.1s
Manuelle Partitionierung
Dipl. Informatiker Gregor Rebel
29
Praktikum Embedded Systems v1.1s
Neue Partitionstabelle
Dipl. Informatiker Gregor Rebel
30
Praktikum Embedded Systems v1.1s
Partition hinzufügen
Dipl. Informatiker Gregor Rebel
31
Praktikum Embedded Systems v1.1s
Eine / Partition ist Pflicht!
Dipl. Informatiker Gregor Rebel
32
Praktikum Embedded Systems v1.1s
Jetzt installieren
Dipl. Informatiker Gregor Rebel
33
Praktikum Embedded Systems v1.1s
Ohne Swap fortfahren
• Auslagern auf virtuelle Festplatte langsam!
• Besser mehr Speicher für Maschine
Dipl. Informatiker Gregor Rebel
34
Praktikum Embedded Systems v1.1s
Anmeldedaten
Dipl. Informatiker Gregor Rebel
35
Praktikum Embedded Systems v1.1s
Ein gutes Passwort wählen
•
•
•
•
GROSS- und kleinbuchstaben
Ziffern
Sonderzeichen , . ! „ § $ % & / ( ) = + Einen leicht zu behaltenen Satz wählen:
Mann was der wieder für einen Quatsch sagt
• Sonderregeln (ein → 1, sagen → ;)
→ Mwdwf1Q;
• Insgesamt 8 Zeichen!
Dipl. Informatiker Gregor Rebel
36
Praktikum Embedded Systems v1.1s
Neustart bestätigen
Dipl. Informatiker Gregor Rebel
37
Praktikum Embedded Systems v1.1s
Ein aufgeräumter Desktop
Dipl. Informatiker Gregor Rebel
38
Praktikum Embedded Systems v1.1s
Updates suchen
Dipl. Informatiker Gregor Rebel
39
Praktikum Embedded Systems v1.1s
Updates installieren
Dipl. Informatiker Gregor Rebel
40
Praktikum Embedded Systems v1.1s
Anwendungen installieren
• Änderungen am System
– nur als Superuser (root)
• Verschiedene Optionen
– Paketmanager
– Installationsskripte
– einzelne Pakete
– Quelltext
Dipl. Informatiker Gregor Rebel
41
Praktikum Embedded Systems v1.1s
Der Superuser
•
•
•
•
•
Ich bin /root, ich darf das!
Eigenes Homeverzeichnis /root
Darf jede Datei verändern
Nur zur Systemkonfiguration
Superuser werden
> sudo su
• Etwas als Superuser ausführen
> sudo reboot -n
Dipl. Informatiker Gregor Rebel
42
Praktikum Embedded Systems v1.1s
Installation per Paketmanager
• Königlich einfache Bedienung
• Große Auswahl vorkonfigurierter
Softwarepakete
• Abhängigkeiten automatisch aufgelöst
• Automatische Updates
• openSuSE:
yast, kpackagekit
• Debian:
apt-get, aptitude, synaptic, kpackagekit
Dipl. Informatiker Gregor Rebel
43
Praktikum Embedded Systems v1.1s
Installationsskripte
•
•
•
•
Ähnlich Installation unter Windows OS
Ausführbares Programm von Webseite
Oft Dateinamensendung .run
Datei muss ausführbar gemacht werden
– chmod +x DATEI
– Eigenschaften/ Dateizugriff im Dateimanager
• Oft als Superuser starten
– sudo ./DATEI
• Beispiele
– QtCreator
– Grafikkartentreiber (NVIDA/ AMD)
Dipl. Informatiker Gregor Rebel
44
Praktikum Embedded Systems v1.1s
Installation einzelner Pakete
• Für Fortgeschrittene User
• Per Suchmaschine suchen
• Muss zur Distribution passen
– openSuSE, RedHat:
sudo rpm –install FILE.rpm
– Debian: .deb Format
sudo gdebi FILE.deb
• Keine automatischen Updates!
Dipl. Informatiker Gregor Rebel
45
Praktikum Embedded Systems v1.1s
Installation aus Quellen
• Pain in the Ass
• Suchen der „richtigen“ Version
– neueste Quellen oft instabil
– Installationsanleitungen oft veraltet
• Neu kompilieren
– COMPILE.txt/ INSTALL.txt lesen!
– Foren durchsuchen
• Abhängigkeiten von Hand lösen
– Fehlermeldungen lesen
– Fehlende Software installieren
• Installationsscript erstellen (Fortgeschritten)
Dipl. Informatiker Gregor Rebel
46
Praktikum Embedded Systems v1.1s
Gasterweiterungen Installieren
• Ermöglichen
– Gemeinsame Ordner
– Geteilter Zwischenspeicher
– 3D Grafikbeschleunigung
– Größenänderungen des Fensters
Dipl. Informatiker Gregor Rebel
47
Praktikum Embedded Systems v1.1s
Auf einigen Distributionen:
Installationsskript ausführen
Dipl. Informatiker Gregor Rebel
48
Praktikum Embedded Systems v1.1s
Alternativ:
Shell im Ordner starten
Dipl. Informatiker Gregor Rebel
49
Praktikum Embedded Systems v1.1s
Terminal starten
Dipl. Informatiker Gregor Rebel
50
Praktikum Embedded Systems v1.1s
autorun.sh ausführen
Dipl. Informatiker Gregor Rebel
51
Praktikum Embedded Systems v1.1s
Gasterweiterungen installiert
Dipl. Informatiker Gregor Rebel
52
Praktikum Embedded Systems v1.1s
Rechner herunterfahren
Dipl. Informatiker Gregor Rebel
53
Praktikum Embedded Systems v1.1s
Extension-Pack herunterladen
• Benötigt um Programmer per USB2 zu
erreichen
Dipl. Informatiker Gregor Rebel
54
Praktikum Embedded Systems v1.1s
Extension Pack installieren
• Doppelklick auf Datei öffnet VirtualBox
• Virtuelle Maschinen vorher herunterfahren
Dipl. Informatiker Gregor Rebel
55
Praktikum Embedded Systems v1.1s
Fertig !
Dipl. Informatiker Gregor Rebel
56
Praktikum Embedded Systems v1.1s
Übung: Installieren
• Linux Distribution eigener Wahl installieren
– openSuSE, Ubuntu/ Kubuntu, Debian, …
– in virtueller Maschine oder als Dualboot System
• Darauf folgende Software installieren:
– QtCreator (http://qt.nokia.com/)
– git
– yakuake
– STM32_ToolChain
(http://hlb-labor.de/cortexm3)
Dipl. Informatiker Gregor Rebel
57
Praktikum Embedded Systems v1.1s
Console
• Textuelles Eingabeinterface
• Erste Console 1991 vom
Kernel bereitgestellt
• Ursprünglich Ausgabe
direkt in Grafikkartenpuffer
• Terminal ist Entsprechung
unter grafischer Oberfläche
• Immer noch erreichbar per Strg+F1, ...
Dipl. Informatiker Gregor Rebel
58
Praktikum Embedded Systems v1.1s
Shell
•
•
•
•
Jede Konsole/ Terminal startet eine Shell
Shell = Schale die sich um das Betriebsystem legt
Interpretiert menschliche Texteingaben
Es gibt verschiedene Shells
– bash, csh, tcsh, … (unterschiedliche Kommandosyntax!)
• Grundfunktionen
– Starten von Programmen
– Ausführen von Skripten
– Vollständige Programmiersprache
– Umsetzen von Pipe Anordnungen
– Bereitstellung von Umgebungsvariablen
– Umleitung von Textausgaben
Dipl. Informatiker Gregor Rebel
59
Praktikum Embedded Systems v1.1s
Shell
• Jede Konsole startet eine Shell
• Interpretiert menschliche Texteingaben
• Es gibt verschiedene Shells
– bash, csh, tcsh, … (unterschiedliche Kommandosyntax!)
• Grundfunktionen
– Starten von Programmen
– Ausführen von Skripten
– Vollständige Programmiersprache
– Umsetzen von Pipe Anordnungen
– Bereitstellung von Umgebungsvariablen
– Umleitung von Textausgaben
Dipl. Informatiker Gregor Rebel
60
Praktikum Embedded Systems v1.1s
Programme starten
• Goldene Regel: Dateien immer tabben!
• Ausführbare Datei finden
– absoluter/ relativer Pfad geht immer
> /bin/less
– Suche im Suchpfad
• Variable $PATH zu durchsuchende Verzeichnisse
> echo $PATH
./:/home/gregor/bin:/bin:/opt/QtSDK/QtCreator/bin/:...
• ./ ist aktuelles Verzeichnis
Dipl. Informatiker Gregor Rebel
61
Praktikum Embedded Systems v1.1s
Ausführen von Skripten
• Synonym: Batchdatei
• Erste Zeile definiert Interpreter
#!/bin/bash
#!/usr/bin/perl
…
• x Bit gesetzt: chmod +x DATEI
• Keine Dateinamensuffixe unter Linux!
• Oft aktuelles Verzeichnis nicht im Pfad
Dipl. Informatiker Gregor Rebel
62
Praktikum Embedded Systems v1.1s
Shell Programmierung
• Variablen
VARIABLE=“WERT“
echo “$VARIABLE“
• Kontrollstrukturen
if [ BEDINGUNG ]; then
...
fi
for A in “a b c ...“; do
...
done
• Kein goto in bash!
Dipl. Informatiker Gregor Rebel
63
Praktikum Embedded Systems v1.1s
Pipeanordnungen
• Idee:
– Kleine Befehle als Werkzeugkasten
– Kombination einfacher Befehle ist mächtig
• Pipe ist Verbindung zweier Befehle
• Pipesymbol ist senkrechter Strich |
• A|B
– Textausgabe von A ist Texteingabe für B
– cat hallo.txt | grep Welt
• Lange Pipeketten möglich
Dipl. Informatiker Gregor Rebel
64
Praktikum Embedded Systems v1.1s
Umgebungsvariablen
•
•
•
•
•
Jede Shell erbt Variablen von Umgebung
Neue Variablen in Elternshell unbekannt
Variablen anzeigen: export (Bash)
Nur exportierte Variablen weiter vererbt
Variablen sterben mit ihrer Shell
→ permanent durch Eintrag in Startup Skript
• Zuweisung: VARIABLE=“Wert“
Dipl. Informatiker Gregor Rebel
65
Praktikum Embedded Systems v1.1s
Textausgaben umleiten
• Jedes Programm
– 1 Eingabestrom: STDIN
– 2 Ausgabeströme: STDOUT, STDERR
• Eingabe aus Datei: BEFEHL <DATEI
• Ausgabe in Datei:
– Zieldatei überschreiben:
BEFEHL >DATEI1 2>DATEI2
– An Zieldatei anhängen:
BEFEHL >>DATEI1 2>>DATEI2
• STDERR nach STDOUT umleiten: 2>&1
Dipl. Informatiker Gregor Rebel
66
Praktikum Embedded Systems v1.1s
Shell Startup Skript
• Bei Start/ Login automatisch ausgeführt
– /etc/bash.bashrc
– ~/.bashrc
– ~/.profile
– ~/.login
• Beispiele:
– Suchpfad zu qtcreator
> echo 'export PATH=“/opt/QtSDK/QtCreator/bin/:$PATH'
>>~/.bashrc
– Permanenter alias
> echo 'alias ll=“ls -lh“' >>~/.bashrc
Dipl. Informatiker Gregor Rebel
67
Praktikum Embedded Systems v1.1s
Werkzeuge
•
•
•
•
•
man
cat
less
find
grep
Dipl. Informatiker Gregor Rebel
gcc/ g++
●
make
●
openocd
●
gdb
●
68
Praktikum Embedded Systems v1.1s
Befehl man (1)
•
•
•
•
•
Textbasiertes Anleitungssystem
Ausführliche Beschreibungen
Praktisch jeder Befehl beschrieben
Programmierhandbuch
Tastenzuordnungen
– Pfeiltasten Blättern
– / nach Text suchen
–q
Beenden
• Alternative: info
Dipl. Informatiker Gregor Rebel
69
Praktikum Embedded Systems v1.1s
Befehl man (2)
• Verschiedene Ebenen
> man EBENE SCHLÜSSELWORT
1
2
3
4
5
6
7
8
9
Ausführbare Programme oder Shellbefehle
Systemaufrufe (Kernelfunktionen)
Bibliotheksaufrufe (Funktionen in System-Bibliotheken)
Spezielle Dateien (gewöhnlich in /dev
Dateiformate und Konventionen, z. B. /etc/passwd
Spiele
Makropakete und Konventionen, z. B. man(7), groff(7)
Systemadministrationsbefehle (in der Regel nur für root)
Kernelroutinen [Nicht Standard]
> man 1 open
> man 2 open
Dipl. Informatiker Gregor Rebel
70
Praktikum Embedded Systems v1.1s
Befehl cat
• Dateiinhalt ausgeben
• Oft Start einer Pipe
> cat DATEI | grep FOO | less
• Here-Skript in Shell-/ Perl-Skript
cat <<“END_OF_TEXT“
Hello
World
END_OF_TEXT
Dipl. Informatiker Gregor Rebel
71
Praktikum Embedded Systems v1.1s
Befehl less
•
•
•
•
Zeigt Dateiinhalt an
Interaktives bidirektionales Blättern
Suchfunktion: /
Beenden: q
Dipl. Informatiker Gregor Rebel
72
Praktikum Embedded Systems v1.1s
Befehl find
• Suchen von Dateien
> find STARTPFAD -name MUSTER
• Viele Optionen zur Suchkonfiguration
– Verknüpfung mittels -and / -or
• Expression für jede gefundene Datei
– Default: -print
– Externes Programm: -exec BEFEHL {} \;
• Beispiele:
> find /home -name “\.*“ | less
> find ./ -name “*.txt“ -exec less {} \;
• → man find
Dipl. Informatiker Gregor Rebel
73
Praktikum Embedded Systems v1.1s
Befehl grep
• Universeller Suchbefehl
– Sucht im Eingabestrom nach Muster
– Gibt alle Zeilen aus, die Muster enthalten
• Syntax: grep MUSTER DATEI
> grep Hallo HalloWelt.txt
> cat HalloWelt.txt | grep Hallo
• Kombination mit find
> find ./ -name “*.h“ | grep -n MUSTER
• → man grep
Dipl. Informatiker Gregor Rebel
74
Praktikum Embedded Systems v1.1s
Befehl gcc/ g++
• Universeller Übersetzer
– Preprozessor
– C/ C++ Compiler
– Linker
– Assembler
• Übersetzen: gcc -c DATEI.c
• Linken: gcc DATEI1.o DATEI2.o -o OUT
• → man gcc (10575 Zeilen)
Dipl. Informatiker Gregor Rebel
75
Praktikum Embedded Systems v1.1s
Befehl make
• Führt makefile aus
• Umsetzung von Abhängigkeiten
• Einfache Regeln
– A.x mittels BEFEHL aus A.y erstellt
– Gegeben A.y, B.y, C.y, …
– Für Ziel Z: Erstelle A.x, B.x, C.x, …
• Ausnutzung Parallelität: make -jN (N = 2x CPU-Kerne)
• Für C wesentliche Dateiendungen
– .s
– .c
– .h
– .o
Assembler Quelltext
C Quelltext
C Headerdatei
Objektdatei (aus .c oder .s erstellt)
Dipl. Informatiker Gregor Rebel
76
Praktikum Embedded Systems v1.1s
makefile
•
•
•
•
Steuerdatei für make
Regeln zur Übersetzung
Konfigurationsoptionen
Definition von
– Übersetzungsziele (ZIEL:)
– Quellverzeichnisse
– Zu erzeugender Objektdateien
– Name der Ausgabedatei
– Übersetzungsoptionen
Dipl. Informatiker Gregor Rebel
77
Praktikum Embedded Systems v1.1s
GNU Make Quellen
• Official Manual
→ http://www.gnu.org/software/make/manual/make.pdf
• German Tutorial
→ http://www.ijon.de/comp/tutorials/makefile.html
Dipl. Informatiker Gregor Rebel
78
Praktikum Embedded Systems v1.1s
ToolChain_STM32
•
•
•
•
•
•
Collection of Scripts
Automatically downloads aprox. 500MB
Sources patched and compiled
Compiler binaries
Programmer/ Debugger
uC Libraries
Dipl. Informatiker Gregor Rebel
79
Praktikum Embedded Systems v1.1s
Features ToolChain
•
•
•
•
•
•
•
•
Downloaded + installed automatically
FreeRTOS Multitasking Scheduler
STM-StdPeripheralsLibrary
Compiler Suite ARM NONE EABI
OpenOCD for In Circuit Programming/ Debugging
additional libraries (math, lcd, lan, …)
configuration files for different proto boards
fexible configuration
→ same code can run on different boards
• out of the box running code examples
Dipl. Informatiker Gregor Rebel
80
Praktikum Embedded Systems v1.1s
Installing ToolChain
• Requirements
– Computer running Linux OS
(openSuSE, Ubuntu, …)
– root access (superuser password) on your computer
– Fast Internet Connection (download > 500MB)
• Issue in Shell (aka Terminal or Console)
> wget http://hlb-labor.de/cortexm3/install_ToolChain_STM32.sh
> chmod +x install_ToolChain_STM32.sh
> ./install_ToolChain_STM32.sh
• The ToolChain will be installed into
~/Source/ToolChain_STM32
(~ means your homefolder)
Dipl. Informatiker Gregor Rebel
81
Praktikum Embedded Systems v1.1s
Creating a new project
Start the adventure!
1. cd ToolChain_STM32
2. ./createNewProject.pl TEST FOO
3. cd ../TEST
4. configure activate_project.sh
5. configure Programmer
6. ./compile.sh
7. Debug
Dipl. Informatiker Gregor Rebel
82
Praktikum Embedded Systems v1.1s
Skript createNewProject.pl
•
•
•
•
•
./createNewProject PROJECT AUTHOR
copies template project
Creates new folder in ../
Fills in name of project and author
Created folder may be moved anywhere
Dipl. Informatiker Gregor Rebel
83
Praktikum Embedded Systems v1.1s
Template Project
• Complete, standalone framework
• Should run on most Cortex M3 boards
• Support for QtSDK Editor
> ./qtcreator.sh
• Compile + program on microcontroller
> ./compile.sh
• Missing ToolChain downloaded + installed
Dipl. Informatiker Gregor Rebel
84
Praktikum Embedded Systems v1.1s
Makefile
• Parsed for every compile.sh run
• Defines how to make things
– Which object files to compile
– Where to find .c/ .h/ .s -files
– Which constants to define
– Which files to include
– Dependencies between build targets
Dipl. Informatiker Gregor Rebel
85
Praktikum Embedded Systems v1.1s
Configure Makefile
(1)
(2)
(3)
(4)
(5)
(6)
(7)
(8)
(9)
basic constants
where to find h-files
where to find c-files
where to find s-files
automagically collect files
define uController to use
minimize code size
phony build targets
add some general files
Dipl. Informatiker Gregor Rebel
86
Praktikum Embedded Systems v1.1s
Makefile - basic constants
• Compileroptimizations (-O)
– O0
– O1
– O3
– Os
no Optimizations (for Debugging)
simple Optimizations
maximal Optimizations
creates smallest Binary
> man gcc
Dipl. Informatiker Gregor Rebel
87
Praktikum Embedded Systems v1.1s
Makefile
where to find h-files
•
•
•
•
•
•
Header files benötigt für #include
gcc Parameter -I definiert einen Includepfad
\ am Zeilenende: mit nächster Zeile fortfahren
$(INCLUDE_DIRS) enhält viele -I Einträge
$(INCLUDE_DIRS) direkt an gcc gegeben
Headerfile finden:
gcc durchläuft Liste der -I Einträge
Dipl. Informatiker Gregor Rebel
88
Praktikum Embedded Systems v1.1s
Makefile
where to find c-files
•
•
•
•
C-Sources compiled into separate .o files
vpath.%c stores list of folders
vpath.%c only used by make
Whenever FOO.o should be created
– Make searches all folders in vpath.%c
– The first found file FOO.c will be compiled
Dipl. Informatiker Gregor Rebel
89
Praktikum Embedded Systems v1.1s
Makefile
where to find s-files
•
•
•
•
.s files contain assembler source code
Often used for Bootloader
vpath %.s stores relative paths of folders
Whenever make should compile a FOO.s
– All folders in vpath %.s are searched
– First found file FOO.s is being compiled
– FOO.s → FOO.o
Dipl. Informatiker Gregor Rebel
90
Praktikum Embedded Systems v1.1s
Makefile – collect files
• Files with certain name suffix
– .c
– .cc
– .cpp
– .s
• Only files in same folder as makefile
• Every found file compiled into corresponding .o file
→ Not required to manually add these files to
makefile
Dipl. Informatiker Gregor Rebel
91
Praktikum Embedded Systems v1.1s
Makefile - define uController
• STM32 uCs are separated into classes
• Classes differ by
– Required bootcode file
– Amount of RAM
– Amount of Flash Memory
– Functional units (GPIOs, Timer, UARTS, ..)
Dipl. Informatiker Gregor Rebel
92
Praktikum Embedded Systems v1.1s
Makefile - minimize code size
• Optimize during Compile & Link
– GCC can write Funktiones/ Data into separate Sektions
of the objektfile
– Unreferenced sections can be skipped during link stage
– Important: All used interrupt handlers have to be
marked as
__attribute__((used))
→ arm-none-eabi-gcc --help=optimizers | less
→ man gcc
Dipl. Informatiker Gregor Rebel
93
Praktikum Embedded Systems v1.1s
Makefile - phony build targets
• .PHONY: clean all
• Lists all Targets which are no files
• Avoids ambiguity between build targets
and files of same name
• Example: „all“, „clean“
Dipl. Informatiker Gregor Rebel
94
Praktikum Embedded Systems v1.1s
Makefile - add general files
• $(MAIN_OBJS) stores list of all object files
to create
• Every mandatory .c file requires one .o
entry in $(MAIN_OBJS)!
• Exceptions: → page Collect Files
• object files can be added to at any stage
of makefile
– E.g.: MAIN_OBJS += foo.o
Dipl. Informatiker Gregor Rebel
95
Praktikum Embedded Systems v1.1s
Makefile Extensions
• Subfolders in project folder
– extensions/
• Makefile fragments provided by ToolChain
• No user created/ project related files here!
– extensions.local/
• User created Makefile fragments go here
– extensions.active/
• Symbolic links to extensions/ or extensions.local/
• all makefile.* files here are included automatically
Dipl. Informatiker Gregor Rebel
96
Praktikum Embedded Systems v1.1s
Folder extensions/
• Two types of files
– makefile.NNN_NAME
• Makefile fragment in rank NNN
• provides some extra settings when activated
– activate.NNN_NAME.sh
• scripts that activate makefile fragments
• will create one or more links in extensions.active/
• some scripts remove interferring links automatically
(e.g. activate.100_board_XXX.sh)
• Added to search PATH
Just cd to your project folder and issue:
> activate.600_example_threading_queues.sh
Dipl. Informatiker Gregor Rebel
97
Praktikum Embedded Systems v1.1s
Extensions in different Ranks
• Extensions, Makefiles, Activate-Scripts
• Filename shows three digit rank number
• High ranked extensions depend on
extensions with lower rank number
• Lowest rank: Basic compiler libraries
• Highest rank: Complex examples
→ ToolChain/ReadMe_Ranks.txt
• Rule: No rank depends on higher ranks!
Dipl. Informatiker Gregor Rebel
98
050
system libraries
100
basic definitions
200
basic libraries
300
public libraries
400
ToolChain libraries
600
simple examples
700
complex examples
999
external tools
Praktikum Embedded Systems v1.1s
Minimal SetUp
> ./clean.sh
> activate.600_example_leds.sh
> activate.100_board_olimex_h107.sh
Creates out of the egg:
makefile.100_board_olimex_h107
makefile.200_cpu_stm32f10x
makefile.250_CPAL_STM32F10x_StdPeriph_Driver
makefile.500_ttc_interrupt
makefile.500_ttc_memory
makefile.600_example_leds
Dipl. Informatiker Gregor Rebel
99
Praktikum Embedded Systems v1.1s
Extension
050_compiler_sourcery_g++
•
•
•
•
•
Basic Libraries provided by Compiler
Binary compiler suite from CodeSourcery
No source code available
Outdated (not updated by CodeSourcery)
CodeSourcery provides updated
commercial compiler suite
> activate.050_compiler_sourcery_g++.sh
Dipl. Informatiker Gregor Rebel
100
Praktikum Embedded Systems v1.1s
Extension
100_board_XXX
• Board description
– CPU type
– Memory Size and Types
– Pin definitions
• One board description is mandatory
> activate.100_board_olimex_p107.sh
Dipl. Informatiker Gregor Rebel
101
Praktikum Embedded Systems v1.1s
Extension
200_std_peripherals_library
• Common library to access Peripherals on
CortexM3 chips
• Mandatory for all example projects
• Required by FreeRTOS
> activate.200_std_peripherals_library.sh
Dipl. Informatiker Gregor Rebel
102
Praktikum Embedded Systems v1.1s
Extension
300_scheduler_free_rtos
•
•
•
•
Multitasking scheduler
Provides inter task communication
Mandatory for all example projects
Requires exactly one activation of
– 300_scheduler_free_rtos_heap1
– 300_scheduler_free_rtos_heap2
– 300_scheduler_free_rtos_heap3
> activate.300_scheduler_free_rtos.sh
Dipl. Informatiker Gregor Rebel
103
Praktikum Embedded Systems v1.1s
Extension
400_debug_registers
• Provides global variables for certain registers
• Each variable is a pointer to detailed struct
• Allows
– easy reading of individual bit in hardware
– named access to bitfields in hardware registers
> activate.400_debug_registers.sh
Dipl. Informatiker Gregor Rebel
104
Praktikum Embedded Systems v1.1s
Extension
400_lcd_320x240_olimex
• driver for LCD panel on prototype board
STM32-LCD from Olimex
• Basic graphical + text features
• Makes use of demo code from Olimex
> activate.400_lcd_320x240_olimex.sh
Dipl. Informatiker Gregor Rebel
105
Praktikum Embedded Systems v1.1s
Extension
400_fix_point_lib
• Library for Fixed Point mathematic operations
• CortexM3 CPUs have no Floating Point Unit
• Fixed Point Math much faster than Floating
Point Math in Software
• Only low loss in accuracy
• Need to choose range according to application!
→ Documentation/Math/
> activate.400_fix_point_lib.sh
Dipl. Informatiker Gregor Rebel
106
Praktikum Embedded Systems v1.1s
Extension
400_network_uip_ste101p
•
•
•
•
•
Basic TCP/IP network Stack uIP
Includes demo software from Olimex
Requires Protoboard Olimex P107
Only one socket at a time
Packet resend to be implemented by
application
→ Documentation/Network/
> activate.400_network_uip_ste101p.sh
Dipl. Informatiker Gregor Rebel
107
Praktikum Embedded Systems v1.1s
Extension
400_support_adc
• Simple functions for ADC configuration
– Configuration of single ADC-channels
– DMA enabled data transfer into variables
– single channel, blocking readout
> activate.400_support_adc.sh
Dipl. Informatiker Gregor Rebel
108
Praktikum Embedded Systems v1.1s
Extension
600_example_leds
• Simple Multitasking example
• Two tasks communicate via shared Variables
– taskBrightness
periodically changes brightnes value
– taskControlLed
creates PWM on pin PB_LED1 to show current
brightness
> activate.600_example_leds.sh
Dipl. Informatiker Gregor Rebel
109
Praktikum Embedded Systems v1.1s
Extension
600_example_lcd
• Basic example how to use LCD panel on
prototype board STM32-LCD from Olimex
• Shown Features
– initialize panel
– set background/ foreground color
– print single characters at certain positions
– printf() at certain/ current positions
> activate.600_example_lcd.sh
Dipl. Informatiker Gregor Rebel
110
Praktikum Embedded Systems v1.1s
Extension
700_example_threading_queues_led
• Example showing
– several tasks communicating via queues
– visualization via two LEDs
– can run on any board that defines
PB_LED1, PB_LED2
> activate.700_example_threading_queues_led.sh
Dipl. Informatiker Gregor Rebel
111
Praktikum Embedded Systems v1.1s
Extension
700_example_threading_queues_lcd
• Example showing
– several tasks communicating via queues
– visualization on LCD panel
• producer task: current value
• consumer task: current value
• Queue size
> activate.700_example_threading_queues_lcd.sh
Dipl. Informatiker Gregor Rebel
112
Praktikum Embedded Systems v1.1s
Extension
700_example_threading_semaphores_led
• Example showing
– several tasks communicate via semaphores
– several producer tasks give varying amount of tags to a
semaphore
– several consumer tasks take one tag each and do something for
a while
• Visualization
– via two LEDs on PB_LED1, PB_LED2
> activate.700_example_threading_semaphores_led.sh
Dipl. Informatiker Gregor Rebel
113
Praktikum Embedded Systems v1.1s
Extension
700_example_threading_semaphores_lcd
• Example showing
– several tasks communicate via semaphores
– several producer tasks give varying amount of tags to a semaphore
– several consumer tasks take one tag each and do something for a
while
• Visualization
– on LCD panel
– two producer tasks
– six consumer tasks
> activate.700_example_threading_semaphores_led.sh
Dipl. Informatiker Gregor Rebel
114
Praktikum Embedded Systems v1.1s
Adapting linker script
• Used when linking all objject files into final binary
• Defines memory adresses and assignments in the uC
• Memory divided into areas with attributes
– Name
– Access (r ead, w rite, x ecutable)
– Start
– Size
• Sections defined with attributes
– Name
– Alignment (1, 2, 4, 8 Bytes)
– Assigned memory area
• . is current address counter
• File stm32.ld contains linker script
Dipl. Informatiker Gregor Rebel
115
Praktikum Embedded Systems v1.1s
Configure Programmer
• Programmer is a hardware device
– JTAG-Connection between Desktop PC/ Laptop and uC
– flashes binary file onto uC
– Allows Incircuit Debugging
– Must be supported by OpenOCD
– Example: ARM-USB-TINY-H from Olimex Corp.
• used programmer defined in
_/interface.cfg
• List of supported programmers to be found in
ToolChain/OpenOCD/share/openocd/scripts/interface/
Dipl. Informatiker Gregor Rebel
116
Praktikum Embedded Systems v1.1s
compile.sh
•
•
•
•
•
•
•
./compile.sh
downloads + installs missing ToolChain
creates symbolic links to installed libraries
may activate basic example framework
compies current project
updates QtCreator/PROJEKT.creator
flashes binary onto uC
Dipl. Informatiker Gregor Rebel
117
Praktikum Embedded Systems v1.1s
Debugging
• In which stage does the bug occur
– During compilation (gcc -c)
• Syntaxerror
• Includefile not found
→ .c-Datei
→ makefile #02
– During linking after compilation
• Objektfile missing
• Objektfile from wrong path
→ makefile #09
→ makefile #03
– During runtime
• Wrong uC chosen
• Too few/ much RAM
• Stackoverflow
Dipl. Informatiker Gregor Rebel
→ activate correct board
→ stm32.ld, FreeRTOSConfig.h
→ xTaskCreate() call
118
Praktikum Embedded Systems v1.1s
Versionskontrollsysteme
• Erfassung von Änderungen an Dateien
– Protokollierung von Änderungen
– Wiederherstellung alter Zustände
– Archivierung der gesamten Historie
– Koordinierung des gemeinsamen Zugriffs
– Verzweigung in mehrere Branches
Dipl. Informatiker Gregor Rebel
119
Praktikum Embedded Systems v1.1s
Funktionsweisen
• Lokale Versionsverwaltung
• Zentrale Versionsverwaltung
• Verteilte Versionsverwaltung
Dipl. Informatiker Gregor Rebel
120
Praktikum Embedded Systems v1.1s
Lokale Versionsverwaltung
•
•
•
•
Oft nur einzelne Datei versioniert
Änderungen in Datei selbst gespeichert
Werkzeuge: SCCS, RCS
Beispiele
– Technische Zeichnung mit Änderungsindex
Dipl. Informatiker Gregor Rebel
121
Praktikum Embedded Systems v1.1s
Zentrale Versionsverwaltung
• Als Client-Server System aufgebaut
• Ein zentraler Server speichert komplette Historie
– optional: Read-Only Zugriff für Fremduser
– optional: Web-Interface
• Entwickler jeweils nur eine Version
• Zugriff auch per Netzwerk möglich
• Werkzeuge
– cvs, svn (OpenSource)
– AlienBrain, Perforce, Team Foundation, ...
Dipl. Informatiker Gregor Rebel
122
Praktikum Embedded Systems v1.1s
Verteilte Versionsverwaltung
• Jeder Entwickler hat komplette Historie
• Zusätzlich zentraler Server möglich
– Änderungen zwischen Entwicklern austauschen
– optional: Read-Only Zugriff für Fremduser
– optional: Web-Interface
• Werkzeuge
– git, GNU arch, Mercurial,
Bazaar (OpenSource)
– BitKeeper
Dipl. Informatiker Gregor Rebel
123
Praktikum Embedded Systems v1.1s
Wie Git das?
•
•
•
•
•
•
•
•
Übersicht
Installation
Dezentrale Verwaltung
Verzweigte Entwicklung
Datenaustausch
Markierte Revisionen
Interoperabilität
Web-Interface
Dipl. Informatiker Gregor Rebel
124
Praktikum Embedded Systems v1.1s
Git - Übersicht
•
•
•
•
Freie Software
Verteilte Versionsverwaltung von Dateien
Entwickelt zur Verwaltung des Linux Kernel Quelltextes
Betriebsysteme
– Linux, Solaris, Mac OS X, FreeBSD, …
– Windows (Cygwin, Msysgit, TortoiseGit-Shell)
• Aktuell eingesetzt in vielen OpenSource Projekten
– Amarok, Android, BusyBox, Debian, DragonFly BSD, Eclipse,
Erlang, Fedora, Git, Gnome, KDE, Qt, Ruby, …
• Kurzanleitung:
• Webseite:
Dipl. Informatiker Gregor Rebel
http://hlb-labor.de/git/
http://git-scm.com/
125
Praktikum Embedded Systems v1.1s
Git - Installation
• Debian/ Ubuntu
– sudo apt-get install git
• openSuSE
– sudo zypper install git
• Windows
– http://de.wikipedia.org/wiki/Cygwin
– http://de.wikipedia.org/wiki/TortoiseGit
Dipl. Informatiker Gregor Rebel
126
Praktikum Embedded Systems v1.1s
Git - Dezentrale Verwaltung
• Jeder Benutzer hat eigenes Repository
> git pull
– Holt neuesten Stand vom Zentralrepository
– Kann Merge ausführen!
> git commit
– sichert aktuellen Stand lokal
– kommentiert aktuellen Stand
– erzeugt einzigartige Commit-ID (Hexzahl)
> git push
– kopiert neuesten Stand auf Zentralrepository
Dipl. Informatiker Gregor Rebel
127
Praktikum Embedded Systems v1.1s
Git - Verzweigte Entwicklung 1/3
4
• Verzweigung in der Entwicklung
> git checkout -b B1
1
• Verzweigung ab einem älteren Zustand
> git checkout -b B2 2
1
• Verzweigungspunkt:
Branchname/ Commit-ID/ Tag
• Alle verfügbaren Branches auflisten
> git branch -r
• Auf anderen Branch wechseln
> git checkout BRANCH_NAME
• Branch zum Server hochladen
> git push origin BRANCH_NAME
Dipl. Informatiker Gregor Rebel
128
B1
2
3
2
3
4
B2
5
Praktikum Embedded Systems v1.1s
Git - Verzweigte Entwicklung 2/3
User B
> git clone …:Repo
→ auf Branch origin master
User A
> git clone …:Repo
→ auf Branch origin master
> git checkout -b B1
→Branch B1 lokal erstellt
→Wechsel zu Branch B1
> git push origin B1
→ Branch auf Server laden
> git pull
→ neuer Branch entdeckt
> git branch -r
→ alle Branches auflisten
> git checkout B1
→Wechsel zu Branch B1
Dipl. Informatiker Gregor Rebel
129
Praktikum Embedded Systems v1.1s
Git - Verzweigte Entwicklung 3/3
• Implizite Verzweigung
• User A
> git commit -a -m „C1“
• User B
> git commit -a -m „C2“
> git push
→ C1 fehlt → Impliziter Branch
• User B
> git pull
> git commit -a
→ automatischer Merge von C1
• User A
> git pull
→ A jetzt auf Stand M
Dipl. Informatiker Gregor Rebel
130
C
C1
C
C1
C2
C
C1
M
C2
Praktikum Embedded Systems v1.1s
Git – Datenaustausch 1/2
• International durch verschiedene Protokolle
– Git-Protokoll über TCP Port 9418
– SSH (verschlüsselt)
– HTTP
– HTTPS
– FTP
– rsync
– EMail (Patches)
Dipl. Informatiker Gregor Rebel
131
Praktikum Embedded Systems v1.1s
Git – Datenaustausch 2/2
• Verschlüsselte Kommunikation per SSH
– Benutzer erzeugt RSA Schlüsselpaar
> ssh-keygen -t rsa -C VORNAME.NACHNAME
~/.ssh/id_rsa
~/.ssh/id_rsa.pub
Private Key
Public Key
• Publickey auf Server kopieren
• Bei Anmeldung wird Private Key benötigt
> git clone [email protected]:REPOSITORY
– Server verschlüsselt Rechenaufgabe mit Public Key
– Client entschlüsselt mit Private Key und sendet Ergebnis
– Server akzeptiert Client
Dipl. Informatiker Gregor Rebel
132
Praktikum Embedded Systems v1.1s
Git - Markierte Revisionen
> git tag MARKIERUNG
– Definiert Markierung für aktuellen Stand
– Markierung beschreibt z.B. Releaseversion
– Markierung später für Branch nutzbar
> git tag
– listet alle Markierungen auf
> man git-tag
Dipl. Informatiker Gregor Rebel
133
Praktikum Embedded Systems v1.1s
Git - Interoperabilität
• Git's das auch anders?
• Kompatibilitätsinterface
– GNU Arch (git-archimport)
– svn (git-svn)
– cvs (git-cvsexportcommit, git-cvsimport git-cvsserver)
– Darcs (darcs-fastconvert, darcs2git)
– Quilt (git-quiltimport)
→ manpages
Dipl. Informatiker Gregor Rebel
134
Praktikum Embedded Systems v1.1s
Git - Web-Interface
• Nice looking access to git
• OpenSource Software (some)
– gitweb is part of git distribution
→ http://git-scm.com/
– ViewGit is a total PHP rewrite
→ http://viewgit.fealdia.org/
– CGit written in C
→ http://hjemli.net/git/cgit/
– gitalist is written in Perl
→ http://www.gitalist.com/
• Existing Webhoster (one)
– GitHub – Closed Source hoster
→ http://github.com/
Dipl. Informatiker Gregor Rebel
135
Praktikum Embedded Systems v1.1s
Chapter 2
Multitasking
in
Embedded Systems
Dipl. Informatiker Gregor Rebel
136
Praktikum Embedded Systems v1.1s
Multitasking in ES
•
•
•
•
•
•
•
•
•
What is Singletasking?
What is Multitasking?
Why Multitasking?
Different approaches
Realtime Operating Systems
FreeRTOS
Synchronization
Example Project
Debugging Multitasking Software
Dipl. Informatiker Gregor Rebel
137
Praktikum Embedded Systems v1.1s
What is Singletasking?
•
•
•
•
•
•
•
•
Do only one thing at a time
Most efficient way to solve problems
Applicable to every algorithm
No management overhead
No internal synchronization required
Easy to code
Busy-wait for I/O
No Interrupts
Dipl. Informatiker Gregor Rebel
138
Praktikum Embedded Systems v1.1s
Singletasking Example
while (1) {
if ( Byte = receiveByte() ) { // blocks!
Buffer[Index++] = Byte;
if ( Index >= MessageSize) {
M_t* M = (M_t*) Buffer;
switch (M->Type) {
case mt_CommandA: ...
case mt_CommandB: …
default: break;
}
Index = 0;
}
}
}
Dipl. Informatiker Gregor Rebel
139
Praktikum Embedded Systems v1.1s
What is Multitasking?
•
•
•
•
•
•
•
•
•
Aim for multiple targets
Switch context often
Management overhead
Synchronization required
Interrupts required
Hard to code/ debug
Implicit delays
Increased memory usage
Implementation difficult for many algorithms
Dipl. Informatiker Gregor Rebel
140
Praktikum Embedded Systems v1.1s
Multitasking Example
main() {
int Queue = xQueueCreate(...);
xTaskCreate(Receive, Queue, …);
xTaskCreate(Process, Queue, …);
}
void Process(int Q) {
while (1) {
M = (M_t*) xQueueReceive(Q);
if (M) {
switch (M->Type) {
case mt_CmdA: ...
case mt_CmdB: ...
default: break;
}}}}
Dipl. Informatiker Gregor Rebel
void Receive(int Q) {
char Buffer[10][100];
int Index = 0;
while (1) {
char* Writer = &(Buffer[Index,0]);
int Remaining = MessageSize;
while (Remaining > 0) {
if ( Byte = readByte() ) { // sleeps!
*Writer++ = Byte; Remaining--;
}
xQueueSend(Q, &(Buffer[Index,0]););
Index++;
if (Index > 99) Index = 0;
}
}
141
Praktikum Embedded Systems v1.1s
Why Multitasking?
•
•
•
•
•
•
Functions spawnable multiple times
Eases handling of slow IO
Benefits from multiple CPU-cores
Only 1 central Timer required
Short Interrupt Service Routines
Less global variables required
Dipl. Informatiker Gregor Rebel
142
Praktikum Embedded Systems v1.1s
No life without Multitasking!
• Every Embedded System needs MT
• MT often implemented via Interrupts
– Complex Service Routines
– Data Exchange via Global Variables
– Difficult to debug
• Typical approach: Super-Loop
– Periodically starts set of functions
– Similar to task scheduler
Dipl. Informatiker Gregor Rebel
143
Praktikum Embedded Systems v1.1s
Different Approaches
• Multiprogramming
– Ancient mechanism for Periheral Access
– Realized via Terminal Stay Ready (TSR)
• Cooperative Multitasking
– Central Scheduler manages Processes
– Each process grants CPU to other processes
– Single process can block whole system
• Preemptive Multitasking
– Scheduler interrupts each process periodically
– Requires central Interrupt-Timer
• Preemptible Multitasking
– High priority Applications can interrupt others (OS/2, Linux, FreeRTOS)
– Allows faster response times
Dipl. Informatiker Gregor Rebel
144
Praktikum Embedded Systems v1.1s
Realtime Scheduling
• Definition of Realtime
Real-time computing means a hardware + software system that
must response within a strict time constraint.
• Realtime constraint
– Hard
Violation causes hazardous results
– Firm
Infrequent violations tolerable but degrade system's quality of
service
– Soft
Usefullness of results degrade after their deadline
→ http://en.wikipedia.org/wiki/Real-time_computing
Dipl. Informatiker Gregor Rebel
145
Praktikum Embedded Systems v1.1s
FreeRTOS
• Multitasking Scheduler
– Premptible Multitasking
– High triority tasks block low priority ones
• Inter Thread Communication
– Semaphores
– Queues
•
•
•
•
Developed specially for Embedded Systems
Open Source
FreeWare with Commercial Support
Ported to several μC Architectures
→ http://www.freertos.org/
Dipl. Informatiker Gregor Rebel
146
Praktikum Embedded Systems v1.1s
FreeRTOS – Queues
• Base of inter task communication
• Send message
Task A
– Task → Task
– Interrupt Service Routine → Task
• Call by value
• Reading from empty queue
3
2
– Function call waits until Queue is filled
– No CPU time is wasted during Wait
Task B
→ FreeRTOS Queues
Dipl. Informatiker Gregor Rebel
1
147
Praktikum Embedded Systems v1.1s
FreeRTOS - Semaphores
• Binary
∎
Task B
∎ ∎ ∎
Task B
Task A
– Implemented as Queue of fixed size 1
– Best suited for inter thread signaling
– Task B waits until Task A activates once
• Counting
Task A
– Implemented as Queue of fixed size N
– Task A can activate B up to N times at once
– Data in Queue is of no interest
→ FreeRTOS - Semaphores
Dipl. Informatiker Gregor Rebel
148
Praktikum Embedded Systems v1.1s
FreeRTOS - Mutex
• best suited for Resource locking
• Semaphore + Priority inheritance
• Non recursive
Task A
– Can be locked only once
• Recursive
∎
Task B
– thread can lock multiple times
– must be unlocked as often as being locked
→ FreeRTOS - Mutexes
Dipl. Informatiker Gregor Rebel
149
Praktikum Embedded Systems v1.1s
Multithreading with Queues
• Activate Extensions for Queues example on STM32-P107
> cd extensions.active/
> rm *
> ln -s ../extensions/makefile.free_rtos .
> ln -s ../extensions/makefile.example_threading_queues .
> ln -s ../extensions/makefile.free_rtos .
> ln -s ../extensions/makefile.example_leds .
Dipl. Informatiker Gregor Rebel
150
Praktikum Embedded Systems v1.1s
Multithreading with Queues
• Activate Extensions for Queues example on STM32-LCD
> cd extensions.active/
> rm *
> ln -s ../extensions/makefile.free_rtos .
> ln -s ../extensions/makefile.example_threading_queues .
> ln -s ../extensions/makefile.free_rtos .
> ln -s ../extensions/makefile.lcd_320x240_olimex .
Dipl. Informatiker Gregor Rebel
151
Praktikum Embedded Systems v1.1s
Multithreading with Queues
• Spawning Tasks
Producer
threading_start()
Consumer
Print
Dipl. Informatiker Gregor Rebel
152
Praktikum Embedded Systems v1.1s
Multithreading with Queues
• Communicate via Queues
s8_t Column;
s8_t Row;
char Text[10];
s16_t Value;
Producer
LED1
6
5
PJ PJ PJ
4
3
Consumer
Dipl. Informatiker Gregor Rebel
Print
enqueue/ dequeue
LED2
setPort()/ clrPort()
153
Praktikum Embedded Systems v1.1s
Multithreading
with Semaphores
• Activate Extensions for Semaphores example on STM32-P107
> activate.700_example_threading_semaphores_led.sh
• Activate Extensions for Semaphores example on STM32-LCD
> activate.700_example_threading_semaphores_lcd.sh
Dipl. Informatiker Gregor Rebel
154
Praktikum Embedded Systems v1.1s
Multithreading with Semaphores
• Spawning Tasks
Producer1
Producer2
threading_start()
Consumer1
Consumer2
Print
Dipl. Informatiker Gregor Rebel
155
Praktikum Embedded Systems v1.1s
Multithreading with Semaphores
• Communicate via Semaphore
enqueue/ dequeue
Producer1
Data irrelevant
0-10/s
• • •
0-10/s
Semaphore
Producer2
setPort()/ clrPort()
5/s
5/s
5/s
5/s
Consumer1
LED1
Consumer2
LED2
Consumer3
Consumer4
PJ PJ PJ
Dipl. Informatiker Gregor Rebel
156
Print
Praktikum Embedded Systems v1.1s
Debugging
Multitasking Software
•
•
•
•
Different types of Memories
Multitasking & Stacks
Watchdogs
Binary file formats
Dipl. Informatiker Gregor Rebel
157
Praktikum Embedded Systems v1.1s
Different types of Memories
• Storage
– RAM
– Heap
– Stack
• Datatypes
– Constants
– Global Variables
– Local Variables
Dipl. Informatiker Gregor Rebel
158
Praktikum Embedded Systems v1.1s
Constants
• Stored in Flash along code
• Automatically initialized
(initial value contained in binary)
• Reference to value points to
address > 0x0800 0000
0x4000 0000
Peripherals
• Example:
char* HelloWorld = „Hello world“;
„Hello World“
0x0800 0000
Flash
Dipl. Informatiker Gregor Rebel
159
0x2000 0000
RAM
Praktikum Embedded Systems v1.1s
RAM
• Area with read- / write access
• Multiple Memories possible
– On chip (start at 0x2000 0000)
– Off Chip via memory controller
• Static RAM
• Dynamic RAM
0x4000 0000
Peripherals
0x2000 0000
RAM
0x0800 0000
Flash
Dipl. Informatiker Gregor Rebel
160
Praktikum Embedded Systems v1.1s
Stack
• Located at one end of RAM
• Grows towards other end by each
– Function call
– Local variable
0x4000 0000
Peripherals
0x2000 0000
RAM
• Shrinks by each
– local variable end of scope
– Return from function call
Stack
0x0800 0000
Flash
Dipl. Informatiker Gregor Rebel
161
Praktikum Embedded Systems v1.1s
Global Variables
•
•
•
•
Stored in RAM
Initialized by code at program start
Assembled into block by linker
Reference to value points to
address > 0x2000 0000
0x4000 0000
Peripherals
Globals
0x2000 0000
RAM
• Example:
int V = 42;
Stack
void main() {
V++;
}
Dipl. Informatiker Gregor Rebel
0x0800 0000
Flash
162
Praktikum Embedded Systems v1.1s
Memory Heap
•
•
•
•
Block of memory in RAM
Allocated via malloc()
Freed via free()
Different managers
0x4000 0000
Peripherals
Globals
Heap
– heap1, heap2, heap3
– see corresponding extensions
0x2000 0000
RAM
Stack
0x0800 0000
Flash
Dipl. Informatiker Gregor Rebel
163
Praktikum Embedded Systems v1.1s
Multitasking & Stacks
•
•
•
•
Stacks in Singletask Applications
Stacks in Multitasking Applications
Stacks in FreeRTOS
Stackoverflow
Dipl. Informatiker Gregor Rebel
164
Praktikum Embedded Systems v1.1s
Stacks in
Singletask Applications
•
•
•
•
•
•
One single stack
Grows from one end of RAM
Efficient memory usage (no holes)
Always big egnough
Only one stack size to calculate
Mostly invisible to developer
0x2000 0000
RAM
Stack
Dipl. Informatiker Gregor Rebel
165
Praktikum Embedded Systems v1.1s
Stacks in
Multitasking Applications
•
•
•
•
•
•
One stack per thread
Grows at different locations
Memory holes between stacks
sizes are tradeoff
Each size hard to calculate
Developer has to re-check size
– New local variables
– More function calls
Dipl. Informatiker Gregor Rebel
0x2000 0000
RAM
Stack
Stack
Stack
Stack
Stack
Stack
166
Praktikum Embedded Systems v1.1s
Stacks in FreeRTOS
• Stacks handled by FreeRTOS
• Stack size defined at stack creation
– Argument to xTaskCreate()
– Cannot be changed during task execution
– Tasks normally do not end
• Stacks allocated on memory heap
Dipl. Informatiker Gregor Rebel
167
Praktikum Embedded Systems v1.1s
Stackoverflow
• Two detection schemes in FreeRTOS
– Activated in FreeRTOSConfig.h
– Stack size checked whenever a task is put to sleep
• activate.300_scheduler_free_rtos_stack_check_1.sh
– Checks stack pointer to be within allowed boundaries
– Cannot find overflow occured during task run time
– Fast check (only two pointer comparisons)
• activate.300_scheduler_free_rtos_stack_check_2.sh
– First checks stack pointer via method 1
– Each stack filled with known value
– Last 16 bytes of stack checked for changed values
– Will find out if stack has grown into last 16 bytes
– Slower than method 1 but more reliable
Dipl. Informatiker Gregor Rebel
168
Praktikum Embedded Systems v1.1s
Stack size calculation
• Three step approach
1) Double stack sizes until no overflow occurs
2) Run application
3) Use high water marks to minimize sizes
• Problems
– How to force tasks to use maximum memory?
→ Global variable: bool TestMode
– Recalculate after code changes
Dipl. Informatiker Gregor Rebel
169
Praktikum Embedded Systems v1.1s
High Water Marks
• Configured in
– #define INCLUDE_uxTaskGetStackHighWaterMark 1
• Activate via
– activate.300_scheduler_free_rtos_high_watermarks.sh
→ FreeRTOS - HighWaterMark
Dipl. Informatiker Gregor Rebel
170
Praktikum Embedded Systems v1.1s
Hardfault Handler
Dipl. Informatiker Gregor Rebel
171
Praktikum Embedded Systems v1.1s
Memfault Handler
Dipl. Informatiker Gregor Rebel
172
Praktikum Embedded Systems v1.1s
Watchdogs
Dipl. Informatiker Gregor Rebel
173
Praktikum Embedded Systems v1.1s
Binary file formats
• objdump
Dipl. Informatiker Gregor Rebel
174
Praktikum Embedded Systems v1.1s
Herunterladen