Академический Документы
Профессиональный Документы
Культура Документы
porting hints
Chris Simmonds 2net Limited Embedded Systems Conference UK. 2009 Copyright 2009, 2net Limited
Overview
Linux is a popular choice as an embedded OS ost pro!ects evolve "rom previous pro!ects o"ten based on an #$OS %ow to get "rom point & '#$OS( to point ) 'Linux(*
+s Linux an #$OS**
-orting options
Application Application
Application
Application
RTOS
Linux
Linux
Linux
%ow much existing code can + /eep* %ow much e""ort is re0uired to port my application to Linux* 1hat should + loo/ out "or* 1hat are the gains*
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
(#er #pace
Application
' library
)ernel #pace
RTOS
Linux
$ar%&are
Chris Simmonds 2net Ltd
$ar%&are
9
6evice drivers
Linux
%o-,R.()
+evice %river
,SR
$ar%&are
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
<verything is a "ile
3or example4 to rea% character# from fir#t #erial port4 open %evice no%e (file) /%ev/ttyS0
f = open (/dev/ttyS0, O_RDWR); l = read (f, buf, len);
/%ev/ttyS0
Linux
@ile read A write operations wor/ well with byte streams 8 such as a serial port %ow about a robot arm* $he ioctl "unction allows any interaction you want
stru t robot_ ontrol_blo ! r ; f = open (/dev/robot, O_RDWR); """ #o tl (f, S$%_RO&O%_'(R()%$RS, *r ); """
=0
%int =
+denti"y all code that accesses hardware directly 6esign a "ile8based inter"ace
Bse ioctl "or things that do not naturally "it the "ile concept
a/e this into a device driver #ememberC /eep device drivers as simple as possible
==
)ut, cannot handle interrupts Do control o" C-B cache or instruction 0ueue Dot the 3Linux way4
$he Linux Bser +O subsystem uses mmap to provide a "lexible "ramewor/ "or user space drivers
B+O E2F
=2
mmap example
+#n lude ,sys/--an"./ +def#ne 0O_'12S_(DDR$SS 0340500000 +def#ne (R)_'OS 030067 +def#ne (R)_)O%0O8 030069 #nt -a#n (#nt ar: , .ar ;ar:v<=) > uns#:ned led_dat; #nt -.; .ar ;ptr; -. = open (?/dev/-e-?, O_RDWR); ptr = --ap (0, 03@000, 'RO%_R$(D A 'RO%_WR0%$, )('_S1(R$D, -., 0O_'12S_(DDR$SS); """ ;(uns#:ned #nt;)(ptr B (R)_'OS) = neC_pos; C.#le (;(uns#:ned #nt;)(ptr B (R)_)O%0O8) D= 0) sleep (@); """
Chris Simmonds 2net Ltd =,
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
=.
-rocesses
6,+ /bin/init 6,+ 7 /bin/http% 6,+ 08 /bin/controler T T2 T8
6roce## 9 a%%re## #pace : pro;ram : threa% of execution Some proce## have < threa%
' library
Linux
=2
-ro
-rotected memory space #esources 'memory, open "iles( released when exit <asy to re8start a "ailed process
Con
=9
-ro
<asy communication using shared variables, mutexes and condition variables Similar memory model to #$OS tas/s
Con
=:
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
=;
Scheduling policies
SC%<6GO$%<#
SC%<6G@+@O
@ixed priority '= to 99(I preempts SC%<6GO$%<# Bse this "or real8time activities
SC%<6G##
&s SC%<6G@+@O but tas/s o" same priority time8slice 6e"ault 0uantum is =00 ms
=9
%int 2
a/e closely8coupled groups o" #$OS tas/s into -OS+7 threads within a process Separate #$OS tas/s with little interaction into separate processes a/e real time threads SC%<6G@+@O
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
2=
-OS+7
$as/s to -OS+7 threads Semaphores and mutexes to -OS+7 semaphores, mutexes and condition variables essage 0ueues to -OS+7 message 0ueues 1atchdog timers to -OS+7 cloc/s and timers
22
-OS+7 $hreads
$hreads run immediately they are created Dot possible to terminate an arbitrary thread
-OS+7 has many types o" mutex, including priority inheritanceH See E.F -OS+7 does have semaphores but they are not much usedH See E2F "or a discussion on mutexes vs semaphores
2,
@or "ull -OS+7 compliance in Linux you need current versions o" Linux and the C library
Small, 3embeddable4 C library Mood "or small systems with NL =9 i) storage )B$, lac/s many -OS+7 "unctions
2.
%int ,
Oou are going to have to re8write some code 1here possible, re8"actor code around shared data
1rite accessor "unctions to hide data structure "rom rest o" the program -rotect against concurrent access using a mutex +n the literature this is called a monitor E:F 8 ma/es "uture maintenance and porting much easier
22
&pplications run in 3user space4 &ll hardware interaction is in 3/ernel space4 &ll i5o via "iles and soc/ets &pplications are processes 6e"ault scheduling policy is time shared -OS+7 &-+ +s Linux real8time*
29
6eterministic scheduler Static priorities 'SC%<6G@+@O( -riority inheritance mutexes Loc/able memory 8 stops demand paging %igh resolution timers 6eterministic interrupt response
2:
6emand paging
6ro;ram a%%re##e#
$eap
6ro;ram file
-ages o" code and data are read "rom the program "ile on demand .Ji) at a time Causes !itter in real8time programs
+ata
'o%e
2;
+nterrupt latency
,nterrupt ,SR #tart# ,SR complete# = call# &a>e-up Ta#> i# #che%ule%
T0
T2
T8
Time
,nterrupt# %i#able%
,SR execute
6reemption latency
,nterrupt latency
29
-reemptible /ernel
#educes !itter in all three areas Mood "or 3"irm4 real8time Dot in the main line /ernel yet
,0
%int .
+n a real8time system, wor/ out what deadlines you have and how much !itter you can accept Loc/ memory in any process with real8time threads with mloc/all @or so"t real8time with !itter P millisecond enable /ernel preemption @or 3"irm4 real8time with !itter P =0>s or =00>s microseconds use the -#<< -$G#$ patch
,=
Summary
%ardware re0uires device drivers $as/s become threads in one or more processes ap #$OS "unctions onto -OS+7 Select real time model
+s Linux an #$OS*
,2
#e"erences
E=F v2lin 8 Qx1or/s &-+ "or Linux httpC55v2linHsource"orgeHnet5 E2F B+OC user8space drivers httpC55lwnHnet5&rticles52,22:2 E,F #ate8monotonic scheduling httpC55enHwi/ipediaHorg5wi/i5#ate8monotonicGscheduling E.F E2F utex mutandisC understanding mutex types and attributes httpC55wwwHembedded8linuxHcoHu/5tutorial5mutexGmutandis utex vsH Semaphores httpC55wwwH"eabhasHcom5blog5labels5SemaphoreHhtml
E9F uClibcC & C library "or embedded Linux httpC55wwwHuclibcHorg5 E:F onitor 'synchroniRation( httpC55enHwi/ipediaHorg5wi/i5 onitorG'synchroniRation(
,,