Embedded Systems Design using the Rabbit 3000 Microprocessor: Interfacing, Networking, and Application Development [1st edition] 9780750678728, 0750678720

The Rabbit 3000 is a popular high-performance microprocessor specifically designed for embedded control, communications,

293 3 4MB

English Pages 481 Year 2004

Report DMCA / Copyright

DOWNLOAD PDF FILE

Recommend Papers

Embedded Systems Design using the Rabbit 3000 Microprocessor: Interfacing, Networking, and Application Development [1st edition]
 9780750678728, 0750678720

  • Commentary
  • 47650
  • 0 0 0
  • Like this paper and download? You can publish your own PDF file online for free in a few minutes! Sign Up
File loading please wait...
Citation preview

Embedded฀Systems฀Design฀Using฀the฀฀ Rabbit฀3000฀Microprocessor

Embedded฀Systems฀Design฀Using฀the฀ Rabbit฀3000฀Microprocessor Interfacing,฀Networking฀and฀Application฀Development

฀ ฀



By฀

Kamal฀Hyder฀ Bob฀Perrin

AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier

Newnes฀is฀an฀imprint฀of฀Elsevier 30฀Corporate฀Drive,฀Suite฀400,฀Burlington,฀MA฀01803,฀USA Linacre฀House,฀Jordan฀Hill,฀Oxford฀OX2฀8DP,฀UK Copyright฀©฀2005,฀Elsevier฀Inc.฀All฀rights฀reserved. No฀part฀of฀this฀publication฀may฀be฀reproduced,฀stored฀in฀a฀retrieval฀system,฀ or฀transmitted฀in฀any฀form฀or฀by฀any฀means,฀electronic,฀mechanical,฀photocopying,฀recording,฀or฀otherwise,฀without฀the฀prior฀written฀permission฀of฀the฀ publisher.฀ Permissions฀may฀be฀sought฀directly฀from฀Elsevier’s฀Science฀&฀Technology฀ Rights฀Department฀in฀Oxford,฀UK:฀phone:฀(+44)฀1865฀843830,฀fax:฀(+44)฀ 1865฀853333,฀e-mail:฀[email protected].฀You฀may฀also฀complete฀ your฀request฀on-line฀via฀the฀Elsevier฀homepage฀(http://elsevier.com),฀by฀ selecting฀“Customer฀Support”฀and฀then฀“Obtaining฀Permissions.” Recognizing฀the฀importance฀of฀preserving฀what฀has฀been฀written,฀฀ Elsevier฀prints฀its฀books฀on฀acid-free฀paper฀whenever฀possible. Library฀of฀Congress฀Cataloging-in-Publication฀Data



(Application฀submitted.)

British฀Library฀Cataloguing-in-Publication฀Data A฀catalogue฀record฀for฀this฀book฀is฀available฀from฀the฀British฀Library. ISBN:฀0-7506-7872-0 For฀information฀on฀all฀Newnes฀publications฀ visit฀our฀Web฀site฀at฀www.books.elsevier.com 04฀05฀06฀07฀08฀09฀฀10฀9฀8฀7฀6฀5฀4฀3฀2฀1 Printed฀in฀the฀United฀States฀of฀America Internet฀Explorer,฀HyperTerminal,฀and฀Windows฀are฀trademarks฀of฀Microsoft฀Corporation.฀ Dynamic฀C฀and฀RCM34xx฀are฀trademarks฀of฀Rabbit฀Semiconductor.฀Softools฀is฀the฀trademark฀ of฀Softools,฀Inc.฀All฀other฀trademarks฀are฀the฀property฀of฀their฀respective฀owners.฀Readers฀should contact฀the฀appropriate฀companies฀for฀more฀complete฀information฀regarding฀trademarks฀and฀registration.

Dedications Kamal฀Hyder To฀my฀parents฀Rasheed฀and฀Najma,฀who฀supported฀my฀vision฀of฀ coming฀to฀America. To฀my฀wife฀Mariam,฀whose฀support฀and฀patience฀got฀me฀through฀ the฀long฀nights฀of฀work. To฀my฀friend฀and฀mentor฀Anugrah,฀who฀showed฀me฀through฀his฀life฀ that฀anything฀is฀achievable฀through฀sincerity฀and฀perseverance.

Bob฀Perrin For฀my฀mom,฀who฀started฀it฀all. For฀my฀wife,฀who฀never฀lets฀up.

v

Contents

Preface฀....................................................................................................................... xi Organization฀....................................................................................................................... xi Example฀Projects฀...............................................................................................................xii Acknowledgments฀.................................................................................................. xiii Chapter฀1:฀Introduction฀............................................................................................. 1 1.1฀Embedded฀Systems฀and฀Embedded฀Controllers฀............................................................ 1 1.2฀Embedded฀Systems—Case฀Studies฀............................................................................... 3 1.3฀Available฀Off-the-Shelf฀Solutions฀............................................................................... 10 1.4฀Software฀Development฀Tools฀...................................................................................... 14 1.5฀Design฀Trade-offs฀........................................................................................................ 16 1.6฀Migration฀to฀Higher฀Volume฀Production฀..................................................................... 17 Chapter฀2:฀The฀Basics฀............................................................................................... 19 2.1฀Evaluating฀Controllers฀................................................................................................. 19 2.2฀Defining฀the฀Problem฀................................................................................................... 28 2.3฀A฀Survey฀of฀Solutions฀.................................................................................................. 29 2.4฀A฀Rabbit’s฀Roots฀.......................................................................................................... 36 2.5฀Rabbit฀in฀Detail฀........................................................................................................... 38 2.6฀In฀Summary฀................................................................................................................. 64 Chapter฀3:฀Starting฀Out฀........................................................................................... 65 3.1฀Introduction฀to฀the฀RCM3200฀Rabbit฀Core฀................................................................. 65 3.2฀Introduction฀to฀the฀Dynamic฀C฀Development฀Environment฀........................................ 66 3.3฀Brief฀Introduction฀to฀Dynamic฀C฀Libraries฀................................................................. 67 3.4฀Memory฀Spaces฀in฀Dynamic฀C฀.................................................................................... 68 3.5฀How฀Code฀is฀Compiled฀and฀Run฀................................................................................. 76 3.6฀Setting฀Up฀a฀PC฀as฀an฀RCM3200฀Development฀System฀............................................. 79 3.7฀Time฀to฀Start฀Writing฀Code!฀........................................................................................ 79 3.8฀What’s฀Next?฀............................................................................................................... 91 Chapter฀4:฀Debugging฀............................................................................................. 92 4.1฀The฀Zen฀of฀Embedded฀Systems฀Development฀and฀Troubleshooting฀.......................... 92 vii

Contents 4.2฀Avoid฀Debugging฀Altogether—Code฀Smart฀฀............................................................... 97 4.3฀Common฀Problems฀...................................................................................................... 98 4.4฀Dynamic฀C฀Debugging฀Tools฀.................................................................................... 101 4.5฀Isolating฀the฀Problem฀................................................................................................. 105 4.6฀Run-Time฀Errors฀........................................................................................................ 109 4.7฀Miscellaneous฀Advanced฀Techniques฀........................................................................ 111 4.8฀Final฀Thoughts฀........................................................................................................... 115 Chapter฀5:฀Interfacing฀to฀the฀External฀World฀...................................................... 116 5.1฀Introduction฀............................................................................................................... 116 5.2฀Digital฀Interfacing฀฀.................................................................................................... 116 5.3฀High฀Current฀Outputs฀................................................................................................ 130 5.4฀CPLDs฀and฀FPGAs฀.................................................................................................... 141 5.5฀Analog฀Interfacing—An฀Overview฀........................................................................... 143 5.6฀Conclusion฀................................................................................................................. 156 Chapter฀6:฀Introduction฀to฀Rabbit฀Assembly฀Language฀...................................... 157 6.1฀Introduction฀to฀the฀Rabbit฀3000฀Instruction฀Set฀........................................................ 158 6.2฀Some฀Unique฀Rabbit฀Instructions฀.............................................................................. 178 6.3฀Starting฀to฀Code฀Assembly฀with฀Dynamic฀C฀............................................................. 180 6.4฀Passing฀Parameters฀Between฀C฀and฀Assembly฀.......................................................... 189 6.5฀Project฀1:฀Creating฀a฀Delay฀Routine฀.......................................................................... 196 6.6฀Project฀2:฀Blinking฀an฀LED฀....................................................................................... 200 6.7฀Project฀3:฀Debouncing฀a฀Switch฀................................................................................ 205 6.8฀Project฀4:฀Driving฀a฀Multiplexed฀LED฀Display฀......................................................... 211 6.9฀Project฀5:฀Setting฀Up฀a฀Real-time฀Clock฀................................................................... 221 Chapter฀7:฀Interrupts฀Overview฀............................................................................ 225 7.1฀Interrupt฀Details฀......................................................................................................... 228 7.2฀Writing฀an฀Interrupt฀Service฀Routine฀........................................................................ 236 7.3฀Project฀1:฀Polled฀vs.฀Interrupt-Driven฀Serial฀Communication฀.................................. 244 7.4฀Project฀2:฀Using฀Timer฀Interrupts฀.............................................................................. 254 7.5฀Project฀3:฀Using฀the฀Watchdog฀Timer฀........................................................................ 270 7.6฀Project฀4:฀Setting฀Up฀a฀Real-time฀Clock฀................................................................... 280 Chapter฀8:฀Multitasking฀Overview฀....................................................................... 288 8.1฀Why฀Use฀Multitasking?฀............................................................................................. 288 8.2฀Some฀More฀Definitions฀฀............................................................................................ 295 8.3฀Cooperative฀Multitasking฀.......................................................................................... 297 8.4฀Preemptive฀Multitasking฀........................................................................................... 298 8.5฀What฀to฀Be฀Careful฀About฀in฀Multitasking฀................................................................ 301 8.6฀Beginning฀to฀Multitask฀with฀Dynamic฀C฀.................................................................. 305 8.7฀Dynamic฀C’s฀Implementation฀of฀Cooperative฀Multitasking฀..................................... 306 8.8฀Dynamic฀C’s฀Implementation฀of฀Preemptive฀Multitasking฀....................................... 310 8.9฀Project฀2:฀Flashing฀LEDs฀with฀Multitasking฀............................................................. 312 viii

Contents 8.10฀Project฀3:฀Using฀Linux฀to฀Display฀Real฀Time฀Data฀................................................. 321 8.11฀Project฀4:฀Designing฀an฀Analog฀Sensor฀Task฀.......................................................... 326 8.12฀Back฀to฀the฀State฀Machine฀from฀Project฀1฀฀.............................................................. 331 8.13฀Final฀Thought฀.......................................................................................................... 333 Chapter฀9:฀Networking฀.......................................................................................... 334 9.1฀Dynamic฀C฀Support฀for฀Networking฀Protocols฀......................................................... 335 9.2฀Typical฀Network฀Setup฀.............................................................................................. 338 9.3฀Setting฀up฀a฀Core฀Module’s฀Network฀Configuration฀................................................. 340 9.4฀Project฀1:฀Bringing฀up฀a฀Rabbit฀Core฀Module฀for฀Networking฀................................. 344 9.5฀The฀Client฀Server฀Paradigm฀...................................................................................... 348 9.6฀The฀Berkeley฀Sockets฀Interface฀................................................................................. 349 9.7฀Using฀TCP฀vs.฀UDP฀in฀an฀Embedded฀Application฀.................................................... 352 9.8฀Important฀Dynamic฀C฀Library฀Functions฀for฀Socket฀Programming฀.......................... 353 9.9฀Project฀2:฀Implementing฀a฀Rabbit฀TCP/IP฀Server฀..................................................... 355 9.10฀Project฀3:฀Implementing฀a฀Rabbit฀TCP/IP฀Client฀.................................................... 361 9.11฀Project฀4:฀Implementing฀a฀Rabbit฀UDP฀Server฀....................................................... 369 9.12฀Project฀5:฀Web฀Enabling฀the฀Sensor฀Routine฀........................................................... 374 9.13฀Project฀6:฀Building฀an฀Ethernet-Connected฀Sprinkler฀Controller฀........................... 384 9.14฀Some฀Useful฀(and฀Free!)฀Networking฀Utilities฀....................................................... 406 9.15฀Final฀Thought฀.......................................................................................................... 409 Chapter฀10:฀Softools—The฀Third฀Party฀Tool฀......................................................... 410 10.1฀Who฀is฀Softools?฀...................................................................................................... 410 10.2฀The฀Rabbit฀WinIDE฀................................................................................................. 411 10.3฀SCRabbit฀Optimizer฀................................................................................................ 417 10.4฀SCRabbit฀Segments฀................................................................................................. 419 10.5฀SCRabbit฀#pragmas฀................................................................................................. 419 10.6฀Near฀and฀Far฀Functions฀........................................................................................... 421 10.7฀Inline฀Assembly฀....................................................................................................... 423 10.8฀Library฀Support฀....................................................................................................... 423 10.9฀WinIDE’s฀SLINK฀Linker฀......................................................................................... 424 10.10฀Debugging฀in฀the฀WinIDE฀..................................................................................... 426 10.11฀Memory฀Layout฀..................................................................................................... 430 10.12฀Real฀Time฀Operating฀Systems฀............................................................................... 435 10.13฀Ethernet฀and฀TCP/IP฀.............................................................................................. 436 10.14฀WinIDE฀and฀the฀Book’s฀Example฀Programs฀......................................................... 436 10.15฀Conclusion฀............................................................................................................. 437 Appendix฀A:฀Rabbit฀3000A—Extending฀the฀Rabbit฀3000’s฀Architecture฀............ 438 About฀the฀Authors฀................................................................................................. 449 Index฀....................................................................................................................... 451

ix

Preface Welcome!฀Are฀you฀new฀to฀Rabbit฀Semiconductor’s฀products?฀Are฀you฀new฀to฀networking?฀ Are฀you฀new฀to฀embedded฀controller฀design?฀Then฀this฀book฀is฀for฀you. This฀book฀is฀written฀by฀embedded฀developers฀for฀their฀peers.฀The฀authors฀asked฀each฀other฀“if฀ we฀were฀starting฀to฀design฀with฀a฀new฀microprocessor฀today,฀what฀would฀we฀want฀to฀know฀ about฀it?฀How฀would฀a฀book฀help฀us฀achieve฀an฀efficient฀design฀quickly?”฀and฀developed฀the฀ book฀accordingly.฀A฀number฀of฀concepts฀presented฀here฀are฀not฀just฀specific฀to฀the฀Rabbit฀ 3000฀microprocessor;฀they฀are฀equally฀applicable฀to฀any฀microprocessor.

Organization The฀book฀starts฀simple฀and฀brings฀readers฀along฀quickly฀to฀a฀level฀where฀they฀can฀assemble฀ hardware,฀wiggle฀bits,฀and฀blink฀lights.฀Then฀the฀real฀fun฀begins—web-enabling฀embedded฀ controllers. The฀first฀two฀chapters฀introduce฀the฀key฀concepts฀needed฀for฀embedded฀system฀design.฀ Next,฀the฀reader฀is฀given฀an฀architectural฀overview฀of฀the฀Rabbit฀3000฀microprocessor฀and฀ introduced฀to฀an฀easy-to-use฀development฀environment—Dynamic฀C.฀Simple฀and฀advanced฀ debugging฀techniques฀are฀covered฀with฀examples.฀ Chapter฀5฀explains฀common฀hardware฀interfacing฀issues.฀Since฀we฀believe฀that฀hardware฀ and฀software฀in฀embedded฀systems฀are฀inexorably฀woven฀together,฀we฀use฀a฀mix฀of฀freely฀ available฀Linux-based฀tools฀to฀show฀how฀analog฀data฀can฀be฀recovered฀from฀an฀embedded฀ controller฀and฀analyzed. Chapters฀6,฀7฀and฀8฀take฀the฀reader฀on฀a฀succinct฀tour฀of฀Rabbit฀assembly฀language,฀interrupts฀ and฀multitasking.฀This฀is฀where฀readers฀familiar฀with฀embedded฀system฀design฀but฀not฀with฀ the฀Rabbit฀will฀probably฀want฀to฀start.฀ Chapter฀9฀is฀a฀comprehensive฀treatment฀of฀how฀to฀bring฀the฀web฀to฀an฀embedded฀system.฀ We฀have฀done฀projects฀ranging฀from฀simple฀UDP฀and฀TCP฀clients฀and฀servers฀on฀both฀PCs฀ and฀Rabbit฀Core฀Modules฀to฀a฀data-acquisition฀system฀and฀an฀automated฀sprinkler฀controller฀ with฀a฀web฀browser฀interface.฀This฀chapter฀also฀introduces฀RabbitWeb,฀a฀novel฀and฀powerful฀ scripting฀language฀that฀makes฀it฀easy฀to฀create฀powerful฀web฀interfaces. Chapter฀10฀introduces฀a฀very฀powerful฀and฀professional฀development฀environment฀for฀ Rabbit฀3000฀code—the฀Softools฀ANSI฀C฀compiler.฀Softools฀brought฀almost฀two฀decades฀of฀ xi

Preface experience฀with฀optimizing฀compilers,฀assemblers฀and฀very฀clever฀linkers฀together฀to฀create฀ an฀easy-to-use฀development฀environment.฀We’ll฀discuss฀it฀at฀length. The฀book฀closes฀with฀Appendix฀A฀which฀covers฀the฀enhancements฀made฀to฀the฀Rabbit฀3000฀ with฀the฀release฀of฀the฀Rabbit฀3000A.฀Both฀processors฀are฀fully฀compatible฀with฀all฀of฀the฀ code฀and฀examples฀used฀throughout฀this฀book.

Example฀Projects The฀authors฀firmly฀believe฀that฀people฀learn฀by฀example.฀This฀philosophy฀pervades฀the฀book.฀ Each฀concept,฀once฀introduced฀and฀discussed,฀is฀used฀in฀a฀project.฀The฀projects฀range฀from฀ simple฀assembly฀code฀that฀blinks฀an฀LED฀to฀a฀web-enabled฀sprinkler฀controller. Today,฀there฀are฀many฀software฀tools฀available฀to฀engineers.฀Recognizing฀that฀not฀all฀engineers฀are฀proficient฀with฀all฀tool฀sets,฀we฀have฀included฀examples฀in฀languages฀ranging฀from฀ assembly฀language฀to฀Java,฀Perl,฀C/C++/C#,฀and฀Bash.฀Target฀environments฀include฀Windows฀ and฀Linux฀and฀of฀course฀the฀Rabbit฀Semiconductor฀hardware.฀The฀idea฀is฀that฀every฀reader฀ will฀likely฀take฀away฀tricks฀for฀tools฀that฀here-to-fore฀have฀not฀been฀in฀their฀toolbox. The฀authors฀have฀deliberately฀chosen฀simple฀examples,฀so฀that฀the฀reader฀would฀focus฀on฀ the฀key฀concepts฀instead฀of฀getting฀bogged฀down฀with฀implementation฀details฀of฀complex฀ projects.฀For฀example,฀the฀objective฀of฀a฀number฀of฀examples฀is฀just฀to฀flash฀an฀LED.฀While฀ this฀may฀seem฀overly฀simple,฀flashing฀an฀LED฀requires฀the฀right฀I/O฀ports฀to฀be฀set฀up฀and฀ the฀right฀logic฀and฀timing฀to฀be฀in฀place.฀These฀are฀key฀elements฀in฀most฀embedded฀systems฀ designs.฀ Our฀intention฀in฀writing฀this฀book฀is฀to฀bring฀to฀the฀reader฀our฀sense฀of฀excitement฀for฀embedded฀systems฀design฀as฀well฀as฀embarking฀on฀an฀adventure฀with฀the฀Rabbit฀3000฀as฀our฀faithful฀ companion.฀We฀hope฀our฀enjoyment฀and฀adventurism฀will฀rub฀off฀on฀you.฀May฀you฀enjoy฀ reading฀this฀book฀as฀much฀as฀we฀have฀enjoyed฀writing฀it. Sincerely, Kamal฀Hyder฀and฀Bob฀Perrin September฀2004

xii

Acknowledgments This฀book฀has฀benefited฀from฀contributions฀great฀and฀small฀from฀a฀long฀list฀of฀professionals.฀฀ Here฀we฀take฀a฀moment฀to฀recognize฀and฀thank฀the฀following฀individuals฀for฀their฀contributions. Norm฀Rogers฀and฀Carrie฀Maha฀(Rabbit฀Semiconductor)฀ Thank฀you฀for฀supporting฀and฀encouraging฀the฀authors฀thoughout฀the฀development฀of฀this฀book฀ and฀for฀arranging฀for฀quick฀and฀in-depth฀technical฀support฀when฀the฀authors฀had฀questions. Bill฀Auerbach฀(Softools฀Inc)฀ Thank฀you฀for฀your฀time฀to฀review฀our฀work฀and฀offer฀suggestions฀on฀improving฀it.฀But฀most฀ of฀all,฀thank฀you฀for฀writing฀the฀bulk฀of฀Chapter฀10—Softools.฀When฀you฀want฀a฀job฀done฀ right,฀go฀to฀an฀expert. Kelly฀Hall฀(Saint฀Bernard฀Software) Thank฀you฀for฀contributing฀the฀Linux฀based฀data฀acquisition฀projects฀(code,฀data฀and฀prose)฀ found฀in฀Chapter฀5฀and฀Chapter฀8. Greg฀Young฀(Progressive฀Solutions)฀ Thank฀you฀for฀helping฀the฀authors฀maintain฀a฀good฀perspective฀on฀this฀project. Pedram฀Abolghasem฀(Rabbit฀Semiconductor)฀ Thank฀you฀for฀being฀unafraid฀to฀tackle฀any฀issue฀at฀Rabbit฀Semiconductor฀for฀us.฀You฀were฀ man฀on฀point฀for฀us฀at฀Rabbit.฀Thank฀you. Brian฀Murtha฀(Rabbit฀Semiconductor)฀ Thank฀you฀for฀contributing฀most฀of฀the฀technical฀information฀about฀debugging฀in฀Dynamic฀C฀ found฀in฀Chapter฀4.฀Especially,฀thank฀you฀for฀writing฀the฀FASTSERMACS.LIB฀for฀this฀book.฀ Readers฀will฀be฀using฀these฀macros฀for฀years!฀Nice฀work,฀Brian. Larry฀Cicchinelli฀(Rabbit฀Semiconductor)฀ Thank฀you฀for฀reviewing฀the฀first฀seven฀chapters฀of฀this฀book.฀Sometimes฀you฀turned฀your฀ edits฀around฀in฀just฀a฀day฀or฀so.฀You฀answered฀a฀lot฀of฀questions฀for฀us,฀and฀this฀book฀would฀ not฀be฀what฀it฀is฀without฀your฀input.฀Thank฀you. Eugene฀Fodor,฀Joel฀Baumert,฀Owen฀Magee฀and฀Steve฀Hardy฀(Rabbit฀Semiconductor) The฀Rabbit฀Semiconductor฀engineers฀really฀stepped฀up฀to฀the฀plate฀as฀reviewers฀for฀the฀last฀ half฀of฀the฀book.฀But฀mostly,฀your฀advice,฀support,฀assistance฀and฀dedication฀helped฀us฀work฀ though฀a฀difficult฀piece฀of฀work—Chapter฀9.฀Thank฀you.

xiii

Acknowledgments Qingyi฀H.฀Perrin฀ For฀photography฀in฀Chapters฀1,฀5฀and฀9. Raymond฀D.฀Payne฀(Ebara฀Technologies) For฀all฀the฀hours฀you฀have฀spent฀researching,฀understanding฀and฀teaching฀AC฀snubbing฀techniques.฀Chapter฀5฀and฀Chapter฀9฀have฀benefited฀from฀your฀experience฀and฀wisdom.฀Thank฀you฀ for฀being฀generous฀with฀your฀time. Scott฀Henion฀(SH฀Designs)฀ For฀helping฀the฀authors฀understand฀the฀Rabbit฀3000฀Timer฀B,฀and฀for฀graciously฀allowing฀us฀ to฀include฀your฀ST-timerb.zip฀and฀timerb.ZIP฀in฀our฀CD.฀These฀libraries฀and฀examples฀will฀be฀ useful฀to฀many฀readers. Carol฀Lewis฀and฀Tiffany฀Gasbarrini฀(Elsevier฀Science฀and฀Technology฀Books) For฀midwifing฀this฀book. Michael฀Caisse฀ For฀his฀careful฀review฀of฀Chapter฀9฀and฀for฀moral฀support฀throughout฀the฀development฀of฀฀ the฀book. Karan฀Bajaj For฀helping฀us฀verify฀the฀networking฀code.฀His฀background฀with฀Microsoft฀Corporation฀and฀ .net฀development฀really฀helped. Prameela฀Mukkavilli฀฀ For฀helping฀us฀verify฀the฀networking฀code.฀Her฀Java฀background฀was฀very฀helpful. Baktha฀Muralidharan฀(Cisco฀Systems) For฀reviewing฀Chapter฀9.฀He฀is฀a฀software฀engineer฀with฀Cisco฀Systems฀and฀has฀contributed฀ towards฀the฀popular฀Ethereal฀tool. Aamer฀Akhter฀(Cisco฀Systems)฀ For฀reviewing฀Chapter฀9.฀He฀is฀a฀technical฀leader฀with฀Cisco฀Systems฀and฀has฀contributed฀ towards฀the฀popular฀Ethereal฀tool. Marco฀Molteni฀(Cisco฀Systems)฀ For฀reviewing฀Chapter฀9.฀He฀is฀a฀Software฀Engineer฀with฀Cisco฀Systems฀and฀works฀on฀future฀ platforms฀and฀IPv6.฀He฀tries฀to฀contribute฀back฀to฀Open฀Source฀tools฀like฀Ethereal. Elias฀Kesh฀(Pioneer฀Electronics)฀ For฀reviewing฀Chapter฀8.฀He฀has฀designed฀and฀implemented฀a฀number฀of฀RTOSes฀and฀is฀ active฀with฀embedded฀Linux฀development.

xiv

1

CHAPTER฀

Introduction This฀book฀focuses฀on฀methods฀and฀practices฀for฀embedded฀system฀design,฀and฀it฀takes฀a฀topdown฀approach฀in฀presenting฀the฀material.฀The฀discussion฀will฀progress฀from฀questions฀of฀ what฀an฀embedded฀system฀is,฀to฀detailed฀examples฀of฀how฀to฀solve฀specific฀design฀problems฀ with฀Rabbit฀Semiconductor’s฀technology. This฀chapter฀examines฀broad฀issues฀surrounding฀embedded฀development฀and฀discusses฀common฀solutions.฀Chapter฀1฀is฀not฀concerned฀with฀specific฀technology฀but฀rather฀over-arching฀ issues฀of฀embedded฀system฀development.฀The฀chapter฀narrows฀the฀scope฀of฀embedded฀system฀ problems฀to฀those฀with฀which฀the฀remainder฀of฀the฀book฀is฀concerned.฀

1.1฀Embedded฀Systems฀and฀Embedded฀Controllers In฀the฀1960s,฀mini-computers฀found฀their฀way฀into฀dedicated฀control฀applications.฀Someone฀ coined฀the฀phrase฀“OEM฀computers”฀to฀differentiate฀these฀machines฀from฀“business฀computers.”฀As฀time฀passed฀and฀technology฀shrunk,฀the฀expression฀“dedicated฀controller”฀came฀into฀ vogue฀and฀was฀promptly฀supplanted฀by฀“embedded฀controller.” The฀phrase฀“embedded฀system”฀was฀coined฀to฀describe฀systems฀that฀contain฀an฀embedded฀ controller.฀Nowadays,฀all฀but฀the฀simplest฀electronic฀devices฀have฀some฀sort฀of฀microprocessor฀ in฀them.฀Hence฀today,฀the฀phrase฀“embedded฀system”฀describes฀almost฀any฀electronic฀product. The฀embedded฀community฀has฀adapted฀various฀industry฀“computer”฀standards฀for฀use฀in฀ industrial฀control฀systems.฀For฀example,฀the฀PCI฀bus฀has฀four฀common฀form฀factors.฀The฀first฀ is฀the฀“standard”฀PCI฀form฀factor฀found฀in฀desktop฀PCs.฀The฀second฀is฀the฀stackable฀PC104+฀ form฀factor.฀The฀third฀is฀the฀rack฀mountable฀CPCI฀(Compact฀PCI).฀The฀fourth฀is฀the฀PCI฀ Industrial฀Computer฀Manufacturers฀Group฀(PICMG)฀adaptation. PC104+,฀CPCI฀and฀PICMG฀are฀all฀attempts฀to฀adapt฀a฀“computer”฀technology฀for฀industrial฀use.฀ The฀electrical฀specifications฀are฀almost฀identical,฀but฀the฀form฀factors฀are฀significantly฀altered. Until฀recently,฀embedded฀PCs฀have฀been฀specialized฀PCs.฀For฀example,฀Ampro฀invented฀the฀ PC104฀form฀factor฀to฀allow฀PCs฀to฀be฀squeezed฀into฀a฀physical฀envelope฀more฀conducive฀to฀ embedded฀applications฀than฀a฀full-sized฀desktop฀PC฀motherboard.฀These฀embedded฀PCs฀have฀ been฀easily฀identified฀as฀“embedded฀controllers”฀and฀are฀quite฀distinct฀in฀form฀from฀desktop฀ computers. In฀2002,฀the฀Mini-ITX฀form-factor฀x86฀motherboards฀hit฀shelves฀everywhere.฀Mini-ITX฀was฀ developed฀ostensibly฀to฀provide฀a฀smaller฀footprint฀for฀desktop฀PCs.฀Embedded฀systems฀ 1

Chapter฀1 designers฀seized฀on฀the฀low฀cost,฀high฀volume฀miniaturized฀full-up฀PC฀motherboards฀ for฀control฀applications.฀Companies฀began฀ packaging฀the฀Mini-ITX฀motherboards฀with฀ power฀supplies฀and฀I/O฀mixes฀suitable฀for฀ embedded฀applications.฀What฀was฀initially฀ designed฀as฀a฀“desktop฀computer”฀is฀now฀ serving฀as฀an฀embedded฀controller.฀Let’s฀take฀ a฀closer฀look฀at฀the฀evolution฀from฀desktop฀to฀ embedded฀PC. Figure฀1.1฀shows฀a฀PC฀motherboard.฀To฀make฀ a฀complete฀system,฀several฀PCI฀or฀ISA฀cards฀ Figure฀1.1:฀A฀desktop฀PC฀motherboard฀is฀big,฀ must฀be฀added฀to฀provide฀video฀and฀I/O.฀Of฀ and฀not฀very฀functional฀without฀additional฀ course,฀a฀power฀supply฀is฀required.฀A฀hard฀ drive฀is฀required฀to฀store฀an฀operating฀system฀ cards,฀power฀supply฀and฀hard฀disk. and฀application฀software.฀Enclosures฀are฀ available฀to฀rack-mount฀this฀type฀of฀system,฀ but฀most฀cases฀are฀designed฀for฀consumer฀ use. Figure฀1.2฀shows฀a฀PC104฀stack.฀The฀ system฀is฀shown฀with฀a฀power฀supply฀card,฀ processor฀card฀and฀five฀expansion฀cards฀ that฀provide฀video,฀storage,฀parallel฀I/O฀and฀ numerous฀serial฀channels.฀The฀super-rugged฀ enclosure฀is฀made฀of฀a฀thick฀aluminum฀ extrusion฀and฀uses฀dense฀rubbery฀rails฀to฀ isolate฀the฀electronics฀from฀vibration฀and฀ shock.฀The฀PC104฀stack฀is฀an฀example฀of฀ Figure฀1.2:฀A฀PC104฀stack฀is฀more฀compact฀and฀ how฀PC฀technology฀was฀adapted฀to฀an฀emrugged฀than฀a฀desktop฀PC. bedded฀form฀factor. Figure฀1.3฀shows฀a฀JK฀Microsystems฀Mini-ITX฀ based฀embedded฀PC.฀This฀system’s฀footprint฀is฀little฀ larger฀than฀a฀compact฀disc.฀The฀system฀has฀a฀power฀ supply฀board฀that฀takes฀7–30฀volts฀DC฀as฀an฀input.฀ Storage฀is฀provided฀by฀up฀to฀two฀Compact฀Flash฀ cards.฀These฀rugged,฀solid-state฀devices฀are฀more฀ consumer฀technology฀suitable฀for฀use฀in฀embedded฀ systems.฀They฀are฀lightweight฀and฀rugged.฀If฀more฀ storage฀is฀required,฀Type฀II฀Compact฀Flash฀mechanical฀hard฀drives฀can฀be฀used. Figure฀1.3:฀The฀Mini-ITX฀was฀adopted฀ A฀watchdog฀timer฀is฀also฀provided฀on฀the฀same฀PCB฀ directly฀into฀the฀embedded฀systems฀ that฀contains฀the฀power฀supply฀and฀Compact฀Flash฀ market. 2

Introduction connectors.฀The฀watchdog฀timer฀is฀a฀device฀that฀many฀embedded฀systems฀contain฀to฀improve฀ reliability.฀In฀the฀event฀of฀a฀software฀crash,฀the฀watchdog฀timer฀will฀reset฀the฀system. A฀compact฀disc฀and฀hard฀drive฀can฀also฀be฀added.฀Figure฀1.3฀shows฀these฀devices฀installed.฀ Both฀devices฀were฀designed฀for฀laptop฀computers.฀They฀are฀lightweight฀and฀tolerate฀environmental฀stresses฀gracefully.฀The฀popularity฀of฀laptops฀has฀pushed฀the฀prices฀of฀these฀ components฀almost฀as฀low฀as฀desktop฀PC฀components. The฀processor฀on฀the฀motherboard฀is฀a฀fanless฀low฀power฀device.฀This฀too฀is฀coincidentally฀ well-suited฀to฀the฀embedded฀market. JK฀Microsystems฀sells฀the฀embedded฀PC฀shown฀in฀Figure฀1.3฀with฀an฀aluminum฀enclosure.฀ I/O฀is฀limited฀to฀keyboard,฀mouse,฀video,฀Ethernet,฀USB,฀serial฀and฀parallel฀ports,฀but฀can฀be฀ expanded฀using฀the฀PCI฀slot฀shown.฀The฀overall฀system฀is฀less฀expensive฀than฀a฀similar฀PC104฀ system฀shown฀in฀Figure฀1.2. In฀just฀a฀few฀years,฀the฀embedded฀PC฀market฀has฀gone฀from฀having฀ill-suited฀desktop฀PC฀ technology฀to฀having฀expensive฀but฀rugged฀PC104฀technology฀and฀currently฀to฀having฀inexpensive฀compact฀rugged฀PCs.฀With฀the฀pressures฀and฀economic฀realities฀of฀consumer฀markets,฀ PC฀technologies฀can฀be฀expected฀to฀become฀less฀expensive,฀smaller฀and฀more฀rugged.฀The฀ embedded฀systems฀sector฀will฀certainly฀adopt฀these฀technologies฀directly. Technology’s฀perpetual฀march฀is฀oblivious฀to฀the฀delicacies฀of฀human฀semantics.฀Today,฀the฀ distinction฀between฀embedded฀controller฀and฀computer฀is฀rapidly฀blurring.฀We฀can฀expect฀to฀see฀ more฀and฀more฀“computer”฀technologies฀adopted฀directly฀into฀embedded฀control฀applications. An฀engineer฀faced฀with฀automating฀a฀process฀or฀building฀an฀instrument฀has฀access฀to฀a฀ wide฀variety฀of฀products.฀There฀are฀embedded฀controllers฀available฀as฀printed฀circuit฀boards฀ (PCBs),฀as฀packaged฀controllers,฀or฀as฀hundreds฀of฀flavors฀of฀embedded฀PCs.฀There฀are฀ microprocessors฀and฀microcontrollers฀that฀range฀from฀8-pin฀devices฀costing฀less฀than฀a฀dollar฀ to฀many฀hundred-pin฀ball฀grid฀array฀(BGA)฀packaged฀devices฀costing฀hundreds฀of฀dollars.฀If฀ none฀of฀these฀devices฀suit฀a฀particular฀application,฀perhaps฀the฀developer฀might฀fancy฀a฀half฀a฀ million-gate฀field฀programmable฀gate฀array฀(FPGA). The฀term฀“embedded฀system”฀is฀applied฀to฀everything฀from฀coffee฀makers฀to฀communications฀ satellites.฀All฀of฀these฀systems฀have฀some฀form฀of฀microprocessor฀lurking฀behind฀the฀scenes฀ orchestrating฀behavior.฀ This฀book฀focuses฀mainly฀on฀small-฀to฀medium-scale฀embedded฀systems฀and฀associated฀ instrumentation.฀These฀type฀of฀systems฀share฀many฀common฀attributes.฀There฀are฀sensors.฀ There฀are฀actuators.฀There฀are฀desired฀behaviors.฀There฀are฀human฀interfaces—Man฀Machine฀ interfaces฀or฀MMIs.฀Above฀all,฀there฀is฀an฀embedded฀controller฀operating฀behind฀the฀scenes,฀ tying฀everything฀together.

1.2฀Embedded฀Systems—Case฀Studies To฀help฀the฀reader฀understand฀the฀scope฀of฀embedded฀applications฀with฀which฀this฀book฀is฀concerned,฀three฀systems฀are฀detailed฀here.฀The฀systems฀as฀presented฀here฀have฀been฀simplified฀from฀ their฀actual฀implementations฀both฀to฀protect฀proprietary฀intellectual฀property฀and฀for฀brevity.

3

Chapter฀1 The฀applications฀range฀from฀an฀underwater฀torque฀tool฀to฀a฀30฀megawatt฀generator.฀Each฀ application฀presented฀its฀designers฀with฀unique฀problems.฀The฀three฀projects฀share฀common฀ threads.฀Each฀project฀shows฀different฀methods฀for฀addressing฀specific฀control฀problems.฀These฀ projects฀give฀the฀reader฀a฀flavor฀of฀the฀diversity฀in฀the฀embedded฀control฀industry. 1.2.1฀Underwater฀Torque฀Tool Four฀hundred฀meters฀below฀the฀North฀Atlantic฀is฀most฀inhospitable.฀It’s฀also฀home฀to฀sizable฀ petroleum฀reserves.฀In฀an฀increasingly฀audacious฀quest฀for฀oil,฀humans฀have฀run฀pipelines฀and฀ placed฀wellheads฀deep฀under฀the฀ocean. One฀task฀that฀must฀be฀performed฀is฀the฀simple฀act฀of฀rotating฀mechanical฀valves฀on฀the฀sea฀ floor.฀One฀technique฀used฀is฀to฀mount฀a฀hydraulic฀torque฀tool฀on฀the฀end฀of฀a฀remotely฀operated฀vehicle’s฀(ROV’s)฀manipulator.฀Fly฀the฀ROV฀and฀tool฀down฀to฀the฀site.฀Insert฀the฀torque฀ tool฀in฀the฀valve฀manifold.฀Turn฀the฀valve. The฀combination฀of฀high-cost฀equipment฀and฀environmental฀impact฀makes฀turning฀underwater฀ valves฀a฀more฀considered฀task฀than฀turning฀on฀a฀garden฀hose.฀If฀the฀wrong฀valve฀is฀turned,฀or฀ rotated฀the฀wrong฀direction,฀or฀moved฀the฀wrong฀amount,฀or฀is฀over-tightened,฀or฀is฀stuck฀or฀is฀ broken,฀the฀environmental฀impact฀can฀be฀disastrous฀and฀the฀economic฀costs฀staggering. The฀system฀described฀here฀was฀designed฀to฀rotate฀28-inch฀ball฀valves฀with฀up฀to฀a฀quartermillion฀foot-pounds฀of฀torque. The฀torque฀tool฀is฀a฀robot฀and฀contains฀sensors฀to฀monitor฀pressures,฀strain,฀speed฀and฀temperatures.฀The฀on-board฀computer฀communicates฀through฀an฀umbilical฀to฀the฀surface฀ship.฀ Electrically฀controlled฀hydraulic฀valves฀control฀the฀tool’s฀actions. Figure฀1.4฀shows฀a฀block฀diagram฀of฀the฀tool.฀The฀front฀of฀the฀tool฀has฀the฀coupler฀and฀ latches.฀Undersea฀valves฀have฀a฀port฀designed฀to฀capture฀torque฀tools.฀This฀port฀is฀referred฀to฀ as฀a฀“bucket.”฀The฀latches฀engage฀the฀bucket฀to฀secure฀the฀tool.฀This฀also฀provides฀a฀stationary฀ anchor฀for฀the฀tool฀to฀press฀against฀as฀it฀produces฀torque฀on฀the฀valve. To฀ensure฀that฀the฀correct฀valve฀is฀turned,฀each฀valve฀has฀a฀different฀orifice฀geometry.฀Much฀ like฀common฀screws฀require฀a฀slotted฀or฀crossed฀screwdriver฀to฀turn฀the฀head,฀underwater฀ valves฀require฀different฀couplers.฀The฀coupler฀can฀be฀changed฀to฀accommodate฀different฀valves. The฀ROV฀pilot฀flies฀the฀tool฀down฀to฀the฀valve.฀Using฀a฀video฀camera,฀the฀manipulator฀arm฀is฀ used฀to฀position฀the฀torque฀tool’s฀nose฀in฀the฀bucket.฀Next,฀the฀tool฀operator฀sends฀a฀command฀ to฀the฀tool฀engaging฀the฀latches฀securing฀the฀tool฀in฀the฀bucket.฀If฀conditions฀are฀right,฀the฀ valve฀rotation฀can฀commence. An฀embedded฀controller฀inside฀the฀torque฀tool฀monitors฀conditions฀in฀the฀tool,฀provides฀a฀ communication฀link฀with฀the฀ship฀and฀directs฀the฀tool’s฀behavior.฀This฀particular฀tool฀uses฀an฀ industrial฀PC฀with฀stackable฀I/O฀cards,฀a฀PC104฀stack. The฀ROV฀contains฀a฀hydraulic฀pressure฀unit฀(HPU),฀and฀the฀pressurized฀fluid฀is฀delivered฀to฀ the฀tool฀through฀a฀high-pressure฀hose.฀Hydraulic฀pressure฀data฀is฀acquired฀through฀sensors฀ and฀monitored฀by฀the฀PC104฀stack.฀ Hall-effect฀sensors฀on฀the฀gearbox฀monitor฀the฀motor฀and฀coupler฀speed.฀Strain฀gauges฀monitor฀the฀actual฀torque฀applied฀to฀the฀valve. 4

Introduction Figure฀1.4:฀ Underwater฀torque฀ tool฀designed฀to฀turn฀ valves.

Umbilical To Surface

Retractable Latches Coupler

Latch Recess

Robotic Arm

Valve Fitting To Valve

ROV

Torque Tool

Nose

Bucket

Ocean Floor Valve Manifold

HPU

Hydraulic line

Umbilical To Surface

Embedded Controller

Retractable Latch

Rotating coupler Hydraulic Motor

Embedded Controller (PC104 stack)

Torque Tool

CPU & memory NIC card

Gearbox

Retractable Latch

Analog to Digital Hydraulic Control Valve

Digital to Analog To umbilical Ethernet

Solenoid Driver

To Sensors To hydraulic control valve

Hydralic line from HPU on ROV

To retractable latches

The฀PC104฀stack฀constantly฀watches฀the฀sensors.฀If฀any฀problem฀is฀detected,฀the฀tool฀is฀shut฀ down฀and฀the฀operator฀alerted. The฀inclusion฀of฀an฀embedded฀controller฀in฀the฀tool฀allows฀the฀implementation฀of฀algorithms฀ to฀gradually฀ramp฀up฀torque฀and฀speed฀on฀the฀valve.฀It฀also฀allows฀for฀an฀operator฀to฀command฀the฀valve฀movement฀precisely.฀If฀a฀valve฀is฀stuck,฀the฀operator฀can฀command฀the฀tool฀to฀ apply฀specific฀torques฀for฀specific฀times฀or฀angular฀distances. The฀PC104฀stack฀ties฀together฀numerous฀sensors฀and฀actuators,฀allowing฀high฀forces฀to฀be฀ applied฀to฀undersea฀valves฀with฀safety฀and฀precision. A฀PC฀was฀chosen฀as฀the฀embedded฀platform.฀The฀developers฀desired฀a฀multi-tasking฀Linux฀ operating฀system.฀This฀allowed฀code฀development฀to฀occur฀on฀inexpensive฀desktop฀PCs฀with฀ a฀low฀initial฀investment฀in฀software฀tools.฀Another฀big฀factor฀was฀the฀Ethernet฀connectivity฀ supported฀by฀Linux. The฀tool฀can฀be฀operated฀through฀an฀IP฀(internet฀protocol)฀network฀from฀a฀geographically฀ remote฀location.฀The฀tool฀operator฀does฀not฀have฀to฀be฀aboard฀the฀ship฀to฀control฀the฀tool.฀ TCP/IP฀packets฀are฀easily฀routed฀over฀the฀Internet. In฀fact,฀during฀initial฀debugging฀sessions,฀the฀tool฀was฀deployed฀from฀a฀ship฀at฀sea฀while฀the฀ torque฀tool฀operator฀was฀located฀in฀a฀cubicle฀3,000฀miles฀away.฀This฀configuration฀allowed฀ 5

Chapter฀1 the฀R฀&฀D฀team฀to฀interrogate฀the฀tool,฀operate฀it฀and฀update฀software฀on฀the฀tool,฀all฀over฀the฀ Internet.฀This฀arrangement฀was฀less฀expensive฀than฀flying฀the฀R฀&฀D฀team฀and฀their฀lab฀to฀a฀ ship. A฀derivative฀design฀of฀this฀controller฀used฀a฀serial฀port฀to฀communicate฀between฀the฀tool฀and฀ the฀ship.฀Point-to-point฀protocol฀(PPP)฀was฀used฀to฀route฀TCP/IP฀packets฀over฀the฀serial฀connection,฀while฀still฀retaining฀the฀diverse฀network฀features฀of฀the฀design. 1.2.2฀Industrial฀Vacuum฀Pump฀for฀Semiconductor฀Processing฀Equipment The฀process฀of฀turning฀silicon฀wafers฀into฀silicon฀chips฀has฀many฀steps.฀Some฀of฀these฀ operations฀are฀carried฀out฀in฀low-pressure฀environments.฀There฀are฀vacuum฀pumps฀designed฀ specifically฀to฀create฀low-pressure฀environments฀for฀use฀in฀wafer฀processing. A฀silicon฀wafer฀can฀yield฀hundreds฀of฀individual฀dice฀(or฀chips).฀Depending฀on฀complexity,฀ each฀die฀may฀be฀worth฀a฀considerable฀sum฀of฀money.฀Each฀wafer฀is฀worth฀several฀hundred฀ times฀the฀price฀of฀a฀die. During฀processing,฀wafers฀are฀ganged฀together฀in฀carriers฀or฀caddies.฀Each฀caddy฀carries฀ tens฀of฀wafers.฀A฀caddy฀of฀silicon฀wafers฀increases฀in฀value฀as฀it฀moves฀through฀the฀fabrication฀process.฀By฀the฀time฀the฀caddy฀is฀halfway฀through฀a฀process฀run,฀it฀is฀not฀unusual฀for฀the฀ value฀of฀the฀caddy’s฀contents฀to฀be฀several฀hundred฀thousand฀dollars. Silicon฀fabrication฀plant฀operators฀consider฀it฀“bad฀form”฀on฀the฀part฀of฀process฀tool฀vendors฀ to฀allow฀a฀process฀tool,฀such฀as฀a฀vacuum฀pump,฀to฀ruin฀a฀batch฀of฀wafers.฀Considerable฀care฀ goes฀into฀the฀design฀of฀vacuum฀pumps฀destined฀for฀silicon฀fabrication฀plants. Figure฀1.5฀shows฀a฀block฀diagram฀of฀an฀industrial฀vacuum฀unit.฀In฀the฀unit,฀two฀pumps฀are฀ cascaded฀to฀develop฀the฀low฀pressures฀required฀by฀the฀wafer฀fabrication฀process.฀Each฀pump฀ is฀driven฀by฀an฀eight-horsepower฀three-phase฀electric฀motor. The฀tool฀chamber฀is฀filled฀with฀highly฀toxic฀vapors฀released฀by฀the฀process฀chemicals.฀Nitrogen฀gas฀is฀mixed฀with฀the฀exhaust฀gasses฀to฀dilute฀the฀toxic฀gases฀to฀safer฀levels. The฀pumps฀require฀water-cooling฀to฀maintain฀acceptable฀operating฀temperatures.฀If฀the฀pump฀ housing฀is฀too฀hot฀or฀is฀unevenly฀heated,฀the฀mechanical฀parts฀fail฀to฀stay฀in฀tolerance฀and฀ leaks฀develop—such฀leaks฀can฀reduce฀vacuum฀in฀the฀process฀chambers,฀which฀can฀in฀turn฀ upset฀the฀process฀chemistry฀and฀diminish฀wafer฀yield. Flow฀sensors฀monitor฀the฀nitrogen฀and฀water฀supplied฀to฀the฀unit.฀A฀disruption฀in฀the฀flow฀of฀ nitrogen฀is฀a฀safety฀concern.฀A฀disruption฀in฀the฀flow฀of฀water฀affects฀pump฀efficiency฀and฀ mechanical฀wear. Thermocouples฀monitor฀pump฀housing฀and฀bearing฀temperature.฀Elevated฀temperatures฀indicate฀excessive฀friction,฀or฀reduced฀water฀circulation.฀Slightly฀elevated฀temperatures฀require฀a฀ maintenance฀engineer฀to฀review฀the฀system,฀while฀severely฀elevated฀temperatures฀require฀the฀ pump฀to฀be฀shut฀down. The฀man-machine฀interface฀(MMI)฀shown฀in฀Figure฀1.5฀consists฀of฀an฀LCD฀mounted฀on฀the฀ front฀of฀the฀equipment฀cabinet.฀The฀MMI฀has฀no฀front฀panel฀controls.฀The฀inclusion฀of฀front฀ panel฀controls฀would฀only฀open฀the฀possibility฀of฀a฀human฀manually฀changing฀the฀pump฀ behavior฀and฀ruining฀a฀caddy฀of฀wafers.฀The฀process฀is฀fully฀automated.฀The฀silicon-wafer฀ process-tool฀controls฀the฀vacuum฀pump฀through฀the฀“control฀interface.” 6

Introduction Process Tool Chamber

Figure฀1.5:฀Industrial฀ vacuum฀pump฀used฀ on฀silicon฀fab฀lines.

MMI Control Interface Diagnostic Interface

Vacuum sensor Embedded Controller

Toxic gases Flow Meter

Temperature sensors

3-phase AC mains

Cold Water

Pilot Relay Contactor

Motor

Boost Pump

Cooling Jacket

Toxic gases Water

Flow Meter

Nitrogen Gas

Temperature sensors Pilot Relay

Contactor

Motor

Main Pump

Cooling Jacket

Warm water connect to chiller and recyler Diluted exhaust gases

The฀control฀interface฀is฀configured฀to฀meet฀the฀requirements฀of฀the฀customer.฀This฀interface฀ may฀be฀a฀simple฀digital฀interface,฀or฀a฀set฀of฀dry-contacts,฀or฀one฀of฀three฀serial฀interfaces— RS-485,฀RS-232฀or฀Echelon’s฀LonTalk™. The฀maintenance฀port฀provides฀access฀to฀a฀pump’s฀performance฀logs฀and฀to฀real-time฀operating฀conditions.฀A฀service฀technician฀uses฀a฀laptop฀with฀an฀RS-232฀connection฀to฀access฀the฀ pump’s฀maintenance฀port. This฀system฀is฀an฀example฀of฀an฀embedded฀application฀that฀requires฀limited฀control฀functions฀ but฀numerous฀monitoring฀and฀logging฀features.฀The฀embedded฀controller฀can฀turn฀the฀pump฀ motors฀on฀and฀off฀but฀has฀no฀ability฀to฀control฀the฀speed฀of฀the฀motors.฀The฀temperatures฀ may฀be฀monitored฀and฀logged฀but฀there฀is฀no฀valve฀affording฀electronic฀control฀over฀coolant.฀ The฀performance฀of฀this฀type฀of฀system฀is฀fixed฀by฀the฀mechanical฀design.฀The฀embedded฀ controller฀can฀monitor฀conditions฀and฀shut฀down฀the฀system฀if฀a฀critical฀fault฀occurs.฀System฀ performance฀can฀be฀measured฀and฀recorded฀for฀later฀inspection฀by฀engineers,฀but฀control฀ algorithms฀aren’t฀needed฀as฀part฀of฀the฀embedded฀controller. When฀it฀came฀to฀selecting/designing฀the฀pump’s฀embedded฀controller,฀the฀engineers฀were฀ faced฀with฀a฀build฀vs.฀buy฀decision.฀The฀decision฀factored฀in฀cost,฀size,฀availability฀and฀computational฀requirements.฀In฀the฀end,฀the฀engineers฀opted฀to฀design฀a฀custom฀controller฀from฀ the฀ground฀up. The฀embedded฀controller฀was฀tailored฀to฀the฀application.฀Extra฀digital฀and฀analog฀channels฀ were฀included฀to฀support฀future฀expansion.฀The฀controller฀served฀the฀application฀well฀for฀ many฀years.

7

Chapter฀1 1.2.3฀Controlling฀a฀30-Megawatt฀Generator An฀embedded฀controller฀may฀only฀be฀a฀discrete฀subsystem฀in฀a฀larger฀electronic฀control฀ project.฀As฀an฀example,฀Figure฀1.6฀shows฀a฀30-megawatt฀natural฀gas฀powered฀turbine-driven฀ generator.฀These฀systems฀were฀built฀to฀provide฀electricity฀in฀remote฀locations฀in฀Mexico,฀ Central฀America฀and฀parts฀of฀South฀America. Variants฀of฀this฀system฀replaced฀the฀generator฀with฀large฀compressors฀or฀other฀mechanical฀ equipment฀necessary฀for฀industrial฀facilities฀to฀operate฀in฀remote฀locales.฀However,฀all฀variants฀of฀this฀system฀used฀the฀same฀natural฀gas฀feeder฀valve฀and฀valve-controller.฀ Concrete Control Shack

Signal Wires for Main Computer

Generator Building

Transmission Lines

Main Computer

RS-485

Main Control Valve

Gas Powered Turbine Rotating Shaft

Valve Controller (Embedded Controller) Armored Cable

30 Megawatt Generator

Natural Gas

Transformer

Figure฀1.6:฀An฀automated฀natural฀gas฀powered฀electrical฀generating฀station.

The฀overall฀system฀required฀the฀monitoring฀and฀control฀of฀a฀variety฀of฀devices฀associated฀ with฀the฀gas฀turbine฀and฀the฀accompanying฀generator฀or฀compressor.฀A฀main฀computer฀was฀ responsible฀for฀all฀the฀high-level฀data฀acquisition฀and฀control฀tasks.฀However,฀in฀the฀case฀of฀ the฀natural฀gas฀feeder฀valve,฀a฀separate฀valve-controller฀was฀developed฀to฀offload฀some฀of฀the฀ low฀level฀monitoring฀and฀control฀functions. The฀valve-controller฀allowed฀the฀main฀computer฀to฀issue฀commands฀such฀as฀“open฀feeder฀ valve฀to฀x%,”฀and฀“emergency฀off.”฀The฀details฀of฀controlling฀the฀valve’s฀stepper฀motor฀and฀ monitoring฀the฀position฀feedback฀were฀left฀to฀the฀valve-controller. Figure฀1.7฀shows฀a฀detailed฀view฀of฀the฀embedded฀system฀designed฀to฀control฀the฀natural฀gas฀ feeder฀valve.฀The฀gas฀valve฀was฀moved฀with฀a฀large฀stepper฀motor฀and฀gearbox.฀There฀was฀ feedback฀indicating฀the฀position฀of฀the฀valve. Embedded Controller (Valve Controller) CARD #1

Main Control Valve Armored and Potted Cable

Stepper Motor Driver

Stepper Motor

Gear Box

CARD #2 CPU & Memory Sensor Inputs RS-485 connects to Main Computer

RS-485

Position Sensor

CARD #3 Power Supply

Natural Gas From main

Valve

Figure฀1.7:฀Valve-controller฀block฀diagram. 8

Natural Gas To Turbine

Introduction Safety฀requirements฀dictated฀that฀the฀valve-controller฀be฀located฀in฀the฀concrete฀control฀ room—see฀Figure฀1.6.฀The฀armored฀and฀potted฀cable฀that฀ran฀out฀to฀the฀automated฀valve฀ protected฀the฀wires฀from฀mechanical฀damage฀and฀prevented฀natural฀gas฀from฀seeping฀up฀the฀ cable฀and฀into฀the฀control฀shack. The฀system฀was฀designed฀in฀the฀late฀1980s.฀The฀valve-controller฀consisted฀of฀three฀rackmounted฀cards.฀The฀first฀card฀contained฀the฀power-supply.฀The฀second฀card฀contained฀a฀ custom฀controller฀design฀based฀on฀the฀8051฀microcontroller,฀and฀the฀third฀card฀was฀a฀commercially฀available฀stepper฀motor฀driver. Embedded฀systems฀integrated฀into฀larger฀projects฀are฀no฀less฀challenging฀to฀design฀than฀stand฀ alone฀designs.฀The฀engineer฀is฀always฀faced฀with฀build฀vs.฀buy฀decisions.฀As฀was฀done฀with฀ this฀project,฀a฀portion฀of฀the฀design฀may฀be฀bought฀(the฀stepper฀motor฀controller)฀while฀the฀ rest฀of฀the฀system฀can฀be฀built.฀The฀process฀of฀putting฀together฀the฀pieces฀is฀called฀“system฀ integration.” Some฀systems฀can฀be฀pieced฀together฀entirely฀by฀a฀systems฀integrator.฀An฀example฀is฀a฀desktop฀PC฀built฀up฀by฀an฀electronics฀enthusiast.฀The฀motherboard,฀PCI฀cards,฀case,฀fans,฀power฀ supplies,฀disk฀drives,฀keyboards,฀mouse฀and฀monitor฀are฀all฀purchased฀components.฀The฀ assembly฀is฀fairly฀mechanical. Other฀types฀of฀systems฀integration฀projects฀can฀take฀a฀lot฀of฀effort.฀For฀instance,฀putting฀ together฀devices฀that฀don’t฀communicate฀using฀same฀protocols฀or฀don’t฀have฀well฀thought฀ out฀and฀well-designed฀interfaces฀may฀consume฀lots฀of฀time.฀Moreover,฀testing฀of฀systems฀or฀ components฀and฀deploying฀pilot฀projects฀can฀be฀nontrivial฀tasks. System฀integration฀can฀reduce฀development฀time,฀and฀in฀small฀volume฀production฀it฀can฀save฀ money.฀In฀the฀case฀of฀the฀valve-controller,฀buying฀the฀stepper฀motor฀controller฀saved฀design฀time. 1.2.4฀Case฀Study฀Summary Each฀of฀these฀case฀studies฀appears฀on฀the฀surface฀to฀be฀quite฀unique.฀However,฀they฀all฀share฀ common฀design฀challenges.฀Sensors฀must฀be฀monitored.฀Communication฀with฀other฀devices฀ is฀required.฀Actuators฀must฀be฀energized.฀Control฀decisions฀must฀be฀made฀locally฀while฀some฀ decisions฀are฀made฀remotely฀by฀humans฀or฀probably฀by฀another฀control฀system. The฀sensory฀tasks฀for฀these฀applications฀are฀as฀demanding฀as฀many฀instrumentation฀projects.฀ Signal฀conditioning฀for฀a฀pressure฀transducer฀in฀a฀hydraulic฀torque-tool฀is฀similar฀to฀the฀circuitry฀for฀monitoring฀pressure฀(or฀lack฀thereof)฀in฀a฀vacuum฀pump. Communicating฀with฀a฀valve฀controller฀via฀a฀command-line฀interface฀over฀RS-485฀is฀very฀ similar฀to฀RS-485฀communication฀in฀a฀silicon฀wafer-processing฀tool.฀The฀commands฀and฀ communications฀protocols฀may฀be฀different฀but฀the฀underlying฀physical฀interface฀is฀the฀same. The฀underlying฀technology฀required฀for฀these฀applications฀is฀common฀to฀many฀other฀projects.฀ An฀engineer฀that฀understands฀the฀issues฀and฀trade-offs฀involved฀with฀these฀case฀studies฀can฀ design฀a฀wide฀variety฀of฀embedded฀systems.

9

Chapter฀1

1.3฀Available฀Off-the-Shelf฀Solutions Commercially฀available฀embedded฀controllers฀can฀be฀divided฀into฀four฀categories—packaged฀ controllers,฀board-level฀controllers,฀core฀modules,฀and฀chip฀level฀devices.฀Each฀has฀advantages฀and฀disadvantages.฀ 1.3.1฀Packaged฀Controllers Commercially฀available฀packaged฀controllers฀have฀an฀enclosure.฀Packaged฀control฀ solutions฀often฀have฀I/O฀targeted฀at฀industrial฀applications.฀The฀controller฀industry฀ offers฀a฀range฀of฀enclosures฀from฀vented฀ plastic฀to฀explosion-proof฀cases.฀Some฀ packaged฀controllers฀have฀generic฀MMIs— touch-screens,฀LCDs฀and฀keypads฀are฀the฀ most฀common.฀Figure฀1.8฀shows฀an฀example฀ of฀a฀Z-World฀packaged฀controller฀with฀a฀ generic฀MMI.

Figure฀1.8:฀The฀OP7200฀is฀a฀full-featured฀

Packaged฀controllers฀are฀the฀most฀expensive฀ controller฀with฀built-in฀LCD฀and฀keypad. of฀the฀four฀embedded฀controller฀classes.฀ In฀applications฀that฀are฀not฀too฀cost-sensitive,฀these฀devices฀can฀also฀provide฀the฀quickest฀ solution฀to฀automation฀problems.฀The฀systems฀integrator฀needs฀only฀to฀mount฀the฀controller’s฀ enclosure,฀hook฀up฀wires฀and฀write฀some฀code.฀ Of฀the฀four฀classes฀of฀commercially฀available฀controllers,฀the฀packaged฀controllers฀are฀the฀ most฀expensive,฀but฀are฀also฀the฀most฀complete฀and฀easy฀to฀use฀solutions.฀Their฀cost฀often฀ causes฀them฀to฀be฀most฀economically฀acceptable฀in฀low-volume฀applications฀where฀accelerated฀time-to-market฀and฀reduced฀development฀costs฀can฀be฀traded฀off฀against฀higher฀per-product฀ costs฀and฀reduced฀assembly฀operations.฀ 1.3.2฀Board-Level฀Controllers Board-level฀controllers฀are฀the฀most฀diverse฀ class฀of฀controllers.฀They฀range฀from฀$2500฀ PICMG฀(PCI฀Industrial฀Computer฀Manufacturers฀Group)฀PCs฀to฀low฀cost฀postage-stamp฀ sized฀controllers.฀Board-level฀controllers฀ require฀the฀systems฀integrator฀to฀provide฀ physical฀protection฀for฀the฀electronics.฀An฀ engineer฀considering฀a฀board-level฀solution฀ has฀a฀vast฀selection฀of฀I/O฀mixes,฀processor฀types,฀memory฀capacities฀and฀I/O฀mixes฀ from฀which฀to฀pick.฀Figure฀1.9฀shows฀an฀ example฀of฀a฀feature-rich฀Z-World฀boardlevel฀controller.

Figure฀1.9:฀The฀BL2100฀offers฀ numerous฀I/O฀points฀and฀Ethernet. 10

Introduction Board-level฀controllers฀require฀mounting,฀some฀form฀of฀physical฀protection,฀and฀sometimes฀ cooling.฀Many฀times฀an฀equipment฀enclosure,฀suitable฀for฀mounting฀the฀embedded฀controller,฀ already฀exists฀in฀an฀industrial฀system.฀Other฀applications฀may฀require฀the฀developer฀to฀design฀ or฀purchase฀a฀separate฀enclosure.฀Some฀board-level฀controller฀manufacturers฀offer฀optional฀ enclosures฀for฀their฀board฀level฀products. Connecting฀wires฀to฀board฀level฀controllers฀requires฀careful฀attention.฀Screw฀terminals฀are฀a฀ popular฀choice฀for฀wire฀termination.฀Figures฀1.10a฀and฀1.10b฀show฀examples฀of฀two฀types฀of฀ screw฀terminals.฀These฀are฀easy฀to฀prototype฀with฀but฀can฀make฀production฀wiring฀harnesses฀ difficult฀or฀time฀consuming฀to฀attach฀to฀the฀controller. Screw฀terminal฀connectors฀that฀are฀fixed฀to฀the฀controller฀board,฀as฀shown฀in฀Figure฀1.9฀and฀ Figure฀1.10b,฀require฀the฀production฀staff฀to฀mount฀the฀controller฀first฀and฀then฀to฀screw฀wires฀ into฀the฀controller.฀This฀means฀that฀people฀making฀the฀wiring฀harness฀can’t฀complete฀the฀wire฀ termination฀until฀the฀harness฀is฀installed฀with฀the฀controller.฀Depending฀on฀the฀product฀and฀ manufacturing฀process,฀this฀may฀not฀be฀desirable. Screw฀terminal฀connectors฀that฀are฀fixed฀to฀the฀controller฀board฀can฀also฀pose฀challenges฀to฀ field฀service฀technicians.฀A฀technician฀may฀want฀to฀swap฀out฀a฀controller฀with฀a฀known฀good฀ unit.฀Having฀to฀unscrew฀a฀large฀number฀of฀screw฀terminals,฀making฀sure฀that฀a฀large฀number฀ of฀wires฀are฀marked฀for฀proper฀reattachment,฀and฀then฀reattaching฀all฀the฀wires฀to฀a฀new฀controller฀is฀time฀consuming฀and฀potentially฀error฀prone. An฀attractive฀alternative฀to฀screw฀terminals฀that฀are฀fixed฀to฀a฀controller฀board฀is฀a฀class฀of฀ connectors฀called฀“pluggable”฀screw฀terminals.฀Figure฀1.10a฀shows฀a฀Z-World฀packaged฀ controller฀(PK2500)฀with฀pluggable฀screw฀terminals.฀This฀system฀allows฀the฀wire฀harness฀to฀ be฀terminated฀with฀a฀screw฀terminal฀socket฀that฀plugs฀into฀a฀fixed฀header฀on฀the฀controller฀ board.฀This฀arrangement฀allows฀for฀rapid฀assembly฀of฀the฀embedded฀systems฀as฀well฀as฀swift฀ and฀error-free฀field฀replacement฀of฀the฀embedded฀controller.฀Of฀course฀the฀trade-off฀for฀this฀ convenience฀is฀a฀small฀increased฀cost.฀

a.

b. Figure฀1.10:฀Pluggable฀screw฀terminals฀are฀more฀expensive฀ but฀are฀more฀flexible฀than฀fixed฀screw฀terminals. 11

Chapter฀1 Systems฀in฀which฀the฀cable-harnesses฀will฀be฀assembled฀separately฀and฀integrated฀into฀a฀system฀will฀benefit฀from฀the฀use฀of฀pluggable฀screw฀terminals.฀Completed฀cable-harnesses฀can฀be฀ easily฀tested.฀Final฀assembly฀will฀go฀much฀smoother฀if฀the฀complete฀and฀tested฀cable-harness฀ is฀simply฀plugged฀into฀the฀PCB฀mounted฀header.฀ One฀ramification฀of฀using฀fixed฀screw฀terminals฀is฀that฀as฀part฀of฀final฀assembly,฀individual฀ wires฀from฀the฀prefabbed฀cable-harness฀must฀be฀terminated฀to฀the฀PCB.฀This฀can฀be฀an฀error-prone฀task.฀Furthermore,฀testing฀of฀the฀cable-harness฀is฀quite฀difficult฀with฀a฀bunch฀of฀ loose฀wires฀flying฀around.฀This฀implies฀that฀the฀first฀time฀the฀cable฀harness฀is฀tested฀is฀in฀the฀ completed฀system.฀Errors฀will฀be฀found,฀and฀depending฀on฀the฀wiring฀error,฀serious฀and฀costly฀ damage฀can฀result. Field฀service฀technicians฀overwhelmingly฀prefer฀pluggable฀screw฀terminals฀to฀fixed฀screw฀ terminals.฀When฀a฀few฀wires฀must฀be฀changed,฀say฀to฀replace฀a฀sensor,฀both฀types฀of฀screw฀ terminals฀are฀equally฀well฀suited.฀However,฀when฀a฀controller฀board฀must฀be฀changed,฀the฀ time฀required฀to฀carefully฀remove,฀label฀and฀reinstall฀a฀lot฀of฀individual฀wires฀is฀much฀longer฀ than฀simply฀removing฀a฀pluggable฀screw฀terminal. Another฀popular฀connector฀choice฀for฀controller฀manufacturers฀is฀the฀D-Subminiature฀or฀฀ D-sub฀connector.฀These฀have฀limited฀current฀carrying฀capacity,฀and฀can฀be฀expensive฀to฀integrate฀into฀production฀wiring฀harnesses.฀On฀the฀positive฀side,฀shielded฀D-sub฀back-shells฀are฀ available฀to฀help฀minimize฀EMI. A฀very฀inexpensive฀connector฀found฀on฀board-level฀controllers฀is฀the฀pin-header.฀Depending฀ on฀the฀pin฀size฀and฀pitch,฀reasonable฀voltage฀isolation฀and฀current฀carrying฀capacity฀can฀be฀ realized.฀Crimp฀pins,฀insulation฀displacement฀connectors฀(IDC),฀and฀mass-termination฀ribbon฀ cables฀are฀available฀for฀production฀wire฀harnesses.฀At฀first฀glance,฀pin-headers฀seem฀a฀bit฀ dubious,฀but฀they฀have฀found฀widespread฀industry฀acceptance. Board฀level-controllers฀in฀the฀form฀of฀edge฀cards,฀epoxy-encapsulated฀modules,฀DIP,฀SIP฀ or฀SIMM฀boards฀require฀a฀“carrier-board.”฀The฀carrier-board฀will฀have฀to฀be฀designed฀to฀ accommodate฀the฀purchased฀controller,฀while฀providing฀a฀pragmatic฀method฀of฀pinning฀out฀ the฀controller’s฀I/O฀to฀the฀devices฀and฀sensors฀that฀make฀up฀the฀control฀application. 1.3.3฀Core฀Modules The฀third฀product฀class฀is฀the฀core฀module.฀A฀core฀module฀is฀a฀physically฀small฀controller฀ consisting฀of฀a฀central฀processing฀unit฀(CPU),฀memory,฀glue฀logic฀and฀simplistic฀input/output฀ (I/O).฀A฀core฀module฀is฀built฀on฀a฀printed฀circuit฀board฀(PCB)฀and฀is฀similar฀to฀many฀of฀the฀ small฀board-level฀controllers.฀The฀primary฀feature฀differentiating฀a฀core฀module฀from฀a฀board฀ level฀controller฀is฀intellectual฀property฀(IP).฀ Board-level฀products฀are฀proprietary฀controllers.฀The฀manufacturer฀of฀these฀devices฀will฀not฀ freely฀license฀the฀controller฀designs.฀Any฀attempt฀to฀copy฀a฀board-level฀controller฀infringes฀on฀ the฀manufacturer’s฀IP฀rights. Core฀modules฀are฀designed฀to฀be฀copied.฀A฀core฀model฀is฀in฀many฀respects฀a฀practical฀reference฀design.฀An฀engineer฀can฀purchase฀a฀low-cost฀core฀module฀and฀develop฀a฀complete฀

12

Introduction embedded฀application.฀Depending฀on฀the฀economics฀of฀ the฀project฀and฀where฀it฀is฀in฀its฀life฀cycle,฀the฀designer฀ can฀freely฀copy฀the฀core฀module฀design฀and฀migrate฀ to฀a฀lower฀cost,฀higher฀volume฀chip฀level฀solution. All฀microprocessor฀manufacturers฀have฀reference฀designs฀available.฀Many฀of฀these฀are฀ available฀on฀demonstration฀or฀evaluation฀boards.฀ These฀reference฀designs฀are฀designed฀for฀use฀on฀ an฀engineer’s฀desktop.฀By฀contrast,฀core฀modules฀ are฀designed฀for฀integration฀into฀production฀systems.฀ Core฀modules฀are฀controllers฀first฀and฀reference฀ Figure฀1.11:฀Rabbit฀Semiconductor฀has฀ designs฀second. a฀core฀module฀for฀every฀occasion. Figure฀1.11฀shows฀a฀sample฀of฀the฀array฀of฀ available฀core฀modules฀available฀from฀Rabbit฀ Semiconductor.฀As฀of฀the฀time฀of฀this฀writing,฀Rabbit฀offers฀ten฀core฀modules,฀and฀has฀more฀ in฀development. These฀are฀physically฀small,฀inexpensive฀and฀easily฀plugged฀into฀an฀application-specific฀carrier฀ board.฀Rabbit฀core฀modules฀are฀great฀for฀fast฀product฀development.฀The฀large฀assortment฀of฀ available฀products฀ensures฀there฀is฀a฀core฀module฀with฀features฀for฀almost฀any฀application. 1.3.4฀Chip฀Solutions Chip฀level฀solutions฀comprise฀the฀last฀class฀of฀controllers.฀The฀complexity฀available฀varies฀ from฀very฀simple฀to฀extremely฀complex.฀All฀chip฀level฀solutions฀require฀a฀PCB฀to฀be฀designed.฀Most฀chips฀don’t฀offer฀rugged฀I/O.฀The฀design฀must฀protect฀the฀delicate฀ICs฀against฀ heat,฀cold,฀humidity,฀Electro฀Static฀Discharge฀(ESD),฀over-voltage,฀back฀Electro฀Magnetic฀ Force฀(EMF),฀brown-outs,฀Electro฀Magnetic฀Interference฀(EMI)฀and฀other฀forms฀of฀abuse. Traditionally,฀engineers฀have฀classified฀computational฀chips฀as฀being฀either฀microprocessors฀ or฀microcontrollers.฀This฀classification฀isn’t฀particularly฀useful,฀and฀as฀technology฀has฀progressed,฀has฀become฀outmoded.฀ The฀classic฀differentiation฀between฀microcontrollers฀and฀microprocessors฀has฀been฀on-chip฀ ROM.฀Microcontrollers฀have฀onboard฀ROM,฀microprocessors฀don’t.฀Over฀time,฀integrated฀I/O฀ has฀also฀come฀to฀be฀associated฀with฀microcontrollers. The฀Intel฀8051฀is฀possibly฀the฀most฀widely฀recognized฀microcontroller฀architecture.฀In฀addition฀to฀a฀modest฀amount฀of฀ROM฀and฀RAM,฀this฀Harvard฀architecture฀sports฀built-in฀serial฀ ports,฀counter/timers฀and฀a฀number฀of฀digital฀I/O฀pins.฀ The฀8051฀has฀been฀so฀successful฀that฀companies฀besides฀Intel฀have฀now฀released฀8051฀cores฀ surrounded฀by฀on-chip฀peripherals฀such฀as฀analog-to-digital฀converters฀(ADCs),฀control฀area฀ network฀(CAN)฀interfaces,฀universal฀serial฀bus฀(USB)฀ports฀and฀others.฀Some฀of฀the฀8051฀ variants฀permit฀or฀even฀require฀external฀memory.฀The฀lack฀of฀internal฀ROM฀in฀some฀of฀these฀ variants฀technically฀makes฀them฀microprocessors.

13

Chapter฀1 Today,฀microprocessors฀routinely฀include฀peripherals฀that฀have฀long฀been฀identified฀with฀ microcontrollers.฀Consider฀the฀Rabbit฀3000฀processor,฀with฀six฀serial฀ports,฀real-time฀clock,฀ internal฀watchdog,฀built-in฀pulse฀width฀modulator฀(PWM),฀quadrature฀decoding฀and฀56฀digital฀ I/O฀pins.฀The฀requirement฀for฀external฀memory฀makes฀it฀a฀microprocessor.฀The฀Rabbit’s฀I/O฀ feature฀set฀makes฀it฀more฀identifiable฀with฀control฀applications฀than฀as฀a฀numeric฀processor. The฀technology฀that฀further฀blurs฀the฀old฀microprocessor/microcontroller฀classification฀scheme฀ is฀the฀field฀programmable฀gate฀array฀(FPGA).฀Popular฀CPU฀designs฀are฀available฀as฀FPGAbased฀soft฀cores.฀Memory฀can฀be฀provided฀on-chip฀or฀off.฀Some฀FPGA฀families฀include฀ SRAM฀cells฀that฀can฀be฀ganged฀and฀initialized฀at฀boot฀to฀provide฀program฀storage฀While฀ FPGAs฀are฀neither฀microprocessors฀nor฀microcontrollers,฀they฀can฀be฀configured฀as฀either. Originally,฀the฀concept฀of฀a฀microcontroller฀was฀intended฀to฀convey฀the฀idea฀of฀a฀“single฀chip฀ solution.”฀This฀looked฀great฀in฀marketing฀brochures,฀but฀seldom฀worked฀out฀in฀copper.฀The฀ delicate฀nature฀of฀the฀I/O฀pins฀on฀most฀microcontrollers฀required฀additional฀interface฀ICs,฀ as฀did฀power฀conditioning฀and฀reset฀management.฀ All฀but฀the฀simplest฀chip-level฀applications฀required฀ multi-IC฀designs.฀ The฀trend฀in฀chip฀level฀design฀is฀to฀make฀parts฀as฀ small฀as฀possible.฀This,฀combined฀with฀the฀demand฀ for฀high฀I/O฀count,฀and฀therefore฀pin฀count,฀has฀ pushed฀IC฀manufacturers฀to฀offer฀processors฀in฀surface฀mount฀(SMT)฀packages.฀Figure฀1.12฀shows฀three฀ Rabbit฀Semiconductor฀microprocessors.฀These฀are฀all฀ SMT฀packages.฀The฀Rabbit฀2000฀is฀the฀largest฀package฀and฀is฀the฀least฀powerful฀part.฀The฀Rabbit฀3000฀ Figure฀1.12:฀The฀Rabbit฀2000฀and฀ is฀offered฀in฀two฀packages.฀The฀smallest฀package฀is฀ Rabbit฀3000฀processors฀have฀enough฀ a฀ball฀grid฀array฀(BGA)฀and฀is฀shown฀on฀the฀left฀of฀ I/O฀that฀only฀SMT฀packaging฀is฀offered. Figure฀1.12.฀

1.4฀Software฀Development฀Tools Just฀as฀silicon฀has฀advanced,฀so฀have฀software฀development฀techniques.฀The฀old฀days฀of฀ writing฀code฀on฀punch฀cards,฀toggling฀in฀binary฀bootstrap฀loaders฀or฀keying฀in฀hexadecimal฀ opcodes฀are฀long฀gone.฀The฀tried,฀true฀and฀tiresome฀technique฀of฀“burn฀and฀learn”฀is฀still฀with฀ us,฀but฀in฀a฀greatly฀reduced฀capacity.฀Most฀applications฀are฀developed฀using฀assemblers,฀compilers,฀linkers,฀loaders,฀simulators,฀emulators,฀EPROM฀programmers฀and฀debuggers. Selecting฀software฀development฀tools฀suited฀to฀a฀particular฀project฀is฀important฀and฀complex.฀ Bad฀tool฀choices฀can฀greatly฀extend฀development฀times.฀Tools฀can฀cost฀thousands฀of฀dollars฀ per฀developer,฀but฀the฀payoff฀can฀be฀justifiable฀because฀of฀increased฀productivity.฀On฀the฀other฀ hand,฀initial฀tool฀choice฀can฀adversely฀affect฀the฀product’s฀maintainability฀years฀down฀the฀road. For฀example,฀deciding฀to฀use฀JAVA฀to฀develop฀code฀for฀a฀PIC®฀microcontroller฀in฀a฀coffee฀ maker฀is฀a฀poor฀choice.฀While฀there฀are฀tools฀available฀to฀do฀this,฀and฀programmers฀willing฀to฀ do฀this,฀code฀maintenance฀is฀likely฀to฀be฀an฀issue.฀Once฀the฀JAVA-wizard฀programmer฀moves฀

14

Introduction on฀to฀developing฀code฀for฀websites,฀it฀may฀be฀difficult฀to฀find฀another฀JAVA-enabled฀programmer฀willing฀to฀sustain฀embedded฀code฀for฀a฀coffee฀maker.฀Equally฀silly฀would฀be฀to฀use฀an฀ assembler฀to฀write฀a฀full-up฀GUI฀(graphical฀user฀interface)-based฀MMI.฀ A฀quick฀trip฀to฀the฀Embedded฀Systems฀Conference฀will฀reveal฀a฀wide฀array฀of฀development฀ tools.฀Many฀of฀these฀are฀ill฀suited฀for฀embedded฀development,฀if฀not฀for฀reasons฀of฀scale฀or฀ cost,฀then฀for฀reasons฀of฀code฀maintainability฀or฀tool฀stability. The฀two฀time-tested฀industry-approved฀solutions฀for฀embedded฀development฀are฀assembly฀ and฀C.฀Forth,฀BASIC,฀JAVA,฀PLM,฀Pascal,฀UML,฀XML฀and฀a฀plethora฀of฀other฀obscure฀ languages฀have฀been฀used฀to฀produce฀functioning฀systems.฀However,฀for฀low-level฀fast฀code,฀ such฀as฀Interrupt฀Service฀Routines฀(ISRs),฀assembly฀is฀the฀only฀real฀option.฀For฀high-level฀ coding,฀C฀is฀the฀best฀choice฀due฀to฀the฀availability฀of฀software฀engineers฀that฀know฀the฀language฀and฀the฀wide฀variety฀of฀available฀libraries. Selecting฀a฀tool฀vendor฀is฀almost฀as฀important฀as฀selecting฀a฀language.฀Selecting฀a฀tool฀vendor฀ without฀a฀proven฀track฀record฀is฀a฀risk.฀If฀the฀tool฀proves฀problematic,฀good฀tech-support฀will฀ be฀required.฀ This฀in฀no฀way฀implies฀that฀a฀small฀shop฀isn’t฀capable฀of฀producing฀an฀excellent฀tool฀and฀providing฀responsive฀support.฀For฀example,฀Softools฀Inc.฀is฀a฀firm฀with฀relatively฀low฀headcount฀ and฀an฀outstanding฀record฀of฀fast฀and฀comprehensive฀technical฀support.฀Their฀ANSI฀compliant฀C฀compiler฀for฀the฀Rabbit฀microprocessor฀is฀a฀solid฀tool฀worthy฀of฀consideration฀for฀any฀ Rabbit-based฀project.฀ Sometimes,฀tool฀vendors฀are฀little฀more฀than฀new฀college฀grads฀trying฀to฀pawn฀off฀warmedover฀senior฀projects฀as฀development฀tools.฀While฀the฀tools฀may฀look฀good,฀dealing฀with฀such฀ an฀embryonic฀company฀is฀a฀risk. Public฀domain฀tools฀have฀uncertain฀histories฀and฀no฀guarantee฀of฀support.฀The฀idea฀behind฀ open฀source฀tools฀is฀that฀if฀support฀is฀needed,฀the฀user฀can฀tweak฀the฀tool’s฀code-base฀to฀force฀ the฀tool฀to฀behave฀as฀desired.฀For฀some฀engineers,฀this฀is฀a฀fine฀state฀of฀affairs.฀On฀the฀other฀ hand,฀many฀embedded฀software฀engineers฀may฀not฀know,฀or฀even฀desire฀to฀know,฀how฀to฀ tweak,฀for฀example,฀a฀backend฀code฀generator฀on฀a฀compiler. Rabbit฀Semiconductor฀and฀Z-World฀offer฀a฀unique฀solution฀to฀the฀tool฀dilemma฀facing฀ embedded฀systems฀designers.฀Rabbit฀Semiconductor฀designs฀ICs฀and฀core฀modules.฀Z-World฀ designs฀board-level฀and฀packaged฀controllers฀based฀on฀Rabbit฀chips.฀Both฀companies฀share฀ the฀development฀and฀maintenance฀of฀Dynamic฀C™. Dynamic฀C฀offers฀the฀developer฀an฀integrated฀development฀environment฀(IDE)฀where฀C฀and฀ assembly฀can฀be฀written฀and฀blended.฀Once฀an฀application฀is฀coded,฀Dynamic฀C฀will฀download฀the฀executable฀image฀to฀the฀target฀system฀over฀a฀serial฀cable.฀Debugging฀tools฀such฀as฀ single฀stepping,฀break฀points,฀and฀watch-windows฀are฀provided฀within฀the฀IDE,฀without฀the฀ need฀for฀an฀expensive฀In-Circuit฀Emulator฀(ICE). Between฀Z-World฀and฀Rabbit฀Semiconductor,฀all฀four฀classes฀of฀controllers฀are฀available฀฀ as฀well฀as฀a฀complete฀set฀of฀highly฀integrated฀development฀tools.฀Libraries฀support฀a฀file฀ 15

Chapter฀1 system,฀Compact฀Flash฀interfaces,฀TCP/IP,฀IrDA,฀SDLC/HDLC,฀SPI,฀I2C,฀AES,฀FFTs,฀and฀฀ the฀uCOS/II฀RTOS. On฀of฀the฀most฀attractive฀features฀of฀Dynamic฀C฀is฀that฀the฀TCP/IP฀stack฀is฀royalty฀free.฀This฀ is฀unusual฀in฀the฀embedded฀industry,฀where฀companies฀are฀charging฀thousands฀of฀dollars฀ for฀TCP/IP฀support.฀If฀TCP/IP฀is฀required฀for฀an฀application,฀the฀absence฀of฀royalties฀makes฀ Dynamic฀C฀a฀very฀attractive฀tool.

1.5฀Design฀Trade-offs The฀stock฀in฀trade฀of฀engineers฀is฀making฀trade-offs.฀An฀embedded฀systems฀designer฀has฀ many฀options฀to฀weigh.฀Striking฀a฀balance฀for฀a฀given฀project฀depends฀primarily฀on฀the฀anticipated฀production฀volume฀of฀a฀project. A฀product฀that฀is฀going฀to฀be฀manufactured฀by฀the฀millions฀will฀have฀an฀entirely฀different฀set฀ of฀trade-offs฀than฀a฀product฀that฀is฀going฀to฀be฀manufactured฀in฀the฀thousands.฀Low฀volume฀ production฀has฀yet฀a฀different฀set฀of฀trade-offs. Something฀that฀will฀have฀high฀production฀volumes฀like฀a฀cell฀phone฀will฀be฀optimized฀for฀the฀ lowest฀possible฀manufacturing฀costs.฀Up฀front฀tooling฀for฀packaging฀will฀be฀more฀than฀paid฀ for฀by฀the฀high฀volume฀sales.฀Custom฀liquid฀crystal฀displays฀(LCDs)฀will฀be฀made.฀Application-specific฀integrated฀circuits฀(ASICs)฀may฀be฀considered. A฀product฀produced฀in฀medium฀quantities,฀say฀the฀tens-of-thousands,฀will฀almost฀certainly฀ be฀designed฀using฀surface฀mount฀technology฀(SMT).฀Designers฀will฀probably฀stay฀away฀from฀ using฀off-the-shelf฀controllers฀that฀come฀prepackaged฀or฀as฀a฀printed฀circuit฀board฀(PCB).฀ Spending฀the฀time฀up฀front฀to฀develop฀or฀license฀a฀controller฀design฀will฀pay฀off฀over฀the฀ course฀of฀product’s฀lifetime฀in฀manufacturing.฀ASICs฀probably฀won’t฀be฀cost฀effective฀because฀the฀required฀minimum฀quantities฀won’t฀be฀reached.฀This฀type฀of฀product฀will฀benefit฀ from฀a฀chip-level฀solution. A฀product฀produced฀in฀low฀quantities฀(up฀to฀hundreds฀of฀units)฀has฀a฀number฀of฀other฀challenges.฀Many฀surface฀mount฀parts฀are฀available฀only฀on฀reels฀of฀thousands฀of฀parts.฀Many฀ small,฀low฀volume฀products฀benefit฀from฀a฀ready-made฀embedded฀controller. This฀book฀deals฀with฀projects฀that฀will฀be฀produced฀in฀low฀to฀medium฀quantities. Many฀products฀that฀aspire฀to฀medium฀quantity฀production฀are฀forced฀by฀market฀conditions฀ to฀start฀out฀being฀produced฀as฀low฀quantity฀products.฀Over฀the฀years฀this฀has฀been฀addressed฀ primarily฀in฀two฀ways,฀neither฀of฀which฀has฀been฀entirely฀satisfactory. Some฀companies฀have฀opted฀to฀design฀the฀products฀initially฀for฀low฀volume฀production.฀Once฀ market฀demand฀justifies฀higher฀production฀volumes,฀the฀product฀is฀redesigned฀for฀medium฀ production฀volumes.฀This฀approach฀incurs฀two฀design฀cycles฀and฀the฀associated฀costs฀and฀risks. Other฀companies฀have฀opted฀to฀design฀the฀products฀initially฀for฀higher฀volume฀production.฀ This฀approach฀costs฀more฀time฀and฀money฀up฀front฀in฀the฀design฀and฀during฀the฀low-volume฀ phase฀of฀the฀product’s฀life,฀thereby฀increasing฀manufacturing฀costs.

16

Introduction

1.6฀Migration฀to฀Higher฀Volume฀Production If฀the฀initial฀design฀incorporated฀purchased฀embedded฀controllers,฀then฀migration฀to฀fully฀ assembled฀custom฀boards฀is฀likely฀to฀be฀arduous.฀Eliminating฀the฀off-the-shelf฀controller฀from฀ the฀new฀design฀will฀reduce฀production฀costs฀simply฀because฀the฀company฀is฀no฀longer฀paying฀the฀markup฀on฀the฀parts฀and฀the฀“value฀added”฀by฀the฀controller฀vendor฀for฀the฀purchased฀ controller.฀However,฀if฀care฀is฀not฀taken,฀bugs฀can฀be฀introduced฀into฀the฀new฀design.฀Seldom฀ do฀off-the-shelf฀controller฀companies฀provide฀complete฀IP฀to฀support฀this฀type฀of฀migration. In฀many฀cases,฀the฀migration฀phase฀is฀never฀completed.฀The฀product฀is฀simply฀produced฀in฀ higher฀quantities฀with฀the฀low฀volume฀design.฀The฀company฀loses฀the฀opportunity฀to฀capitalize฀on฀the฀intrinsic฀savings฀offered฀by฀higher฀volume฀production฀techniques. The฀second฀approach฀to฀the฀migration฀problem฀has฀been฀for฀companies฀to฀spend฀the฀time฀and฀ money฀up฀front฀to฀design฀the฀product฀specifically฀for฀medium฀production฀quantities.฀This฀ approach฀often฀extends฀the฀initial฀development฀time—sometimes฀by฀months.฀ An฀important฀consideration฀for฀many฀projects฀is฀“time-to-market.”฀Being฀able฀to฀provide฀a฀ working฀proof฀of฀concept฀for฀investors฀or฀a฀“prototype”฀for฀a฀potential฀customer฀often฀will฀ make฀or฀break฀a฀project—or฀company. This฀approach฀will฀incur฀decidedly฀higher฀manufacturing฀costs฀while฀the฀product฀is฀in฀the฀low฀ volume฀production฀phase.฀ The฀first฀approach฀puts฀off฀development฀issues฀and฀costs฀until฀a฀product฀reaches฀a฀sufficient฀ level฀of฀production฀to฀warrant฀redesign.฀But฀design฀issues฀and฀reverse฀engineering฀of฀off-theshelf฀embedded฀controllers฀can฀sandbag฀the฀migration฀phase.฀The฀second฀approach฀spends฀all฀ the฀money฀and฀time฀upfront฀to฀develop฀a฀product฀ill฀suited฀to฀the฀manufacturing฀scale฀that฀it฀ will฀be฀confined฀to฀for฀the฀first฀portion฀of฀its฀life. Neither฀solution฀is฀wholly฀satisfactory. Several฀year฀ago฀a฀pioneering฀embedded฀controller฀company,฀Z-World฀of฀Davis,฀California,฀ took฀a฀long฀hard฀look฀at฀how฀to฀help฀customers฀develop฀their฀applications฀quickly฀and฀in฀a฀ fashion฀suited฀to฀low฀production฀techniques,฀while฀affording฀the฀same฀customers฀a฀seamless฀ migration฀path฀to฀higher฀quantity฀fully฀custom฀designs.฀The฀solution฀was฀dubbed฀the฀“Smart฀ Core™.”฀Thus,฀the฀core฀module฀solution฀was฀born. A฀core฀module฀is฀a฀small฀PCB฀containing฀a฀CPU,฀memory฀subsystem,฀reset฀and฀watchdog฀ circuits,฀power฀supervisors,฀and฀firmware.฀The฀core฀module฀provides฀I/O฀signals฀on฀headers฀ suitable฀for฀insertion฀into฀socket฀strips฀on฀the฀customer’s฀target฀system.฀Thus,฀a฀developer฀can฀ purchase฀a฀fully฀functional฀controller฀and฀immediately฀plug฀it฀into฀the฀application. When฀the฀customer฀decides฀to฀migrate฀to฀a฀medium฀volume฀production฀board,฀Z-World฀will฀ license฀all฀the฀IP฀associated฀with฀the฀core฀module฀to฀the฀customer฀for฀inclusion฀in฀the฀new฀ “single฀board”฀or฀integrated฀design.฀This฀provides฀a฀smooth฀low-risk฀transition฀for฀the฀designer฀between฀the฀low฀volume฀production฀and฀medium฀volume฀production฀phase.

17

Chapter฀1 Seeing฀the฀need฀for฀improved฀microprocessors฀in฀the฀embedded฀market,฀Z-World’s฀CEO,฀ Norm฀Rogers,฀founded฀Rabbit฀Semiconductor฀to฀carry฀on฀innovation฀in฀the฀embedded฀controller฀industry฀at฀the฀chip฀level.฀ Today,฀Rabbit฀Semiconductor฀produces฀chips฀and฀core-module฀designs.฀Rabbit฀is฀built฀on฀฀ Z-World’s฀experience฀in฀the฀embedded฀systems฀arena.฀This฀allows฀Rabbit฀to฀tailor฀IC฀designs฀ to฀meet฀the฀needs฀of฀the฀embedded฀system฀designer.฀Painstaking฀research฀goes฀in฀Rabbit฀chips฀ to฀optimize฀them฀for฀embedded฀C฀code.฀The฀Rabbit’s฀I/O฀mix฀is฀based฀on฀Z-World’s฀decades฀ of฀market฀experience. Z-World฀continues฀as฀a฀force฀in฀the฀embedded฀controller฀industry.฀It฀offers฀the฀Rabbit฀core฀ modules,฀but฀the฀focus฀is฀on฀full-featured฀controllers.฀These฀controllers฀offer฀the฀embedded฀system฀designer฀a฀smorgasbord฀of฀quarter-VGA฀touch-screens,฀high-current฀I/O฀points,฀ protected฀digital฀inputs,฀analog฀inputs,฀analog฀outputs,฀industrial฀relays,฀and฀all฀manner฀of฀ communication฀busses.฀These฀types฀of฀controllers฀can฀often฀be฀used฀to฀provide฀a฀complete฀ control฀solution฀for฀low฀volume฀applications.฀ Embedded฀system฀designers฀can฀now฀purchase฀Rabbit฀microprocessors,฀Rabbit-based฀core฀ modules฀or฀full-featured฀controllers฀from฀the฀Rabbit/Z-World฀fraternity.฀All฀of฀these฀products฀ share฀common฀development฀tools฀and฀IP.฀This฀makes฀design฀migrations฀or฀new฀design฀variants฀quick,฀easy,฀low฀cost฀and฀low฀risk. The฀next฀chapter฀examines฀how฀the฀Rabbit฀solution฀suite฀stacks฀up฀against฀the฀many฀solutions฀ available฀today.

18

2

CHAPTER฀

The฀Basics 2.1฀Evaluating฀Controllers Embedded฀system฀design฀boils฀down฀to฀monitoring฀sensors฀and฀actuating฀devices.฀Depending฀ on฀the฀complexity฀of฀the฀desired฀behavior,฀an฀embedded฀controller฀may฀not฀be฀required. In฀some฀cases฀a฀sensor฀may฀be฀adequate฀to฀control฀the฀actuator.฀In฀these฀situations,฀controllers฀ are฀redundant.฀For฀example,฀a฀household฀light฀switch฀can฀directly฀control฀a฀lamp.฀These฀types฀ of฀systems฀are฀not฀embedded฀systems. Some฀systems฀require฀control฀logic,฀but฀not฀necessarily฀a฀microprocessor฀or฀microcontroller.฀ If฀the฀system’s฀desired฀behavior฀can฀be฀implemented฀with฀simple฀combinatorial฀logic,฀then฀ the฀system฀is฀not฀considered฀an฀embedded฀system. If฀the฀controller฀requires฀sequential฀logic฀then฀the฀application฀may฀rightfully฀be฀called฀an฀ embedded฀system.฀After฀all,฀a฀microprocessor฀is฀just฀a฀glorified฀finite฀state฀machine฀(FSM). For฀example,฀a฀digital฀lock฀may฀be฀implemented฀with฀an฀FSM.฀For฀many฀years,฀a฀programmable฀logic฀device฀(PLD)฀was฀the฀usual฀tool฀for฀building฀an฀FSM.฀In฀the฀past,฀PLDs฀were฀ dramatically฀less฀expensive฀than฀microcontrollers.฀However,฀microcontroller฀prices฀have฀ dropped฀to฀the฀level฀that฀many฀engineers฀have฀switched฀from฀PLDs฀to฀small฀microcontrollers฀ even฀for฀simple฀FSM฀designs.฀The฀simplicity฀of฀programming฀a฀microcontroller฀outweighs฀ the฀ever฀shrinking฀cost฀advantage฀a฀PLD฀may฀have.฀As฀PLDs฀age,฀obsolescence฀is฀a฀concern. Many฀factors฀bear฀on฀the฀selection฀of฀a฀controller.฀Performance,฀cost฀and฀availability฀are฀the฀ most฀often฀bandied฀factors.฀Development฀tools,฀time-to-market฀and฀product฀sustainability฀ must฀also฀be฀considered.฀ 2.1.1฀Performance฀and฀Cost The฀overarching฀criterion฀when฀selecting฀an฀embedded฀controller฀is฀performance.฀If฀a฀controller฀cannot฀handle฀the฀required฀task,฀then฀that฀controller฀must฀be฀discarded.฀ A฀close฀second฀to฀performance฀is฀cost.฀If฀a฀controller฀is฀qualified฀but฀too฀expensive,฀then฀it฀฀ is฀a฀poor฀solution.฀A฀mantra฀for฀engineers฀must฀be฀“If฀we฀can’t฀afford฀the฀solution,฀then฀it’s฀ not฀a฀solution.” Comparing฀the฀performance฀and฀features฀of฀competing฀controllers฀is฀almost฀always฀an฀apples-฀ to-oranges฀comparison.฀I/O฀mixes฀in฀competing฀models฀are฀seldom฀equivalent.฀Some฀controllers฀have฀internal฀memory,฀some฀external,฀and฀some฀both.฀Devices฀with฀internal฀memory฀may฀ have฀different฀programming฀models.฀Comparing฀controllers,฀chip฀level฀or฀board฀level,฀is฀neither฀simple฀nor฀scientific.฀Selecting฀a฀controller฀is฀an฀exercise฀in฀the฀art฀of฀risk฀management. 19

Chapter฀2 Consider฀the฀case฀of฀a฀simple฀application฀requiring฀only฀a฀few฀hundred฀assembly฀instructions฀ and฀ten฀I/O฀pins฀with฀an฀anticipated฀production฀run฀of฀1000฀units.฀The฀application฀could฀be฀ implemented฀in฀a฀small฀FPGA฀(Field฀Programmable฀Gate฀Array)฀as฀a฀big฀FSM.฀The฀application฀could฀be฀implemented฀with฀a฀small฀microcontroller฀like฀a฀PIC฀processor฀from฀Microchip฀ Technologies,฀or฀with฀an฀AVR®฀from฀Atmel.฀The฀application฀could฀also฀be฀implemented฀with฀ a฀desktop฀PC฀by฀using฀the฀PC’s฀parallel฀printer฀port. The฀FPGA฀solution฀is฀overly฀difficult฀for฀most฀engineers,฀and฀FPGAs฀are฀somewhat฀pricey.฀If฀the฀ project฀is฀a฀“one-off,”฀the฀PC-based฀solution฀is฀perhaps฀viable.฀Assuming฀that฀the฀application฀will฀ require฀a฀production฀run฀of฀a฀thousand฀units,฀the฀PIC฀or฀AVR฀solution฀looks฀more฀attractive. For฀the฀sake฀of฀this฀example,฀assume฀the฀AVR฀AT90S1200฀solution฀is฀$0.75฀more฀expensive฀ than฀a฀PIC16C55-based฀solution.฀If฀each฀part฀is฀equally฀capable฀of฀performing฀the฀control฀ task,฀which฀part฀should฀be฀chosen?฀The฀cheaper? The฀answer฀is฀“it฀depends.”฀The฀AVR฀and฀PIC฀solutions฀are฀not฀apples-to-apples.฀Table฀2.1฀ lists฀some฀features฀of฀each฀part.฀The฀most฀glaring฀difference฀is฀in฀the฀programming฀model.฀ The฀PIC฀solution฀is฀OTP฀(one฀time฀programmable)฀while฀the฀AVR฀part฀is฀Flash-based. Table฀2.1:฀PIC฀and฀AVR฀have฀similar฀features. Feature Digital฀I/O฀pins On฀board฀program฀space฀(words) On฀board฀data฀space฀(bytes) EEPROM฀(bytes) Counter/timers External฀Interrupts Max฀clock฀speed Machine฀Cycles฀per฀instruction Clock฀cycles฀per฀machine฀cycle฀ In฀System฀Programmable Programming฀model

PIC16C55 12 512 24 0 1 0 40฀MHz 1฀or฀2 4 Yes OTP

AVR฀AT90S1200 15 512 0 64 1 1 12 1฀or฀2 1 Yes Flash-based

The฀question฀that฀the฀designer฀must฀answer฀is:฀will฀the฀extra฀$0.75฀for฀the฀AVR’s฀Flash-based฀ programming฀model฀buy฀anything฀useful฀for฀the฀project?฀Two฀facets฀of฀the฀product’s฀life฀ cycle฀need฀to฀be฀considered—product฀development,฀and฀production. During฀product฀development,฀code฀must฀be฀written฀and฀loaded฀into฀a฀target฀system.฀If฀OTP฀ parts฀are฀used,฀either฀the฀IC฀must฀be฀socketed฀in฀the฀development฀platform,฀or฀the฀project฀ engineers฀must฀be฀experts฀at฀soldering/removing฀ICs฀in฀the฀target฀system.฀Another฀approach฀ to฀developing฀for฀OTP฀devices฀is฀to฀use฀an฀in-circuit-emulator฀(ICE). An฀ICE฀allows฀executable฀code฀to฀be฀loaded฀into฀it฀from฀a฀host฀platform,฀usually฀a฀PC.฀The฀ ICE฀also฀plugs฀into฀the฀target฀system,฀usually฀in฀a฀socket฀intended฀for฀the฀actual฀microcontroller฀or฀microprocessor.฀The฀ICE฀executes฀the฀code฀while฀wiggling฀the฀signals฀on฀the฀target฀ system฀just฀like฀the฀actual฀microcontroller฀would. 20

The฀Basics For฀simple฀devices฀like฀the฀PIC16C55,฀a฀number฀of฀excellent฀ICE฀tools฀are฀available,฀most฀ under฀$1000.฀For฀faster฀processors,฀the฀authenticity฀of฀the฀signal฀wiggling฀can฀be฀compromised฀by฀how฀the฀ICE฀connects฀to฀the฀target’s฀socket.฀For฀complicated฀processors,฀an฀ICE฀ may฀not฀be฀able฀to฀emulate฀the฀target฀processor฀in฀real฀time.฀However,฀for฀our฀PIC-based฀ example,฀an฀ICE฀is฀a฀reasonable฀solution. If฀the฀product฀has฀multiple฀developers฀working฀on฀it,฀chances฀are฀that฀each฀engineer฀will฀want฀ an฀ICE.฀This฀may฀mean฀spending฀several฀thousand฀dollars฀“extra”฀on฀emulators฀for฀the฀development฀team. Even฀if฀the฀product฀only฀has฀one฀developer,฀if฀the฀ICE฀costs฀$750฀then฀the฀PIC’s฀savings฀over฀ the฀AVR฀are฀negated.฀The฀anticipated฀production฀was฀only฀one฀thousand฀units.฀Of฀course,฀ after฀the฀development฀is฀done,฀the฀engineering฀group฀will฀still฀have฀the฀ICE.฀This฀may฀or฀ may฀not฀be฀useful,฀depending฀on฀whether฀a฀similar฀flavor฀of฀PIC฀microcontroller฀is฀used฀for฀a฀ subsequent฀project. The฀production฀phase฀of฀the฀product’s฀life฀may฀benefit฀from฀the฀AVR’s฀Flash-based฀ISP฀program฀memory฀if฀a฀bug฀is฀discovered.฀Bugs฀crop฀up฀in฀code฀all฀the฀time.฀Industry฀experts฀like฀ to฀play฀at฀quantifying฀how฀many฀bugs฀can฀be฀expected฀in฀a฀project฀given฀the฀number฀of฀lines฀ of฀code.฀Let’s฀say฀that฀based฀on฀experience฀or฀trade฀publications฀or฀industry฀studies฀(or฀which฀ way฀the฀wind฀blows฀in฀Siberia),฀we฀believe฀we฀can฀expect฀one฀serious฀bug฀for฀every฀three฀ hundred฀lines฀of฀assembly฀code฀initially฀released฀to฀production. Initially,฀we฀stated฀that฀the฀application฀would฀require฀a฀few฀hundred฀instructions.฀That฀implies฀ that฀we฀believe฀that฀we฀can฀expect฀one฀serious฀bug฀to฀be฀in฀the฀code฀released฀to฀production.฀ Now,฀that฀doesn’t฀mean฀that฀there฀will฀be฀a฀bug.฀It฀just฀means฀that฀we฀think฀there฀is฀likely฀to฀ be฀a฀bug. If฀the฀Flash-based฀AVR฀is฀used฀in฀production฀units฀and฀a฀bug฀is฀found,฀then฀the฀existing฀ inventory฀of฀assembled฀boards฀can฀be฀reprogrammed฀without฀having฀to฀desolder฀parts.฀Units฀ already฀deployed฀may฀be฀field฀upgraded฀if฀they฀were฀designed฀to฀be.฀RMAs฀(returned฀materials฀authorized)฀can฀be฀reprogrammed฀quickly฀without฀soldering. Reprogramming฀the฀AVR฀in฀circuit฀may฀require฀the฀addition฀of฀an฀extra฀header฀and฀a฀few฀ passive฀components฀to฀the฀PCB.฀As฀of฀the฀year฀2004,฀component฀insertion฀costs฀are฀running฀ around฀$0.12.฀If฀four฀extra฀components฀(a฀header฀and฀three฀resistors)฀are฀required฀to฀reprogram฀the฀AVR,฀then฀the฀$0.75฀cost฀increase฀of฀the฀AVR฀over฀the฀PIC฀is฀further฀increased฀by฀ $0.12฀×฀4฀=฀$0.48฀plus฀the฀cost฀of฀the฀four฀extra฀components฀(assume฀$0.09). For฀the฀production฀phase฀of฀the฀product฀to฀see฀the฀benefits฀of฀the฀AVR’s฀ISP฀flash,฀the฀product฀ cost฀must฀increase฀over฀the฀PIC฀solution฀by฀$0.75฀+฀$0.48฀+฀$0.09฀=฀$1.35. An฀innovative฀engineer฀might฀be฀able฀to฀eliminate฀the฀need฀for฀the฀extra฀components฀by฀ designing฀or฀buying฀a฀clip-on฀programmer.฀This฀may฀add฀cost฀to฀the฀product฀development฀ phase฀for฀the฀research฀to฀identify,฀purchase฀and฀test฀the฀clip-on฀programmer. In฀this฀example,฀the฀engineer฀selecting฀the฀processor฀may฀well฀reason฀that฀the฀75฀cent฀ or฀$1.35฀difference฀in฀parts฀cost฀over฀1,000฀projected฀units฀is฀worth฀having฀the฀ability฀to฀ reprogram฀defective฀controllers฀or฀upgrade฀existing฀products฀with฀new฀features.฀The฀engineer฀

21

Chapter฀2 may฀also฀feel฀that฀the฀AVR’s฀ISP฀flash฀will฀eliminate฀the฀need฀for฀an฀ICE,฀thus฀saving฀more฀ money.฀This฀engineer฀would฀select฀the฀AVR. Another฀engineer฀may฀decide฀that฀the฀debugging฀features฀of฀an฀ICE฀are฀indispensable฀for฀ developers฀and฀therefore฀the฀cost฀of฀an฀ICE฀for฀the฀PIC฀solution฀would฀be฀balanced฀by฀the฀ cost฀of฀an฀ICE฀for฀the฀AVR.฀This฀engineer฀may฀further฀reason฀that฀the฀production฀projection฀ of฀1000฀units฀is฀likely฀to฀be฀wrong฀and฀that฀many฀more฀units฀will฀be฀built.฀Hence,฀reducing฀ per-unit฀cost฀becomes฀more฀important.฀Furthermore,฀he฀may฀gamble฀that฀released฀code฀will฀ be฀bug-free.฀This฀engineer฀would฀select฀the฀PIC. In฀even฀this฀simplest฀of฀examples,฀two฀chains฀of฀thought฀lead฀to฀different฀conclusions.฀The฀ selection฀of฀a฀controller฀is฀seldom฀a฀“cut฀and฀dried”฀process.฀It฀is฀an฀exercise฀in฀trade-offs฀to฀ manage฀risk.฀ 2.1.2฀Performance฀by฀Benchmark Attempts฀have฀been฀made฀to฀provide฀comparative฀rankings฀of฀processor฀performance.฀Benchmark฀algorithms฀exist.฀Most฀were฀developed฀to฀compare฀performance฀of฀desktop,฀mini฀or฀ mainframe฀computers.฀The฀apples-to-oranges฀world฀of฀embedded฀platforms฀makes฀traditional฀ benchmarks฀all฀but฀irrelevant. Benchmarks฀are฀useful฀if฀the฀hardware฀platforms฀are฀similar.฀When฀purchasing฀a฀desktop฀PC฀ it฀might฀be฀useful฀to฀know฀how฀fast฀a฀numeric฀simulation฀or฀a฀CAD฀rendering฀package฀or฀ even฀a฀graphics฀intensive฀game฀will฀run.฀Embedded฀platforms฀are฀so฀dramatically฀different฀ that฀developing฀a฀meaningful฀benchmark฀that฀will฀run฀“identically”฀on฀dissimilar฀hardware฀ platforms฀is฀nearly฀impossible. The฀Internet฀newsgroups,฀such฀as฀comp.arch.embedded,฀are฀riddled฀with฀tedious฀pedantic฀discussions฀of฀the฀pros฀and฀cons฀of฀benchmarks.฀Flame฀wars฀abound฀regarding฀the฀legitimacy฀of฀ how฀some฀group฀or฀another฀implemented฀a฀so-called฀“standard”฀benchmark฀algorithm.฀This฀ polemic฀nattering฀can฀sound฀all฀hoity-toity,฀but฀the฀reality฀is฀that฀choosing฀a฀control฀solution฀ is฀not฀something฀that฀can฀be฀done฀with฀benchmarks. Digital฀signal฀processing฀(DSP)฀is฀the฀one฀area฀where฀specialized฀and฀relevant฀benchmarks฀ have฀been฀developed.฀This฀stems฀from฀the฀fact฀that฀DSPs฀implementing฀standard฀filter฀topologies฀are฀in฀large฀part฀computationally฀an฀apples-to-apples฀comparison.฀This฀is฀a฀result฀of฀the฀ fact฀that฀the฀filter฀algorithms฀are฀straightforward.฀On฀the฀other฀hand,฀if฀the฀DSP฀is฀going฀to฀ perform฀tasks฀other฀than฀simple฀filtering,฀then฀the฀additional฀features฀of฀the฀DSPs,฀such฀as฀I/O฀ access฀and฀interrupt฀overhead,฀may฀come฀into฀play,฀and฀benchmarks฀will฀not฀be฀of฀much฀help. Embedded฀controllers฀differ฀in฀feature฀set฀and฀implementation฀so฀dramatically฀that฀embedded฀ benchmarking฀is฀more฀an฀art฀than฀a฀science.฀Controller฀selection฀encompasses฀so฀much฀more฀ than฀pure฀computational฀horsepower฀that฀any฀benchmarks฀are฀nearly฀irrelevant. About฀the฀best฀that฀can฀be฀said฀for฀benchmarks฀is฀that฀with฀careful฀scrutiny,฀in฀some฀applications,฀they฀may฀be฀of฀assistance฀in฀comparing฀computational฀performance.฀ 2.1.3฀Availability Controllers,฀be฀they฀packaged,฀board-level฀or฀chip฀level,฀are฀as฀susceptible฀to฀market฀forces฀as฀ any฀other฀commodity.฀Many฀embedded฀products฀have฀life฀cycles฀of฀a฀decade฀or฀even฀longer.฀ 22

The฀Basics The฀engineer฀selecting฀a฀controller฀must฀consider฀the฀availability฀of฀the฀solution฀for฀the฀duration฀of฀the฀product’s฀anticipated฀life฀cycle. For฀example,฀there฀are฀numerous฀products฀on฀the฀market฀targeted฀at฀the฀cell฀phone฀industry.฀ The฀low-power,฀small฀size฀and฀low฀cost฀of฀these฀parts฀makes฀them฀attractive฀to฀embedded฀ designers.฀But฀before฀settling฀on฀a฀chip-de-jour,฀consider฀how฀fast฀the฀chip’s฀core฀market฀is฀ changing.฀New฀generations฀of฀cell฀phones฀are฀born฀about฀every฀six฀months.฀Does฀it฀make฀ sense฀for฀the฀IC฀manufacturers฀to฀continue฀to฀produce฀an฀IC฀after฀it฀has฀become฀obsolete฀in฀ the฀target฀market?฀No. One฀of฀the฀reasons฀this฀book฀uses฀Rabbit฀Semiconductor’s฀microcontrollers฀and฀modules฀is฀ that฀Rabbit฀is฀committed฀to฀the฀long-term฀availability฀of฀their฀ICs.฀Rabbit฀Semiconductor฀ grew฀out฀of฀Z-World,฀a฀leading฀manufacturer฀of฀embedded฀controllers.฀Over฀the฀years,฀฀ Z-World฀had฀to฀manage฀numerous฀end-of-life฀(EOL)฀announcements฀from฀vendors. A฀chip฀going฀EOL฀is฀always฀troublesome฀for฀companies฀using฀the฀chip.฀If฀the฀IC฀is฀a฀commodity฀part,฀like฀a฀logic฀chip,฀simply฀qualifying฀a฀new฀vendor฀may฀be฀fairly฀easy.฀If฀the฀IC฀is฀a฀ single-source฀part,฀like฀a฀real-time฀clock฀or฀CPU,฀then฀often฀the฀best฀that฀can฀be฀done฀is฀a฀onetime฀lifetime฀buy฀of฀the฀parts.฀The฀cash฀outlay฀for฀this฀can฀be฀crippling฀for฀a฀small฀company. Any฀part฀can฀be฀designed฀out฀of฀a฀system,฀but฀design฀cycles฀are฀slow฀and฀incur฀both฀financial฀ costs฀and฀opportunity฀costs.฀These฀costs฀can฀be฀difficult฀to฀bear฀for฀any฀company.฀Nobody฀ wants฀to฀revisit฀an฀old฀and฀successful฀design฀simply฀because฀a฀vendor฀has฀discontinued฀a฀part. Another฀very฀real฀issue฀to฀contend฀with฀is฀leadtime.฀Some฀microprocessor฀chips฀have฀phenomenally฀long฀lead฀times.฀Certain฀companies฀have฀seriously฀bad฀reputations฀for฀delivering฀ product฀on฀time.฀Some฀companies฀make฀a฀run฀of฀controllers฀only฀once฀or฀twice฀a฀year.฀If฀the฀ parts฀have฀been฀allocated฀in฀advance฀to฀large฀customers,฀then฀smaller฀companies฀may฀be฀faced฀ with฀six-month฀leadtimes.฀ One฀California-based฀engineer฀is฀fond฀of฀relating฀a฀story฀of฀how฀his฀firm฀designed฀an฀instrument฀ around฀a฀Japanese-made฀microcontroller.฀The฀instrument฀was฀entering฀the฀advanced฀prototyping฀ phase฀and฀the฀engineer฀was฀given฀the฀task฀of฀ordering฀parts฀to฀build฀a฀dozen฀new฀prototypes฀for฀ testing.฀All฀of฀the฀parts฀for฀the฀instrument฀were฀readily฀available฀except฀the฀microcontroller.฀ The฀Japanese฀manufacturer฀had฀production฀runs฀scheduled฀every฀six฀months,฀with฀fixed฀numbers฀of฀parts฀scheduled฀to฀ship฀to฀the฀United฀States.฀US฀distribution฀channels฀had฀allocated฀all฀ of฀the฀parts฀to฀large฀customers.฀The฀company฀that฀only฀needed฀twelve฀microcontrollers฀was฀ quoted฀a฀154-week฀leadtime฀(yes,฀three฀years). To฀solve฀the฀immediate฀problem฀of฀building฀and฀testing฀prototypes,฀the฀engineer฀arranged฀ to฀have฀some฀existing฀microcontrollers฀removed฀from฀older฀prototypes฀and฀placed฀in฀the฀ advanced฀prototypes.฀He฀still฀was฀faced฀with฀the฀need฀to฀acquire฀several฀more฀microcontrollers฀to฀finish฀the฀project.฀Desperate฀as฀the฀US฀company฀was,฀they฀could฀not฀find฀any฀of฀the฀ Japanese฀microcontrollers฀for฀sale.฀In฀the฀end฀they฀identified฀a฀$500฀evaluation฀board฀with฀the฀ $20฀microcontroller฀on฀it.฀They฀purchased฀$3000฀in฀evaluation฀boards฀to฀get฀the฀remaining฀ $120฀worth฀of฀microcontrollers฀they฀needed.฀An฀additional฀round฀of฀development฀was฀scheduled฀to฀replace฀the฀Japanese฀built฀microcontroller฀with฀a฀more฀available฀device.

23

Chapter฀2 If฀an฀engineer฀settles฀on฀a฀packaged฀or฀board-level฀controller฀solution,฀then฀the฀vendor฀ building฀the฀controller฀must฀manage฀the฀supply฀chain.฀If฀the฀controller฀vendor฀is฀not฀well฀capitalized,฀maintains฀poor฀relationships฀with฀silicon฀distributors,฀or฀is฀inexperienced฀in฀supply฀ chain฀management,฀leadtimes฀on฀the฀packaged฀or฀board-level฀controller฀can฀vary฀dramatically. In฀Chapter฀One,฀an฀example฀of฀an฀underwater฀torque฀tool฀was฀given.฀Being฀specialized฀tools,฀ only฀a฀few฀of฀these฀systems฀were฀to฀be฀produced.฀When฀the฀design฀team฀completed฀the฀prototype฀and฀the฀second฀tool฀was฀ready฀to฀be฀manufactured,฀the฀torque฀tool฀company฀attempted฀to฀ order฀a฀second฀PC104฀motherboard.฀The฀PC104฀vendor฀had฀planned฀poorly฀and฀was฀not฀only฀ out฀of฀stock,฀but฀the฀lead-time฀was฀quoted฀at฀some฀ghastly฀number฀like฀five฀months.฀After฀ many฀long฀and฀heated฀conversations,฀an฀engineer฀at฀the฀PC104฀vendor฀found฀a฀shop-queen฀ and฀shipped฀it฀to฀the฀torque฀tool฀company. A฀shop-queen฀is฀a฀board฀that฀has฀spent฀a฀lot฀of฀time฀in฀“the฀shop.”฀In฀many฀industries฀this฀carries฀the฀stigma฀of฀a฀lemon.฀A฀shop-queen฀in฀this฀instance฀was฀a฀board฀that฀was฀used฀internally฀ for฀testing฀and฀experimentation฀in฀the฀manufacturer’s฀facility.฀When฀the฀PC104฀board฀arrived฀ at฀the฀torque฀tool฀company,฀the฀board฀had฀bent฀connectors฀and฀a฀lot฀of฀dust.฀However,฀after฀a฀ bit฀of฀cleaning,฀lead฀straightening฀and฀rigorous฀evaluation,฀it฀was฀deemed฀functional,฀placed฀ in฀a฀production฀torque฀tool,฀was฀shipped฀and฀worked฀fine. There฀are฀two฀morals฀to฀this฀tale.฀First,฀if฀the฀controller฀supplier฀is฀inept฀at฀managing฀their฀ supply฀chain,฀the฀systems฀designer฀will฀bear฀the฀brunt.฀Second,฀even฀something฀as฀“standard”฀ as฀a฀PC104฀embedded฀PC฀is฀not฀always฀easily฀second-sourced. The฀production฀departments฀at฀Z-World฀and฀Rabbit฀Semiconductor฀have฀nearly฀two฀decades฀ of฀experience฀managing฀volatile฀supply฀chains.฀Customers฀that฀use฀Z-World฀or฀Rabbit฀controllers฀don’t฀have฀to฀worry฀about฀leadtimes฀on฀individual฀parts.฀The฀folks฀at฀Z-World฀and฀ Rabbit฀worry฀about฀that.฀Their฀commitment฀to฀product฀availability฀makes฀Z-World฀an฀excellent฀choice฀for฀packaged฀or฀board-level฀control฀solutions.฀The฀same฀can฀be฀said฀for฀Rabbit฀ Semiconductor฀in฀regard฀to฀chip-level฀and฀core-module฀solutions.฀Both฀companies฀have฀ consistently฀short฀lead฀times฀for฀their฀products,฀usually฀shipping฀from฀stock. 2.1.4฀Tools฀and฀Time-to-Market Seldom฀do฀engineers฀have฀the฀luxury฀of฀long฀development฀cycles.฀Even฀“big”฀projects฀pack฀ the฀work฀into฀the฀tightest,฀most฀optimistic฀schedule฀that฀can฀be฀cooked฀up฀by฀management.฀ There฀is฀never฀time฀budgeted฀for฀“fighting฀with฀and฀debugging฀development฀tools.”฀Selecting฀ a฀controller฀solution฀that฀has฀first฀class฀tools฀is฀desirable. Tools฀can฀roughly฀be฀broken฀down฀into฀hardware฀and฀software฀tools.฀Hardware฀tools฀include฀ schematic฀capture,฀PCB฀layout,฀circuit฀simulation,฀and฀HDL฀(hardware฀description฀language)฀ compilers.฀Software฀tools฀include฀compilers฀that฀generate฀code฀for฀the฀controller’s฀processor,฀ assemblers,฀simulators,฀and฀emulators/debuggers. The฀axiom฀“you฀get฀what฀you฀pay฀for,”฀implies฀that฀good฀tools฀will฀be฀expensive.฀Surprisingly,฀this฀isn’t฀always฀true฀of฀software฀development฀tools.฀The฀axiom,฀however,฀seems฀to฀hold฀ fast฀for฀hardware฀tools. 24

The฀Basics Z-World,฀Rabbit฀Semiconductor฀and฀Softools฀each฀sell฀outstanding฀compilers฀for฀less฀than฀a฀ thousand฀dollars.฀Technically฀refined฀yet฀inexpensive฀development฀tools฀make฀Rabbit-based฀ embedded฀controllers฀a฀cost-effective฀solution฀for฀many฀applications. Dynamic฀C™฀provides฀a฀complete฀integrated฀development฀environment฀(IDE)฀with฀a฀C฀compiler,฀assembler฀and฀debugger.฀The฀vast฀libraries฀are฀provided฀in฀source฀form฀so฀engineers฀can฀ adapt฀standard฀drivers฀to฀custom฀applications. Softools฀offers฀an฀ANSI-compliant฀toolset฀featuring฀an฀IDE฀with฀a฀C฀compiler,฀assembler฀and฀ debugger.฀Complete฀libraries฀are฀also฀provided. Dynamic฀C™฀and฀Softools฀both฀communicate฀with฀the฀Rabbit฀processors฀through฀a฀serial฀ interface.฀Both฀tools฀offer฀all฀the฀debugging฀capability฀needed฀by฀developers.฀This฀simple฀ interface฀means฀no฀expensive฀hardware฀programmers,฀emulators฀or฀debuggers฀are฀needed.฀ Rabbit’s฀innovative฀core฀modules฀give฀system฀designers฀a฀ready-made฀electronic฀heart฀comprised฀of฀fine-pitch฀surface฀mount฀(SMT)฀components฀all฀on฀a฀compact฀board฀with฀easy฀to฀ access฀through-hole฀headers.฀The฀core฀module฀designs฀may฀be฀freely฀copied฀to฀a฀custom฀PCB฀ in฀production,฀if฀required. The฀core฀modules฀have฀“development”฀boards฀available.฀These฀development฀boards฀sport฀a฀ prototyping฀area฀as฀well฀as฀some฀common฀peripherals฀(LEDs,฀switches,฀power฀supply฀and฀ the฀like).฀Rabbit฀core฀module฀boards฀differ฀from฀other฀manufacturers’฀development฀boards฀in฀ their฀practicality.฀ Many฀CPU฀vendors฀offer฀development฀boards฀that฀are฀not฀easily฀adapted฀to฀use฀in฀control฀ applications.฀Signals฀are฀only฀pinned฀out฀on฀headers,฀there฀are฀no฀prototyping฀areas฀and฀the฀ boards฀are฀unreasonably฀expensive.฀Rabbit’s฀development฀boards฀are฀useful฀tools฀for฀engineers฀and฀not฀simply฀marketing฀aids. In฀addition฀to฀the฀Rabbit฀core฀modules,฀Z-World฀offers฀Rabbit-based฀board-level฀and฀packaged฀ controllers.฀This฀one-stop-shop฀for฀inexpensive฀software฀tools฀and฀diverse฀hardware฀platforms฀ sets฀Z-World฀and฀Rabbit฀Semiconductor฀apart฀from฀other฀embedded฀controller฀vendors. Good฀hardware฀development฀tools฀are฀expensive.฀The฀more฀complex฀the฀circuit,฀the฀more฀an฀ engineer฀will฀want฀to฀have฀a฀“good”฀layout฀tool.฀For฀example,฀if฀a฀circuit฀only฀has฀a฀couple฀of฀ connectors฀on฀a฀small฀PCB฀then฀an฀inexpensive฀tool฀can฀produce฀usable฀PCB฀Gerber฀files฀and฀ a฀drill฀file.฀Gerber฀files฀are฀CAM฀(computer฀aided฀manufacturing)฀files฀that฀describe฀where฀to฀ put฀copper฀on฀a฀PCB.฀A฀drill฀file,฀sometimes฀called฀an฀NC฀drill฀file฀for฀“numerically฀controlled”฀drill฀file,฀describes฀where฀and฀what฀sized฀holes฀to฀place฀in฀the฀PCB. If฀a฀board฀design฀includes฀fine฀pitch฀SMT฀parts฀and฀hundreds฀of฀connections,฀a฀cheap฀PCB฀ layout฀tool฀will฀cost฀more฀in฀layout฀time,฀errors฀and฀debugging฀than฀initially฀buying฀a฀good฀ tool.฀One฀tool฀that฀has฀a฀proven฀track฀record฀is฀PCAD™.฀It฀is฀perhaps฀the฀best฀tool฀available฀ today฀for฀doing฀schematic฀capture฀and฀PCB฀layout฀of฀simple฀or฀complex฀designs฀for฀the฀small฀ and฀medium฀sized฀company.฀ If฀complex฀circuit฀design฀can฀be฀avoided,฀then฀PCB฀design฀can฀be฀accomplished฀economically฀ with฀a฀low-cost฀tool฀or฀by฀farming฀the฀work฀out฀to฀a฀design฀bureau.฀If฀a฀core฀module฀can฀be฀ used,฀often฀the฀additional฀circuitry฀for฀an฀application฀is฀minimal.฀The฀application฀board฀design฀

25

Chapter฀2 is฀often฀a฀matter฀of฀providing฀simple฀interface฀components฀and฀a฀power฀supply.฀The฀development฀boards฀are฀usually฀suitable฀for฀design฀using฀a฀low฀cost฀tool฀or฀PCB฀design฀house. The฀Z-World฀full-grown฀board-level฀and฀packaged฀controllers฀often฀eliminate฀the฀need฀for฀ any฀secondary฀PCB฀design.฀This฀can฀save฀thousands฀of฀dollars฀in฀tools฀and฀time.฀However,฀ for฀medium฀or฀large฀production฀volumes,฀it฀is฀generally฀more฀economical฀to฀go฀with฀a฀Rabbit฀ Semiconductor฀core฀module฀or฀chip-based฀design. 2.1.5฀Libraries When฀selecting฀a฀controller,฀an฀often฀overlooked฀fact฀is฀that฀the฀amount฀of฀time฀spent฀writing฀ code฀for฀an฀embedded฀system฀usually฀dwarfs฀the฀time฀spent฀designing฀hardware.฀Selecting฀a฀ control฀solution฀that฀minimizes฀the฀software฀development฀task฀is฀desirable. Many฀companies฀offer฀controller฀hardware,฀but฀few฀offer฀hardware฀and฀tightly฀coupled฀software฀libraries.฀This฀means฀that฀software฀engineers฀may฀have฀to฀spend฀weeks฀writing฀code฀to฀ talk฀to฀even฀the฀simplest฀devices. Rabbit฀Semiconductor฀offers฀complete฀libraries฀for฀all฀of฀their฀controllers.฀There฀are฀drivers฀ for฀simple฀devices฀like฀analog-to-digital฀converters฀(ADCs)฀as฀well฀as฀complex฀drivers฀for฀ devices฀like฀¼฀VGA฀graphic฀LCDs. Libraries฀go฀beyond฀hardware฀device฀drivers.฀Various฀communication฀protocols฀are฀provided฀ with฀Rabbit-based฀products.฀TCP/IP฀and฀Modbus฀are฀two฀heavily฀used฀protocols.฀TCP/IP฀is฀ usually฀used฀with฀Ethernet.฀Modbus฀is฀most฀often฀used฀over฀RS-485. An฀embedded฀systems฀designer’s฀time฀is฀best฀spent฀developing฀algorithms฀and฀software฀specific฀to฀the฀project,฀not฀reinventing฀drivers.฀By฀selecting฀a฀control฀solution฀with฀an฀abundance฀ of฀tested฀and฀proven฀drivers,฀development฀time฀can฀be฀minimized. One฀of฀the฀advantages฀of฀an฀embedded฀PC฀is฀the฀amount฀of฀software฀available.฀For฀instance,฀ operating฀systems฀like฀Microsoft®Windows®,฀Linux฀and฀DOS®฀are฀readily฀available.฀Drivers฀for฀many฀standard฀devices฀are฀available.฀Ethernet,฀disk฀access,฀video฀and฀keyboards฀are฀ readily฀available. Care฀must฀be฀exercised฀when฀attempting฀to฀use฀desktop฀OSs฀in฀embedded฀applications.฀After฀ all,฀none฀of฀these฀were฀designed฀specifically฀with฀embedded฀systems฀in฀mind.฀How฀do฀you฀ talk฀to฀an฀embedded฀I/O฀device฀like฀an฀ADC?฀What฀do฀you฀do฀if฀you฀want฀to฀decode฀signals฀ from฀a฀quadrature฀encoder?฀The฀answer฀in฀most฀cases฀is฀to฀handle฀the฀I/O฀directly฀and฀write฀ your฀own฀drivers. The฀experience฀Rabbit฀Semiconductor฀has฀gained฀over฀the฀years฀has฀allowed฀the฀company฀to฀ craft฀feature฀rich฀drivers฀for฀embedded฀systems฀designers.฀For฀example,฀Rabbit฀drivers฀exist฀ to฀talk฀to฀both฀ADCs฀and฀quadrature฀encoders.฀ The฀vast฀libraries฀separate฀Rabbit’s฀solutions฀from฀their฀competition.฀The฀existence฀of฀solid฀ drivers฀and฀a฀royalty-free฀TCP/IP฀stack฀allows฀the฀system฀designer฀to฀focus฀on฀the฀application฀ and฀not฀on฀reinventing฀drivers฀or฀writing฀a฀protocol฀stack.฀

26

The฀Basics 2.1.6฀Sustainability Products฀spend฀90%฀of฀their฀life฀in฀production.฀The฀original฀designers฀often฀move฀to฀other฀ companies฀before฀a฀product฀completes฀its฀lifecycle.฀Engineers฀who฀were฀not฀involved฀with฀฀ the฀product’s฀development฀will฀almost฀always฀be฀responsible฀for฀sustaining฀the฀product฀in฀ production. Modifications฀made฀to฀a฀product฀that฀has฀been฀released฀to฀production฀can฀be฀loosely฀grouped฀ into฀four฀categories.฀These฀are฀bug฀fixes,฀corrections฀to฀address฀hardware฀EOL฀(end฀of฀life)฀ issues,฀product฀enhancements,฀and฀improvements฀for฀manufacturability. Bug฀fixes฀are฀the฀most฀urgent฀class฀of฀changes.฀Software฀or฀hardware฀bugs฀may฀be฀discovered.฀Selecting฀a฀controller฀with฀easy฀to฀learn฀software฀development฀tools฀will฀allow฀a฀future฀ engineer฀to฀quickly฀come฀up฀to฀speed฀on฀how฀to฀modify฀the฀product’s฀code฀base.฀If฀library฀ source฀code฀is฀available,฀then฀even฀driver฀bugs฀may฀be฀repaired. In฀the฀case฀of฀hardware฀bugs,฀selecting฀a฀microcontroller฀with฀good฀documentation฀and฀ debugging฀features฀is฀prudent.฀A฀sustaining฀engineer฀will฀have฀to฀come฀up฀to฀speed฀quickly฀ on฀the฀available฀troubleshooting฀tools.฀The฀desired฀emphasis฀will฀naturally฀be฀on฀understanding฀and฀fixing฀the฀product’s฀bug,฀not฀learning฀complicated฀or฀poorly฀documented฀tools. The฀second฀class฀of฀production฀side฀modifications฀addresses฀EOL฀issues.฀If฀the฀part฀becoming฀obsolete฀is฀easily฀second฀sourced฀then฀the฀only฀modification฀to฀the฀product฀will฀be฀to฀the฀ product’s฀documentation.฀A฀second฀source฀for฀the฀part฀will฀be฀qualified฀and฀added.฀However,฀ if฀the฀part฀going฀EOL฀is฀a฀single฀source฀part,฀then฀hardware฀and฀software฀changes฀will฀likely฀ be฀required. Consider฀the฀case฀of฀a฀real฀time฀clock฀chip฀(RTC)฀going฀EOL.฀These฀devices฀are฀often฀single฀ source.฀Occasionally฀there฀will฀be฀“pin฀compatible”฀devices฀available,฀but฀even฀these฀may฀not฀ be฀functionally฀equivalent฀devices.฀So฀in฀the฀best฀case฀a฀software฀modification฀will฀need฀to฀be฀ made฀to฀the฀RTC฀driver.฀The฀worst฀case฀requires฀both฀a฀PCB฀change฀and฀a฀new฀RTC฀driver. As฀in฀the฀case฀of฀a฀bug฀fix,฀good฀software฀development฀tools,฀good฀product฀documentation฀ and฀library฀source฀code฀assist฀in฀focusing฀on฀the฀problem฀not฀the฀process.฀This฀saves฀time฀and฀ money฀and฀allows฀the฀company฀to฀address฀EOL฀issues฀nimbly. The฀third฀class฀of฀post-production฀release฀modifications฀addresses฀the฀inevitable฀demands฀for฀ feature฀enhancement.฀The฀reason฀for฀modifications฀to฀the฀product฀may฀be฀different฀than฀for฀ the฀EOL฀modifications,฀but฀the฀actual฀work฀required฀is฀the฀same.฀Product฀enhancements฀will฀ require฀changing฀software,฀hardware฀or฀both. The฀prescient฀designer฀will฀initially฀select฀a฀controller฀with฀a฀few฀more฀resources฀than฀are฀ actually฀needed฀for฀the฀product฀rollout.฀This฀will฀allow฀a฀future฀engineer฀to฀add฀product฀ enhancements฀without฀changing฀the฀hardware. The฀last฀class฀of฀changes฀relates฀to฀manufacturability.฀Generally฀these฀are฀mechanical฀adjustments฀that฀make฀assembly฀easier.฀The฀initial฀controller฀selection฀has฀little฀impact฀on฀these฀types฀ of฀changes.฀One฀notable฀exception฀crops฀up฀when฀the฀production฀test฀department฀or฀the฀repair/ re-work฀group฀wants฀to฀install฀special฀test฀or฀diagnostic฀code฀in฀the฀product.฀In฀this฀situation,฀ an฀in-system฀programmable฀(ISP)฀controller฀can฀save฀the฀company฀much฀time฀and฀money.

27

Chapter฀2 Some฀companies฀opt฀to฀do฀functional฀tests฀on฀every฀instrument฀produced.฀If฀the฀instrument฀is฀ complex฀or฀requires฀the฀interaction฀of฀many฀different฀hardware฀subsystems,฀a฀special฀functional฀test฀program฀(test฀code)฀may฀be฀preferred฀over฀the฀full฀application฀to฀verify฀that฀the฀ individual฀hardware฀components฀are฀operational. Test฀code฀seldom฀bears฀any฀resemblance฀to฀the฀full฀application฀program.฀A฀test฀program฀ attempts฀to฀isolate฀individual฀hardware฀components฀and฀verify฀their฀operation.฀If฀sensors฀ require฀calibration฀then฀the฀test฀code฀may฀also฀perform฀this฀task.฀Ideally,฀a฀test฀program฀will฀ identify฀and฀isolate฀problems.฀The฀repair฀department฀will฀often฀use฀production’s฀test฀code฀as฀a฀ diagnostic฀tool.฀ After฀an฀instrument’s฀hardware฀is฀qualified฀by฀the฀test฀code,฀the฀application฀code฀must฀be฀ loaded.฀The฀instrument฀is฀given฀a฀cursory฀examination฀and฀then฀placed฀in฀finished฀goods. If฀the฀controller฀is฀not฀ISP,฀then฀a฀socketed฀nonvolatile฀memory฀device฀will฀need฀to฀be฀ designed฀into฀the฀system.฀Changing฀memory฀devices฀can฀be฀time฀consuming,฀and฀the฀test฀ ROMs฀will฀be฀subject฀to฀physical฀damage฀from฀constant฀handling.฀An฀ISP฀controller฀allows฀ production฀test฀code฀or฀diagnostic฀code฀to฀be฀installed฀and฀replaced฀easily.฀

2.2฀Defining฀the฀Problem Before฀selecting฀a฀control฀solution,฀the฀control฀problem฀must฀be฀well฀defined.฀This฀can฀be฀ complicated฀by฀indecisive฀management฀or฀by฀technological฀hurdles฀that฀are฀of฀unknown฀ magnitude.฀The฀former฀is฀the฀most฀frustrating฀for฀engineers.฀The฀latter฀can฀often฀be฀mitigated฀ through฀research฀and฀simulation. Management฀will฀provide฀a฀product฀description.฀Some฀companies฀have฀a฀formal฀process฀for฀ creating฀a฀product฀requirement฀document฀(PRD),฀sometimes฀called฀a฀product฀specification,฀ sometimes฀called฀a฀system฀requirements฀document฀(SRD).฀Sadly,฀most฀companies฀do฀not฀ have฀a฀formal฀process.฀All฀too฀often,฀management฀will฀simply฀issue฀a฀short฀memo฀or฀a฀napkin฀ sketch฀describing฀the฀new฀product’s฀features. In฀either฀case,฀the฀engineer฀must฀start฀with฀what฀is฀given.฀Obvious฀questions฀must฀be฀ answered—either฀by฀managerial฀edict,฀technical฀prowess฀or฀educated฀guessing.฀ Next,฀the฀system฀architecture฀must฀be฀developed.฀Some฀companies฀have฀a฀formal฀process฀for฀ taking฀an฀SRD฀and฀splitting฀out฀hardware฀and฀software฀requirements฀into฀a฀hardware฀requirements฀specification฀(HRS)฀and฀a฀software฀requirements฀specification฀(SRS).฀From฀there,฀ hardware฀and฀software฀architectures฀can฀be฀defined. But฀for฀most฀companies,฀at฀this฀stage฀the฀controller฀is฀simply฀a฀box฀on฀a฀diagram฀with฀sensors฀ and฀actuators฀reporting฀to฀it฀and฀a฀blob฀on฀the฀side฀of฀the฀paper฀with฀what฀passes฀for฀a฀flowchart฀amongst฀friends. Regardless฀of฀the฀formality฀of฀the฀process,฀the฀engineer฀will฀get฀an฀idea฀of฀the฀type฀of฀I/O฀required฀for฀the฀project.฀If฀ADCs฀are฀required,฀their฀resolution฀should฀be฀defined.฀If฀pulse฀width฀ modulation฀is฀being฀generated฀then฀characteristics฀such฀as฀drive฀current,฀frequency,฀duty฀cycle฀ range฀and฀resolution฀should฀be฀defined.฀As฀much฀as฀possible฀should฀be฀nailed฀down฀up฀front.

28

The฀Basics Undoubtedly,฀as฀the฀project฀progresses,฀requirements฀will฀change฀and฀the฀initial฀guesses฀at฀ various฀features฀will฀require฀adjustment.฀This฀is฀just฀part฀of฀the฀process฀of฀evolving฀an฀idea฀ into฀a฀product.฀Formal฀processes฀try฀to฀avoid฀costly฀changes฀half฀way฀down฀a฀product฀development฀path.฀But฀front-loading฀a฀project฀with฀detailed฀requirements฀documentation฀is฀not฀ well฀suited฀to฀the฀needs฀of฀many฀small฀companies.฀ Time-to-market฀may฀require฀playing฀fast฀and฀loose฀with฀the฀requirements฀phase฀just฀to฀get฀ a฀product฀to฀market฀quickly.฀Sometimes฀having฀an฀imperfect฀plan฀on฀a฀placemat฀will฀be฀of฀ greater฀use฀in฀getting฀a฀sellable฀product฀done฀quickly฀than฀having฀a฀perfect฀plan฀in฀six฀months. Once฀a฀plan฀is฀in฀hand,฀technological฀hurdles฀may฀still฀complicate฀a฀project.฀These฀are฀problems฀that฀an฀engineer฀does฀not฀know฀how฀to฀solve฀up฀front.฀These฀problems฀are฀only฀solved฀ through฀research฀and฀experimentation.

2.3฀A฀Survey฀of฀Solutions While฀this฀book฀uses฀Rabbit฀3000-based฀products฀for฀all฀the฀projects฀detailed,฀there฀are฀a฀ myriad฀of฀other฀controllers฀on฀the฀market.฀Many฀of฀the฀techniques฀presented฀are฀applicable฀to฀ controllers฀other฀than฀the฀Rabbit฀3000.฀So฀let’s฀briefly฀examine฀some฀of฀the฀available฀microprocessors฀and฀microcontrollers฀and฀see฀how฀they฀compare฀with฀the฀Rabbit-based฀solutions. 2.3.1฀Small฀Microcontrollers There฀are฀many฀“small”฀microcontrollers฀on฀the฀market.฀Probably฀the฀most฀widely฀known฀is฀ the฀PIC™฀line฀of฀controllers฀from฀Microchip.฀These฀parts฀along฀with฀their฀hopeful฀brethren— AVR฀from฀Atmel,฀COP8™฀from฀National฀Semiconductor,฀Nitron™฀from฀Motorola฀and฀many฀ others—fill฀a฀niche฀in฀the฀“not-too-complicated”฀class฀of฀embedded฀solutions.฀ These฀parts฀are฀available฀from฀$0.80฀to฀about฀$5.00.฀Code฀is฀stored฀either฀in฀“One฀Time฀Programmable”฀(OTP)฀ROM฀or฀internal฀flash,฀making฀small฀controllers฀fairly฀self-contained. The฀lower฀end฀of฀this฀style฀of฀controller฀finds฀parts฀like฀Microchip’s฀PIC16C54฀and฀Atmel’s฀ AT90S1200.฀These฀parts฀have฀a฀low฀pin฀count฀and฀subsequently฀low฀I/O฀capability฀and฀have฀ been฀optimized฀for฀simple฀applications. Table฀2.2฀shows฀how฀the฀feature฀sets฀stack฀up฀between฀these฀two฀parts. Table฀2.2:฀Low฀end฀PICs฀and฀AVRs฀go฀head-to-head. Pin฀count Maximum฀I/O฀pins Frequency฀ranges฀ Available RAM EEPROM General฀Purpose฀ Registers Program฀Storage

PIC16C54 28 20 DC฀–฀40฀MHz 24฀bytes ZERO Same฀as฀RAM฀24฀×฀8

AT90S1200 20 15 DC฀–฀12฀ MHz 0 64฀bytes 32฀×฀8

512฀×฀12฀bits฀(OTP)

1฀KB฀(flash)

29

Comments

PICTM฀W฀register฀is฀accumulator฀ AVR฀has฀no฀accumulator

Chapter฀2 Table฀2.2:฀Low฀end฀PICs฀and฀AVRs฀go฀head-to-head฀(continued). Code฀Size

PIC16C54 512฀

AT90S1200 512฀

Counter/Timers External฀Interrupts Sleep฀mode Watchdog฀Timer ISP Analog฀Comparator

1 0 Y Y Y N

1 1 Y Y Y Y

Comments PIC฀12-bit฀instructions฀ AVR฀16-bit฀instructions

For฀systems฀requiring฀analog฀input,฀integrated฀analog-to-digital-converters฀(ADCs)฀are฀ available.฀These฀are฀generally฀limited฀to฀8-bit,฀10-bit฀or฀12-bit฀converters.฀For฀example,฀the฀ PIC16C710฀has฀a฀built-in฀8-bit฀converter฀with฀a฀4-to-1฀analog฀multiplexer.฀The฀PIC16C710฀ has฀a฀similar฀pinout฀to฀the฀PIC16C54,฀but฀several฀pins฀have฀modified฀functions. Historically,฀analog฀designers฀have฀frowned฀upon฀the฀performance฀of฀ADCs฀built฀on฀the฀same฀ silicon฀die฀as฀the฀microcontroller.฀Noise฀performance฀on฀some฀integrated฀ADCs฀has฀been฀ known฀to฀be฀less฀than฀stellar.฀This฀is฀something฀that฀must฀be฀evaluated฀on฀a฀part-by-part฀basis.฀ Some฀manufacturers฀do฀an฀excellent฀job;฀others฀are฀not฀as฀good฀at฀keeping฀digital฀noise฀out฀of฀ the฀analog฀portion฀of฀their฀designs. Burr-Brown,฀now฀a฀division฀of฀Texas฀Instruments,฀is฀a฀name฀synonymous฀with฀excellence฀in฀ analog฀design.฀The฀Burr-Brown/TI฀MSC1200฀is฀an฀example฀of฀a฀device฀where฀a฀fast฀computational฀core฀coexists฀with฀high-resolution฀low-noise฀analog. For฀around฀$6.50฀the฀MSC1200฀offers฀a฀lot฀of฀functionality.฀The฀following฀list฀enumerates฀the฀ device’s฀key฀features: ฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

฀

8051฀high-speed฀core฀(4฀clocks฀per฀instruction฀cycle) DC-33฀MHz฀operation 4K฀ISP฀Flash 1K฀boot฀block 128฀bytes฀of฀SRAM 24-bit฀delta-sigma฀converter Programmable฀gain฀amplifier฀(PGA)฀from฀1฀to฀128 8฀input฀analog฀MUX Differential฀or฀single฀ended฀inputs On-site฀burnout฀detection On-site฀precision฀reference On-site฀temperature฀sensor 8-bit฀current฀DAC 16฀digital฀I/O฀pins Watchdog฀timer Two฀16-bit฀timer/counters 30

The฀Basics ฀

Full฀duplex฀UART −40C฀to฀+85C฀temperature฀range ฀ 2.7฀to฀5.25฀volt฀allowable฀rails

฀

The฀MSC1200฀datasheet฀indicates฀that฀a฀system฀designer฀can฀expect฀to฀see฀between฀5฀and฀22฀ effective฀bits฀of฀resolution฀depending฀on฀how฀the฀analog฀channel฀is฀configured.฀Many฀of฀BurrBrown’s฀high-resolution฀delta-sigma฀converters฀allow฀the฀system฀designer฀to฀trade-off฀gain฀ and฀sample฀rate฀against฀uncertainty฀(noise).฀The฀MSC1200฀allows฀the฀same฀trade-offs. A฀flexible฀analog฀MUX฀allows฀the฀analog฀inputs฀to฀be฀either฀differential฀or฀single฀ended.฀The฀ MUX฀offers฀the฀unusual฀capability฀to฀pair฀arbitrary฀inputs฀as฀a฀single฀differential฀channel. The฀high-speed฀core฀is฀based฀on฀the฀venerable฀8051฀architecture.฀The฀popularity฀enjoyed฀by฀ 8051฀over฀decades฀is฀a฀testament฀to฀the฀original฀designer’s฀insightfulness.฀Yet,฀the฀MSC1200฀ core฀has฀several฀improvements฀over฀the฀original฀architecture.฀ Notably,฀the฀MSC1200฀core฀has฀only฀four฀machine฀cycles฀per฀instruction฀cycle.฀The฀traditional฀8051฀implementations฀have฀twelve฀machine฀cycles฀per฀instruction฀cycle.฀This,฀combined฀ with฀a฀screaming฀33฀MHz฀top฀speed,฀make฀the฀MSC1200฀a฀reasonably฀potent฀computational฀ engine฀for฀the฀price. The฀MSC1200฀is฀an฀example฀of฀a฀controller฀that฀has฀fantastic฀analog฀noise฀performance฀ married฀to฀a฀CPU฀on฀a฀single฀die.฀ Small฀controllers฀are฀suitable฀for฀offloading฀simple฀tasks฀from฀a฀larger฀CPU.฀For฀example,฀ consider฀a฀controller฀that฀must฀implement฀a฀small฀servo฀control฀loop฀as฀part฀of฀a฀larger฀system.฀This฀servo฀loop฀requires฀sampling฀a฀position฀error฀and฀quickly฀adjusting฀a฀drive฀signal฀to฀ a฀motor.฀If฀the฀designer฀moves฀this฀task฀from฀a฀main฀processor,฀say฀a฀Rabbit฀3000,฀to฀a฀satellite฀processor฀like฀a฀PIC,฀then฀the฀software฀on฀the฀Rabbit฀becomes฀simpler฀and฀more฀of฀the฀ Rabbit’s฀resources฀are฀available฀for฀other฀system฀tasks. Using฀small฀microcontrollers฀as฀satellite฀controllers฀is฀a฀technique฀that฀can,฀in฀some฀circumstances,฀save฀development฀time.฀The฀additional฀microcontroller฀may฀drive฀up฀the฀end฀product฀ cost.฀This฀may฀or฀may฀not฀be฀offset฀by฀reduction฀in฀development฀time.฀If฀the฀main฀CPU฀can฀ do฀the฀offloaded฀task฀even฀at฀the฀expense฀of฀more฀complex฀software,฀then฀it฀may฀be฀worth฀ spending฀the฀extra฀time฀on฀software฀NRE฀(nonrecurring฀engineering)฀fees฀to฀reduce฀the฀perunit฀cost฀of฀the฀hardware. In฀some฀cases,฀the฀application฀of฀an฀inexpensive฀satellite฀processor฀can฀actually฀keep฀the฀cost฀ of฀a฀system฀down.฀If฀a฀main฀processor฀is฀stretched฀to฀the฀limit฀and฀an฀additional฀task฀must฀be฀ added,฀the฀incremental฀cost฀of฀moving฀from฀one฀main฀CPU฀to฀another฀may฀be฀higher฀than฀ offloading฀the฀additional฀task฀to฀an฀inexpensive฀satellite฀controller. 2.3.2฀Mid-sized฀Microcontrollers฀and฀Microprocessors Parts฀costing฀$10฀to฀$20฀are฀generally฀considered฀“mid-sized”฀controllers.฀There฀are฀a฀large฀ number฀of฀parts฀available฀in฀this฀class.฀The฀Rabbit฀2000฀and฀Rabbit฀3000฀microprocessors,฀ the฀AVR฀ATmega128฀and฀the฀Intel386™EX฀all฀fall฀squarely฀into฀the฀mid-sized฀class. Different฀architectural฀philosophies฀drove฀the฀development฀of฀each฀of฀these฀processors.฀This฀ is฀evident฀from฀each฀processor’s฀feature฀set. 31

Chapter฀2 For฀many,฀the฀name฀Intel฀conjures฀up฀images฀of฀multi-gigahertz฀multi-watt฀desktop฀processors.฀It’s฀easy฀to฀forget฀that฀Intel฀has฀a฀long฀history฀of฀serving฀the฀embedded฀market.฀The฀ Intel386฀EX฀was฀designed฀specifically฀for฀the฀embedded฀market฀by฀adapting฀the฀popular฀ desktop฀i386. The฀Intel386฀EX฀datasheet฀states,฀“It฀(the฀Intel386฀EX)฀provides฀the฀performance฀benefits฀of฀ 32-bit฀programming฀with฀the฀cost฀savings฀associated฀with฀16-bit฀hardware฀systems.”฀Clearly,฀ Intel฀was฀trying฀to฀leverage฀the฀success฀of฀the฀x86฀family฀while฀trading฀off฀performance฀for฀ lower฀cost.฀By฀building฀from฀an฀existing฀design฀and฀maintaining฀binary฀compatibility฀with฀ earlier฀family฀members,฀Intel฀ensured฀that฀a฀wide฀array฀of฀existing฀development฀tools฀would฀ generate฀code฀for฀the฀Intel386฀EX. The฀Atmel฀AVR฀family฀sports฀a฀very฀straightforward,฀almost฀textbook,฀architecture.฀When฀ the฀AVR฀architecture฀was฀created,฀all฀of฀the฀development฀tools฀had฀to฀be฀built฀from฀scratch.฀ Atmel฀created฀a฀design฀that฀scaled฀nicely฀across฀a฀wide฀array฀of฀derivative฀products.฀The฀same฀ basic฀architecture฀can฀be฀found฀in฀the฀least฀expensive฀AVRs฀(such฀as฀the฀AT90S1200)฀all฀฀ the฀way฀to฀the฀mid-sized฀family฀members฀like฀the฀ATmega128.฀All฀of฀the฀AVRs฀are฀flashbased฀controllers. Rabbit฀Semiconductor฀struck฀a฀middle฀road฀between฀“old”฀and฀“new.”฀The฀Rabbit฀parts฀are฀ based฀closely฀on฀the฀Zilog฀Z180฀architecture,฀although฀they฀are฀not฀binary฀compatible฀with฀ the฀Zilog฀parts.฀This฀caused฀Rabbit฀Semiconductor฀to฀have฀to฀create฀a฀suite฀of฀development฀ tools฀to฀support฀the฀new฀design.฀At฀the฀same฀time,฀Rabbit฀was฀able฀to฀freely฀add฀instructions฀ to,฀and฀otherwise฀tweak,฀the฀Z180฀architecture. So,฀like฀Intel,฀Rabbit฀built฀upon฀a฀widely฀respected฀and฀successful฀architecture.฀Unlike฀Intel,฀ Rabbit฀did฀not฀maintain฀binary฀compatibility฀with฀the฀original฀design. Like฀Atmel,฀Rabbit฀had฀to฀produce฀firmware฀development฀tools,฀and฀likewise,฀Rabbit฀optimized฀their฀design฀for฀C฀programming.฀ Now฀that฀we฀have฀an฀idea฀of฀the฀general฀philosophies฀behind฀the฀processor฀designs,฀let’s฀have฀ a฀look฀at฀key฀features฀of฀each฀device.฀We’ll฀start฀with฀the฀Intel386฀EX. Intel386TMEX ฀ Static฀Intel386™฀core;฀DC฀–฀33฀MHz ฀ Full฀32-bit฀internal฀architecture ฀ External฀16-bit฀data฀bus฀allows฀use฀of฀less฀expensive฀memories ฀ 26-bit฀address฀bus ฀ Address฀pipelining฀allows฀use฀of฀slower,฀inexpensive฀memories ฀ MMU฀fully฀compatible฀with฀80286฀and฀Intel386™฀DX฀processors ฀ On-chip฀dedicated฀debugging฀support฀including฀break฀point฀registers ฀ Watchdog฀timer ฀ Chip-select฀unit ฀ Timer/counter฀unit ฀ Interrupt฀control฀unit ฀ Parallel฀I/O฀unit฀(24฀I/O฀pins) ฀ UARTs 32

The฀Basics ฀ ฀ ฀ ฀

DMA฀and฀bus฀arbiter฀unit Refresh฀control฀unit JTAG-compliant฀test-logic฀unit 140฀mA฀@฀25฀MHz,฀VCC฀=฀3.6฀volts฀(device฀held฀in฀reset฀with฀inputs฀held฀in฀฀ inactive฀state)

By฀looking฀at฀the฀features฀listed฀above,฀one฀can฀tell฀that฀the฀CPU฀is฀a฀derivative฀of฀the฀desktop฀ processor฀line.฀Besides฀the฀giveaway฀bullet฀of฀“Intel386™฀Core,”฀the฀processor฀has฀features฀ such฀as฀a฀32-bit฀architecture,฀a฀huge฀26-bit฀external฀address฀space,฀DMA฀and฀a฀bus฀arbiter.฀All฀ are฀indicative฀of฀a฀powerful฀desktop฀processor. Intel฀was฀clearly฀targeting฀the฀embedded฀market฀with฀this฀processor.฀They฀squeezed฀down฀the฀ external฀data฀bus฀to฀16-bits฀to฀allow฀less฀expensive฀memory฀devices฀to฀be฀used฀in฀the฀embedded฀design.฀A฀counter/timer,฀UART฀and฀24฀I/O฀pins฀were฀added฀around฀the฀processor฀core฀as฀ a฀nod฀to฀embedded฀interfacing.฀The฀power฀consumption,฀while฀low฀for฀a฀desktop฀processor,฀is฀ still฀fairly฀high฀for฀low-powered฀embedded฀applications. This฀reliable฀processor฀has฀enjoyed฀many฀years฀of฀success.฀The฀widespread฀availability฀of฀ quality฀development฀tools฀is฀certainly฀a฀boon฀for฀the฀part.฀ Next,฀we฀consider฀the฀AVR฀ATmega128.฀The฀following฀list฀captures฀the฀highlights฀of฀this฀ controller. ATmega128 ฀ 8-bit฀data฀bus ฀ Static฀core฀design;฀DC฀–฀16฀MHz ฀ 32฀×฀8-bit฀general฀purpose฀registers ฀ Up฀to฀16฀MIPs฀throughput฀at฀16฀MHz ฀ 128฀K-Bytes฀of฀internal฀ISP฀flash ฀ 4฀K-Bytes฀of฀internal฀EEPROM ฀ 4฀K-Bytes฀of฀internal฀SRAM ฀ Up฀to฀64฀K-bytes฀of฀external฀memory ฀ Two฀8-bit฀timer/counters ฀ Two฀16-bit฀timer/counters ฀ Two฀8-bit฀PWM฀channels ฀ Six฀PWM฀channels฀from฀2฀to฀16฀bits ฀ 8฀channel,฀10-bit฀ADC ฀ Two฀UARTS ฀ Watchdog฀timer ฀ 53฀I/O฀pins ฀ 18฀mA฀@฀16฀MHz,฀VCC฀=฀3.3฀volts This฀part฀is฀loaded฀with฀features฀that฀scream฀“embedded฀controller.”฀The฀power฀consumption฀is฀very฀low.฀There฀is฀an฀eight฀channel฀10-bit฀ADC฀on฀the฀die.฀The฀53฀pins฀of฀digital฀I/O฀is฀ certainly฀targeted฀at฀the฀embedded฀market. This฀controller฀has฀all฀of฀the฀program฀memory฀in฀flash฀and฀on฀board.฀A฀significant฀amount฀of฀ SRAM฀is฀also฀included.฀Many฀small฀embedded฀applications฀will฀fit฀into฀this฀memory฀model. 33

Chapter฀2 The฀4K฀of฀onboard฀EEPROM฀can฀be฀used฀to฀store฀calibration฀constants,฀log฀a฀small฀amount฀ of฀data,฀or฀even฀store฀system฀ID฀and฀configuration฀information.฀These฀are฀common฀tasks฀performed฀in฀embedded฀systems. If฀a฀larger฀data฀memory฀is฀required,฀some฀of฀the฀53฀I/O฀pins฀can฀be฀traded฀off฀and฀up฀to฀฀ 64฀K-bytes฀of฀external฀SRAM฀added.฀For฀many฀systems,฀64K฀of฀data฀storage฀is฀adequate.฀฀ But฀for฀many฀applications,฀the฀ATmega128’s฀memory฀model฀will฀be฀too฀limited. Even฀with฀a฀RISC-like฀ability฀to฀execute฀many฀instructions฀in฀a฀single฀clock฀cycle,฀a฀16฀MHz฀ speed฀is฀considered฀slow฀by฀many฀engineers.฀Depending฀on฀the฀application,฀clock฀speed฀may฀ not฀be฀an฀issue. Consider฀a฀small฀solar-powered฀controller฀that฀performs฀simple฀biophysical฀data฀acquisition.฀ One฀of฀the฀major฀design฀considerations฀for฀this฀type฀of฀system฀is฀power฀consumption.฀There฀ are฀two฀frontline฀techniques฀used฀to฀reduce฀power฀consumption.฀Run฀the฀system฀at฀a฀lower฀ voltage฀so฀the฀V2/R฀power฀is฀lowered.฀Run฀the฀system฀at฀a฀slow฀clock฀rate—power฀consumption฀is฀linearly฀proportional฀to฀clock฀speed. In฀this฀type฀of฀system,฀a฀16฀MHz฀clock฀might฀be฀ten฀or฀a฀hundred฀times฀faster฀than฀the฀ designer฀will฀need.฀In฀this฀case,฀the฀speed฀of฀the฀internal฀flash฀memory฀is฀not฀relevant฀when฀ selecting฀a฀controller. Next,฀we’ll฀briefly฀look฀at฀the฀Rabbit฀3000A.฀ Rabbit฀3000A ฀ 8-bit฀data฀bus ฀ 20-bit฀address฀bus ฀ Static฀Core฀Design;฀DC฀–฀54฀MHz ฀ Glueless฀memory฀interface ฀ Clock฀spreader฀for฀EMI฀reduction ฀ High฀frequency฀clock฀and฀32768฀Hz฀clock ฀ Built-in฀clock฀doubler ฀ 4฀levels฀of฀interrupt฀priority ฀ Bootable฀over฀serial฀port฀(cold฀boot฀feature) ฀ 56฀I/O฀signals฀(shared฀with฀serial฀ports฀and฀other฀peripherals) ฀ Four฀Pulse฀width฀modulation฀channels ฀ Six฀UARTs ฀ Auxiliary฀I/O฀bus฀reduces฀loading฀on฀memory฀bus ฀ Two฀input-capture฀channels ฀ Two฀quadrature฀decoder฀channels ฀ Built-in฀watchdog฀timer ฀ Standard฀10-pin฀programming฀port ฀ 65฀mA฀@฀30฀MHz,฀VCC฀=฀3.3฀volts In฀some฀ways,฀the฀Rabbit฀3000A฀strikes฀a฀middle฀ground฀between฀the฀Intel386฀EX฀and฀the฀ ATmega128.฀The฀20-bit฀address฀space฀is฀much฀bigger฀than฀the฀AVR฀but฀smaller฀than฀the฀฀ 34

The฀Basics 26-bit฀space฀offered฀on฀the฀Intel฀part.฀The฀power฀consumption฀is฀higher฀than฀the฀ATmega128,฀ but฀much฀lower฀than฀the฀Intel386. The฀Rabbit฀offers฀the฀highest฀clock฀rate฀of฀the฀three฀devices.฀This฀does฀not฀necessarily฀translate฀into฀higher฀computational฀performance.฀Internally,฀the฀Intel฀core฀is฀four฀times฀as฀wide฀and฀ the฀external฀data฀bus฀is฀twice฀as฀wide฀as฀the฀Rabbit.฀The฀Rabbit,฀on฀balance,฀requires฀more฀ clock฀cycles฀to฀execute฀an฀instruction฀than฀the฀AVR. One฀potential฀disadvantage฀of฀turning฀up฀the฀CPU฀clock฀speed฀is฀that฀faster฀external฀memory฀ is฀required.฀However,฀as฀time฀progressed,฀what฀was฀once฀“fast”฀expensive฀memory฀has฀ become฀“slow”฀cheap฀memory.฀Rabbit฀Semiconductor฀understood฀this฀when฀their฀processor฀was฀designed.฀This฀forward฀thinking฀has฀produced฀a฀snappy฀little฀part฀that฀today฀doesn’t฀ require฀expensive฀memory. Another฀potential฀disadvantage฀of฀running฀a฀part฀at฀high-speed฀is฀the฀increased฀power฀consumption฀of฀the฀CPU฀and฀memory.฀As฀clock฀speed฀increases,฀so฀does฀power฀consumption.฀ The฀Rabbit฀is฀a฀fully฀static฀core฀and฀can฀be฀run฀from฀DC฀to฀54฀MHz.฀ The฀Rabbit’s฀I/O฀mix฀is฀a฀lot฀more฀complete฀than฀either฀the฀Intel฀or฀Atmel฀parts.฀ The฀Rabbit’s฀feature฀rich฀I/O฀set฀is฀derived฀from฀Z-World’s฀firsthand฀knowledge฀of฀the฀ embedded฀market.฀Rabbit฀sports฀PWM฀channels,฀quadrature฀decoder฀channels,฀input฀capture฀ channels฀and฀a฀high฀digital฀I/O฀count. One฀of฀Rabbit’s฀more฀useful฀features฀is฀the฀inclusion฀of฀six฀UARTs.฀At฀first฀glance,฀six฀UARTs฀ might฀seem฀a฀bit฀extravagant,฀until฀one฀considers฀the฀nature฀of฀embedded฀communications.฀ The฀advent฀of฀clocked฀serial฀peripherals฀means฀that฀a฀system฀designer฀can฀add฀many฀types฀of฀ sophisticated฀I/O฀without฀loading฀the฀processor’s฀main฀bus.฀ADCs,฀DACs,฀RTCs,฀EEPROMs฀ and฀a฀host฀of฀other฀devices฀are฀available฀with฀clocked-serial฀interfaces฀(I2C™,฀SPI™฀and฀Microwire™).฀The฀Rabbit’s฀UARTs฀make฀expanding฀the฀system’s฀I/O฀features฀quick฀and฀inexpensive.฀ Rabbit’s฀auxiliary฀I/O฀bus฀is฀another฀tool฀that฀can฀be฀used฀to฀expand฀the฀I/O฀feature฀set฀ quickly฀and฀easily.฀In฀conventional฀processors,฀parallel฀I/O฀devices฀are฀placed฀on฀the฀CPU’s฀ data฀and฀address฀busses.฀This฀can฀lead฀to฀undesirable฀capacitive฀loading฀of฀the฀data฀and฀ address฀busses.฀Rabbit’s฀auxiliary฀I/O฀bus฀allows฀designers฀to฀place฀parallel฀I/O฀devices฀on฀a฀ bus฀completely฀separate฀from฀the฀memory฀bus. Another฀unique฀feature฀the฀Rabbit฀offers฀is฀a฀programming฀port฀that฀is฀used฀to฀program฀flash฀ memory฀connected฀to฀the฀CPU’s฀memory฀bus.฀This฀clever฀interface฀allows฀any฀flash฀memories฀ used฀on฀the฀CPU฀bus฀to฀be฀treated฀as฀ISP฀devices.฀The฀programming฀port฀allows฀a฀PC฀with฀a฀ serial฀connection฀and฀a฀bit฀of฀software฀to฀program฀flash฀memories฀through฀the฀Rabbit฀processor. 2.3.3฀Final฀Thoughts An฀engineer฀faced฀with฀selecting฀a฀microcontroller฀or฀microprocessor฀must฀be฀prepared฀to฀ weigh฀a฀number฀of฀factors฀and฀make฀trade-offs.฀Comparing฀microcontrollers฀is฀an฀exercise฀in฀ contrasting฀apples-to-oranges. Finding฀a฀controller฀with฀the฀right฀mix฀of฀computational฀speed,฀integrated฀peripherals฀and฀ development฀tools฀is฀an฀easier฀task฀than฀one฀might฀think.฀The฀difficulty฀is฀picking฀just฀one฀of฀ the฀many฀excellent฀solutions฀available.฀ 35

Chapter฀2

2.4฀A฀Rabbit’s฀Roots As฀we฀saw฀in฀the฀last฀section,฀a฀company’s฀design฀philosophy฀greatly฀influences฀the฀final฀ form฀of฀its฀products.฀In฀the฀next฀section,฀we฀will฀examine฀the฀Rabbit’s฀architecture฀in฀detail.฀ But฀before฀we฀do,฀it฀will฀be฀informative฀to฀have฀a฀look฀at฀Rabbit฀Semiconductor’s฀history฀and฀ understand฀the฀forces฀that฀have฀shaped฀its฀design฀philosophy. Our฀story฀starts฀in฀Silicon฀Valley฀in฀1981.฀At฀the฀age฀of฀40,฀Norm฀Rogers฀and฀co-worker฀Paul฀ Mennen฀quit฀their฀jobs฀at฀GenRad฀and฀with฀equal฀investments฀of฀$34,000฀formed฀Decmation.฀ Their฀original฀product฀was฀a฀plug-in฀card฀that฀allowed฀Digital฀Equipment฀Corporation฀minicomputers฀to฀run฀CP/M฀software.฀The฀idea฀wasn’t฀new.฀There฀was฀another฀product฀already฀ in฀the฀market฀that฀sold฀for฀around฀$2,500฀and฀did฀about฀the฀same฀thing.฀Decmation’s฀product฀ sold฀for฀$700.฀ In฀the฀first฀year,฀the฀little฀company฀had฀a฀quarter-million฀in฀sales.฀Marketing฀consisted฀of฀running฀small฀advertisements฀in฀the฀back฀of฀electronics฀magazines฀and฀attending฀the฀occasional฀ trade฀show.฀Orders฀were฀processed฀by฀telephone. Business฀remained฀strong฀and฀in฀1983,฀Norm฀mortgaged฀his฀home฀in฀San฀Jose,฀bought฀out฀ half฀of฀Paul’s฀interest฀in฀the฀company฀for฀$50,000฀and฀incorporated฀Decmation.฀From฀1983฀ on,฀Norm฀Rogers฀maintained฀the฀controlling฀interest฀in฀the฀company.฀ Norm฀poured฀time฀into฀research฀and฀development.฀By฀late฀1985฀he฀had฀his฀next฀big฀hit฀almost฀ complete.฀This฀product,฀named฀Blue฀Lightning,฀did฀for฀IBM฀PCs฀what฀the฀original฀product฀ did฀for฀DEC฀minicomputers.฀There฀were฀a฀large฀number฀of฀applications฀written฀for฀CP/M,฀but฀ since฀IBM฀had฀thrown฀in฀with฀Microsoft,฀MS฀DOS฀PC’s฀were฀in,฀and฀CP/M฀machines฀were฀ on฀their฀way฀out.฀Blue฀Lightning฀allowed฀IBM฀users฀to฀run฀their฀legacy฀CP/M฀applications.฀ Again,฀there฀was฀already฀competition฀in฀the฀market.฀The฀competitor’s฀well฀known฀product฀ was฀called฀the฀“Baby฀Blue,”฀a฀takeoff฀on฀the฀color฀of฀IBM฀computers.฀Decmation’s฀name,฀ Blue฀Lightning,฀was฀a฀shameless฀rip฀off.฀ By฀a฀stroke฀of฀luck฀the฀competitor฀went฀bankrupt฀the฀same฀day฀the฀Blue฀Lightning฀was฀introduced.฀The฀bankruptcy฀was฀caused฀by฀problems฀unrelated฀to฀the฀CP/M฀board฀business.฀ An฀improved฀Decmation฀design฀soon฀followed—Blue฀Thunder,฀which฀soon฀replaced฀Blue฀ Lightning. In฀an฀email฀sent฀to฀the฀authors฀of฀this฀book฀Norm฀Rogers฀recalls฀Blue฀Thunder:฀ “My฀champion฀product฀was฀the฀Blue฀Thunder฀12฀MHz฀card.฀At฀that฀time฀no฀Z80฀had฀ a฀faster฀official฀clock฀speed฀than฀6฀MHz.฀However฀I฀discovered฀that฀NEC฀chips฀would฀ actually฀run฀up฀to฀14฀MHz฀before฀they฀failed.฀I฀also฀discovered฀that฀one฀brand฀of฀ Japanese฀dynamic฀RAM฀would฀run฀fast฀enough฀to฀keep฀up฀with฀a฀12฀MHz฀Z80.฀So,฀ I฀put฀this฀all฀together,฀sanded฀off฀the฀part฀numbers฀so฀that฀no฀one฀could฀discover฀my฀ discovery,฀and฀sold฀12฀MHz฀Blue฀thunders฀for฀about฀3฀times฀what฀the฀6฀MHz฀ones฀ sold฀for.฀I฀sold฀a฀lot฀of฀them฀with฀a฀great฀profit฀margin.”฀—Norm฀Rogers,฀March฀2003 By฀1986,฀the฀company’s฀main฀business฀of฀CP/M฀simulators฀was฀waning.฀Customers฀were฀ phasing฀out฀CP/M-based฀software฀applications.฀In฀search฀of฀new฀products,฀Decmation฀tried฀ a฀number฀of฀different฀avenues,฀with฀varying฀success.฀The฀CP/M฀cards฀that฀plugged฀into฀PCs฀ 36

The฀Basics were฀redesigned฀with฀a฀serial฀port฀and฀made฀into฀“communications฀coprocessors”฀which฀ could฀offload฀serial฀communications฀from฀the฀PC’s฀CPU.฀ Another฀new฀business฀was฀development฀tools฀for฀Z80฀based฀embedded฀systems.฀A฀great฀ deal฀of฀very฀high฀quality฀CP/M฀based฀development฀tools฀were฀now฀considered฀obsolete฀and฀ were฀available฀for฀purchase฀at฀low฀prices.฀Decmation฀licensed฀and฀rebranded฀these฀as฀tools฀ for฀embedded฀systems฀engineers,฀either฀wrapped฀with฀a฀CP/M฀emulator฀or฀recompiled฀to฀be฀ MS-DOS฀applications.฀This฀change฀of฀business฀direction฀plus฀threatening฀letters฀from฀Digital฀Equipment฀Corporation฀(“DEC”)฀lawyers฀led฀to฀changing฀the฀name฀of฀the฀company฀from฀ Decmation฀to฀Z-World.฀The฀“Z”฀stood฀for฀the฀Z80฀microprocessor฀at฀which฀the฀development฀ tools฀were฀aimed. In฀1987,฀Norm฀moved฀from฀San฀Jose฀to฀Davis,฀California.฀His฀young฀children฀were฀school฀ age,฀and฀the฀public฀schools฀in฀Davis฀were฀better฀than฀the฀marginal฀schools฀in฀San฀Jose.฀฀ Z-World฀rented฀a฀1200฀square฀foot฀office฀in฀a฀small฀business฀park฀and฀had฀four฀employees.฀ Being฀host฀to฀the฀University฀of฀California,฀Davis฀had฀eager฀young฀college฀students.฀A฀towering฀young฀computer฀science฀student฀who฀had฀independently฀written฀some฀Z80฀development฀ tools฀made฀contact฀with฀Norm.฀Norm฀recognized฀the฀energy฀and฀genius฀of฀Greg฀Young,฀and฀ Z-World฀quickly฀hired฀him.฀Greg฀wrote฀the฀first฀integrated฀development฀environment฀(IDE)฀ for฀Dynamic฀C.฀Greg฀would฀later฀go฀on฀to฀setup฀and฀manage฀the฀technical฀support฀group฀and฀ serve฀as฀a฀design฀engineer,฀and฀today,฀still฀consults฀on฀special฀projects. In฀1989,฀Z-World฀brought฀in฀a฀professional฀marketing฀manager฀by฀the฀name฀of฀Carrie฀Maha.฀ While฀she฀was฀new฀to฀the฀burgeoning฀embedded฀systems฀market,฀she฀brought฀an฀organization฀and฀polish฀that฀Z-World฀needed.฀No฀longer฀would฀marketing฀consist฀simply฀of฀black฀and฀ white฀ads฀in฀the฀backs฀of฀magazines.฀Carrie฀built฀a฀world-class฀marketing฀and฀sales฀organization฀from฀the฀ground฀up฀and฀today฀serves฀as฀the฀Executive฀Vice฀President. The฀first฀Z-World฀embedded฀platform,฀dubbed฀the฀Little฀Giant,฀combined฀a฀Z180฀microprocessor฀and฀practical฀I/O฀designed฀to฀control฀and฀supervise฀real-world฀embedded฀systems.฀ Z-World’s฀Dynamic฀C฀development฀system,฀written฀by฀Norm฀and฀Greg,฀made฀it฀easy฀to฀ program฀the฀new฀board.฀Bundled฀with฀Dynamic฀C,฀this฀product฀was฀an฀instant฀hit฀when฀it฀was฀ introduced฀in฀1990.฀The฀Little฀Giant฀still฀sells฀well฀today฀as฀a฀legacy฀product฀to฀the฀original฀ OEMs.฀The฀vertical฀integration฀of฀controller฀hardware฀and฀development฀tools฀would฀propel฀ Z-World฀through฀the฀decade฀and฀into฀the฀next฀century.฀ By฀1997,฀the฀company฀had฀moved฀twice฀to฀accommodate฀its฀rapid฀growth.฀Revenue฀was฀up,฀ profit฀was฀up฀and฀the฀stage฀was฀set฀for฀another฀technological฀leap.฀Norm฀Rogers฀had฀spent฀the฀ better฀part฀of฀two฀decades฀working฀around฀the฀shortcomings฀of฀the฀Z80฀and฀Z180.฀Ten฀years฀ of฀tweaking฀compilers฀and฀building฀hardware฀had฀given฀Norm฀a฀well-rounded฀and฀intimate฀ view฀of฀the฀relationship฀between฀hardware,฀the฀firmware฀that฀runs฀on฀it฀and฀the฀software฀tools฀ used฀to฀create฀the฀firmware.฀Norm฀was฀ready฀to฀build฀a฀new฀processor. In฀1997,฀Norm฀was฀introduced฀to฀Pedram฀Abolghasem,฀an฀ambitious฀electrical฀engineering฀student฀at฀Sacramento฀State฀University.฀Upon฀graduating,฀Pedram฀joined฀the฀company฀ to฀spearhead฀the฀R฀&฀D฀efforts฀behind฀the฀first฀microprocessor฀that฀would฀address฀the฀many฀ weaknesses฀of฀the฀Z180฀design. 37

Chapter฀2 Embarking฀on฀a฀methodical฀search฀for฀a฀partner฀who฀had฀already฀ridden฀the฀ASIC฀pony,฀ Pedram฀located฀a฀one฀man฀consulting฀company,฀Systemyde฀International,฀owned฀by฀former฀Zilog฀designer฀Monte฀Dalrymple.฀With฀15฀patents฀credited฀to฀his฀name฀and฀experience฀ as฀Principal฀Architect฀and฀Designer฀on฀Zilog฀products฀such฀as฀the฀Z16C30,฀Z16C32฀and฀ Z16C35,฀Monte฀had฀the฀experience฀needed฀to฀bring฀Norm’s฀ideas฀to฀fruition. In฀September฀1997,฀the฀development฀of฀the฀Rabbit฀2000฀began฀in฀earnest.฀Norm,฀Pedram฀and฀ Monte฀worked฀tirelessly฀to฀move฀the฀project฀forward.฀Norm฀supplied฀the฀architecture.฀Monte,฀ as฀a฀consultant,฀wrote฀the฀Verilog฀code฀and฀provided฀mature฀advice฀to฀Norm฀and฀Pedram.฀ Initially,฀Pedram฀was฀the฀only฀onsite฀engineer฀assigned฀to฀the฀project.฀This฀meant฀he฀was฀the฀ catch-all฀for฀work.฀Pedram฀designed฀printed฀circuit฀boards,฀using฀the฀largest฀and฀most฀state฀ of฀the฀art฀Altera’s฀FPGAs,฀with฀which฀to฀test฀Monte’s฀Verilog฀code.฀Pedram฀found฀numerous฀ bugs.฀Monte฀fixed฀them.฀In฀the฀end,฀Pedram฀and฀Monte฀turned฀Norm’s฀ideas฀into฀silicon.฀ The฀entire฀development฀costs฀of฀the฀Rabbit฀2000฀amounted฀to฀less฀than฀$500,000.฀This฀was฀ only฀1/40th฀the฀cost฀that฀some฀other฀companies฀were฀spending฀at฀the฀time฀in฀their฀microprocessor฀development฀efforts—many฀of฀which฀failed.฀ In฀late฀1999,฀Rabbit฀Semiconductor฀was฀launched฀to฀continue฀and฀support฀the฀microprocessor฀ development.฀In฀November฀1999,฀the฀Rabbit฀2000฀processor฀hit฀the฀streets.฀Its฀success฀was฀ followed฀by฀the฀Rabbit฀3000฀in฀2001,฀and฀the฀Rabbit฀3000A฀in฀2002.฀The฀Rabbit฀4000฀is฀in฀ the฀works.฀ Norm฀Rogers฀is฀still฀majority฀shareholder฀and฀company฀president฀of฀both฀Z-World฀and฀Rabbit฀ Semiconductor.฀Pedram฀is฀the฀Director฀of฀Engineering.฀Monte฀continues฀to฀work฀closely฀with฀ Norm฀and฀Pedram฀on฀Rabbit’s฀new฀designs.฀ With฀the฀advent฀of฀the฀Rabbit฀line฀of฀processors,฀Z-World฀and฀Rabbit฀Semiconductor฀have฀ attained฀a฀degree฀of฀vertical฀integration฀not฀seen฀in฀the฀embedded฀market฀to฀date.฀Customers฀can฀choose฀from฀a฀plethora฀of฀off฀the฀shelf฀solutions฀from฀processors฀to฀fully฀packaged฀ controllers.฀The฀Dynamic฀C฀platform,฀now฀a฀32-bit฀Microsoft฀Windows-based฀application,฀ supports฀C฀and฀assembly฀language฀development฀as฀well฀as฀debugging฀on฀all฀the฀target฀hardware.฀The฀Rabbit฀processors฀have฀enjoyed฀wide฀industry฀acceptance฀and฀are฀supported฀by฀ third฀party฀tools.

2.5฀Rabbit฀in฀Detail As฀the฀rest฀of฀the฀book฀uses฀the฀Rabbit฀3000฀microprocessor฀for฀all฀projects,฀an฀examination฀of฀ the฀chip’s฀architecture฀is฀in฀order.฀Figure฀2.1฀shows฀a฀top-level฀block฀diagram฀of฀Rabbit฀3000. 2.5.1฀The฀CPU At฀the฀heart฀of฀the฀Rabbit฀3000฀is฀the฀CPU฀block.฀As฀seen฀in฀Figure฀2.1,฀all฀external฀data฀is฀ transferred฀to฀and฀from฀the฀external฀buses฀to฀the฀CPU.฀The฀CPU฀registers฀act฀as฀a฀source฀or฀ destination฀for฀all฀external฀bus฀transfers. The฀CPU฀communicates฀with฀the฀internal฀peripherals฀using฀an฀internal฀bus.฀This฀bus฀consists฀ of฀an฀8-bit฀internal฀data฀bus,฀8-bit฀internal฀address฀bus฀and฀sundry฀internal฀arbitration฀signals.฀ The฀8-bit฀internal฀address฀bus฀means฀the฀Rabbit฀3000฀is฀limited฀to฀256฀internal฀peripheral฀ I/O฀registers.฀It฀also฀means฀access฀to฀the฀registers฀doesn’t฀require฀a฀two฀byte฀operand฀fetch฀to฀ 38

The฀Basics D[7:0]

Data Buffer

RESOUT/

External Interface

RESET

Memory Chip Interface

/CS2, /CS1, /CS0 / OE1, /OE0 /WE1, /WE0

A[19:0]

Address Buffer

ID[7:0] IA[ 5:0] I[ 7:0]

External I /O Chip Interface

/IOWR

Memory Management & Control Unit

/IORD /BUFEN

CPU

SMODE0 SMODE1 STATUS /WDTOUT

INT0A, INT1A INT0B, INT1B XTAL1 XTAL2

External Interrupts

Fast Oscillator

CLK

Spectrum Spreader

Clock Doubler

32.768 kHz Clock Input

CLK32K

Global Power Save & Clock Distribution SD[ 7:0] SA[1:0] / SCS, / SRD, /SWR / SLAVEATTN

Slave Port & Bootstrap

Timer A Serial Port A Asynchronous Serial Synchronous Serial Asynchronous Bootstrap Synchronous Bootstrap Asynchronous IrDA IrDA Bootstrap

Timer B Parallel Ports PA[7:0]

Port A

PB[7:0]

Port B

PC[7:0]

Port C

PD[7:0]

Port D

PE[7:0]

Port E

PF [7: 0]

Port F

PG[ 7:0]

Port G

Real-Time Clock Watchdog Timer

Serial Ports B, C, D Asynchronous Serial Synchronous Serial Asynchronous IrDA

Periodic Interrupt

TXB, RXB, CLKB ATXB, ARXB TXC, RXC, CLKC TXD, RXD, CLKD

Serial Ports E, F

Pulse Width Modulation

PWM[3: 0]

Quadrature Decoder

QD1A, QD2A, AQD1A, AQD2A,

Asynchronous Serial HDLC / SDLC Asynchronous IrDA HDLC / SDLC IrDA

Input Capture

QD1B QD2B AQD1B AQD2B

TXA,RXA ATXA, ARXA CLKA

TXE, RXE, TCLKE, RCLKE TXF, RXF, TCLKF , RCLKF

PC[ 7,5,3, 1] PD[ 7,5,3, 1] PF[7.5. 3.1] PG [7, 5,3,1]

Figure฀2.1:฀A฀bird’s-eye฀view฀of฀the฀Rabbit฀3000฀shows฀a฀feature฀rich฀architecture.

acquire฀a฀complete฀16-bit฀address.฀This฀ increases฀the฀Rabbit’s฀efficiency. The฀CPU฀block,฀while฀similar฀to฀the฀ Z180฀CPU,฀has฀some฀important฀differences.฀A฀diagram฀of฀the฀registers฀ available฀in฀the฀CPU฀block฀is฀shown฀฀ in฀Figure฀2.2.

S - SIGN BIT Z - ZERO BIT X - NOT IMPLEMENTED X - NOT IMPLEMENTED X - NOT IMPLEMENTED V - OVERFLOW BIT X - NOT IMPLEMENTED C - CARRY BIT

A

S

Z

X

X

X

V

X

C

F B

C

IX

IP

D

E

IY

IIR

H

L

SP

EIR

PC

XPC

A' F'

Figure฀2.2:฀The฀Rabbit฀CPU฀has฀ an฀abundance฀of฀registers.

B'

C'

D'

E'

H'

L'

8-BIT REGISTERS

16-BIT REGISTERS

16-BIT PAIRED REGISTERS

A - ACCUMULATOR F - (STATUS) FLAGS B, C, D, E, H, L A ', B ', C ', D ', E ', F ', H ', L ' IP - INTERRUPT PRIORITY IIR - INTERNAL INTERUPT EIR - EXTERNAL INTERUPT XPC - MMU REGISTER

IX - INDEX X IY - INDEX Y SP - STACK POINTER PC - PROGRAM COUNTER

BC DE H L - 16-BIT ACCUMULATOR B'C ' D'E' H'L'

39

Chapter฀2 Those฀familiar฀with฀the฀Z80฀and฀Z180฀will฀immediately฀recognize฀the฀familiar฀general฀purpose฀registers฀(A,฀F,฀B,฀C,฀D,฀E,฀H,฀L)฀and฀the฀alternate฀register฀set฀(A',฀F',฀B',฀C',฀D',฀E',฀H',฀ L').฀Rabbit฀Semiconductor฀has฀added฀some฀new฀registers฀and฀extended฀the฀functionality฀of฀the฀ traditional฀complement. The฀alternate฀register฀set฀was฀originally฀used฀to฀store฀the฀contents฀of฀the฀regular฀register฀set฀ when฀a฀special฀function฀was฀called—often฀an฀interrupt฀service฀routine฀(ISR).฀ The฀EXX฀instruction฀exchanges฀the฀contents฀of฀HL,฀BC,฀DE฀with฀HL',฀BC',฀DE'.฀The฀ instruction฀EX฀AF,฀AF'฀treats฀the฀resisters฀A฀and฀F฀as฀a฀register฀pair฀and฀exchanges฀the฀ contents฀with฀AF'.฀In฀the฀Z80/Z180฀the฀exchange฀instructions฀were฀the฀only฀way฀to฀access฀ the฀contents฀of฀the฀alternate฀registers.฀This฀made฀using฀the฀alternate฀registers฀for฀data฀ manipulation฀cumbersome฀at฀best.฀Rabbit฀has฀added฀a฀couple฀of฀instructions฀that฀make฀using฀ the฀alternate฀registers฀as฀general฀purpose฀registers฀easier.฀Rabbit฀states฀that฀this฀“effectively฀ doubles฀the฀number฀of฀registers฀that฀are฀easily฀available.” The฀alternate฀registers฀are฀still฀a฀bit฀cumbersome฀to฀use฀for฀computation฀or฀data฀manipulation฀ as฀compared฀to฀their฀regular฀register฀counterparts.฀They฀are฀by฀no฀means฀“easy”฀in฀the฀sense฀ that฀one฀might฀assume฀that฀the฀regular฀registers฀and฀alternate฀registers฀are฀orthogonal฀from฀an฀ opcode฀point฀of฀view.฀However,฀the฀Rabbit฀instructions฀have฀breathed฀new฀life฀into฀an฀awkward฀legacy฀feature.฀ The฀Rabbit฀CPU,฀like฀its฀predecessors,฀is฀still฀an฀accumulator-based฀architecture.฀The฀8-bit฀ A฀register฀is฀the฀accumulator฀for฀8-bit฀operations.฀For฀16-bit฀operations,฀the฀HL฀register฀pair฀ is฀the฀accumulator.฀In฀some฀circumstances,฀the฀IX฀and฀IY฀registers฀can฀be฀used฀as฀a฀16-bit฀ accumulator. The฀IIR฀and฀EIR฀registers฀point฀to฀interrupt฀tables฀for฀internal฀and฀external฀interrupt฀generators.฀The฀Z80฀R฀register฀is฀logically฀analogous฀to฀the฀Rabbit’s฀IIR฀register. The฀IP฀register฀is฀new.฀The฀8-bit฀register฀is฀divided฀into฀four฀2-bit฀fields.฀The฀fields฀are฀used฀to฀ hold฀a฀history฀of฀the฀Interrupt฀priority.฀This฀is฀an฀important฀tool฀for฀managing฀the฀4฀levels฀of฀ interrupt฀priority฀the฀Rabbit฀sports. The฀XPC฀register฀is฀also฀new,฀and฀there฀is฀some฀debate฀as฀to฀whether฀this฀register฀should฀ be฀treated฀as฀part฀of฀the฀CPU฀core฀architecture฀or฀as฀part฀of฀the฀Memory฀Management฀Unit฀ (MMU).฀The฀register฀establishes฀where฀the฀XPC฀Segment฀maps฀in฀physical฀memory.฀However,฀instructions฀operate฀on฀XPC฀like฀a฀CPU฀register฀rather฀than฀an฀I/O฀mapped฀peripheral฀ register.฀We,฀like฀Rabbit,฀will฀treat฀XPC฀as฀a฀CPU฀register. Rabbit’s฀designers฀explain฀that฀the฀XPC฀was฀created฀as฀a฀CPU฀register฀so฀that฀XPC฀can฀be฀ accessed฀quickly.฀When฀we฀talk฀about฀the฀MMU,฀we’ll฀see฀that฀the฀XPC฀register฀is฀used฀to฀ locate฀the฀Extended฀Memory฀Segment.฀Since฀this฀is฀the฀primary฀avenue฀for฀accessing฀the฀bulk฀ of฀the฀Rabbit’s฀external฀20-bit฀address฀space,฀the฀Extended฀Memory฀Segment฀is฀remapped฀ frequently.฀Saving฀a฀few฀clock฀cycles฀on฀each฀access฀to฀the฀XPC฀register฀will฀favorably฀ impact฀code฀execution฀on฀Rabbit-based฀systems฀using฀Dynamic฀C.

40

The฀Basics Other฀MMU฀registers฀are฀implemented฀as฀I/O฀mapped฀peripheral฀registers฀and฀do฀not฀show฀ up฀in฀the฀CPU.฀The฀only฀MMU฀related฀register฀afforded฀the฀privilege฀of฀being฀a฀proper฀CPU฀ register฀is฀the฀XPC฀register. The฀IX฀and฀IY฀registers฀are฀index฀registers.฀They฀are฀commonly฀used฀as฀pointers฀to฀memory฀ locations.฀The฀HL฀register฀pair฀can฀also฀be฀used฀as฀a฀pointer฀into฀memory. 2.5.2฀Parallel฀I/O The฀Rabbit฀3000฀has฀seven฀8-bit฀digital฀I/O฀ports.฀The฀56฀I/O฀pins฀share฀functions฀with฀other฀ on-chip฀peripherals.฀For฀example,฀if฀a฀system฀design฀requires฀a฀serial฀port฀then฀some฀number฀ of฀digital฀I/O฀pins฀will฀be฀traded-off.฀This฀is฀common฀in฀microcontrollers.฀ The฀Rabbit฀3000฀I/O฀ports฀have฀several฀desirable฀features.฀One฀of฀the฀biggest฀practical฀concerns฀is฀the฀ability฀to฀interface฀to฀5-volt฀logic.฀The฀Rabbit฀3000฀is฀designed฀to฀operate฀on฀a฀฀ 1.8฀–฀3.6฀volt฀supply.฀However,฀all฀of฀the฀Rabbit’s฀inputs฀(with฀the฀exception฀of฀the฀oscillator฀ and฀power฀pins)฀are฀fully฀5-volt฀tolerant. Rabbit฀I/O฀pins฀are฀powered฀from฀a฀separate฀set฀of฀power฀supply฀pins฀than฀the฀CPU฀core.฀The฀ VDDIO฀pins฀supply฀current฀to฀the฀I/O฀pins.฀VDDCORE฀pins฀supply฀current฀to฀the฀CPU฀core.฀ A฀savvy฀designer฀can฀reduce฀overall฀EMI฀by฀confining฀the฀CPU’s฀noisy฀harmonic-rich฀power฀ supply฀to฀a฀very฀limited฀physical฀area฀on฀the฀PCB.฀The฀I/O฀pins฀can฀be฀powered฀from฀a฀separate฀(filtered)฀supply฀rail.฀The฀EMI฀generated฀as฀part฀of฀the฀fast฀oscillator฀and฀CPU฀switching฀ will฀not฀be฀passed฀to฀the฀I/O฀pins฀and฀therefore฀not฀be฀conducted฀on฀the฀I/O฀lines.฀Compliance฀ with฀FCC฀and฀CE฀standards฀will฀be฀much฀easier฀to฀achieve฀with฀this฀arrangement. The฀Rabbit฀I/O฀pins฀on฀ports฀D,฀E,฀F฀and฀G฀have฀the฀option฀to฀synchronize฀their฀output฀ updates฀with฀a฀timer.฀Figure฀2.3฀shows฀a฀conceptual฀diagram฀of฀an฀output฀with฀the฀timer฀ synchronization฀option. HOLDING REGISTER CPU DATA CPU GENERATED LOAD CLOCK

D

OUTPUT REGISTER

Q

D

CLK

Q

OUTPUT PORT

CLK

TIMER GENERATED CLOCK

Figure฀2.3:฀Timer฀synchronized฀outputs฀allow฀for฀precise฀timing฀of฀output฀pulses.

For฀systems฀that฀need฀to฀generate฀precisely฀timed฀pulses,฀timer฀synchronization฀is฀an฀excellent฀feature.฀The฀timer฀that฀updates฀the฀port฀can฀also฀be฀used฀to฀generate฀an฀interrupt.฀The฀ISR฀ can฀load฀the฀next฀scheduled฀value฀into฀the฀first฀flip-flop฀holding฀register฀in฀the฀output฀chain.฀ The฀next฀time฀the฀timer฀trips,฀the฀output฀will฀be฀updated฀and฀the฀ISR฀called฀again.฀This฀cycle฀ can฀be฀repeated฀indefinitely. Timers฀A1,฀B1฀and฀B2฀can฀be฀used฀to฀provide฀the฀synchronized฀update฀clock.฀ Another฀clock฀source฀available฀to฀update฀the฀output฀registers฀synchronously฀is฀the฀peripheral฀ clock฀(PCLK).฀The฀“global฀power฀save฀and฀clock฀distribution”,฀as฀seen฀in฀Figure฀2.1,฀generates฀PCLK฀from฀either฀the฀fast฀clock฀crystal฀(divided฀by฀2,฀4,฀6฀or฀8)฀or฀the฀32.768฀KHz฀clock. 41

Chapter฀2 Applications฀that฀can฀take฀advantage฀of฀this฀feature฀include฀Pulse฀code฀modulation฀(PCM)฀ audio฀playback.฀Pulse฀width฀modulated฀(PWM)฀DACs฀can฀also฀benefit฀from฀synchronized฀bit฀ transitions. 2.5.3฀Rabbit฀Serial฀Ports Rabbit฀has฀six฀serial฀ports.฀At฀first฀glance,฀this฀may฀seem฀like฀an฀over฀abundance.฀However,฀ this฀is฀one฀of฀the฀features฀that฀can฀trace฀roots฀back฀to฀Z-World.฀Over฀the฀years,฀one฀of฀the฀ most฀often฀requested฀features฀for฀Z-World’s฀embedded฀controllers฀was฀“more฀serial฀ports.”฀ The฀Rabbit฀3000฀delivered. All฀six฀serial฀ports฀may฀be฀used฀as฀simple฀three฀wire฀asynchronous฀serial฀ports฀with฀a฀maximum฀bit฀rate฀of฀the฀system฀clock฀divided฀by฀eight.฀This฀mode฀is฀suitable฀for฀driving฀RS-232฀ transceivers฀if฀no฀hardware฀handshaking฀is฀required. In฀asynchronous฀mode,฀a฀7-bit฀or฀8-bit฀data฀length฀can฀be฀selected,฀with฀optional฀parity.฀A฀ special฀9-bit฀protocol฀is฀also฀supported฀in฀which฀a฀9th฀bit฀is฀set฀or฀cleared฀to฀mark฀the฀first฀byte฀ of฀a฀block฀transfer. Embedded฀systems฀commonly฀employ฀protocols฀other฀than฀RS-232.฀One฀of฀the฀most฀common฀is฀the฀three฀wire฀RS-485฀interface. RS-485฀has฀an฀A฀and฀B฀line฀in฀addition฀to฀the฀ground-reference.฀This฀half-duplex฀protocol฀ is฀sensitive฀to฀the฀difference฀in฀voltage฀between฀A฀and฀B.฀If฀the฀voltage฀A-B฀is฀positive฀and฀ greater฀than฀200฀mV฀then฀the฀data฀is฀a฀one.฀If฀the฀voltage฀A-B฀is฀negative฀and฀greater฀than฀฀ 200฀mV฀then฀the฀data฀is฀a฀ZERO. RS-485฀is฀designed฀so฀that฀many฀nodes฀can฀share฀the฀same฀A฀and฀B฀signals.฀Only฀one฀node฀is฀ allowed฀to฀transmit฀at฀a฀time.฀This฀feature฀is฀why฀RS-485฀is฀referred฀to฀a฀“multi-drop฀protocol.” One฀issue฀that฀Z-World฀discovered฀early฀on฀with฀the฀Z180฀UART฀is฀that฀detecting฀when฀ the฀last฀bit฀was฀finally฀shifted฀out฀of฀the฀output฀register฀and฀onto฀an฀RS-485฀bus฀was฀very฀ difficult.฀This฀made฀turning฀around฀the฀RS-485฀bus฀a฀bit฀tricky.฀If฀the฀RS-485฀transmitter฀ were฀disabled฀too฀quickly,฀data฀would฀be฀lost.฀If฀too฀much฀time฀elapsed฀before฀the฀bus฀was฀ released,฀then฀the฀RS-485฀transmitter฀might฀cause฀bus฀contention฀with฀another฀node. Building฀on฀Z-World’s฀experience,฀Rabbit฀Semiconductor฀enabled฀the฀Rabbit฀3000฀UARTs฀ to฀generate฀an฀interrupt฀when฀the฀last฀bit฀is฀finally฀shifted฀out฀on฀to฀the฀bus.฀This฀is฀how฀the฀ Rabbit฀can฀quickly฀and฀safely฀turn฀off฀the฀RS-485฀transmitter,฀freeing฀the฀bus฀for฀traffic฀originating฀from฀another฀node. Ports฀A,฀B,฀C฀and฀D฀can฀also฀be฀operated฀in฀synchronous฀mode.฀This฀is฀sometimes฀called฀ “clocked฀serial”฀mode. When฀operating฀in฀synchronous฀mode,฀either฀the฀Rabbit฀3000฀or฀an฀external฀device฀can฀generate฀the฀clock.฀If฀the฀Rabbit฀3000฀generates฀the฀clock,฀the฀maximum฀clock฀rate฀is฀equal฀to฀the฀ system฀clock฀divided฀by฀two.฀If฀an฀external฀device฀generates฀the฀clock,฀then฀the฀maximum฀ clock฀rate฀is฀limited฀to฀the฀Rabbit’s฀system฀clock฀divided฀by฀six.฀In฀this฀mode,฀SPI฀devices฀ may฀be฀attached฀to฀the฀UARTs.

42

The฀Basics Ports฀E฀and฀F฀support฀HDLC/SDLC฀communication.฀This฀protocol฀supports฀frame฀checking,฀ clock฀recovery฀and฀error฀checking.฀HDLC฀has฀found฀use฀in฀high-end฀router฀back-channel฀ communication฀interfaces,฀and฀internal฀mainframe฀computer฀busses.฀HDLC฀is฀not฀commonly฀ supported฀by฀microprocessors฀in฀Rabbit’s฀price฀range.฀If฀HDLC฀is฀required฀for฀an฀application,฀ a฀designer฀often฀has฀to฀purchase฀special฀USARTs฀to฀accommodate฀the฀requirement. The฀Rabbit฀3000฀supports฀NRZ,฀NRZI,฀Biphase-level฀(Manchester),฀Biphase-space฀(FM0)฀ and฀Biphase-mark฀(FM1)฀encoding.฀A฀digital฀phase-locked฀loop฀(DPLL)฀is฀used฀to฀recover฀ the฀data฀clock฀from฀the฀received฀signal. A฀standard฀CRC-CCITT฀polynomial฀(x16฀+฀x12฀+฀x5฀+฀1)฀is฀used฀to฀generate฀the฀cyclic฀redundancy฀check฀(CRC)฀code.฀Frame฀flags฀are฀generated฀automatically.฀Framing฀errors฀are฀also฀ detected฀automatically. The฀HDLC฀protocol฀is฀used฀for฀high-speed฀communications฀using฀infrared฀IrDA฀transceivers฀ in฀FIR฀mode. Port฀A฀can฀be฀used฀to฀cold-boot฀(also฀called฀bootstrap)฀the฀Rabbit฀processor.฀Normally,฀upon฀ reset,฀the฀processor฀begins฀executing฀code฀from฀the฀memory฀located฀at฀physical฀address฀ 0x00000.฀As฀a฀general฀rule,฀Rabbit-based฀systems฀will฀have฀a฀ROM฀or฀flash฀memory฀located฀ at฀0x00000.฀In฀the฀case฀that฀the฀flash฀memory฀is฀not฀programmed,฀or฀in฀systems฀that฀operate฀ purely฀from฀RAM,฀the฀processor฀can฀be฀forced฀to฀acquire฀data฀from฀a฀bootstrap฀port. SMODE1฀and฀SMODE0฀pins฀control฀the฀bootstrap฀operation.฀When฀they฀are฀both฀zero,฀the฀ processor฀boots฀normally.฀When฀SMODE1฀and฀SMODE0฀are฀{1,0},฀the฀Rabbit฀will฀boot฀ from฀serial฀port฀A฀in฀a฀synchronous฀mode.฀The฀Rabbit฀requires฀an฀external฀device฀to฀provide฀ the฀clock฀for฀the฀synchronous฀boot.฀This฀precludes฀the฀use฀of฀an฀EEPROM฀as฀a฀boot฀device. When฀SMODE1฀and฀SMODE0฀are฀{1,1},฀the฀Rabbit฀will฀boot฀from฀serial฀port฀A฀in฀asynchronous฀mode.฀The฀communication฀rate฀is฀2400฀baud฀and฀is฀derived฀from฀the฀32.768฀kHz฀clock. When฀the฀Rabbit฀is฀bootstrapping,฀it฀expects฀to฀see฀three-byte฀messages,฀called฀triplets.฀Each฀ triplet฀is฀composed฀of฀a฀2-byte฀address฀and฀one฀byte฀of฀data.฀Each฀triplet฀causes฀the฀Rabbit฀ to฀store฀the฀data฀byte฀at฀the฀16-bit฀address฀with฀the฀exception฀that฀if฀the฀most฀significant฀bit฀ (MSB)฀of฀the฀address฀is฀a฀1,฀then฀the฀data฀will฀be฀written฀into฀the฀addressed฀onboard฀peripheral฀register. This฀allows฀a฀bootloader฀program฀to฀be฀loaded฀into฀SRAM฀and฀the฀MMU฀registers฀to฀be฀configured฀so฀that฀the฀SRAM฀appears฀at฀physical฀address฀0x0000. The฀sequence฀0x80,฀0x24,฀0x80฀will฀cause฀0x80฀to฀be฀written฀to฀the฀internal฀peripheral฀register฀0x24.฀This฀will฀terminate฀bootstrap฀operation;฀it฀allows฀the฀newly฀loaded฀code฀to฀begin฀ executing฀at฀address฀0x00000. 2.5.4฀Slave฀Port In฀the฀past,฀processor-to-processor฀communications฀was฀somewhat฀difficult.฀To฀overcome฀this฀ problem,฀dual-ported฀RAM฀has฀often฀been฀used฀to฀provide฀a฀shared฀space฀accessible฀to฀two฀ separate฀processors.฀Even฀with฀silicon฀prices฀on฀an฀exponentially฀decreasing฀cost฀curve,฀dualporting฀is฀expensive.฀Lead฀times฀for฀such฀specialty฀items฀can฀be฀long. 43

Chapter฀2 Dedicated฀serial฀channels฀are฀another฀scheme฀often฀employed฀to฀communicate฀between฀ processors.฀This฀approach฀requires฀few฀wires฀and฀UARTs฀are฀fairly฀inexpensive.฀For฀many฀ applications,฀a฀dedicated฀serial฀port฀is฀a฀reasonable฀solution฀to฀processor-to-processor฀communication.฀The฀Rabbit,฀as฀we฀have฀just฀seen,฀sports฀six฀serial฀ports. Sometimes,฀a฀designer฀may฀not฀have฀an฀extra฀serial฀port.฀Or฀perhaps฀the฀processors฀are฀running฀on฀slow-speed฀clocks฀to฀conserve฀power฀and฀the฀available฀serial฀port฀bandwidth฀is฀overly฀ limited.฀In฀such฀cases,฀the฀Rabbit฀processor฀offers฀a฀processor-to-processor฀parallel฀communication฀channel฀called฀a฀slave฀port. The฀chosen฀moniker฀is฀a฀bit฀unfortunate.฀One฀often฀thinks฀of฀“slave”฀devices฀being฀dumb.฀ The฀Rabbit฀“slave฀port”฀is฀best฀thought฀of฀as฀a฀specialized฀communications฀port฀for฀highefficiency฀processor-to-processor฀communications. The฀slave฀port฀allows฀a฀Rabbit฀processor฀to฀be฀placed฀on฀a฀second฀Rabbit฀processor’s฀I/O฀bus฀ and฀appear฀as฀a฀peripheral.฀The฀slave฀port฀implements฀six฀registers.฀Three฀of฀these฀can฀be฀ written฀from฀the฀external฀bus฀and฀read฀by฀the฀“slave”฀processor’s฀internal฀CPU.฀The฀remaining฀three฀registers฀can฀be฀written฀by฀the฀slave฀processor’s฀CPU฀and฀read฀from฀the฀external฀bus. These฀six฀slave฀port฀registers฀don’t฀act฀exactly฀as฀a฀dual฀port฀RAM,฀but฀they฀do฀provide฀a฀ means฀of฀high-speed฀parallel฀communication฀between฀two฀Rabbits.฀Although฀the฀following฀ examples฀show฀Rabbit-to-Rabbit฀connections,฀there฀is฀nothing฀that฀prohibits฀the฀Rabbit฀slave฀ port฀from฀connecting฀to฀another฀processor฀or฀even฀a฀CPLD฀or฀FPGA. Figure฀2.4฀shows฀a฀diagram฀of฀the฀slave฀port฀signals.฀The฀data฀path฀is฀8-bits฀wide.฀The฀two฀ address฀signals฀act฀as฀register฀selects.฀The฀/SWR฀and฀/SRD฀determine฀which฀way฀data฀will฀ be฀flowing฀on฀the฀bus.฀The฀/SCS฀signal฀enables฀the฀port.฀/SLAVEATTN฀can฀be฀used฀by฀the฀ Rabbit฀to฀signal฀the฀“master”฀that฀the฀“slave”฀requests฀a฀transaction. The฀slave฀port฀is฀versatile;฀the฀CPUs฀may฀be฀“masters”฀ or฀“slaves”฀or฀even฀both.฀Figure฀2.5฀illustrates฀how฀such฀ creativity฀might฀manifest฀in฀a฀design. The฀slave฀port฀opens฀up฀the฀possibility฀of฀building฀very฀ smart฀peripherals฀yet฀having฀them฀communicate฀with฀other฀ system฀processors฀over฀the฀I/O฀bus.฀For฀example,฀consider฀ a฀Rabbit฀“slave”฀processor฀that฀acts฀as฀a฀communication฀ subsystem.฀The฀slave฀might฀have฀serial฀streams฀from฀six฀ separate฀I/O฀devices฀converge฀on฀the฀six฀serial฀ports.฀The฀ slave฀could฀not฀only฀handle฀the฀bit฀level฀serial฀transactions,฀ but฀implement฀higher฀level฀protocols,฀data฀compression/ decompression,฀error฀detection/corrections฀and฀parsing.฀ The฀system฀“master”฀processor฀may฀be฀attending฀to฀local฀ I/O฀tasks,฀control-loops฀and฀MMI฀responsibilities.฀Offloading฀the฀bulk฀of฀the฀communication฀burden฀to฀the฀slave฀ processor฀allows฀the฀master฀to฀only฀have฀to฀deal฀with฀the฀ highest฀level฀details฀of฀the฀messaging. 44

SD0-SD7 SA1 SA0 SWR SRD SCS SLAVEATTN

Rabbit 3000

Figure฀2.4:฀The฀Slave฀port฀ presents฀a฀seamless฀interface฀ to฀many฀CPU฀I/O฀busses.

The฀Basics Rabbit 3000

Rabbit 3000

Rabbit 3000

D0-D7

SD0-SD7

D0-D7

SD0-SD7

A0

SA0

A0

SA0

A1

SA1

A1

SA1

IORD

SRD

IORD

SRD

IOWR

SWR

IOWR

SWR

I6

SCS

I6

SCS

INT0A

SLAVEATTN

INT0A

SLAVEATTN

I7 MASTER

MASTER AND SLAVE

INT1A SLAVE

Rabbit 3000 SD0-SD7 SA0 SA1 SRD SWR SCS SLAVEATTN

SLAVE

Figure฀2.5:฀Slaves฀can฀have฀slaves—the฀possibilities฀are฀endless.

In฀addition฀to฀fostering฀processor-to-processor฀communications,฀the฀Rabbit’s฀slave฀port฀may฀ be฀used฀to฀cold-boot฀the฀processor.฀Upon฀reset,฀if฀the฀SMODE1฀and฀SMODE0฀pins฀are฀held฀ in฀a฀{0,1}฀configuration,฀the฀Rabbit฀will฀accept฀boot฀code฀over฀the฀slave฀port฀in฀much฀the฀ same฀way฀that฀boot฀data฀can฀be฀provided฀over฀serial฀channel฀A. 2.5.5฀Input฀Capture One฀of฀Rabbit’s฀more฀unique฀features฀is฀the฀dual฀channel฀Input฀Capture฀module.฀This฀clever฀ bit฀of฀engineering฀allows฀the฀Rabbit฀3000฀to฀precisely฀measure฀the฀time฀between฀external฀ digital฀events.฀This฀can฀be฀used฀for฀measuring฀pulse฀width฀and฀frequency.฀The฀module’s฀ability฀to฀generate฀interrupts฀allows฀the฀design฀engineer฀to฀add฀up฀to฀four฀external฀interrupts฀to฀the฀ Rabbit-based฀system฀even฀if฀the฀module’s฀event฀timing฀capabilities฀go฀unused. Figure฀2.6฀shows฀a฀conceptual฀model฀of฀a฀single฀input฀capture฀channel.฀The฀Source฀Register฀ (ICSxR)฀determines฀which฀of฀the฀bits฀from฀Ports฀C,฀D,฀F฀or฀G฀will฀be฀the฀source฀of฀the฀start฀ and฀stop฀conditions.฀ The฀Trigger฀Register฀(ICTxR)฀determines฀whether฀the฀start฀and฀stop฀conditions฀will฀be฀risingedge฀or฀falling-edge฀triggered฀or฀triggered฀on฀any฀edge.฀This฀register฀also฀determines฀whether฀ a฀“start”฀or฀“stop“฀event฀will฀latch฀the฀value฀of฀the฀16-bit฀counter฀into฀the฀Capture฀Register.฀ ICTxR฀controls฀options฀for฀generating฀an฀interrupt฀and฀starting฀and฀stopping฀the฀counter. The฀Input฀Capture฀Control฀Register฀(ICCR)฀specifies฀what฀interrupt฀priority฀is฀to฀be฀used฀by฀ the฀Input฀Capture฀module.฀Both฀Input฀Capture฀channels฀share฀this฀register.฀ A฀program฀may฀reset฀the฀counter฀through฀the฀Control฀and฀Status฀Register.฀This฀register฀also฀ contains฀a฀“disable/enable฀interrupts”฀bit฀for฀the฀Input฀Capture฀module.฀By฀reading฀the฀ICCSR,฀ 45

Chapter฀2 a฀program฀is฀able฀to฀determine฀if฀a฀start฀or฀stop฀condition฀has฀occurred฀or฀if฀the฀16-bit฀Input฀ Capture฀Counter฀has฀rolled฀over.฀The฀ICCSR฀is฀shared฀between฀both฀Input฀Capture฀channels. The฀only฀registers฀shared฀between฀the฀two฀channels฀are฀the฀ICCSR฀and฀the฀ICCR฀registers.฀ Each฀Input฀Capture฀Channel฀has฀a฀16-bit฀counter฀as฀well฀as฀support฀registers. In฀Figure฀2.6,฀we฀have฀used฀an฀‘x’฀to฀denote฀a฀position฀in฀a฀register฀name฀where฀a฀‘1’฀or฀a฀‘2’฀ may฀be฀placed฀to฀identify฀a฀register฀with฀a฀specific฀Input฀Capture฀channel.฀For฀example,฀the฀ ICT1R฀is฀the฀Input฀Capture฀Trigger฀Register฀for฀channel฀1,฀while฀ICT2R฀is฀the฀Trigger฀register฀for฀channel฀2. The฀system฀peripheral฀clock฀(PCLK฀or฀PCLK/2)฀feeds฀timer฀A8.฀The฀A8฀timer฀can฀divide฀ its฀source฀by฀n฀+฀1,฀where฀n฀is฀0฀to฀255.฀This฀means฀the฀highest฀time฀resolution฀of฀the฀Input฀ Capture฀is,฀best฀case,฀one-half฀period฀of฀PCLK/2. Rabbit฀Semiconductor฀suggests฀three฀operating฀modes฀for฀the฀Input฀Capture฀module: 1.฀ The฀counter฀is฀enabled฀by฀the฀start฀condition฀and฀halted฀by฀the฀stop฀condition.฀The฀ stop฀condition฀also฀latches฀the฀counter฀value฀into฀the฀Input฀Capture฀Registers. 2.฀ The฀counter฀runs฀continuously฀and฀the฀start฀and฀stop฀conditions฀latch฀the฀counter฀ value฀into฀the฀Capture฀Registers. 3.฀ The฀counter฀is฀started฀by฀software฀instructions฀and฀halted฀by฀the฀stop฀event. Mode฀one฀is฀great฀for฀measuring฀pulse฀width฀or฀for฀measuring฀the฀time฀between฀external฀ events.฀For฀example,฀a฀positive฀going฀pulse฀can฀be฀measured฀by฀configuring฀the฀Input฀Capture฀module฀to฀accept฀a฀rising฀edge฀as฀a฀start฀event฀and฀the฀falling฀edge฀as฀the฀stop฀event.฀The฀ Input฀Capture฀Counter฀can฀be฀cleared฀and฀the฀Input฀Capture฀module฀configured฀to฀start฀the฀ counter฀when฀the฀start฀event฀(rising฀edge฀of฀our฀pulse)฀occurs.฀There฀is฀one฀caveat:฀the฀system฀ will฀stop฀counting฀even฀if฀a฀stop฀signal฀occurs฀before฀a฀start฀signal,฀so฀you฀do฀need฀to฀synchronize฀properly. Once฀a฀stop฀event฀happens,฀the฀counter฀is฀halted,฀and฀the฀contents฀are฀latched฀into฀the฀Input฀ Capture฀Register.฀The฀application฀program฀need฀only฀look฀at฀the฀Input฀Capture฀Register฀to฀see฀ how฀long฀the฀pulse฀was. Provisions฀are฀included฀to฀contend฀with฀counter฀rollover฀and฀interrupt฀generation,฀to฀alert฀the฀ application฀program฀that฀the฀stop฀event฀has฀occurred. Mode฀two฀can฀be฀used฀to฀timestamp฀events.฀Each฀start฀event฀and฀each฀stop฀event฀latch฀the฀ contents฀of฀the฀counter฀into฀the฀Input฀Capture฀Registers.฀In฀most฀cases,฀the฀Input฀Capture฀ module฀will฀also฀be฀programmed฀to฀generate฀an฀interrupt฀to฀inform฀the฀application฀program฀ that฀an฀event฀has฀occurred฀and฀the฀data฀in฀the฀Input฀Capture฀Registers฀needs฀to฀be฀processed฀ before฀a฀future฀event฀occurs฀and฀overwrites฀the฀timestamp฀in฀the฀Input฀Capture฀Registers. Since฀start฀and฀stop฀events฀do฀not฀have฀to฀occur฀on฀the฀same฀external฀pin,฀this฀mode฀can฀be฀ used฀simply฀to฀generate฀interrupts.฀This฀gives฀a฀system฀designer฀the฀ability฀to฀have฀up฀to฀four฀ more฀external฀interrupt฀signals฀(a฀start฀and฀stop฀event฀on฀two฀channels).฀This฀is฀a฀simple฀way฀ of฀expanding฀the฀available฀external฀interrupt฀pins. 46

The฀Basics Rabbit's Internal 8-bit Data Bus

8

16-bit counter

Capture Register MSB (ICMxR)

Logic and Input Capture Control

Counter MSB

8

TIMER A8

CLR

Load

Capture Register LSB (ICLxR)

Counter LSB

8

CLR

PORT C

Load

bit 1 bit 3

4 bit 5

Control & Status Register (ICCSR)

bit 7 8

PORT D Control Register (ICCR)

bit 1 bit 3

8

4 bit 5 bit 7

Trigger Register (ICTxR)

8

PORT F

bit 1 bit 3

4 bit 5 bit 7

Source Register (ICSxR)

8

PORT G

bit 1 bit 3

4 bit 5

Interrupt

bit 7

Figure฀2.6:฀The฀input฀capture฀channels฀are฀one฀of฀Rabbit’s฀most฀versatile฀I/O฀features.

Mode฀three฀allows฀the฀timer฀to฀free-run,฀and฀only฀the฀stop฀event฀will฀halt฀the฀timer.฀This฀is฀ useful฀for฀measuring฀the฀time฀between฀the฀application฀program฀clearing฀the฀Input฀Capture฀ timer฀and฀an฀external฀event฀occurring.฀ 47

Chapter฀2 The฀input฀pins฀for฀the฀start฀and฀stop฀events฀need฀not฀be฀the฀same฀pin.฀They฀needn’t฀even฀ be฀on฀the฀same฀port.฀The฀two฀Input฀Capture฀channels฀can฀share฀an฀external฀pin฀as฀an฀event฀ generator.฀The฀Rabbit฀Input฀Capture฀configuration฀registers฀allow฀versatile฀and฀creative฀ configuration฀of฀the฀Input฀Capture฀module.฀For฀bit฀level฀details฀on฀each฀register,฀refer฀to฀the฀ Rabbit฀3000฀Microprocessor฀User’s฀Manual฀included฀on฀the฀CD฀packaged฀with฀this฀book.฀ (The฀latest฀copy฀is฀available฀on฀the฀Rabbit฀web฀site.) 2.5.6฀Quadrature฀Decoder The฀quadrature฀decoder฀module฀is฀another฀Rabbit฀Semiconductor฀innovation฀that฀fell฀out฀of฀ Z-World’s฀long฀experience฀in฀the฀embedded฀market.฀Z-World฀has฀known฀for฀years฀that฀optical฀ quadrature฀encoders฀are฀commonly฀used฀in฀rotating฀machinery฀as฀feedback฀for฀shaft฀position.฀ In฀keeping฀with฀Rabbit’s฀philosophy฀of฀“let฀the฀silicon฀do฀the฀work,”฀the฀Rabbit฀3000฀sports฀ an฀8-bit฀quadrature฀decoder฀module. Before฀looking฀at฀the฀details฀of฀the฀Rabbit’s฀quadrature฀decoder,฀let’s฀quickly฀review฀what฀ quadrature฀encoding฀is฀and฀where฀it฀is฀used. Quadrature฀encoding฀uses฀a฀two-bit฀Gray฀code฀to฀indicate฀the฀direction฀of฀travel฀of฀a฀mechanical฀device.฀Mechanical฀and฀optical฀encoders฀are฀available฀that฀implement฀quadrature฀encoding฀ on฀rotary฀shafts.฀These฀range฀in฀price฀based฀on฀the฀angular฀resolution฀of฀the฀device฀as฀well฀as฀ the฀quality฀of฀the฀mechanical฀construction. All฀quadrature฀encoders฀contain฀some฀sort฀of฀encoder฀wheel,฀such฀as฀the฀one฀shown฀in฀Figure฀ 2.7.฀The฀encoder฀wheel฀is฀read฀optically฀or฀mechanically฀and฀an฀electrical฀signal฀is฀produced฀on฀ two฀wires.฀The฀two฀electrical฀signals฀are฀designated฀as฀the฀In-phase฀(I)฀signal฀and฀Quadrature฀(Q)฀ signal.฀In฀Figure฀2.7,฀the฀I-signal฀corresponds฀to฀the฀outer฀ring฀and฀the฀Q-signal฀to฀the฀inner฀ring.฀ The฀idea฀behind฀a฀quadrature฀encoder฀is฀that฀the฀bit฀sequence฀produced฀will฀present฀unique฀ transitions฀depending฀on฀the฀direction฀of฀rotation.฀Unit฀distance฀binary฀codes,฀such฀as฀the฀ two-bit฀reflective฀Gray฀Code฀shown฀in฀Figure฀2.7,฀are฀ideally฀suited฀for฀this฀purpose.฀The฀ code฀sequence฀shown฀in฀the฀table฀at฀the฀bottom฀of฀Figure฀2.7฀illustrates฀how฀the฀In-phase฀and฀ Quadrature฀signals฀vary฀as฀a฀function฀of฀angular฀displacement. The฀code฀sequence฀in฀the฀table฀progresses฀from฀the฀left฀to฀the฀right฀and฀this฀corresponds฀to฀a฀ clockwise฀traversal฀of฀the฀wheel.฀Careful฀inspection฀of฀the฀code฀sequence฀will฀reveal฀that฀no฀ transition฀that฀occurs฀on฀a฀clockwise฀traversal฀will฀appear฀in฀a฀counter-clockwise฀traversal.฀ For฀example,฀the฀transition฀of฀IQ฀from฀{1,1}฀to฀{0,1}฀will฀only฀appear฀on฀a฀clockwise฀traversal.฀Similarly,฀the฀sequence฀{1,1}฀to฀{1,0}฀will฀only฀appear฀on฀a฀counter-clockwise฀traversal. Figure฀2.7฀picked฀an฀arbitrary฀rotation฀of฀the฀ring฀to฀correspond฀to฀0°฀(IQ={1,0}).฀The฀encoder฀wheel฀was฀structured฀such฀that฀a฀clockwise฀traversal฀(counter฀clockwise฀rotation)฀will฀ create฀a฀code฀sequence฀that฀will฀cause฀the฀Rabbit’s฀quadrature฀decode฀module’s฀8-bit฀counter฀ to฀increment.฀Likewise,฀counter-clockwise฀traversal฀will฀cause฀Rabbit’s฀counter฀to฀decrement. Rabbit’s฀quadrature฀decoder฀is฀easy฀to฀use.฀There฀are฀two฀channels.฀They฀share฀control฀and฀ status฀registers,฀but฀each฀channel฀has฀a฀unique฀8-bit฀counter฀to฀keep฀track฀of฀the฀encoder’s฀ position. 48

The฀Basics

330o 11

0o 10

350o 00

340o 01

o

10 11

o

20 01

o

30 00

40 o 10

320o 10

50 o 11

310o 00 300o 01

o

60 01

o

290o 11

70 00

o

280o 10

80 10

90 o 11

270o 00

260o 01

100o 01 110o 00

250o 11

120o 10

240o 10 130o 11

230o 00 140o 01

220o 01 210o 11

200o 10

190o 00

170o 11

180o 01

150o 00

160o 10

Angle Codes 0

o

10

o

20

o

30

o

40

o

50

o

60

o

70

o

80

o

90

o

100

o

110

o

120

Outer Ring - I

1

1

0

0

1

1

0

0

1

1

0

0

1

Inner Ring - Q

0

1

1

0

0

1

1

0

0

1

1

0

0

n

n+1

n+2

n+3

n+4

n+5

n+6

n+7

n+8

n+9

o

Rabbit’s I Input Rabbit’s Q Input Rabbit’s Quadrature Counter

n+10 n+11 n+12

Figure฀2.7:฀Optical฀encoders฀allow฀the฀monitoring฀of฀rotational฀direction฀and฀ with฀some฀additional฀processing,฀speed฀and฀position฀can฀also฀be฀obtained.

49

Chapter฀2 Figure฀2.8฀shows฀a฀block฀diagram฀of฀the฀quadrature฀decoder฀channels.฀The฀I฀and฀Q฀inputs฀can฀ be฀selected฀from฀Port฀F.฀Some฀care฀must฀be฀taken฀when฀hooking฀up฀a฀quadrature฀encoder฀to฀ port฀F,฀since฀only฀predefined฀pin฀pairs฀can฀be฀used.฀Port฀F฀bits฀“0฀and฀1”฀or฀bits฀“4฀and฀5”฀may฀ be฀used฀to฀connect฀to฀channel฀1.฀Port฀F฀bits฀“2฀and฀3”฀or฀bits฀“6฀and฀7”฀may฀be฀used฀to฀connect฀to฀channel฀2.฀The฀odd฀number฀bits฀always฀correspond฀to฀the฀I-signal. The฀control฀registers฀allow฀application฀code฀to฀determine฀if฀a฀counter฀has฀rolled฀over.฀The฀ Quadrature฀Decoder฀may฀also฀be฀configured฀to฀generate฀an฀interrupt฀when฀a฀counter฀makes฀a฀ transition฀between฀the฀0xFF฀and฀0x00฀states.฀The฀interrupt,฀if฀enabled,฀will฀occur฀regardless฀ of฀the฀transition฀direction฀between฀the฀two฀states฀(0xFF฀⇒฀0x00฀or฀0x00฀⇒฀0xFF). The฀QDCSR฀(Quadrature฀Decode฀Ctrl/Status฀Register)฀allows฀an฀application฀program฀to฀reset฀ either฀channel’s฀counter฀to฀0x00.฀This฀allows฀an฀application฀program฀to฀“home”฀the฀quadrature฀encoder฀wheel฀to฀correspond฀to฀a฀“count”฀of฀0x00.

Rabbit’s Internal 8-bit Data Bus

8

Quadrature Decode Register Channel 1 (QDC1R)

8

Quadrature Decode Register Channel 2 (QDC2R)

8

Counter 1

Logic and Quadrature Decoding Control

UP/ DOWN

TIMER A10

CLR

Counter 2

UP/ DOWN CLR

PORT F

bit 0 bit 1 bit 2

Control & Status Register (QDCSR)

bit 3 8

8 bit 4 bit 5 bit 6 bit 7

Control Register (QDCR)

8

Interrupt

Figure฀2.8:฀Two฀independent฀channels฀of฀quadrature฀decoding฀ give฀the฀Rabbit฀great฀flexibility฀in฀controlling฀rotary฀systems. 50

The฀Basics It฀is฀no฀accident฀that฀the฀Input฀Capture฀Module฀and฀the฀Quadrature฀Decoder฀module฀both฀ allow฀inputs฀from฀Port฀F.฀The฀pulse฀width฀of฀either฀the฀In-phase฀or฀Quadrature฀signal฀will฀฀ be฀proportional฀to฀the฀velocity฀of฀the฀encoder฀wheel.฀The฀Input฀Capture฀Module,฀shown฀in฀ Figure฀2.6,฀only฀allows฀measurements฀from฀the฀odd฀numbered฀bits฀of฀Port฀F฀(or฀C,฀D฀or฀G).฀ This฀will฀correspond฀to฀the฀I-signal฀if฀a฀quadrature฀encoder฀is฀connected. A฀clever฀engineer฀will฀combine฀the฀rotational฀displacement฀information฀from฀the฀Quadrature฀ Decoder฀Module฀with฀velocity฀data฀gathered฀through฀the฀Input฀Capture฀Module฀to฀get฀a฀complete฀picture฀of฀the฀behavior฀of฀any฀rotating฀machinery฀the฀Rabbit฀is฀monitoring฀or฀controlling. 2.5.7฀Pulse฀Width฀Modulation฀(PWM)฀Channels Pulse฀Width฀Modulation฀is฀one฀of฀the฀most฀versatile฀tools฀available฀to฀the฀embedded฀system฀ designer.฀Everything฀from฀audio฀generation฀to฀motor฀control฀can฀be฀implemented฀with฀PWM.฀ Recognizing฀this,฀Rabbit฀Semiconductor฀developed฀a฀PWM฀module฀that฀provides฀four฀independent฀channels฀of฀PWM฀output.฀Rabbit฀went฀one฀step฀further฀than฀many฀companies—the฀ Rabbit’s฀PWM฀module฀allows฀the฀system฀designer฀optionally฀to฀spread฀the฀“on-state”฀power฀ over฀the฀output฀waveform’s฀period.฀In฀some฀applications,฀such฀as฀audio฀output฀or฀a฀DAC฀ implemented฀with฀a฀low฀pass฀filter,฀the฀ripple฀in฀the฀final฀waveform฀is฀reduced. Let’s฀first฀look฀at฀what฀PWM฀is฀and฀then฀see฀how฀Rabbit’s฀PWM฀module฀operates. Figure฀2.9฀shows฀a฀pulse฀and฀the฀definitions฀for฀some฀common฀terms.฀Assuming฀that฀energy฀ in฀a฀pulse฀is฀contained฀completely฀in฀the฀“on฀time”฀portion฀of฀the฀waveform,฀the฀duty฀cycle฀is฀ directly฀proportional฀to฀the฀energy.฀100%฀duty฀cycle฀implies฀a฀steady฀state฀high฀voltage.฀0%฀ duty฀cycle฀implies฀a฀“pulse”฀that฀is฀off฀all฀the฀time.฀By฀varying฀the฀duty฀cycle฀of฀the฀pulse,฀we฀ can฀cause฀the฀energy฀in฀the฀waveform฀to฀go฀from฀0฀to฀100%. Percent Duty Cycle =

On Time ⋅ 100% Period

Pulse Period On Time

Off Time

Voltage

Time

Figure฀2.9:฀Pulse฀Width฀Modulation฀is฀nothing฀more฀than฀varying฀duty฀cycle.

The฀term฀“pulse฀width฀modulation”฀derives฀from฀an฀assumption฀that฀the฀pulse฀period฀is฀fixed.฀ In฀this฀case,฀the฀“pulse”฀is฀thought฀of฀the฀high฀part฀of฀the฀waveform฀in฀Figure฀2.9.฀“On฀time”฀ is฀equivalent฀to฀pulse-width฀when฀we฀are฀talking฀about฀PWM. 51

Chapter฀2 Some฀newcomers฀confuse฀“pulse฀width”฀with฀“pulse฀period.”฀Careful฀attention฀to฀nomenclature฀and฀definitions฀is฀important.฀ The฀Rabbit฀PWM฀module฀is฀straightforward—a฀single฀10-bit฀free-running฀counter฀counts฀ from฀0x000฀to฀0x3FF฀(0฀to฀1023)฀and฀rolls฀over.฀The฀application฀program฀loads฀a฀PWM฀ “width”฀register฀with฀a฀value฀“n.”฀Out฀of฀1024฀counts,฀the฀PWM’s฀output฀pulse฀is฀high฀for฀n+1฀ counts.฀This฀gives฀the฀application฀the฀ability฀to฀vary฀the฀duty฀cycle฀from฀about฀0.1%฀to฀100%.฀ 1024฀ticks฀from฀timer฀A9฀define฀the฀overall฀period. Figure฀2.10฀shows฀a฀block฀diagram฀of฀the฀PWM฀module.฀In฀the฀register฀names฀PWMxR฀and฀ PWLxR฀we฀are฀using฀an฀“x”฀to฀indicate฀that฀a฀number฀between฀0฀and฀3฀can฀be฀substituted฀to฀ control฀PWM฀channel฀0฀to฀3. To฀connect฀the฀PWM฀module฀to฀a฀pin฀on฀Port฀F,฀the฀Port฀F฀function฀register฀(PFFR฀is฀part฀ of฀Port฀F฀and฀is฀not฀explicitly฀shown฀in฀Figure฀2.10)฀must฀be฀programmed.฀If฀an฀application฀ needs฀to฀run฀a฀PWM฀output฀all฀the฀way฀down฀to฀0%฀duty-cycle,฀PFFR฀can฀be฀programmed฀to฀ disconnect฀the฀PWM฀module฀from฀the฀Port฀F฀pin฀and฀the฀corresponding฀Port฀F฀pin฀can฀subsequently฀programmed฀to฀be฀off. Figure฀2.10฀shows฀that฀only฀three฀bits฀of฀the฀PWM฀LSB฀register฀(PWLxR)฀actually฀go฀to฀the฀ PWM฀state฀machines.฀Two฀of฀these฀bits฀are฀concatenated฀with฀the฀8฀bits฀from฀the฀PWMxR฀ register฀to฀form฀the฀10-bit฀value฀of฀n.฀The฀third฀bit฀in฀PWLxR฀determines฀the฀Rabbit’s฀PWM฀ energy฀spreader฀is฀enabled. The฀PWM฀frequency฀can฀be฀calculated฀from฀the฀formula F฀=฀Fcpu฀/฀2฀/฀1024฀/฀(TAT9R฀+฀1) and฀it฀is฀the฀same฀for฀all฀four฀PWM฀signals. Rabbit’s Internal 8-bit Data Bus n=10-bit 8

PWM MSB Register (PWMxR)

TIMER A9 8

10-bit free running counter & PWM state machines

PWM LSB Register (PWLxR)

PORT F

bit 4 bit 5

4 bit 6 bit 7

Spreader Enable

Figure฀2.10:฀Two฀registers฀are฀all฀that฀are฀needed฀to฀control฀each฀PWM฀channel.

52

The฀Basics There฀are฀a฀couple฀of฀ways฀to฀think฀about฀the฀PWM฀spreader.฀The฀easiest,฀but฀not฀completely฀ accurate,฀way฀is฀to฀simply฀think฀of฀the฀spreader฀as฀a฀4×฀frequency฀multiplier.฀The฀reason฀this฀ is฀not฀accurate฀is฀that฀the฀actual฀timer฀ticks,฀and฀therefore฀resolution,฀do฀not฀change฀based฀on฀ the฀spreader฀being฀enabled฀or฀not. Figure฀2.11,฀borrowed฀from฀Rabbit’s฀User’s฀Manual,฀shows฀how฀the฀PWM฀spreader฀affects฀a฀ series฀of฀sample฀configurations.฀The฀interesting฀bit฀is฀that฀the฀Rabbit’s฀spreader฀automatically฀ adjusts฀the฀pulse฀widths฀to฀be฀different฀lengths฀totaling฀up฀to฀“n฀+฀1”฀complete฀ticks฀of฀A9฀so฀ the฀total฀energy฀in฀a฀given฀1024฀tick฀period฀is฀the฀same฀regardless฀of฀whether฀the฀spreader฀is฀ enabled฀or฀not.฀Notice฀in฀Figure฀2.11฀when฀n฀=฀257,฀the฀spreader฀automatically฀alternates฀the฀ short฀(64฀count)฀and฀long฀(65฀count)฀pulses. The฀most฀accurate฀way฀to฀describe฀the฀PWM฀spreader฀is฀as฀a฀device฀that฀breaks฀up฀a฀1024฀ count฀period฀into฀four฀quadrants฀and฀distributes฀n฀+฀1฀total฀counts฀of฀“on฀time”฀across฀the฀four฀ quadrants฀as฀evenly฀as฀possible. 1024 counts

256 counts

n=255, normal

768 counts

n=255, spread

64

192

64

192

64

192

64

192

n=256, spread

65

191

64

192

64

192

64

192

n=257, spread

65

191

64

192

65

191

64

192

n=258, spread

65

191

65

191

65

191

64

192

n=259, spread

65

65

191

65

191

65

191

n=259, normal

191

259 counts

765 counts

Figure฀2.11:฀The฀spreader฀distributes฀the฀on-time฀energy฀ across฀four฀quadrants฀in฀the฀1024฀count฀period.

2.5.8฀Auxiliary฀I/O฀Bus The฀Auxiliary฀I/O฀Bus฀is฀stroke฀of฀genius!฀As฀Z-World฀learned฀over฀the฀years,฀interfacing฀a฀ bunch฀of฀I/O฀devices฀to฀a฀memory฀bus,฀especially฀a฀high-speed฀memory฀bus,฀causes฀all฀manner฀of฀problems฀associated฀with฀the฀capacitive฀loading฀associated฀with฀the฀I/O฀devices.฀The฀ Rabbit฀3000฀gives฀system฀designers฀the฀option฀to฀keep฀the฀CPU’s฀memory฀bus฀separate฀from฀ the฀I/O฀bus.฀This฀occurs฀at฀the฀sacrifice฀of฀14฀I/O฀pins฀from฀Ports฀A฀and฀B.฀Furthermore,฀the฀ use฀of฀the฀Slave฀Port฀and฀the฀Auxiliary฀I/O฀bus฀is฀mutually฀exclusive. 53

Chapter฀2 The฀Auxiliary฀I/O฀bus฀uses฀the฀8฀bits฀of฀I/O฀Port฀A฀as฀a฀data฀bus.฀6฀bits฀from฀Port฀B฀provide฀ the฀least฀significant฀6฀bits฀of฀the฀16-bit฀I/O฀address.฀6฀bits฀is฀only฀64฀unique฀addresses,฀but฀for฀ many฀systems฀this฀is฀more฀than฀adequate.฀ When฀the฀Auxiliary฀I/O฀bus฀is฀used,฀I/O฀transactions฀occur฀simultaneously฀on฀the฀main฀ system฀bus.฀If฀additional฀address฀lines฀are฀needed,฀they฀can฀be฀pulled฀from฀the฀main฀bus฀and฀ buffered.฀If฀only฀one฀or฀two฀signals฀are฀needed,฀a฀designer฀might฀consider฀using฀the฀UHS฀ family฀Tiny฀Logic™฀buffers฀from฀Fairchild฀Semiconductor.฀These฀offer฀a฀small฀SOT-23฀footprint฀and฀only฀cost฀pennies.฀Texas฀Instruments฀has฀a฀similar฀line฀of฀products. When฀using฀a฀Rabbit฀Core฀module฀in฀a฀design,฀the฀Auxiliary฀I/O฀Bus฀is฀often฀the฀only฀I/O฀ bus฀available.฀This฀keeps฀the฀trace฀lengths฀of฀the฀high-speed฀memory฀bus฀short฀and฀unwanted฀ EMI฀minimized.฀The฀Auxiliary฀I/O฀bus฀is฀one฀of฀Rabbit’s฀strongest฀features฀for฀real฀world฀ embedded฀designs. 2.5.9฀Timer฀A Rabbit฀has฀two฀timer฀modules—Timer฀A฀and฀Timer฀B.฀Of฀all฀or฀Rabbit’s฀modules,฀the฀timer฀ models฀catch฀the฀most฀flak฀from฀users.฀Unfortunately,฀the฀transistor฀fairy฀has฀come฀and฀gone.฀ We฀are฀stuck฀with฀the฀timers฀as฀they฀sit฀in฀the฀Rabbit฀3000.฀So฀let’s฀see฀how฀they฀operate฀and฀ how฀Rabbit฀intended฀each฀to฀be฀used.฀From฀there,฀the฀system฀designer฀can฀make฀the฀most฀of฀ what’s฀available. The฀Timer฀A฀module฀is฀actually฀ fairly฀straightforward.฀Figure฀ 2.12฀shows฀a฀block฀diagram฀ of฀the฀Timer฀A฀module.฀Each฀ timer,฀A1-A10,฀is฀an฀8-bit฀downcounter฀with฀an฀accompanying฀ reload฀register.฀This฀allows฀each฀ counter฀to฀divide฀the฀incoming฀ clock฀by฀1฀to฀256.

PERCLK

A1 ÷2

A3

A4

A5

A฀quick฀glance฀at฀Figure฀2.12฀ shows฀that฀the฀output฀of฀every฀ timer฀has฀a฀preferred฀function.฀In฀ the฀cases฀of฀A8,฀A9฀and฀A10฀the฀ timers฀cannot฀generate฀an฀interrupt฀and฀are฀only฀useful฀for฀their฀ assigned฀task. The฀A8,฀A9฀and฀A10฀timers฀may฀ a฀first฀glance฀look฀as฀generic฀as฀ A2–A7,฀but฀alas฀they฀are฀not.฀ These฀three฀resources฀serve฀only฀ their฀I/O฀modules฀and฀cannot฀ generate฀interrupts.฀They฀cannot฀

A2

Serial E

Serial F

Serial A

Serial B

A6

Serial C

A7

Serial D

A8

Input Capture

A9

PWM

A10

Quadrature Decode

Figure฀2.12:฀Timer฀A฀consists฀of฀ten฀8-bit฀timers. 54

The฀Basics be฀used฀as฀prescalers฀or฀be฀cascaded฀with฀A1.฀They฀are฀intended฀to฀produce฀relatively฀fast฀ clocks฀for฀their฀respective฀I/O฀modules.฀They฀fulfill฀their฀purpose฀well.฀Some฀engineers฀find฀it฀ less฀painful฀to฀think฀of฀these฀timers฀as฀part฀of฀their฀assigned฀modules. A1฀can฀be฀used฀as฀a฀prescaler฀for฀A2–A7.฀However,฀unless฀ultraslow฀bit฀rates฀are฀used,฀A1฀ isn’t฀usually฀needed฀as฀a฀prescaler฀for฀baud฀rate฀generation.฀A1฀can฀generate฀an฀interrupt฀ when฀it฀counts฀down฀to฀0x00.฀Unless฀A1฀is฀being฀used฀as฀a฀prescaler,฀it฀is฀available฀for฀general-purpose฀interrupt฀generation. A2–A7฀can฀generate฀an฀interrupt฀when฀their฀counts฀reach฀0x00.฀If฀these฀are฀not฀being฀used฀to฀ generate฀baud฀rates,฀they฀can฀be฀used฀as฀general-purpose฀time฀bases.฀Figure฀2.12฀shows฀that฀ these฀timers฀may฀not฀be฀cascaded฀to฀produce฀low฀frequency฀interrupts.฀Furthermore,฀the฀Timer฀ A฀module฀shares฀a฀single฀interrupt฀among฀all฀the฀timers฀with฀interrupts฀enabled.฀The฀Interrupt฀ Service฀Routine฀(ISR)฀is฀responsible฀for฀determining฀which฀timer(s)฀tripped฀the฀interrupt. Rabbit฀Semiconductor฀has฀been฀very฀careful฀in฀their฀design฀regarding฀the฀bits฀that฀indicate฀ if฀a฀particular฀timer฀(A1–A7)฀has฀rolled฀over.฀Under฀no฀circumstances฀will฀a฀bit฀be฀dropped฀ if฀multiple฀timers฀simultaneously฀rollover.฀When฀an฀ISR฀reads฀the฀register฀(TACSR—Timer฀ A฀Control฀and฀Status฀Register)฀containing฀the฀rollover฀bits,฀the฀ISR฀must฀be฀exhaustive฀in฀ the฀inspection฀of฀these฀bits฀as฀they฀are฀cleared฀in฀TACSR฀upon฀read.฀If฀multiple฀timers฀have฀ expired,฀then฀the฀ISR฀must฀execute฀the฀code฀required฀to฀service฀each฀timer฀that฀expired. If฀additional฀timers฀expire฀while฀the฀ISR฀is฀running,฀a฀new฀interrupt฀will฀be฀generated฀once฀ the฀ISR฀has฀completed฀and฀the฀interrupt฀priority฀is฀lowered.฀Under฀no฀circumstances฀will฀the฀ Rabbit฀Timer฀A฀Module฀lose฀rollover฀information. The฀timers฀A1–A10฀may฀not฀be฀read฀directly.฀They฀may฀not฀be฀used฀to฀timestamp฀events.฀ However,฀the฀Input฀Capture฀Module,฀driven฀by฀A8,฀can฀be฀used฀as฀a฀timestamp.฀Timer฀B฀may฀ also฀be฀read฀directly฀by฀the฀CPU.

55

Chapter฀2 2.5.10฀Timer฀B Figure฀2.13฀shows฀how฀the฀two฀“timers”฀B1฀and฀B2฀are฀arranged.฀Not฀shown฀in฀Figure฀2.13฀ are฀two฀control฀registers฀that฀determine฀from฀which฀source฀the฀10-bit฀counter฀is฀clocked฀and฀ whether฀a฀“match”฀will฀generate฀an฀interrupt฀or฀not. There฀is฀no฀method฀to฀reset฀the฀10-bit฀counter.฀This฀means฀that฀to฀use฀B1฀or฀B2฀to฀generate฀an฀ interrupt฀on฀a฀schedule฀other฀than฀1/1024฀of฀the฀selected฀clock฀source,฀the฀ISR฀must฀compute฀ the฀next฀“match”฀value฀required฀to฀get฀the฀next฀IRQ฀at฀the฀desired฀time.฀While฀unorthodox฀(to฀ be฀kind),฀this฀arrangement฀is฀functional. To฀read฀the฀value฀of฀the฀10-bit฀counter,฀a฀special฀procedure,฀described฀in฀the฀Rabbit฀Microprocessor฀User’s฀Manual,฀must฀be฀followed.฀This฀is฀required฀because฀the฀10-bit฀counter฀can’t฀ be฀represented฀in฀a฀single฀8-bit฀byte.฀Since฀two฀bytes฀have฀to฀be฀read฀to฀acquire฀the฀counter’s฀ state,฀the฀possibility฀exists฀that฀in฀the฀time฀between฀the฀two฀reads,฀a฀counter฀rollover฀may฀have฀ occurred฀thus฀invalidating฀the฀first฀byte฀read. PERCLK A1

÷2 10-Bit Counter

÷8 10

MSB

2

Read the counter here

LSB

8

Preload Registers MSB

2 8

LSB

MSB

Comparators

Match Registers 10

B1

10

B2

LD

2 8

LSB

LD

8 Rabbit’s Internal Data Bus

Figure฀2.13:฀Timer฀B฀is฀a฀creative฀architecture. 56

The฀Basics Besides฀being฀useful฀for฀generating฀periodic฀interrupts,฀B1฀and฀B2฀can฀both฀be฀used฀to฀synchronously฀update฀parallel฀I/O. By฀most฀standards,฀Timer฀B฀is฀a฀bit฀of฀a฀pain฀to฀use.฀The฀repeated฀computation฀of฀reload฀values฀is฀annoying.฀The฀fact฀that฀B1฀and฀B2฀share฀a฀common฀counter฀and฀time฀base฀is฀a฀little฀less฀ than฀optimal.฀However,฀what฀the฀hardware฀lacks,฀the฀firmware฀can฀make฀up.฀The฀trade-off฀is฀ simply฀CPU฀cycles. 2.5.11฀Clock฀Spreader Electromagnetic฀regulatory฀compliance฀is฀a฀very฀real฀issue฀for฀companies.฀Testing฀is฀expensive.฀Redesigning฀circuits฀in฀a฀product฀that฀is฀already฀complete,฀just฀to฀pass฀government฀EMI฀ tests,฀is฀expensive฀and฀time฀consuming. Rabbit฀Semiconductor฀looked฀at฀Z-World’s฀experience฀with฀EMI฀and฀created฀a฀Clock฀Spreader฀to฀reduce฀EMI฀at฀the฀source.฀Most฀EMI฀issues฀occur฀at฀harmonics฀of฀the฀system฀crystals฀or฀ CPU฀bus฀periods. The฀Clock฀Spreader฀works฀by฀introducing฀a฀seemingly฀random฀modulation฀on฀the฀crystal฀ oscillator,฀thus฀spreading฀out฀the฀energy฀associated฀with฀the฀oscillator’s฀fundamental฀and฀harmonic฀frequencies฀over฀a฀greater฀bandwidth.฀This฀reduces฀peak฀emissions฀and฀makes฀passing฀ government฀EMI฀tests฀much฀easier. The฀Clock฀Spreader฀has฀three฀settings:฀OFF,฀NORMAL฀and฀STRONG. The฀settings฀operate฀as฀one฀would฀intuitively฀expect.฀OFF฀allows฀the฀system฀crystal฀to฀ring฀at฀ the฀most฀pure฀frequency฀possible.฀This฀has฀the฀benefit฀of฀ensuring฀the฀most฀predictable฀system฀ wide฀timing.฀Baud฀rates฀derived฀from฀the฀crystal฀will฀be฀as฀accurate฀as฀possible.฀The฀disadvantage฀is฀that฀in฀these฀days฀of฀ever-increasing฀EMI฀regulations,฀the฀system฀will฀also฀have฀the฀ highest฀emissions. The฀NORMAL฀and฀STRONG฀modes฀introduce฀a฀small฀or฀modest฀amount฀of฀modulation฀ (spreading).฀The฀more฀modulation,฀the฀more฀the฀undesired฀harmonic฀energy฀is฀spread฀out฀ making฀EMC฀compliance฀easier.฀ In฀some฀extremely฀rare฀situations฀if฀the฀Clock฀Spreader฀is฀set฀to฀STRONG฀and฀the฀highest฀ baud฀rates฀are฀being฀used฀with฀asynchronous฀serial฀channels,฀some฀bit฀errors฀may฀occur.฀Only฀ very฀rarely฀will฀a฀designer฀run฀into฀a฀baud฀rate฀issue.฀If฀one฀does฀crop฀up,฀the฀Clock฀Spreader฀ can฀be฀dialed฀back฀from฀STRONG฀to฀NORMAL฀or฀even฀to฀OFF. 2.5.12฀Clock฀Doubler The฀Rabbit฀3000฀sports฀a฀Clock฀Doubler.฀Slow฀speed฀external฀crystals฀can฀be฀used฀and฀ doubled฀by฀the฀Rabbit฀3000.฀One฀application฀for฀the฀Clock฀Doubler฀is฀to฀allow฀the฀CPU฀the฀ option฀of฀trading฀power฀consumption฀against฀computational฀horsepower. A฀system’s฀power฀consumption฀is฀roughly฀linearly฀proportional฀to฀the฀system฀clock฀rate.฀For฀ example,฀turning฀on฀the฀Clock฀Doubler,฀the฀Rabbit฀3000฀can฀approximately฀halve฀the฀time฀ required฀to฀compute฀arithmetic฀operations.฀However,฀when฀the฀application฀isn’t฀“computing,”฀ the฀Clock฀Doubler฀can฀be฀disabled,฀allowing฀the฀power฀consumed฀by฀the฀CPU฀to฀be฀reduced฀ by฀50%. 57

Chapter฀2 2.5.13฀MMU฀and฀MIU The฀memory฀management฀unit฀(MMU)฀and฀memory฀interface฀unit฀(MIU)฀combine฀to฀give฀ Rabbit฀3000฀the฀ability฀to฀directly฀address฀six฀megabytes฀of฀memory.฀That’s฀a฀huge฀memory฀ space฀for฀a฀little฀8-bit฀processor. Currently฀available฀compilers฀only฀handle฀up฀to฀a฀1-megabyte฀address฀space.฀This฀is฀in฀keeping฀with฀the฀Rabbit’s฀20-bit฀physical฀address฀bus.฀The฀overhead฀of฀swapping฀memory฀pages฀ through฀the฀Chip฀Select,฀Output฀Enable฀and฀Write฀Enable฀or฀selective฀inversion฀of฀A18฀and฀ A19฀lines฀is฀not฀incurred.฀Nonetheless,฀clever฀engineers฀could฀use฀the฀Rabbit฀to฀address฀a฀full฀ six฀megabytes฀if฀they฀are฀willing฀to฀do฀the฀bookkeeping฀manually. Being฀an฀8-bit฀processor,฀Rabbit’s฀external฀and฀internal฀data฀busses฀are฀8฀bits฀wide.฀This฀does฀ not฀theoretically฀constrain฀the฀size฀of฀the฀external฀address฀bus.฀Supporting฀a฀wide฀address฀bus฀ requires฀the฀CPU฀to฀do฀multiple฀fetches฀across฀the฀8-bit฀wide฀data฀bus฀to฀acquire฀instruction฀ address฀operands.

1 Megabyte of Physical Memory 0xFFFFF

Y

X

SEGSIZE register

0xFFFF Extended Memory Segment (XPC Segment) 0xE000 Stack Segment 0xY000

Data Segment

0xX000 Root Segment (Base Segment) 0x00000

0x0000 16-bit Logical Address

MMU maps to

20-bit Physical Address

Figure฀2.14:฀The฀MMU฀creates฀segments฀in฀the฀16-bit฀logical฀address฀ space฀that฀allow฀access฀to฀windows฀in฀the฀20-bit฀physical฀address฀space. 58

The฀Basics For฀example,฀a฀16-bit฀wide฀address฀bus฀requires฀the฀CPU฀to฀perform฀three฀read฀cycles฀over฀the฀ 8-bit฀data฀bus฀to฀fetch฀a฀complete฀instruction฀that฀loads฀a฀register฀from฀a฀memory฀location.฀The฀ first฀read฀acquires฀the฀“load”฀instruction.฀The฀two฀following฀read฀cycles฀acquire฀the฀address. A฀16-bit฀(64฀KB)฀address฀space฀is฀perfectly฀good฀for฀many฀applications.฀The฀Tandy฀TRS-80™฀฀ Model฀III฀business฀computer฀ran฀with฀only฀48฀KB฀of฀stock฀memory.฀The฀1980s฀are฀well฀behind฀ us฀and฀modern฀systems฀are฀expected฀to฀support฀larger฀memory฀models.฀ Rabbit฀Semiconductor฀was฀unwilling฀to฀trade฀the฀overhead฀needed฀to฀fetch฀a฀third฀byte฀for฀ each฀operand฀containing฀an฀address.฀To฀eliminate฀this฀inefficiency,฀Rabbit฀adopted฀an฀MMU.฀ This฀module฀maps฀a฀16-bit฀logical฀address฀into฀a฀20-bit฀physical฀address.฀The฀CPU฀instructions฀generally฀operate฀on฀16-bit฀addresses฀and฀therefore฀only฀two฀read฀cycles฀are฀required฀to฀ fetch฀addresses.฀Rabbit฀has฀also฀included฀some฀special฀instructions฀that฀operate฀directly฀on฀ 20-bit฀addresses.฀These฀instructions,฀while฀useful,฀are฀slower฀than฀instructions฀that฀operate฀on฀ 16-bit฀addresses. The฀MMU฀maps฀the฀small฀16-bit฀logical฀address฀space฀into฀the฀larger฀20-bit฀physical฀space฀by฀ creating฀windows,฀called฀“segments.”฀Figure฀2.14฀illustrates฀the฀idea฀of฀mapping฀with฀segments. The฀segments฀are฀named฀more฀or฀less฀for฀how฀Dynamic฀C฀uses฀them.฀For฀this฀discussion,฀we฀ are฀not฀concerned฀with฀how฀Dynamic฀C฀manipulates฀the฀MMU฀(this฀is฀covered฀in฀Chapter฀3).฀ Here฀we฀are฀only฀concerned฀with฀how฀the฀Rabbit’s฀hardware฀treats฀the฀address฀space. The฀first฀segment,฀called฀the฀Root฀Segment฀or฀Base฀Segment,฀is฀forced฀to฀start฀at฀logical฀ address฀0x0000฀and฀physical฀address฀0x00000.฀ The฀size฀of฀the฀second฀and฀third฀segments฀is฀determined฀by฀the฀contents฀of฀the฀SEGSIZE฀ register.฀The฀four฀least฀significant฀bits฀of฀SEGSIZE฀specify฀the฀logical฀address฀where฀the฀ second฀segment,฀called฀the฀Data฀Segment,฀starts.฀The฀four฀most฀significant฀bits฀from฀SEGSIZE฀specify฀the฀logical฀address฀where฀the฀third฀segment,฀called฀the฀Stack฀Segment,฀starts.฀ The฀fourth฀segment,฀called฀the฀XPC฀Segment฀or฀Extended฀Memory฀Segment฀always฀starts฀at฀ logical฀address฀0xE000. Upon฀receipt฀of฀a฀16-bit฀logical฀address,฀the฀MMU฀decides฀to฀which฀segment฀the฀address฀ belongs,฀based฀on฀SEGSIZE.฀Then฀the฀MMU฀computes฀a฀20-bit฀physical฀address฀by฀adding฀ the฀16-bit฀logical฀address฀to฀a฀segment฀mapping฀register฀unique฀to฀each฀segment. Figure฀2.15฀shows฀the฀three฀mapping฀registers฀accessible฀by฀the฀user฀and฀how฀they฀are฀added฀ to฀the฀16-bit฀logical฀address฀to฀form฀a฀20-bit฀physical฀address.฀The฀XPC,฀STACKSEG฀and฀ DATASEG฀registers฀are฀8฀bits.฀The฀XPC฀is฀actually฀a฀proper฀CPU฀register฀so฀that฀it฀can฀be฀ changed฀quickly฀by฀an฀application.฀STACKSEG,฀DATASEG฀and฀SEGSIZE฀are฀internal฀ peripheral฀I/O฀registers. Figure฀2.15฀shows฀that฀a฀change฀of฀the฀least฀significant฀bit฀in฀XPC฀will฀move฀the฀XPC฀ segment฀in฀physical฀memory฀by฀4K.฀The฀reasoning฀behind฀the฀fixed฀8K฀size฀of฀the฀XPC฀is฀ now฀explainable. The฀XPC฀segment฀is฀mapped฀into฀physical฀memory฀with฀a฀granularity฀that฀is฀one-half฀of฀its฀ size.฀This฀allows฀the฀XPC฀segment฀to฀be฀slid฀over฀large฀slices฀of฀code฀or฀large฀data฀structures฀ in฀memory฀without฀completely฀losing฀sight฀of฀the฀image฀just฀viewed.฀ 59

Chapter฀2 For฀example,฀if฀a฀12K฀space฀in฀physical฀memory฀contains฀code,฀initially฀the฀ XPC฀segment฀could฀be฀mapped฀over฀ the฀first฀8K.฀As฀the฀code฀execution฀ progresses฀to฀the฀6K฀mark,฀the฀XPC฀ segment฀could฀be฀adjusted฀to฀view฀the฀ 4K–12K฀portion.฀This฀allows฀the฀currently฀executing฀code฀(near฀6K)฀to฀be฀ visible฀in฀both฀mappings. As฀it฀turns฀out,฀this฀ability฀to฀slide฀a฀ segment฀gradually฀across฀physical฀ memory฀makes฀life฀easier฀for฀compiler฀ designers.฀The฀Z180฀MMU฀did฀not฀have฀ this฀feature—Rabbit฀Semiconductor฀ added฀it฀to฀their฀MMU. Rabbit’s฀memory฀management฀is฀split฀ between฀two฀modules.฀As฀we฀have฀seen,฀ the฀MMU฀is฀responsible฀for฀mapping฀ a฀16-bit฀logical฀address฀into฀a฀20-bit฀ physical฀address.฀The฀MIU฀contains฀ potent฀mojo฀to฀implement฀separate฀ instruction฀and฀data฀spaces,฀generate฀ external฀chip฀selects,฀output฀enables฀and฀ write฀enables.฀The฀MIU฀also฀is฀where฀ the฀Rabbit฀obtains฀the฀capability฀to฀access฀a฀full฀six฀megabytes฀of฀memory. Figure฀2.15฀shows฀that฀the฀MIU฀has฀the฀ ability฀to฀selectively฀invert฀A16฀or฀A19฀ depending฀on฀whether฀an฀instruction฀or฀ data฀fetch฀is฀occurring.฀The฀MMIDR฀ (MMU฀Instruction/Data฀Register)฀ controls฀this฀behavior.฀This฀is฀how฀the฀ Rabbit฀implements฀separate฀instruction฀ and฀data฀space฀(I฀&฀D฀space). Separate฀I฀&฀D฀space฀only฀applies฀to฀the฀ first฀two฀segments.฀The฀Stack฀Segment฀ and฀XPC฀segment฀are฀unaffected฀by฀ the฀selective฀inversion฀of฀A16฀and฀A19฀ Figure฀2.15:฀The฀MMU฀and฀ MIU฀form฀a฀complete฀memory฀ management฀structure.

16-bit Address From CPU

Memory Management Unit XPC

+

16-bit Logical Address 20- bit Physical Address

STACKSEG

+

16-bit Logical Address 20- bit Physical Address

DATASEG

+

16-bit Logical Address 20- bit Physical Address

From CPU Instruction /Data Transaction

20- bit Physical Address From MMU

Memory Interface Unit

A16

A18

A19

Separate I& D Space Control

MMIDR

MB0CR

Memory Interface control

MB1CR MB2CR MB3CR

CS0

60

CS1

CS2

OE0

OE1 WE0 WE1

A16

A18

A19

A0..15, 17

The฀Basics dictated฀by฀the฀MMIDR.฀This฀arrangement฀allows฀the฀XPC฀Segment฀(and฀Stack฀Segment)฀to฀ map฀into฀the฀physical฀memory฀without฀complications฀from฀the฀MIU. The฀MIU฀is฀also฀responsible฀for฀generating฀the฀memory฀interface฀signals฀(/CS0,฀/CS1,฀/CS2,฀ /OE0,฀/OE1,฀/WE0,฀/WE1).฀This฀is฀done฀through฀the฀four฀Memory฀Bank฀Control฀Registers฀ (MBxCR).฀ The฀one฀megabyte฀memory฀space฀is฀divided฀into฀four฀256K฀quadrants.฀Each฀quadrant฀has฀a฀ corresponding฀MBxCR.฀These฀registers฀allow฀the฀application฀program฀to฀specify฀the฀assertion฀ of฀the฀memory฀interface฀signals฀as฀a฀function฀of฀the฀physical฀address฀being฀accessed.฀This฀ allows฀the฀Rabbit฀to฀accommodate฀a฀variety฀of฀memory฀devices.฀ For฀example,฀consider฀a฀system฀with฀a฀512K฀SRAM฀chip฀and฀a฀512K฀flash฀chip.฀A฀logical฀ approach฀to฀memory฀interface฀would฀be฀to฀configure฀MB0CR฀and฀MB1CR฀to฀assert฀the฀/CS0.฀ Connect฀/CS0฀to฀the฀flash฀chip.฀Configure฀MB2CR฀and฀MB3CR฀to฀assert฀/CS1฀for฀accesses฀ to฀top฀512K฀of฀the฀memory฀space.฀Then฀connect฀/CS1฀to฀enable฀the฀SRAM.฀Both฀the฀flash฀ and฀SRAM฀could฀share฀the฀Write฀Enables฀and฀ WE0 Output฀Enables. OE 0 Next,฀consider฀a฀system฀with฀the฀same฀512K฀ Memory Device 1 SRAM฀chip,฀but฀only฀a฀256K฀flash฀chip.฀In฀ CS0 this฀system,฀we฀probably฀still฀want฀to฀place฀ the฀flash฀chip฀down฀at฀0x0000฀(as฀that’s฀where฀ Memory Device 2 the฀Rabbit฀will฀want฀to฀boot฀from).฀We฀may฀ still฀want฀to฀place฀the฀SRAM฀at฀the฀top฀of฀the฀ 20-bit฀address฀space.฀We฀configure฀MB0CR฀to฀ assert฀the฀/CS0,฀then฀connect฀/CS0฀to฀the฀flash฀ chip.฀Next,฀configure฀MB2CR฀and฀MB3CR฀ Memory Device 3 to฀assert฀/CS1฀for฀accesses฀to฀the฀top฀512K฀ CS1 of฀the฀memory฀space฀and฀connect฀/CS1฀to฀the฀ Memory Device 4 SRAM.฀Now฀we฀must฀decide฀what฀to฀do฀if฀ the฀application฀attempts฀to฀access฀the฀second฀ quadrant฀(256K–512K).฀We฀might฀map฀that฀ quadrant฀back฀onto฀the฀flash฀memory,฀or฀we฀ might฀map฀it฀to฀the฀SRAM.฀Another฀approach฀ would฀be฀to฀configure฀MB1CR฀to฀assert฀/CS2,฀ Memory Device 5 then฀use฀/CS2฀to฀generate฀an฀external฀interrupt.฀ CS2 The฀ISR฀could฀diagnose฀the฀attempt฀to฀access฀a฀ Memory Device 6 nonexistent฀memory฀device฀and฀set฀the฀system฀ WE1 up฀for฀a฀graceful฀recovery. OE 1 Now฀that฀we฀understand฀how฀an฀application฀ program฀can฀modify฀the฀MBxCR฀registers฀to฀ A0-A19 assign฀a฀/CSx฀to฀specific฀address฀range,฀let’s฀ Figure฀2.16:฀The฀Rabbit฀3000฀can฀directly฀ look฀at฀how฀we฀can฀use฀this฀to฀connect฀six฀฀ connect฀to฀six฀memory฀devices,฀each฀of฀ 1฀MB฀memory฀devices฀to฀the฀Rabbit.฀Figure฀ which฀can฀be฀up฀to฀one฀megabyte. 2.16฀shows฀such฀an฀arrangement. 61

Chapter฀2 The฀astute฀engineer฀will,฀at฀ this฀point,฀be฀squinting฀and฀ wondering฀how฀the฀four฀ Memory฀Bank฀Control฀ Registers฀(MBxCRs)฀will฀ generate฀three฀chip-selects฀ across฀six฀megabytes฀of฀ address฀space.฀The฀answer฀ is,฀“not฀automagically.”฀ The฀application฀program฀ must฀do฀the฀bookkeeping฀ necessary฀to฀keep฀track฀ of฀which฀chip฀selects฀are฀ set฀up฀at฀any฀given฀time฀ in฀which฀MBxCR.฀The฀ application฀program฀must฀ also฀keep฀adjusting฀the฀ MBxCR฀configuration฀ to฀swap฀in฀the฀desired฀ memory฀device฀at฀the฀desired฀time.฀Keep฀in฀mind฀ that฀this฀is฀not฀normally฀ required,฀since฀products฀ manufactured฀by฀Rabbit฀Semiconductor฀have฀ at฀most฀one฀megabyte฀of฀ memory. The฀Rabbit฀MIU฀offers฀an฀ additional฀feature฀that฀is฀useful฀ for฀managing฀memory฀sizes฀ greater฀than฀1฀MB.฀This฀is฀฀ the฀selective฀inversion฀of฀A18฀ and฀A19฀depending฀on฀the฀ MBxCRs.฀ Figure฀2.17฀shows฀how฀4฀MB฀ of฀memory฀can฀be฀connected฀ to฀the฀Rabbit฀and,฀through฀the฀ use฀of฀selective฀inversion,฀256฀ KB฀quadrants฀in฀each฀memory฀ device฀may฀be฀paged฀into฀the฀

Selective Inversion Required to Access this page 11 10

Address logic levels for A 19 and A18 01 00

MB3CR

A19 A18

A19 A18

/CS1 /OE1 /WE1

11 10 01 00

MB2CR

A19 A18

/CS1 /OE0 /WE0

11 10 01 00

A19 A18

A19 A18

A19 A18

A19 A18

/CS0 /OE1 /WE1

Only one of the 256K pages are visible at a time based on the selective inversion programmed into the MBxCR

11 10 01 00

MB0CR

A19 A18

A19 A18

A19 A18

MMU

MB1CR

A19 A18

A19 A18

/CS0 /OE0 /WE0

A19 A18

A19 A18

A19 A18

A19 A18

Default Pages Shown in Gray

Each 1 MB memory device is controlled by one of the Memory Bank Control Registers

Figure฀2.17:฀Selective฀inversion฀of฀A18฀and฀A19฀ allow฀the฀Rabbit฀to฀page฀256฀KB฀quadrants฀in฀ 1฀MB฀external฀memory฀devices฀manually.

62

The฀Basics visible฀space.฀Again,฀the฀application฀program฀is฀responsible฀for฀the฀paging,฀and฀careful฀housekeeping฀will฀be฀required. In฀Figure฀2.17,฀it฀is฀important฀to฀understand฀that฀the฀selective฀inversion฀of฀A18฀and฀A19฀occur฀ after฀the฀MBxCR฀register฀has฀been฀chosen.฀In฀Figure฀2.15,฀as฀well฀as฀in฀Figure฀2.17,฀the฀ address฀lines฀A18฀and฀A19฀are฀brought฀from฀the฀MMU฀into฀the฀Memory฀Interface฀Control฀ block.฀The฀Memory฀Interface฀Control฀block฀uses฀A18฀and฀A19฀to฀determine฀which฀MBxCR฀ is฀applicable฀to฀the฀current฀address.฀If฀selective฀inversion฀is฀required,฀then฀the฀inversion฀is฀ accomplished฀on฀the฀A18฀and฀A19฀line฀inside฀of฀the฀Memory฀Interface฀Control฀block฀shown฀ in฀Figure฀2.15. Figure฀2.15฀shows฀that฀A19฀may฀be฀inverted฀as฀part฀of฀the฀separate฀I฀&฀D฀functionality฀before฀ being฀delivered฀to฀the฀Memory฀Interface฀Control฀Block.฀For฀the฀sake฀of฀understanding฀the฀ Rabbit’s฀ability฀to฀index฀large฀memory฀blocks฀by฀paging,฀through฀the฀use฀of฀selective฀inversion฀ of฀A18฀and฀A19,฀the฀separate฀I฀&฀D฀space฀functionality฀should฀be฀ignored฀initially. As฀a฀practical฀matter,฀Rabbit฀Semiconductor฀and฀Z-World฀do฀not฀offer฀any฀commercial฀ products฀with฀more฀than฀1฀MB฀of฀memory฀on฀the฀main฀processor฀bus.฀Dynamic฀C฀does฀not฀ support฀the฀bookkeeping฀to฀track฀the฀manual฀paging฀of฀external฀memory฀devices฀configured฀ as฀shown฀in฀Figures฀2.16฀and฀2.17.฀ Rabbit฀Semiconductor฀has฀built฀engineering฀prototypes฀sporting฀large฀memory฀footprints฀such฀ as฀those฀shown฀in฀Figures฀2.16฀and฀2.17.฀The฀technology฀exists฀for฀large฀memory฀support.฀It฀ only฀awaits฀an฀application. Figure฀2.15฀omits฀several฀MIU฀related฀registers.฀The฀Memory฀Timing฀Control฀Register฀ (MTCR)฀tells฀the฀MIU฀how฀to฀align฀the฀memory฀interface฀signals฀against฀the฀system฀clock,฀thus฀ allowing฀the฀Rabbit฀maximum฀versatility฀in฀interfacing฀to฀a฀large฀array฀of฀available฀memories. The฀Breakpoint/Debug฀Control฀Register฀(BDCR)฀allows฀the฀MUI฀to฀control฀how฀RST฀28฀ instructions฀are฀handled.฀The฀RST฀28฀instruction฀can฀act฀as฀a฀software฀interrupt฀(commonly฀ called฀a฀trap)฀for฀debugging.฀In฀final฀production฀code,฀the฀RST฀28฀instructions฀can฀be฀left฀in฀the฀ application฀code฀to฀ensure฀overall฀system฀timing฀is฀the฀same฀between฀“debugging”฀and฀“runtime”฀ execution.฀The฀BDCR฀can฀command฀RST฀28฀to฀behave฀as฀a฀NOP.฀Dynamic฀C฀uses฀this฀feature. 2.5.14฀Glueless฀Memory฀Bus Our฀discussion฀of฀the฀Rabbit฀architecture฀started฀with฀the฀CPU฀and฀has฀progressed฀outward฀ through฀the฀array฀of฀onboard฀peripherals฀and฀MMU.฀At฀last฀we฀come฀to฀the฀microprocessor’s฀ largest฀boundary—the฀memory฀bus. Once฀again,฀Rabbit’s฀designers฀pulled฀out฀all฀the฀stops฀to฀create฀an฀easy-to-use฀interface.฀ The฀system฀designer฀can฀plunk฀down฀just฀about฀any฀old฀8-bit฀memory฀device฀and฀the฀Rabbit฀ 3000฀will฀talk฀to฀it.฀The฀Rabbit฀generates฀all฀the฀control฀signals฀necessary฀to฀talk฀to฀memory฀ devices—Output฀Enables,฀Chip฀Selects,฀Write฀Enables฀and฀of฀course฀the฀Address฀and฀Data฀ buses฀are฀all฀provided.฀Memory฀interfacing฀is฀glueless. Figure฀2.16฀shows฀just฀how฀easy฀it฀is฀to฀interface฀memory฀to฀the฀Rabbit฀3000.฀ The฀Rabbit฀Semiconductor฀web฀site฀maintains฀a฀Technical฀Note฀(TN226)฀with฀a฀list฀of฀viable฀ flash฀devices:฀http://www.rabbitsemiconductor.com/support/techNotes_whitePapers.shtml 63

Chapter฀2

2.6฀In฀Summary When฀picking฀a฀processor฀for฀a฀project,฀many฀issues฀need฀to฀be฀considered.฀Some฀are฀technical.฀Some฀are฀economic.฀Some฀are฀related฀to฀risk฀management.฀In฀all฀cases,฀evaluating฀ processors฀or฀controllers฀will฀involve฀apples-to-oranges฀comparisons. Rabbit฀Semiconductor฀grew฀out฀of฀Z-World’s฀vast฀experience฀building฀embedded฀controllers฀ for฀a฀wide฀array฀of฀applications.฀Rabbit‘s฀designers฀created฀a฀processor฀capable฀of฀simplifying฀ many฀of฀the฀tasks฀commonly฀found฀in฀embedded฀system฀design.฀Rabbit฀and฀Z-World,฀through฀ the฀development฀of฀core฀modules฀and฀packaged฀controllers,฀have฀hardware฀solutions฀from฀the฀ chip฀level฀to฀packaged฀controllers. Z-World฀supports฀software฀development฀on฀Rabbit-based฀systems฀with฀highly฀integrated฀tools฀ dubbed฀Dynamic฀C™.฀Third฀party฀tools฀such฀as฀those฀offered฀by฀Softools฀are฀also฀available. Next,฀we฀will฀look฀at฀how฀to฀get฀started฀with฀a฀Rabbit-based฀core฀module฀using฀Dynamic฀C฀as฀ the฀development฀environment.

64

3

CHAPTER฀

Starting฀Out In฀Chapter฀1,฀we฀covered฀the฀concept฀of฀using฀microprocessors฀versus฀cores.฀We฀will฀start฀ this฀chapter฀with฀a฀brief฀description฀of฀the฀RCM3200฀Rabbit฀core฀and฀then฀get฀into฀the฀Rabbit฀ development฀environment.฀We฀will฀cover฀development฀and฀debugging฀aspects฀of฀Dynamic฀ C฀and฀will฀jump฀into฀our฀first฀Rabbit฀program.฀We฀will฀highlight฀some฀of฀the฀differences฀ between฀Dynamic฀C฀and฀ANSI฀C.฀Knowing฀these฀differences฀will฀make฀our฀life฀easier฀as฀we฀ learn฀to฀program฀with฀Dynamic฀C.

3.1฀Introduction฀to฀the฀RCM3200฀Rabbit฀Core A฀processor฀does฀not฀mean฀a฀lot฀by฀itself.฀The฀designer฀has฀to฀select฀the฀right฀support฀components,฀such฀as฀memory,฀external฀peripherals,฀interface฀components,฀etc.฀The฀designer฀has฀ to฀interface฀these฀components฀to฀the฀CPU,฀and฀design฀the฀timing฀and฀the฀glue฀logic฀to฀make฀ them฀all฀work฀together.฀There฀are฀design฀risks฀involved฀in฀undertaking฀such฀a฀task,฀not฀to฀ mention฀the฀time฀in฀designing,฀prototyping,฀and฀testing฀such฀a฀system. Using฀a฀core฀module฀solves฀most฀of฀these฀issues.฀Buying฀a฀low-cost฀module฀that฀integrates฀ all฀these฀peripherals฀means฀someone฀has฀already฀taken฀the฀design฀through฀the฀prototyping,฀ debugging,฀and฀assembly฀phases.฀In฀addition,฀core฀manufacturers฀generally฀take฀EMI฀issues฀ into฀account.฀This฀allows฀the฀embedded฀system฀builder฀to฀focus฀on฀interface฀issues฀and฀ application฀code.฀ As฀discussed฀in฀Chapter฀1,฀there฀are฀several฀advantages฀to฀using฀cores.฀The฀greatest฀advantage฀ is฀reduced฀time-to-market.฀Instead฀of฀putting฀together฀the฀fundamental฀building฀blocks฀such฀ as฀CPU,฀RAM฀and฀ROM,฀the฀designer฀can฀quickly฀start฀coding฀and฀focus฀instead฀on฀the฀application฀they฀are฀trying฀to฀develop. To฀illustrate฀how฀to฀use฀a฀core฀module,฀we฀will฀setup฀an฀RCM3200฀core฀module฀and฀step฀ through฀the฀code฀development฀process. The฀RCM3200฀core฀offers฀the฀following฀features: ฀

฀

฀

฀

฀

The฀Rabbit฀3000฀CPU฀running฀at฀44.2฀MHz 512K฀of฀Flash฀memory฀for฀code 512K฀of฀fast฀SRAM฀for฀program฀execution฀ 256K฀of฀battery฀backed฀SRAM฀for฀data฀storage Built฀in฀real-time฀clock 65

Chapter฀3 ฀

10/100Base-T฀Ethernet Six฀serial฀ports ฀ 52฀bits฀of฀digital฀I/O฀ ฀ Operation฀from฀3.15฀V฀to฀3.45฀V

฀

During฀development,฀cores฀mount฀on฀prototyping฀boards฀supplied฀by฀Rabbit฀Semiconductor.฀An฀RCM3200฀prototyping฀board฀contains฀connectors฀for฀power฀and฀I/O,฀level฀shifters฀for฀ serial฀I/O,฀a฀reset฀switch,฀and฀a฀pair฀of฀switches฀and฀LEDs฀connected฀to฀I/O฀pins.฀A฀useful฀feature฀of฀the฀prototyping฀board฀is฀the฀prototyping฀area฀that฀has฀both฀thru-holes฀and฀SMT฀pads.฀ This฀is฀where฀designers฀can฀populate฀their฀own฀devices฀and฀interface฀them฀with฀the฀core. The฀Rabbit฀Semiconductor฀prototyping฀boards฀are฀designed฀to฀allow฀a฀system฀developer฀to฀ build฀preliminary฀designs฀and฀write฀code฀on฀the฀prototyping฀board.฀This฀allows฀initial฀system฀ development฀to฀occur฀even฀if฀the฀application’s฀target฀hardware฀is฀not฀available. Once฀final฀hardware฀is฀complete,฀the฀core฀module฀can฀be฀moved฀from฀the฀prototyping฀board฀ to฀the฀target฀hardware฀and฀the฀system฀software฀can฀then฀be฀finalized฀and฀tested.

3.2฀Introduction฀to฀the฀Dynamic฀C฀Development฀Environment The฀Dynamic฀C฀development฀system฀includes฀an฀editor,฀compiler,฀downloader,฀and฀in-circuit฀ debugger.฀The฀development฀tools฀allow฀users฀to฀write฀and฀compile฀their฀code฀on฀a฀Windows฀ platform,฀and฀download฀the฀executable฀code฀to฀the฀core.฀Dynamic฀C฀is฀a฀powerful฀platform฀ for฀development฀and฀debugging: Development: ฀ Dynamic฀C฀includes฀an฀integrated฀development฀environment฀(IDE).฀Users฀do฀not฀ need฀to฀buy฀or฀use฀separate฀editors,฀compilers,฀assemblers฀or฀linkers. ฀ Dynamic฀C฀has฀an฀extensive฀library฀of฀drivers.฀For฀most฀applications,฀designers฀do฀ not฀need฀to฀write฀low-level฀peripheral฀interface฀code.฀They฀simply฀need฀to฀make฀the฀ right฀API฀calls.฀Designers฀can฀focus฀on฀developing฀the฀higher-level฀application฀rather฀ than฀spend฀their฀time฀writing฀low-level฀drivers. ฀ Dynamic฀C฀uses฀a฀serial฀port฀to฀download฀code฀into฀the฀target฀core.฀There฀is฀no฀need฀ to฀use฀an฀expensive฀CPU฀or฀ROM฀emulator.฀Users฀of฀most฀cores฀load฀and฀run฀code฀ from฀flash. ฀ Dynamic฀C฀is฀not฀ANSI฀C.฀We฀will฀highlight฀some฀of฀the฀differences฀as฀we฀move฀along. Debugging: Dynamic฀C฀has฀a฀host฀of฀debugging฀features.฀In฀a฀traditional฀development฀environment฀a฀CPU฀ emulator฀performs฀these฀functions.฀However,฀Dynamic฀C฀performs฀these฀functions,฀saving฀ the฀developer฀hundreds฀or฀thousands฀of฀dollars฀in฀emulator฀costs.฀Dynamic฀C’s฀debugging฀ features฀include: ฀

Breakpoints—Set฀breakpoints฀that฀can฀stop฀program฀flow฀where฀required,฀so฀that฀the฀ programmer฀can฀examine฀and฀change฀the฀state฀of฀variables฀and฀registers฀or฀figure฀out฀ how฀the฀program฀got฀to฀a฀certain฀part฀of฀the฀code

66

Starting฀Out ฀ ฀

฀ ฀

฀ ฀

฀

฀

Single฀stepping—Step฀into฀or฀over฀functions฀at฀a฀source฀or฀machine฀code฀level.฀ Single฀stepping฀will฀let฀the฀programmer฀examine฀program฀flow,฀or฀values฀of฀CPU฀ registers,฀program฀variables,฀or฀memory฀locations. Code฀disassembly—The฀disassembly฀window฀displays฀addresses,฀opcodes,฀mnemonics,฀and฀machine฀cycle฀times.฀This฀can฀help฀the฀programmer฀examine฀how฀C฀code฀got฀ converted฀into฀assembly฀language,฀as฀well฀as฀calculate฀how฀many฀machine฀cycles฀it฀ may฀take฀to฀execute฀a฀section฀of฀code. Switch฀between฀debugging฀at฀machine฀code฀level฀and฀source฀code฀level฀by฀simply฀ opening฀or฀closing฀the฀disassembly฀window.฀ Watch฀expressions—This฀window฀displays฀values฀of฀selected฀variables฀or฀even฀complex฀expressions,฀including฀function฀calls.฀The฀programmer฀can฀therefore฀examine฀or฀ evaluate฀values฀of฀selected฀variables฀during฀program฀execution.฀Watch฀expressions฀ can฀be฀updated฀with฀or฀without฀stopping฀program฀execution฀and฀can฀be฀used฀to฀trigger฀the฀operation฀of฀hardware฀devices฀in฀the฀target.฀Use฀the฀mouse฀to฀“hover฀over”฀a฀ variable฀name฀to฀examine฀its฀value. Register฀window—All฀processor฀registers฀and฀flags฀are฀displayed.฀The฀contents฀of฀ registers฀may฀be฀modified฀as฀needed. Stack฀window—Shows฀the฀contents฀of฀the฀top฀of฀the฀stack.฀ Hex฀memory฀dump—Displays฀the฀contents฀of฀memory฀at฀any฀address.฀ STDIO฀window—printf฀outputs฀to฀this฀window,฀and฀keyboard฀input฀on฀the฀host฀PC฀ can฀be฀detected฀for฀debugging฀purposes.

3.3฀Brief฀Introduction฀to฀Dynamic฀C฀Libraries Dynamic฀C฀provides฀extensive฀libraries฀of฀drivers.฀Low-level฀drivers฀have฀already฀been฀written฀and฀provided฀for฀common฀devices.฀For฀instance,฀Dynamic฀C฀drivers฀for฀I2C,฀SPI,฀various฀ LCD฀displays,฀keypads,฀file฀systems฀on฀flash฀memory฀devices,฀and฀even฀GPS฀interfaces฀are฀ already฀provided.฀A฀complete฀TCP฀stack฀is฀also฀included฀for฀cores฀that฀support฀networking. There฀are฀some฀differences฀between฀Dynamic฀C฀and฀ANSI฀C.฀This฀will฀be฀especially฀important฀to฀programmers฀porting฀code฀to฀a฀Rabbit฀environment.฀As฀we฀cover฀various฀aspects฀of฀ code฀development,฀we฀will฀highlight฀differences฀between฀Dynamic฀C฀and฀ANSI฀C. Source฀code฀for฀Dynamic฀C฀libraries฀is฀supplied฀with฀the฀Dynamic฀C฀distribution.฀Although฀ the฀Dynamic฀C฀library฀files฀end฀with฀a฀“.LIB”฀extension,฀these฀are฀actually฀source฀files฀that฀ can฀be฀opened฀with฀a฀text฀editor.฀ For฀example,฀let฀us฀examine฀the฀LCD฀library.฀If฀Dynamic฀C฀is฀installed฀into฀its฀default฀directories,฀we฀find฀an฀LCD฀library฀file฀at฀DynamicC\Lib\Displays\LCD122KEY7.LIB: The฀library฀file฀defines฀various฀variables฀and฀functions.฀Because฀it฀is฀an฀LCD฀library,฀we฀find฀ functions฀that฀initialize฀a฀display฀and฀allow฀the฀programmer฀to฀write฀to฀an฀LCD.฀ Looking฀at฀the฀function฀descriptions,฀the฀programmer฀can฀quickly฀understand฀how฀Rabbit’s฀ engineers฀implemented฀each฀function.฀The฀embedded฀systems฀designer฀can฀tailor฀the฀library฀ functions฀to฀suit฀particular฀applications฀and฀save฀them฀in฀separate฀libraries. 67

Chapter฀3 Quick฀Summary:  Dynamic฀C฀is฀not฀ANSI฀C ฀ Dynamic฀C฀library฀files฀end฀with฀a฀“.LIB”฀extension,฀and฀are฀source฀files฀that฀ can฀be฀opened฀with฀a฀text฀editor

3.4฀Memory฀Spaces฀in฀Dynamic฀C In฀Chapter฀2,฀we฀looked฀at฀various฀memory฀spaces฀used฀by฀the฀Rabbit.฀Here฀we฀see฀how฀ Dynamic฀C฀manipulates฀the฀MMU฀to฀provide฀an฀optimal฀memory฀usage฀for฀the฀application. The฀Rabbit฀has฀an฀external฀8-bit฀data฀bus.฀This฀allows฀the฀processor฀to฀interface฀to฀inexpensive฀8-bit฀memory฀devices.฀The฀trade-off฀with฀a฀small฀data฀bus฀is฀the฀multiple฀bus฀accesses฀ required฀to฀read฀large฀amounts฀of฀data.฀To฀minimize฀the฀time฀required฀to฀fetch฀operands฀ containing฀addresses฀while฀still฀providing฀a฀useful฀amount฀of฀address฀space,฀the฀Rabbit฀uses฀a฀ 16-bit฀address฀for฀all฀instruction฀operands. A฀16-bit฀address฀requires฀two฀read฀cycles฀over฀the฀data฀bus฀to฀acquire฀an฀address฀as฀an฀operand.฀This฀implies฀an฀address฀space฀limited฀to฀216฀(65536)฀bytes.฀A฀16-bit฀address฀space,฀while฀ usable,฀is฀somewhat฀limiting. To฀achieve฀a฀usable฀memory฀space฀larger฀than฀216฀bytes฀the฀Rabbit’s฀designers฀gave฀the฀microprocessor฀a฀memory฀management฀unit฀(MMU).฀This฀device฀maps฀a฀16-bit฀logical฀address฀to฀a฀ 20-bit฀physical฀address. The฀Rabbit฀designers฀could฀have฀simply฀made฀the฀Rabbit’s฀instructions฀accept฀20-bit฀address฀ operands.฀This฀would฀require฀3฀bytes฀to฀contain฀the฀operands฀and฀would฀therefore฀require฀ three฀fetches฀over฀the฀8-bit฀data฀bus฀to฀pull฀in฀the฀complete฀20-bit฀address.฀This฀is฀a฀50%฀ penalty฀over฀the฀2฀fetches฀required฀to฀gather฀a฀16-bit฀address. Many฀programs฀fit฀quite฀handily฀in฀a฀16-bit฀address฀space.฀The฀performance฀penalty฀incurred฀ by฀making฀all฀the฀instructions฀operate฀on฀a฀20-bit฀address฀is฀not฀desirable.฀The฀MMU฀offers฀a฀ compromise฀between฀a฀large฀address฀space฀and฀efficient฀bus฀utilization.฀Good฀speed฀and฀code฀ density฀are฀achieved฀by฀minimizing฀the฀instruction฀length.฀The฀MMU฀makes฀available฀a฀large฀ address฀space฀to฀applications฀requiring฀more฀than฀a฀16-bit฀address฀space. The฀Rabbit฀3000™฀Designer’s฀Handbook฀covers฀the฀MMU฀in฀exacting฀detail.฀However,฀most฀ engineers฀using฀the฀Rabbit฀only฀need฀understand฀the฀rudimentary฀details฀of฀how฀Dynamic฀C฀ uses฀the฀feature-rich฀Rabbit฀MMU.฀ 3.4.1฀Rabbit’s฀Memory฀Segments The฀Rabbit฀3000’s฀MMU฀maps฀four฀segments฀from฀the฀16-bit฀logical฀address฀space฀into฀the฀ 20-bit฀physical฀address฀space฀accessible฀on฀the฀chip’s฀address฀pins.฀These฀segments฀are฀shown฀ in฀Figure฀3.1. In฀Chapter฀2,฀we฀examined฀the฀MMU฀registers฀in฀some฀detail.฀Further฀information฀is฀also฀available฀in฀the฀Rabbit฀3000฀documentation฀available฀on฀the฀CD฀included฀with฀this฀book.฀We฀will฀ 68

Starting฀Out 1 Megabyte of Physical Memory 0xFFFFF

0xFFFF Extended Memory Segment (XPC Segment)

Stack Segment

Data Segment Root Memory

Root Segment (Base Segment)

0x00000

0x0000

16-bit Logical Address

MMU maps to

20-bit Physical Address

Figure฀3.1:฀The฀Rabbit฀3000฀MMU฀segments.

not฀rehash฀the฀details฀of฀how฀the฀MMU฀performs฀the฀mapping฀of฀a฀16-bit฀logical฀address฀to฀a฀ 20-bit฀physical฀address.฀Our฀objective฀here฀is฀to฀understand฀how฀Dynamic฀C฀uses฀the฀Rabbit’s฀ MMU฀to฀create฀a฀framework฀for฀embedded฀applications฀written฀in฀C฀and฀assembly฀language. Dynamic฀C฀uses฀the฀available฀segments฀differently฀depending฀on฀whether฀separate฀instruction฀ and฀data฀space฀is฀enabled.฀First฀we฀will฀consider฀the฀case฀without฀separate฀I฀&฀D฀space฀enabled. 3.4.2฀Dynamic฀C’s฀Memory฀Usage฀Without฀Separate฀I฀&฀D฀Space Dynamic฀C’s฀support฀of฀separate฀I฀&฀D฀space฀allows฀much฀better฀memory฀utilization฀than฀the฀ older฀model฀without฀separate฀I฀&฀D฀space.฀This฀section฀is฀included฀for฀the฀benefit฀of฀engineers฀who฀may฀have฀to฀maintain฀code฀written฀for฀the฀older฀memory฀model.฀New฀applications฀ should฀be฀developed฀using฀separate฀I฀&฀D฀space.฀The฀newer฀memory฀model฀almost฀doubles฀ the฀amount฀of฀root฀memory฀available฀to฀an฀application. Dynamic฀C฀uses฀each฀of฀the฀four฀segments฀for฀specific฀purposes.฀The฀Root฀Segment฀and฀Data฀ Segment฀hold฀the฀most฀frequently฀accessed฀program฀code฀and฀data.฀The฀Stack฀Segment฀is฀ where฀the฀system฀stack฀resides.฀The฀Extended฀Memory฀Segment฀is฀used฀to฀access฀code฀or฀ data฀that฀is฀placed฀outside฀of฀the฀memory฀mapped฀into฀the฀lower฀three฀segments. 69

Chapter฀3 A฀bit฀of฀Rabbit฀terminology฀worth฀remembering฀is฀the฀term฀“root฀memory.”฀Root฀memory฀ contains฀the฀memory฀pointed฀to฀by฀the฀Root฀segment,฀the฀Data฀Segment฀and฀the฀Stack฀Segment฀(per฀Rabbit฀3000฀Microprocessor฀Designer’s฀Handbook).฀This฀can฀be฀seen฀in฀Figure฀3.1.฀ Another฀bit฀of฀nomenclature฀to฀keep฀in฀mind฀is฀the฀word฀“segment.”฀When฀we฀use฀the฀word฀ “segment”฀we฀are฀referring฀to฀the฀logical฀address฀space฀that฀the฀MMU฀maps฀to฀physical฀ memory.฀This฀is฀a฀function฀of฀the฀Rabbit฀3000฀chip.฀Of฀course,฀Dynamic฀C฀sets฀up฀the฀MMU฀ registers,฀but฀a฀“segment”฀is฀a฀slice฀of฀logical฀address฀space฀and฀correspondingly฀a฀reference฀ to฀the฀physical฀memory฀mapped. Segments฀can฀be฀remapped฀during฀runtime.฀The฀XPC฀segment฀gets฀remapped฀frequently฀to฀ access฀extended฀memory,฀but฀most฀applications฀do฀not฀remap฀the฀other฀segments฀while฀running.฀ The฀semantics฀may฀seem฀a฀little฀picky,฀but฀this฀attention฀to฀detail฀will฀help฀to฀enforce฀the฀ logical฀abstractions฀between฀Dynamic฀C’s฀usage฀of฀the฀Rabbit’s฀hardware฀resources฀and฀the฀ resources฀themselves. An฀example฀is฀the฀phrase฀“Stack฀Segment”฀and฀the฀word฀“stack.”฀The฀“Stack฀Segment”฀is฀ just฀a฀mapping฀of฀a฀slice฀of฀physical฀memory฀into฀logical฀address฀space.฀There฀is฀no฀intrinsic฀ hardware฀requirement฀that฀the฀system฀“stack”฀be฀located฀in฀this฀segment.฀The฀“Stack฀Segment”฀was฀so฀named฀because฀Dynamic฀C฀happens฀to฀use฀this฀third฀MMU฀segment฀to฀hold฀the฀ system฀stack.฀The฀“Stack฀Segment”฀is฀a฀piece฀of฀memory฀mapped฀by฀the฀MMU’s฀third฀segment.฀The฀“stack”฀is฀a฀data฀structure฀that฀could฀be฀placed฀in฀any฀segment. The฀Root฀Segment฀is฀sometimes฀referred฀to฀as฀the฀Base฀Segment.฀The฀Root฀Segment฀maps฀ to฀BIOS฀code,฀application฀code฀and฀Dynamic฀C฀constants.฀In฀most฀designs฀the฀Root฀Segment฀ is฀mapped฀to฀flash฀memory.฀The฀BIOS฀is฀placed฀at฀address฀0x00000฀and฀grows฀upward.฀The฀ application฀code฀is฀placed฀above฀the฀BIOS฀and฀grows฀to฀the฀top฀of฀the฀segment.฀Constants฀are฀ intermixed฀with฀the฀application฀code. Dynamic฀C฀refers฀to฀executable฀code฀placed฀in฀the฀Root฀Segment฀as฀“Root฀Code.”฀The฀ Dynamic฀C฀constants฀are฀called฀“Root฀Constants”฀and฀are฀also฀stored฀in฀the฀Root฀Segment. The฀Data฀Segment฀is฀used฀by฀Dynamic฀C฀primarily฀to฀hold฀C฀variables.฀The฀Rabbit฀3000฀microprocessor฀can฀actually฀execute฀code฀from฀any฀segment;฀however,฀Dynamic฀C฀uses฀the฀Data฀ Segment฀primarily฀for฀data.฀Application฀data฀placed฀in฀the฀Data฀Segment฀is฀called฀“Root฀Data.” Some฀versions฀of฀Dynamic฀C฀do฀squeeze฀a฀few฀extra฀goodies฀into฀the฀Data฀Segment฀that฀one฀ might฀not฀normally฀associate฀with฀being฀program฀data.฀These฀items฀are฀nonetheless฀critically฀ important฀to฀the฀proper฀functioning฀of฀an฀embedded฀system.฀A฀quick฀glance฀at฀Figure฀3.2฀ will฀reveal฀that฀at฀the฀top฀1024฀bytes฀of฀the฀data฀segment฀are฀allocated฀to฀hold฀watch-code฀for฀ debugging฀and฀interrupt฀vectors.฀Future฀versions฀of฀Dynamic฀C฀may฀use฀more฀or฀less฀space฀ and฀may฀place฀different฀items฀in฀this฀space. Dynamic฀C฀begins฀placing฀C฀variables฀(Root฀Data)฀just฀below฀the฀watch-code฀and฀grows฀ them฀downward฀toward฀the฀Root฀Segment.฀All฀static฀variables,฀even฀those฀local฀to฀functions฀ placed฀in฀the฀extended฀memory,฀are฀located฀in฀Data฀Segment.฀This฀is฀important฀to฀keep฀in฀ mind฀as฀the฀Data฀Segment฀can฀fill฀up฀quickly.฀

70

Starting฀Out 0xFFFF

Extended Memory Segment Executable code or data depending on Mapping

Mapped to Flash or RAM as needed

Stack Segment System Stack

Data Segment

External Interrupt Vectors Internal Interrupt Vectors Watch Code (Debugging)

1024 bytes

Mapped to RAM

Dynamic C Static Variables

Root Segment

Dynamic C Constants And Application Code

0x0000

Mapped to Flash

Dynamic C BIOS

Logical Address

Figure฀3.2:฀Dynamic฀C’s฀usage฀of฀the฀Rabbit฀3000฀segments.

Dynamic฀C’s฀default฀settings฀allocate฀approximately฀28K฀bytes฀for฀the฀Data฀Segment฀and฀ 24K฀bytes฀for฀the฀Root฀Segment฀spaces.฀The฀macro฀DATAORG,฀found฀in฀RabbitBios.c,฀ can฀be฀modified,฀in฀steps฀of฀0x1000,฀to฀change฀the฀boundary฀between฀these฀two฀spaces.฀Each฀ increase฀of฀0x1000฀will฀gain฀0x1000฀bytes฀for฀code฀with฀an฀attendant฀loss฀of฀0x1000฀for฀data.฀ Each฀incremental฀decrease฀of฀0x1000฀will฀have฀the฀opposite฀effect. The฀Stack฀Segment,฀as฀the฀name฀implies,฀holds฀the฀system฀stack.฀The฀stack฀is฀used฀by฀ Dynamic฀C฀to฀keep฀track฀of฀return฀addresses฀as฀well฀as฀to฀pass฀some฀variables฀between฀functions.฀Variables฀of฀type฀“auto”฀also฀reside฀on฀the฀stack.฀The฀system฀stack฀starts฀at฀the฀top฀of฀ the฀stack฀segment฀and฀grows฀downward. The฀XPC฀Segment,฀sometimes฀called฀the฀Extended฀Memory฀Segment,฀allows฀access฀to฀code฀ and฀data฀that฀is฀stored฀in฀the฀physical฀memory฀devices฀outside฀of฀the฀areas฀pointed฀to฀by฀the฀ three฀segments฀in฀“Root฀Memory.”฀Root฀Memory฀is฀comprised฀of฀the฀Root฀Segment,฀the฀Data฀ Segment฀and฀the฀Stack฀Segment. The฀system’s฀“extended฀memory”฀is฀all฀of฀the฀memory฀not฀mapped฀into฀the฀Root฀Memory฀as฀ shown฀in฀Figure฀3.1.฀Extended฀Memory฀includes฀not฀only฀the฀physical฀memory฀mapped฀into฀ the฀XPC฀segment,฀but฀all฀the฀other฀physical฀memory฀shown฀in฀Figure฀3.1฀in฀gray. When฀we฀refer฀to฀extended฀memory,฀we฀are฀not฀referring฀just฀to฀memory฀mapped฀into฀the฀ XPC฀Segment.฀The฀XPC฀segment฀is฀the฀tool฀(MMU฀segment)฀that฀Dynamic฀C฀uses฀to฀access฀ all฀of฀the฀system’s฀extended฀memory.฀We฀will฀use฀XMEM฀interchangeably฀with฀“extended฀ memory”฀to฀mean฀all฀physical฀memory฀not฀mapped฀into฀Root฀Memory.฀ 71

Chapter฀3 Generally,฀functions฀can฀be฀placed฀in฀XMEM฀or฀in฀root฀code฀space฀interchangeably.฀The฀only฀ reason฀a฀function฀must฀be฀placed฀in฀root฀memory฀is฀if฀the฀function฀is฀an฀interrupt฀service฀routine฀(ISR)฀or฀if฀the฀function฀modifies฀the฀MMU฀mapping฀of฀the฀XPC฀register. ฀If฀an฀application฀grows฀large,฀moving฀functions฀to฀XMEM฀is฀a฀good฀choice฀for฀increasing฀ the฀available฀root฀code฀space.฀Rabbit฀Semiconductor฀has฀an฀excellent฀technical฀note฀TN219,฀ “Root฀Memory฀Usage฀Reduction฀Tips.”฀For฀engineers฀with฀large฀applications,฀this฀technical฀ note฀is฀a฀must฀read.฀ An฀easy฀method฀to฀gain฀more฀space฀for฀Root฀Code฀is฀simply฀to฀enable฀separate฀I฀&฀D฀space,฀ but฀for฀when฀that฀is฀not฀an฀option,฀moving฀function฀code฀to฀XMEM฀is฀the฀best฀alternative. 3.4.3฀Placing฀Functions฀in฀XMEM Assembly฀or฀C฀functions฀may฀be฀placed฀in฀root฀memory฀or฀extended฀memory.฀Access฀to฀ variables฀in฀C฀statements฀is฀not฀affected฀by฀the฀placement฀of฀the฀function,฀since฀all฀variables฀ are฀in฀the฀Data฀Segment฀of฀root฀memory.฀Dynamic฀C฀will฀automatically฀place฀C฀functions฀in฀ extended฀memory฀as฀root฀memory฀fills.฀ Functions฀placed฀in฀extended฀memory฀will฀incur฀a฀slight฀12฀machine฀cycle฀execution฀penalty฀ on฀call฀and฀return.฀This฀is฀because฀the฀assembly฀instructions฀LCALL฀and฀LRET฀take฀longer฀to฀ execute฀than฀the฀assembly฀instructions฀CALL฀and฀RET.฀If฀execution฀speed฀is฀important,฀consider฀leaving฀frequently฀called฀functions฀in฀the฀root฀segment. Short,฀frequently฀used฀functions฀may฀be฀declared฀with฀the฀root฀keyword฀to฀force฀Dynamic฀ C฀to฀load฀them฀in฀Root฀Memory.฀Functions฀that฀have฀embedded฀assembly฀that฀modifies฀the฀ MMU’s฀special฀function฀register฀called฀XPC฀must฀also฀be฀located฀in฀Root฀Memory.฀It฀is฀ always฀a฀good฀idea฀to฀use฀the฀root฀keyword฀to฀explicitly฀tell฀Dynamic฀C฀to฀locate฀functions฀ in฀root฀memory฀if฀the฀functions฀must฀be฀placed฀in฀root฀memory. Interrupt฀service฀routines฀(ISRs)฀must฀always฀be฀located฀in฀root฀memory.฀ISRs฀will฀be฀discussed฀in฀detail฀in฀Chapter฀7. Dynamic฀C฀provides฀the฀keyword฀xmem฀to฀force฀a฀function฀into฀extended฀memory.฀If฀the฀ application฀program฀is฀structured฀such฀that฀it฀really฀matters฀where฀functions฀are฀located,฀the฀ keywords฀root฀and฀xmem฀should฀be฀used฀to฀tell฀the฀compiler฀explicitly฀where฀to฀locate฀the฀ functions.฀If฀Dynamic฀C฀is฀left฀to฀its฀own฀devices,฀there฀is฀no฀guarantee฀that฀different฀versions฀ of฀Dynamic฀C฀will฀locate฀functions฀in฀the฀same฀memory฀segments.฀This฀can฀sometimes฀be฀an฀ issue฀for฀code฀maintenance. For฀example,฀say฀an฀application฀is฀released฀with฀one฀version฀of฀Dynamic฀C,฀and฀a฀year฀later฀ the฀application฀must฀be฀modified.฀If฀the฀xmem฀and฀root฀keywords฀are฀contained฀in฀the฀application฀code,฀it฀doesn’t฀matter฀what฀version฀of฀Dynamic฀C฀the฀second฀engineer฀uses฀to฀modify฀ the฀application.฀The฀compiler฀will฀place฀the฀functions฀is฀the฀intended฀memory—XMEM฀or฀ Root฀Memory.

72

Starting฀Out 3.4.4฀Separate฀Instruction฀and฀Data฀Memory The฀Rabbit฀3000฀microprocessor฀supports฀a฀separate฀memory฀space฀for฀instructions฀and฀data.฀ By฀enabling฀separate฀I฀&฀D฀spaces,฀Dynamic฀C฀is฀essentially฀given฀double฀the฀amount฀of฀root฀ memory฀for฀both฀code฀and฀data.฀This฀is฀a฀powerful฀feature,฀and฀one฀that฀separates฀the฀Rabbit฀ 3000฀processors฀and฀Dynamic฀C฀from฀many฀other฀processor/tool฀combinations฀on฀the฀market. The฀application฀developer฀has฀control฀over฀whether฀Dynamic฀C฀uses฀separate฀instruction฀and฀ data฀space฀(I฀&฀D฀space).฀In฀the฀Dynamic฀C฀integrated฀development฀environment฀(IDE)฀the฀ engineer฀need฀only฀navigate฀the฀OPTIONS฀⇒฀PROJECT฀OPTIONS฀⇒฀COMPILER฀menu฀ and฀use฀the฀check฀box฀labeled฀“enable฀separate฀instruction฀and฀data฀spaces”. When฀Separate฀I฀&฀D฀space฀is฀enabled,฀some฀of฀the฀terms฀Z-World฀uses฀to฀describe฀MMU฀ segments฀and฀their฀contents฀are฀slightly฀altered฀from฀the฀older฀memory฀model฀without฀separate฀ I฀&฀D฀spaces.฀Likewise,฀some฀of฀the฀macro฀definitions฀in฀RabbitBios.c฀have฀altered฀meanings.฀ For฀example,฀the฀DATAORG฀macro฀in฀the฀older฀memory฀model฀tells฀the฀compiler฀how฀much฀ memory฀to฀allocate฀to฀the฀Data฀Segment฀(used฀for฀Root฀Data)฀and฀the฀Root฀Segment฀(used฀for฀ Root฀Code)฀and฀Root฀Constants.฀In฀a฀separate฀I฀&฀D฀space฀model,฀the฀DATAORG฀macro฀has฀ no฀effect฀on฀the฀amount฀of฀memory฀allocated฀to฀code฀(instructions),฀but฀instead,฀tells฀the฀compiler฀how฀to฀split฀the฀data฀space฀between฀Root฀Data฀and฀Root฀Constants.฀With฀separate฀I฀&฀D฀ space฀enabled,฀each฀increase฀of฀0x1000฀will฀decrease฀Root฀Data฀and฀increase฀Root฀Constant฀ spaces฀by฀0x1000฀each. The฀reason฀for฀the฀difference฀in฀function฀is฀an฀artifact฀of฀how฀Dynamic฀C฀uses฀the฀segments฀and฀ how฀the฀MMU฀maps฀memory฀when฀separate฀I฀&฀D฀space฀is฀enabled.฀For฀most฀software฀engineers,฀it฀is฀enough฀to฀know฀that฀enabling฀separate฀I฀&฀D฀space฀will฀usually฀map฀44K฀of฀SRAM฀ and฀flash฀for฀use฀as฀Root฀Data฀and฀Root฀Constants฀and฀52K฀of฀flash฀for฀use฀as฀Root฀Code. The฀more฀inquisitive฀developer฀may฀wish฀to฀delve฀deeper฀into฀the฀memory฀mapping฀scheme.฀ To฀accommodate฀this,฀we฀will฀briefly฀cover฀how฀separate฀I฀&฀D฀space฀works,฀but฀the฀nittygritty฀details฀are฀to฀be฀found฀in฀Chapter฀2฀and฀the฀accompanying฀Rabbit฀Semiconductor฀CD. When฀separate฀I฀&฀D฀space฀is฀enabled,฀the฀lower฀two฀MMU฀segments฀are฀mapped฀to฀different฀ address฀spaces฀in฀the฀physical฀memory฀depending฀on฀whether฀the฀fetch฀is฀for฀an฀instruction฀ or฀data.฀Dynamic฀C฀treats฀the฀lower฀MMU฀two฀segments฀(the฀Root฀Segment฀and฀the฀Data฀ Segment)฀as฀one฀combined฀larger฀segment฀for฀Root฀Code฀during฀instruction฀fetches.฀During฀ data฀fetches,฀Dynamic฀C฀uses฀the฀lowest฀MMU฀segment฀(the฀Root฀Segment)฀to฀access฀Root฀ Constants.฀During฀data฀fetches฀the฀second฀MMU฀segment฀(the฀Data฀Segment)฀is฀used฀to฀access฀Root฀Data. When฀separate฀I฀&฀D฀space฀is฀enabled,฀the฀lower฀two฀MMU฀segments฀are฀both฀mapped฀to฀flash฀ for฀instruction฀fetches,฀while฀for฀data฀fetches฀the฀lower฀MMU฀segment฀is฀mapped฀to฀flash฀(to฀ store฀Root฀Constants)฀and฀the฀second฀MMU฀segment฀is฀mapped฀to฀SRAM฀(to฀store฀Root฀Data). This฀is฀an฀area฀where฀it฀is฀easy฀to฀become฀lost฀or฀misled฀by฀nomenclature.฀When฀separate฀฀ I฀&฀D฀space฀is฀enabled,฀the฀terms฀Root฀Code฀and฀Root฀Data฀mean฀more฀or฀less฀the฀same฀thing฀ to฀the฀compiler฀in฀that฀“code”฀and฀“data”฀are฀being฀manipulated.฀But฀the฀underlying฀segment฀ mapping฀is฀very฀different฀than฀when฀separate฀I฀&฀D฀space฀is฀not฀enabled. 73

Chapter฀3 When฀separate฀I฀&฀D฀space฀is฀not฀enabled,฀the฀Root฀Code฀is฀only฀to฀be฀found฀in฀the฀physical฀ memory฀mapped฀into฀the฀lowest฀MMU฀segment฀(the฀Root฀Segment). When฀separate฀I฀&฀D฀space฀is฀enabled,฀the฀Root฀Code฀is฀found฀in฀both฀the฀lower฀MMU฀segments฀(named฀“Root฀Segment”฀and฀“Data฀Segment”).฀Dynamic฀C฀knows฀that฀the฀separate฀฀ I฀&฀D฀feature฀on฀the฀Rabbit฀3000฀allows฀both฀of฀the฀lower฀MMU฀segments฀to฀map฀to฀alternate฀ places฀in฀physical฀memory฀depending฀on฀the฀type฀of฀CPU฀fetch.฀Dynamic฀C฀sets฀up฀the฀lower฀ MMU฀segments฀so฀that฀they฀BOTH฀map฀to฀flash฀when฀an฀instruction฀is฀being฀fetched.฀Therefore฀Root฀Code฀can฀be฀stored฀in฀physical฀memory฀such฀that฀Dynamic฀C฀can฀use฀the฀two฀lower฀ MMU฀segments฀to฀access฀Root฀Code. This฀may฀seem฀contrary฀to฀the฀segment฀name฀of฀the฀second฀MMU฀segment,฀the฀Data฀Segment.฀The฀reader฀must฀bear฀in฀mind฀that฀the฀MMU฀segments฀were฀named฀based฀on฀the฀older฀ memory฀model฀without฀separate฀I฀&฀D฀space.฀In฀that฀model,฀the฀CPU฀segment฀names฀were฀ descriptive฀of฀how฀Dynamic฀C฀used฀the฀MMU฀segments.฀When฀the฀Rabbit฀3000฀came฀out฀and฀ included฀the฀option฀for฀separate฀I฀&฀D฀space,฀the฀MMU฀segments฀were฀still฀given฀their฀legacy฀ names.฀When฀separate฀I฀&฀D฀space฀was฀enabled,฀Dynamic฀C฀used฀the฀MMU฀segments฀differently,฀but฀the฀segment฀names฀on฀the฀microprocessor฀remained฀the฀same. This฀brings฀us฀to฀how฀Dynamic฀C฀uses฀the฀lower฀two฀MMU฀segments฀when฀separate฀I฀&฀D฀ space฀is฀enabled฀and฀a฀data฀fetch฀(or฀write)฀occurs.฀We฀are฀already฀familiar฀with฀the฀idea฀of฀ “Root฀Data,”฀and฀this฀is฀mapped฀into฀physical฀memory฀(SRAM)฀through฀the฀second฀MMU฀ segment—the฀Data฀Segment. Constants฀are฀another฀type฀of฀data฀with฀which฀Dynamic฀C฀must฀contend.฀In฀the฀older฀memory฀ model฀without฀separate฀I฀&฀D฀space฀enabled,฀constants฀(Root฀Constants)฀were฀intermixed฀ with฀the฀code฀and฀accessed฀by฀Dynamic฀C฀through฀the฀lowest฀MMU฀segment฀(the฀Root฀Segment).฀In฀the฀new฀memory฀model฀with฀separate฀I฀&฀D฀space฀enabled,฀Dynamic฀C฀still฀uses฀the฀ lower฀MMU฀segment฀(the฀root฀segment)฀to฀access฀Root฀Constants.฀But฀with฀separate฀I฀&฀D฀ space฀enabled,฀when฀data฀accesses฀occur,฀the฀lowest฀MMU฀segment฀(root฀segment)฀is฀mapped฀ to฀a฀space฀where฀code฀is฀not฀stored.฀This฀means฀there฀is฀more฀space฀to฀store฀Root฀Constants฀as฀ they฀are฀not฀sharing฀memory฀with฀Root฀Code. Root฀Constants฀must฀be฀stored฀in฀flash.฀This฀implies฀that฀the฀lowest฀MMU฀segment฀is฀mapped฀ into฀physical฀flash฀memory฀for฀both฀instruction฀and฀data฀accesses.฀Root฀Code฀resides฀in฀flash,฀ as฀do฀Root฀Constants. Given฀this฀overview,฀we฀can฀consider฀the฀effect฀of฀DATAORG฀again.฀DATAORG฀is฀used฀to฀ specify฀the฀size฀of฀the฀first฀two฀MMU฀segments.฀Since฀Dynamic฀C฀maps฀the฀first฀two฀MMU฀ segments฀to฀Root฀Code฀for฀instruction฀accesses,฀and฀treats฀the฀first฀two฀MMU฀segments฀as฀ one฀big฀logical฀address฀space฀for฀Root฀Code,฀changing฀DATAORG฀has฀no฀effect฀on฀the฀space฀ available฀for฀Root฀Code. Now฀consider฀the฀case฀when฀separate฀I฀&฀D฀space฀is฀enabled฀and฀data฀is฀being฀accessed.฀The฀ lowest฀MMU฀segment฀(the฀Root฀Segment)฀is฀mapped฀into฀flash฀and฀is฀used฀to฀access฀Root฀ Constants.฀The฀second฀MMU฀segment฀(the฀Data฀Segment)฀is฀mapped฀into฀SRAM฀and฀is฀used฀ to฀access฀Root฀Data.฀ 74

Starting฀Out Changing฀DATAORG฀can฀increase฀or฀decrease฀the฀size฀of฀the฀first฀two฀segments.฀For฀data฀ accesses,฀this฀means฀the฀size฀of฀flash฀mapped฀to฀the฀MMU’s฀first฀segment฀is฀either฀made฀ larger฀or฀smaller฀while฀the฀second฀segment฀is฀oppositely฀affected.฀This฀means฀there฀will฀be฀ more฀or฀less฀flash฀memory฀mapped฀(through฀the฀first฀MMU฀segment)฀for฀Dynamic฀C฀to฀use฀ for฀Root฀Constants฀with฀a฀corresponding฀decrease฀or฀increase฀in฀SRAM฀mapped฀(through฀the฀ second฀MMU฀segment)฀for฀Dynamic฀C฀to฀use฀as฀Root฀Data. When฀separate฀I฀&฀D฀spaces฀are฀enabled,฀the฀stack฀segment฀and฀extended฀memory฀segment฀ are฀unaffected.฀This฀means฀that฀the฀same฀system฀stack฀is฀mapped฀regardless฀of฀whether฀ instructions฀or฀data฀are฀being฀fetched.฀Likewise,฀extended฀memory฀can฀still฀be฀mapped฀ anywhere฀in฀physical฀memory฀to฀accommodate฀storing/retrieving฀either฀executable฀code฀or฀ application฀data. For฀most฀engineers฀it฀is฀enough฀just฀to฀know฀that฀using฀separate฀I฀&฀D฀space฀gives฀the฀ developer฀the฀most฀Root฀memory฀for฀the฀application.฀In฀the฀rare฀circumstance฀in฀which฀the฀ memory฀model฀needs฀to฀be฀tweaked,฀the฀DATAORG฀macro฀is฀easily฀used฀to฀adjust฀the฀ratio฀of฀ Root฀Data฀to฀Root฀Constant฀space฀available.฀For฀the฀truly฀hardcore,฀the฀Rabbit฀documentation฀ has฀all฀the฀details. 3.4.5฀Putting฀It฀All฀Together We฀have฀spent฀a฀considerable฀amount฀of฀time฀going฀over฀segments.฀In฀Section฀3.7.3,฀we฀will฀ look฀at฀code฀that฀will฀reinforce฀these฀concepts.

฀ ฀ ฀ ฀

Quick฀Summary: Logical฀addresses฀are฀16-bits Physical฀addresses฀exist฀outside฀the฀CPU฀in฀a฀20-bit฀space The฀MMU฀maps฀logical฀addresses฀to฀physical฀addresses฀through฀segments Depending฀on฀application฀requirements฀such฀as฀speed฀and฀space,฀it฀may฀be฀ important฀to฀control฀where฀code฀and฀data฀are฀placed.฀Dynamic฀C’s฀defaults฀ can฀be฀overridden,฀allowing฀the฀programmer฀to฀decide฀where฀to฀place฀these฀ code฀elements฀in฀memory

75

Chapter฀3

3.5฀How฀Code฀is฀Compiled฀and฀Run Let’s฀look฀at฀the฀traditional฀build฀process฀and฀contrast฀it฀with฀how฀Dynamic฀C฀builds฀code: 3.5.1฀How฀Code฀is฀Built฀in฀Traditional฀Development฀Environments ฀ The฀programmer฀edits฀the฀code฀in฀an฀editor,฀often฀part฀of฀the฀IDE;฀the฀editor฀saves฀the฀ source฀file฀in฀a฀text฀format. ฀ The฀programmer฀compiles฀the฀code,฀from฀within฀the฀IDE,฀from฀command฀line฀ parameters,฀or฀by฀using฀a฀make฀utility.฀The฀programmer฀can฀either฀do฀a฀“Compile฀ All,”฀which฀will฀compile฀all฀modules;฀or฀the฀make฀utility฀or฀IDE฀can฀only฀compile฀the฀ modules฀that฀were฀changed฀since฀the฀last฀time฀the฀code฀was฀built.฀The฀compiler฀generates฀object฀code฀and฀a฀list฀file฀that฀shows฀how฀each฀line฀of฀C฀code฀got฀compiled฀into฀ one฀or฀more฀lines฀of฀assembly฀code.฀Unless฀specified,฀each฀object฀module฀has฀relative฀ memory฀references฀and฀is฀relocatable฀within฀the฀memory฀space,฀meaning฀it฀can฀reside฀ anywhere฀in฀memory.฀Similarly,฀each฀assembly฀module฀gets฀assembled฀and฀generates฀ its฀own฀relocatable฀object฀module฀and฀list฀file. ฀ If฀there฀are฀no฀compilation฀or฀assembly฀errors,฀the฀linker฀executes฀next,฀putting฀the฀ various฀object฀modules฀together฀into฀a฀single฀binary฀file.฀The฀linker฀converts฀relative฀addresses฀into฀absolute฀addresses,฀and฀creates฀a฀single฀binary฀file฀of฀the฀entire฀ program.฀Almost฀all฀linkers฀nowadays฀also฀have฀a฀built-in฀locator฀that฀locates฀code฀ into฀specific฀memory฀locations.฀The฀linker฀generates฀a฀map฀file฀that฀shows฀a฀number฀ of฀useful฀things,฀including฀where฀each฀object฀module฀resides฀in฀memory,฀how฀much฀ space฀does฀the฀whole฀program฀take,฀etc.฀If฀library฀modules฀are฀utilized,฀the฀linker฀ simply฀links฀in฀pre-compiled฀object฀code฀from฀the฀libraries.฀ ฀ The฀programmer฀can฀download฀the฀binary฀file฀into฀the฀target฀system฀using฀a฀monitor฀ utility,฀a฀bootstrap฀loader,฀using฀an฀EPROM฀emulator,฀or฀by฀simply฀burning฀the฀image฀ into฀an฀EPROM฀and฀plugging฀in฀the฀device฀into฀the฀prototyping฀board.฀If฀a฀CPU฀emulator฀is฀being฀used,฀the฀programmer฀can฀simply฀download฀the฀code฀into฀the฀emulator.

76

Starting฀Out Figure฀3.3฀illustrates฀how฀code฀is฀built฀on฀most฀development฀environments: Process Step

Coding Phase

Activity

Output

Edit Source File

ASCII (text) file

No

Any Source Files changed since the previous compile?

Compilation Phase

No Need to Compile

End of Build Process

Yes

Object Module(s) list file(s)

Compile Changed Files

Yes

Any Compilation Errors? Library Modules

No

Link Object Modules Together

End of Build Process

Binary Image, map file

Figure฀3.3:฀The฀traditional฀build฀process.

3.5.2฀How฀Code฀is฀Built฀with฀Dynamic฀C ฀ The฀programmer฀edits฀the฀code฀in฀the฀Dynamic฀C฀IDE,฀and฀saves฀the฀source฀file฀in฀a฀ text฀format. ฀ The฀Dynamic฀C฀IDE฀compiles฀the฀code.฀If฀needed,฀the฀programmer฀can฀compile฀from฀ command฀line฀parameters.฀Unlike฀most฀other฀development฀environments,฀Dynamic฀C฀ prefers฀to฀compile฀every฀source฀file฀and฀every฀library฀file฀for฀each฀build.฀There฀is฀an฀ option฀that฀allows฀the฀user฀to฀define฀precompiled฀functions. ฀ There฀is฀no฀separate฀linker.฀Each฀build฀results฀in฀a฀single฀binary฀file฀(with฀the฀“.BIN”฀ extension)฀and฀a฀map฀file฀(with฀the฀“.MAP”฀extension). ฀ The฀Dynamic฀C฀IDE฀downloads฀the฀executable฀binary฀file฀into฀the฀target฀system฀ using฀the฀programming฀cable. 77

Chapter฀3 Figure฀3.4฀illustrates฀how฀code฀is฀built฀and฀run฀with฀Dynamic฀C: Process Step

Coding Phase

Compilation Phase

Activity

Output

Edit Source File

ASCII (text) file

Compile All Source Files + Compile All Libraries + Link All Modules Together (if no errors )

Yes

Any Compilation Errors ?

No

List file(s)

En d

of Bu ild

Pr o c es

s

Binary Image, map file

Figure฀3.4:฀How฀Dynamic฀C฀builds฀code.

฀฀ ฀฀ ฀฀ ฀฀

Quick฀Summary: Dynamic฀C฀builds฀code฀differently฀from฀the฀traditional฀edit/compile/link/ download฀cycle Each฀time฀code฀is฀built,฀Dynamic฀C฀always฀compiles฀each฀library฀file฀and฀ each฀source฀file Each฀time฀code฀is฀run,฀Dynamic฀C฀does฀a฀complete฀build. Within฀the฀Dynamic฀C฀IDE,฀executable฀images฀can฀be฀downloaded฀to฀a฀target฀ system฀through฀a฀simple฀programming฀cable Reference: Figure 4.2 Title: Build Process with Dynamic C Drawn By: Kamal Hyder Drawn On: 4/24/03

78

Starting฀Out

3.6฀Setting฀Up฀a฀PC฀as฀an฀RCM3200฀Development฀System Before฀we฀start฀using฀Dynamic฀C฀to฀write฀code,฀we฀need฀to฀set฀up฀an฀RCM3200฀core฀module฀ and฀prototyping฀board.฀This฀simple฀process฀only฀takes฀a฀few฀minutes. Setting฀up฀an฀RCM3200฀development฀system฀requires฀fulfilling฀the฀following฀steps: 1.฀ 2.฀ 3.฀ 4.฀ 5.฀

Using฀the฀CD-ROM฀found฀in฀the฀development฀kit,฀install฀Dynamic฀C฀on฀your฀system. Choose฀a฀COM฀(serial)฀port฀on฀your฀PC฀to฀connect฀to฀the฀RCM3200฀. Attach฀the฀RCM3200฀to฀the฀prototyping฀board. Connect฀the฀serial฀programming฀cable฀between฀the฀PC฀and฀the฀core฀module.฀ Provide฀power฀to฀the฀prototyping฀board.

Now฀that฀the฀hardware฀is฀setup,฀we฀need฀to฀configure฀Dynamic฀C.฀Some฀Rabbit฀core฀modules฀ are฀able฀to฀run฀code฀from฀fast฀SRAM฀instead฀of฀flash.฀This฀feature฀can฀be฀enabled฀from฀the฀ Dynamic฀C฀“Options฀⇒฀Project฀Options฀⇒฀Compiler”฀menu.฀The฀RCM฀3200฀will฀run฀programs฀from฀fast฀SRAM฀instead฀of฀flash.฀ For฀our฀simple฀examples,฀it฀really฀doesn’t฀matter฀whether฀we฀configure฀Dynamic฀C฀to฀generate฀code฀that฀will฀run฀from฀fast฀SRAM฀or฀from฀flash.฀However,฀for฀the฀sake฀of฀consistency,฀we฀ always฀configure฀Dynamic฀C฀to฀enable฀code฀to฀be฀run฀from฀fast฀SRAM฀for฀the฀examples฀in฀ this฀text฀that฀use฀the฀RCM3200.

3.7฀Time฀to฀Start฀Writing฀Code! Now฀that฀the฀RCM3200฀system฀is฀ready฀for฀software฀development,฀it฀is฀time฀to฀roll฀up฀the฀ sleeves฀and฀start฀writing฀code.฀The฀first฀program฀is฀very฀simple.฀The฀intent฀of฀this฀exercise฀is฀ to฀make฀sure฀the฀computer฀(the฀host฀PC)฀is฀able฀to฀talk฀to฀the฀RCM3200.฀Once฀we฀are฀able฀ to฀successfully฀compile฀and฀run฀a฀program,฀we฀will฀explore฀some฀of฀Dynamic฀C’s฀debugging฀ features,฀as฀well฀as฀some฀differences฀between฀Dynamic฀C฀and฀ANSI฀C.฀ 3.7.1฀Project:฀Everyone’s฀First฀Rabbit฀Program It฀has฀been฀customary฀for฀computer฀programmers฀to฀start฀familiarizing฀themselves฀with฀a฀ new฀language฀or฀a฀development฀environment฀by฀writing฀a฀program฀that฀simply฀prints฀a฀string฀ (“Hello฀World”)฀on฀the฀screen.฀We฀do฀just฀that—here’s฀the฀program฀listing: Program฀3.1:฀helloWorld.c main() { ฀ printf฀(“Hello฀World”);฀ }



//฀output฀a฀string฀

79

Chapter฀3 Here’s฀how฀to฀compile฀and฀run฀the฀Rabbit฀program: 1.฀ Launch฀Dynamic฀C฀through฀the฀Windows฀Start฀Menu—or฀the฀Dynamic฀C฀Desktop฀ Icon. 2.฀ Click฀“File”฀and฀“Open”฀to฀load฀the฀source฀file฀“HELLOWORLD.C.”฀This฀program฀ is฀found฀on฀the฀CD-ROM฀accompanying฀this฀book. 3.฀ Press฀the฀“F9”฀function฀key฀to฀run฀the฀code. After฀compiling฀the฀code,฀the฀IDE฀loads฀it฀into฀the฀Rabbit฀Core,฀opens฀a฀serial฀window฀on฀the฀ screen,฀titled฀the฀“STDIO฀window,”฀and฀runs฀the฀program.฀The฀text฀“Hello฀World”฀appears฀ in฀the฀STDIO฀window.฀When฀the฀program฀terminates,฀the฀IDE฀shows฀a฀dialog฀box฀that฀reads฀ “Program฀Terminated.฀Exit฀Code฀0.” If฀this฀doesn’t฀work,฀the฀following฀troubleshooting฀tips฀maybe฀helpful:฀ ฀

The฀target฀should฀be฀ready,฀indicated฀by฀the฀message฀"BIOS฀successfully฀compiled..."฀ If฀this฀message฀did฀not฀appear฀or฀a฀communication฀error฀occurred,฀recompile฀the฀ BIOS฀by฀typing฀฀or฀select฀Reset Target/Compile BIOS฀from฀the฀ Compile฀menu. ฀ If฀the฀message฀"No฀Rabbit฀Processor฀Detected"฀appears,฀verify฀the฀target฀system฀has฀ power฀and฀the฀programming฀cable฀is฀connected฀between฀the฀PC฀and฀the฀target.฀ ฀ The฀programming฀cable฀must฀be฀connected฀to฀the฀controller.฀The฀colored฀wire฀on฀the฀ programming฀cable฀is฀closest฀to฀pin฀1฀on฀the฀programming฀header฀on฀the฀controller.฀ Make฀sure฀you฀use฀the฀connector฀labeled฀as฀PROG฀and฀not฀the฀connector฀labeled฀ DIAG.฀The฀other฀end฀of฀the฀programming฀cable฀must฀be฀connected฀to฀the฀PC฀serial฀ port.฀The฀COM฀port฀specified฀in฀the฀Dynamic฀C฀Options฀menu฀must฀be฀the฀same฀as฀ the฀one฀to฀which฀the฀programming฀cable฀is฀connected.฀ ฀ To฀verify฀the฀correct฀serial฀port฀is฀connected฀to฀the฀target,฀select฀Compile,฀then฀ Compile BIOS,฀or฀press฀.฀If฀the฀“BIOS฀successfully฀compiled฀...”฀message฀does฀not฀display,฀try฀a฀different฀serial฀port฀using฀the฀Dynamic฀C฀Options฀menu.฀ Don’t฀change฀anything฀in฀this฀menu฀except฀the฀COM฀number.฀The฀baud฀rate฀should฀ be฀115,200฀bps฀and฀the฀stop฀bits฀should฀be฀1.



A฀Useful฀Dynamic฀C฀Shortcut “F9”฀causes฀Dynamic฀C฀to฀do฀the฀following: ฀ ฀

Compiles฀the฀project฀source฀code Assuming฀that฀there฀were฀no฀compilation฀errors, –฀ Loads฀the฀code฀into฀flash฀on฀the฀target฀board –฀ Begins฀execution฀of฀the฀application฀code฀on฀the฀target฀system

80

Starting฀Out Although฀the฀program฀terminates,฀the฀IDE฀is฀still฀controlling฀the฀target.฀In฀this฀mode,฀called฀ debug฀or฀run฀mode,฀the฀IDE฀will฀not฀let฀the฀programmer฀edit฀the฀code.฀For฀the฀IDE฀to฀release฀ the฀target฀and฀allow฀editing,฀we฀need฀to฀close฀the฀debug฀session฀by฀clicking฀on฀“Edit”฀and฀ “Edit฀Mode.”฀Alternatively,฀pressing฀“F4”฀will฀enter฀Edit฀Mode. Auxiliary฀Files฀Created฀by฀Dynamic฀C฀During฀Compilation helloWorld.BDL฀is฀the฀binary฀download฀image฀of฀the฀program helloWorld.BRK฀stores฀breakpoint฀information.฀It฀can฀be฀opened฀with฀a฀text฀editor฀ to฀see฀the฀number฀of฀breakpoints฀and฀the฀position฀of฀each฀breakpoint฀in฀the฀source฀file helloWorld.HDL฀is฀a฀simple฀Intel฀format฀Hex฀download฀file฀of฀the฀program฀image helloWorld.MAP฀shows฀what฀the฀labels฀(variables฀and฀code฀references)฀resolve฀to.฀ In฀addition,฀it฀shows฀the฀length฀and฀origin฀of฀each฀module฀and฀which฀memory฀space฀ (Root฀Code,฀Root฀Data,฀or฀XMEM฀Code)฀in฀which฀the฀module฀resides helloWorld.ROM฀is฀a฀program฀image฀in฀a฀proprietary฀format 3.7.2฀Dynamic฀C’s฀Debugging฀Features:฀ Dynamic฀C฀offers฀powerful฀debugging฀features.฀This฀innovation฀eliminates฀the฀need฀for฀an฀ expensive฀hardware฀emulator.฀This฀section฀covers฀the฀basics฀of฀using฀Dynamic฀C’s฀debugging฀ features. Program฀3.2฀(watchDemo.C฀on฀the฀enclosed฀CD-ROM)฀is฀the฀simple฀program฀that฀will฀be฀ used฀to฀illustrate฀Dynamic฀C’s฀debugging฀features. Program฀3.2:฀watchDemo.c void฀delay฀() { ฀ int฀j; ฀ for฀(j=0;฀j฀plot฀“data.log-352-15:44:06-histogram”฀with฀boxes gnuplot>฀set฀output gnuplot>฀exit

For฀more฀information฀on฀the฀versatile฀gnuplot฀program,฀please฀refer฀to฀the฀gnuplot฀man฀page฀ or฀the฀gnuplot฀homepage฀http://www.gnuplot.org/. Sample฀ADC฀Noise฀Quantification฀and฀Visualization

Here฀we฀put฀together฀the฀work฀in฀the฀preceding฀sections฀and฀characterize฀the฀first฀DAQ฀channel฀on฀the฀RCM3400฀development฀board.฀ Since฀our฀voltage฀reference฀was฀2.5฀V฀we฀had฀the฀option฀of฀using฀ADC฀gain฀settings฀of฀1,฀2,฀4฀ and฀5.฀We฀ran฀the฀analysis฀for฀all฀gain฀settings. The฀histograms฀showed฀that฀the฀data฀reported฀from฀the฀ADC฀was฀tightly฀clumped฀around฀the฀ mean฀value.฀In฀each฀histogram,฀only฀two฀or฀three฀bins฀had฀data฀in฀them.฀An฀ideal฀ADC฀in฀a฀ noiseless฀system฀would฀only฀report฀data฀in฀one฀bin.฀In฀real฀life฀systems,฀a฀histogram฀with฀only฀ two฀or฀three฀bins฀is฀excellent.฀Figure฀5.16฀shows฀a฀representative฀histogram฀from฀our฀experiments.฀The฀reported฀ADC฀codes฀are฀on฀the฀horizontal฀axis.฀The฀number฀of฀occurrences฀is฀ shown฀on฀the฀vertical฀axis.

Figure฀5.16:฀GAIN฀=฀4,฀0–5฀volt฀input฀range,฀2.5฀volt฀stimulus. 154

Interfacing฀to฀the฀External฀World An฀example฀of฀the฀textual฀output฀from฀our฀Perl฀script฀is฀shown฀here, Assuming฀an฀฀11฀bit฀converter฀with฀0฀to฀฀20V฀input฀range Assuming฀a฀฀2.500฀volt฀precision฀reference 1฀Code฀=฀0.00977฀Volts Measured฀Mean฀(codes)฀฀=฀฀฀251.9695 Expected฀Mean฀(codes)฀฀=฀฀฀256.0000 offset฀(volts)฀฀฀฀฀฀฀฀฀=฀฀฀0.039฀Volts RMSnoise(ADC฀codes)฀฀฀฀฀฀฀฀฀=฀฀฀0.172 RMSnoise(volts)฀฀฀฀฀฀฀฀฀฀฀฀฀=฀฀฀0.0017 Noise(pk-to-pk)(ADC฀codes)฀฀=฀฀฀0.568 Noise(pk-to-pk)(volts)฀฀฀฀฀฀=฀฀฀0.0055

The฀results฀of฀our฀four฀experiments฀are฀summarized฀in฀Table฀5.2. Table฀5.2:฀The฀RCM3400฀performed฀admirably฀over฀all฀of฀the฀ranges฀measured. Gain฀of฀1 0-20฀volt฀range

Gain฀of฀2 0-19฀volt฀range

Gain฀of฀4 0-5฀volt฀range

Gain฀of฀5 0-4฀volt฀range

Volts฀per฀code

9.77฀mV

4.88฀mV

2.44฀mV

1.95฀mV

Measured฀offset

39฀mV

36฀mV

34฀mV

32฀mV

Measured฀RMS฀ noise฀in฀codes

0.172

0.409

0.403

0.675

Measured฀RMS฀ noise฀in฀volts

1.7฀mV

2.4฀mV

1.0฀mV

1.3฀mV

Measured pk-pk฀noise in฀codes

0.568

1.617

1.329

2.227

Measured฀ pk-pk฀noise฀ in฀volts

5.5฀mV

7.9฀mV

3.2฀mV

4.3฀mV

If฀the฀RMS฀noise฀on฀a฀channel฀is฀greater฀than฀1฀bit,฀we฀can฀compute฀the฀effective฀resolution฀of฀ the฀channel฀from, RESOLUTION EFFECTIVE = ADC฀bits − LOG2 ( NoiseRMS ฀in฀codes ) The฀RCM3400฀has฀RMS฀noise฀levels฀so฀low฀as฀to฀render฀the฀computation฀of฀effective฀resolution฀meaningless.฀The฀logarithm฀becomes฀negative฀for฀RMS฀noise฀levels฀less฀than฀one฀code.฀ The฀conclusion฀we฀can฀draw฀from฀our฀experiments฀is฀that฀the฀effective฀resolution฀of฀the฀ RCM3400฀is฀a฀full฀11฀bits฀for฀gains฀of฀1,฀2,฀4฀and฀5. 155

Chapter฀5 If฀the฀peak-to-peak฀noise฀on฀a฀channel฀is฀greater฀than฀1฀bit,฀we฀can฀compute฀the฀noise฀free฀ resolution฀of฀the฀channel฀from: RESOLUTION NOISE฀FREE = ADC฀bits − LOG2 ( NoisePK − PK ฀in฀codes ) In฀our฀experiment,฀we฀see฀that฀the฀worst฀case฀peak฀to฀peak฀noise฀on฀the฀RCM3400฀channel฀1฀ occurs฀with฀a฀gain฀of฀5฀and฀is฀2.227฀codes.฀From฀the฀equation฀above,฀we฀compute, RESOLUTION NOISE฀FREE฀Gain฀=฀5 = 11 − LOG2 (2.227) = 11 −

ln (2.227) lnn (2 )

= 9.84 ฀bits

Our฀analysis฀of฀the฀RCM3400’s฀DAQ฀channel฀showed฀admirable฀performance.฀For฀this฀ example,฀we฀only฀examined฀one฀of฀the฀eight฀single-ended฀(or฀four฀differential)฀DAQ฀channels฀ on฀the฀RCM3400.฀A฀careful฀engineer฀will฀characterize฀all฀DAQ฀channels฀used฀in฀a฀system.฀ The฀DC฀analysis฀techniques฀presented฀here฀will฀allow฀an฀engineer฀to฀get฀a฀good฀feel฀for฀how฀ accurately฀the฀ADC฀is฀reporting฀sensor฀data฀under฀ideal฀conditions.฀This฀is฀the฀best฀performance฀ that฀the฀engineer฀can฀expect฀from฀the฀system฀without฀calibration฀or฀averaging฀data฀samples.

5.6฀Conclusion Companies฀are฀rolling฀more฀and฀more฀features฀into฀silicon,฀making฀the฀system฀designer’s฀job฀ easier.฀However,฀the฀system฀designer฀must฀still฀exercise฀caution฀when฀devising฀an฀interface฀ between฀real฀world฀sensors฀and฀a฀processor.฀Issues฀of฀ESD,฀bus฀loading฀and฀power฀consumption฀still฀exist฀and฀must฀be฀handled฀by฀the฀system฀engineer. The฀Rabbit฀3000฀has฀simplified฀the฀bus฀loading฀issue฀by฀providing฀an฀auxiliary฀I/O฀bus฀ allowing฀peripheral฀devices฀to฀be฀added฀without฀unduly฀loading฀the฀high-speed฀memory฀bus.฀ The฀core฀module฀designs฀address฀issues฀of฀memory฀interfacing,฀battery฀backup,฀system฀reset,฀ power฀supervision฀and,฀on฀some฀cores,฀analog฀interfacing.฀ Some฀core฀modules฀from฀Rabbit฀Semiconductor฀have฀provided฀low฀noise฀DAQ฀channels.฀For฀ example,฀the฀RCM3400฀will฀drop฀right฀into฀many฀applications฀needing฀one฀to฀eight฀solid฀฀ 11-bit฀resolution฀DAQ฀channels.

156

6

CHAPTER฀

Introduction฀to฀Rabbit฀Assembly฀Language Assembly฀language฀has฀long฀been฀a฀favorite฀of฀programmers,฀for฀many฀reasons.฀Looking฀ back฀into฀history,฀some฀of฀the฀early฀computers฀(such฀as฀the฀MITS฀Altair฀8080)฀could฀only฀be฀ programmed฀in฀assembly฀language.฀Even฀some฀industrial฀machines,฀which฀were฀considered฀ “powerful”฀for฀their฀period,฀had฀primitive฀programming฀technologies฀by฀present฀day฀standards.฀For฀example,฀in฀1974,฀the฀Raytheon฀RDS฀500฀was฀originally฀designed฀to฀track฀missile฀ trajectories฀but฀had฀to฀be฀programmed฀one฀assembly฀instruction฀at฀a฀time,฀using฀bit฀switches฀ on฀the฀front฀panel.฀Soon฀after฀personal฀computers฀began฀to฀get฀popular,฀compilers฀for฀higherlevel฀languages฀began฀to฀appear฀for฀them,฀and฀programmers฀could฀program฀these฀machines฀in฀ BASIC,฀C,฀FORTRAN,฀Pascal,฀and฀even฀Forth. Programmers฀found฀that฀certain฀things฀were฀best฀done฀with฀assembly฀language.฀In฀some฀ cases,฀high-level฀languages฀did฀not฀give฀programmers฀the฀level฀of฀control฀they฀desired฀to฀ manipulate฀machine฀internals,฀while฀in฀other฀cases,฀given฀the฀CPU฀clock฀speeds฀of฀that฀ time,฀certain฀things฀happened฀too฀slowly฀unless฀they฀were฀done฀in฀assembly฀language.฀Even฀ higher฀level฀languages฀such฀as฀C฀and฀“simple”฀languages฀such฀as฀BASIC฀allowed฀programmers฀to฀manipulate฀bits฀and฀bytes฀and฀embed฀assembly฀language฀code฀between฀higher-level฀ statements.฀Moreover,฀the฀limited฀amount฀of฀memory฀available฀in฀such฀systems฀further฀necessitated฀the฀need฀for฀efficient฀code,฀thus฀forcing฀programmers฀to฀use฀assembly฀language. Although฀much฀has฀changed฀from฀those฀days,฀programmers฀working฀with฀microcontrollers฀ still฀conclude฀that฀some฀things฀are฀best฀done฀with฀assembly฀language.฀Although฀CPU฀clock฀ speeds฀have฀drastically฀increased฀in฀the฀last฀three฀decades,฀most฀programmers฀will฀agree฀that฀ the฀most฀efficient฀code,฀in฀the฀context฀of฀execution฀time฀and฀memory฀usage,฀is฀still฀written฀with฀assembly฀language.฀Wherever฀things฀have฀to฀be฀done฀with฀tight฀timing฀constraints,฀ programmers฀will฀explore฀whether฀they฀should฀resort฀to฀assembly฀language.฀For฀example,฀ using฀an฀8-bit฀microcontroller฀to฀implement฀modem฀connection฀sequences,฀a฀programmer฀can฀ usually฀only฀rely฀on฀assembly฀language฀code฀to฀generate฀and฀detect฀tones฀in฀the฀precise฀time฀ windows฀needed. This฀chapter฀starts฀with฀an฀overview฀of฀the฀Rabbit฀instruction฀set.฀Subsequent฀sections฀cover฀ some฀useful฀concepts—passing฀parameters฀between฀assembly฀language฀and฀C,฀and฀coding฀ in฀a฀mixed฀C฀/฀Assembly฀environment.฀The฀chapter฀concludes฀with฀a฀number฀of฀projects,฀ highlighting฀low-level฀control฀of฀on-chip฀and฀off-chip฀peripherals,฀as฀well฀as฀the฀ability฀of฀ assembly฀language฀to฀make฀things฀happen฀in฀critical฀time฀windows.

157

Chapter฀6

6.1฀Introduction฀to฀the฀Rabbit฀3000฀Instruction฀Set Rabbit฀assembly฀instructions฀are฀used฀to: ฀

฀

฀

฀ ฀

฀ ฀

Load฀data฀into฀specific฀registers฀or฀memory฀locations.฀These฀instructions฀are฀used฀ to฀load฀data฀from฀a฀source฀memory฀location฀or฀register฀into฀a฀register฀(usually฀the฀ accumulator),฀perform฀some฀operations,฀and฀transfer฀the฀results฀back฀into฀a฀register฀or฀ memory฀location. Exchange฀contents฀of฀certain฀registers.฀When฀operations฀affect฀the฀contents฀of฀certain฀ registers,฀and฀the฀programmer฀wishes฀to฀save฀the฀original฀contents฀of฀the฀registers,฀ the฀programmer฀can฀use฀these฀exchange฀instructions฀to฀leave฀the฀original฀registers฀ alone,฀work฀an฀alternate฀set฀of฀registers,฀and฀once฀the฀work฀is฀completed,฀the฀original฀ register฀contents฀maybe฀restored.฀When฀the฀registers฀are฀“exchanged฀out,”฀(also฀called฀ “swapped฀out”)฀the฀contents฀of฀the฀original฀registers฀will฀be฀unaffected฀by฀the฀previous฀operations. Push฀data฀on฀and฀off฀the฀stack.฀While฀keeping฀track฀of฀subroutines’฀return฀addresses฀ and฀their฀parameters,฀the฀stack฀is฀a฀handy฀place฀to฀store฀data฀temporarily1.฀Various฀ instructions฀are฀provided฀to฀“push”฀data฀on฀the฀stack฀and฀“pop”฀it฀back฀into฀a฀given฀ register.฀The฀programmer฀must฀be฀careful฀to฀remove฀as฀much฀data฀from฀the฀stack฀as฀ was฀pushed฀onto฀the฀stack,฀otherwise฀a฀stack฀overflow฀condition฀can฀develop. Perform฀arithmetic฀and฀Boolean฀operations.฀Almost฀all฀of฀the฀8-bit฀arithmetic฀operations฀involve฀the฀accumulator,฀while฀16-bit฀operations฀involve฀8-bit฀register฀pairs.฀The฀ Rabbit฀even฀has฀a฀multiply฀instruction! Test฀and฀manipulate฀individual฀bits.฀These฀instructions฀come฀in฀especially฀handy฀for฀ I/O฀operations฀when฀the฀code฀needs฀to฀know฀if฀a฀bit฀got฀set฀by฀an฀external฀input฀or฀ when฀the฀program฀needs฀to฀turn฀an฀external฀device฀on฀or฀off฀by฀manipulating฀a฀port฀ bit.฀Additionally,฀bits฀of฀internal฀registers฀and฀memory฀locations฀can฀be฀tested.฀For฀ example,฀bits฀in฀the฀status฀register฀can฀be฀tested฀to฀determine฀if฀the฀result฀of฀a฀prior฀ arithmetic฀operation฀required฀a฀carry฀or฀borrow. Copy฀entire฀blocks฀of฀memory.฀Some฀of฀the฀Rabbit’s฀instructions฀allow฀the฀programmer฀to฀copy฀an฀entire฀block฀of฀memory฀in฀as฀few฀as฀four฀instructions. Jump฀to฀alternate฀sections฀of฀code.฀Assembly฀programs฀can฀make฀branching฀decisions,฀ usually฀by฀testing฀results฀of฀operations.฀Various฀instructions฀allow฀programmers฀to฀test฀ for฀certain฀Boolean฀conditions฀and฀make฀branching฀decisions฀accordingly.

Although฀the฀Rabbit฀instruction฀set฀is฀derived฀from฀the฀Z80฀instructions,฀quite฀a฀few฀new฀ instructions฀have฀been฀added฀to฀the฀Rabbit฀3000฀microprocessor. The฀assembly฀language฀instructions฀and฀the฀register฀designations฀are฀NOT฀case฀sensitive.฀ However,฀there฀are฀times฀when฀it฀is฀advisable฀to฀use฀specific฀case฀in฀order฀to฀make฀the฀code฀ easier฀to฀read.฀For฀example,฀the฀HL฀register฀pair฀can฀be฀easily฀misinterpreted฀by฀the฀reader฀if฀ it฀is฀in฀lower฀case:฀hl.฀Some฀type฀fonts฀do฀not฀do฀well฀differentiating฀between฀a฀lower฀case฀l฀ and฀the฀digit฀1. 1

฀฀ This฀is฀the฀mechanism฀used฀to฀store฀“auto”฀variables฀in฀Dynamic฀C฀as฀well฀as฀most฀other฀C฀compilers. 158

Introduction฀to฀Rabbit฀Assembly฀Language Rabbit฀assembly฀instructions฀generally฀consist฀of฀an฀opcode฀followed฀by฀zero฀or฀more฀operands.฀The฀opcode฀is฀the฀“instruction”฀while฀the฀operands฀are฀data.฀Operands฀can฀take฀many฀ different฀forms.฀For฀example,฀they฀may฀consist฀of฀a฀16-bit฀address฀or฀be฀a฀single฀bit.฀Table฀ 6.1฀shows฀how฀we฀represent฀operands฀and฀result฀of฀operations฀on฀CPU฀flags.฀The฀following฀ instruction฀descriptions฀use฀the฀operand฀abbreviations฀from฀this฀table. Table฀6.1:฀Operands฀used฀in฀the฀Rabbit฀instruction฀set. Operand

b฀

cc฀ d฀ dd฀ dd’ e฀

f฀

m฀ mn฀ n฀

r,฀g฀

ss v฀ xx฀ yy฀ zz฀

Meaning Bit฀select:฀ 000฀=฀bit฀0,฀001฀=฀bit฀1,฀ 010฀=฀bit฀2,฀011฀=฀bit฀3,฀฀ 100฀=฀bit฀4,฀101฀=฀bit฀5,฀฀ 110฀=฀bit฀6,฀111฀=฀bit฀7 Condition฀code฀select:฀฀ 00฀=฀NZ,฀01฀=฀Z,฀฀ 10฀=฀NC,฀11฀=฀C 7-bit฀(signed)฀displacement.฀Expressed฀in฀two’s฀complement. Word฀register฀select฀destination:฀00฀=฀BC,฀01฀=฀DE,฀10฀=฀HL,฀11฀=฀SP Word฀register฀select฀alternate:฀00฀=฀BC’,฀01฀=฀DE’,฀10฀=฀HL’ 8-bit฀(signed)฀displacement฀added฀to฀PC. Condition฀code฀select:฀ 000฀=฀NZ฀(non฀zero),฀001฀=฀Z฀(zero),฀฀ 010฀=฀NC฀(non฀carry),฀011฀=฀C฀(carry),฀฀ 100฀=฀LZ2฀(logical฀zero),฀101฀=฀LO3฀(logical฀one),฀฀ 110฀=฀P฀(sign฀plus),฀111฀=฀M฀(sign฀minus) MSB฀of฀a฀16-bit฀constant. 16-bit฀constant. 8-bit฀constant฀or฀LSB฀of฀a฀16-bit฀constant. Byte฀register฀select:฀฀ 000฀=฀B,฀001฀=฀C,฀฀ 010฀=฀D,฀011฀=฀E,฀฀ 100฀=฀H,฀101฀=฀L,฀฀ 111฀=฀A Word฀register฀select฀(source):฀00฀=฀BC,฀01฀=฀DE,฀10฀=฀HL,฀11฀=฀SP Restart฀address฀select:฀฀ 010฀=฀0020h,฀011฀=฀0030h,฀฀ 100฀=฀0040h,฀101฀=฀0050h,฀฀ 111฀=฀0070h Word฀register฀select:฀00฀=฀BC,฀01฀=฀DE,฀10฀=฀IX,฀11฀=฀SP Word฀register฀select:฀00฀=฀BC,฀01฀=฀DE,฀10฀=฀IY,฀11฀=฀SP Word฀register฀select:฀00฀=฀BC,฀01฀=฀DE,฀10฀=฀HL,฀11฀=฀AF

฀฀ Logical฀zero฀if฀all฀four฀of฀the฀most฀significant฀bits฀of฀the฀result฀are฀0. ฀฀ Logical฀one฀if฀any฀of฀the฀four฀most฀significant฀bits฀of฀the฀result฀are฀1.

2 3

159

Chapter฀6 Rabbit฀assembly฀instructions฀are฀divided฀into฀the฀following฀groups: ฀

฀ ฀ ฀ ฀ ฀

฀

฀

฀

฀ ฀

฀

฀

฀

฀

฀

฀

฀

฀

Load฀and฀Store −฀ Load฀Immediate฀Data −฀ Load฀&฀Store฀to฀Immediate฀Address −฀ 8-bit฀Indexed฀Load฀and฀Store −฀ 16-bit฀Indexed฀Load฀and฀Store −฀ Register฀to฀Register฀Moves Exchange฀Instructions Stack฀Manipulation฀Instructions Arithmetic฀and฀Logical฀Operations −฀ 8-bit฀Arithmetic฀and฀Logical฀Operations −฀ 16-bit฀Arithmetic฀and฀Logical฀Operations 8-bit฀Bit฀Set,฀Reset฀and฀Test 8-bit฀Increment฀and฀Decrement 8-bit฀Fast฀Accumulator฀Operations 8-bit฀Shifts฀and฀Rotates Instruction฀Prefixes Block฀Move฀Instructions Control฀Instructions—Jumps฀and฀Calls Miscellaneous฀Instructions

To฀those฀familiar฀with฀assembly฀programming,฀several฀groups฀will฀look฀familiar.฀Most฀processors฀have฀“load฀and฀store”฀and฀“arithmetic฀and฀logical”฀instructions.฀Some฀groups฀will฀ look฀familiar฀to฀Z-80฀enthusiasts฀such฀as฀the฀group฀containing฀“exchange฀instructions.”฀However,฀the฀Rabbit฀also฀has฀a฀number฀of฀unique฀instructions฀such฀as฀those฀found฀in฀the฀“block฀ move฀group”฀and฀“instruction฀prefix”฀group. The฀tables฀presented฀in฀this฀text฀use฀a฀format฀and฀nomenclature฀consistent฀with฀the฀Rabbit฀ 2000/3000฀Microprocessor฀Instruction฀Reference฀Manual.฀Each฀instruction฀description฀shows฀ entries฀in฀a฀table฀with฀the฀following฀headings. Instruction

Clk

A

I

S

Z

V

C

Operation

The฀Instruction฀column฀contains฀the฀instruction฀mnemonic฀and฀opcode฀format. The฀Clk฀column฀indicates฀the฀number฀of฀machine฀cycles฀required฀for฀the฀instruction฀to฀execute. The฀A฀column฀indicates฀what฀effect฀the฀ALTD฀prefix฀instruction฀has฀on฀the฀instruction.฀The฀ following฀table฀shows฀the฀key฀for฀the฀“A”฀column.฀ Symbol F R SP

Description ALTD฀selects฀alternate฀flags ALTD฀selects฀the฀alternate฀destination฀register ALTD฀operation฀is฀a฀special฀case 160

Introduction฀to฀Rabbit฀Assembly฀Language The฀I฀column฀indicates฀what฀effect฀the฀IOI฀and฀IOE฀prefix฀instructions฀have฀on฀the฀instruction.฀The฀following฀table฀shows฀the฀key฀for฀the฀“I”฀column. Symbol S D

Description IOI฀and฀IOE฀affect฀source IOI฀and฀IOE฀affect฀destination

The฀S,฀Z,฀V,฀and฀C฀columns฀correspond฀to฀the฀Sign,฀Zero,฀Overflow฀and฀Carry฀flags.฀These฀ are฀found฀in฀the฀Rabbit’s฀“Flags”฀register฀(sometimes฀called฀the฀Status฀or฀Status฀Flags฀register).฀The฀Overflow฀flag฀is฀sometimes฀referred฀to฀as฀the฀Logical/Overflow฀or฀LV฀flag฀in฀the฀ Rabbit฀processor฀documentation.฀The฀following฀table฀shows฀the฀key฀for฀the฀symbols฀used฀in฀ the฀flags฀column. Symbol * 0 1 V L

Description Flag฀affected Flag฀unaffected Flag฀is฀cleared Flag฀is฀set Arithmetic฀Overflow฀is฀stored Logical฀Result฀is฀stored

6.1.1฀Load฀Immediate฀Data฀ Instructions฀that฀belong฀to฀this฀addressing฀mode฀load฀a฀constant฀into฀the฀destination฀register฀ or฀register฀pair.฀This฀is฀called฀the฀“immediate”฀addressing฀mode฀because฀the฀constant฀to฀be฀ loaded฀immediately฀follows฀the฀opcode฀for฀the฀load฀instruction.฀Table฀6.2฀lists฀instructions฀in฀ the฀“immediate”฀group. Table฀6.2:฀Load฀immediate฀data. Instruction LD฀IX,mn LD฀IY,mn LD฀dd,mn LD฀r,n

Clk 8 8 6 4

A

r r

I

S -

Z -

V -

C -

Operation IX฀=฀mn IY฀=฀mn dd฀=฀mn r฀=฀n

The฀following฀instructions฀illustrate฀how฀immediate฀data฀is฀loaded฀in฀8฀and฀16-bit฀registers: ld฀ a,฀5฀ ฀ ld฀ ix,฀0x1234฀

;฀register฀a฀gets฀a฀value฀of฀5฀(decimal) ;฀the฀ix฀register฀gets฀a฀value฀of฀1234฀(hex)

If฀a฀programmer฀programs฀a฀loop฀that฀always฀executes฀a฀fixed฀number฀of฀times,฀a฀register฀can฀ be฀set฀up฀as฀the฀loop฀counter฀and฀the฀register฀can฀be฀initialized฀the฀first฀time฀using฀an฀immediate฀instruction.฀The฀following฀instructions฀illustrate฀how฀this฀is฀done: ฀ ฀

#define฀COUNTER฀ 240฀ ld฀ BC,฀COUNTER฀ ฀

฀ ฀

;฀counter฀constant฀(decimal) ;฀load฀counter฀value

161

Chapter฀6 6.1.2฀Load฀and฀Store฀to฀Immediate฀Address฀ In฀this฀addressing฀mode,฀one฀of฀the฀operands฀is฀a฀register,฀while฀the฀other฀operand฀is฀fetched฀ from฀memory.฀Depending฀on฀the฀instruction,฀a฀16-bit฀address฀is฀used฀to฀point฀to฀source฀or฀ destination฀data.฀This฀instruction฀set฀has฀the฀term฀“immediate฀address”฀in฀it฀because฀the฀16-bit฀ address฀immediately฀follows฀the฀opcode.฀Table฀6.3฀lists฀instructions฀in฀this฀group. Table฀6.3:฀Load฀and฀store฀to฀immediate฀address. Instruction

clk

LD฀(mn),A

10

LD฀A,(mn)

9

A

I

S

Z

V

C

Operation

d

-

-

-

-

(mn)฀=฀A฀฀฀฀฀฀฀฀฀

s

-

-

-

-

A฀=฀(mn)

r

LD฀(mn),HL

13

d

-

-

-

-

(mn)฀=฀L;฀฀฀฀฀฀฀฀฀(mn+1)฀=฀H

LD฀(mn),IX

15

d

-

-

-

-

(mn)฀=฀IXL;฀฀฀฀฀(mn+1)฀=฀IXH

LD฀(mn),IY

15

d

-

-

-

-

(mn)฀=฀IYL;฀฀฀฀฀(mn+1)฀=฀IYH

LD฀(mn),ss

15

d

-

-

-

-

(mn)฀=฀ssL;฀฀฀฀฀฀(mn+1)฀=฀ssH

LD฀HL,(mn)

11

s

-

-

-

-

L฀=฀(mn);฀฀฀฀฀฀฀฀฀H฀=฀(mn+1)

LD฀IX,(mn)

13

s

-

-

-

-

IXL฀=฀(mn);฀฀฀฀฀IXH฀=฀(mn+1)

LD฀IY,(mn)

13

s

-

-

-

-

IYL฀=฀(mn);฀฀฀฀฀IYH฀=฀(mn+1)

LD฀dd,(mn)

13

s

-

-

-

-

ddL฀=฀(mn);฀฀฀฀฀ddH฀=฀(mn+1)

r

r

The฀above฀instructions฀are฀useful฀for฀performing฀simple฀pointer-based฀operations.฀The฀16-bit฀ pointer฀can฀store฀or฀retrieve฀one฀or฀two฀bytes฀to฀or฀from฀memory฀(or฀I/O,฀if฀the฀I/O฀is฀memory฀ mapped).฀For฀example,฀if฀a฀program฀has฀to฀use฀a฀number฀of฀variables,฀the฀variables฀can฀be฀ stored฀in฀memory฀locations฀and฀the฀results฀of฀operations฀can฀be฀stored฀in฀the฀variables. The฀following฀instructions฀illustrate฀some฀examples฀of฀this฀instruction฀group: ฀ ฀ ฀

#define฀BUFFER_SIZE฀64฀ char฀ bytestoread฀ ฀ int฀ bytecounter฀ ฀

;฀spce฀to฀allocate฀for฀buffer ;฀number฀of฀bytes฀to฀read ;฀number฀of฀bytes฀that฀have฀been฀read



ld฀

a,฀(bytestoread)฀

;฀find฀out฀how฀many฀bytes฀to฀read



ld฀

(bytecounter),฀hl฀

;฀update฀byte฀counter



ld฀

ix,(sp+2+BUFFER_SIZE)฀

;฀ix=buffer

6.1.3฀8-bit฀Indexed฀Load฀and฀Store฀ In฀their฀simplest฀form,฀these฀instructions฀come฀in฀handy฀for฀performing฀pointer-based฀operations.฀For฀instance,฀if฀a฀program฀has฀to฀add฀a฀set฀of฀values฀stored฀in฀RAM,฀the฀programmer฀ can฀set฀up฀a฀register฀pair฀to฀point฀to฀the฀starting฀location฀of฀the฀table฀in฀RAM,฀read฀the฀values฀ one฀by฀one,฀and฀keep฀adding฀them฀to฀a฀destination฀register. 162

Introduction฀to฀Rabbit฀Assembly฀Language In฀a฀more฀complex฀application฀of฀indexed฀addressing,฀the฀contents฀of฀an฀index฀register฀are฀ added฀to฀a฀displacement฀to฀compute฀the฀address฀of฀the฀operand.฀This฀is฀what฀the฀“IX+d”฀ and฀“IY+d”฀instructions฀below฀accomplish,฀where฀“d”฀is฀the฀8-bit฀displacement.฀C฀language฀ programmers฀can฀think฀of฀the฀“index฀with฀displacement”฀model฀as฀a฀structure฀where฀the฀index฀ register฀points฀to฀the฀beginning฀of฀the฀structure฀and฀the฀displacement฀is฀used฀to฀point฀to฀elements฀within฀the฀structure.฀Table฀6.4฀lists฀8-bit฀indexed฀instructions: Table฀6.4:฀8-bit฀indexed฀load฀and฀store. Instruction

clk

A

I

S

Z

V

C

Operation

LD฀A,(BC)

6

r

s

-

-

-

-

A฀=฀(BC)

LD฀A,(DE)

6

r

s

-

-

-

-

A฀=฀(DE)

LD฀(BC),A

7

d

-

-

-

-

(BC)฀=฀A

LD฀(DE),A

7

d

-

-

-

-

(DE)฀=฀A

LD฀(HL),n

7

d

-

-

-

-

(HL)฀=฀n

LD฀(HL),r

6

d

-

-

-

-

(HL)฀=฀r฀=฀B,฀C,฀D,฀E,฀H,฀L,฀A

LD฀r,(HL)

5

s

-

-

-

-

r฀=฀(HL)

r

LD฀(IX+d),n

11

d

-

-

-

-

(IX+d)฀=฀n

LD฀(IX+d),r

10

d

-

-

-

-

(IX+d)฀=฀r

LD฀r,(IX+d)

9

s

-

-

-

-

r฀=฀(IX+d)

LD฀(IY+d),n

11

d

-

-

-

-

(IY+d)฀=฀n

LD฀(IY+d),r

10

d

-

-

-

-

(Iy+d)฀=฀r

LD฀r,(IY+d)

9

s

-

-

-

-

r฀=฀(IY+d)

r

r

A฀simple฀example฀of฀indexed฀addressing฀would฀be฀a฀program฀that฀adds฀a฀series฀of฀contiguous฀integers฀in฀RAM.฀The฀programmer฀can฀set฀up฀a฀16-bit฀register฀pair฀to฀point฀to฀the฀start฀of฀ the฀table,฀and฀then฀load฀each฀integer฀into฀the฀accumulator.฀The฀program฀can฀add฀individual฀ integers฀to฀a฀32-bit฀result฀value฀in฀RAM. The฀following฀instructions฀illustrate฀8-bit฀indexed฀load฀and฀stores: ฀ ฀

ld฀ ld฀

฀ ฀

a,(iy+8)฀ (ix+8),b฀

฀ ฀

;฀ ;฀

163

Chapter฀6 6.1.4฀16-bit฀Indexed฀Load฀and฀Store฀ The฀16-bit฀loads฀and฀stores฀are฀similar฀to฀their฀counterparts฀described฀in฀Section฀6.1.3,฀except฀ that฀in฀this฀case฀the฀source฀or฀destination฀is฀a฀16-bit฀register฀pair฀or฀two฀contiguous฀locations฀ in฀memory. Because฀these฀instructions฀require฀fetching฀addresses฀from฀memory,฀adding฀a฀displacement,฀ and฀storing฀the฀contents฀to฀a฀destination฀register฀pair,฀the฀instructions฀require฀a฀relatively฀large฀ number฀of฀clock฀cycles฀to฀execute.฀Table฀6.5฀lists฀16-bit฀indexed฀instructions: Table฀6.5:฀16-bit฀indexed฀load฀and฀store. Instruction

clk

A

I

S

Z

V

C

Operation

d

-

-

-

-

(HL+d)฀=฀L;฀(HL+d+1)฀=฀H

s

-

-

-

-

L฀=฀(HL+d);฀H฀=฀(HL+d+1)

LD฀(HL+d),HL

13

LD฀HL,(HL+d)

11

LD฀(SP+n),HL

11

-

-

-

-

(SP+n)฀=฀L;฀(SP+n+1)฀=฀H

LD฀(SP+n),IX

13

-

-

-

-

(SP+n)฀=฀IXL;฀(SP+n+1)฀=฀IXH

LD฀(SP+n),IY

13

-

-

-

-

(SP+n)฀=฀IYL;฀(SP+n+1)฀=฀IYH

LD฀HL,(SP+n)

9

-

-

-

-

L฀=฀(SP+n);฀H฀=฀(SP+n+1)

LD฀IX,(SP+n)

11

-

-

-

-

IXL฀=฀(SP+n);฀IXH฀=฀(SP+n+1)

LD฀IY,(SP+n)

11

-

-

-

-

IYL฀=฀(SP+n);฀IYH฀=฀(SP+n+1)

LD฀(IX+d),HL

11

d

-

-

-

-

(IX+d)฀=฀L;฀(IX+d+1)฀=฀H

LD฀HL,(IX+d)

9

s

-

-

-

-

L฀=฀(IX+d);฀H฀=฀(IX+d+1)

LD฀(IY+d),HL

13

d

-

-

-

-

(IY+d)฀=฀L;฀(IY+d+1)฀=฀H

LD฀HL,(IY+d)

11

s฀

-

-

-

-

L฀=฀(IY+d);฀H฀=฀(IY+d+1)

r

r

r r

The฀following฀instructions฀utilize฀16-bit฀indexed฀load฀and฀stores: ฀ ฀

ld฀ ld฀

฀ ฀

hl,(ix+4)฀ (ix+2),hl฀

฀ ฀

;฀ ;฀

6.1.5฀Register฀to฀Register฀Moves฀ This฀addressing฀mode฀is฀also฀referred฀to฀as฀“register฀addressing,”฀since฀all฀the฀operands฀are฀ the฀CPU฀registers.฀As฀the฀name฀implies,฀these฀instructions฀copy฀contents฀of฀one฀register฀ into฀another—any฀of฀the฀8-bit฀registers฀can฀be฀moved฀into฀any฀other฀8-bit฀register.฀This฀is฀ necessary฀because฀most฀of฀the฀math฀and฀logical฀operations฀are฀performed฀in฀an฀accumulator฀(register฀A฀or฀register฀pair฀HL).฀As฀shown฀in฀Table฀6.6,฀certain฀16-bit฀registers฀can฀also฀ perform฀a฀move฀into฀other฀16-bit฀registers. Because฀register฀to฀register฀moves฀happen฀within฀the฀CPU฀and฀do฀not฀require฀operands฀to฀be฀ fetched฀from฀memory,฀the฀instructions฀take฀few฀clock฀cycles.

164

Introduction฀to฀Rabbit฀Assembly฀Language Table฀6.6:฀Register฀to฀register฀moves. Instruction

clk

A

LD฀r,g

2

LD฀A,EIR

I

S

Z

V

C

Operation

R

-

-

-

-

r฀=฀g฀(r,฀g฀any฀of฀B,฀C,฀D,฀E,฀H,฀L,฀A)

4

fr

*

*

-

-

A฀=฀EIR

LD฀A,IIR

4

fr

*

*

-

-

A฀=฀IIR

LD฀A,XPC

4

R

-

-

-

-

A฀=฀MMU

LD฀EIR,A

4

-

-

-

-

EIR฀=฀A

LD฀IIR,A

4

-

-

-

-

IIR฀=฀A

LD฀XPC,A

4

-

-

-

-

XPC฀=฀A

LD฀HL,IX

4

R

-

-

-

-

HL฀=฀IX

LD฀HL,IY

4

R

-

-

-

-

HL฀=฀IY

LD฀IX,HL

4

-

-

-

-

IX฀=฀HL

LD฀IY,HL

4

-

-

-

-

IY฀=฀HL

LD฀SP,HL

2

-

-

-

-

SP฀=฀HL

LD฀SP,IX

4

-

-

-

-

SP฀=฀IX

LD฀SP,IY

4

-

-

-

-

SP฀=฀IY

LD฀dd',BC

4

-

-

-

-

dd’฀=฀BC฀(dd':฀BC',฀DE',฀HL')

LD฀dd',DE

4

-

-

-

-

dd’฀=฀DE฀(dd':฀BC',฀DE',฀HL')

For฀example,฀consider฀the฀IIR฀register:฀it฀points฀to฀an฀interrupt฀vector฀table฀specific฀to฀internally฀generated฀interrupts.฀When฀a฀programmer฀wishes฀to฀use฀internally฀generated฀interrupts,฀ the฀IIR฀register฀cannot฀be฀immediately฀loaded฀with฀a฀value—such฀an฀opcode฀does฀not฀exist.฀ Instead,฀the฀immediate฀value฀can฀be฀loaded฀in฀the฀accumulator,฀and฀the฀accumulator฀can฀be฀ copied฀into฀the฀IIR. 6.1.6฀Exchange฀Instructions฀ The฀CPU฀contains฀an฀“alternate฀register฀set,”฀where฀register฀pairs฀AF,฀HL,฀BC,฀and฀DE฀contain฀their฀alternates,฀AF",฀HL',฀BC',฀and฀DE',฀respectively.฀Under฀normal฀circumstances,฀the฀ programmer฀does฀not฀use฀the฀alternate฀register฀pairs,฀since฀Dynamic฀C฀uses฀them฀for฀its฀own฀ purposes. As฀the฀name฀indicated,฀the฀“Exchange”฀instructions฀swap฀contents฀of฀16-bit฀registers฀with฀ special฀alternate฀registers.฀For฀instance,฀the฀“EX฀DE',HL”฀instruction฀swaps฀the฀contents฀of฀ HL฀with฀those฀of฀alternate฀register฀pair฀DE'.฀There฀are฀two฀special฀cases: ฀

The฀“EXX”฀instruction฀does฀three฀swaps฀with฀a฀single฀instruction:฀BC,฀DE฀and฀HL. The฀“EX฀AF,฀AF'”฀instruction฀treats฀the฀accumulator฀and฀flags฀register฀as฀a฀register฀ pair฀and฀swaps฀them฀out฀with฀their฀alternate฀registers. ฀ The฀“ALTD”฀prefix,฀covered฀later฀in฀Section฀6.1.14,฀allows฀instruction฀to฀directly฀ access฀the฀alternate฀registers,฀without฀exchanging฀all฀the฀registers

฀

165

Chapter฀6 Table฀6.7฀lists฀various฀exchange฀instructions. Table฀6.7:฀Exchange฀instructions. Instruction

clk

A

I

r

S

Z

V

C

Operation

-

-

-

-

H฀฀(SP+1);฀L฀฀(SP)

EX฀(SP),HL

15

EX฀(SP),IX

15

-

-

-

-

IXH฀฀(SP+1);฀IXL฀฀(SP)

EX฀(SP),IY

15

-

-

-

-

IYH฀฀(SP+1);฀IYL฀฀(SP)

EX฀AF,AF'

2

-

-

-

-

EX฀DE',HL

2

s

-

-

-

-

AF฀฀AF’ if฀(!ALTD)฀then฀DE'฀฀HL else฀DE’฀฀HL'

EX฀DE',HL'

4

s

-

-

-

-

EX฀DE,HL

2

s

-

-

-

-

EX฀DE,HL'

4

s

-

-

-

-

DE฀฀HL'

EXX

2

-

-

-

-

BC฀฀BC’;฀DE฀฀DE';฀HL฀฀HL'

DE’฀฀HL' if฀(!ALTD)฀then฀DE฀฀HL else฀DE฀฀HL’

Figure฀6.1฀illustrates฀how฀the฀register฀pairs฀are฀swapped: EX DE,HL A

F

H

L

EX AF,AF’ EX DE,HL’ A’

F’

H‘

L’

D

E

B

C

B’

C’

EX DE’,HL D’

EX DE’,HL’

E’

EXX − exchange HL,HL’,DE,DE’,BC,BC’

Figure฀6.1:฀Exchange฀Instructions

6.1.7฀Stack฀Manipulation฀Instructions฀ It฀is฀assumed฀the฀reader฀knows฀what฀a฀stack฀is฀and฀how฀it฀works.฀Table฀6.8฀lists฀the฀stack฀ manipulation฀instructions.฀The฀first฀instruction฀in฀this฀group฀(ADD฀SP,฀d)฀adds฀a฀one-byte฀ displacement฀to฀the฀stack.฀This฀is฀a฀quick฀way฀for฀the฀programmer฀to฀move฀the฀stack฀pointer฀ forward฀by฀up฀to฀255฀bytes,฀without฀changing฀the฀contents฀of฀the฀stack.฀The฀displacement฀is฀ always฀positive.฀The฀main฀reason฀for฀the฀instruction฀would฀be฀for฀reserving฀a฀block฀of฀memory฀on฀the฀stack. The฀other฀instructions฀in฀this฀group฀either฀push฀16-bit฀register฀pairs฀on฀the฀stack,฀or฀pop฀them฀off. These฀instructions฀allow฀the฀programmer฀to฀store฀(push)฀data฀and฀variables฀on฀the฀stack,฀ and฀then฀read฀them฀back฀(pop฀them)฀when฀required.฀One฀has฀to฀be฀careful฀in฀dealing฀with฀ the฀stack฀when฀working฀with฀subroutines฀or฀interrupts,฀since฀these฀elements฀use฀the฀stack฀to฀ remember฀where฀to฀return฀after฀they฀are฀done.฀For฀instance,฀if฀a฀main฀program฀has฀to฀pass฀ 166

Introduction฀to฀Rabbit฀Assembly฀Language data฀to฀a฀subroutine฀through฀the฀stack,฀the฀calling฀program฀can฀push฀the฀data฀on฀the฀stack.฀ When฀the฀subroutine฀is฀called,฀it฀cannot฀simply฀read฀the฀data฀using฀the฀current฀stack฀pointer,฀ since฀the฀subroutine฀call฀has฀altered฀the฀stack฀contents฀and฀the฀stack฀pointer—the฀stack฀ pointer฀now฀has฀to฀be฀adjusted฀in฀order฀to฀pop฀the฀relevant฀data฀correctly,฀and฀then฀adjusted฀ again฀to฀point฀to฀the฀return฀address฀of฀the฀calling฀program. In฀Dynamic฀C฀it฀is฀the฀responsibility฀of฀the฀CALLING฀routine฀to฀restore฀the฀stack฀pointer฀ once฀the฀called฀routine฀has฀returned.฀The฀called฀routine฀accesses฀the฀passed฀values฀using฀SP฀ relative฀indexed฀addressing. Table฀6.8:฀Stack฀manipulation฀instructions. Instruction

clk A f

I

S

Z

V

C

Operation

-

-

-

*

SP฀=฀SP฀+฀d฀—฀d=0฀to฀255

ADD฀SP,d

4

POP฀IP

7

-

-

-

-

IP฀=฀(SP);฀SP฀=฀SP+1

POP฀IX

9

-

-

-

-

IXL฀=฀(SP);฀IXH฀=฀(SP+1);฀SP฀=฀SP+2

POP฀IY

9

-

-

-

-

POP฀zz

7

-

-

-

-

IYL฀=฀(SP);฀IYH฀=฀(SP+1);฀SP฀=฀SP+2 zzL฀=฀(SP);฀zzH฀=฀(SP+1); SP=SP+2฀—฀zz=฀BC,DE,HL,AF

PUSH฀IP

9

-

-

-

-

(SP-1)฀=฀IP;฀SP฀=฀SP-1

PUSH฀IX

12

-

-

-

-

(SP-1)฀=฀IXH;฀(SP-2)฀=฀IXL;฀SP฀=฀SP-2

PUSH฀IY

12

-

-

-

-

PUSH฀zz

10

-

-

-

-

(SP-1)฀=฀IYH;฀(SP-2)฀=฀IYL;฀SP฀=฀SP-2 (SP-1)฀=฀zzH;฀(SP-2)฀=฀zzL; SP=SP-2฀—฀zz=฀BC,DE,HL,AF

r

6.1.8฀8-bit฀Arithmetic฀and฀Logical฀Operations The฀Rabbit฀microprocessor฀performs฀the฀typical฀arithmetic฀and฀logical฀operations฀found฀ in฀other฀8-bit฀processors:฀adds,฀subtracts,฀complements,฀ANDs,฀ORs,฀XORs.฀There฀are฀no฀ multiply฀or฀divide฀instructions฀in฀this฀instruction฀group,฀although฀the฀16-bit฀instruction฀group฀ allows฀for฀a฀multiply฀involving฀16-bit฀register฀pairs. There฀are฀special฀add฀and฀subtract฀instructions฀that฀also฀take฀the฀carry฀bit฀into฀account. These฀instructions฀work฀using฀the฀accumulator฀and฀an฀operand฀that฀can฀be฀an฀immediate฀value,฀another฀8-bit฀register,฀or฀a฀memory฀location฀pointed฀to฀by฀the฀HL,฀IX฀or฀IY฀register฀pairs.

167

Chapter฀6 Table฀6.9฀lists฀8-bit฀arithmetic฀and฀logical฀operations. Table฀6.9:฀8-bit฀arithmetic฀and฀logical฀operations. Instruction ADC฀A,(HL) ADC฀A,(IX+d) ADC฀A,(IY+d) ADC฀A,n ADC฀A,r ADD฀A,(HL) ADD฀A,(IX+d) ADD฀A,(IY+d) ADD฀A,n ADD฀A,r AND฀(HL) AND฀(IX+d) AND฀(IY+d) AND฀n AND฀r CP฀(HL) CP฀(IX+d) CP฀(IY+d) CP฀n CP฀r OR฀(HL) OR฀(IX+d) OR฀(IY+d) OR฀n OR฀r SBC฀(IX+d) SBC฀(IY+d) SBC฀A,(HL) SBC฀A,n SBC฀A,r SUB฀(HL) SUB฀(IX+d) SUB฀(IY+d) SUB฀n SUB฀r XOR฀(HL) XOR฀(IX+d) XOR฀(IY+d) XOR฀n XOR฀r

clk 5 9 9 4 2 5 9 9 4 2 5 9 9 4 2 5 9 9 4 2 5 9 9 4 2 9 9 5 4 2 5 9 9 4 2 5 9 9 4 2

A fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr f f f f f fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr fr

I s s s s s s s s s s s s s s s s s s s s s s s s

S * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

Z * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *

V V V V V V V V V V V L L L L L V V V V V L L L L L V V V V V V V V V V L L L L L

C4 * * * * * * * * * * 0 0 0 0 0 * * * * * 0 0 0 0 0 * * * * * * * * * * 0 0 0 0 0

Operation A฀=฀A฀+฀(HL)฀+฀CF A฀=฀A฀+฀(IX+d)฀+฀CF A฀=฀A฀+฀(IY+d)฀+฀CF A฀=฀A฀+฀n฀+฀CF A฀=฀A฀+฀r฀+฀CF A฀=฀A฀+฀(HL) A฀=฀A฀+฀(IX+d) A฀=฀A฀+฀(IY+d) A฀=฀A฀+฀n A฀=฀A฀+฀r A฀=฀A฀&฀(HL) A฀=฀A฀&฀(IX+d) A฀=฀A฀&฀(IY+d) A฀=฀A฀&฀n A฀=฀A฀&฀r A฀-฀(HL) A฀-฀(IX+d) A฀-฀(IY+d) A฀-฀n A฀-฀r A฀=฀A฀|฀(HL) A฀=฀A฀|฀(IX+d) A฀=฀A฀|฀(IY+d) A฀=฀A฀|฀n A฀=฀A฀|฀r A฀=฀A฀-฀(IX+d)฀-฀CY A฀=฀A฀-฀(IY+d)฀-฀CY A฀=฀A฀-฀(HL)฀-฀CY A฀=฀A-n-CY฀(cout฀if฀(r-CY)>A) A฀=฀A-r-CY฀(cout฀if฀(r-CY)>A) A฀=฀A฀-฀(HL) A฀=฀A฀-฀(IX+d) A฀=฀A฀-฀(IY+d) A฀=฀A฀-฀n A฀=฀A฀-฀r A฀=฀[A฀&฀~(HL)]฀|฀[~A฀&฀(HL)] A฀=฀[A฀&฀~(IX+d)]฀|฀[~A฀&฀(IX+d)] A฀=฀[A฀&฀~(IY+d)]฀|฀[~A฀&฀(IY+d)] A฀=฀[A฀&฀~n]฀|฀[~A฀&฀n] A฀=฀[A฀&฀~r]฀|฀[~A฀&฀r]

฀฀ SBC฀and฀CP฀instruction฀output฀inverted฀carry.฀C฀is฀set฀if฀A=B.฀SUB฀outputs฀carry฀in฀opposite฀sense฀from฀SBC฀and฀CP.

168

Introduction฀to฀Rabbit฀Assembly฀Language The฀following฀instructions฀illustrate฀8-bit฀arithmetic฀and฀logical฀operations: #define฀ BIT_MASK฀ cp฀ a,b฀ ฀ ;฀ and฀BIT_MASK฀ ; cp฀ 0x01฀ ฀ ; xor฀0x80฀ ฀ ;฀

0xA5

6.1.9฀16-bit฀Arithmetic฀and฀Logical฀Operations These฀instructions,฀shown฀in฀Table฀6.10,฀are฀similar฀to฀their฀8-bit฀counterparts฀except฀for฀one฀ key฀difference:฀because฀the฀Rabbit฀CPU฀only฀has฀8-bit฀registers,฀the฀16-bit฀math฀instructions฀ use฀register฀pairs฀as฀operands. There฀is฀a฀multiply฀instruction฀that฀uses฀BC฀and฀DE฀as฀operands฀and฀stores฀the฀result฀in฀HL฀and฀ BC.฀There฀are฀various฀increment,฀decrement,฀and฀rotate฀instructions฀that฀involve฀register฀pairs. Table฀6.10:฀16-bit฀arithmetic฀and฀logical฀operations. Instruction ADC฀HL,ss ADD฀HL,ss ADD฀IX,xx ADD฀IY,yy ADD฀SP,d AND฀HL,DE AND฀IX,DE AND฀IY,DE BOOL฀HL BOOL฀IX BOOL฀IY DEC฀IX DEC฀IY DEC฀ss INC฀IX INC฀IY INC฀ss MUL OR฀HL,DE OR฀IX,DE OR฀IY,DE RL฀DE RR฀DE RR฀HL RR฀IX RR฀IY SBC฀HL,ss

clk 4 2 4 4 4 2 4 4 2 4 4 4 4 2 4 4 2

A fr fr f f f fr f f fr f f r r

12 2 4 4 2 2 2 4 4 4

fr f f fr fr fr f f fr

I

S * * * * * * * -

Z * * * * * * * -

V V L L L 0 0 0 -

C * * * * * 0 0 0 0 0 0 -

-

-

-

-

* * * * * * * * *

* * * * * * * * *

L L L L L L L L V

0 0 0 * * * * * * 169

Operation HL฀=฀HL฀+฀ss฀+฀CF฀—฀ss=BC,฀DE,฀HL,฀SP HL฀=฀HL฀+฀ss IX฀=฀IX฀+฀xx฀—฀xx=BC,฀DE,฀IX,฀SP IY฀=฀IY฀+฀yy฀—฀yy=BC,฀DE,฀IY,฀SP SP฀=฀SP฀+฀d฀—฀d=0฀to฀255 HL฀=฀HL฀&฀DE IX฀=฀IX฀&฀DE IY฀=฀IY฀&฀DE if฀(HL฀!=฀0)฀HL฀=฀1,฀set฀flags฀to฀match฀HL if฀(IX฀!=฀0)฀IX฀=฀1 if฀(IY฀!=฀0)฀IY฀=฀1 IX฀=฀IX฀–฀1 IY฀=฀IY฀–฀1 ss฀=฀ss฀-฀1฀(ss=฀BC,฀DE,฀HL,฀SP) IX฀=฀IX฀+฀1 IY฀=฀IY฀+฀1 ss฀=฀ss฀+฀1฀(ss=฀BC,฀DE,฀HL,฀SP) HL:BC฀=฀BC฀*฀DE,฀signed฀32฀bit฀result.฀DE฀ unchanged HL฀=฀HL฀|฀DE฀—฀bitwise฀or IX฀=฀IX฀|฀DE IY฀=฀IY฀|฀DE {CY,DE}฀=฀{DE,CY}฀—฀left฀shift฀with฀CF {DE,CY}฀=฀{CY,DE} {HL,CY}฀=฀{CY,HL} {IX,CY}฀=฀{CY,IX} {IY,CY}฀=฀{CY,IY} HL=HL-ss-CY฀(cout฀if฀(ss-CY)>hl)

Chapter฀6 6.1.10฀8-bit฀Bit฀Set,฀Reset฀and฀Test The฀bit฀manipulation฀instructions฀are฀used฀to฀set฀or฀reset฀bits,฀as฀well฀as฀test฀the฀status฀of฀bits.฀ Consider฀an฀8-bit฀parallel฀port฀that฀uses฀certain฀bits฀for฀outputs;฀in฀order฀to฀change฀the฀status฀ of฀just฀one฀bit,฀the฀contents฀of฀the฀port฀can฀be฀read,฀one฀bit฀changed฀using฀these฀instructions,฀ and฀the฀contents฀can฀be฀written฀back฀to฀the฀port.฀This฀sequence฀is฀often฀referred฀to฀as฀a฀readmodify-write฀operation. Instructions฀in฀this฀group,฀shown฀in฀Table฀6.11,฀allow฀bits฀to฀be฀manipulated฀in฀certain฀8-bit฀ registers,฀or฀memory฀locations฀pointed฀to฀by฀HL,฀IX฀and฀IY฀register฀pairs. Table฀6.11:฀8-bit฀bit฀set,฀reset฀and฀test. Instruction BIT฀b,(HL) BIT฀b,(IX+d)) BIT฀b,(IY+d)) BIT฀b,r RES฀b,(HL) RES฀b,(IX+d) RES฀b,(IY+d) RES฀b,r SET฀b,(HL) SET฀b,(IX+d) SET฀b,(IY+d) SET฀b,r

clk 7 10 10 4 10 13 13 4 10 13 13 4

A f f f f

I s s s d d d

r b b b r

S -

Z * * * * -

V -

C -

Operation (HL)฀&฀bit (IX+d)฀&฀bit (IY+d)฀&฀bit r฀&฀bit (HL)฀=฀(HL)฀&฀~bit (IX+d)฀=฀(IX+d)฀&฀~bit (IY+d)฀=฀(IY+d)฀&฀~bit r฀=฀r฀&฀~bit (HL)฀=฀(HL)฀|฀bit (IX+d)฀=฀(IX+d)฀|฀bit (IY+d)฀=฀(IY+d)฀|฀bit r฀=฀r฀|฀bit

In฀the฀above฀table,฀“bit”฀is฀a฀value฀between฀0฀and฀7,฀where฀7฀is฀the฀most฀significant฀bit. The฀following฀instructions฀illustrate฀various฀bit฀set,฀reset฀and฀test฀operations: #define฀ON_BIT฀ set฀ON_BIT,฀a฀

4฀ ฀

;฀bit฀to฀turn฀motor฀on ;฀turn฀motor฀on฀

bit฀7,(hl)฀;฀test฀busy฀bit res฀7,(hl)฀;฀clear฀busy฀bit฀

6.1.11฀8-bit฀Increment฀and฀Decrement฀ These฀instructions฀operate฀on฀locations฀pointed฀to฀by฀HL,฀IX฀or฀IY฀register฀pairs,฀as฀well฀ as฀directly฀on฀the฀CPU’s฀8-bit฀registers.฀An฀increment฀or฀decrement฀is฀always฀quicker฀than฀ an฀“add฀one฀to”฀or฀“subtract฀one฀from,”฀respectively,฀because฀no฀immediate฀data฀has฀to฀be฀ loaded.฀Unlike฀the฀16-bit฀INC/DEC฀instructions,฀their฀8-bit฀counterparts฀do฀affect฀the฀status฀ flags.฀The฀8-bit฀increment฀and฀decrement฀instructions฀are฀shown฀in฀Table฀6.12.

170

Introduction฀to฀Rabbit฀Assembly฀Language Table฀6.12:฀8-bit฀increment฀and฀decrement฀instructions. Instruction DEC฀(HL) DEC฀(IX+d) DEC฀(IY+d) DEC฀r INC฀(HL) INC฀(IX+d) INC฀(IY+d) INC฀r

clk 8 12 12 2 8 12 12 2

A f f f fr f f f fr

I b b b

S * * * * * * * *

b b b

Z * * * * * * * *

V V V V V V V V V

C -

Operation (HL)฀=฀(HL)฀-฀1 (IX+d)฀=฀(IX+d)฀-1 (IY+d)฀=฀(IY+d)฀-1 r฀=฀r฀–฀1 (HL)฀=฀(HL)฀+฀1 (IX+d)฀=฀(IX+d)฀+฀1 (IY+d)฀=฀(IY+d)฀+฀1 r฀=฀r฀+฀1

6.1.12฀8-bit฀Fast฀Accumulator฀Operations฀ These฀instructions฀are฀considered฀“fast”฀because฀they฀do฀not฀load฀immediate฀data,฀or฀use฀ pointers฀to฀point฀to฀operands—everything฀happens฀right฀in฀the฀accumulator.฀Table฀6.13฀lists฀ instructions฀in฀this฀group. Table฀6.13:฀8-bit฀fast฀accumulator฀operations. Instruction CPL NEG RLA RLCA RRA RRCA

clk 2 4 2 2 2 2

A r fr fr fr fr fr

I

S * -

Z * -

V V -

C * * * * *

Operation A฀=฀~A A฀=฀0฀–฀A {CY,A}฀=฀{A,CY} A฀=฀{A[6,0],A[7]};฀CY฀=฀A[7] {A,CY}฀=฀{CY,A} A฀=฀{A[0],A[7,1]};฀CY฀=฀A[0]

6.1.13฀8-bit฀Shifts฀and฀Rotates฀ These฀instructions฀allow฀bits฀to฀be฀shifted฀or฀rotated฀in฀and฀out฀of฀a฀register฀or฀a฀memory฀location.฀Before฀we฀proceed,฀it฀is฀important฀to฀have฀a฀distinction฀between฀shifts฀and฀rotates: ฀

฀

A฀“shift”฀happens฀when฀bits฀in฀a฀register฀or฀memory฀location฀are฀shifted฀one฀place,฀ left฀or฀right.฀The฀bit฀shifted฀out฀goes฀to฀the฀carry฀flag,฀while฀a฀0฀gets฀shifted฀in.฀There฀ is฀a฀special฀case฀(SRA)฀ where฀the฀most฀significant฀ RL, RLA SLA 0 bit฀remains฀unchanged. C C A฀“rotate”฀is฀different฀from฀ a฀shift฀because฀bits฀are฀ shifted฀out฀through฀the฀carry฀ flag,฀and฀then฀shifted฀back฀in฀ (see฀Figure฀6.2).฀There฀are฀ special฀cases฀about฀which฀bit฀ gets฀shifted฀in,฀the฀carry฀bit฀ or฀the฀bit฀being฀shifted฀out.

RLC, RLCA

C

C

SRA

RR, RRA

C

RRC, RRCA

C

SRL

0

Figure฀6.2:฀8-bit฀shifts฀and฀rotates. 171

C

Chapter฀6 There฀are฀many฀reasons฀a฀programmer฀may฀want฀to฀shift฀bits: ฀ A฀“left฀shift”฀has฀the฀same฀affect฀as฀a฀“multiply-by-two”฀while฀a฀“right฀shift”฀performs฀a฀“divide-by-two.”฀This฀is฀likely฀the฀quickest฀way฀to฀achieve฀these฀results.฀A฀ program฀may฀call฀for฀receiving฀a฀byte,฀reversing฀the฀order,฀and฀transmitting฀out฀the฀ result.฀Once฀a฀byte฀has฀been฀received฀and฀verified฀for฀correctness,฀it฀is฀very฀easy฀to฀ shift฀its฀bits฀out฀through฀the฀carry฀flag,฀and฀rotate฀those฀bits฀into฀a฀separate฀register฀or฀ memory฀location.฀This฀would฀effectively฀place฀bits฀in฀reverse฀order฀into฀the฀destination.฀In฀the฀case฀of฀division฀by฀two,฀there฀is฀a฀loss฀of฀resolution,฀but฀when฀performing฀ integer฀math,฀a฀reduced฀precision฀is฀inevitable.฀ ฀ Programmers฀sometimes฀use฀parallel฀port฀pins฀to฀transmit฀serial฀data,฀especially฀in฀ cases฀where฀the฀serial฀ports฀are฀already฀being฀used.฀When฀transmitting฀data฀serially,฀ a฀programmer฀will฀typically฀store฀the฀byte฀to฀be฀transmitted฀in฀a฀parallel฀register,฀and฀ will฀shift฀each฀bit฀that฀has฀to฀be฀transmitted.฀Similarly,฀on฀the฀receiving฀end,฀each฀ received฀bit฀will฀be฀shifted฀in฀so฀that฀a฀complete฀byte฀can฀be฀put฀together.฀Using฀port฀ pins฀as฀serial฀ports฀in฀this฀manner฀is฀often฀referred฀to฀as฀“bit-banging”฀a฀serial฀port. Table฀6.14฀lists฀instructions฀in฀the฀“shift฀and฀rotate”฀group. Table฀6.14:฀8-bit฀shifts฀and฀rotates. Instruction RL฀(HL) RL฀(IX+d) RL฀(IY+d) RL฀r RLC฀(HL)

clk 10 13 13 4 10

A F F F fr F

I b b b b

S * * * * *

Z * * * * *

V L L L L L

C * * * * *

RLC฀(IX+d)

13 F

b

*

*

L

*

RLC฀(IY+d)

13 F

b

*

*

L

*

RLC฀r RR฀(HL) RR฀(IX+d) RR฀(IY+d) RR฀r RRC฀(HL)

4 10 13 13 4 10

b

* * * * * *

* * * * * *

L L L L L L

* * * * * *

RRC฀(IX+d)

13 F

b

*

*

L

*

RRC฀(IY+d)

13 F

b

*

*

L

*

RRC฀r SLA฀(HL) SLA฀(IX+d) SLA฀(IY+d) SLA฀r

4 10 13 13 4

b b b

* * * * *

* * * * *

L L L L L

* * * * *

fr F F F fr F

fr F F F fr

b b b

172

Operation {CY,(HL)}฀=฀{(HL),CY} {CY,(IX+d)}฀=฀{(IX+d),CY} {CY,(IY+d)}฀=฀{(IY+d),CY} {CY,r}฀=฀{r,CY} (HL)฀=฀{(HL)[6,0],(HL)[7]};฀CY฀=฀(HL)[7] (IX+d)฀=฀{(IX+d)[6,0],฀(IX+d)[7]};฀ CY฀=฀(IX+d)[7] (IY+d)฀=฀{(IY+d)[6,0],฀(IY+d)[7]};฀ CY฀=฀(IY+d)[7] r฀=฀{r[6,0],r[7]};฀CY฀=฀r[7] {(HL),CY}฀=฀{CY,(HL)} {(IX+d),CY}฀=฀{CY,(IX+d)} {(IY+d),CY}฀=฀{CY,(IY+d)} {r,CY}฀=฀{CY,r} (HL)฀=฀{(HL)[0],(HL)[7,1]};฀CY฀=฀(HL)[0] (IX+d)฀=฀{(IX+d)[0],฀(IX+d)[7,1]};฀ CY฀=฀(IX+d)[0] (IY+d)฀=฀{(IY+d)[0],(IY+d)[7,1]};฀ CY฀=฀(IY+d)[0] r฀=฀{r[0],r[7,1]};฀CY฀=฀r[0] (HL)฀=฀{(HL)[6,0],0};฀CY฀=(HL)[7] (IX+d)฀=฀{(IX+d)[6,0],0};฀CY฀=฀(IX+d)[7] (IY+d)฀=฀{(IY+d)[6,0],0};฀CY฀=฀(IY+d)[7] r฀=฀{r[6,0],0};฀CY฀=฀r[7]

Introduction฀to฀Rabbit฀Assembly฀Language Table฀6.14:฀8-bit฀shifts฀and฀rotates฀(continued). Instruction SRA฀(HL)

clk A 10 F

I b

S *

Z *

V L

C *

SRA฀(IX+d)

13 F

b

*

*

L

*

SRA฀(IY+d)

13 F

b

*

*

L

*

SRA฀r SRL฀(HL) SRL฀(IX+d) SRL฀(IY+d) SRL฀r

4 10 13 13 4

b b b

* * * * *

* * * * *

L L L L L

* * * * *

fr F F F fr

Operation (HL)฀=฀{(HL)[7],(HL)[7,1]};฀CY฀=฀(HL)[0] (IX+d)฀=฀{(IX+d)[7],฀(IX+d)[7,1]};฀CY฀=฀ (IX+d)[0] (IY+d)฀=฀{(IY+d)[7],฀(IY+d)[7,1]};฀ CY฀=฀(IY+d)[0] r฀=฀{r[7],r[7,1]};฀CY฀=฀r[0] (HL)฀=฀{0,(HL)[7,1]};฀CY฀=฀(HL)[0] (IX+d)฀=฀{0,(IX+d)[7,1]};฀CY฀=฀(IX+d)[0] (IY+d)฀=฀{0,(IY+d)[7,1]};฀CY฀=฀(IY+d)[0] r฀=฀{0,r[7,1]};฀CY฀=฀r[0]

6.1.14฀Instruction฀Prefixes฀ The฀Rabbit฀has฀two฀I/O฀spaces:฀internal฀I/O฀registers฀and฀external฀I/O฀registers.฀Instruction฀ prefixes฀IOI฀and฀IOE฀can฀be฀used฀to฀generate฀code฀that฀accesses฀internal฀or฀external฀I/O฀registers฀instead฀of฀accessing฀memory.฀With฀these฀prefixes,฀any฀16-bit฀memory฀address฀is฀decoded฀ as฀an฀internal฀or฀external฀I/O฀address: ฀

The฀IOI฀instruction฀prefix฀causes฀the฀following฀instruction฀to฀access฀an฀internal฀I/O฀ port฀instead฀of฀memory.฀Since฀the฀internal฀I/O฀peripherals฀occupy฀the฀first฀256฀bytes฀ of฀the฀internal฀I/O฀space,฀the฀upper฀byte฀of฀the฀16-bit฀memory฀address฀is฀ignored฀and฀ the฀lower฀byte฀is฀used฀to฀access฀the฀internal฀I/O฀port.

The฀following฀instruction฀provides฀as฀an฀example฀of฀the฀IOI฀prefix.฀The฀use฀of฀shadow฀registers฀is฀defined฀in฀Section฀6.3.7. ฀ xor฀ ฀ ld฀ ioi฀ld฀

a฀ ฀ ฀ (PEFRShadow),฀a฀ (PEFR),a฀ ฀

;฀port฀e฀bit฀1..7฀inputs,฀0฀output ;฀update฀shadow฀register฀first ;฀set฀up฀function฀register

฀ ld฀ ฀ ld฀ ioi฀ld฀

a,0x01 (PEDDRShadow),฀a฀ (PEDDR),a฀ ฀

;฀update฀shadow฀register฀first ;฀set฀up฀data฀direction฀register

฀

The฀IOE฀instruction฀prefix฀causes฀the฀following฀instruction฀to฀access฀an฀external฀I/O฀ port฀instead฀of฀memory.฀Since฀external฀I/O฀peripherals฀can฀be฀mapped฀within฀a฀64K฀ space,฀the฀full฀16-bit฀address฀is฀used฀to฀access฀external฀I/O฀ports.฀By฀default,฀writes฀are฀ inhibited฀for฀external฀I/O฀operations฀and฀fifteen฀wait฀states฀are฀added฀for฀I/O฀accesses.

The฀following฀instruction฀provides฀as฀an฀example฀of฀the฀ALTD฀prefix: #define฀

EXT_ADDRESS฀ 0xFC00฀;฀external฀address

฀ ฀

ld฀ ld฀

฀ ioe฀

฀ ฀

hl,฀EXT_ADDRESS฀ ;฀set฀up฀pointer (hl),a฀฀ ;฀put฀data฀value฀externally

173

Chapter฀6 ฀

The฀ALTD฀prefix฀causes฀the฀immediately฀following฀instruction฀to฀affect฀the฀alternate฀ flags,฀or฀use฀the฀alternate฀registers฀for฀the฀destination฀of฀the฀data,฀or฀both.฀Using฀this฀ instruction฀allows฀the฀programmer฀to฀access฀the฀alternate฀register฀set฀or฀alternate฀flags฀ directly,฀without฀the฀need฀to฀exchange฀all฀the฀registers.฀See฀Section฀6.1.6฀for฀more฀ details฀on฀the฀alternate฀register฀set.

The฀following฀instruction฀provides฀as฀an฀example฀of฀the฀ALTD฀prefix: ฀

altd฀

ex฀

de,hl฀ ฀

;฀de฀=฀index฀(was฀in฀hl’)

Instruction฀prefixes฀are฀shown฀in฀Table฀6.15. Table฀6.15:฀Instruction฀prefixes. Instruction ALTD IOE IOI

clk 2 2 2

A

I

S -

Z -

V -

C -

Operation alternate฀register฀destination฀for฀next฀Instruction I/O฀external฀prefix I/O฀internal฀prefix

6.1.15฀Block฀Move฀Instructions฀ These฀instructions,฀listed฀in฀Table฀6.16,฀are฀used฀to฀rapidly฀copy฀(or฀move)฀blocks฀of฀data฀ from฀one฀part฀of฀memory฀to฀another.฀They฀work฀in฀the฀following฀manner: ฀ BC฀is฀set฀up฀as฀the฀counter—number฀of฀bytes฀to฀copy ฀ HL฀is฀set฀up฀to฀point฀to฀the฀source฀block ฀ DE฀is฀set฀up฀to฀point฀to฀the฀destination฀block The฀instructions฀differ฀from฀each฀other฀in฀the฀following฀manner: LDD: ฀ ฀ ฀ ฀

Set฀up฀DE฀to฀point฀to฀destination฀address Set฀up฀HL฀to฀point฀to฀source฀address One฀byte฀of฀data฀is฀copied฀from฀(HL)฀to฀(DE) both฀HL฀and฀DE฀are฀decremented

LDDR: ฀ Set฀up฀DE฀to฀point฀to฀highest฀address฀of฀destination฀block ฀ Set฀up฀HL฀to฀point฀to฀highest฀address฀of฀source฀block ฀ A฀block฀of฀data฀is฀copied฀from฀(HL)฀to฀(DE);฀block฀size฀is฀specified฀by฀BC ฀ both฀HL฀and฀DE฀are฀decremented฀during฀the฀operation LDI:

฀

Set฀up฀DE฀to฀point฀to฀destination฀address Set฀up฀HL฀to฀point฀to฀source฀address ฀ One฀byte฀of฀data฀is฀copied฀from฀(HL)฀to฀(DE) ฀ both฀HL฀and฀DE฀are฀incremented

฀

174

Introduction฀to฀Rabbit฀Assembly฀Language LDIR: ฀ ฀ ฀ ฀

Set฀up฀DE฀to฀point฀to฀lowest฀address฀of฀destination฀block Set฀up฀HL฀to฀point฀to฀lowest฀address฀of฀source฀block A฀block฀of฀data฀is฀copied฀from฀(HL)฀to฀(DE);฀block฀size฀is฀specified฀by฀BC both฀HL฀and฀DE฀are฀incremented฀during฀the฀operation Table฀6.16:฀Block฀move฀instructions.

Instruction LDD LDDR LDI LDIR

clk A 10 6+7i

I D D

S -

Z -

V * *

C -

10

D

-

-

*

-

6+7i

D

-

-

*

-

Operation (DE)฀=฀(HL);฀BC฀=฀BC-1;฀DE฀=฀DE-1;฀HL฀=฀HL-1 if฀{BC฀!=฀0}฀repeat: (DE)฀=฀(HL);฀BC฀=฀BC-1;฀DE฀=฀DE+1;฀HL฀=฀ HL+1 if฀{BC฀!=฀0}฀repeat:

6.1.16฀Control฀Instructions—Jumps฀and฀Calls฀ Instructions฀in฀this฀group฀affect฀program฀flow,฀and฀can฀be฀divided฀into฀the฀following฀sections: ฀

Unconditional฀Jumps:฀These฀instructions฀start฀execution฀from฀another฀point฀in฀code.฀ As฀Table฀6.17A฀indicates,฀these฀instructions฀start฀program฀execution฀from฀a฀fixed฀ address฀(PC=mn),฀or฀from฀an฀address฀pointed฀to฀the฀HL,฀IX฀or฀IY฀register฀pair.฀The฀ LJP฀instruction฀is฀special฀because฀it฀allows฀a฀jump฀to฀be฀made฀to฀a฀computed฀address฀ in฀XMEM. Table฀6.17A:฀Unconditional฀jump฀instructions.

Instruction JP฀mn JP฀(HL) JP฀(IX) JP฀(IY)

Clk A 7 4 6 6

JR฀e LJP฀xpc,mn ฀

I

S -

Z -

V -

C -

5

-

-

-

-

10

-

-

-

-

Operation PC฀=฀mn PC฀=฀HL PC฀=฀IX PC฀=฀IY PC฀=฀PC฀+฀e฀(if฀e==0฀next฀seq฀instruction฀ is฀executed) XPC=xpc;฀PC฀=฀mn

Conditional฀Jumps:฀These฀instructions,฀shown฀in฀Table฀6.17B,฀cause฀a฀jump฀if฀one฀of฀ the฀given฀conditions฀is฀met.฀ Table฀6.17B:฀Conditional฀jump฀instructions. Instruction JP฀f,mn JR฀cc,e

Clk

A 7 5

I

S -

Z -

175

V -

C -

Operation if฀{f}฀PC฀=฀mn if฀{cc}฀PC฀=฀PC฀+฀e

Chapter฀6 ฀

Subroutine฀Calls฀can฀be฀made฀to฀a฀16-bit฀address฀using฀the฀CALL฀instruction,฀฀ or฀to฀a฀computed฀address฀in฀XMEM,฀using฀the฀LCALL฀instruction,฀as฀shown฀in฀฀ Table฀6.17C. Table฀6.17C:฀Subroutine฀calls. Instruction

฀

Clk A

I

S

Z

V

C

CALL฀mn

12

-

-

-

-

LCALL฀xpc,mn

19

-

-

-

-

Operation (SP-1)฀=฀PCH;฀(SP-2)฀=฀PCL; PC฀=฀mn;฀SP฀=฀SP-2 (SP-1)฀=฀XPC;฀(SP-2)฀=฀PCH;฀ (SP-3)฀=฀PCL;฀XPC=xpc; PC฀=฀mn;฀SP฀=฀(SP-3)

Returns฀from฀subroutines฀can฀be฀made฀unconditionally,฀using฀the฀RET฀instruction,฀ or฀utilize฀one฀of฀several฀flag฀conditions฀with฀the฀RET฀f฀instruction.฀LRET฀is฀used฀ to฀return฀from฀subroutines฀stored฀in฀XMEM฀space.฀These฀instructions฀are฀shown฀in฀ Table฀6.17D. Table฀6.17D:฀Return฀instructions.

Instruction RET RET฀f

Clk 8 8/2

LRET

A

I

S -

Z -

V -

C -

-

-

-

-

-

13

Operation PCL฀=฀(SP);฀PCH฀=฀(SP+1);฀SP฀=฀SP+2 If฀{f}฀PCL฀=฀(SP);฀PCH฀=฀(SP+1);฀SP฀=฀SP+2 PCL฀=฀(SP);฀PCH฀=฀(SP+1);฀XPC฀=฀(SP+2);฀ SP฀=฀SP+3

Interrupt฀Service฀Routines฀(ISRs)฀present฀a฀special฀case:฀ ฀

Unlike฀other฀processors,฀the฀Rabbit฀uses฀a฀“Return”฀(RET)฀instruction฀rather฀than฀ a฀“Return฀from฀Interrupt”฀(RETI)฀instruction฀to฀return฀from฀an฀interrupt.฀The฀RETI฀ instruction฀is฀shown฀in฀Table฀6.17E฀and฀does฀not฀seem฀to฀serve฀any฀useful฀purpose.฀

In฀most฀cases,฀an฀interrupt฀service฀routine฀should฀be฀terminated฀as฀follows: ฀ ฀

ipres฀ ฀ ret฀ ฀

;฀restore฀the฀interrupt฀priority ;฀return฀to฀interrupted฀code

A฀programmer฀does฀not฀usually฀have฀to฀do฀anything฀special฀to฀use฀this฀instruction;฀just฀putting฀a฀“RET”฀at฀the฀end฀of฀the฀interrupt฀service฀routine฀is฀sufficient.฀Chapter฀7฀provides฀many฀ examples฀of฀this฀instruction. Table฀6.17E:฀Return฀from฀interrupt฀instruction. Instruction

clk

RETI

12

A

I

S

Z

V

C

-

-

-

-

176

Operation IP฀=฀(SP);฀PCL฀=฀(SP+1); PCH฀=฀(SP+2);฀SP฀=฀SP+3

Introduction฀to฀Rabbit฀Assembly฀Language ฀

฀ ฀

Decrement฀and฀Jump฀if฀Non-Zero,฀shown฀in฀Table฀6.17F.฀This฀instruction฀is฀especially฀handy฀for฀implementing฀loops.฀Any฀block฀of฀code฀preceding฀this฀instruction฀can฀be฀ repeated,฀provided฀that฀it฀meets฀the฀following฀conditions: −฀ The฀block฀of฀code฀can฀execute฀a฀maximum฀of฀256฀times฀(if฀B=0฀at฀the฀start฀of฀the฀ count). −฀ The฀entire฀code฀block฀that฀has฀to฀be฀repeated฀must฀fit฀within฀this฀distance฀limitation.฀This฀is฀because฀the฀instruction฀performs฀a฀relative฀jump฀to฀within฀a฀256-byte฀ distance฀from฀the฀current฀instruction, Table฀6.17F:฀Decrement฀and฀jump฀instruction. Instruction DJNZ฀j

฀

Clk 5

A R

I

S -

Z -

V -

C -

Operation B฀=฀B-1;฀if฀{B฀!=฀0}฀PC฀=฀PC฀+฀j

Reset฀(RST)฀instruction.฀This฀instruction,฀shown฀in฀Table฀6.17G,฀pushes฀the฀current฀ Program฀Counter฀onto฀the฀stack฀and฀then฀starts฀program฀execution฀from฀vector฀v฀in฀ the฀interrupt฀table.฀Storing฀the฀current฀program฀counter฀on฀the฀stack฀tells฀the฀interrupt฀ where฀to฀resume฀operation฀after฀servicing฀the฀interrupt.฀

As฀shown฀in฀the฀table,฀RST฀10,฀RST฀18,฀RST฀20,฀RST฀28,฀and฀RST฀38฀are฀available.฀The฀ RST฀0x28฀instruction฀is฀special฀because฀it฀transfers฀program฀execution฀to฀the฀Dynamic฀C฀ debug฀kernel.฀This฀is฀the฀only฀reset฀instruction฀used฀by฀Dynamic฀C. Table฀6.17G:฀Reset฀instruction. Instruction

Clk

RST฀v

10

A

I

S

Z

V

C

-

-

-

-

177

Operation (SP-1)฀=฀PCH;฀(SP-2)฀=฀PCL;฀ SP฀=฀SP฀-฀2;฀PC฀=฀{R,v) v฀=฀10,18,20,28,38฀only

Chapter฀6 6.1.17฀Miscellaneous฀Instructions฀ The฀following฀instructions฀do฀not฀fit฀in฀the฀above฀groups;฀a฀brief฀description฀of฀each฀instruction฀is฀shown฀in฀Table฀6.18: Table฀6.18:฀Miscellaneous฀instruction. Instruction CCF IPSET฀0 IPSET฀1 IPSET฀2 IPSET฀3 IPRES LD฀A,EIR LD฀A,IIR LD฀A,XPC LD฀EIR,A LD฀IIR,A LD฀XPC,A NOP POP฀IP PUSH฀IP SCF

clk 2 4 4 4 4 4 4 4 4 4 4 4 2 7 9 2

A f

fr fr r

F

I

S * * -

Z * * -

V -

C * 1

Operation CF฀=฀~CF IP฀=฀{IP[5:0],฀00} IP฀=฀{IP[5:0],฀01} IP฀=฀{IP[5:0],฀10} IP฀=฀{IP[5:0],฀11} IP฀=฀{IP[1:0],฀IP[7:2]} A฀=฀EIR A฀=฀IIR A฀=฀MMU EIR฀=฀A IIR฀=฀A XPC฀=฀A No฀Operation IP฀=฀(SP);฀SP฀=฀SP+1 (SP-1)฀=฀IP;฀SP฀=฀SP-1 CF฀=฀1

6.2฀Some฀Unique฀Rabbit฀Instructions Although฀the฀Rabbit฀3000฀instruction฀set฀is฀derived฀from฀Z-180฀instructions,฀there฀are฀some฀ significant฀differences.฀The฀Rabbit฀3000฀uses฀some฀special฀instructions฀in฀areas฀of฀I/O฀and฀ memory฀access;฀these฀instructions฀do฀not฀exist฀for฀the฀Z-180.฀Moreover,฀the฀Rabbit฀3000฀has฀ dropped฀some฀Z-180฀instructions;฀programmers฀can฀refer฀to฀the฀Rabbit฀3000฀microprocessor฀ user฀manual฀for฀details฀on฀these฀instructions. 6.2.1฀Instructions฀That฀Make฀Some฀Operations฀More฀Efficient ฀ The฀DJNZ฀instruction,฀covered฀in฀Table฀6.17F,฀is฀useful฀for฀repeatedly฀executing฀a฀ block฀of฀code. ฀ The฀LDD,฀LDDR,฀LDI฀and฀LDIR฀instructions,฀covered฀in฀Table฀6.16,฀are฀useful฀for฀ moving฀a฀block฀of฀memory. ฀ The฀“LCALL฀xpc,mn”฀instruction,฀shown฀in฀Table฀6.17C,฀allows฀a฀jump฀to฀a฀computed฀address฀in฀XMEM.฀ ฀ The฀16-bit฀multiply฀instruction,฀shown฀in฀Table฀6.10,฀is฀useful฀for฀generating฀a฀signed฀ 32-bit฀multiply฀out฀of฀the฀BC฀and฀DE฀registers. 6.2.2฀Instructions฀That฀Bypass฀the฀Memory฀Management฀Unit Chapter฀2฀covered฀the฀Memory฀Management฀Unit฀(MMU)฀in฀some฀detail.฀Instructions฀in฀ Table฀6.19฀bypass฀the฀MMU฀and฀access฀the฀entire฀physical฀memory฀space฀directly.฀While฀ 178

Introduction฀to฀Rabbit฀Assembly฀Language the฀instructions฀use฀16-bit฀register฀pairs,฀they฀target฀a฀20-bit฀address฀space.฀The฀four฀most฀ significant฀bits฀of฀the฀20-bit฀address฀are฀derived฀from฀the฀four฀least฀significant฀bits฀of฀the฀ accumulator฀(bits฀3฀though฀0).฀ Table฀6.19:฀Instructions฀that฀bypass฀the฀MMU. Instruction

clk

LDP฀(HL),HL

A

I

S

Z

12

-

-

LDP฀(IX),HL

12

-

-

LDP฀(IY),HL

12

-

-

LDP฀HL,(HL)

10

-

-

LDP฀HL,(IX)

10

-

-

LDP฀HL,(IY)

10

-

-

LDP฀(mn),HL

15

-

-

LDP฀(mn),IX

15

-

-

LDP฀(mn),IY

15

-

-

LDP฀HL,(mn)

13

-

-

LDP฀IX,(mn)

13

-

-

LDP฀IY,(mn)

13

-

-

V C Operation (HL)฀=฀L;฀(HL+1)฀=฀H. - (Adr[19:16]฀=฀A[3:0]) (IX)฀=฀L;฀(IX+1)฀=฀H. - (Adr[19:16]฀=฀A[3:0]) (IY)฀=฀L;฀(IY+1)฀=฀H. - (Adr[19:16]฀=฀A[3:0]) L฀=฀(HL);฀H฀=฀(HL+1). - (Adr[19:16]฀=฀A[3:0]) L฀=฀(IX);฀H฀=฀(IX+1). - (Adr[19:16]฀=฀A[3:0]) L฀=฀(IY);฀H฀=฀(IY+1). - (Adr[19:16]฀=฀A[3:0]) (mn)฀=฀L;฀(mn+1)฀=฀H. - (Adr[19:16]฀=฀A[3:0]) (mn)฀=฀IXL;฀(mn+1)฀=฀IXH. - (Adr[19:16]฀=฀A[3:0]) (mn)฀=฀IYL;฀(mn+1)฀=฀IYH. - (Adr[19:16]฀=฀A[3:0]) L฀=฀(mn);฀H฀=฀(mn+1). - (Adr[19:16]฀=฀A[3:0]) IXL฀=฀(mn);฀IXH฀=฀(mn+1). - (Adr[19:16]฀=฀A[3:0]) IYL฀=฀(mn);฀IYH฀=฀(mn+1). - (Adr[19:16]฀=฀A[3:0])

179

Chapter฀6

6.3฀Starting฀to฀Code฀Assembly฀with฀Dynamic฀C Dynamic฀C฀provides฀a฀lot฀of฀freedom฀to฀the฀assembly฀language฀programmer.฀There฀are฀various฀ways฀of฀incorporating฀assembly฀language฀in฀Dynamic฀C: ฀

Assembly฀language฀statements฀can฀be฀embedded฀in฀C฀functions Entire฀functions฀can฀be฀written฀in฀assembly฀language ฀ C฀statements฀may฀be฀embedded฀in฀assembly฀code ฀ C-language฀variables฀may฀be฀accessed฀by฀the฀assembly฀code ฀ Parameters฀may฀be฀passed฀between฀C-language฀code฀and฀assembly฀code.฀This฀is฀ described฀in฀Section฀6.4.

฀

6.3.1฀Inline฀Assembly฀Code Dynamic฀C฀allows฀the฀use฀of฀assembly฀language฀anywhere฀in฀the฀code;฀the฀programmer฀simply฀has฀to฀put฀“#asm”฀before฀and฀“#endasm”฀after฀the฀block฀of฀assembly฀language฀code฀that฀ needs฀to฀be฀assembled.฀Program฀6.1฀illustrates฀this฀concept฀with฀a฀simple฀example: Program฀6.1:฀Inline฀assembly. printf฀(“\nWarning฀Light฀on\n”);฀

//฀operating฀in฀C

#asm ฀ ld฀ ฀ set฀ ฀ ld฀ ioi฀ld฀ #endasm

฀ ฀ ฀ ฀

;฀get฀contents฀of฀shadow฀register ;฀turn฀LED฀on ;฀update฀shadow฀register ;฀write฀data฀to฀port฀g





a,฀(PGDRShadow)฀ 6,฀a฀ ฀ ฀ (PGDRShadow),฀a฀ (PGDR),฀a฀ ฀

get_status();฀







//฀back฀to฀C

6.3.2฀“C”฀Wrappers A฀block฀of฀assembly฀code฀can฀be฀encapsulated฀within฀a฀“C”฀wrapper.฀This฀is฀typically฀done฀ when฀a฀“C”฀function฀is฀defined฀in฀order฀to฀contain฀assembly฀code.฀Parameters฀to฀the฀“C”฀ function฀can฀be฀passed฀into฀assembly฀code฀using฀pointers.฀Function฀initPort฀in฀Program฀ 6.11฀provides฀an฀example฀of฀a฀C฀wrapper฀encapsulating฀assembly฀code. A฀C฀function฀containing฀embedded฀assembly฀code฀may฀use฀a฀C฀return฀statement฀to฀return฀฀ a฀value. 6.3.3฀Standalone฀Assembly Programmers฀can฀write฀entire฀functions฀in฀assembly฀and฀benefit฀from฀the฀speed฀of฀machine฀ language.฀Assembly฀functions฀can฀be฀declared฀just฀as฀C฀functions฀and฀can฀receive฀and฀return฀ parameters.฀As฀an฀example,฀Program฀6.2฀shows฀how฀an฀assembly฀function฀is฀used฀for฀fast฀ table฀lookup฀and฀the฀value฀is฀returned฀to฀the฀C฀calling฀program.฀

180

Introduction฀to฀Rabbit฀Assembly฀Language Program฀6.2:฀Code฀fragment฀showing฀standalone฀assembly฀function.฀ int฀CRC_lookup฀(int฀value); main() {฀ ฀฀฀int฀i,j;฀ ฀฀฀i=1;฀ ฀฀฀j=฀CRC_lookup(i);฀ } #asm฀ CRC_lookup::฀฀ ฀฀฀...฀ ฀฀฀ld฀hl,a฀฀฀฀฀฀฀฀฀฀//฀The฀return฀value฀is฀put฀in฀HL฀ ret฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀//฀just฀before฀the฀function฀returns฀ #endasm

A฀few฀things฀to฀note฀about฀standalone฀assembly฀functions: ฀

A฀function฀prototype฀needs฀to฀be฀declared฀before฀the฀function฀declaration.฀Ideally,฀ the฀function฀prototype฀is฀declared฀towards฀the฀beginning฀of฀the฀program.฀Defining฀a฀ function฀prototype฀is฀important฀so฀that฀functions฀that฀have฀not฀been฀compiled฀may฀be฀ called,฀and฀that฀the฀compiler฀can฀do฀typechecking฀on฀the฀function’s฀parameters. ฀ Note฀the฀use฀of฀double฀colons฀in฀declaring฀function฀CRC_lookup—this฀is฀Dynamic฀ C’s฀standard฀method฀for฀calling฀standalone฀assembly฀routines฀from฀C.฀The฀assembly฀function฀is฀identified฀by฀the฀double฀colon฀“::”฀and฀the฀double฀colon฀construction฀ declares฀that฀the฀label฀is฀of฀global฀scope. ฀ CPU฀registers฀used฀by฀the฀assembly฀program฀do฀not฀have฀to฀be฀saved฀upon฀entering฀ the฀function฀or฀restored฀upon฀exit.฀However,฀as฀we฀will฀see฀in฀Chapter฀7,฀if฀the฀standalone฀assembly฀function฀is฀an฀Interrupt฀Service฀Routine฀(ISR),฀this฀rule฀is฀reversed฀and฀ CPU฀registers฀used฀by฀the฀ISR฀have฀to฀be฀saved฀upon฀entry฀and฀restored฀upon฀exit.

181

Chapter฀6 6.3.4฀Embedding฀C฀Statements฀in฀Assembly Dynamic฀C฀allows฀embedding฀C฀statements฀in฀assembly.฀The฀C฀statements฀have฀to฀be฀preceded฀by฀a฀“c”฀indicating฀that฀they฀are฀to฀be฀compiled฀and฀not฀assembled.฀The฀following฀code฀ fragment฀uses฀embedded฀C฀code฀to฀perform฀some฀math฀in฀some฀assembly฀code: Program฀6.3:฀Code฀fragment฀showing฀embedded฀C฀statements฀in฀assembly. int฀result฀(int฀x); #define฀DIVIDEND฀57 #define฀DIVISOR฀19 int฀temp1,฀temp2,฀temp3; /////////////////////////////////////////////////////////// //฀int฀result฀(int฀x) //฀the฀assembly฀subroutine฀returns฀x฀+฀(DIVIDEND฀/฀DIVISOR) /////////////////////////////////////////////////////////// #asm฀debug฀root result:: ฀ //฀HL฀should฀contain฀parameter฀x ฀



ex฀de,hl฀





;฀store฀x฀in฀DE

฀฀ ฀ ฀ ฀ ฀



push฀





;฀save฀needed฀registers

฀ ฀ ฀

c฀temp1฀=฀DIVIDEND฀/฀DIVISOR; c฀temp2฀=฀(DIVIDEND฀*฀4)฀/฀(DIVISOR฀-฀3); c฀temp3฀=฀temp1฀+฀temp2;

฀฀฀฀฀฀

pop฀

฀฀฀฀฀฀฀฀฀฀ld฀

de฀

de฀



a,฀(temp3)฀



;฀restore฀needed฀registers



;฀load฀global฀variable

฀ ฀ ld฀h,฀0฀ ฀฀฀฀฀฀฀฀฀฀ld฀l,฀a ฀฀฀฀฀฀฀฀฀฀add฀hl,฀de





;฀add฀x฀and฀temp3

฀ ฀ #endasm





;฀return฀result฀in฀HL฀and฀DE

ret฀



main() { int฀x,y,z; ฀฀for฀(x=0;฀x//

The฀current฀Time฀is ::

The฀current฀temperature฀is



The฀final฀item฀to฀examine฀is฀how฀the฀C฀program฀loads฀the฀HTML฀page฀into฀flash.฀This฀is฀ described฀in฀detail฀in฀the฀next฀section.฀The฀project฀will฀load฀three฀different฀web฀pages,฀one฀ requiring฀authenticated฀access. 9.12.4฀A฀RabbitWeb฀Dynamic฀Web฀Server฀with฀Authentication This฀project฀picks฀up฀from฀Project฀4฀in฀Chapter฀8.฀In฀that฀chapter,฀we฀implemented฀an฀analog฀sensor฀task฀that฀would฀take฀periodic฀temperature฀readings,฀compare฀them฀against฀preset฀ thresholds,฀and฀trigger฀alarms฀accordingly.฀We฀will฀now฀use฀RabbitWeb฀to฀enable฀that฀project฀ for฀a฀web฀interface. The฀program฀converts฀readings฀from฀a฀thermistor฀into฀temperature฀values฀and฀compares฀them฀ to฀four฀temperature฀thresholds:฀Upper฀Critical,฀Upper฀Non-Critical,฀Lower฀Non-Critical,฀and฀ Lower฀Critical.฀It฀allows฀for฀scanning฀of฀each฀threshold,฀and฀shows฀on฀the฀screen฀whether฀one฀ of฀the฀threshold฀alarms฀has฀been฀triggered. Before฀coding฀such฀a฀project,฀it฀is฀important฀to฀consider฀various฀elements฀of฀the฀user฀ interface.฀We฀need฀to฀know฀which฀variables฀the฀program฀will฀expose฀to฀the฀user,฀and฀which฀ variables฀the฀user฀will฀be฀able฀to฀change฀and฀how฀(for฀example,฀whether฀it฀uses฀check฀boxes,฀ radio฀buttons,฀etc.).฀We฀decided฀to฀implement฀three฀web฀pages฀for฀this฀project: ฀

The฀“Monitor”฀page฀displays฀the฀date,฀time,฀current฀temperature,฀and฀the฀status฀of฀ each฀sensor.฀It฀needs฀read-only฀access฀to฀variables฀that฀deal฀with฀date฀and฀time,฀the฀ four฀temperature฀thresholds,฀whether฀scanning฀has฀been฀enabled฀for฀each฀threshold,฀ and฀if฀a฀threshold฀has฀generated฀an฀alarm.฀This฀page฀has฀been฀implemented฀with฀file฀ sensor2_monitor.zhtml. ฀ The฀“Set฀Time”฀page฀allows฀us฀to฀change฀the฀date฀and฀time฀on฀the฀core฀module.฀For฀ additional฀security,฀this฀page฀is฀protected฀with฀first฀time฀authentication.฀The฀user฀ name฀is฀“task”฀and฀the฀password฀is฀“mangler.”฀Once฀the฀user฀has฀authenticated,฀the฀ protected฀web฀page฀is฀available฀until฀the฀browser฀is฀closed฀and฀a฀new฀browser฀session฀is฀initiated.฀This฀page฀has฀been฀implemented฀with฀file฀sensor2_settime.zhtml.฀ RabbitWeb฀performs฀additional฀verification฀on฀the฀entered฀date฀and฀time฀values. ฀ The฀“Set฀Temperature฀Thresholds”฀page฀allows฀us฀to฀change฀values฀for฀each฀threshold฀ and฀whether฀the฀system฀will฀scan฀for฀that฀threshold.฀This฀page฀has฀been฀implemented฀ 379

Chapter฀9 with฀file฀sensor2_settemp.zhtml.฀฀ RabbitWeb฀does฀some฀rule฀checking฀on฀the฀ entered฀temperature฀thresholds. The฀main฀code฀module฀for฀this฀program฀is฀฀ sensorweb2.c,฀which฀has฀a฀scanning฀routine฀very฀ similar฀to฀the฀one฀presented฀in฀Chapter฀8.฀We฀will฀ describe฀the฀code฀here฀not฀from฀the฀perspective฀of฀the฀ scanning฀routine,฀but฀how฀the฀sensor฀routine฀is฀integrated฀into฀RabbitWeb.฀More฀importantly,฀we฀will฀examine฀ how฀RabbitWeb฀provides฀for฀a฀two-way฀interface฀ between฀the฀C฀program฀and฀associated฀ZHTML฀pages. The฀“Monitor”฀page฀is฀shown฀in฀Figure฀9.15. Let฀us฀first฀examine฀how฀the฀web฀pages฀are฀described฀at฀ compile฀time฀in฀the฀C฀program.฀Since฀we฀will฀use฀two฀ images฀to฀indicate฀whether฀an฀alarm฀condition฀exists฀or฀ not,฀we฀will฀load฀the฀images฀into฀flash฀as฀well:

Figure฀9.15A:฀Screenshot฀of฀the฀ “monitor”฀page. //฀sensorweb2.c

#ximport฀“sensor2_monitor.zhtml”฀ #ximport฀“sensor2_settime.zhtml”฀ #ximport฀“sensor2_settemp.zhtml”฀ #ximport฀“ledset.gif”฀฀฀฀฀฀฀ ฀ #ximport฀“ledreset.gif”฀฀฀฀฀฀ ฀

monitor_zhtml settime_zhtml settemp_zhtml ledon_gif ledoff_gif

Next,฀we฀will฀associate฀the฀#ximported฀files฀with฀the฀web฀server: SSPEC_RESOURCETABLE_START ฀ SSPEC_RESOURCE_XMEMFILE(“/”,฀monitor_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/index.zhtml”,฀monitor_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/settemp.zhtml”,฀settemp_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/admin/index.zhtml”,฀settime_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/ledon.gif”,฀ledon_gif), ฀ SSPEC_RESOURCE_XMEMFILE(“/ledoff.gif”,฀ledoff_gif) SSPEC_RESOURCETABLE_END

380

Networking We฀created฀a฀special฀“admin”฀directory฀above฀which฀stores฀files฀that฀require฀authentication.฀ RabbitWeb฀uses฀the฀concept฀of฀groups,฀where฀each฀group฀can฀be฀granted฀separate฀access฀ privileges฀through฀authentication.฀We฀also฀defined฀an฀“admin”฀group฀for฀authenticated฀access฀ to฀the฀Rabbit฀clock: #web_groups฀admin sspec_addrule(“/admin”,฀“Admin”,฀admin,฀admin,฀SERVER_ANY,฀SERVER_AUTH_ BASIC,฀NULL);

The฀following฀two฀lines฀create฀an฀“admin”฀user฀and฀add฀it฀to฀the฀admin฀group.฀The฀user฀ID฀is฀ “task”฀and฀the฀password฀is฀“mangler”: userid฀=฀sauth_adduser(“task”,฀“mangler”,฀SERVER_ANY); sauth_setusermask(userid,฀admin,฀NULL);

We฀will฀now฀examine฀how฀program฀variables฀are฀set฀up฀so฀that฀their฀values฀can฀be฀displayed฀ on฀and฀altered฀by฀the฀web฀pages฀described฀above.฀For฀instance,฀we฀know฀that฀none฀of฀the฀web฀ pages฀should฀alter฀the฀value฀of฀the฀temperature฀variable,฀so฀we฀should฀declare฀it฀“read฀only”฀ by฀all฀groups: float฀web_temp; #web฀web_temp฀groups=all(ro)

As฀described฀in฀Section฀9.12.2,฀RabbitWeb฀allows฀us฀to฀use฀“guard฀expressions”฀to฀watch฀ against฀invalid฀user฀data.฀We฀have฀implemented฀the฀following฀rules฀with฀guard฀expressions: ฀

Time฀and฀Date฀entry:฀the฀rules฀for฀time฀and฀date฀are฀well฀known…฀these฀include฀the฀ treatment฀of฀the฀number฀of฀days฀in฀each฀month,฀consideration฀for฀leap฀years,฀as฀well฀ the฀range฀for฀the฀year฀the฀system฀operates฀in.฀For฀example,฀it฀is฀easy฀to฀set฀up฀a฀guard฀ expression฀to฀enforce฀that฀minutes฀and฀seconds฀are฀within฀the฀“0฀to฀59”฀range: #web฀time.minute฀($time.minute฀>=฀0฀&&฀$time.minute฀=฀0฀&&฀$time.second฀

Using฀the฀error()฀function,฀once฀RabbitWeb฀has฀flagged฀a฀user฀entry฀as฀an฀error,฀it฀does฀not฀ update฀the฀associated฀variables. Another฀interesting฀aspect฀of฀RabbitWeb฀is฀how฀vales฀are฀passed฀back฀from฀the฀HTML฀page฀ to฀the฀C฀program.฀As฀an฀example,฀Let฀us฀look฀at฀the฀radio฀buttons฀in฀sensor2_settemp.zhtml฀ that฀enable฀or฀disable฀scanning฀of฀the฀upper฀critical฀temperature฀threshold: ฀CHECKED ฀CHECKED ฀฀฀฀>

These฀lines฀treat฀the฀radio฀button฀in฀its฀checked฀and฀unchecked฀state,฀respectively.฀The฀web฀ variable,฀$alarm.Scan_UC,฀is฀passed฀back฀to฀the฀C฀program฀in฀runtime.฀In฀Section฀9.13.6,฀we฀ will฀look฀at฀how฀RabbitWeb฀treats฀an฀array฀of฀checkboxes.

382

Networking Figure฀9.15B฀shows฀a฀snapshot฀of฀the฀“Set฀ Time”฀page. Note฀the฀list฀box฀for฀the฀“month”฀field;฀the฀ selection฀is฀defined฀in฀the฀C฀code฀and฀the฀ HTML฀follows฀these฀values. The฀temperature฀thresholds฀are฀defined฀as฀ shown฀in฀Figure฀9.15C.฀Entered฀values฀for฀ thresholds฀are฀validated฀and฀the฀appropriate฀variables฀are฀updated.฀Similarly,฀the฀ scanning฀function฀for฀each฀sensor฀can฀be฀ altered฀and฀the฀values฀passed฀back฀to฀the฀฀ C฀program. In฀keeping฀with฀our฀practice฀of฀recommending฀improvements฀to฀projects,฀let฀us฀ offer฀some฀suggestions฀for฀enhancing฀this฀ program: ฀

฀

฀

฀

Figure฀9.15B:฀Screenshot฀of฀the฀“set฀time”฀page.

Instead฀of฀hard฀coding฀usernames฀ and฀passwords,฀define฀defaults฀for฀ these฀values฀and฀allow฀the฀user฀to฀ change฀them.฀ The฀system฀can฀allow฀the฀user฀ to฀change฀positive฀and฀negative฀ hysteresis฀and฀sensing฀time฀for฀the฀ analog฀sensor.฀The฀code฀presented฀ here฀uses฀hard฀coded฀values.฀ The฀system฀can฀send฀out฀an฀email฀ for฀each฀temperature฀threshold฀ that฀triggers฀an฀alarm. The฀HTML฀could฀be฀reworked฀ and฀optimized฀for฀“surfing”฀from฀a฀ cell฀phone฀browser. Figure฀9.15C:฀Screenshot฀of฀the฀“set฀ temperature฀thresholds”฀page.

383

Chapter฀9

9.13฀Project฀6:฀Building฀an฀Ethernet-Connected฀Sprinkler฀฀ Controller In฀this฀section,฀we฀bring฀together฀all฀of฀the฀tools฀discussed฀thus฀far฀to฀create฀a฀fully฀web฀ enabled฀embedded฀system.฀We฀have฀selected฀a฀sprinkler฀controller฀as฀the฀project,฀both฀ because฀the฀scope฀of฀the฀project฀is฀manageable฀in฀the฀context฀of฀a฀chapter,฀and฀because฀this฀ particular฀control฀problem฀will฀benefit฀greatly฀from฀an฀intuitive฀browser-based฀interface. 9.13.1฀Using฀Best฀Practices฀for฀Design:฀Defining฀the฀Requirements When฀defining฀a฀project,฀the฀first฀step฀is฀identifying฀requirements.฀There฀are฀a฀number฀of฀formal฀approaches฀to฀capturing฀requirements.฀Most฀have฀fancy฀academic฀names฀like฀Structured฀ Analysis฀Employing฀Hierarchical฀Decomposition,฀but฀“common฀sense”฀will฀be฀our฀approach. When฀generating฀requirements,฀it฀is฀important฀to฀keep฀in฀mind฀the฀difference฀between฀a฀feature฀ and฀a฀requirement.฀For฀example,฀a฀gold-plated฀welded-chain฀steering-wheel฀is฀a฀feature.฀A฀ requirement฀that฀might฀imply฀a฀need฀for฀such฀a฀device฀is฀“the฀vehicle฀interior฀must฀look฀cool.” Another฀example฀is฀that฀a฀keypad฀and฀LCD฀module฀as฀a฀user฀interface฀is฀a฀feature.฀A฀requirement฀that฀might฀imply฀a฀need฀for฀an฀LCD฀and฀keypad฀is฀“a฀field฀service฀technician฀must฀be฀ able฀to฀communicate฀with฀the฀system.” The฀distinction฀between฀the฀requirement฀and฀the฀features฀that฀fulfill฀the฀requirement฀are฀ important.฀Consider฀the฀requirement฀“a฀field฀service฀technician฀must฀be฀able฀to฀communicate฀ with฀the฀system.”฀Several฀methods฀exist฀that฀might฀fulfill฀the฀requirement฀besides฀just฀slapping฀an฀LCD฀and฀keypad฀on฀the฀front฀of฀the฀box.฀For฀example฀a฀serial฀port฀or฀USB฀port฀might฀ do฀the฀trick฀if฀we฀know฀that฀field฀service฀technicians฀routinely฀carry฀laptops฀or฀PalmPilots™.฀ Here฀are฀the฀system฀level฀requirements฀we฀settled฀on฀for฀the฀sprinkler฀controller 1.฀ Must฀be฀able฀to฀be฀controlled฀remotely฀via฀a฀PC฀or฀laptop฀using฀commonly฀available฀ software.฀No฀additional฀software฀need฀be฀installed฀on฀the฀PC฀or฀laptop. 2.฀ Must฀be฀capable฀of฀replacing฀the฀existing฀controller฀in฀Bob฀Perrin’s฀front฀yard. 3.฀ Must฀be฀capable฀of฀deciding฀to฀reduce฀water฀when฀it฀is฀raining.฀We฀will฀refer฀to฀this฀ as฀the฀“rainy฀day”฀rule. 4.฀ Must฀be฀capable฀of฀determining฀if฀a฀day฀is฀particularly฀hot฀and฀decide฀if฀watering฀ beyond฀the฀“normally฀scheduled฀water฀application”฀is฀needed.฀We฀will฀refer฀to฀this฀as฀ the฀“hot฀day”฀rule. 5.฀ Must฀have฀an฀intuitive฀user฀interface.฀Strictly฀speaking,฀this฀is฀a฀difficult฀one,฀since฀ measuring฀and฀testing฀this฀requirement฀can฀be฀tricky. 6.฀ The฀system฀must฀be฀inexpensive. Now฀that฀we฀have฀the฀system฀level฀requirements,฀we฀can฀generate฀hardware฀and฀software฀ features฀that฀fulfill฀the฀system฀requirements.

384

Networking Features

1.฀ (to฀Requirement฀1)฀To฀be฀“remotely”฀controllable,฀the฀system฀will฀be฀designed฀with฀ an฀Ethernet฀interface. 2.฀ (to฀Requirement฀1)฀To฀be฀configurable฀using฀commonly฀available฀software,฀the฀system฀will฀be฀designed฀to฀support฀a฀remote฀browser฀interface฀(HTTP฀interface). 3.฀ (to฀Requirement฀2)฀Bob’s฀current฀system฀has฀four฀24฀VAC฀valves฀that฀can฀be฀actuated฀ one฀at฀a฀time.฀The฀new฀system฀will฀be฀able฀to฀drive฀the฀same฀valves. 4.฀ (to฀Requirements฀2,฀6)฀Bob฀has฀two฀Opto฀22฀solid฀state฀relays฀and฀two฀twelve฀volt฀ electromechanical฀relays฀in฀his฀junk฀pile—to฀reduce฀cost,฀the฀four฀channels฀will฀be฀ implemented฀with฀these฀devices฀as฀the฀valve฀drivers. 5.฀ (to฀Requirements฀3,6)฀To฀determine฀if฀the฀day฀is฀wet,฀the฀system฀will฀include฀a฀moisture฀probe.฀Bob฀has฀a฀Wescor฀S-460฀leaf฀wetness฀probe฀in฀his฀collection฀of฀surplus฀ sensors฀that฀will฀allow฀the฀system฀to฀determine฀if฀it฀is฀raining฀or฀has฀recently฀rained.฀ The฀S-460฀will฀require฀an฀analog฀input.฀To฀reduce฀system฀cost,฀the฀system฀will฀use฀ the฀Wescor฀probe. 6.฀ (to฀Requirements฀4,฀6)฀To฀determine฀if฀a฀day฀is฀“hot,”฀the฀system฀will฀need฀to฀measure฀ temperature.฀Bob฀has฀a฀PT100฀temperature฀probe฀in฀his฀collection฀of฀junk,฀to฀reduce฀ system฀cost,฀the฀system฀will฀use฀a฀PT100฀temperature฀probe.฀This฀will฀require฀an฀ analog฀input.฀We฀will฀use฀an฀RCM3400฀because฀it฀has฀analog฀inputs. 7.฀ (to฀Requirement฀5)฀We฀will฀have฀to฀concoct฀a฀simple฀web-based฀interface฀for฀setting฀a฀ watering฀schedule. 8.฀ (to฀Requirements฀3,฀4,฀5)฀The฀software฀that฀reads฀the฀sensors฀will฀need฀to฀have฀some฀ pre-determined฀thresholds฀for฀what฀is฀“rainy฀/฀wet”฀and฀what฀is฀“hot.”฀The฀user฀will฀ not฀be฀able฀to฀change฀these฀hard฀coded฀thresholds—as฀that฀flexibility฀is฀just฀one฀more฀ level฀of฀complication฀for฀the฀user. Our฀six฀requirements฀generated฀eight฀features.฀Once฀we฀have฀the฀feature฀list,฀we฀can฀go฀about฀ the฀business฀of฀defining฀the฀system. 9.13.2฀Using฀Best฀Practices฀for฀Design:฀Sprinkler฀Controller฀Hardware In฀California,฀automated฀sprinkler฀systems฀are฀ commonplace.฀Not฀so฀for฀other฀parts฀of฀the฀ world.฀Before฀delving฀into฀the฀nitty-gritty฀of฀the฀ electronics,฀let’s฀zoom฀back฀and฀look฀at฀Bob’s฀ existing฀irrigation฀system. Figure฀9-16A฀shows฀the฀four฀valves฀in฀Bob฀ Perrin’s฀front฀yard.฀The฀web฀enabled฀sprinkler฀ controller฀will฀ultimately฀allow฀easy฀control฀of฀ these฀devices. Each฀valve฀is฀actuated฀by฀a฀24฀VAC฀solenoid.฀ Figure฀9-16A฀shows฀the฀wires฀that฀carry฀current฀ Figure฀9.16A:฀Four฀electrically฀operated฀ to฀the฀solenoids.฀The฀wires฀enter฀the฀garage฀ sprinkler฀valves฀control฀drippers,฀sprayers฀and฀ wall฀behind฀the฀sprinkler฀valves. two฀circuits฀of฀pop-up฀sprinklers. 385

Chapter฀9 A฀4-station฀commercially฀available฀sprinkler฀controller฀ directs฀the฀behavior฀of฀the฀four฀valves฀shown฀in฀Figure฀฀ 9-16A.฀Figure฀9.16B฀shows฀the฀controller฀and฀its฀cryptic฀ user฀interface.฀This฀controller฀is฀mounted฀inside฀the฀garage฀ on฀the฀wall฀shown฀behind฀the฀valves฀in฀Figure฀9.16A.฀As฀is฀ common฀with฀new฀homes฀in฀California,฀this฀setup฀came฀with฀ the฀house. The฀user฀interface฀shown฀in฀Figure฀9.16B฀is฀infuriating.฀ Getting฀a฀program฀into฀this฀little฀box฀is฀tantamount฀to฀booting฀an฀old฀PDP฀with฀toggle฀switches.฀The฀knob฀and฀buttons฀ shown฀in฀Figure฀9.16B฀have฀names฀that฀might฀as฀well฀be฀in฀ Cyrillic.฀What฀is฀the฀ABC฀button฀for?฀What฀is฀the฀difference฀ between฀the฀“Manual฀Station(s)”฀and฀the฀“Manual฀Cycle”฀ setting?

OFF

AUTO

DATE/TIME

PRESET SCHEDULES JAN

SET MONTH

SET DAY

ABC

1 SET TIME

PM

+ − ABC

DAY CYCLE

TEST



WATER DAY PRGM START TIME

MANUAL CYCLE MANUAL STATION(S)

WATER BUDGET

%

STATION RUN TIME



Figure฀9.16B:฀A฀commercially฀ available฀sprinkler฀controller.

While฀the฀hardware฀shown฀in฀this฀project฀will฀not฀be฀in฀a฀nice฀little฀molded฀plastic฀box,฀the฀ user฀interface฀will฀allow฀the฀user฀to฀set฀up฀a฀watering฀schedule฀intuitively฀over฀Ethernet฀(and฀ with฀a฀trip฀to฀the฀computer฀store,฀wireless฀Ethernet).฀Once฀the฀sprinkler฀controller฀works,฀we฀ can฀always฀spend฀the฀time฀and฀money฀to฀spiff฀up฀the฀package.฀“Pretty฀packaging”฀was฀not฀a฀ requirement฀set฀forth฀in฀the฀previous฀section. One฀of฀the฀requirements฀for฀this฀project฀was฀“cost฀effectiveness.”฀Most฀of฀the฀components฀for฀ this฀project฀were฀salvaged฀from฀the฀techno-junk฀piles฀in฀the฀authors’฀garages. Figure฀9.16C฀shows฀the฀accumulation฀of฀hardware฀assembled฀for฀the฀project.฀The฀only฀purchased฀component฀ended฀up฀being฀the฀test฀valve฀(bottom฀center).฀The฀moisture฀sensor฀(top฀ right)฀and฀the฀PT100฀resistive฀ temperature฀device฀(RTD)฀ (bottom฀right)฀were฀left฀over฀ from฀previous฀projects.฀ Since฀the฀valves฀in฀Figure฀ 9.16A฀were฀unavailable฀for฀ bench฀top฀experimentation฀(an฀ addition฀constraint฀placed฀on฀ the฀project฀by฀Bob’s฀wife),฀the฀ authors฀were฀forced฀to฀purchase฀ an฀inexpensive฀24-volt฀valve.฀ The฀new฀valve฀served฀as฀a฀test฀ load. For฀convenience,฀banana฀plugs฀ were฀installed฀on฀the฀valve฀ solenoid฀to฀allow฀quick฀connection฀of฀the฀RCM3400-based฀ sprinkler฀controller.

Figure฀9.16C:฀The฀RCM3400฀development฀board฀allows฀ quick฀integration฀of฀a฀wide฀variety฀of฀sensors฀and฀actuators. 386

Networking The฀Rabbit฀Semiconductor฀RCM3400฀development฀board฀was฀selected฀for฀the฀sprinkler฀ controller฀project.฀The฀RCM3400฀sports฀eight฀analog฀inputs฀(we฀need฀two),฀an฀Ethernet฀port,฀ plenty฀of฀digital฀I/O฀(we฀need฀to฀control฀four฀valves),฀and฀a฀real-time฀battery-backed฀clock. Figure฀9.16D฀shows฀a฀closer฀view฀ of฀the฀actual฀RCM3400฀development฀board.฀To฀interface฀a฀ moisture฀sensor,฀temperature฀probe฀ and฀high-current฀driver฀channels,฀ we฀only฀needed฀to฀add฀a฀few฀parts฀ in฀the฀prototyping฀area. The฀valve฀drivers฀were฀derived฀ from฀the฀discussion฀of฀BJT฀drivers฀ and฀relays฀in฀Chapter฀5.฀Figure฀ 9.16E฀shows฀the฀schematic฀for฀the฀ valve฀drivers.฀Notice฀a฀metal฀oxide฀ varistor฀(MOV)฀placed฀on฀all฀relay฀ contacts฀to฀protect฀the฀devices฀ against฀the฀inductive฀fly-back฀from฀ the฀sprinkler฀valve฀solenoids— Figure฀9.16D:฀The฀RCM3400฀development฀board฀is฀a฀ more฀on฀this฀latter. feature฀rich,฀easily฀configurable฀tool. The฀two฀electromechanical฀relays฀ required฀a฀fly-back฀diode฀across฀their฀coil.฀Without฀this฀diode,฀the฀NPN฀transistors฀driving฀the฀ relays฀would฀be฀short฀lived.฀The฀1N914฀diodes฀were฀soldered฀directly฀across฀the฀relay฀coil’s฀ contacts฀and฀cannot฀be฀seen฀in฀Figure฀9.16D. The฀solid-state฀relays฀(SSRs)฀do฀not฀generate฀any฀inductive฀fly-back฀and฀their฀driver฀circuit฀ contains฀no฀fly-back฀diode. The฀RCM3400฀development฀board฀has฀many฀features฀to฀show฀off฀the฀RCM3400’s฀ability฀to฀ read฀switches,฀drive฀LEDs,฀talk฀to฀an฀LCD฀and฀Keypad,฀interface฀to฀Ethernet฀and฀read฀analog฀ signals.฀The฀downside฀of฀having฀all฀this฀demo฀hardware฀is฀that฀most฀of฀the฀RCM3400฀digital฀ I/O฀lines฀are฀pre-assigned฀to฀I/O฀features฀on฀the฀development฀board.฀ Our฀sprinkler฀controller฀must฀control฀four฀valves.฀We฀had฀to฀choose฀four฀I/O฀lines฀on฀the฀RCM3400฀ development฀board฀that฀were฀either฀unused฀or฀could฀be฀modified฀for฀use฀in฀our฀design. After฀looking฀at฀the฀RCM3400฀development฀board’s฀schematic,฀we฀decided฀that฀Port฀D฀ bits฀4฀to฀7฀were฀going฀to฀drive฀our฀solenoids.฀First,฀we฀had฀to฀disconnect฀the฀devices฀on฀the฀ RCM3400฀development฀board฀that฀were฀wired฀to฀Port฀D’s฀upper฀nibble.฀ PD6฀and฀PD7฀were฀dedicated฀to฀driving฀two฀active฀low฀LEDs฀(DS1฀and฀DS2)฀on฀the฀ RCM3400฀development฀board.฀We฀disconnected฀these฀LEDs฀by฀simply฀removing฀their฀current฀limiting฀resistors฀R47฀and฀R57.฀This฀freed฀up฀PD6฀and฀PD7฀for฀controlling฀valves. PD4฀and฀PD5฀were฀used฀by฀the฀RCM3400฀development฀board฀for฀switch฀input.฀There฀were฀ two฀pull-up฀resistors฀(R43฀and฀R44)฀connected฀to฀these฀bits.฀We฀removed฀these฀resistors฀from฀ the฀development฀board. 387

Chapter฀9 + 12 V +5 V GRN

1N914

1K

CHANNEL 1

24 VAC Sprinkler Valve

MOV 40 Vrms

220 Ω

PN2222A

Rabbit PD4 10K

+ 12 V +5 V GRN

1N914

1K

CHANNEL 2

24 VAC Sprinkler Valve

MOV 40 Vrms

220 Ω

PN2222A

Rabbit PD5 10K

+ 5V +5 V

3 GRN

1

+ SSR OPTO 22 240D10

4

2

-

220 Ω

1K

CHANNEL 3

24 VAC Sprinkler Valve

MOV 40 Vrms

PN2222A

Rabbit PD6 10K

+ 5V +5 V

3 GRN

1

+ SSR OPTO 22 240D10

4

-

2

220 Ω

CHANNEL 4

4.7 K

24 VAC Sprinkler Valve

MOV 40 Vrms

PN2222A

Rabbit PD7 100 K 24 VAC XFMR

Figure฀9.16E:฀Two฀SSRs฀and฀two฀electromechanical฀relays฀drive฀four฀valves.฀The฀software฀ driving฀the฀channels฀cannot฀tell฀the฀difference฀between฀the฀two฀types฀of฀relays.

The฀fact฀that฀PD4฀and฀PD5฀were฀used฀as฀inputs฀on฀the฀development฀board฀and฀we฀wanted฀to฀ use฀them฀as฀outputs฀meant฀that฀we฀had฀to฀change฀the฀initialization฀settings.฀We฀could฀either฀ modify฀Rcm34xx.LIB฀in฀Dynamic฀C’s฀/LIB/RCM3400฀directory฀or฀just฀re-initialize฀the฀port฀ after฀the฀brdInit()฀function฀is฀called.฀We฀chose฀to฀do฀the฀latter฀and฀reinitialized฀Port฀D฀ direction฀registers.฀For฀readers฀who฀would฀rather฀fix฀the฀library,฀we฀have฀included฀a฀modified฀ Rcm34xx.LIB฀library฀on฀the฀CD-ROM. While฀modifying฀Rcm34xx.LIB฀is฀expedient,฀if฀we฀ever฀want฀to฀use฀our฀Dynamic฀C฀installation฀ with฀another฀stock฀RCM3400฀development฀board,฀Rcm34xx.LIB฀will฀have฀to฀be฀restored. 388

Networking By฀default,฀the฀brdInit()฀function฀also฀initializes฀the฀output฀bits.฀We฀initialized฀PD4฀through฀ PD7฀to฀be฀LOW;฀the฀valve฀drivers฀shown฀in฀Figure฀9.16E฀are฀active฀high. The฀code฀fragment฀in฀Program฀9.13฀reinitializes฀the฀Port฀D฀direction฀registers: Program฀9.13:฀Setting฀up฀Port฀D฀to฀work฀with฀the฀valve฀drivers. //--------------------------------------------------------------------//฀Port฀D฀configuration // //฀PD0฀ RS485฀transmit฀enable฀ Output฀Low฀(disabled) //฀PD1฀ ADC฀Busy฀line฀฀ Input฀ ฀ Low฀(ADC฀device฀driven) //฀PD2฀ ADC฀device฀select฀ ฀ Output฀High฀(active฀low) //฀PD3฀ Not฀used฀ ฀ ฀ Input฀ ฀ Pulled-up฀core //฀PD4฀ Valve฀#1฀ ฀ ฀ Output฀Active฀High //฀PD5฀ Valve฀#2฀ ฀ ฀ Output฀Active฀High //฀PD6฀ Valve฀#3฀ ฀ ฀ Output฀Active฀High //฀PD7฀ Valve฀#4฀ ฀ ฀ Output฀Active฀High //--------------------------------------------------------------------WrPortI(PDCR,฀&PDCRShadow,฀0x00);฀ //clear฀all฀bits฀to฀pclk/2 WrPortI(PDFR,฀&PDFRShadow,฀0x00);฀ //clear฀all฀bits฀to฀normal฀function WrPortI(PDDCR,฀&PDDCRShadow,฀0x00);฀ //clear฀all฀bits฀to฀drive฀high฀and฀ //low WrPortI(PDDR,฀&PDDRShadow,฀0x0c);฀ //set฀bits฀3,2,0,฀clear฀bits฀ //7,6,5,4,1 WrPortI(PDDDR,฀&PDDDRShadow,฀0xf5);฀ //set฀bits฀7,6,5,4฀2,0฀to฀output฀and //clear฀3,1฀to฀input

In฀the฀brief฀time฀between฀power฀being฀applied฀to฀the฀RCM3400฀and฀the฀execution฀of฀the฀ brdInit()฀function฀there฀will฀be฀period฀while฀PD4..7฀will฀be฀HIGH-Z.฀To฀ensure฀that฀the฀ valves฀are฀kept฀OFF฀during฀this฀pre-firmware-initialization฀period,฀10฀K฀pull-down฀resistors฀ were฀added฀to฀the฀BJT฀drivers—shown฀in฀Figure฀9.16E. The฀24฀VAC฀transformer฀can฀only฀reliably฀power฀one฀sprinkler฀solenoid฀at฀a฀time.฀There฀is฀no฀ hardware฀assuring฀mutually฀exclusive฀solenoid฀actuation.฀For฀simplicity฀and฀cost฀we฀put฀the฀ burden฀of฀mutually฀exclusivity฀on฀the฀Rabbit฀firmware. Each฀driver฀channel฀has฀a฀MOV฀across฀the฀relay฀contacts฀to฀suppress฀the฀inductive฀flyback฀ caused฀by฀the฀sprinkler฀solenoid.฀ Chapter฀5฀discussed฀MOV฀operation฀and฀showed฀how฀MOVs฀can฀be฀used฀to฀suppress฀highvoltage฀transients฀on฀power฀and฀data฀communication฀lines.฀Inductive฀flyback฀is฀simply฀a฀form฀ of฀high-voltage฀transient฀and฀an฀MOV฀can฀be฀used฀effectively฀to฀protect฀a฀switch. In฀Chapter฀5฀we฀discussed฀RC฀snubber฀circuits฀for฀flyback฀suppression.฀A฀MOV฀is฀another฀ tool฀commonly฀used฀for฀controlling฀flyback฀in฀AC฀systems.฀Each฀approach฀has฀pros฀and฀cons.฀ Some฀systems฀use฀both฀an฀RC฀snubber฀and฀an฀MOV.฀The฀RC฀snubber฀can฀reduce฀the฀ harmonic฀content฀associated฀with฀the฀leading฀edge฀of฀the฀flyback฀while฀the฀MOV฀will฀clamp฀ prolonged฀voltage฀excursion฀to฀a฀safe฀level.฀This฀keeps฀the฀snubber’s฀capacitor฀reasonably฀ sized. 389

Chapter฀9 Since฀we฀were฀not฀overly฀worried฀about฀radiated฀emissions,฀we฀opted฀to฀forgo฀the฀RC฀snubber฀and฀simply฀use฀the฀MOV. To฀verify฀the฀effectiveness฀of฀the฀MOV฀for฀flyback฀suppression,฀a฀Tektronics฀digital฀storage฀ oscilloscope฀(DSO)฀was฀used฀to฀observe฀the฀operation฀of฀the฀relay฀drivers฀with฀and฀without฀an฀ MOV.฀We฀used฀an฀electromechanical฀relay฀channel฀for฀these฀experiments. Figures฀9.16F฀and฀9.16G฀show฀how฀the฀circuit฀operated฀without฀a฀MOV฀installed.฀ Channel฀two฀(CH2)฀probed฀the฀Rabbit฀port฀pin฀used฀to฀drive฀the฀BJT฀and฀subsequently฀the฀ valve฀control฀relay.฀CH2’s฀vertical฀sensitivity฀is฀shown฀as฀200฀mV/division.฀The฀channel฀had฀ a฀10x฀probe฀attached,฀so฀the฀actual฀sensitivity฀at฀the฀probe฀tip฀was฀2฀volts/division.฀When฀the฀ CH2฀trace฀is฀“high”฀the฀relay฀is฀actuated. The฀upper฀trace,฀Channel฀1฀(CH1),฀shows฀the฀voltage฀across฀the฀relay฀switch฀contacts.฀When฀ CH1฀is฀flat,฀the฀relay฀contacts฀are฀closed฀and฀the฀valve฀solenoid฀is฀actuated.฀CH1฀vertical฀ sensitivity฀was฀set฀to฀50฀volts/division.฀ Figure฀9.16F฀illustrates฀the฀large฀voltage฀spikes฀that฀are฀generated฀by฀inductive฀flyback.฀ The฀negative฀going฀spike฀that฀occurred฀when฀the฀solenoid฀was฀turned฀off฀is฀greater฀than฀฀ 300฀volts฀and฀went฀completely฀off฀the฀measurement฀scale. Figure฀9.16G฀illustrates฀that฀inductive฀flyback฀occurs฀both฀when฀the฀solenoid฀is฀energized฀and฀ when฀it฀is฀de-energized.

Figure฀9.16G:฀Flyback฀occurs฀when฀ turning฀on฀or฀turning฀off฀the฀solenoid.

Figure฀9.16F:฀Without฀a฀snubber฀inductive฀ flyback฀will฀generate฀high฀voltages.฀The฀ negative฀going฀spike฀on฀the฀left฀is฀greater฀ than฀300฀volts.

390

Networking Figure฀9.16H฀shows฀how฀the฀circuit฀behaves฀ after฀an฀MOV฀was฀installed฀across฀the฀relay฀ contacts฀(as฀shown฀in฀the฀final฀circuit฀configuration—see฀Figure฀9.16E).฀The฀inductive฀ spikes฀are฀clamped฀to฀±฀60฀volts.฀This฀is฀ within฀the฀contact฀rating฀of฀the฀relays.฀ Now฀that฀we฀can฀control฀all฀four฀solenoids,฀ we฀turn฀our฀attention฀to฀the฀sensors. The฀finished฀system฀will฀support฀a฀“Hot฀ Day”฀rule฀that,฀if฀enabled,฀will฀increase฀the฀ amount฀of฀water฀dispensed฀on฀hot฀days.฀This฀ means฀the฀Rabbit฀must฀be฀able฀to฀read฀the฀ outside฀temperature.

Figure฀9.16H:฀With฀a฀MOV฀installed,฀the฀ flyback฀excursions฀are฀clamped฀to฀±60฀volts.

The฀RCM3400฀development฀kit฀came฀with฀ a฀fragile฀little฀thermistor.฀This฀was฀briefly฀considered฀for฀the฀project฀and฀discarded฀as฀too฀ mechanically฀fragile.฀Besides,฀Rabbit฀has฀all฀the฀functions฀and฀sample฀programs฀worked฀out฀ for฀measuring฀thermistors.฀Where’s฀the฀challenge฀in฀that? We฀settled฀on฀using฀a฀PT100฀RTD.฀These฀devices฀are฀a฀platinum฀wire฀or฀film,฀usually฀mounted฀on฀a฀ceramic฀substrate฀and฀secured฀inside฀a฀protective฀stainless฀steel฀probe.฀The฀PT100฀is฀ shown฀in฀Figure฀9.16C฀(bottom฀right).฀PT100s฀are฀widely฀used฀in฀the฀biophysical,฀meteorological฀and฀industrial฀controls฀industries. PT100฀probes฀come฀in฀a฀few฀standard฀flavors.฀All฀PT100’s฀have฀a฀nominal฀100฀ohm฀resistance฀ at฀0฀°C.฀However,฀the฀different฀flavors฀of฀probe฀are฀built฀with฀different฀grades฀of฀platinum฀and฀ have฀different฀temperature฀coefficients. The฀PT100฀we฀had฀was฀surplus฀and฀we฀did฀not฀know฀the฀exact฀temperature฀coefficient฀of฀the฀ probe.฀This฀meant฀we฀needed฀to฀characterize฀the฀probe฀to฀get฀accurate฀measurements. If฀we฀must฀go฀to฀the฀trouble฀of฀characterizing฀a฀probe,฀we฀might฀as฀well฀just฀wait฀until฀we฀ have฀the฀entire฀measurement฀channel฀wired฀up฀and฀characterize฀the฀entire฀channel.฀The฀work฀ is฀the฀same.฀And฀whatever฀gain฀and฀offset฀errors฀may฀exist฀in฀the฀measurement฀channel฀will฀ be฀calibrated฀out. Figure฀9.16I฀shows฀the฀circuit฀we฀used฀to฀interface฀the฀PT100฀to฀the฀RCM3400. We฀experimented฀with฀values฀for฀the฀resistors฀in฀the฀bridge฀section฀and฀gain฀settings฀for฀the฀ instrumentation฀amplifier฀(the฀AD620).฀Precision฀designs฀often฀attempt฀to฀limit฀the฀current฀in฀ the฀PT100฀to฀50฀micro-amps฀or฀so.฀This฀requires฀the฀high฀and฀low฀side฀resistors฀in฀the฀bridge฀ to฀be฀about฀50K.฀It฀also฀requires฀gain฀on฀the฀order฀of฀103฀v/v. When฀we฀tried฀this,฀we฀noticed฀that฀not฀only฀did฀we฀not฀have฀the฀sensitivity฀that฀we฀wanted,฀ but฀also฀that฀high฀gain฀was฀amplifying฀unwanted฀noise.฀ We฀could฀have฀placed฀some฀capacitors฀in฀the฀circuit฀to฀reduce฀the฀unwanted฀noise,฀but฀increasing฀the฀signal฀to฀noise฀ratio฀and฀dropping฀the฀gain฀was฀a฀better฀solution. 391

Chapter฀9 +5 V

RCM 3400 Development Board

AIN 0

+5 V 220 Ω

+ REF

AD 620

4. 7 K 2 PLCS

220 Ω

PT100

100 Ω

− VREF

220 Ω

220 Ω

Figure฀9.16I:฀The฀PT100฀interface฀is฀simple,฀but฀effective.

In฀the฀end,฀we฀settled฀on฀the฀values฀shown฀in฀Figure฀9.16I.฀The฀stimulus฀current฀is฀about฀฀ 10฀mA.฀The฀AD620’s฀gain฀is฀set฀around฀22฀v/v.฀Even฀without฀filters,฀there฀is฀no฀significant฀ noise.฀ The฀10฀mA฀of฀stimulus฀current฀is฀three฀full฀orders฀of฀magnitude฀greater฀than฀the฀50฀microamps฀found฀in฀precision฀designs.฀The฀physical฀size฀of฀the฀PT100,฀and฀the฀imprecise฀application฀ (determining฀if฀it฀is฀“hot”฀outside)฀means฀the฀self฀heating฀associated฀with฀the฀10฀mA฀stimulus฀ current฀is฀negligible. When฀using฀amplifiers฀in฀a฀single฀supply฀configuration฀a฀designer฀generally฀must฀keep฀the฀ amplifier’s฀inputs฀away฀from฀the฀supplies฀by฀a฀volt฀or฀more.฀This฀of฀course฀varies฀from฀part฀to฀ part.฀The฀bridge฀configuration฀in฀Figure฀9.16I฀keeps฀the฀AD620’s฀inputs฀between฀2฀and฀3฀volts.฀ Some฀amplifiers฀do฀not฀have฀output฀stages฀that฀will฀drive฀fully฀rail-to-rail.฀And฀even฀if฀they฀ do,฀sometimes฀the฀linearity฀of฀the฀amplifier฀isn’t฀that฀great฀as฀the฀output฀approaches฀a฀rail. The฀AD620,฀like฀many฀instrumentation฀amplifiers,฀has฀a฀REF฀pin.฀This฀adds฀an฀offset฀to฀ the฀amplifier’s฀output.฀We฀tied฀this฀to฀the฀VREF฀output฀from฀the฀RCM3400.฀This฀allows฀the฀ amplifier฀to฀produce฀voltages฀between฀about฀2฀and฀4฀volts฀over฀the฀temperature฀range฀in฀ which฀we฀are฀interested. Once฀the฀hardware฀was฀completed,฀we฀used฀a฀small฀test฀program฀to฀sample฀the฀RCM3400฀ channel฀and฀display฀the฀results฀using฀printf().฀For฀brevity,฀we฀will฀omit฀this฀code,฀but฀it฀is฀ very฀similar฀to฀that฀used฀in฀both฀Chapters฀5฀and฀8฀for฀reading฀AIN0. With฀the฀program฀running,฀we฀immersed฀the฀PT100฀in฀ice฀water฀and฀recorded฀the฀voltage฀ reported฀by฀the฀RCM3400.฀We฀also฀used฀a฀commercially฀available฀digital฀thermometer฀to฀ measure฀the฀temperature฀of฀the฀ice฀water.฀We฀repeated฀the฀experiment฀with฀hot฀water.฀The฀ results฀are฀shown฀here. Environment฀to฀which฀ the฀PT100฀was฀exposed Ice฀Water Hot฀Water

RCM3400฀฀ reported฀voltage 2.115 4.015

392

Temperature฀reported฀by฀ digital฀thermometer 34.5฀°F 138.5฀°F

Networking From฀this฀information,฀we฀can฀derive฀a฀function฀that฀will฀accept฀the฀measured฀voltage฀and฀ return฀a฀temperature.฀Over฀the฀limited฀range฀of฀interest,฀the฀PT100฀is฀known฀to฀be฀fairly฀linear.฀We฀will฀thus฀derive฀a฀simple฀straight-line฀(linear)฀function฀of฀the฀form฀shown฀here. y฀=฀m ⋅ x + b f(x)฀=฀m ⋅ x + b temperature(volts)฀=฀sensitivity ⋅ volts + offset Using฀the฀numbers฀from฀the฀table฀above,฀we฀derived฀the฀following฀values, °F volt offset฀=฀ − 80.133° F sensitivity฀=฀54.2

This฀allows฀the฀RCM3400฀to฀compute฀the฀temperature฀of฀the฀PT100’s฀environment฀from฀the฀ voltage฀measured.฀ Now฀that฀we฀can฀measure฀temperature,฀we฀can฀turn฀our฀attention฀to฀the฀last฀environmental฀ condition฀that฀we฀had—a฀requirement฀to฀sense฀rain. Since฀the฀authors฀had฀a฀few฀surplus฀Wescor฀S-460฀Leaf฀Wetness฀sensors฀lying฀about,฀one฀of฀ these฀biophysical฀sensors฀was฀pressed฀into฀service฀as฀a฀rain฀detector.฀The฀top฀right฀section฀of฀ Figure฀9.16C฀shows฀the฀Wescor฀sensor. The฀Wescor฀S-460฀consists฀of฀a฀circuit฀board฀containing฀two฀gold฀plated฀electrodes฀in฀the฀ form฀of฀interdigitated฀combs฀covered฀with฀latex-based฀“secret฀sauce”฀paint.฀The฀sensor฀ requires฀about฀a฀4฀VAC฀stimulus฀voltage.฀The฀data฀loggers฀commonly฀used฀with฀this฀sensor฀ supply฀AC฀at฀300฀hertz. The฀requirement฀for฀an฀AC฀stimulus฀voltage฀is฀probably฀to฀reduce฀the฀likelihood฀of฀unwanted฀ plating฀of฀the฀electrode฀when฀a฀DC฀bias฀is฀applied฀over฀an฀extended฀period฀of฀time. The฀S-460฀datasheet฀says฀that฀when฀dry฀the฀sensor฀has฀an฀impedance฀of฀about฀3฀mega-ohms.฀ When฀water฀is฀on฀the฀surface,฀the฀impedance฀drops฀to฀about฀1฀K. The฀Rabbit฀3000฀has฀a฀PWM฀module฀that฀can฀be฀used฀to฀generate฀a฀square-wave฀with฀about฀ a฀50%฀duty฀cycle฀and฀a฀300฀Hz฀frequency.฀If฀we฀were฀concerned฀with฀building฀a฀precision฀ instrument,฀perhaps฀we฀might฀have฀considered฀building฀an฀oscillator฀with฀an฀output฀that฀was฀ more฀spectrally฀pure฀than฀a฀square-wave.฀But,฀we฀are฀just฀interested฀in฀detecting฀the฀presence฀ or฀absence฀of฀rain. Figure฀9.16J฀shows฀ 1 uF 50 WVDC the฀interface฀circuit฀ RCM3400 PWM (PF4) Wescor used฀to฀drive฀and฀ S-460 RCM3400 AIN1 measure฀the฀Wescor฀ 100 nF S-460฀Leaf฀Wetness฀ on development board 100 K 50 WVDC sensor. Figure฀9.16J:฀The฀moisture฀sensor฀requires฀an฀AC฀stimulus—the฀ Rabbit฀PWM฀feature฀and฀a฀single฀1uF฀capacitor฀provide฀it. 393

Chapter฀9 The฀1-microfarad฀capacitor฀decouples฀the฀DC฀bias฀of฀square-wave.฀This฀will฀prevent฀undesired฀plating฀of฀the฀electrodes. The฀Wescor฀S-460฀is฀placed฀in฀a฀voltage฀divider฀with฀a฀100K฀resistor.฀As฀the฀impedance฀of฀the฀ Wescor฀sensor฀changes,฀the฀signal฀at฀the฀center฀of฀the฀voltage฀divider฀will฀increase฀or฀decrease. The฀diode฀and฀100฀nanofarad฀capacitor฀form฀a฀detector฀and฀low-pass฀filter.฀What’s฀not฀shown฀ in฀Figure฀9.16J฀is฀the฀RCM3400฀development฀board’s฀analog-input’s฀impedance,฀about฀200K.฀ This฀200K฀is฀in฀parallel฀with฀the฀100฀nanofarad฀capacitor฀and฀allows฀the฀capacitor฀to฀discharge฀over฀time. The฀RCM3400฀actually฀has฀an฀input฀impedance฀of฀6–7฀mega-ohms,฀but฀the฀development฀board฀ places฀a฀voltage฀divider฀between฀the฀RCM3400฀and฀the฀screw-terminal฀connector฀on฀the฀development฀board฀(see฀Figure฀9.16D).฀The฀voltage฀divider฀on฀the฀RCM3400฀development฀board,฀ shown฀in฀Figure฀9.16K,฀decreases฀the฀input฀impedance฀to฀around฀200K.฀The฀circuit฀in฀Figure฀ 9.16J฀takes฀advantage฀of฀the฀decreased฀impedance฀to฀bleed฀the฀100฀nanofarad฀capacitor. AIN ON DEVELOPMENT BOARD 178 K

RCM3400 ANALOG INPUT 20.0 K

1 nF

Figure฀9.16K:฀The฀RCM3400฀development฀board฀has฀a฀bit฀of฀extra฀ signal฀conditioning฀that฀extends฀the฀RCM3400’s฀analog฀input฀range.

The฀voltage฀divider฀on฀the฀development฀board฀serves฀a฀couple฀of฀purposes.฀First,฀it฀protects฀ the฀RCM3400฀from฀over฀voltages.฀Second,฀the฀voltage฀divider฀extends฀the฀range฀of฀voltages฀ the฀RCM3400฀can฀measure. The฀development฀board฀also฀places฀a฀1฀nanofarad฀capacitor฀on฀the฀center฀of฀the฀voltage฀ divider.฀This฀forms฀a฀1-pole฀anti-alias฀filter฀and฀removes฀unwanted฀high-frequency฀noise. The฀RCM3400฀measures฀about฀0.2฀volts฀from฀the฀rain฀sensor฀circuit฀when฀the฀Wescor฀S-460฀ is฀dry฀and฀in฀a฀34%฀relative฀humidity฀(RH)฀environment.฀The฀RH฀was฀checked฀with฀an฀inexpensive฀consumer-grade฀hygrometer11. The฀RCM3400฀measures฀about฀0.8฀volts฀when฀the฀S-460฀has฀water฀on฀the฀sensor’s฀painted฀ surface. Picking฀a฀0.675-volt฀threshold฀for฀the฀“is฀it฀a฀rainy฀day”฀rule฀seems฀to฀work฀satisfactory. The฀Wescor฀S-460฀and฀PT100฀were฀mounted฀to฀the฀eves฀of฀Bob’s฀house.฀There,฀they฀are฀ exposed฀to฀rain,฀wind฀and฀sun.฀We฀expect฀a฀five-year฀minimum฀service฀period฀for฀the฀S-460฀ 11

฀ A฀hygrometer฀measures฀atmospheric฀relative฀humidity,฀while฀a฀hydrometer฀measures฀specific฀gravity.

394

Networking and฀a฀much฀longer฀period฀for฀the฀PT100.฀The฀sensors฀are฀high฀enough฀that฀they฀are฀not฀affected฀by฀the฀water฀spray฀from฀the฀sprinklers. Now฀that฀the฀RCM3400฀can฀read฀temperature,฀detect฀rain฀and฀actuate฀four฀valves฀(one฀at฀a฀ time),฀the฀system฀is฀ready฀to฀be฀web-enabled.฀Then฀Bob฀will฀be฀able฀to฀control฀his฀front฀yard฀ irrigation฀system฀from฀his฀laptop—just฀like฀surfing฀the฀web. 9.13.3฀Using฀Best฀Practices฀for฀Design:฀Software฀Requirements The฀software฀required฀for฀the฀sprinkler฀controller฀is฀in฀large฀part฀dependant฀on฀the฀browserbased฀user฀interface฀(UI)฀we฀define.฀The฀UI฀must฀be฀easy฀to฀use,฀yet฀versatile฀enough฀that฀ watering฀schedules฀can฀be฀quickly฀altered฀based฀on฀local฀weather฀conditions. Once฀the฀user฀has฀selected฀the฀watering฀schedule,฀we฀will฀update฀an฀event฀table.฀The฀event฀ table฀simply฀contains฀the฀list฀of฀start฀and฀stop฀times฀for฀each฀channel.฀The฀RCM3400฀will฀ constantly฀monitor฀the฀event฀table฀and฀engage฀sprinklers฀based฀on฀the฀event฀table฀and฀the฀Real฀ Time฀Clock฀(RTC). The฀software฀will฀also฀need฀to฀take฀into฀account฀the฀temperature฀and฀soil฀moisture฀content.฀ If฀the฀user฀opts฀to฀allow฀the฀watering฀schedule฀to฀be฀affected฀by฀temperature฀and/or฀moisture฀ content,฀the฀RCM3400฀firmware฀must฀alter฀the฀event฀table฀accordingly. We฀will฀allow฀two฀environmental฀modifications฀from฀which฀the฀user฀may฀allow฀one฀or฀both฀to฀ affect฀the฀schedule. The฀first฀rule฀is฀the฀“Hot฀Day”฀rule.฀This฀rule฀says—if฀the฀temperature฀is฀detected฀to฀be฀over฀ 100฀°F฀then฀each฀watering฀interval฀will฀be฀increased฀by฀five฀minutes. The฀second฀rule฀is฀the฀“Rainy฀Day”฀rule.฀This฀rule฀says—if฀the฀day฀is฀wet฀then฀decrease฀each฀ watering฀interval฀by฀five฀minutes.฀We฀could฀have฀decided฀to฀disable฀watering฀entirely,฀but฀if฀ the฀moisture฀sensor฀becomes฀defective฀and฀erroneously฀reports฀a฀“rainy”฀day,฀we฀do฀not฀want฀ to฀starve฀the฀plants฀entirely฀of฀water. For฀simplicity,฀we฀will฀limit฀the฀times฀that฀a฀channel฀can฀be฀turned฀on.฀For฀example,฀channel฀ #1฀can฀be฀turned฀on฀only฀at฀the฀top฀of฀each฀hour.฀Channels฀2,฀3฀and฀4฀are฀allowed฀start฀times฀a฀ quarter฀of฀an฀hour฀apart. We฀will฀define฀a฀standard฀watering฀interval฀as฀the฀time฀a฀sprinkler฀is฀turned฀on฀if฀neither฀the฀ Hot฀Day฀rule฀nor฀the฀Rainy฀Day฀rule฀is฀applied.฀We฀will฀fix฀the฀standard฀watering฀interval฀at฀ 10฀minutes. This฀means฀a฀sprinkler฀will฀be฀on฀for฀15฀minutes฀on฀a฀hot฀day฀and฀only฀on฀for฀5฀minutes฀on฀a฀ Rainy฀day. Next,฀we฀can฀sketch฀out฀a฀user฀interface฀that฀will฀provide฀these฀features. 9.13.4฀Using฀Best฀Practices฀for฀Design:฀Formalizing฀the฀User฀Interface Our฀web฀interface฀to฀the฀sprinkler฀system฀will฀allow฀the฀user฀to฀set฀up฀the฀sprinkler฀schedule,฀ set฀the฀clock฀on฀the฀device,฀and฀get฀the฀current฀status฀of฀the฀sprinkler฀system.฀Each฀of฀these฀ three฀functions฀will฀be฀contained฀on฀separate฀web฀pages. The฀sprinkler฀schedule฀page฀consists฀largely฀of฀a฀4x24฀table฀that฀allows฀the฀user฀to฀select฀the฀ times฀that฀each฀of฀the฀four฀sprinklers฀will฀be฀active.฀If฀a฀box฀is฀checked,฀then฀that฀sprinkler฀ 395

Chapter฀9 (indicated฀by฀the฀column)฀will฀be฀active฀starting฀at฀the฀given฀time฀for฀a฀default฀of฀10฀minutes.฀ At฀the฀bottom฀of฀the฀page฀is฀an฀additional฀table฀that฀allows฀the฀user฀to฀enable฀“rainy฀day”฀and฀ “hot฀day”฀rules.฀On฀a฀rainy฀day,฀the฀sprinkler฀duration฀is฀reduced฀to฀5฀minutes,฀but฀only฀if฀this฀ rule฀is฀active฀(i.e.,฀checked).฀On฀a฀hot฀day,฀the฀duration฀is฀increased฀to฀15฀minutes.฀The฀sprinkler฀schedule฀page฀also฀includes฀links฀to฀see฀the฀current฀system฀status฀and฀to฀set฀the฀clock. The฀status฀page฀is฀a฀small฀pop-up฀window.฀It฀contains฀a฀table฀that฀shows฀whether฀each฀sprinkler฀ is฀“ON”฀or฀“OFF.”฀It฀also฀indicates฀whether฀the฀conditions฀are฀currently฀“hot”฀and/or฀“rainy.” The฀clock฀page฀allows฀the฀user฀to฀set฀the฀real-time฀clock.฀The฀interface฀needs฀to฀check฀the฀input฀for฀correctness.฀For฀example,฀it฀should฀take฀into฀account฀the฀number฀of฀days฀in฀the฀month,฀ even฀for฀leap฀years฀versus฀non-leap฀years. We฀needed฀to฀come฀up฀with฀a฀clever฀design฀for฀the฀user฀interface,฀since฀sprinkler฀systems฀want฀ only฀one฀sprinkler฀valve฀to฀be฀on฀at฀a฀time,฀to฀deliver฀the฀highest฀water฀pressure.฀We฀designed฀ a฀user฀interface฀that฀allows฀the฀user฀to฀select฀no฀more฀than฀one฀valve฀at฀a฀time.฀Figure฀9.17฀ shows฀the฀HTML฀mock฀up฀of฀the฀interface;฀each฀checked฀box฀below฀will฀engage฀a฀sprinkler฀ for฀a฀10-minute฀interval,฀and฀no฀more฀than฀one฀sprinkler฀valve฀can฀turn฀on฀at฀the฀same฀time. Welcome฀to฀the฀Automated฀Sprinkler฀System Sprinkler฀1  12:00฀AM  1:00  2:00  3:00  4:00  5:00  6:00  7:00  8:00  9:00  10:00  11:00  12:00฀PM  1:00  2:00  3:00  4:00  5:00  6:00  7:00  8:00  9:00  10:00  11:00

Sprinkler฀2 12:15฀AM 1:15 2:15 3:15 4:15 5:15 6:15 7:15 8:15 9:15 10:15 11:15 12:15฀PM 1:15 2:15 3:15 4:15 5:15 6:15 7:15 8:15 9:15 10:15 11:15

                       

Sprinkler฀3 12:30฀AM 1:30 2:30 3:30 4:30 5:30 6:30 7:30 8:30 9:30 10:30 11:30 12:30฀PM 1:30 2:30 3:30 4:30 5:30 6:30 7:30 8:30 9:30 10:30 11:30

                       

Sprinkler฀4 12:45฀AM 1:45 2:45 3:45 4:45 5:45 6:45 7:45 8:45 9:45 10:45 11:45 12:45฀PM 1:45 2:45 3:45 4:45 5:45 6:45 7:45 8:45 9:45 10:45 11:45

Hot฀Day฀Rule Rainy฀Day฀Rule  Soil฀Wet฀Water฀Less Temperature฀>100F฀then฀Water฀More (5฀minute฀intervals) (15฀minute฀intervals) Figure฀9.17:฀A฀simple฀user฀interface฀allows฀check฀boxes฀to฀ allow฀users฀to฀quickly฀modify฀the฀watering฀schedule. 396

                        

Networking In฀the฀next฀section,฀we฀will฀describe฀how฀the฀interface฀was฀implemented฀with฀RabbitWeb.฀ While฀the฀HTML฀mock฀up฀looked฀good฀on฀the฀printed฀page,฀once฀we฀looked฀at฀it฀in฀a฀browser฀ window,฀we฀found฀it฀to฀be฀non-intuitive—in฀early฀trials,฀some฀users฀had฀difficulty฀associating฀ a฀check฀box฀to฀its฀time฀of฀day.฀We฀then฀modified฀the฀HTML฀code฀to฀make฀the฀interface฀more฀ intuitive.฀This฀constituted฀a฀refinement฀of฀a฀feature,฀not฀a฀change฀of฀a฀requirement. 9.13.5฀The฀Web฀Interface:฀C฀Code The฀code฀for฀this฀project฀resides฀in฀the฀file฀sprinkler.c.฀The฀web฀pages฀are฀in฀the฀files฀ sprinkler.zhtml฀(for฀scheduling),฀time.zhtml฀(for฀setting฀the฀time),฀and฀status.zhtml฀ (for฀getting฀system฀status).฀We฀will฀neither฀list฀nor฀describe฀every฀line฀of฀code฀here,฀but฀will฀ generally฀explain฀how฀the฀code฀behind฀the฀interface฀was฀written. The฀first฀things฀to฀note฀are฀the฀following฀lines: //฀sprinkler.c #define฀USE_RABBITWEB฀1 #define฀RWEB_POST_MAXVARS฀128 #define฀RWEB_POST_MAXBUFFER฀4096

Defining฀USE_RABBITWEB฀to฀1฀simply฀allows฀the฀developer฀to฀use฀the฀RabbitWeb฀extensions.฀ Otherwise,฀the฀extensions฀would฀not฀be฀compiled฀into฀the฀web฀server.฀Since฀this฀interface฀uses฀ many฀variables฀(recall฀the฀4x24฀table),฀most฀of฀which฀can฀be฀updated฀all฀at฀once,฀we฀need฀to฀ raise฀the฀number฀of฀variables฀that฀the฀RabbitWeb฀extensions฀can฀handle.฀This฀is฀controlled฀by฀ the฀RWEB_POST_MAXVARS฀macro฀(defaults฀to฀64).฀Additionally,฀since฀so฀many฀variable฀updates฀ can฀be฀sent฀to฀the฀web฀server,฀we฀need฀to฀increase฀the฀size฀of฀the฀buffer฀used฀to฀store฀those฀ variables.฀RWEB_POST_MAXBUFFER฀(defaults฀to฀2048฀bytes)฀defines฀the฀number฀of฀bytes฀we฀ need.฀This฀number฀can฀be฀approximated฀by฀inspecting฀a฀network฀trace฀of฀a฀request฀and฀noting฀ the฀number฀of฀bytes฀in฀the฀request.฀Section฀9.14฀mentions฀some฀useful฀utilities฀for฀examining฀ network฀traffic. #use฀“dcrtcp.lib” #use฀“http.lib”

These฀lines฀simply฀include฀the฀TCP/IP฀stack฀and฀the฀web฀(HTTP)฀server. #ximport฀“sprinkler.zhtml”฀sprinkler_zhtml #ximport฀“status.zhtml”฀status_zhtml #ximport฀“time.zhtml”฀time_zhtml

The฀interface฀files฀(written฀in฀HTML฀with฀RabbitWeb฀extensions)฀must฀be฀imported฀into฀the฀ user฀program.฀#ximport฀does฀this,฀and฀provides฀long฀pointers฀to฀the฀contents฀of฀each฀of฀the฀ files฀(sprinkler_zthml,฀status_zhtml,฀and฀time_zhtml).

397

Chapter฀9 SSPEC_MIMETABLE_START ฀ SSPEC_MIME_FUNC(“.zhtml”,฀“text/html”,฀zhtml_handler), ฀ SSPEC_MIME(“.html”,฀“text/html”) SSPEC_MIMETABLE_END

The฀MIME฀table฀allows฀certain฀filename฀extensions฀to฀be฀associated฀with฀specific฀MIME฀ types฀(which฀is฀how฀a฀web฀browser฀determines฀the฀type฀of฀content฀in฀a฀file),฀as฀well฀as฀associating฀web฀server฀handlers฀for฀specific฀types฀of฀content.฀Note฀the฀“.zhtml”฀entry฀above:฀the฀ zhtml_handler฀parameter฀means฀that฀files฀ending฀in฀“.zhtml”฀will฀first฀be฀processed฀by฀the฀ zhtml_handler฀before฀being฀sent฀out.฀This฀lets฀the฀web฀server฀know฀that฀it฀needs฀to฀search฀ for฀specific฀commands฀with฀those฀files฀(known฀as฀ZHTML฀commands)฀that฀allows฀for฀dynamic฀content.฀This฀will฀become฀more฀apparent฀when฀we฀look฀at฀some฀of฀these฀“.zhtml”฀files. SSPEC_RESOURCETABLE_START ฀ SSPEC_RESOURCE_XMEMFILE(“/”,฀sprinkler_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/index.zhtml”,฀sprinkler_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/status.zhtml”,฀status_zhtml), ฀ SSPEC_RESOURCE_XMEMFILE(“/time.zhtml”,฀time_zhtml), SSPEC_RESOURCETABLE_END

Each฀of฀the฀imported฀files฀is฀associated฀with฀specific฀names.฀When฀the฀server฀gets฀a฀request฀ for฀one฀of฀these฀names,฀it฀servers฀the฀associated฀file. A฀number฀of฀variables฀are฀declared฀in฀the฀program,฀such฀as฀a฀schedule[][]฀array฀and฀the฀ hotrule฀and฀rainyrule฀variables.฀There฀is฀nothing฀special฀about฀these฀declarations,฀but฀the฀ “#web”฀lines฀that฀follow฀them฀are฀very฀special.฀We฀dealt฀with฀the฀#web฀lines฀in฀the฀previous฀ section;฀the฀“hotrule”฀line฀is฀not฀much฀different: int฀hotrule;฀

//฀1฀if฀the฀hot฀temperatures฀rule฀is฀enabled,฀0฀otherwise

#web฀hotrule฀($hotrule฀==฀0฀||฀$hotrule฀==฀1)

This฀line฀registers฀the฀variable฀hotrule฀for฀use฀with฀the฀RabbitWeb฀extensions.฀This฀variable฀ can฀be฀displayed฀and฀updated฀through฀the฀web฀interface.฀The฀expression฀at฀the฀end฀of฀the฀line฀ is฀the฀error-checking,฀or฀“guard,”฀expression.฀Each฀update฀of฀the฀variable’s฀value฀must฀pass฀ this฀expression฀(i.e.,฀the฀expression฀must฀be฀true)฀for฀the฀update฀to฀be฀applied.฀Note฀the฀“$”฀ symbols฀in฀front฀of฀each฀instance฀of฀hotrule.฀This฀indicates฀that฀the฀proposed฀updated฀value฀ of฀the฀variable฀should฀be฀used฀in฀the฀guard. RabbitWeb฀also฀allows฀arrays฀to฀be฀registered,฀as฀in฀the฀following: #web฀schedule[@][@]฀($schedule[@[0]][@[1]]฀==฀0฀||฀\ ฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀$schedule[@[0]][@[1]]฀==฀1)

The฀“@”฀symbols฀are฀used฀to฀wildcard฀the฀array฀indices.฀That฀is,฀this฀statement฀applies฀to฀ schedule[0][0],฀schedule[0][1],฀schedule[2][13],฀etc.฀In฀the฀guard,฀the฀“@[0]”฀and฀“@[1]”฀ expressions฀refer฀to฀the฀first฀wildcard฀and฀the฀second฀wildcard,฀respectively.฀ 398

Networking It฀is฀also฀possible฀to฀make฀variables฀read-only: #web฀hours[@]฀groups=all(ro) #web฀minutes[@]฀groups=all(ro)

The฀hours[]฀and฀minutes[]฀arrays฀are฀used฀to฀construct฀the฀time฀columns฀in฀the฀scheduling฀ table.฀They฀are฀used฀for฀display฀only.฀Hence,฀they฀are฀made฀read-only฀by฀the฀“groups=all(ro)”฀ expressions.฀This฀syntax฀means฀that฀these฀variables฀are฀read-only฀(ro)฀for฀all฀groups.฀ RabbitWeb฀allows฀access฀to฀variables฀to฀be฀restricted฀by฀user฀group,฀so฀these฀authorization฀ expressions฀can฀be฀more฀sophisticated฀than฀is฀presented฀here. Here฀is฀another฀interesting฀#web฀line: #web฀time.month฀select(“January”฀=฀1,฀“February”,฀“March”,฀“April”,฀\ ฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀“May”,฀“June”,฀“July”,฀“August”,฀“September”,฀\ ฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀฀“October”,฀“November”,฀“December”)

This฀registration฀of฀the฀time.month฀variable฀(note฀that฀RabbitWeb฀also฀allows฀use฀of฀structures)฀indicates฀that฀it฀is฀a฀“selection฀variable.”฀A฀selection฀variable฀is฀an฀integer฀with฀specific฀ string฀values฀associated฀with฀each฀integral฀value.฀Conceptually,฀it฀is฀like฀an฀enum฀in฀C.฀This฀ construct฀supports฀the฀use฀of฀pull-down฀menus,฀as฀we฀will฀see฀later. #web฀time.day฀(checkday($time.day,฀$time.month,฀$time.year))

And฀here฀is฀one฀last฀#web฀statement.฀Not฀only฀can฀we฀give฀the฀guard฀expression฀inline,฀we฀ can฀also฀call฀a฀separate฀function฀to฀perform฀the฀error฀check.฀The฀checkday()฀function฀makes฀ sure฀that฀the฀number฀given฀for฀the฀day฀is฀valid฀for฀the฀given฀month฀and฀year.฀Since฀it฀even฀ takes฀leap฀years฀into฀account,฀then฀giving฀this฀entire฀expression฀inline฀would฀be฀cumbersome. When฀the฀time฀is฀updated฀through฀the฀clock฀interface,฀how฀does฀the฀program฀know฀to฀apply฀ this฀update฀to฀the฀real-time฀clock?฀The฀answer฀lies฀in฀the฀following฀line: #web_update฀time.year,time.month,time.day,time.hour,time.minute,\ ฀฀฀฀฀฀฀฀฀฀฀฀time.second฀updatertc

This฀line฀means฀that฀when฀one฀of฀the฀“time”฀members฀is฀successfully฀updated,฀then฀the฀function฀updatertc()฀will฀be฀called.฀updatertc()฀applies฀the฀time฀change฀to฀the฀real-time฀clock. There฀is฀not฀much฀else฀to฀study฀in฀the฀C฀code.฀http_init()฀initializes฀the฀web฀server.฀ tcp_reserveport(80)฀provides฀a฀performance฀improvement฀to฀the฀web฀server฀(port฀80)฀by฀ allowing฀it฀to฀queue฀requests฀that฀it฀cannot฀currently฀service฀(rather฀than฀refusing฀them฀outright).฀And฀http_handler(),฀which฀is฀called฀from฀the฀main฀loop,฀drives฀the฀web฀server. We฀implemented฀three฀costates฀for฀the฀project: ฀

The฀mainHandler฀costatement฀drives฀the฀HTTP฀server฀with฀the฀important฀฀ http_handler()฀call.฀It฀also฀updates฀the฀time฀structure฀from฀the฀real-time฀clock,฀so฀ 399

Chapter฀9 that฀the฀time฀can฀be฀displayed฀properly฀in฀the฀web฀interface.฀This฀costatement฀runs฀ every฀50฀milliseconds. ฀ The฀sensorHandler฀costatement฀drives฀the฀routine฀that฀averages฀samples฀from฀the฀ rain฀and฀temperature฀sensor.฀The฀routine฀determines฀the฀“hot฀day”฀and฀“rainy฀day”฀ status฀and฀the฀appropriate฀watering฀interval.฀This฀costatement฀runs฀once฀a฀minute฀and,฀ if฀required,฀could฀update฀the฀sensors฀even฀less฀frequently. ฀ The฀sprinklerHandler฀costatement฀keeps฀track฀of฀the฀current฀time฀and฀the฀sprinkler฀ table฀from฀the฀user฀interface,฀and฀determines฀whether฀a฀sprinkler฀has฀to฀be฀turned฀on.฀ Since฀the฀hardware฀requirement฀called฀for฀turning฀on฀only฀one฀sprinkler฀at฀a฀time,฀ the฀code฀makes฀sure฀all฀sprinklers฀are฀off฀before฀it฀turns฀one฀on.฀It฀uses฀printfs฀as฀a฀ debugging฀aid฀to฀inform฀the฀user฀on฀the฀stdio฀window฀when฀a฀sprinkler฀gets฀turned฀ on฀and฀for฀how฀long.฀This฀costatement฀runs฀every฀fifteen฀seconds;฀it฀would฀do฀just฀as฀ well฀if฀it฀ran฀once฀every฀thirty฀seconds. 9.13.6฀The฀Web฀Interface:฀The฀ZHTML฀Scripting฀Language This฀section฀does฀not฀purport฀to฀be฀an฀HTML฀primer.฀It฀will฀only฀cover฀the฀features฀that฀are฀ unique฀to฀the฀ZHTML฀scripting฀language.฀There฀are฀many฀fine฀books฀and฀tutorials฀available฀ on฀HTML;฀www.w3.org฀contains฀the฀definitive฀resources฀in฀the฀form฀of฀various฀standards. Special฀commands฀in฀.zhtml฀files฀allow฀the฀developer฀to฀create฀dynamic฀content.฀These฀commands฀are฀encased฀in฀“”฀tags.฀The฀zhtml_handler()฀mentioned฀earlier฀searches฀ for฀these฀tags฀within฀.zhtml฀files.฀When฀it฀finds฀a฀command฀within฀those฀tags,฀it฀performs฀the฀ appropriate฀action.฀One฀important฀consideration฀when฀writing฀ZHTML฀is฀that฀each฀ZHTML฀ command฀must฀be฀in฀separate฀“”฀tags.฀That฀is,฀we฀cannot฀include฀multiple฀ ZHTML฀commands฀in฀a฀single฀set฀of฀tags. status.zhtml

We฀will฀begin฀by฀looking฀at฀the฀sprinkler฀system฀status฀page,฀status.zhtml.฀The฀first฀ZHTML฀ command฀is฀a฀for฀loop:

for฀loops฀work฀just฀like฀in฀C.฀ZHTML฀allows฀temporary฀loop฀variables฀with฀the฀names฀$A,฀ $B,฀$C,฀etc.฀to฀be฀used฀in฀for฀loops.฀The฀count($status,฀0)฀expression฀evaluates฀to฀the฀ number฀of฀elements฀in฀the฀first฀dimension฀of฀the฀$status[]฀array.฀This฀line,฀then,฀begins฀a฀ loop฀that฀iterates฀over฀the฀members฀of฀$status.฀This฀will฀be฀used฀to฀build฀a฀table฀that฀indi-

cates฀whether฀each฀sprinkler฀is฀“ON”฀or฀“OFF”.

฀฀฀OFF

ZHTML฀also฀allows฀if฀statements.฀We฀check฀the฀value฀of฀$status[$A],฀and฀if฀it฀is฀0,฀then฀we฀ display฀an฀“OFF”฀message฀in฀red.฀ZHTML฀allows฀simple฀integral฀comparisons฀for฀if฀expressions.฀The฀other฀if฀statements฀in฀the฀rest฀of฀status.zhtml฀are฀similar฀to฀the฀one฀above. 400

Networking time.zhtml

Now฀let฀us฀look฀at฀the฀time.zthml฀file,฀which฀contains฀the฀interface฀to฀set฀the฀date฀and฀time฀ of฀the฀sprinkler฀controller.฀

฀฀฀ ฀฀฀The฀date฀and฀time฀you฀entered฀was฀invalid.฀฀Please฀try฀correcting฀the ฀฀฀error฀and฀setting฀the฀clock฀again.

฀฀฀

In฀addition฀to฀integral฀comparisons,฀ZHTML฀has฀a฀few฀special฀expressions฀for฀if฀statements.฀ The฀error()฀expression฀evaluates฀to฀true฀if฀we฀are฀displaying฀this฀page฀as฀a฀result฀of฀an฀error฀ in฀input.฀We฀use฀this฀to฀tell฀the฀user฀to฀try฀entering฀the฀date฀and฀time฀again.

฀฀฀ ฀฀฀฀฀฀The฀clock฀was฀set฀successfully.

฀฀฀

The฀updating()฀expression฀evaluates฀to฀true฀if฀we฀are฀displaying฀this฀page฀as฀the฀result฀of฀an฀ attempted฀variable฀update.฀These฀nested฀if฀statements฀will฀display฀a฀“success”฀message฀if฀we฀ have฀updated฀without฀error.

This฀is฀not฀a฀ZHTML฀command,฀but฀rather฀the฀beginning฀of฀an฀HTML฀form.฀We฀note฀it฀here฀ because฀of฀the฀action฀and฀method฀parameters.฀The฀action฀parameter฀indicates฀that฀after฀the฀ received฀variable฀updates฀from฀the฀form฀have฀been฀processed,฀the฀“/time.zhtml”฀page฀will฀ be฀displayed฀(or฀in฀this฀case,฀redisplayed).฀For฀the฀web฀server฀to฀process฀the฀variables,฀the฀ method฀parameter฀must฀always฀be฀“POST”.

฀฀฀ ฀฀฀> ฀฀฀

The฀above฀code฀generates฀the฀pull-down฀menu฀for฀the฀month.฀Note฀that฀the฀name฀option฀ to฀the฀SELECT฀tag฀is฀“time.month”,฀without฀the฀“$”.฀The฀variable฀name฀must฀always฀ 401

Chapter฀9 match,฀but฀must฀not฀include฀the฀“$”฀sign฀when฀given฀as฀parameters฀to฀a฀form฀element.฀ “count($time.month)”฀returns฀the฀number฀of฀options฀in฀the฀selection฀variable,฀time.month.฀ “selected($time.month,฀$A)”฀is฀another฀special฀if฀statement฀expression.฀It฀returns฀whether฀ or฀not฀the฀$Ath฀option฀is฀selected.฀The฀“print_opt($time.month,฀$A)”฀command฀outputs฀the฀ string฀corresponding฀to฀the฀$Ath฀option.฀The฀current฀month฀is฀selected฀by฀default฀for฀the฀user. ”>

This฀line฀creates฀a฀field฀for฀the฀hour.฀Notice฀the฀printf()฀ZHTML฀statement.฀printf()฀allows฀ us฀to฀provide฀a฀format฀specifier.฀Note฀that฀printf()฀only฀allows฀a฀single฀variable,฀unlike฀ printf()฀in฀C. The฀remainder฀of฀time.zhtml฀is฀either฀similar฀to฀what฀we฀have฀already฀studied,฀or฀contains฀ only฀standard฀HTML.฀ sprinkler.zhtml

Although฀sprinkler.zhtml฀is฀the฀longest฀ZHTML฀page,฀we฀have฀already฀covered฀almost฀all฀ of฀the฀ZHTML฀features฀that฀it฀uses.฀One฀interesting฀technique฀used฀on฀this฀page฀has฀nothing฀ to฀do฀with฀ZHTML.฀Rather,฀it฀uses฀JavaScript:

Check฀the฀sprinkler฀status

The฀above฀JavaScript฀function฀opens฀a฀new฀window฀with฀a฀width฀of฀400฀pixels฀and฀a฀height฀ of฀200฀pixels.฀This฀new฀window฀will฀display฀the฀“status.zhtml”฀page.฀The฀“A฀HREF”฀tag฀ contains฀the฀link฀to฀the฀JavaScript฀openStatus()฀function฀that฀we฀defined. We฀display฀the฀hours฀in฀the฀table฀in฀a฀slightly฀different฀manner฀than฀before:

402

Networking The฀echo()฀command฀is฀actually฀the฀same฀as฀the฀print()฀command฀noted฀earlier. Normally,฀checkboxes฀in฀HTML฀only฀send฀a฀value฀to฀the฀web฀server฀when฀the฀checkbox฀is฀ selected.฀If฀the฀checkbox฀is฀not฀selected,฀then฀no฀value฀for฀the฀corresponding฀variable฀is฀sent฀ to฀the฀web฀server.฀This฀is฀problematic฀for฀RabbitWeb.฀It฀cannot฀distinguish฀between฀a฀variable฀ that฀has฀not฀been฀sent฀because฀it฀is฀not฀selected,฀and฀a฀variable฀that฀has฀not฀been฀sent฀because฀ it฀is฀not฀contained฀in฀the฀form.฀Hence,฀if฀a฀user฀deselects฀a฀checkbox,฀then฀the฀associated฀variable฀is฀not฀changed฀by฀RabbitWeb฀simply฀because฀it฀does฀not฀get฀an฀update฀for฀that฀variable.฀ The฀following฀code฀implements฀a฀workaround: ” ฀฀฀฀฀฀฀value=”1” ฀฀฀ ฀฀฀฀฀฀CHECKED ฀฀฀ >

The฀“hidden”฀INPUT฀tag฀above฀creates฀a฀variable฀that฀is฀always฀sent฀to฀the฀web฀server.฀Hence,฀ the฀web฀server฀will฀always฀receive฀a฀value฀of฀“0”฀for฀each฀of฀$schedule[$B][$A].฀However,฀if฀ the฀checkbox฀for฀$schedule[$B][$A]฀is฀selected,฀then฀it฀will฀also฀receive฀a฀value฀of฀“1”.฀The฀ web฀server฀will฀accept฀the฀last฀value฀received฀as฀the฀definitive฀value.฀Therefore,฀if฀the฀checkbox฀is฀not฀selected,฀the฀variable฀will฀be฀given฀the฀value฀“0”;฀if฀it฀is฀selected,฀it฀will฀be฀given฀ the฀value฀“1”. Note฀the฀varname()฀command฀within฀the฀name฀field฀to฀the฀INPUT฀tags฀above.฀Array฀variables,฀when฀given฀in฀name฀fields,฀must฀be฀modified฀so฀that฀they฀can฀be฀sent฀in฀the฀update฀to฀ the฀web฀server.฀The฀varname()฀command฀encodes฀the฀variable฀name฀so฀that฀it฀can฀be฀sent.฀ varname()฀can฀be฀used฀for฀all฀variables,฀not฀just฀array฀variables;฀however,฀it฀is฀necessary฀for฀ array฀variables. The฀remainder฀of฀sprinkler.zhtml฀is฀similar฀to฀material฀that฀we฀have฀already฀covered.฀ 9.13.7฀Project฀Screenshots Figure฀9.18A฀shows฀the฀status฀screen฀that฀presents฀sprinkler฀and฀sensor฀status฀to฀the฀user.฀We฀ simulated฀rain฀by฀putting฀some฀drops฀ of฀water฀on฀the฀rain฀sensor,฀and฀turned฀ on฀the฀“rainy฀day”฀rule.฀The฀rain฀sensor,฀ with฀the฀thresholds฀set฀at฀0.650฀to฀0.675฀ volts,฀worked฀as฀expected. Before฀connecting฀the฀sprinkler฀controller฀to฀real฀sprinklers,฀we฀used฀printfs฀in฀ the฀code฀to฀simulate฀sprinkler฀operation.฀ Figure฀9.18B฀shows฀the฀output฀on฀the฀ stdio฀screen฀that฀corresponds฀to฀sprinkler฀settings฀from฀Figure฀9.18C.

Figure฀9.18A:฀Sprinkler฀status฀page. 403

Chapter฀9

Figure฀9.18B:฀Status฀output฀on฀the฀ stdio฀window.

Figure฀9.18C:฀Web฀interface฀for฀ the฀sprinkler฀system.

Figure฀9.18B฀shows฀that฀the฀simulation฀started฀with฀ moisture฀on฀the฀rain฀sensor,฀and฀the฀“rainy฀day”฀ rule฀made฀the฀sprinklers฀run฀for฀five฀minutes.฀As฀the฀ moisture฀on฀the฀sensor฀dried฀up฀in฀the฀next฀ninety฀ minutes,฀although฀the฀“rainy฀day”฀rule฀was฀still฀in฀ affect,฀the฀sprinklers฀reverted฀to฀the฀standard฀watering฀interval฀of฀ten฀minutes,฀as฀defined฀in฀the฀project฀ specification. Figure฀9.18C฀shows฀the฀web฀interface฀for฀the฀ sprinkler฀system.฀It฀does฀not฀look฀too฀different฀from฀ Figure฀9.17,฀and฀provides฀links฀for฀setting฀the฀clock฀ and฀displaying฀sensor฀status. 9.13.8฀Final฀Thought We฀have฀successfully฀met฀the฀requirements฀for฀our฀ web-enabled฀sprinkler฀controller.฀Now฀we฀consider฀ other฀requirements฀that฀might฀be฀interesting. What฀are฀the฀consequences฀of฀someone฀hacking฀into฀ the฀sprinkler฀controller?฀Would฀a฀prankster฀be฀successful฀in฀killing฀the฀lawn฀by฀forcing฀the฀sprinkler฀ controller฀to฀over฀or฀under-water฀while฀the฀homeowner฀is฀on฀vacation?฀ If฀the฀home฀network฀upon฀which฀the฀sprinkler฀ controller฀resides฀is฀not฀suitably฀protected฀it฀is฀ conceivable฀a฀miscreant฀could฀maliciously฀alter฀the฀ 404

Networking watering฀times.฀This฀could฀result฀in฀a฀brown฀or฀dead฀lawn฀if฀the฀homeowner฀is฀inattentive฀or฀ absent.฀It฀could฀also฀result฀in฀a฀huge฀waste฀of฀water. To฀avoid฀this฀situation,฀we฀might฀consider฀some฀of฀the฀following฀approaches. ฀

Secure฀Socket฀Layer฀(SSL)-based฀authentication฀might฀be฀required฀of฀a฀user฀before฀ the฀user฀could฀modify฀the฀system฀settings.฀ ฀ Additional฀safeguards฀such฀as฀hard-coding฀a฀minimum฀and฀maximum฀allowable฀ watering฀interval฀for฀each฀valve.฀ ฀ We฀could฀enforce฀that฀each฀sprinkler฀is฀not฀turned฀on฀for฀more฀than฀a฀total฀of฀one฀ hour฀per฀day. ฀ We฀could฀add฀separate฀layers฀of฀authentication฀to฀the฀interface,฀to฀only฀allow฀certain฀ users฀to฀modify฀the฀setup,฀while฀other฀users฀can฀only฀view฀the฀setup.฀

Anytime฀someone฀talks฀about฀security,฀an฀oft-overlooked฀element฀is฀simply฀that฀of฀physical฀ security.฀All฀of฀the฀valves฀for฀the฀sprinkler฀system฀are฀simply฀sitting฀in฀a฀front฀yard.฀An฀individual฀intent฀on฀doing฀harm,฀need฀only฀walk฀up฀to฀the฀exposed฀valves฀and฀disconnect฀a฀few฀ wires,฀unscrew฀the฀solenoids,฀or฀manually฀turn฀on฀the฀valves.฀If฀the฀homeowner฀is฀on฀vacation,฀the฀same฀harm฀is฀done฀as฀“hacking฀into”฀the฀sprinkler฀system. In฀a฀case฀like฀this,฀the฀extra฀effort฀of฀locking฀the฀“electronic”฀door฀is฀probably฀not฀worth฀the฀ engineering. Another฀issue฀to฀consider฀is฀that฀of฀reliability.฀What฀does฀it฀mean฀for฀the฀system฀to฀be฀reliable?฀In฀a฀home฀appliance,฀it฀means฀the฀system฀will฀do฀what฀it฀is฀told฀and฀when฀it฀is฀told.฀In฀ this฀case,฀it฀means฀that฀as฀long฀as฀the฀system฀has฀power,฀the฀controller฀will฀accurately฀keep฀ track฀of฀time฀and฀execute฀the฀watering฀instructions฀given฀to฀it. To฀improve฀reliability,฀we฀could฀improve฀the฀packaging฀so฀the฀device฀isn’t฀easily฀damaged.฀ We฀already฀have฀a฀small฀battery฀on฀the฀RCM3400฀development฀board฀that฀will฀back฀up฀the฀ RTC,฀but฀the฀watering฀schedule฀is฀only฀stored฀in฀volatile฀RAM.฀We฀could฀ensure฀a฀copy฀of฀ the฀current฀watering฀schedule฀is฀stored฀in฀the฀FLASH.฀Upon฀re-boot฀after฀a฀power฀outage,฀the฀ system฀could฀then฀reload฀the฀most฀recent฀watering฀schedule. Often,฀people฀like฀to฀talk฀about฀“availability”฀when฀discussing฀web-based฀systems.฀Generally฀this฀term฀is฀applied฀to฀mean฀“accessible฀and฀operable”.฀This฀can฀be฀tied฀to฀reliability,฀but฀ often฀marketers฀and฀engineers฀draw฀a฀distinction.฀ Consider฀a฀firewall.฀A฀firewall฀may฀have฀excellent฀availability,฀but฀may฀have฀poor฀reliability.฀ For฀example,฀a฀firewall฀may฀never฀crash฀(being฀highly฀available),฀but฀it฀may฀not฀stop฀unauthorized฀access฀to฀a฀network฀(not฀be฀reliable). In฀the฀case฀of฀a฀simple฀home฀appliance,฀the฀end฀user฀will฀see฀“availability”฀and฀“reliability”฀ together฀into฀“its-a-working-or-not-ability,”฀thus฀rendering฀the฀distinction฀between฀availability฀and฀reliability฀mute.฀The฀end฀user฀will฀only฀care฀that฀the฀device฀appears฀to฀both฀water฀the฀ lawn฀on฀queue,฀and฀be฀available฀for฀configuration฀via฀browser฀at฀any฀time. Safety฀is฀another฀issue฀to฀consider.฀We฀have฀not฀fused฀anything.฀At฀a฀minimum,฀the฀AC฀ transformer฀should฀be฀fused.฀This฀would฀protect฀the฀system฀from฀an฀accidental฀fire฀should฀the฀ wires฀going฀to฀a฀solenoid฀short.฀This฀is฀an฀imperative฀in฀a฀residential฀setting. 405

Chapter฀9 Clearly,฀this฀project฀has฀a฀long฀road฀to฀becoming฀a฀commercial฀product.฀However,฀it฀does฀ illustrate฀the฀powerful฀features฀of฀the฀Rabbit฀hardware฀and฀Dynamic฀C’s฀RabbitWeb฀tools.฀ The฀entire฀system฀involved฀very฀little฀hardware฀to฀interface฀to฀the฀sensors฀and฀AC฀solenoids.฀ The฀software฀is฀small฀and฀easy฀to฀understand฀and฀modify. As฀far฀as฀the฀authors฀know,฀there฀is฀no฀other฀combination฀of฀hardware฀and฀software฀on฀the฀ market฀other฀than฀Rabbit฀and฀Dynamic฀C฀with฀RabbitWeb฀that฀will฀allow฀a฀project฀of฀this฀ scope฀to฀be฀assembled฀as฀quickly,฀simply฀or฀compactly.฀

9.14฀Some฀Useful฀(and฀Free!)฀Networking฀Utilities We฀should฀not฀take฀code฀libraries฀at฀face฀value—things฀do฀not฀sometimes฀work฀as฀planned,฀ and,฀in฀the฀networking฀world,฀it฀is฀important฀to฀be฀able฀to฀independently฀observe฀network฀traffic฀while฀debugging.฀The฀following฀tools฀will฀be฀handy฀in฀debugging฀applications. 9.14.1฀Ping This฀useful฀utility฀is฀found฀on฀almost฀all฀operating฀systems,฀and฀its฀simplicity฀and฀ubiquity฀ make฀it฀a฀popular฀application.฀The฀key฀function฀of฀Ping฀is฀to฀quickly฀establish฀whether฀a฀ device฀on฀the฀network฀is฀reachable.฀Moreover,฀because฀Ping฀also฀reports฀the฀length฀of฀time฀ between฀the฀outgoing฀request฀and฀the฀response,฀it฀can฀help฀us฀determine฀network฀conditions.฀ Curious฀programmers฀should฀go฀one฀step฀further฀and฀explore฀how฀Ping฀uses฀ICMP฀messages฀ to฀perform฀its฀function. Figure฀9.19A฀shows฀how฀we฀used฀Ping฀in฀a฀Windows฀XP฀command฀window฀to฀reach฀the฀ default฀gateway฀in฀Figure฀9.3. C:\>ping฀192.168.1.1 Pinging฀192.168.1.1฀with฀32฀bytes฀of฀data: Reply฀from฀192.168.1.1:฀bytes=32฀time=1ms฀TTL=150 Reply฀from฀192.168.1.1:฀bytes=32฀time>฀12฀;฀Upper฀nibble฀start฀boundary (SEGSIZE),฀a฀ ฀ ;฀Set฀SEGSIZE a,฀(sgat฀TABLE฀>>฀12)฀–฀(sgst฀TABLE฀>>฀12) (DATASEG),฀a฀ ฀ ;฀Set฀DATASEG

For฀this฀discussion,฀let฀us฀assume฀segment฀TABLE฀will฀be฀linked฀at฀logical฀address฀C000h฀and฀ physical฀address฀70000h.฀The฀third฀instruction฀takes฀the฀upper฀nibble฀of฀the฀logical฀start฀of฀ segment฀TABLE฀and฀adds฀that฀to฀the฀lower฀nibble฀of฀STACKSEG.฀sgst฀gets฀the฀logical฀start฀of฀a฀ segment฀while฀sgat฀gets฀the฀physical฀start฀address.฀This฀ORs฀0Ch฀into฀SEGSIZE.฀ 414

Softools—The฀Third฀Party฀Tool The฀fifth฀instruction฀is฀one฀step฀more฀complicated.฀Since฀the฀Rabbit฀MMU฀adds฀the฀logical฀ nibble฀to฀the฀final฀address,฀it฀must฀be฀removed฀from฀the฀MMU฀DATASEG฀register.฀ The฀first฀part฀is฀the฀physical฀address฀of฀“TABLE฀shift฀right฀12,”฀which฀is฀70h.฀The฀second฀part฀ is฀the฀0Ch฀used฀above.฀This฀loads฀64h฀(70h฀–฀0Ch)฀into฀DATASEG.฀If฀the฀program฀loads฀a฀word฀ from฀TABLE+1230h,฀which฀is฀0D123h,฀the฀0Dh฀selects฀DATASEG,฀which฀adds฀DATASEG,฀64h,฀ to฀0Dh,฀getting฀71h,฀for฀a฀final฀runtime฀address฀of฀71123h.฀ One฀might฀argue฀that฀we฀could฀simply฀load฀these฀values฀immediately฀in฀the฀instructions฀with฀ constants.฀The฀advantage฀here฀is฀that,฀at฀link฀time,฀we฀can฀move฀segment฀TABLE฀to฀any฀logical฀ address฀(e.g.,฀9000h฀or฀5000h)฀and฀any฀physical฀address฀and฀the฀code฀above฀works฀without฀ being฀changed฀or฀assembled.฀ The฀linker’s฀job฀is฀to฀resolve฀symbols฀and฀locate฀segments.฀We฀should฀be฀able฀to฀do฀so฀without฀changing฀the฀source฀code.฀ Unfortunately,฀most฀development฀tools฀can’t฀do฀complex฀expression฀evaluation฀at฀link฀time,฀ and฀require฀source฀code฀changes฀when฀changing฀the฀memory฀layout฀of฀the฀program. 10.2.6฀SAMRabbit฀Macros Microsoft’s฀M80฀8080/Z80฀macro฀assembler฀from฀the฀mid฀1980’s฀set฀a฀de-facto฀standard฀ for฀macro฀support.฀Yes,฀even฀Microsoft฀once฀had฀its฀fingers฀in฀the฀Z80฀and฀8-bit฀tool฀market.฀ Softools’฀SASMRabbit฀is฀compatible฀with฀M80฀and฀its฀macro฀syntax.฀ Macros฀can฀be฀used฀for฀many฀tedious฀tasks.฀They฀can฀also฀be฀used฀for฀clever฀things,฀and฀to฀ take฀it฀to฀the฀extreme,฀even฀for฀things฀like฀calculating฀checksums฀of฀program฀strings฀to฀detect฀ program฀tampering.฀ Here’s฀a฀practical฀example฀that฀creates฀a฀256-byte฀lookup฀table฀with฀the฀parity฀for฀the฀byte฀ where฀the฀byte฀would฀be฀used฀as฀the฀index฀into฀the฀table.฀If฀the฀byte฀taken฀from฀the฀table฀is฀ zero,฀then฀the฀byte฀has฀even฀parity,฀and฀conversely฀is฀one฀for฀odd฀parity฀bytes.฀This฀has฀a฀direct฀ application฀on฀the฀Rabbit฀3000฀as฀parity฀for฀the฀serial฀ports฀must฀be฀calculated฀in฀software. Program฀10.2฀shows฀a฀macro฀to฀generate฀the฀parity฀table฀and฀the฀fastest฀code฀to฀generate฀parity฀for฀a฀byte฀in฀register฀A: Program฀10.2:฀Generating฀a฀parity฀table. genParityTable฀ .macro byte฀ =฀ 0 ฀ .rept฀ 256 par฀=฀ 0 mask฀ =฀ 1 ฀ .rept฀ ฀ .if฀ par฀=฀ ฀ .endif mask฀

8 byte฀&฀mask par+1 =฀

mask฀