CONTENS
7
More Operting System Services 7.1 7.2 .3 7.4 7
8
82 83 84 8. 8.6 7
Overvew 21 Principles 217 An Exampe 233 Encapsuating Semaphore and Queues 244 Har Rea-me Scheduling Consideraions Saving Memory Space 24 Saving Power 27 Capter Summary 29 Probems 260 ·
23
Embedded Software evelopment os 91 9.2 9.3
10
99
Basic Design Using a Real-Time Operating System 215 8.
9
Message Queues, Mailboxes and Pipes 73 Timer Functions 184 Events 191 Memory Management 195 Interrupt Routnes in an ROS Environment Chapter ummary 206 Problems 207
173
Host and Target Machnes 261 Lnker/Locators fr Embedded Sofware 263 Getting Embeded Sofware nto the Target System Chapter Summar 280
ebuggng Techiques 101 12 10.3
Testng on Your Hos Machine nsruction Set Simators 302 The assert Macro 304
283 28
261
276
lX
X
C:oNIFTS
14
Using Laboatory Tools
Chapte Sumary Pobles 32
11
26
An Example System 11. 1
307
329
Wa Progra Dos 30 Envionmnt n c Pogr Os A Gud o h Soc Co 33 Souc o 9 Sumary 42 Problems
Afo
405
F Rg Ix
49
4
407
333
Preface
This book is to hep you learn the basic principes of writing sofware embedded systems. I surveys the issues and discusses the various techniques dng wh the n paticular, it dscuse approaches to the aproprite use of the ea-tme operatng systes upon whch uch ebedded sowae s ased. addton to expaning what these systes do this book points out how you can use the ost eectivey You need kow nothing about ebedded-systes sowre and is probles to read ths book; book; we'l dscss everything everythi ng o the very beginning. Yo shoud e a wth basc compute prograng concepts: you ght be a soware engineer wth a year or ore of experience or perhaps a student with a few prograing courses under your et You should understand he polms nvoved n witing appicaon pogas This book requirs a reading knowedge of the prograng language snc C s the ingua anc of ebeded systems you w have to ean t sooner o later i you hope to get into te ed A itte knowedge of asseby anguage wil also be hep. You have no doubt seen any books aout soware that are 80 or 9 or eve 1 page og Presualy you have notced by now that this book i much sae than that This s intentonathe dea s that you igh actuay want to read al a l the way through it. This ook is not entited Everything Ter Is to Know about Embedded Syst Softwae. Noody could write that ook and if oeone coud and dd you woudnt want to red it anyway This ook is oe ke Wat Yo Need to Know to Get Started n mbedded Stem Sotwar, teng you enough that youl unstand he ssues you wi ce and gettng you started on ndig the inration bout your partcua syste so hat you can eove those issues Ths book is not specc to any microprocesso or eal-ie operating syste o s t oiented towads any patcua softwr design methodoogy The principles are the sae regardess of which mcroprocesso and whch e tie operang syste and which soware design ethdoogy you use We wil concentrate on te principlesprincipes principlespr incipes that you can appy to amost
Xl
PRFAC
any embedded syste projec. When you need o know he pec f yur iropresor n you eal- prng syse, o t manuas ha hardwe and sofware vendors povide wih he pc p c This book wi hep you know wha inraion o ook r This book is no academc or heoreica; i oers enineerin inrmaion and enineerin advice In shor his book is he cornersone o he knowee ha you' nee r wriin wrii n ebedesys ebe desysems ems soware Davd E. Smon
Ackowledgments
No one hs enough god ides boo sch s ths o the peseveac to see it thoug wthout hep om othe peope. So he-moe o les i honoogi odes the story of his bo nd the peope who hepe e tun it into et Fist, thns e due to the eope t Pobts Copotion: to A. J Nicho who hs mde the compn the thought highquli sow envioent tht t s; to Miche Gisch the ongong dbtes on ebeddedss design nd oding se; to Richd Steinbeg who checed the code exe in this book nd to Ric Vibg who evewed the two hptes on hdwe nd oeted nume of m misonceptions M wife Lnn Godn encouged e to wite this boo pediting oet s it tuned outtht I woud enjo dong it Thn ou gett me stted nd thns the editng hep ven if ou e ws ight ot the ne points of Engish usge Thn ou to hfdozen csses of students to those of ou who ed the dvned uestions nd ed e to i thning to those o o who sed te eement question nd ed e to i m xpnton nd to of ou who sueed in sence wth e vesions of the mnusp hns to the smt peope t Dgon Sstems Inc who wote Nat1rlySpeking voice eognition pog good enough to ow me to pe mnusipt whie inuie pevented me om tpng much A huge thns to Jen bosse giing pemission to incude his ete opeting sstem µC!OS, s pt of ths boo You hve done the wod vo in wting this sstem nd n owing it to be used ee educton puposes A thn ou to John Keenn wh tught me ot of wht I ow bot hdwe nd who stightened out few undes tht mde it into the mnusipt The owing peope eviewed the st vesion of the mnusipt n povided ems of good suggestions mn of which incopoted no t
XV
iC K NOWLEDGMTS
book: Anono Bigaz, Fred Clegg Davd Cua Mch Eag Joh Kwan Tom Lyon Lyon and Steve Vnok. hank o a ank are de o Mke Hume a Zog who gv ps to u e hema exale a he end of Caer 3 and wh rn down legble oy of Fnay hank o Debbe aery and aqueyn Douee ho heherded h book hrough varou age; o nn ngh and Regna no wo oned o a o oe hng ha ad omeow made ene wen I we hem bu ddn' aer when omeone ee red read em and o aue Mue o urned my u kee no egbe gue
About Ts Book and the Accompanyng CD-ROM
The Perversities of Embedded Sysem O vy uua apc of mbddd yms i ha · rminolo uoudg m i o y cosse Fo y cocp, h a wo o h d wod. o y wod a u o suly d maig You w ju ha o l wh hs polm I his book w l o ou h aao mag; w wi asig a spcc maig ach wod so a w do' g co1sd Wh you a adig oh ooks o akig o po, how youll ha o b awa th hi wod may ma somg ligy di om wha y ma i h ook Aoh uua olm is ha h m mddd syt co such a oad ag of oduc ha galizaio a difcul Sym a buil wih micopocssos a di a h Z8 a 8-bi micooco ha ao u xal mmoy o h PowPC, a 32-bi micopocso ha c acc gigabs Th cod iz o h appicaio ais om ud 500 bys o mios o y Bcau of hi ad bcau of w aiy of applaos mbddd sowa a ld whic o wdm is uisal Ay u lowd by 85 c of g a pa of accpd go of s pcc a o b ok by o 15 c ju o g ym o wok s ook w cu o u)s of 85 c mhazg cc ad asog d us ad hg you dcd w you soud ow h como acc o f you oc pa of h 5 c
Cha per De penence n Ts Book oug ook dd be ad ag houg d aoug y ca dds a as a l o cas a pcd ; you ca k oud f you k Sc hs bo s y suc a y giig you ay b abl o k om c i ou aady kow som of h ia
XV
ABOUT THS BOOK
The mos imporan deendencie among te chae e ow e aa hee. 1. A First Look a Embedded Sysems
Hadware
Ierus
undamens r he Software Engieer
Adcd Hardwae ndametas
9 Deveopmen Tools
In duc i o Real-me Oe a ain g stm
0 Debgg g echques
5 Sur are Acitecte
7 Me e Opratng Sysem vces
8 B Dig Ug a Rl .pei Syem
xp Sye
I you aleay ko abo haae, exae o f yo o oe' eque a you ko anyh a yo ca k Cae 2 a 3 Hove don y o ead Cae 6 ou ea Cae 4 o ko aou he maeia n C++
Aough C++ a ceany oua laa e eee-e od you ee h ook T i t e to s o rae o om ung C hc oua th embd-sy e ae goo eao i ola e aplictio w. Hver, oe o· e acknolee iavaae o C + h pi ae any ay uc moe cul a u hn C T p rgrmi e ce oo ay ua C n C+ (n A J A,
ABouT Tms ooK I
xvn
.
and any other l�ngage in whic o miht hoose to progra yor embedded system, r that matter). herere r the prposes of istration n this book it maes sense o se ear of the ompiatios of + +
C!!
One of the probems of providing nderstandabe exampes in a book sh as this (and in t one of he probems of software in genera) is hat imporan poins end to ge os in a moass of detail. o prevent that om happening soe of the· exampes in ths book i o be written entirey in Instead hey wi be written in is idenia to , exept tht wherever yo pt wo exlamation points he omper does waeve is esibed aer those examation points Fo exmpe: if X 0) { Read time value fom the harwae !! Do a 11 the necesay ugy arhmetc y = esut of the ugy athmetc (y > 97 Tun on the warnn ht
f x is no zero then his program does whateer is neessary to read he ae om the hadwre ime hen it does vaios neessary aations and sores the rest in y. f y is geae than this program trns on the warning ight Rest assed that we only se he speia atre of r sh things as hardware-dependent ode or sei alations needed r some arane appiaion. he pars of he examples that pertain the point being made are witen in pain od ania 1
. It would be nice if we ou a re a u pbms in u ununaey, he coi r C is une eveen.
XVlll ABOU THIS OOK
H Vl Nm Many of th code exampls in tis book us convntion r nming varibls cl ere is brif introuction to tt onvntion. In ungr ian, th nam of a vribl ontins inrmtion bout its typ For ml nams r int vribs bgn wt th lttr "i "i '' nms r byt (unsiged char) vriabls bgin with by n so on on n iton rtin pr prss on vrib nams indicat tt vriabl is pointr (p_) n rr (a), n so on r ar som som typicl typ icl vrib nms an wat you cn infr bout th variabls variabls om thir nms: byEror- bt vrbl (tt contins n ror co probbl) H-an integr (that ontins th numbr of tnk prps) p_ T- ponter to a intgr acPr an aray of hrctrs (to b prnt most ik) fDon-J a (nt wtr a prcss s on). populr bea vn toug th varibl nms r t rst sh crpti may bliv tt th itt bit of inrmtion cotind i th na maks oin a littl asir2 ug s
µCIOS Whn yo gt to the dicsion of ratim oprting sstms in Captr 6 n beo ou miht want t try your hn t sing on of ts sstms o hp ou o tht t CD tht ccompnis ts book hs o am µC!S p "=. t on t D wth the pe rssion of Jn L who rte th sste You ar to us µCI a a stu tool b yu m no e it r a ommrcial product witot prmision om Mr Labrs. µ/ NOT "RWR insing provisions r µC ar shon on pg inrmation n Chptrs 6 an 7 n n bl 1 shoud gt yo strt us this systm f o wan mor inrmtion abot t sstm s Mr Lbrosse s book list in t sction on Furtr Ring or go to th µCIS µCIS 2. The Hungarian sed n th book i atualy a dc, o q gn onvenon he maj dieene beween he daet and the gna ae (1) ona do not e an undeoe o epaate a ex o the ret of h aae nae ad 2 the dae e onnon to nae nton a we as vaab.
µCI OS Licensing nrmation µ/ soce nd objec code cn be eely ditribued (o udt) b acrdit
coge and unveiie wihou equn lcene, lon thee no commcl ppcon noed. In ohe wod no cenin eued f µI ued educio ue You t obtain n Obec Code Dbuon Lcene to embed µI in comec oduc Th cene o u µI in poduc h od wh the nen o ke a pot hee wi be cene ee uch iuon, nd you need o contt M. Lboe picn You us obtn Souce Code Dtbution Lcene o dtibute µI ouc code Aan hee i fe uh icene nd ou need t conct boe picin You n conct Jen Lboe : eanLboe@uCOS-Icom o en boe 94 Cetiew Cice Weon FL 33327 USA l952172036 phone
-95-27-237 (fx)
Web site a wwwucos-iicom You can conact M Labosse r suppo of µC/OS, but pleae do not.do his unti yo have checked he IOS Web sie he aest vesions and xes Th eampe pogrms on the CD and /OS are inended use wih he Boad I++ compie DOS. You can ge he "schoar edion o ths comple (agan stdy use no r use in deveoping a commeia product) $49 95 as o his wiing Various eaies carry i o you can conac Boad at wwwbondcom
A First Look at Enbddd Systes
As microprocessors have become sallr and cheaper, more and more prod ucts have microprocessors "embedded in them to make hm smart. Such poucts as VCRs dgtal watches elevators, automobile engines thermostats ndustral ontrol equipment and scientic and medical nstruments are riven by thee micopocessors and their soware People use the term embedded system to mean any computer system hdden in any of these products Soware r embedded systems must hande ay problems beond those und in appcation sowre desktop or mainame computes Embedded systems oen have several hings to do at once They must espod o external events· (eg, someone pushes an elevator button) They must cope with al unusual conditions without uman interventon Ther work is subject to dadlines
1.1
Examples
of Embedde
Systems
o undersand the issues of embedded-systems soware and to make he pob lems a litle more concrete, let's start by examining a few smple systems Wel look back at these examples om tme to time as we dscuss specc issues and specic slutions
Telegraph The rst sysem that we wll udy is one that was codenmed Teegrph during is development Telegraph alows you o connect a printe that has ony a highspeed serial port ework From the outside Telegraph i a le
2
A FIRST LOOK EMBDDD Y
Figure
1.
Telegaph
Prner connecor Netwok connecto
astc box, 2 to 3 nces on a sde and abou af an nc tck. A gta cabe on one sde of e box ugs nto te sea ot on te n� A conneco on e oe sde of e box ugs no e newok A se of eega s sown n gue gue Obvousy ega mus eceve daa om e new0k and coy ono e sea o Howeve Teega s ae moe comcaed an at ee ae jus a few tngs tat Teega must do:
I
n newok dta soemes ave ou of ode daa sometmes ge os ong e w and oe of te daa sommes ave wce Teega mus so o e caos on e newok and vde a cen daa seam o e ne T ee mgt mgt be os of comutes on e newo1k a of wc gt wan o nt at once e nte exects o be ugged no a snge come Teega mus feed e nte one n jo at a me and someow od o a te ote comutes Neok ne ms ovde staus nmaon o any comute on e netok equest t eve ey ae busy nng a ob some oe omue e ogna se-o ne can' do a Teega as o ea as o wok w a numbe of deen tyes of nes wto cstome conguato;eega as o be abe o gue out te knd of ne o wc s aaced
Tlgraph ws buit o wok wh App nj pne, whch pcy hd a r por h yo ould onn diec to a Mcosh 1.