293 3 4MB
English Pages 481 Year 2004
EmbeddedSystemsDesignUsingthe Rabbit3000Microprocessor
EmbeddedSystemsDesignUsingthe Rabbit3000Microprocessor Interfacing,NetworkingandApplicationDevelopment
By
KamalHyder BobPerrin
AMSTERDAM • BOSTON • HEIDELBERG • LONDON NEW YORK • OXFORD • PARIS • SAN DIEGO SAN FRANCISCO • SINGAPORE • SYDNEY • TOKYO Newnes is an imprint of Elsevier
NewnesisanimprintofElsevier 30CorporateDrive,Suite400,Burlington,MA01803,USA LinacreHouse,JordanHill,OxfordOX28DP,UK Copyright©2005,ElsevierInc.Allrightsreserved. Nopartofthispublicationmaybereproduced,storedinaretrievalsystem, ortransmittedinanyformorbyanymeans,electronic,mechanical,photocopying,recording,orotherwise,withoutthepriorwrittenpermissionofthe publisher. PermissionsmaybesoughtdirectlyfromElsevier’sScience&Technology RightsDepartmentinOxford,UK:phone:(+44)1865843830,fax:(+44) 1865853333,e-mail:[email protected].Youmayalsocomplete yourrequeston-lineviatheElsevierhomepage(http://elsevier.com),by selecting“CustomerSupport”andthen“ObtainingPermissions.” Recognizingtheimportanceofpreservingwhathasbeenwritten, Elsevierprintsitsbooksonacid-freepaperwheneverpossible. LibraryofCongressCataloging-in-PublicationData
(Applicationsubmitted.)
BritishLibraryCataloguing-in-PublicationData AcataloguerecordforthisbookisavailablefromtheBritishLibrary. ISBN:0-7506-7872-0 ForinformationonallNewnespublications visitourWebsiteatwww.books.elsevier.com 04050607080910987654321 PrintedintheUnitedStatesofAmerica InternetExplorer,HyperTerminal,andWindowsaretrademarksofMicrosoftCorporation. DynamicCandRCM34xxaretrademarksofRabbitSemiconductor.Softoolsisthetrademark ofSoftools,Inc.Allothertrademarksarethepropertyoftheirrespectiveowners.Readersshould contacttheappropriatecompaniesformorecompleteinformationregardingtrademarksandregistration.
Dedications KamalHyder TomyparentsRasheedandNajma,whosupportedmyvisionof comingtoAmerica. TomywifeMariam,whosesupportandpatiencegotmethrough thelongnightsofwork. TomyfriendandmentorAnugrah,whoshowedmethroughhislife thatanythingisachievablethroughsincerityandperseverance.
BobPerrin Formymom,whostarteditall. Formywife,whoneverletsup.
v
Contents
Preface....................................................................................................................... xi Organization....................................................................................................................... xi ExampleProjects...............................................................................................................xii Acknowledgments.................................................................................................. xiii Chapter1:Introduction............................................................................................. 1 1.1EmbeddedSystemsandEmbeddedControllers............................................................ 1 1.2EmbeddedSystems—CaseStudies............................................................................... 3 1.3AvailableOff-the-ShelfSolutions............................................................................... 10 1.4SoftwareDevelopmentTools...................................................................................... 14 1.5DesignTrade-offs........................................................................................................ 16 1.6MigrationtoHigherVolumeProduction..................................................................... 17 Chapter2:TheBasics............................................................................................... 19 2.1EvaluatingControllers................................................................................................. 19 2.2DefiningtheProblem................................................................................................... 28 2.3ASurveyofSolutions.................................................................................................. 29 2.4ARabbit’sRoots.......................................................................................................... 36 2.5RabbitinDetail........................................................................................................... 38 2.6InSummary................................................................................................................. 64 Chapter3:StartingOut........................................................................................... 65 3.1IntroductiontotheRCM3200RabbitCore................................................................. 65 3.2IntroductiontotheDynamicCDevelopmentEnvironment........................................ 66 3.3BriefIntroductiontoDynamicCLibraries................................................................. 67 3.4MemorySpacesinDynamicC.................................................................................... 68 3.5HowCodeisCompiledandRun................................................................................. 76 3.6SettingUpaPCasanRCM3200DevelopmentSystem............................................. 79 3.7TimetoStartWritingCode!........................................................................................ 79 3.8What’sNext?............................................................................................................... 91 Chapter4:Debugging............................................................................................. 92 4.1TheZenofEmbeddedSystemsDevelopmentandTroubleshooting.......................... 92 vii
Contents 4.2AvoidDebuggingAltogether—CodeSmart............................................................... 97 4.3CommonProblems...................................................................................................... 98 4.4DynamicCDebuggingTools.................................................................................... 101 4.5IsolatingtheProblem................................................................................................. 105 4.6Run-TimeErrors........................................................................................................ 109 4.7MiscellaneousAdvancedTechniques........................................................................ 111 4.8FinalThoughts........................................................................................................... 115 Chapter5:InterfacingtotheExternalWorld...................................................... 116 5.1Introduction............................................................................................................... 116 5.2DigitalInterfacing.................................................................................................... 116 5.3HighCurrentOutputs................................................................................................ 130 5.4CPLDsandFPGAs.................................................................................................... 141 5.5AnalogInterfacing—AnOverview........................................................................... 143 5.6Conclusion................................................................................................................. 156 Chapter6:IntroductiontoRabbitAssemblyLanguage...................................... 157 6.1IntroductiontotheRabbit3000InstructionSet........................................................ 158 6.2SomeUniqueRabbitInstructions.............................................................................. 178 6.3StartingtoCodeAssemblywithDynamicC............................................................. 180 6.4PassingParametersBetweenCandAssembly.......................................................... 189 6.5Project1:CreatingaDelayRoutine.......................................................................... 196 6.6Project2:BlinkinganLED....................................................................................... 200 6.7Project3:DebouncingaSwitch................................................................................ 205 6.8Project4:DrivingaMultiplexedLEDDisplay......................................................... 211 6.9Project5:SettingUpaReal-timeClock................................................................... 221 Chapter7:InterruptsOverview............................................................................ 225 7.1InterruptDetails......................................................................................................... 228 7.2WritinganInterruptServiceRoutine........................................................................ 236 7.3Project1:Polledvs.Interrupt-DrivenSerialCommunication.................................. 244 7.4Project2:UsingTimerInterrupts.............................................................................. 254 7.5Project3:UsingtheWatchdogTimer........................................................................ 270 7.6Project4:SettingUpaReal-timeClock................................................................... 280 Chapter8:MultitaskingOverview....................................................................... 288 8.1WhyUseMultitasking?............................................................................................. 288 8.2SomeMoreDefinitions............................................................................................ 295 8.3CooperativeMultitasking.......................................................................................... 297 8.4PreemptiveMultitasking........................................................................................... 298 8.5WhattoBeCarefulAboutinMultitasking................................................................ 301 8.6BeginningtoMultitaskwithDynamicC.................................................................. 305 8.7DynamicC’sImplementationofCooperativeMultitasking..................................... 306 8.8DynamicC’sImplementationofPreemptiveMultitasking....................................... 310 8.9Project2:FlashingLEDswithMultitasking............................................................. 312 viii
Contents 8.10Project3:UsingLinuxtoDisplayRealTimeData................................................. 321 8.11Project4:DesigninganAnalogSensorTask.......................................................... 326 8.12BacktotheStateMachinefromProject1.............................................................. 331 8.13FinalThought.......................................................................................................... 333 Chapter9:Networking.......................................................................................... 334 9.1DynamicCSupportforNetworkingProtocols......................................................... 335 9.2TypicalNetworkSetup.............................................................................................. 338 9.3SettingupaCoreModule’sNetworkConfiguration................................................. 340 9.4Project1:BringingupaRabbitCoreModuleforNetworking................................. 344 9.5TheClientServerParadigm...................................................................................... 348 9.6TheBerkeleySocketsInterface................................................................................. 349 9.7UsingTCPvs.UDPinanEmbeddedApplication.................................................... 352 9.8ImportantDynamicCLibraryFunctionsforSocketProgramming.......................... 353 9.9Project2:ImplementingaRabbitTCP/IPServer..................................................... 355 9.10Project3:ImplementingaRabbitTCP/IPClient.................................................... 361 9.11Project4:ImplementingaRabbitUDPServer....................................................... 369 9.12Project5:WebEnablingtheSensorRoutine........................................................... 374 9.13Project6:BuildinganEthernet-ConnectedSprinklerController........................... 384 9.14SomeUseful(andFree!)NetworkingUtilities....................................................... 406 9.15FinalThought.......................................................................................................... 409 Chapter10:Softools—TheThirdPartyTool......................................................... 410 10.1WhoisSoftools?...................................................................................................... 410 10.2TheRabbitWinIDE................................................................................................. 411 10.3SCRabbitOptimizer................................................................................................ 417 10.4SCRabbitSegments................................................................................................. 419 10.5SCRabbit#pragmas................................................................................................. 419 10.6NearandFarFunctions........................................................................................... 421 10.7InlineAssembly....................................................................................................... 423 10.8LibrarySupport....................................................................................................... 423 10.9WinIDE’sSLINKLinker......................................................................................... 424 10.10DebuggingintheWinIDE..................................................................................... 426 10.11MemoryLayout..................................................................................................... 430 10.12RealTimeOperatingSystems............................................................................... 435 10.13EthernetandTCP/IP.............................................................................................. 436 10.14WinIDEandtheBook’sExamplePrograms......................................................... 436 10.15Conclusion............................................................................................................. 437 AppendixA:Rabbit3000A—ExtendingtheRabbit3000’sArchitecture............ 438 AbouttheAuthors................................................................................................. 449 Index....................................................................................................................... 451
ix
Preface Welcome!AreyounewtoRabbitSemiconductor’sproducts?Areyounewtonetworking? Areyounewtoembeddedcontrollerdesign?Thenthisbookisforyou. Thisbookiswrittenbyembeddeddevelopersfortheirpeers.Theauthorsaskedeachother“if wewerestartingtodesignwithanewmicroprocessortoday,whatwouldwewanttoknow aboutit?Howwouldabookhelpusachieveanefficientdesignquickly?”anddevelopedthe bookaccordingly.AnumberofconceptspresentedherearenotjustspecifictotheRabbit 3000microprocessor;theyareequallyapplicabletoanymicroprocessor.
Organization Thebookstartssimpleandbringsreadersalongquicklytoalevelwheretheycanassemble hardware,wigglebits,andblinklights.Thentherealfunbegins—web-enablingembedded controllers. Thefirsttwochaptersintroducethekeyconceptsneededforembeddedsystemdesign. Next,thereaderisgivenanarchitecturaloverviewoftheRabbit3000microprocessorand introducedtoaneasy-to-usedevelopmentenvironment—DynamicC.Simpleandadvanced debuggingtechniquesarecoveredwithexamples. Chapter5explainscommonhardwareinterfacingissues.Sincewebelievethathardware andsoftwareinembeddedsystemsareinexorablywoventogether,weuseamixoffreely availableLinux-basedtoolstoshowhowanalogdatacanberecoveredfromanembedded controllerandanalyzed. Chapters6,7and8takethereaderonasuccincttourofRabbitassemblylanguage,interrupts andmultitasking.Thisiswherereadersfamiliarwithembeddedsystemdesignbutnotwith theRabbitwillprobablywanttostart. Chapter9isacomprehensivetreatmentofhowtobringthewebtoanembeddedsystem. WehavedoneprojectsrangingfromsimpleUDPandTCPclientsandserversonbothPCs andRabbitCoreModulestoadata-acquisitionsystemandanautomatedsprinklercontroller withawebbrowserinterface.ThischapteralsointroducesRabbitWeb,anovelandpowerful scriptinglanguagethatmakesiteasytocreatepowerfulwebinterfaces. Chapter10introducesaverypowerfulandprofessionaldevelopmentenvironmentfor Rabbit3000code—theSoftoolsANSICcompiler.Softoolsbroughtalmosttwodecadesof xi
Preface experiencewithoptimizingcompilers,assemblersandverycleverlinkerstogethertocreate aneasy-to-usedevelopmentenvironment.We’lldiscussitatlength. ThebookcloseswithAppendixAwhichcoverstheenhancementsmadetotheRabbit3000 withthereleaseoftheRabbit3000A.Bothprocessorsarefullycompatiblewithallofthe codeandexamplesusedthroughoutthisbook.
ExampleProjects Theauthorsfirmlybelievethatpeoplelearnbyexample.Thisphilosophypervadesthebook. Eachconcept,onceintroducedanddiscussed,isusedinaproject.Theprojectsrangefrom simpleassemblycodethatblinksanLEDtoaweb-enabledsprinklercontroller. Today,therearemanysoftwaretoolsavailabletoengineers.Recognizingthatnotallengineersareproficientwithalltoolsets,wehaveincludedexamplesinlanguagesrangingfrom assemblylanguagetoJava,Perl,C/C++/C#,andBash.TargetenvironmentsincludeWindows andLinuxandofcoursetheRabbitSemiconductorhardware.Theideaisthateveryreader willlikelytakeawaytricksfortoolsthathere-to-forehavenotbeenintheirtoolbox. Theauthorshavedeliberatelychosensimpleexamples,sothatthereaderwouldfocuson thekeyconceptsinsteadofgettingboggeddownwithimplementationdetailsofcomplex projects.Forexample,theobjectiveofanumberofexamplesisjusttoflashanLED.While thismayseemoverlysimple,flashinganLEDrequirestherightI/Oportstobesetupand therightlogicandtimingtobeinplace.Thesearekeyelementsinmostembeddedsystems designs. OurintentioninwritingthisbookistobringtothereaderoursenseofexcitementforembeddedsystemsdesignaswellasembarkingonanadventurewiththeRabbit3000asourfaithful companion.Wehopeourenjoymentandadventurismwillruboffonyou.Mayyouenjoy readingthisbookasmuchaswehaveenjoyedwritingit. Sincerely, KamalHyderandBobPerrin September2004
xii
Acknowledgments Thisbookhasbenefitedfromcontributionsgreatandsmallfromalonglistofprofessionals. Herewetakeamomenttorecognizeandthankthefollowingindividualsfortheircontributions. NormRogersandCarrieMaha(RabbitSemiconductor) Thankyouforsupportingandencouragingtheauthorsthoughoutthedevelopmentofthisbook andforarrangingforquickandin-depthtechnicalsupportwhentheauthorshadquestions. BillAuerbach(SoftoolsInc) Thankyouforyourtimetoreviewourworkandoffersuggestionsonimprovingit.Butmost ofall,thankyouforwritingthebulkofChapter10—Softools.Whenyouwantajobdone right,gotoanexpert. KellyHall(SaintBernardSoftware) ThankyouforcontributingtheLinuxbaseddataacquisitionprojects(code,dataandprose) foundinChapter5andChapter8. GregYoung(ProgressiveSolutions) Thankyouforhelpingtheauthorsmaintainagoodperspectiveonthisproject. PedramAbolghasem(RabbitSemiconductor) ThankyouforbeingunafraidtotackleanyissueatRabbitSemiconductorforus.Youwere manonpointforusatRabbit.Thankyou. BrianMurtha(RabbitSemiconductor) ThankyouforcontributingmostofthetechnicalinformationaboutdebugginginDynamicC foundinChapter4.Especially,thankyouforwritingtheFASTSERMACS.LIBforthisbook. Readerswillbeusingthesemacrosforyears!Nicework,Brian. LarryCicchinelli(RabbitSemiconductor) Thankyouforreviewingthefirstsevenchaptersofthisbook.Sometimesyouturnedyour editsaroundinjustadayorso.Youansweredalotofquestionsforus,andthisbookwould notbewhatitiswithoutyourinput.Thankyou. EugeneFodor,JoelBaumert,OwenMageeandSteveHardy(RabbitSemiconductor) TheRabbitSemiconductorengineersreallysteppeduptotheplateasreviewersforthelast halfofthebook.Butmostly,youradvice,support,assistanceanddedicationhelpeduswork thoughadifficultpieceofwork—Chapter9.Thankyou.
xiii
Acknowledgments QingyiH.Perrin ForphotographyinChapters1,5and9. RaymondD.Payne(EbaraTechnologies) Forallthehoursyouhavespentresearching,understandingandteachingACsnubbingtechniques.Chapter5andChapter9havebenefitedfromyourexperienceandwisdom.Thankyou forbeinggenerouswithyourtime. ScottHenion(SHDesigns) ForhelpingtheauthorsunderstandtheRabbit3000TimerB,andforgraciouslyallowingus toincludeyourST-timerb.zipandtimerb.ZIPinourCD.Theselibrariesandexampleswillbe usefultomanyreaders. CarolLewisandTiffanyGasbarrini(ElsevierScienceandTechnologyBooks) Formidwifingthisbook. MichaelCaisse ForhiscarefulreviewofChapter9andformoralsupportthroughoutthedevelopmentof thebook. KaranBajaj Forhelpingusverifythenetworkingcode.HisbackgroundwithMicrosoftCorporationand .netdevelopmentreallyhelped. PrameelaMukkavilli Forhelpingusverifythenetworkingcode.HerJavabackgroundwasveryhelpful. BakthaMuralidharan(CiscoSystems) ForreviewingChapter9.HeisasoftwareengineerwithCiscoSystemsandhascontributed towardsthepopularEtherealtool. AamerAkhter(CiscoSystems) ForreviewingChapter9.HeisatechnicalleaderwithCiscoSystemsandhascontributed towardsthepopularEtherealtool. MarcoMolteni(CiscoSystems) ForreviewingChapter9.HeisaSoftwareEngineerwithCiscoSystemsandworksonfuture platformsandIPv6.HetriestocontributebacktoOpenSourcetoolslikeEthereal. EliasKesh(PioneerElectronics) ForreviewingChapter8.HehasdesignedandimplementedanumberofRTOSesandis activewithembeddedLinuxdevelopment.
xiv
1
CHAPTER
Introduction Thisbookfocusesonmethodsandpracticesforembeddedsystemdesign,andittakesatopdownapproachinpresentingthematerial.Thediscussionwillprogressfromquestionsof whatanembeddedsystemis,todetailedexamplesofhowtosolvespecificdesignproblems withRabbitSemiconductor’stechnology. Thischapterexaminesbroadissuessurroundingembeddeddevelopmentanddiscussescommonsolutions.Chapter1isnotconcernedwithspecifictechnologybutratherover-arching issuesofembeddedsystemdevelopment.Thechapternarrowsthescopeofembeddedsystem problemstothosewithwhichtheremainderofthebookisconcerned.
1.1EmbeddedSystemsandEmbeddedControllers Inthe1960s,mini-computersfoundtheirwayintodedicatedcontrolapplications.Someone coinedthephrase“OEMcomputers”todifferentiatethesemachinesfrom“businesscomputers.”Astimepassedandtechnologyshrunk,theexpression“dedicatedcontroller”cameinto vogueandwaspromptlysupplantedby“embeddedcontroller.” Thephrase“embeddedsystem”wascoinedtodescribesystemsthatcontainanembedded controller.Nowadays,allbutthesimplestelectronicdeviceshavesomesortofmicroprocessor inthem.Hencetoday,thephrase“embeddedsystem”describesalmostanyelectronicproduct. Theembeddedcommunityhasadaptedvariousindustry“computer”standardsforusein industrialcontrolsystems.Forexample,thePCIbushasfourcommonformfactors.Thefirst isthe“standard”PCIformfactorfoundindesktopPCs.ThesecondisthestackablePC104+ formfactor.ThethirdistherackmountableCPCI(CompactPCI).ThefourthisthePCI IndustrialComputerManufacturersGroup(PICMG)adaptation. PC104+,CPCIandPICMGareallattemptstoadapta“computer”technologyforindustrialuse. Theelectricalspecificationsarealmostidentical,buttheformfactorsaresignificantlyaltered. Untilrecently,embeddedPCshavebeenspecializedPCs.Forexample,Amproinventedthe PC104formfactortoallowPCstobesqueezedintoaphysicalenvelopemoreconduciveto embeddedapplicationsthanafull-sizeddesktopPCmotherboard.TheseembeddedPCshave beeneasilyidentifiedas“embeddedcontrollers”andarequitedistinctinformfromdesktop computers. In2002,theMini-ITXform-factorx86motherboardshitshelveseverywhere.Mini-ITXwas developedostensiblytoprovideasmallerfootprintfordesktopPCs.Embeddedsystems 1
Chapter1 designersseizedonthelowcost,highvolumeminiaturizedfull-upPCmotherboards forcontrolapplications.Companiesbegan packagingtheMini-ITXmotherboardswith powersuppliesandI/Omixessuitablefor embeddedapplications.Whatwasinitially designedasa“desktopcomputer”isnow servingasanembeddedcontroller.Let’stake acloserlookattheevolutionfromdesktopto embeddedPC. Figure1.1showsaPCmotherboard.Tomake acompletesystem,severalPCIorISAcards Figure1.1:AdesktopPCmotherboardisbig, mustbeaddedtoprovidevideoandI/O.Of andnotveryfunctionalwithoutadditional course,apowersupplyisrequired.Ahard driveisrequiredtostoreanoperatingsystem cards,powersupplyandharddisk. andapplicationsoftware.Enclosuresare availabletorack-mountthistypeofsystem, butmostcasesaredesignedforconsumer use. Figure1.2showsaPC104stack.The systemisshownwithapowersupplycard, processorcardandfiveexpansioncards thatprovidevideo,storage,parallelI/Oand numerousserialchannels.Thesuper-rugged enclosureismadeofathickaluminum extrusionandusesdenserubberyrailsto isolatetheelectronicsfromvibrationand shock.ThePC104stackisanexampleof Figure1.2:APC104stackismorecompactand howPCtechnologywasadaptedtoanemruggedthanadesktopPC. beddedformfactor. Figure1.3showsaJKMicrosystemsMini-ITX basedembeddedPC.Thissystem’sfootprintislittle largerthanacompactdisc.Thesystemhasapower supplyboardthattakes7–30voltsDCasaninput. StorageisprovidedbyuptotwoCompactFlash cards.Theserugged,solid-statedevicesaremore consumertechnologysuitableforuseinembedded systems.Theyarelightweightandrugged.Ifmore storageisrequired,TypeIICompactFlashmechanicalharddrivescanbeused. Figure1.3:TheMini-ITXwasadopted AwatchdogtimerisalsoprovidedonthesamePCB directlyintotheembeddedsystems thatcontainsthepowersupplyandCompactFlash market. 2
Introduction connectors.Thewatchdogtimerisadevicethatmanyembeddedsystemscontaintoimprove reliability.Intheeventofasoftwarecrash,thewatchdogtimerwillresetthesystem. Acompactdiscandharddrivecanalsobeadded.Figure1.3showsthesedevicesinstalled. Bothdevicesweredesignedforlaptopcomputers.Theyarelightweightandtolerateenvironmentalstressesgracefully.Thepopularityoflaptopshaspushedthepricesofthese componentsalmostaslowasdesktopPCcomponents. Theprocessoronthemotherboardisafanlesslowpowerdevice.Thistooiscoincidentally well-suitedtotheembeddedmarket. JKMicrosystemssellstheembeddedPCshowninFigure1.3withanaluminumenclosure. I/Oislimitedtokeyboard,mouse,video,Ethernet,USB,serialandparallelports,butcanbe expandedusingthePCIslotshown.TheoverallsystemislessexpensivethanasimilarPC104 systemshowninFigure1.2. Injustafewyears,theembeddedPCmarkethasgonefromhavingill-suiteddesktopPC technologytohavingexpensivebutruggedPC104technologyandcurrentlytohavinginexpensivecompactruggedPCs.Withthepressuresandeconomicrealitiesofconsumermarkets, PCtechnologiescanbeexpectedtobecomelessexpensive,smallerandmorerugged.The embeddedsystemssectorwillcertainlyadoptthesetechnologiesdirectly. Technology’sperpetualmarchisoblivioustothedelicaciesofhumansemantics.Today,the distinctionbetweenembeddedcontrollerandcomputerisrapidlyblurring.Wecanexpecttosee moreandmore“computer”technologiesadopteddirectlyintoembeddedcontrolapplications. Anengineerfacedwithautomatingaprocessorbuildinganinstrumenthasaccesstoa widevarietyofproducts.Thereareembeddedcontrollersavailableasprintedcircuitboards (PCBs),aspackagedcontrollers,orashundredsofflavorsofembeddedPCs.Thereare microprocessorsandmicrocontrollersthatrangefrom8-pindevicescostinglessthanadollar tomanyhundred-pinballgridarray(BGA)packageddevicescostinghundredsofdollars.If noneofthesedevicessuitaparticularapplication,perhapsthedevelopermightfancyahalfa million-gatefieldprogrammablegatearray(FPGA). Theterm“embeddedsystem”isappliedtoeverythingfromcoffeemakerstocommunications satellites.Allofthesesystemshavesomeformofmicroprocessorlurkingbehindthescenes orchestratingbehavior. Thisbookfocusesmainlyonsmall-tomedium-scaleembeddedsystemsandassociated instrumentation.Thesetypeofsystemssharemanycommonattributes.Therearesensors. Thereareactuators.Therearedesiredbehaviors.Therearehumaninterfaces—ManMachine interfacesorMMIs.Aboveall,thereisanembeddedcontrolleroperatingbehindthescenes, tyingeverythingtogether.
1.2EmbeddedSystems—CaseStudies Tohelpthereaderunderstandthescopeofembeddedapplicationswithwhichthisbookisconcerned,threesystemsaredetailedhere.Thesystemsaspresentedherehavebeensimplifiedfrom theiractualimplementationsbothtoprotectproprietaryintellectualpropertyandforbrevity.
3
Chapter1 Theapplicationsrangefromanunderwatertorquetooltoa30megawattgenerator.Each applicationpresenteditsdesignerswithuniqueproblems.Thethreeprojectssharecommon threads.Eachprojectshowsdifferentmethodsforaddressingspecificcontrolproblems.These projectsgivethereaderaflavorofthediversityintheembeddedcontrolindustry. 1.2.1UnderwaterTorqueTool FourhundredmetersbelowtheNorthAtlanticismostinhospitable.It’salsohometosizable petroleumreserves.Inanincreasinglyaudaciousquestforoil,humanshaverunpipelinesand placedwellheadsdeepundertheocean. Onetaskthatmustbeperformedisthesimpleactofrotatingmechanicalvalvesonthesea floor.Onetechniqueusedistomountahydraulictorquetoolontheendofaremotelyoperatedvehicle’s(ROV’s)manipulator.FlytheROVandtooldowntothesite.Insertthetorque toolinthevalvemanifold.Turnthevalve. Thecombinationofhigh-costequipmentandenvironmentalimpactmakesturningunderwater valvesamoreconsideredtaskthanturningonagardenhose.Ifthewrongvalveisturned,or rotatedthewrongdirection,ormovedthewrongamount,orisover-tightened,orisstuckoris broken,theenvironmentalimpactcanbedisastrousandtheeconomiccostsstaggering. Thesystemdescribedherewasdesignedtorotate28-inchballvalveswithuptoaquartermillionfoot-poundsoftorque. Thetorquetoolisarobotandcontainssensorstomonitorpressures,strain,speedandtemperatures.Theon-boardcomputercommunicatesthroughanumbilicaltothesurfaceship. Electricallycontrolledhydraulicvalvescontrolthetool’sactions. Figure1.4showsablockdiagramofthetool.Thefrontofthetoolhasthecouplerand latches.Underseavalveshaveaportdesignedtocapturetorquetools.Thisportisreferredto asa“bucket.”Thelatchesengagethebuckettosecurethetool.Thisalsoprovidesastationary anchorforthetooltopressagainstasitproducestorqueonthevalve. Toensurethatthecorrectvalveisturned,eachvalvehasadifferentorificegeometry.Much likecommonscrewsrequireaslottedorcrossedscrewdrivertoturnthehead,underwater valvesrequiredifferentcouplers.Thecouplercanbechangedtoaccommodatedifferentvalves. TheROVpilotfliesthetooldowntothevalve.Usingavideocamera,themanipulatorarmis usedtopositionthetorquetool’snoseinthebucket.Next,thetooloperatorsendsacommand tothetoolengagingthelatchessecuringthetoolinthebucket.Ifconditionsareright,the valverotationcancommence. Anembeddedcontrollerinsidethetorquetoolmonitorsconditionsinthetool,providesa communicationlinkwiththeshipanddirectsthetool’sbehavior.Thisparticulartoolusesan industrialPCwithstackableI/Ocards,aPC104stack. TheROVcontainsahydraulicpressureunit(HPU),andthepressurizedfluidisdeliveredto thetoolthroughahigh-pressurehose.Hydraulicpressuredataisacquiredthroughsensors andmonitoredbythePC104stack. Hall-effectsensorsonthegearboxmonitorthemotorandcouplerspeed.Straingaugesmonitortheactualtorqueappliedtothevalve. 4
Introduction Figure1.4: Underwatertorque tooldesignedtoturn 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
ThePC104stackconstantlywatchesthesensors.Ifanyproblemisdetected,thetoolisshut downandtheoperatoralerted. Theinclusionofanembeddedcontrollerinthetoolallowstheimplementationofalgorithms tograduallyrampuptorqueandspeedonthevalve.Italsoallowsforanoperatortocommandthevalvemovementprecisely.Ifavalveisstuck,theoperatorcancommandthetoolto applyspecifictorquesforspecifictimesorangulardistances. ThePC104stacktiestogethernumeroussensorsandactuators,allowinghighforcestobe appliedtounderseavalveswithsafetyandprecision. APCwaschosenastheembeddedplatform.Thedevelopersdesiredamulti-taskingLinux operatingsystem.ThisallowedcodedevelopmenttooccuroninexpensivedesktopPCswith alowinitialinvestmentinsoftwaretools.AnotherbigfactorwastheEthernetconnectivity supportedbyLinux. ThetoolcanbeoperatedthroughanIP(internetprotocol)networkfromageographically remotelocation.Thetooloperatordoesnothavetobeaboardtheshiptocontrolthetool. TCP/IPpacketsareeasilyroutedovertheInternet. Infact,duringinitialdebuggingsessions,thetoolwasdeployedfromashipatseawhilethe torquetooloperatorwaslocatedinacubicle3,000milesaway.Thisconfigurationallowed 5
Chapter1 theR&Dteamtointerrogatethetool,operateitandupdatesoftwareonthetool,alloverthe Internet.ThisarrangementwaslessexpensivethanflyingtheR&Dteamandtheirlabtoa ship. Aderivativedesignofthiscontrollerusedaserialporttocommunicatebetweenthetooland theship.Point-to-pointprotocol(PPP)wasusedtorouteTCP/IPpacketsovertheserialconnection,whilestillretainingthediversenetworkfeaturesofthedesign. 1.2.2IndustrialVacuumPumpforSemiconductorProcessingEquipment Theprocessofturningsiliconwafersintosiliconchipshasmanysteps.Someofthese operationsarecarriedoutinlow-pressureenvironments.Therearevacuumpumpsdesigned specificallytocreatelow-pressureenvironmentsforuseinwaferprocessing. Asiliconwafercanyieldhundredsofindividualdice(orchips).Dependingoncomplexity, eachdiemaybeworthaconsiderablesumofmoney.Eachwaferisworthseveralhundred timesthepriceofadie. Duringprocessing,wafersaregangedtogetherincarriersorcaddies.Eachcaddycarries tensofwafers.Acaddyofsiliconwafersincreasesinvalueasitmovesthroughthefabricationprocess.Bythetimethecaddyishalfwaythroughaprocessrun,itisnotunusualforthe valueofthecaddy’scontentstobeseveralhundredthousanddollars. Siliconfabricationplantoperatorsconsiderit“badform”onthepartofprocesstoolvendors toallowaprocesstool,suchasavacuumpump,toruinabatchofwafers.Considerablecare goesintothedesignofvacuumpumpsdestinedforsiliconfabricationplants. Figure1.5showsablockdiagramofanindustrialvacuumunit.Intheunit,twopumpsare cascadedtodevelopthelowpressuresrequiredbythewaferfabricationprocess.Eachpump isdrivenbyaneight-horsepowerthree-phaseelectricmotor. Thetoolchamberisfilledwithhighlytoxicvaporsreleasedbytheprocesschemicals.Nitrogengasismixedwiththeexhaustgassestodilutethetoxicgasestosaferlevels. Thepumpsrequirewater-coolingtomaintainacceptableoperatingtemperatures.Ifthepump housingistoohotorisunevenlyheated,themechanicalpartsfailtostayintoleranceand leaksdevelop—suchleakscanreducevacuumintheprocesschambers,whichcaninturn upsettheprocesschemistryanddiminishwaferyield. Flowsensorsmonitorthenitrogenandwatersuppliedtotheunit.Adisruptionintheflowof nitrogenisasafetyconcern.Adisruptionintheflowofwateraffectspumpefficiencyand mechanicalwear. Thermocouplesmonitorpumphousingandbearingtemperature.Elevatedtemperaturesindicateexcessivefriction,orreducedwatercirculation.Slightlyelevatedtemperaturesrequirea maintenanceengineertoreviewthesystem,whileseverelyelevatedtemperaturesrequirethe pumptobeshutdown. Theman-machineinterface(MMI)showninFigure1.5consistsofanLCDmountedonthe frontoftheequipmentcabinet.TheMMIhasnofrontpanelcontrols.Theinclusionoffront panelcontrolswouldonlyopenthepossibilityofahumanmanuallychangingthepump behaviorandruiningacaddyofwafers.Theprocessisfullyautomated.Thesilicon-wafer process-toolcontrolsthevacuumpumpthroughthe“controlinterface.” 6
Introduction Process Tool Chamber
Figure1.5:Industrial vacuumpumpused onsiliconfablines.
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
Thecontrolinterfaceisconfiguredtomeettherequirementsofthecustomer.Thisinterface maybeasimpledigitalinterface,orasetofdry-contacts,oroneofthreeserialinterfaces— RS-485,RS-232orEchelon’sLonTalk™. Themaintenanceportprovidesaccesstoapump’sperformancelogsandtoreal-timeoperatingconditions.AservicetechnicianusesalaptopwithanRS-232connectiontoaccessthe pump’smaintenanceport. Thissystemisanexampleofanembeddedapplicationthatrequireslimitedcontrolfunctions butnumerousmonitoringandloggingfeatures.Theembeddedcontrollercanturnthepump motorsonandoffbuthasnoabilitytocontrolthespeedofthemotors.Thetemperatures maybemonitoredandloggedbutthereisnovalveaffordingelectroniccontrolovercoolant. Theperformanceofthistypeofsystemisfixedbythemechanicaldesign.Theembedded controllercanmonitorconditionsandshutdownthesystemifacriticalfaultoccurs.System performancecanbemeasuredandrecordedforlaterinspectionbyengineers,butcontrol algorithmsaren’tneededaspartoftheembeddedcontroller. Whenitcametoselecting/designingthepump’sembeddedcontroller,theengineerswere facedwithabuildvs.buydecision.Thedecisionfactoredincost,size,availabilityandcomputationalrequirements.Intheend,theengineersoptedtodesignacustomcontrollerfrom thegroundup. Theembeddedcontrollerwastailoredtotheapplication.Extradigitalandanalogchannels wereincludedtosupportfutureexpansion.Thecontrollerservedtheapplicationwellfor manyyears.
7
Chapter1 1.2.3Controllinga30-MegawattGenerator Anembeddedcontrollermayonlybeadiscretesubsysteminalargerelectroniccontrol project.Asanexample,Figure1.6showsa30-megawattnaturalgaspoweredturbine-driven generator.ThesesystemswerebuilttoprovideelectricityinremotelocationsinMexico, CentralAmericaandpartsofSouthAmerica. Variantsofthissystemreplacedthegeneratorwithlargecompressorsorothermechanical equipmentnecessaryforindustrialfacilitiestooperateinremotelocales.However,allvariantsofthissystemusedthesamenaturalgasfeedervalveandvalve-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
Figure1.6:Anautomatednaturalgaspoweredelectricalgeneratingstation.
Theoverallsystemrequiredthemonitoringandcontrolofavarietyofdevicesassociated withthegasturbineandtheaccompanyinggeneratororcompressor.Amaincomputerwas responsibleforallthehigh-leveldataacquisitionandcontroltasks.However,inthecaseof thenaturalgasfeedervalve,aseparatevalve-controllerwasdevelopedtooffloadsomeofthe lowlevelmonitoringandcontrolfunctions. Thevalve-controllerallowedthemaincomputertoissuecommandssuchas“openfeeder valvetox%,”and“emergencyoff.”Thedetailsofcontrollingthevalve’ssteppermotorand monitoringthepositionfeedbackwerelefttothevalve-controller. Figure1.7showsadetailedviewoftheembeddedsystemdesignedtocontrolthenaturalgas feedervalve.Thegasvalvewasmovedwithalargesteppermotorandgearbox.Therewas feedbackindicatingthepositionofthevalve. 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
Figure1.7:Valve-controllerblockdiagram. 8
Natural Gas To Turbine
Introduction Safetyrequirementsdictatedthatthevalve-controllerbelocatedintheconcretecontrol room—seeFigure1.6.Thearmoredandpottedcablethatranouttotheautomatedvalve protectedthewiresfrommechanicaldamageandpreventednaturalgasfromseepingupthe cableandintothecontrolshack. Thesystemwasdesignedinthelate1980s.Thevalve-controllerconsistedofthreerackmountedcards.Thefirstcardcontainedthepower-supply.Thesecondcardcontaineda customcontrollerdesignbasedonthe8051microcontroller,andthethirdcardwasacommerciallyavailablesteppermotordriver. Embeddedsystemsintegratedintolargerprojectsarenolesschallengingtodesignthanstand alonedesigns.Theengineerisalwaysfacedwithbuildvs.buydecisions.Aswasdonewith thisproject,aportionofthedesignmaybebought(thesteppermotorcontroller)whilethe restofthesystemcanbebuilt.Theprocessofputtingtogetherthepiecesiscalled“system integration.” Somesystemscanbepiecedtogetherentirelybyasystemsintegrator.AnexampleisadesktopPCbuiltupbyanelectronicsenthusiast.Themotherboard,PCIcards,case,fans,power supplies,diskdrives,keyboards,mouseandmonitorareallpurchasedcomponents.The assemblyisfairlymechanical. Othertypesofsystemsintegrationprojectscantakealotofeffort.Forinstance,putting togetherdevicesthatdon’tcommunicateusingsameprotocolsordon’thavewellthought outandwell-designedinterfacesmayconsumelotsoftime.Moreover,testingofsystemsor componentsanddeployingpilotprojectscanbenontrivialtasks. Systemintegrationcanreducedevelopmenttime,andinsmallvolumeproductionitcansave money.Inthecaseofthevalve-controller,buyingthesteppermotorcontrollersaveddesigntime. 1.2.4CaseStudySummary Eachofthesecasestudiesappearsonthesurfacetobequiteunique.However,theyallshare commondesignchallenges.Sensorsmustbemonitored.Communicationwithotherdevices isrequired.Actuatorsmustbeenergized.Controldecisionsmustbemadelocallywhilesome decisionsaremaderemotelybyhumansorprobablybyanothercontrolsystem. Thesensorytasksfortheseapplicationsareasdemandingasmanyinstrumentationprojects. Signalconditioningforapressuretransducerinahydraulictorque-toolissimilartothecircuitryformonitoringpressure(orlackthereof)inavacuumpump. Communicatingwithavalvecontrollerviaacommand-lineinterfaceoverRS-485isvery similartoRS-485communicationinasiliconwafer-processingtool.Thecommandsand communicationsprotocolsmaybedifferentbuttheunderlyingphysicalinterfaceisthesame. Theunderlyingtechnologyrequiredfortheseapplicationsiscommontomanyotherprojects. Anengineerthatunderstandstheissuesandtrade-offsinvolvedwiththesecasestudiescan designawidevarietyofembeddedsystems.
9
Chapter1
1.3AvailableOff-the-ShelfSolutions Commerciallyavailableembeddedcontrollerscanbedividedintofourcategories—packaged controllers,board-levelcontrollers,coremodules,andchipleveldevices.Eachhasadvantagesanddisadvantages. 1.3.1PackagedControllers Commerciallyavailablepackagedcontrollershaveanenclosure.Packagedcontrol solutionsoftenhaveI/Otargetedatindustrialapplications.Thecontrollerindustry offersarangeofenclosuresfromvented plastictoexplosion-proofcases.Some packagedcontrollershavegenericMMIs— touch-screens,LCDsandkeypadsarethe mostcommon.Figure1.8showsanexample ofaZ-Worldpackagedcontrollerwitha genericMMI.
Figure1.8:TheOP7200isafull-featured
Packagedcontrollersarethemostexpensive controllerwithbuilt-inLCDandkeypad. ofthefourembeddedcontrollerclasses. Inapplicationsthatarenottoocost-sensitive,thesedevicescanalsoprovidethequickest solutiontoautomationproblems.Thesystemsintegratorneedsonlytomountthecontroller’s enclosure,hookupwiresandwritesomecode. Ofthefourclassesofcommerciallyavailablecontrollers,thepackagedcontrollersarethe mostexpensive,butarealsothemostcompleteandeasytousesolutions.Theircostoften causesthemtobemosteconomicallyacceptableinlow-volumeapplicationswhereacceleratedtime-to-marketandreduceddevelopmentcostscanbetradedoffagainsthigherper-product costsandreducedassemblyoperations. 1.3.2Board-LevelControllers Board-levelcontrollersarethemostdiverse classofcontrollers.Theyrangefrom$2500 PICMG(PCIIndustrialComputerManufacturersGroup)PCstolowcostpostage-stamp sizedcontrollers.Board-levelcontrollers requirethesystemsintegratortoprovide physicalprotectionfortheelectronics.An engineerconsideringaboard-levelsolution hasavastselectionofI/Omixes,processortypes,memorycapacitiesandI/Omixes fromwhichtopick.Figure1.9showsan exampleofafeature-richZ-Worldboardlevelcontroller.
Figure1.9:TheBL2100offers numerousI/OpointsandEthernet. 10
Introduction Board-levelcontrollersrequiremounting,someformofphysicalprotection,andsometimes cooling.Manytimesanequipmentenclosure,suitableformountingtheembeddedcontroller, alreadyexistsinanindustrialsystem.Otherapplicationsmayrequirethedevelopertodesign orpurchaseaseparateenclosure.Someboard-levelcontrollermanufacturersofferoptional enclosuresfortheirboardlevelproducts. Connectingwirestoboardlevelcontrollersrequirescarefulattention.Screwterminalsarea popularchoiceforwiretermination.Figures1.10aand1.10bshowexamplesoftwotypesof screwterminals.Theseareeasytoprototypewithbutcanmakeproductionwiringharnesses difficultortimeconsumingtoattachtothecontroller. Screwterminalconnectorsthatarefixedtothecontrollerboard,asshowninFigure1.9and Figure1.10b,requiretheproductionstafftomountthecontrollerfirstandthentoscrewwires intothecontroller.Thismeansthatpeoplemakingthewiringharnesscan’tcompletethewire terminationuntiltheharnessisinstalledwiththecontroller.Dependingontheproductand manufacturingprocess,thismaynotbedesirable. Screwterminalconnectorsthatarefixedtothecontrollerboardcanalsoposechallengesto fieldservicetechnicians.Atechnicianmaywanttoswapoutacontrollerwithaknowngood unit.Havingtounscrewalargenumberofscrewterminals,makingsurethatalargenumber ofwiresaremarkedforproperreattachment,andthenreattachingallthewirestoanewcontrolleristimeconsumingandpotentiallyerrorprone. Anattractivealternativetoscrewterminalsthatarefixedtoacontrollerboardisaclassof connectorscalled“pluggable”screwterminals.Figure1.10ashowsaZ-Worldpackaged controller(PK2500)withpluggablescrewterminals.Thissystemallowsthewireharnessto beterminatedwithascrewterminalsocketthatplugsintoafixedheaderonthecontroller board.Thisarrangementallowsforrapidassemblyoftheembeddedsystemsaswellasswift anderror-freefieldreplacementoftheembeddedcontroller.Ofcoursethetrade-offforthis convenienceisasmallincreasedcost.
a.
b. Figure1.10:Pluggablescrewterminalsaremoreexpensive butaremoreflexiblethanfixedscrewterminals. 11
Chapter1 Systemsinwhichthecable-harnesseswillbeassembledseparatelyandintegratedintoasystemwillbenefitfromtheuseofpluggablescrewterminals.Completedcable-harnessescanbe easilytested.Finalassemblywillgomuchsmootherifthecompleteandtestedcable-harness issimplypluggedintothePCBmountedheader. Oneramificationofusingfixedscrewterminalsisthataspartoffinalassembly,individual wiresfromtheprefabbedcable-harnessmustbeterminatedtothePCB.Thiscanbeanerror-pronetask.Furthermore,testingofthecable-harnessisquitedifficultwithabunchof loosewiresflyingaround.Thisimpliesthatthefirsttimethecableharnessistestedisinthe completedsystem.Errorswillbefound,anddependingonthewiringerror,seriousandcostly damagecanresult. Fieldservicetechniciansoverwhelminglypreferpluggablescrewterminalstofixedscrew terminals.Whenafewwiresmustbechanged,saytoreplaceasensor,bothtypesofscrew terminalsareequallywellsuited.However,whenacontrollerboardmustbechanged,the timerequiredtocarefullyremove,labelandreinstallalotofindividualwiresismuchlonger thansimplyremovingapluggablescrewterminal. AnotherpopularconnectorchoiceforcontrollermanufacturersistheD-Subminiatureor D-subconnector.Thesehavelimitedcurrentcarryingcapacity,andcanbeexpensivetointegrateintoproductionwiringharnesses.Onthepositiveside,shieldedD-subback-shellsare availabletohelpminimizeEMI. Averyinexpensiveconnectorfoundonboard-levelcontrollersisthepin-header.Depending onthepinsizeandpitch,reasonablevoltageisolationandcurrentcarryingcapacitycanbe realized.Crimppins,insulationdisplacementconnectors(IDC),andmass-terminationribbon cablesareavailableforproductionwireharnesses.Atfirstglance,pin-headersseemabit dubious,buttheyhavefoundwidespreadindustryacceptance. Boardlevel-controllersintheformofedgecards,epoxy-encapsulatedmodules,DIP,SIP orSIMMboardsrequirea“carrier-board.”Thecarrier-boardwillhavetobedesignedto accommodatethepurchasedcontroller,whileprovidingapragmaticmethodofpinningout thecontroller’sI/Otothedevicesandsensorsthatmakeupthecontrolapplication. 1.3.3CoreModules Thethirdproductclassisthecoremodule.Acoremoduleisaphysicallysmallcontroller consistingofacentralprocessingunit(CPU),memory,gluelogicandsimplisticinput/output (I/O).Acoremoduleisbuiltonaprintedcircuitboard(PCB)andissimilartomanyofthe smallboard-levelcontrollers.Theprimaryfeaturedifferentiatingacoremodulefromaboard levelcontrollerisintellectualproperty(IP). Board-levelproductsareproprietarycontrollers.Themanufacturerofthesedeviceswillnot freelylicensethecontrollerdesigns.Anyattempttocopyaboard-levelcontrollerinfringeson themanufacturer’sIPrights. Coremodulesaredesignedtobecopied.Acoremodelisinmanyrespectsapracticalreferencedesign.Anengineercanpurchasealow-costcoremoduleanddevelopacomplete
12
Introduction embeddedapplication.Dependingontheeconomicsof theprojectandwhereitisinitslifecycle,thedesigner canfreelycopythecoremoduledesignandmigrate toalowercost,highervolumechiplevelsolution. Allmicroprocessormanufacturershavereferencedesignsavailable.Manyoftheseare availableondemonstrationorevaluationboards. Thesereferencedesignsaredesignedforuseon anengineer’sdesktop.Bycontrast,coremodules aredesignedforintegrationintoproductionsystems. Coremodulesarecontrollersfirstandreference Figure1.11:RabbitSemiconductorhas designssecond. acoremoduleforeveryoccasion. Figure1.11showsasampleofthearrayof availablecoremodulesavailablefromRabbit Semiconductor.Asofthetimeofthiswriting,Rabbitofferstencoremodules,andhasmore indevelopment. Thesearephysicallysmall,inexpensiveandeasilypluggedintoanapplication-specificcarrier board.Rabbitcoremodulesaregreatforfastproductdevelopment.Thelargeassortmentof availableproductsensuresthereisacoremodulewithfeaturesforalmostanyapplication. 1.3.4ChipSolutions Chiplevelsolutionscomprisethelastclassofcontrollers.Thecomplexityavailablevaries fromverysimpletoextremelycomplex.AllchiplevelsolutionsrequireaPCBtobedesigned.Mostchipsdon’tofferruggedI/O.ThedesignmustprotectthedelicateICsagainst heat,cold,humidity,ElectroStaticDischarge(ESD),over-voltage,backElectroMagnetic Force(EMF),brown-outs,ElectroMagneticInterference(EMI)andotherformsofabuse. Traditionally,engineershaveclassifiedcomputationalchipsasbeingeithermicroprocessors ormicrocontrollers.Thisclassificationisn’tparticularlyuseful,andastechnologyhasprogressed,hasbecomeoutmoded. Theclassicdifferentiationbetweenmicrocontrollersandmicroprocessorshasbeenon-chip ROM.MicrocontrollershaveonboardROM,microprocessorsdon’t.Overtime,integratedI/O hasalsocometobeassociatedwithmicrocontrollers. TheIntel8051ispossiblythemostwidelyrecognizedmicrocontrollerarchitecture.InadditiontoamodestamountofROMandRAM,thisHarvardarchitecturesportsbuilt-inserial ports,counter/timersandanumberofdigitalI/Opins. The8051hasbeensosuccessfulthatcompaniesbesidesIntelhavenowreleased8051cores surroundedbyon-chipperipheralssuchasanalog-to-digitalconverters(ADCs),controlarea network(CAN)interfaces,universalserialbus(USB)portsandothers.Someofthe8051 variantspermitorevenrequireexternalmemory.ThelackofinternalROMinsomeofthese variantstechnicallymakesthemmicroprocessors.
13
Chapter1 Today,microprocessorsroutinelyincludeperipheralsthathavelongbeenidentifiedwith microcontrollers.ConsidertheRabbit3000processor,withsixserialports,real-timeclock, internalwatchdog,built-inpulsewidthmodulator(PWM),quadraturedecodingand56digital I/Opins.Therequirementforexternalmemorymakesitamicroprocessor.TheRabbit’sI/O featuresetmakesitmoreidentifiablewithcontrolapplicationsthanasanumericprocessor. Thetechnologythatfurtherblurstheoldmicroprocessor/microcontrollerclassificationscheme isthefieldprogrammablegatearray(FPGA).PopularCPUdesignsareavailableasFPGAbasedsoftcores.Memorycanbeprovidedon-chiporoff.SomeFPGAfamiliesinclude SRAMcellsthatcanbegangedandinitializedatboottoprovideprogramstorageWhile FPGAsareneithermicroprocessorsnormicrocontrollers,theycanbeconfiguredaseither. Originally,theconceptofamicrocontrollerwasintendedtoconveytheideaofa“singlechip solution.”Thislookedgreatinmarketingbrochures,butseldomworkedoutincopper.The delicatenatureoftheI/OpinsonmostmicrocontrollersrequiredadditionalinterfaceICs, asdidpowerconditioningandresetmanagement. Allbutthesimplestchip-levelapplicationsrequired multi-ICdesigns. Thetrendinchipleveldesignistomakepartsas smallaspossible.This,combinedwiththedemand forhighI/Ocount,andthereforepincount,has pushedICmanufacturerstoofferprocessorsinsurfacemount(SMT)packages.Figure1.12showsthree RabbitSemiconductormicroprocessors.Theseareall SMTpackages.TheRabbit2000isthelargestpackageandistheleastpowerfulpart.TheRabbit3000 Figure1.12:TheRabbit2000and isofferedintwopackages.Thesmallestpackageis Rabbit3000processorshaveenough aballgridarray(BGA)andisshownontheleftof I/OthatonlySMTpackagingisoffered. Figure1.12.
1.4SoftwareDevelopmentTools Justassiliconhasadvanced,sohavesoftwaredevelopmenttechniques.Theolddaysof writingcodeonpunchcards,togglinginbinarybootstraploadersorkeyinginhexadecimal opcodesarelonggone.Thetried,trueandtiresometechniqueof“burnandlearn”isstillwith us,butinagreatlyreducedcapacity.Mostapplicationsaredevelopedusingassemblers,compilers,linkers,loaders,simulators,emulators,EPROMprogrammersanddebuggers. Selectingsoftwaredevelopmenttoolssuitedtoaparticularprojectisimportantandcomplex. Badtoolchoicescangreatlyextenddevelopmenttimes.Toolscancostthousandsofdollars perdeveloper,butthepayoffcanbejustifiablebecauseofincreasedproductivity.Ontheother hand,initialtoolchoicecanadverselyaffecttheproduct’smaintainabilityyearsdowntheroad. Forexample,decidingtouseJAVAtodevelopcodeforaPIC®microcontrollerinacoffee makerisapoorchoice.Whiletherearetoolsavailabletodothis,andprogrammerswillingto dothis,codemaintenanceislikelytobeanissue.OncetheJAVA-wizardprogrammermoves
14
Introduction ontodevelopingcodeforwebsites,itmaybedifficulttofindanotherJAVA-enabledprogrammerwillingtosustainembeddedcodeforacoffeemaker.Equallysillywouldbetousean assemblertowriteafull-upGUI(graphicaluserinterface)-basedMMI. AquicktriptotheEmbeddedSystemsConferencewillrevealawidearrayofdevelopment tools.Manyoftheseareillsuitedforembeddeddevelopment,ifnotforreasonsofscaleor cost,thenforreasonsofcodemaintainabilityortoolstability. Thetwotime-testedindustry-approvedsolutionsforembeddeddevelopmentareassembly andC.Forth,BASIC,JAVA,PLM,Pascal,UML,XMLandaplethoraofotherobscure languageshavebeenusedtoproducefunctioningsystems.However,forlow-levelfastcode, suchasInterruptServiceRoutines(ISRs),assemblyistheonlyrealoption.Forhigh-level coding,Cisthebestchoiceduetotheavailabilityofsoftwareengineersthatknowthelanguageandthewidevarietyofavailablelibraries. Selectingatoolvendorisalmostasimportantasselectingalanguage.Selectingatoolvendor withoutaproventrackrecordisarisk.Ifthetoolprovesproblematic,goodtech-supportwill berequired. Thisinnowayimpliesthatasmallshopisn’tcapableofproducinganexcellenttoolandprovidingresponsivesupport.Forexample,SoftoolsInc.isafirmwithrelativelylowheadcount andanoutstandingrecordoffastandcomprehensivetechnicalsupport.TheirANSIcompliantCcompilerfortheRabbitmicroprocessorisasolidtoolworthyofconsiderationforany Rabbit-basedproject. Sometimes,toolvendorsarelittlemorethannewcollegegradstryingtopawnoffwarmedoverseniorprojectsasdevelopmenttools.Whilethetoolsmaylookgood,dealingwithsuch anembryoniccompanyisarisk. Publicdomaintoolshaveuncertainhistoriesandnoguaranteeofsupport.Theideabehind opensourcetoolsisthatifsupportisneeded,theusercantweakthetool’scode-basetoforce thetooltobehaveasdesired.Forsomeengineers,thisisafinestateofaffairs.Ontheother hand,manyembeddedsoftwareengineersmaynotknow,orevendesiretoknow,howto tweak,forexample,abackendcodegeneratoronacompiler. RabbitSemiconductorandZ-Worldofferauniquesolutiontothetooldilemmafacing embeddedsystemsdesigners.RabbitSemiconductordesignsICsandcoremodules.Z-World designsboard-levelandpackagedcontrollersbasedonRabbitchips.Bothcompaniesshare thedevelopmentandmaintenanceofDynamicC™. DynamicCoffersthedeveloperanintegrateddevelopmentenvironment(IDE)whereCand assemblycanbewrittenandblended.Onceanapplicationiscoded,DynamicCwilldownloadtheexecutableimagetothetargetsystemoveraserialcable.Debuggingtoolssuchas singlestepping,breakpoints,andwatch-windowsareprovidedwithintheIDE,withoutthe needforanexpensiveIn-CircuitEmulator(ICE). BetweenZ-WorldandRabbitSemiconductor,allfourclassesofcontrollersareavailable aswellasacompletesetofhighlyintegrateddevelopmenttools.Librariessupportafile 15
Chapter1 system,CompactFlashinterfaces,TCP/IP,IrDA,SDLC/HDLC,SPI,I2C,AES,FFTs,and theuCOS/IIRTOS. OnofthemostattractivefeaturesofDynamicCisthattheTCP/IPstackisroyaltyfree.This isunusualintheembeddedindustry,wherecompaniesarechargingthousandsofdollars forTCP/IPsupport.IfTCP/IPisrequiredforanapplication,theabsenceofroyaltiesmakes DynamicCaveryattractivetool.
1.5DesignTrade-offs Thestockintradeofengineersismakingtrade-offs.Anembeddedsystemsdesignerhas manyoptionstoweigh.Strikingabalanceforagivenprojectdependsprimarilyontheanticipatedproductionvolumeofaproject. Aproductthatisgoingtobemanufacturedbythemillionswillhaveanentirelydifferentset oftrade-offsthanaproductthatisgoingtobemanufacturedinthethousands.Lowvolume productionhasyetadifferentsetoftrade-offs. Somethingthatwillhavehighproductionvolumeslikeacellphonewillbeoptimizedforthe lowestpossiblemanufacturingcosts.Upfronttoolingforpackagingwillbemorethanpaid forbythehighvolumesales.Customliquidcrystaldisplays(LCDs)willbemade.Application-specificintegratedcircuits(ASICs)maybeconsidered. Aproductproducedinmediumquantities,saythetens-of-thousands,willalmostcertainly bedesignedusingsurfacemounttechnology(SMT).Designerswillprobablystayawayfrom usingoff-the-shelfcontrollersthatcomeprepackagedorasaprintedcircuitboard(PCB). Spendingthetimeupfronttodeveloporlicenseacontrollerdesignwillpayoffoverthe courseofproduct’slifetimeinmanufacturing.ASICsprobablywon’tbecosteffectivebecausetherequiredminimumquantitieswon’tbereached.Thistypeofproductwillbenefit fromachip-levelsolution. Aproductproducedinlowquantities(uptohundredsofunits)hasanumberofotherchallenges.Manysurfacemountpartsareavailableonlyonreelsofthousandsofparts.Many small,lowvolumeproductsbenefitfromaready-madeembeddedcontroller. Thisbookdealswithprojectsthatwillbeproducedinlowtomediumquantities. Manyproductsthataspiretomediumquantityproductionareforcedbymarketconditions tostartoutbeingproducedaslowquantityproducts.Overtheyearsthishasbeenaddressed primarilyintwoways,neitherofwhichhasbeenentirelysatisfactory. Somecompanieshaveoptedtodesigntheproductsinitiallyforlowvolumeproduction.Once marketdemandjustifieshigherproductionvolumes,theproductisredesignedformedium productionvolumes.Thisapproachincurstwodesigncyclesandtheassociatedcostsandrisks. Othercompanieshaveoptedtodesigntheproductsinitiallyforhighervolumeproduction. Thisapproachcostsmoretimeandmoneyupfrontinthedesignandduringthelow-volume phaseoftheproduct’slife,therebyincreasingmanufacturingcosts.
16
Introduction
1.6MigrationtoHigherVolumeProduction Iftheinitialdesignincorporatedpurchasedembeddedcontrollers,thenmigrationtofully assembledcustomboardsislikelytobearduous.Eliminatingtheoff-the-shelfcontrollerfrom thenewdesignwillreduceproductioncostssimplybecausethecompanyisnolongerpayingthemarkuponthepartsandthe“valueadded”bythecontrollervendorforthepurchased controller.However,ifcareisnottaken,bugscanbeintroducedintothenewdesign.Seldom dooff-the-shelfcontrollercompaniesprovidecompleteIPtosupportthistypeofmigration. Inmanycases,themigrationphaseisnevercompleted.Theproductissimplyproducedin higherquantitieswiththelowvolumedesign.Thecompanylosestheopportunitytocapitalizeontheintrinsicsavingsofferedbyhighervolumeproductiontechniques. Thesecondapproachtothemigrationproblemhasbeenforcompaniestospendthetimeand moneyupfronttodesigntheproductspecificallyformediumproductionquantities.This approachoftenextendstheinitialdevelopmenttime—sometimesbymonths. Animportantconsiderationformanyprojectsis“time-to-market.”Beingabletoprovidea workingproofofconceptforinvestorsora“prototype”forapotentialcustomeroftenwill makeorbreakaproject—orcompany. Thisapproachwillincurdecidedlyhighermanufacturingcostswhiletheproductisinthelow volumeproductionphase. Thefirstapproachputsoffdevelopmentissuesandcostsuntilaproductreachesasufficient levelofproductiontowarrantredesign.Butdesignissuesandreverseengineeringofoff-theshelfembeddedcontrollerscansandbagthemigrationphase.Thesecondapproachspendsall themoneyandtimeupfronttodevelopaproductillsuitedtothemanufacturingscalethatit willbeconfinedtoforthefirstportionofitslife. Neithersolutioniswhollysatisfactory. Severalyearagoapioneeringembeddedcontrollercompany,Z-WorldofDavis,California, tookalonghardlookathowtohelpcustomersdeveloptheirapplicationsquicklyandina fashionsuitedtolowproductiontechniques,whileaffordingthesamecustomersaseamless migrationpathtohigherquantityfullycustomdesigns.Thesolutionwasdubbedthe“Smart Core™.”Thus,thecoremodulesolutionwasborn. AcoremoduleisasmallPCBcontainingaCPU,memorysubsystem,resetandwatchdog circuits,powersupervisors,andfirmware.ThecoremoduleprovidesI/Osignalsonheaders suitableforinsertionintosocketstripsonthecustomer’stargetsystem.Thus,adevelopercan purchaseafullyfunctionalcontrollerandimmediatelyplugitintotheapplication. Whenthecustomerdecidestomigratetoamediumvolumeproductionboard,Z-Worldwill licensealltheIPassociatedwiththecoremoduletothecustomerforinclusioninthenew “singleboard”orintegrateddesign.Thisprovidesasmoothlow-risktransitionforthedesignerbetweenthelowvolumeproductionandmediumvolumeproductionphase.
17
Chapter1 Seeingtheneedforimprovedmicroprocessorsintheembeddedmarket,Z-World’sCEO, NormRogers,foundedRabbitSemiconductortocarryoninnovationintheembeddedcontrollerindustryatthechiplevel. Today,RabbitSemiconductorproduceschipsandcore-moduledesigns.Rabbitisbuilton Z-World’sexperienceintheembeddedsystemsarena.ThisallowsRabbittotailorICdesigns tomeettheneedsoftheembeddedsystemdesigner.PainstakingresearchgoesinRabbitchips tooptimizethemforembeddedCcode.TheRabbit’sI/OmixisbasedonZ-World’sdecades ofmarketexperience. Z-Worldcontinuesasaforceintheembeddedcontrollerindustry.ItofferstheRabbitcore modules,butthefocusisonfull-featuredcontrollers.Thesecontrollersoffertheembeddedsystemdesignerasmorgasbordofquarter-VGAtouch-screens,high-currentI/Opoints, protecteddigitalinputs,analoginputs,analogoutputs,industrialrelays,andallmannerof communicationbusses.Thesetypesofcontrollerscanoftenbeusedtoprovideacomplete controlsolutionforlowvolumeapplications. EmbeddedsystemdesignerscannowpurchaseRabbitmicroprocessors,Rabbit-basedcore modulesorfull-featuredcontrollersfromtheRabbit/Z-Worldfraternity.Alloftheseproducts sharecommondevelopmenttoolsandIP.Thismakesdesignmigrationsornewdesignvariantsquick,easy,lowcostandlowrisk. ThenextchapterexamineshowtheRabbitsolutionsuitestacksupagainstthemanysolutions availabletoday.
18
2
CHAPTER
TheBasics 2.1EvaluatingControllers Embeddedsystemdesignboilsdowntomonitoringsensorsandactuatingdevices.Depending onthecomplexityofthedesiredbehavior,anembeddedcontrollermaynotberequired. Insomecasesasensormaybeadequatetocontroltheactuator.Inthesesituations,controllers areredundant.Forexample,ahouseholdlightswitchcandirectlycontrolalamp.Thesetypes ofsystemsarenotembeddedsystems. Somesystemsrequirecontrollogic,butnotnecessarilyamicroprocessorormicrocontroller. Ifthesystem’sdesiredbehaviorcanbeimplementedwithsimplecombinatoriallogic,then thesystemisnotconsideredanembeddedsystem. Ifthecontrollerrequiressequentiallogicthentheapplicationmayrightfullybecalledan embeddedsystem.Afterall,amicroprocessorisjustaglorifiedfinitestatemachine(FSM). Forexample,adigitallockmaybeimplementedwithanFSM.Formanyyears,aprogrammablelogicdevice(PLD)wastheusualtoolforbuildinganFSM.Inthepast,PLDswere dramaticallylessexpensivethanmicrocontrollers.However,microcontrollerpriceshave droppedtothelevelthatmanyengineershaveswitchedfromPLDstosmallmicrocontrollers evenforsimpleFSMdesigns.Thesimplicityofprogrammingamicrocontrolleroutweighs theevershrinkingcostadvantageaPLDmayhave.AsPLDsage,obsolescenceisaconcern. Manyfactorsbearontheselectionofacontroller.Performance,costandavailabilityarethe mostoftenbandiedfactors.Developmenttools,time-to-marketandproductsustainability mustalsobeconsidered. 2.1.1PerformanceandCost Theoverarchingcriterionwhenselectinganembeddedcontrollerisperformance.Ifacontrollercannothandletherequiredtask,thenthatcontrollermustbediscarded. Aclosesecondtoperformanceiscost.Ifacontrollerisqualifiedbuttooexpensive,thenit isapoorsolution.Amantraforengineersmustbe“Ifwecan’taffordthesolution,thenit’s notasolution.” Comparingtheperformanceandfeaturesofcompetingcontrollersisalmostalwaysanapples- to-orangescomparison.I/Omixesincompetingmodelsareseldomequivalent.Somecontrollershaveinternalmemory,someexternal,andsomeboth.Deviceswithinternalmemorymay havedifferentprogrammingmodels.Comparingcontrollers,chiplevelorboardlevel,isneithersimplenorscientific.Selectingacontrollerisanexerciseintheartofriskmanagement. 19
Chapter2 Considerthecaseofasimpleapplicationrequiringonlyafewhundredassemblyinstructions andtenI/Opinswithananticipatedproductionrunof1000units.Theapplicationcouldbe implementedinasmallFPGA(FieldProgrammableGateArray)asabigFSM.TheapplicationcouldbeimplementedwithasmallmicrocontrollerlikeaPICprocessorfromMicrochip Technologies,orwithanAVR®fromAtmel.Theapplicationcouldalsobeimplementedwith adesktopPCbyusingthePC’sparallelprinterport. TheFPGAsolutionisoverlydifficultformostengineers,andFPGAsaresomewhatpricey.Ifthe projectisa“one-off,”thePC-basedsolutionisperhapsviable.Assumingthattheapplicationwill requireaproductionrunofathousandunits,thePICorAVRsolutionlooksmoreattractive. Forthesakeofthisexample,assumetheAVRAT90S1200solutionis$0.75moreexpensive thanaPIC16C55-basedsolution.Ifeachpartisequallycapableofperformingthecontrol task,whichpartshouldbechosen?Thecheaper? Theansweris“itdepends.”TheAVRandPICsolutionsarenotapples-to-apples.Table2.1 listssomefeaturesofeachpart.Themostglaringdifferenceisintheprogrammingmodel. ThePICsolutionisOTP(onetimeprogrammable)whiletheAVRpartisFlash-based. Table2.1:PICandAVRhavesimilarfeatures. Feature DigitalI/Opins Onboardprogramspace(words) Onboarddataspace(bytes) EEPROM(bytes) Counter/timers ExternalInterrupts Maxclockspeed MachineCyclesperinstruction Clockcyclespermachinecycle InSystemProgrammable Programmingmodel
PIC16C55 12 512 24 0 1 0 40MHz 1or2 4 Yes OTP
AVRAT90S1200 15 512 0 64 1 1 12 1or2 1 Yes Flash-based
Thequestionthatthedesignermustansweris:willtheextra$0.75fortheAVR’sFlash-based programmingmodelbuyanythingusefulfortheproject?Twofacetsoftheproduct’slife cycleneedtobeconsidered—productdevelopment,andproduction. Duringproductdevelopment,codemustbewrittenandloadedintoatargetsystem.IfOTP partsareused,eithertheICmustbesocketedinthedevelopmentplatform,ortheproject engineersmustbeexpertsatsoldering/removingICsinthetargetsystem.Anotherapproach todevelopingforOTPdevicesistouseanin-circuit-emulator(ICE). AnICEallowsexecutablecodetobeloadedintoitfromahostplatform,usuallyaPC.The ICEalsoplugsintothetargetsystem,usuallyinasocketintendedfortheactualmicrocontrollerormicroprocessor.TheICEexecutesthecodewhilewigglingthesignalsonthetarget systemjustliketheactualmicrocontrollerwould. 20
TheBasics ForsimpledeviceslikethePIC16C55,anumberofexcellentICEtoolsareavailable,most under$1000.Forfasterprocessors,theauthenticityofthesignalwigglingcanbecompromisedbyhowtheICEconnectstothetarget’ssocket.Forcomplicatedprocessors,anICE maynotbeabletoemulatethetargetprocessorinrealtime.However,forourPIC-based example,anICEisareasonablesolution. Iftheproducthasmultipledevelopersworkingonit,chancesarethateachengineerwillwant anICE.Thismaymeanspendingseveralthousanddollars“extra”onemulatorsforthedevelopmentteam. Eveniftheproductonlyhasonedeveloper,iftheICEcosts$750thenthePIC’ssavingsover theAVRarenegated.Theanticipatedproductionwasonlyonethousandunits.Ofcourse, afterthedevelopmentisdone,theengineeringgroupwillstillhavetheICE.Thismayor maynotbeuseful,dependingonwhetherasimilarflavorofPICmicrocontrollerisusedfora subsequentproject. Theproductionphaseoftheproduct’slifemaybenefitfromtheAVR’sFlash-basedISPprogrammemoryifabugisdiscovered.Bugscropupincodeallthetime.Industryexpertslike toplayatquantifyinghowmanybugscanbeexpectedinaprojectgiventhenumberoflines ofcode.Let’ssaythatbasedonexperienceortradepublicationsorindustrystudies(orwhich waythewindblowsinSiberia),webelievewecanexpectoneseriousbugforeverythree hundredlinesofassemblycodeinitiallyreleasedtoproduction. Initially,westatedthattheapplicationwouldrequireafewhundredinstructions.Thatimplies thatwebelievethatwecanexpectoneseriousbugtobeinthecodereleasedtoproduction. Now,thatdoesn’tmeanthattherewillbeabug.Itjustmeansthatwethinkthereislikelyto beabug. IftheFlash-basedAVRisusedinproductionunitsandabugisfound,thentheexisting inventoryofassembledboardscanbereprogrammedwithouthavingtodesolderparts.Units alreadydeployedmaybefieldupgradediftheyweredesignedtobe.RMAs(returnedmaterialsauthorized)canbereprogrammedquicklywithoutsoldering. ReprogrammingtheAVRincircuitmayrequiretheadditionofanextraheaderandafew passivecomponentstothePCB.Asoftheyear2004,componentinsertioncostsarerunning around$0.12.Iffourextracomponents(aheaderandthreeresistors)arerequiredtoreprogramtheAVR,thenthe$0.75costincreaseoftheAVRoverthePICisfurtherincreasedby $0.12×4=$0.48plusthecostofthefourextracomponents(assume$0.09). FortheproductionphaseoftheproducttoseethebenefitsoftheAVR’sISPflash,theproduct costmustincreaseoverthePICsolutionby$0.75+$0.48+$0.09=$1.35. Aninnovativeengineermightbeabletoeliminatetheneedfortheextracomponentsby designingorbuyingaclip-onprogrammer.Thismayaddcosttotheproductdevelopment phasefortheresearchtoidentify,purchaseandtesttheclip-onprogrammer. Inthisexample,theengineerselectingtheprocessormaywellreasonthatthe75cent or$1.35differenceinpartscostover1,000projectedunitsisworthhavingtheabilityto reprogramdefectivecontrollersorupgradeexistingproductswithnewfeatures.Theengineer
21
Chapter2 mayalsofeelthattheAVR’sISPflashwilleliminatetheneedforanICE,thussavingmore money.ThisengineerwouldselecttheAVR. AnotherengineermaydecidethatthedebuggingfeaturesofanICEareindispensablefor developersandthereforethecostofanICEforthePICsolutionwouldbebalancedbythe costofanICEfortheAVR.Thisengineermayfurtherreasonthattheproductionprojection of1000unitsislikelytobewrongandthatmanymoreunitswillbebuilt.Hence,reducing per-unitcostbecomesmoreimportant.Furthermore,hemaygamblethatreleasedcodewill bebug-free.ThisengineerwouldselectthePIC. Ineventhissimplestofexamples,twochainsofthoughtleadtodifferentconclusions.The selectionofacontrollerisseldoma“cutanddried”process.Itisanexerciseintrade-offsto managerisk. 2.1.2PerformancebyBenchmark Attemptshavebeenmadetoprovidecomparativerankingsofprocessorperformance.Benchmarkalgorithmsexist.Mostweredevelopedtocompareperformanceofdesktop,minior mainframecomputers.Theapples-to-orangesworldofembeddedplatformsmakestraditional benchmarksallbutirrelevant. Benchmarksareusefulifthehardwareplatformsaresimilar.WhenpurchasingadesktopPC itmightbeusefultoknowhowfastanumericsimulationoraCADrenderingpackageor evenagraphicsintensivegamewillrun.Embeddedplatformsaresodramaticallydifferent thatdevelopingameaningfulbenchmarkthatwillrun“identically”ondissimilarhardware platformsisnearlyimpossible. TheInternetnewsgroups,suchascomp.arch.embedded,areriddledwithtediouspedanticdiscussionsoftheprosandconsofbenchmarks.Flamewarsaboundregardingthelegitimacyof howsomegrouporanotherimplementedaso-called“standard”benchmarkalgorithm.This polemicnatteringcansoundallhoity-toity,buttherealityisthatchoosingacontrolsolution isnotsomethingthatcanbedonewithbenchmarks. Digitalsignalprocessing(DSP)istheoneareawherespecializedandrelevantbenchmarks havebeendeveloped.ThisstemsfromthefactthatDSPsimplementingstandardfiltertopologiesareinlargepartcomputationallyanapples-to-applescomparison.Thisisaresultofthe factthatthefilteralgorithmsarestraightforward.Ontheotherhand,iftheDSPisgoingto performtasksotherthansimplefiltering,thentheadditionalfeaturesoftheDSPs,suchasI/O accessandinterruptoverhead,maycomeintoplay,andbenchmarkswillnotbeofmuchhelp. Embeddedcontrollersdifferinfeaturesetandimplementationsodramaticallythatembedded benchmarkingismoreanartthanascience.Controllerselectionencompassessomuchmore thanpurecomputationalhorsepowerthatanybenchmarksarenearlyirrelevant. Aboutthebestthatcanbesaidforbenchmarksisthatwithcarefulscrutiny,insomeapplications,theymaybeofassistanceincomparingcomputationalperformance. 2.1.3Availability Controllers,betheypackaged,board-levelorchiplevel,areassusceptibletomarketforcesas anyothercommodity.Manyembeddedproductshavelifecyclesofadecadeorevenlonger. 22
TheBasics Theengineerselectingacontrollermustconsidertheavailabilityofthesolutionforthedurationoftheproduct’santicipatedlifecycle. Forexample,therearenumerousproductsonthemarkettargetedatthecellphoneindustry. Thelow-power,smallsizeandlowcostofthesepartsmakesthemattractivetoembedded designers.Butbeforesettlingonachip-de-jour,considerhowfastthechip’scoremarketis changing.Newgenerationsofcellphonesarebornabouteverysixmonths.Doesitmake sensefortheICmanufacturerstocontinuetoproduceanICafterithasbecomeobsoletein thetargetmarket?No. OneofthereasonsthisbookusesRabbitSemiconductor’smicrocontrollersandmodulesis thatRabbitiscommittedtothelong-termavailabilityoftheirICs.RabbitSemiconductor grewoutofZ-World,aleadingmanufacturerofembeddedcontrollers.Overtheyears, Z-Worldhadtomanagenumerousend-of-life(EOL)announcementsfromvendors. AchipgoingEOLisalwaystroublesomeforcompaniesusingthechip.IftheICisacommoditypart,likealogicchip,simplyqualifyinganewvendormaybefairlyeasy.IftheICisa single-sourcepart,likeareal-timeclockorCPU,thenoftenthebestthatcanbedoneisaonetimelifetimebuyoftheparts.Thecashoutlayforthiscanbecripplingforasmallcompany. Anypartcanbedesignedoutofasystem,butdesigncyclesareslowandincurbothfinancial costsandopportunitycosts.Thesecostscanbedifficulttobearforanycompany.Nobody wantstorevisitanoldandsuccessfuldesignsimplybecauseavendorhasdiscontinuedapart. Anotherveryrealissuetocontendwithisleadtime.Somemicroprocessorchipshavephenomenallylongleadtimes.Certaincompanieshaveseriouslybadreputationsfordelivering productontime.Somecompaniesmakearunofcontrollersonlyonceortwiceayear.Ifthe partshavebeenallocatedinadvancetolargecustomers,thensmallercompaniesmaybefaced withsix-monthleadtimes. OneCalifornia-basedengineerisfondofrelatingastoryofhowhisfirmdesignedaninstrument aroundaJapanese-mademicrocontroller.Theinstrumentwasenteringtheadvancedprototyping phaseandtheengineerwasgiventhetaskoforderingpartstobuildadozennewprototypesfor testing.Allofthepartsfortheinstrumentwerereadilyavailableexceptthemicrocontroller. TheJapanesemanufacturerhadproductionrunsscheduledeverysixmonths,withfixednumbersofpartsscheduledtoshiptotheUnitedStates.USdistributionchannelshadallocatedall ofthepartstolargecustomers.Thecompanythatonlyneededtwelvemicrocontrollerswas quoteda154-weekleadtime(yes,threeyears). Tosolvetheimmediateproblemofbuildingandtestingprototypes,theengineerarranged tohavesomeexistingmicrocontrollersremovedfromolderprototypesandplacedinthe advancedprototypes.Hestillwasfacedwiththeneedtoacquireseveralmoremicrocontrollerstofinishtheproject.DesperateastheUScompanywas,theycouldnotfindanyofthe Japanesemicrocontrollersforsale.Intheendtheyidentifieda$500evaluationboardwiththe $20microcontrolleronit.Theypurchased$3000inevaluationboardstogettheremaining $120worthofmicrocontrollerstheyneeded.AnadditionalroundofdevelopmentwasscheduledtoreplacetheJapanesebuiltmicrocontrollerwithamoreavailabledevice.
23
Chapter2 Ifanengineersettlesonapackagedorboard-levelcontrollersolution,thenthevendor buildingthecontrollermustmanagethesupplychain.Ifthecontrollervendorisnotwellcapitalized,maintainspoorrelationshipswithsilicondistributors,orisinexperiencedinsupply chainmanagement,leadtimesonthepackagedorboard-levelcontrollercanvarydramatically. InChapterOne,anexampleofanunderwatertorquetoolwasgiven.Beingspecializedtools, onlyafewofthesesystemsweretobeproduced.Whenthedesignteamcompletedtheprototypeandthesecondtoolwasreadytobemanufactured,thetorquetoolcompanyattemptedto orderasecondPC104motherboard.ThePC104vendorhadplannedpoorlyandwasnotonly outofstock,butthelead-timewasquotedatsomeghastlynumberlikefivemonths.After manylongandheatedconversations,anengineeratthePC104vendorfoundashop-queen andshippedittothetorquetoolcompany. Ashop-queenisaboardthathasspentalotoftimein“theshop.”Inmanyindustriesthiscarriesthestigmaofalemon.Ashop-queeninthisinstancewasaboardthatwasusedinternally fortestingandexperimentationinthemanufacturer’sfacility.WhenthePC104boardarrived atthetorquetoolcompany,theboardhadbentconnectorsandalotofdust.However,aftera bitofcleaning,leadstraighteningandrigorousevaluation,itwasdeemedfunctional,placed inaproductiontorquetool,wasshippedandworkedfine. Therearetwomoralstothistale.First,ifthecontrollersupplierisineptatmanagingtheir supplychain,thesystemsdesignerwillbearthebrunt.Second,evensomethingas“standard” asaPC104embeddedPCisnotalwayseasilysecond-sourced. TheproductiondepartmentsatZ-WorldandRabbitSemiconductorhavenearlytwodecades ofexperiencemanagingvolatilesupplychains.CustomersthatuseZ-WorldorRabbitcontrollersdon’thavetoworryaboutleadtimesonindividualparts.ThefolksatZ-Worldand Rabbitworryaboutthat.TheircommitmenttoproductavailabilitymakesZ-Worldanexcellentchoiceforpackagedorboard-levelcontrolsolutions.ThesamecanbesaidforRabbit Semiconductorinregardtochip-levelandcore-modulesolutions.Bothcompanieshave consistentlyshortleadtimesfortheirproducts,usuallyshippingfromstock. 2.1.4ToolsandTime-to-Market Seldomdoengineershavetheluxuryoflongdevelopmentcycles.Even“big”projectspack theworkintothetightest,mostoptimisticschedulethatcanbecookedupbymanagement. Thereisnevertimebudgetedfor“fightingwithanddebuggingdevelopmenttools.”Selecting acontrollersolutionthathasfirstclasstoolsisdesirable. Toolscanroughlybebrokendownintohardwareandsoftwaretools.Hardwaretoolsinclude schematiccapture,PCBlayout,circuitsimulation,andHDL(hardwaredescriptionlanguage) compilers.Softwaretoolsincludecompilersthatgeneratecodeforthecontroller’sprocessor, assemblers,simulators,andemulators/debuggers. Theaxiom“yougetwhatyoupayfor,”impliesthatgoodtoolswillbeexpensive.Surprisingly,thisisn’talwaystrueofsoftwaredevelopmenttools.Theaxiom,however,seemstohold fastforhardwaretools. 24
TheBasics Z-World,RabbitSemiconductorandSoftoolseachselloutstandingcompilersforlessthana thousanddollars.TechnicallyrefinedyetinexpensivedevelopmenttoolsmakeRabbit-based embeddedcontrollersacost-effectivesolutionformanyapplications. DynamicC™providesacompleteintegrateddevelopmentenvironment(IDE)withaCcompiler,assembleranddebugger.Thevastlibrariesareprovidedinsourceformsoengineerscan adaptstandarddriverstocustomapplications. SoftoolsoffersanANSI-complianttoolsetfeaturinganIDEwithaCcompiler,assemblerand debugger.Completelibrariesarealsoprovided. DynamicC™andSoftoolsbothcommunicatewiththeRabbitprocessorsthroughaserial interface.Bothtoolsofferallthedebuggingcapabilityneededbydevelopers.Thissimple interfacemeansnoexpensivehardwareprogrammers,emulatorsordebuggersareneeded. Rabbit’sinnovativecoremodulesgivesystemdesignersaready-madeelectronicheartcomprisedoffine-pitchsurfacemount(SMT)componentsallonacompactboardwitheasyto accessthrough-holeheaders.ThecoremoduledesignsmaybefreelycopiedtoacustomPCB inproduction,ifrequired. Thecoremoduleshave“development”boardsavailable.Thesedevelopmentboardssporta prototypingareaaswellassomecommonperipherals(LEDs,switches,powersupplyand thelike).Rabbitcoremoduleboardsdifferfromothermanufacturers’developmentboardsin theirpracticality. ManyCPUvendorsofferdevelopmentboardsthatarenoteasilyadaptedtouseincontrol applications.Signalsareonlypinnedoutonheaders,therearenoprototypingareasandthe boardsareunreasonablyexpensive.Rabbit’sdevelopmentboardsareusefultoolsforengineersandnotsimplymarketingaids. InadditiontotheRabbitcoremodules,Z-WorldoffersRabbit-basedboard-levelandpackaged controllers.Thisone-stop-shopforinexpensivesoftwaretoolsanddiversehardwareplatforms setsZ-WorldandRabbitSemiconductorapartfromotherembeddedcontrollervendors. Goodhardwaredevelopmenttoolsareexpensive.Themorecomplexthecircuit,themorean engineerwillwanttohavea“good”layouttool.Forexample,ifacircuitonlyhasacoupleof connectorsonasmallPCBthenaninexpensivetoolcanproduceusablePCBGerberfilesand adrillfile.GerberfilesareCAM(computeraidedmanufacturing)filesthatdescribewhereto putcopperonaPCB.Adrillfile,sometimescalledanNCdrillfilefor“numericallycontrolled”drillfile,describeswhereandwhatsizedholestoplaceinthePCB. IfaboarddesignincludesfinepitchSMTpartsandhundredsofconnections,acheapPCB layouttoolwillcostmoreinlayouttime,errorsanddebuggingthaninitiallybuyingagood tool.OnetoolthathasaproventrackrecordisPCAD™.Itisperhapsthebesttoolavailable todayfordoingschematiccaptureandPCBlayoutofsimpleorcomplexdesignsforthesmall andmediumsizedcompany. Ifcomplexcircuitdesigncanbeavoided,thenPCBdesigncanbeaccomplishedeconomically withalow-costtoolorbyfarmingtheworkouttoadesignbureau.Ifacoremodulecanbe used,oftentheadditionalcircuitryforanapplicationisminimal.Theapplicationboarddesign
25
Chapter2 isoftenamatterofprovidingsimpleinterfacecomponentsandapowersupply.ThedevelopmentboardsareusuallysuitablefordesignusingalowcosttoolorPCBdesignhouse. TheZ-Worldfull-grownboard-levelandpackagedcontrollersofteneliminatetheneedfor anysecondaryPCBdesign.Thiscansavethousandsofdollarsintoolsandtime.However, formediumorlargeproductionvolumes,itisgenerallymoreeconomicaltogowithaRabbit Semiconductorcoremoduleorchip-baseddesign. 2.1.5Libraries Whenselectingacontroller,anoftenoverlookedfactisthattheamountoftimespentwriting codeforanembeddedsystemusuallydwarfsthetimespentdesigninghardware.Selectinga controlsolutionthatminimizesthesoftwaredevelopmenttaskisdesirable. Manycompaniesoffercontrollerhardware,butfewofferhardwareandtightlycoupledsoftwarelibraries.Thismeansthatsoftwareengineersmayhavetospendweekswritingcodeto talktoeventhesimplestdevices. RabbitSemiconductorofferscompletelibrariesforalloftheircontrollers.Therearedrivers forsimpledeviceslikeanalog-to-digitalconverters(ADCs)aswellascomplexdriversfor deviceslike¼VGAgraphicLCDs. Librariesgobeyondhardwaredevicedrivers.Variouscommunicationprotocolsareprovided withRabbit-basedproducts.TCP/IPandModbusaretwoheavilyusedprotocols.TCP/IPis usuallyusedwithEthernet.ModbusismostoftenusedoverRS-485. Anembeddedsystemsdesigner’stimeisbestspentdevelopingalgorithmsandsoftwarespecifictotheproject,notreinventingdrivers.Byselectingacontrolsolutionwithanabundance oftestedandprovendrivers,developmenttimecanbeminimized. OneoftheadvantagesofanembeddedPCistheamountofsoftwareavailable.Forinstance, operatingsystemslikeMicrosoft®Windows®,LinuxandDOS®arereadilyavailable.Driversformanystandarddevicesareavailable.Ethernet,diskaccess,videoandkeyboardsare readilyavailable. CaremustbeexercisedwhenattemptingtousedesktopOSsinembeddedapplications.After all,noneoftheseweredesignedspecificallywithembeddedsystemsinmind.Howdoyou talktoanembeddedI/OdevicelikeanADC?Whatdoyoudoifyouwanttodecodesignals fromaquadratureencoder?TheanswerinmostcasesistohandletheI/Odirectlyandwrite yourowndrivers. TheexperienceRabbitSemiconductorhasgainedovertheyearshasallowedthecompanyto craftfeaturerichdriversforembeddedsystemsdesigners.Forexample,Rabbitdriversexist totalktobothADCsandquadratureencoders. ThevastlibrariesseparateRabbit’ssolutionsfromtheircompetition.Theexistenceofsolid driversandaroyalty-freeTCP/IPstackallowsthesystemdesignertofocusontheapplication andnotonreinventingdriversorwritingaprotocolstack.
26
TheBasics 2.1.6Sustainability Productsspend90%oftheirlifeinproduction.Theoriginaldesignersoftenmovetoother companiesbeforeaproductcompletesitslifecycle.Engineerswhowerenotinvolvedwith theproduct’sdevelopmentwillalmostalwaysberesponsibleforsustainingtheproductin production. Modificationsmadetoaproductthathasbeenreleasedtoproductioncanbelooselygrouped intofourcategories.Thesearebugfixes,correctionstoaddresshardwareEOL(endoflife) issues,productenhancements,andimprovementsformanufacturability. Bugfixesarethemosturgentclassofchanges.Softwareorhardwarebugsmaybediscovered.Selectingacontrollerwitheasytolearnsoftwaredevelopmenttoolswillallowafuture engineertoquicklycomeuptospeedonhowtomodifytheproduct’scodebase.Iflibrary sourcecodeisavailable,thenevendriverbugsmayberepaired. Inthecaseofhardwarebugs,selectingamicrocontrollerwithgooddocumentationand debuggingfeaturesisprudent.Asustainingengineerwillhavetocomeuptospeedquickly ontheavailabletroubleshootingtools.Thedesiredemphasiswillnaturallybeonunderstandingandfixingtheproduct’sbug,notlearningcomplicatedorpoorlydocumentedtools. ThesecondclassofproductionsidemodificationsaddressesEOLissues.Ifthepartbecomingobsoleteiseasilysecondsourcedthentheonlymodificationtotheproductwillbetothe product’sdocumentation.Asecondsourceforthepartwillbequalifiedandadded.However, ifthepartgoingEOLisasinglesourcepart,thenhardwareandsoftwarechangeswilllikely berequired. Considerthecaseofarealtimeclockchip(RTC)goingEOL.Thesedevicesareoftensingle source.Occasionallytherewillbe“pincompatible”devicesavailable,buteventhesemaynot befunctionallyequivalentdevices.Sointhebestcaseasoftwaremodificationwillneedtobe madetotheRTCdriver.TheworstcaserequiresbothaPCBchangeandanewRTCdriver. Asinthecaseofabugfix,goodsoftwaredevelopmenttools,goodproductdocumentation andlibrarysourcecodeassistinfocusingontheproblemnottheprocess.Thissavestimeand moneyandallowsthecompanytoaddressEOLissuesnimbly. Thethirdclassofpost-productionreleasemodificationsaddressestheinevitabledemandsfor featureenhancement.Thereasonformodificationstotheproductmaybedifferentthanfor theEOLmodifications,buttheactualworkrequiredisthesame.Productenhancementswill requirechangingsoftware,hardwareorboth. Theprescientdesignerwillinitiallyselectacontrollerwithafewmoreresourcesthanare actuallyneededfortheproductrollout.Thiswillallowafutureengineertoaddproduct enhancementswithoutchangingthehardware. Thelastclassofchangesrelatestomanufacturability.Generallythesearemechanicaladjustmentsthatmakeassemblyeasier.Theinitialcontrollerselectionhaslittleimpactonthesetypes ofchanges.Onenotableexceptioncropsupwhentheproductiontestdepartmentortherepair/ re-workgroupwantstoinstallspecialtestordiagnosticcodeintheproduct.Inthissituation, anin-systemprogrammable(ISP)controllercansavethecompanymuchtimeandmoney.
27
Chapter2 Somecompaniesopttodofunctionaltestsoneveryinstrumentproduced.Iftheinstrumentis complexorrequirestheinteractionofmanydifferenthardwaresubsystems,aspecialfunctionaltestprogram(testcode)maybepreferredoverthefullapplicationtoverifythatthe individualhardwarecomponentsareoperational. Testcodeseldombearsanyresemblancetothefullapplicationprogram.Atestprogram attemptstoisolateindividualhardwarecomponentsandverifytheiroperation.Ifsensors requirecalibrationthenthetestcodemayalsoperformthistask.Ideally,atestprogramwill identifyandisolateproblems.Therepairdepartmentwilloftenuseproduction’stestcodeasa diagnostictool. Afteraninstrument’shardwareisqualifiedbythetestcode,theapplicationcodemustbe loaded.Theinstrumentisgivenacursoryexaminationandthenplacedinfinishedgoods. IfthecontrollerisnotISP,thenasocketednonvolatilememorydevicewillneedtobe designedintothesystem.Changingmemorydevicescanbetimeconsuming,andthetest ROMswillbesubjecttophysicaldamagefromconstanthandling.AnISPcontrollerallows productiontestcodeordiagnosticcodetobeinstalledandreplacedeasily.
2.2DefiningtheProblem Beforeselectingacontrolsolution,thecontrolproblemmustbewelldefined.Thiscanbe complicatedbyindecisivemanagementorbytechnologicalhurdlesthatareofunknown magnitude.Theformeristhemostfrustratingforengineers.Thelattercanoftenbemitigated throughresearchandsimulation. Managementwillprovideaproductdescription.Somecompanieshaveaformalprocessfor creatingaproductrequirementdocument(PRD),sometimescalledaproductspecification, sometimescalledasystemrequirementsdocument(SRD).Sadly,mostcompaniesdonot haveaformalprocess.Alltoooften,managementwillsimplyissueashortmemooranapkin sketchdescribingthenewproduct’sfeatures. Ineithercase,theengineermuststartwithwhatisgiven.Obviousquestionsmustbe answered—eitherbymanagerialedict,technicalprowessoreducatedguessing. Next,thesystemarchitecturemustbedeveloped.Somecompanieshaveaformalprocessfor takinganSRDandsplittingouthardwareandsoftwarerequirementsintoahardwarerequirementsspecification(HRS)andasoftwarerequirementsspecification(SRS).Fromthere, hardwareandsoftwarearchitecturescanbedefined. Butformostcompanies,atthisstagethecontrollerissimplyaboxonadiagramwithsensors andactuatorsreportingtoitandablobonthesideofthepaperwithwhatpassesforaflowchartamongstfriends. Regardlessoftheformalityoftheprocess,theengineerwillgetanideaofthetypeofI/Orequiredfortheproject.IfADCsarerequired,theirresolutionshouldbedefined.Ifpulsewidth modulationisbeinggeneratedthencharacteristicssuchasdrivecurrent,frequency,dutycycle rangeandresolutionshouldbedefined.Asmuchaspossibleshouldbenaileddownupfront.
28
TheBasics Undoubtedly,astheprojectprogresses,requirementswillchangeandtheinitialguessesat variousfeatureswillrequireadjustment.Thisisjustpartoftheprocessofevolvinganidea intoaproduct.Formalprocessestrytoavoidcostlychangeshalfwaydownaproductdevelopmentpath.Butfront-loadingaprojectwithdetailedrequirementsdocumentationisnot wellsuitedtotheneedsofmanysmallcompanies. Time-to-marketmayrequireplayingfastandloosewiththerequirementsphasejusttoget aproducttomarketquickly.Sometimeshavinganimperfectplanonaplacematwillbeof greateruseingettingasellableproductdonequicklythanhavingaperfectplaninsixmonths. Onceaplanisinhand,technologicalhurdlesmaystillcomplicateaproject.Theseareproblemsthatanengineerdoesnotknowhowtosolveupfront.Theseproblemsareonlysolved throughresearchandexperimentation.
2.3ASurveyofSolutions WhilethisbookusesRabbit3000-basedproductsforalltheprojectsdetailed,therearea myriadofothercontrollersonthemarket.Manyofthetechniquespresentedareapplicableto controllersotherthantheRabbit3000.Solet’sbrieflyexaminesomeoftheavailablemicroprocessorsandmicrocontrollersandseehowtheycomparewiththeRabbit-basedsolutions. 2.3.1SmallMicrocontrollers Therearemany“small”microcontrollersonthemarket.Probablythemostwidelyknownis thePIC™lineofcontrollersfromMicrochip.Thesepartsalongwiththeirhopefulbrethren— AVRfromAtmel,COP8™fromNationalSemiconductor,Nitron™fromMotorolaandmany others—fillanicheinthe“not-too-complicated”classofembeddedsolutions. Thesepartsareavailablefrom$0.80toabout$5.00.Codeisstoredeitherin“OneTimeProgrammable”(OTP)ROMorinternalflash,makingsmallcontrollersfairlyself-contained. ThelowerendofthisstyleofcontrollerfindspartslikeMicrochip’sPIC16C54andAtmel’s AT90S1200.ThesepartshavealowpincountandsubsequentlylowI/Ocapabilityandhave beenoptimizedforsimpleapplications. Table2.2showshowthefeaturesetsstackupbetweenthesetwoparts. Table2.2:LowendPICsandAVRsgohead-to-head. Pincount MaximumI/Opins Frequencyranges Available RAM EEPROM GeneralPurpose Registers ProgramStorage
PIC16C54 28 20 DC–40MHz 24bytes ZERO SameasRAM24×8
AT90S1200 20 15 DC–12 MHz 0 64bytes 32×8
512×12bits(OTP)
1KB(flash)
29
Comments
PICTMWregisterisaccumulator AVRhasnoaccumulator
Chapter2 Table2.2:LowendPICsandAVRsgohead-to-head(continued). CodeSize
PIC16C54 512
AT90S1200 512
Counter/Timers ExternalInterrupts Sleepmode WatchdogTimer ISP AnalogComparator
1 0 Y Y Y N
1 1 Y Y Y Y
Comments PIC12-bitinstructions AVR16-bitinstructions
Forsystemsrequiringanaloginput,integratedanalog-to-digital-converters(ADCs)are available.Thesearegenerallylimitedto8-bit,10-bitor12-bitconverters.Forexample,the PIC16C710hasabuilt-in8-bitconverterwitha4-to-1analogmultiplexer.ThePIC16C710 hasasimilarpinouttothePIC16C54,butseveralpinshavemodifiedfunctions. Historically,analogdesignershavefrownedupontheperformanceofADCsbuiltonthesame silicondieasthemicrocontroller.NoiseperformanceonsomeintegratedADCshasbeen knowntobelessthanstellar.Thisissomethingthatmustbeevaluatedonapart-by-partbasis. Somemanufacturersdoanexcellentjob;othersarenotasgoodatkeepingdigitalnoiseoutof theanalogportionoftheirdesigns. Burr-Brown,nowadivisionofTexasInstruments,isanamesynonymouswithexcellencein analogdesign.TheBurr-Brown/TIMSC1200isanexampleofadevicewhereafastcomputationalcorecoexistswithhigh-resolutionlow-noiseanalog. Foraround$6.50theMSC1200offersalotoffunctionality.Thefollowinglistenumeratesthe device’skeyfeatures:
8051high-speedcore(4clocksperinstructioncycle) DC-33MHzoperation 4KISPFlash 1Kbootblock 128bytesofSRAM 24-bitdelta-sigmaconverter Programmablegainamplifier(PGA)from1to128 8inputanalogMUX Differentialorsingleendedinputs On-siteburnoutdetection On-siteprecisionreference On-sitetemperaturesensor 8-bitcurrentDAC 16digitalI/Opins Watchdogtimer Two16-bittimer/counters 30
TheBasics
FullduplexUART −40Cto+85Ctemperaturerange 2.7to5.25voltallowablerails
TheMSC1200datasheetindicatesthatasystemdesignercanexpecttoseebetween5and22 effectivebitsofresolutiondependingonhowtheanalogchannelisconfigured.ManyofBurrBrown’shigh-resolutiondelta-sigmaconvertersallowthesystemdesignertotrade-offgain andsamplerateagainstuncertainty(noise).TheMSC1200allowsthesametrade-offs. AflexibleanalogMUXallowstheanaloginputstobeeitherdifferentialorsingleended.The MUXofferstheunusualcapabilitytopairarbitraryinputsasasingledifferentialchannel. Thehigh-speedcoreisbasedonthevenerable8051architecture.Thepopularityenjoyedby 8051overdecadesisatestamenttotheoriginaldesigner’sinsightfulness.Yet,theMSC1200 corehasseveralimprovementsovertheoriginalarchitecture. Notably,theMSC1200corehasonlyfourmachinecyclesperinstructioncycle.Thetraditional8051implementationshavetwelvemachinecyclesperinstructioncycle.This,combined withascreaming33MHztopspeed,maketheMSC1200areasonablypotentcomputational enginefortheprice. TheMSC1200isanexampleofacontrollerthathasfantasticanalognoiseperformance marriedtoaCPUonasingledie. SmallcontrollersaresuitableforoffloadingsimpletasksfromalargerCPU.Forexample, consideracontrollerthatmustimplementasmallservocontrolloopaspartofalargersystem.Thisservolooprequiressamplingapositionerrorandquicklyadjustingadrivesignalto amotor.Ifthedesignermovesthistaskfromamainprocessor,sayaRabbit3000,toasatelliteprocessorlikeaPIC,thenthesoftwareontheRabbitbecomessimplerandmoreofthe Rabbit’sresourcesareavailableforothersystemtasks. Usingsmallmicrocontrollersassatellitecontrollersisatechniquethatcan,insomecircumstances,savedevelopmenttime.Theadditionalmicrocontrollermaydriveuptheendproduct cost.Thismayormaynotbeoffsetbyreductionindevelopmenttime.IfthemainCPUcan dotheoffloadedtaskevenattheexpenseofmorecomplexsoftware,thenitmaybeworth spendingtheextratimeonsoftwareNRE(nonrecurringengineering)feestoreducetheperunitcostofthehardware. Insomecases,theapplicationofaninexpensivesatelliteprocessorcanactuallykeepthecost ofasystemdown.Ifamainprocessorisstretchedtothelimitandanadditionaltaskmustbe added,theincrementalcostofmovingfromonemainCPUtoanothermaybehigherthan offloadingtheadditionaltasktoaninexpensivesatellitecontroller. 2.3.2Mid-sizedMicrocontrollersandMicroprocessors Partscosting$10to$20aregenerallyconsidered“mid-sized”controllers.Therearealarge numberofpartsavailableinthisclass.TheRabbit2000andRabbit3000microprocessors, theAVRATmega128andtheIntel386™EXallfallsquarelyintothemid-sizedclass. Differentarchitecturalphilosophiesdrovethedevelopmentofeachoftheseprocessors.This isevidentfromeachprocessor’sfeatureset. 31
Chapter2 Formany,thenameIntelconjuresupimagesofmulti-gigahertzmulti-wattdesktopprocessors.It’seasytoforgetthatIntelhasalonghistoryofservingtheembeddedmarket.The Intel386EXwasdesignedspecificallyfortheembeddedmarketbyadaptingthepopular desktopi386. TheIntel386EXdatasheetstates,“It(theIntel386EX)providestheperformancebenefitsof 32-bitprogrammingwiththecostsavingsassociatedwith16-bithardwaresystems.”Clearly, Intelwastryingtoleveragethesuccessofthex86familywhiletradingoffperformancefor lowercost.Bybuildingfromanexistingdesignandmaintainingbinarycompatibilitywith earlierfamilymembers,Intelensuredthatawidearrayofexistingdevelopmenttoolswould generatecodefortheIntel386EX. TheAtmelAVRfamilysportsaverystraightforward,almosttextbook,architecture.When theAVRarchitecturewascreated,allofthedevelopmenttoolshadtobebuiltfromscratch. Atmelcreatedadesignthatscalednicelyacrossawidearrayofderivativeproducts.Thesame basicarchitecturecanbefoundintheleastexpensiveAVRs(suchastheAT90S1200)all thewaytothemid-sizedfamilymembersliketheATmega128.AlloftheAVRsareflashbasedcontrollers. RabbitSemiconductorstruckamiddleroadbetween“old”and“new.”TheRabbitpartsare basedcloselyontheZilogZ180architecture,althoughtheyarenotbinarycompatiblewith theZilogparts.ThiscausedRabbitSemiconductortohavetocreateasuiteofdevelopment toolstosupportthenewdesign.Atthesametime,Rabbitwasabletofreelyaddinstructions to,andotherwisetweak,theZ180architecture. So,likeIntel,Rabbitbuiltuponawidelyrespectedandsuccessfularchitecture.UnlikeIntel, Rabbitdidnotmaintainbinarycompatibilitywiththeoriginaldesign. LikeAtmel,Rabbithadtoproducefirmwaredevelopmenttools,andlikewise,RabbitoptimizedtheirdesignforCprogramming. Nowthatwehaveanideaofthegeneralphilosophiesbehindtheprocessordesigns,let’shave alookatkeyfeaturesofeachdevice.We’llstartwiththeIntel386EX. Intel386TMEX StaticIntel386™core;DC–33MHz Full32-bitinternalarchitecture External16-bitdatabusallowsuseoflessexpensivememories 26-bitaddressbus Addresspipeliningallowsuseofslower,inexpensivememories MMUfullycompatiblewith80286andIntel386™DXprocessors On-chipdedicateddebuggingsupportincludingbreakpointregisters Watchdogtimer Chip-selectunit Timer/counterunit Interruptcontrolunit ParallelI/Ounit(24I/Opins) UARTs 32
TheBasics
DMAandbusarbiterunit Refreshcontrolunit JTAG-complianttest-logicunit 140mA@25MHz,VCC=3.6volts(deviceheldinresetwithinputsheldin inactivestate)
Bylookingatthefeatureslistedabove,onecantellthattheCPUisaderivativeofthedesktop processorline.Besidesthegiveawaybulletof“Intel386™Core,”theprocessorhasfeatures suchasa32-bitarchitecture,ahuge26-bitexternaladdressspace,DMAandabusarbiter.All areindicativeofapowerfuldesktopprocessor. Intelwasclearlytargetingtheembeddedmarketwiththisprocessor.Theysqueezeddownthe externaldatabusto16-bitstoallowlessexpensivememorydevicestobeusedintheembeddeddesign.Acounter/timer,UARTand24I/Opinswereaddedaroundtheprocessorcoreas anodtoembeddedinterfacing.Thepowerconsumption,whilelowforadesktopprocessor,is stillfairlyhighforlow-poweredembeddedapplications. Thisreliableprocessorhasenjoyedmanyyearsofsuccess.Thewidespreadavailabilityof qualitydevelopmenttoolsiscertainlyaboonforthepart. Next,weconsidertheAVRATmega128.Thefollowinglistcapturesthehighlightsofthis controller. ATmega128 8-bitdatabus Staticcoredesign;DC–16MHz 32×8-bitgeneralpurposeregisters Upto16MIPsthroughputat16MHz 128K-BytesofinternalISPflash 4K-BytesofinternalEEPROM 4K-BytesofinternalSRAM Upto64K-bytesofexternalmemory Two8-bittimer/counters Two16-bittimer/counters Two8-bitPWMchannels SixPWMchannelsfrom2to16bits 8channel,10-bitADC TwoUARTS Watchdogtimer 53I/Opins 18mA@16MHz,VCC=3.3volts Thispartisloadedwithfeaturesthatscream“embeddedcontroller.”Thepowerconsumptionisverylow.Thereisaneightchannel10-bitADConthedie.The53pinsofdigitalI/Ois certainlytargetedattheembeddedmarket. Thiscontrollerhasalloftheprogrammemoryinflashandonboard.Asignificantamountof SRAMisalsoincluded.Manysmallembeddedapplicationswillfitintothismemorymodel. 33
Chapter2 The4KofonboardEEPROMcanbeusedtostorecalibrationconstants,logasmallamount ofdata,orevenstoresystemIDandconfigurationinformation.Thesearecommontasksperformedinembeddedsystems. Ifalargerdatamemoryisrequired,someofthe53I/Opinscanbetradedoffandupto 64K-bytesofexternalSRAMadded.Formanysystems,64Kofdatastorageisadequate. Butformanyapplications,theATmega128’smemorymodelwillbetoolimited. EvenwithaRISC-likeabilitytoexecutemanyinstructionsinasingleclockcycle,a16MHz speedisconsideredslowbymanyengineers.Dependingontheapplication,clockspeedmay notbeanissue. Considerasmallsolar-poweredcontrollerthatperformssimplebiophysicaldataacquisition. Oneofthemajordesignconsiderationsforthistypeofsystemispowerconsumption.There aretwofrontlinetechniquesusedtoreducepowerconsumption.Runthesystematalower voltagesotheV2/Rpowerislowered.Runthesystemataslowclockrate—powerconsumptionislinearlyproportionaltoclockspeed. Inthistypeofsystem,a16MHzclockmightbetenorahundredtimesfasterthanthe designerwillneed.Inthiscase,thespeedoftheinternalflashmemoryisnotrelevantwhen selectingacontroller. Next,we’llbrieflylookattheRabbit3000A. Rabbit3000A 8-bitdatabus 20-bitaddressbus StaticCoreDesign;DC–54MHz Gluelessmemoryinterface ClockspreaderforEMIreduction Highfrequencyclockand32768Hzclock Built-inclockdoubler 4levelsofinterruptpriority Bootableoverserialport(coldbootfeature) 56I/Osignals(sharedwithserialportsandotherperipherals) FourPulsewidthmodulationchannels SixUARTs AuxiliaryI/Obusreducesloadingonmemorybus Twoinput-capturechannels Twoquadraturedecoderchannels Built-inwatchdogtimer Standard10-pinprogrammingport 65mA@30MHz,VCC=3.3volts Insomeways,theRabbit3000AstrikesamiddlegroundbetweentheIntel386EXandthe ATmega128.The20-bitaddressspaceismuchbiggerthantheAVRbutsmallerthanthe 34
TheBasics 26-bitspaceofferedontheIntelpart.ThepowerconsumptionishigherthantheATmega128, butmuchlowerthantheIntel386. TheRabbitoffersthehighestclockrateofthethreedevices.Thisdoesnotnecessarilytranslateintohighercomputationalperformance.Internally,theIntelcoreisfourtimesaswideand theexternaldatabusistwiceaswideastheRabbit.TheRabbit,onbalance,requiresmore clockcyclestoexecuteaninstructionthantheAVR. OnepotentialdisadvantageofturninguptheCPUclockspeedisthatfasterexternalmemory isrequired.However,astimeprogressed,whatwasonce“fast”expensivememoryhas become“slow”cheapmemory.RabbitSemiconductorunderstoodthiswhentheirprocessorwasdesigned.Thisforwardthinkinghasproducedasnappylittlepartthattodaydoesn’t requireexpensivememory. Anotherpotentialdisadvantageofrunningapartathigh-speedistheincreasedpowerconsumptionoftheCPUandmemory.Asclockspeedincreases,sodoespowerconsumption. TheRabbitisafullystaticcoreandcanberunfromDCto54MHz. TheRabbit’sI/OmixisalotmorecompletethaneithertheIntelorAtmelparts. TheRabbit’sfeaturerichI/OsetisderivedfromZ-World’sfirsthandknowledgeofthe embeddedmarket.RabbitsportsPWMchannels,quadraturedecoderchannels,inputcapture channelsandahighdigitalI/Ocount. OneofRabbit’smoreusefulfeaturesistheinclusionofsixUARTs.Atfirstglance,sixUARTs mightseemabitextravagant,untiloneconsidersthenatureofembeddedcommunications. Theadventofclockedserialperipheralsmeansthatasystemdesignercanaddmanytypesof sophisticatedI/Owithoutloadingtheprocessor’smainbus.ADCs,DACs,RTCs,EEPROMs andahostofotherdevicesareavailablewithclocked-serialinterfaces(I2C™,SPI™andMicrowire™).TheRabbit’sUARTsmakeexpandingthesystem’sI/Ofeaturesquickandinexpensive. Rabbit’sauxiliaryI/ObusisanothertoolthatcanbeusedtoexpandtheI/Ofeatureset quicklyandeasily.Inconventionalprocessors,parallelI/OdevicesareplacedontheCPU’s dataandaddressbusses.Thiscanleadtoundesirablecapacitiveloadingofthedataand addressbusses.Rabbit’sauxiliaryI/ObusallowsdesignerstoplaceparallelI/Odevicesona buscompletelyseparatefromthememorybus. AnotheruniquefeaturetheRabbitoffersisaprogrammingportthatisusedtoprogramflash memoryconnectedtotheCPU’smemorybus.Thiscleverinterfaceallowsanyflashmemories usedontheCPUbustobetreatedasISPdevices.TheprogrammingportallowsaPCwitha serialconnectionandabitofsoftwaretoprogramflashmemoriesthroughtheRabbitprocessor. 2.3.3FinalThoughts Anengineerfacedwithselectingamicrocontrollerormicroprocessormustbepreparedto weighanumberoffactorsandmaketrade-offs.Comparingmicrocontrollersisanexercisein contrastingapples-to-oranges. Findingacontrollerwiththerightmixofcomputationalspeed,integratedperipheralsand developmenttoolsisaneasiertaskthanonemightthink.Thedifficultyispickingjustoneof themanyexcellentsolutionsavailable. 35
Chapter2
2.4ARabbit’sRoots Aswesawinthelastsection,acompany’sdesignphilosophygreatlyinfluencesthefinal formofitsproducts.Inthenextsection,wewillexaminetheRabbit’sarchitectureindetail. Butbeforewedo,itwillbeinformativetohavealookatRabbitSemiconductor’shistoryand understandtheforcesthathaveshapeditsdesignphilosophy. OurstorystartsinSiliconValleyin1981.Attheageof40,NormRogersandco-workerPaul MennenquittheirjobsatGenRadandwithequalinvestmentsof$34,000formedDecmation. Theiroriginalproductwasaplug-incardthatallowedDigitalEquipmentCorporationminicomputerstorunCP/Msoftware.Theideawasn’tnew.Therewasanotherproductalready inthemarketthatsoldforaround$2,500anddidaboutthesamething.Decmation’sproduct soldfor$700. Inthefirstyear,thelittlecompanyhadaquarter-millioninsales.Marketingconsistedofrunningsmalladvertisementsinthebackofelectronicsmagazinesandattendingtheoccasional tradeshow.Orderswereprocessedbytelephone. Businessremainedstrongandin1983,NormmortgagedhishomeinSanJose,boughtout halfofPaul’sinterestinthecompanyfor$50,000andincorporatedDecmation.From1983 on,NormRogersmaintainedthecontrollinginterestinthecompany. Normpouredtimeintoresearchanddevelopment.Bylate1985hehadhisnextbighitalmost complete.Thisproduct,namedBlueLightning,didforIBMPCswhattheoriginalproduct didforDECminicomputers.TherewerealargenumberofapplicationswrittenforCP/M,but sinceIBMhadthrowninwithMicrosoft,MSDOSPC’swerein,andCP/Mmachineswere ontheirwayout.BlueLightningallowedIBMuserstoruntheirlegacyCP/Mapplications. Again,therewasalreadycompetitioninthemarket.Thecompetitor’swellknownproduct wascalledthe“BabyBlue,”atakeoffonthecolorofIBMcomputers.Decmation’sname, BlueLightning,wasashamelessripoff. ByastrokeofluckthecompetitorwentbankruptthesamedaytheBlueLightningwasintroduced.ThebankruptcywascausedbyproblemsunrelatedtotheCP/Mboardbusiness. AnimprovedDecmationdesignsoonfollowed—BlueThunder,whichsoonreplacedBlue Lightning. InanemailsenttotheauthorsofthisbookNormRogersrecallsBlueThunder: “MychampionproductwastheBlueThunder12MHzcard.AtthattimenoZ80had afasterofficialclockspeedthan6MHz.HoweverIdiscoveredthatNECchipswould actuallyrunupto14MHzbeforetheyfailed.Ialsodiscoveredthatonebrandof JapanesedynamicRAMwouldrunfastenoughtokeepupwitha12MHzZ80.So, Iputthisalltogether,sandedoffthepartnumberssothatnoonecoulddiscovermy discovery,andsold12MHzBluethundersforabout3timeswhatthe6MHzones soldfor.Isoldalotofthemwithagreatprofitmargin.”—NormRogers,March2003 By1986,thecompany’smainbusinessofCP/Msimulatorswaswaning.Customerswere phasingoutCP/M-basedsoftwareapplications.Insearchofnewproducts,Decmationtried anumberofdifferentavenues,withvaryingsuccess.TheCP/McardsthatpluggedintoPCs 36
TheBasics wereredesignedwithaserialportandmadeinto“communicationscoprocessors”which couldoffloadserialcommunicationsfromthePC’sCPU. AnothernewbusinesswasdevelopmenttoolsforZ80basedembeddedsystems.Agreat dealofveryhighqualityCP/Mbaseddevelopmenttoolswerenowconsideredobsoleteand wereavailableforpurchaseatlowprices.Decmationlicensedandrebrandedtheseastools forembeddedsystemsengineers,eitherwrappedwithaCP/Memulatororrecompiledtobe MS-DOSapplications.ThischangeofbusinessdirectionplusthreateninglettersfromDigitalEquipmentCorporation(“DEC”)lawyersledtochangingthenameofthecompanyfrom DecmationtoZ-World.The“Z”stoodfortheZ80microprocessoratwhichthedevelopment toolswereaimed. In1987,NormmovedfromSanJosetoDavis,California.Hisyoungchildrenwereschool age,andthepublicschoolsinDaviswerebetterthanthemarginalschoolsinSanJose. Z-Worldrenteda1200squarefootofficeinasmallbusinessparkandhadfouremployees. BeinghosttotheUniversityofCalifornia,Davishadeageryoungcollegestudents.AtoweringyoungcomputersciencestudentwhohadindependentlywrittensomeZ80development toolsmadecontactwithNorm.NormrecognizedtheenergyandgeniusofGregYoung,and Z-Worldquicklyhiredhim.Gregwrotethefirstintegrateddevelopmentenvironment(IDE) forDynamicC.Gregwouldlatergoontosetupandmanagethetechnicalsupportgroupand serveasadesignengineer,andtoday,stillconsultsonspecialprojects. In1989,Z-WorldbroughtinaprofessionalmarketingmanagerbythenameofCarrieMaha. Whileshewasnewtotheburgeoningembeddedsystemsmarket,shebroughtanorganizationandpolishthatZ-Worldneeded.Nolongerwouldmarketingconsistsimplyofblackand whiteadsinthebacksofmagazines.Carriebuiltaworld-classmarketingandsalesorganizationfromthegroundupandtodayservesastheExecutiveVicePresident. ThefirstZ-Worldembeddedplatform,dubbedtheLittleGiant,combinedaZ180microprocessorandpracticalI/Odesignedtocontrolandsupervisereal-worldembeddedsystems. Z-World’sDynamicCdevelopmentsystem,writtenbyNormandGreg,madeiteasyto programthenewboard.BundledwithDynamicC,thisproductwasaninstanthitwhenitwas introducedin1990.TheLittleGiantstillsellswelltodayasalegacyproducttotheoriginal OEMs.Theverticalintegrationofcontrollerhardwareanddevelopmenttoolswouldpropel Z-Worldthroughthedecadeandintothenextcentury. By1997,thecompanyhadmovedtwicetoaccommodateitsrapidgrowth.Revenuewasup, profitwasupandthestagewassetforanothertechnologicalleap.NormRogershadspentthe betterpartoftwodecadesworkingaroundtheshortcomingsoftheZ80andZ180.Tenyears oftweakingcompilersandbuildinghardwarehadgivenNormawell-roundedandintimate viewoftherelationshipbetweenhardware,thefirmwarethatrunsonitandthesoftwaretools usedtocreatethefirmware.Normwasreadytobuildanewprocessor. In1997,NormwasintroducedtoPedramAbolghasem,anambitiouselectricalengineeringstudentatSacramentoStateUniversity.Upongraduating,Pedramjoinedthecompany tospearheadtheR&Deffortsbehindthefirstmicroprocessorthatwouldaddressthemany weaknessesoftheZ180design. 37
Chapter2 EmbarkingonamethodicalsearchforapartnerwhohadalreadyriddentheASICpony, Pedramlocatedaonemanconsultingcompany,SystemydeInternational,ownedbyformerZilogdesignerMonteDalrymple.With15patentscreditedtohisnameandexperience asPrincipalArchitectandDesigneronZilogproductssuchastheZ16C30,Z16C32and Z16C35,MontehadtheexperienceneededtobringNorm’sideastofruition. InSeptember1997,thedevelopmentoftheRabbit2000beganinearnest.Norm,Pedramand Monteworkedtirelesslytomovetheprojectforward.Normsuppliedthearchitecture.Monte, asaconsultant,wrotetheVerilogcodeandprovidedmatureadvicetoNormandPedram. Initially,Pedramwastheonlyonsiteengineerassignedtotheproject.Thismeanthewasthe catch-allforwork.Pedramdesignedprintedcircuitboards,usingthelargestandmoststate oftheartAltera’sFPGAs,withwhichtotestMonte’sVerilogcode.Pedramfoundnumerous bugs.Montefixedthem.Intheend,PedramandMonteturnedNorm’sideasintosilicon. TheentiredevelopmentcostsoftheRabbit2000amountedtolessthan$500,000.Thiswas only1/40ththecostthatsomeothercompanieswerespendingatthetimeintheirmicroprocessordevelopmentefforts—manyofwhichfailed. Inlate1999,RabbitSemiconductorwaslaunchedtocontinueandsupportthemicroprocessor development.InNovember1999,theRabbit2000processorhitthestreets.Itssuccesswas followedbytheRabbit3000in2001,andtheRabbit3000Ain2002.TheRabbit4000isin theworks. NormRogersisstillmajorityshareholderandcompanypresidentofbothZ-WorldandRabbit Semiconductor.PedramistheDirectorofEngineering.Montecontinuestoworkcloselywith NormandPedramonRabbit’snewdesigns. WiththeadventoftheRabbitlineofprocessors,Z-WorldandRabbitSemiconductorhave attainedadegreeofverticalintegrationnotseenintheembeddedmarkettodate.Customerscanchoosefromaplethoraofofftheshelfsolutionsfromprocessorstofullypackaged controllers.TheDynamicCplatform,nowa32-bitMicrosoftWindows-basedapplication, supportsCandassemblylanguagedevelopmentaswellasdebuggingonallthetargethardware.TheRabbitprocessorshaveenjoyedwideindustryacceptanceandaresupportedby thirdpartytools.
2.5RabbitinDetail AstherestofthebookusestheRabbit3000microprocessorforallprojects,anexaminationof thechip’sarchitectureisinorder.Figure2.1showsatop-levelblockdiagramofRabbit3000. 2.5.1TheCPU AttheheartoftheRabbit3000istheCPUblock.AsseeninFigure2.1,allexternaldatais transferredtoandfromtheexternalbusestotheCPU.TheCPUregistersactasasourceor destinationforallexternalbustransfers. TheCPUcommunicateswiththeinternalperipheralsusinganinternalbus.Thisbusconsists ofan8-bitinternaldatabus,8-bitinternaladdressbusandsundryinternalarbitrationsignals. The8-bitinternaladdressbusmeanstheRabbit3000islimitedto256internalperipheral I/Oregisters.Italsomeansaccesstotheregistersdoesn’trequireatwobyteoperandfetchto 38
TheBasics 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]
Figure2.1:Abird’s-eyeviewoftheRabbit3000showsafeaturericharchitecture.
acquireacomplete16-bitaddress.This increasestheRabbit’sefficiency. TheCPUblock,whilesimilartothe Z180CPU,hassomeimportantdifferences.Adiagramoftheregisters availableintheCPUblockisshown inFigure2.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'
Figure2.2:TheRabbitCPUhas anabundanceofregisters.
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
Chapter2 ThosefamiliarwiththeZ80andZ180willimmediatelyrecognizethefamiliargeneralpurposeregisters(A,F,B,C,D,E,H,L)andthealternateregisterset(A',F',B',C',D',E',H', L').RabbitSemiconductorhasaddedsomenewregistersandextendedthefunctionalityofthe traditionalcomplement. Thealternateregistersetwasoriginallyusedtostorethecontentsoftheregularregisterset whenaspecialfunctionwascalled—oftenaninterruptserviceroutine(ISR). TheEXXinstructionexchangesthecontentsofHL,BC,DEwithHL',BC',DE'.The instructionEXAF,AF'treatstheresistersAandFasaregisterpairandexchangesthe contentswithAF'.IntheZ80/Z180theexchangeinstructionsweretheonlywaytoaccess thecontentsofthealternateregisters.Thismadeusingthealternateregistersfordata manipulationcumbersomeatbest.Rabbithasaddedacoupleofinstructionsthatmakeusing thealternateregistersasgeneralpurposeregisterseasier.Rabbitstatesthatthis“effectively doublesthenumberofregistersthatareeasilyavailable.” Thealternateregistersarestillabitcumbersometouseforcomputationordatamanipulation ascomparedtotheirregularregistercounterparts.Theyarebynomeans“easy”inthesense thatonemightassumethattheregularregistersandalternateregistersareorthogonalfroman opcodepointofview.However,theRabbitinstructionshavebreathednewlifeintoanawkwardlegacyfeature. TheRabbitCPU,likeitspredecessors,isstillanaccumulator-basedarchitecture.The8-bit Aregisteristheaccumulatorfor8-bitoperations.For16-bitoperations,theHLregisterpair istheaccumulator.Insomecircumstances,theIXandIYregisterscanbeusedasa16-bit accumulator. TheIIRandEIRregisterspointtointerrupttablesforinternalandexternalinterruptgenerators.TheZ80RregisterislogicallyanalogoustotheRabbit’sIIRregister. TheIPregisterisnew.The8-bitregisterisdividedintofour2-bitfields.Thefieldsareusedto holdahistoryoftheInterruptpriority.Thisisanimportanttoolformanagingthe4levelsof interruptprioritytheRabbitsports. TheXPCregisterisalsonew,andthereissomedebateastowhetherthisregistershould betreatedaspartoftheCPUcorearchitectureoraspartoftheMemoryManagementUnit (MMU).TheregisterestablisheswheretheXPCSegmentmapsinphysicalmemory.However,instructionsoperateonXPClikeaCPUregisterratherthananI/Omappedperipheral register.We,likeRabbit,willtreatXPCasaCPUregister. Rabbit’sdesignersexplainthattheXPCwascreatedasaCPUregistersothatXPCcanbe accessedquickly.WhenwetalkabouttheMMU,we’llseethattheXPCregisterisusedto locatetheExtendedMemorySegment.Sincethisistheprimaryavenueforaccessingthebulk oftheRabbit’sexternal20-bitaddressspace,theExtendedMemorySegmentisremapped frequently.SavingafewclockcyclesoneachaccesstotheXPCregisterwillfavorably impactcodeexecutiononRabbit-basedsystemsusingDynamicC.
40
TheBasics OtherMMUregistersareimplementedasI/Omappedperipheralregistersanddonotshow upintheCPU.TheonlyMMUrelatedregisteraffordedtheprivilegeofbeingaproperCPU registeristheXPCregister. TheIXandIYregistersareindexregisters.Theyarecommonlyusedaspointerstomemory locations.TheHLregisterpaircanalsobeusedasapointerintomemory. 2.5.2ParallelI/O TheRabbit3000hasseven8-bitdigitalI/Oports.The56I/Opinssharefunctionswithother on-chipperipherals.Forexample,ifasystemdesignrequiresaserialportthensomenumber ofdigitalI/Opinswillbetraded-off.Thisiscommoninmicrocontrollers. TheRabbit3000I/Oportshaveseveraldesirablefeatures.Oneofthebiggestpracticalconcernsistheabilitytointerfaceto5-voltlogic.TheRabbit3000isdesignedtooperateona 1.8–3.6voltsupply.However,alloftheRabbit’sinputs(withtheexceptionoftheoscillator andpowerpins)arefully5-volttolerant. RabbitI/OpinsarepoweredfromaseparatesetofpowersupplypinsthantheCPUcore.The VDDIOpinssupplycurrenttotheI/Opins.VDDCOREpinssupplycurrenttotheCPUcore. AsavvydesignercanreduceoverallEMIbyconfiningtheCPU’snoisyharmonic-richpower supplytoaverylimitedphysicalareaonthePCB.TheI/Opinscanbepoweredfromaseparate(filtered)supplyrail.TheEMIgeneratedaspartofthefastoscillatorandCPUswitching willnotbepassedtotheI/OpinsandthereforenotbeconductedontheI/Olines.Compliance withFCCandCEstandardswillbemucheasiertoachievewiththisarrangement. TheRabbitI/OpinsonportsD,E,FandGhavetheoptiontosynchronizetheiroutput updateswithatimer.Figure2.3showsaconceptualdiagramofanoutputwiththetimer synchronizationoption. HOLDING REGISTER CPU DATA CPU GENERATED LOAD CLOCK
D
OUTPUT REGISTER
Q
D
CLK
Q
OUTPUT PORT
CLK
TIMER GENERATED CLOCK
Figure2.3:Timersynchronizedoutputsallowforprecisetimingofoutputpulses.
Forsystemsthatneedtogeneratepreciselytimedpulses,timersynchronizationisanexcellentfeature.Thetimerthatupdatestheportcanalsobeusedtogenerateaninterrupt.TheISR canloadthenextscheduledvalueintothefirstflip-flopholdingregisterintheoutputchain. Thenexttimethetimertrips,theoutputwillbeupdatedandtheISRcalledagain.Thiscycle canberepeatedindefinitely. TimersA1,B1andB2canbeusedtoprovidethesynchronizedupdateclock. Anotherclocksourceavailabletoupdatetheoutputregisterssynchronouslyistheperipheral clock(PCLK).The“globalpowersaveandclockdistribution”,asseeninFigure2.1,generatesPCLKfromeitherthefastclockcrystal(dividedby2,4,6or8)orthe32.768KHzclock. 41
Chapter2 ApplicationsthatcantakeadvantageofthisfeatureincludePulsecodemodulation(PCM) audioplayback.Pulsewidthmodulated(PWM)DACscanalsobenefitfromsynchronizedbit transitions. 2.5.3RabbitSerialPorts Rabbithassixserialports.Atfirstglance,thismayseemlikeanoverabundance.However, thisisoneofthefeaturesthatcantracerootsbacktoZ-World.Overtheyears,oneofthe mostoftenrequestedfeaturesforZ-World’sembeddedcontrollerswas“moreserialports.” TheRabbit3000delivered. Allsixserialportsmaybeusedassimplethreewireasynchronousserialportswithamaximumbitrateofthesystemclockdividedbyeight.ThismodeissuitablefordrivingRS-232 transceiversifnohardwarehandshakingisrequired. Inasynchronousmode,a7-bitor8-bitdatalengthcanbeselected,withoptionalparity.A special9-bitprotocolisalsosupportedinwhicha9thbitissetorclearedtomarkthefirstbyte ofablocktransfer. EmbeddedsystemscommonlyemployprotocolsotherthanRS-232.OneofthemostcommonisthethreewireRS-485interface. RS-485hasanAandBlineinadditiontotheground-reference.Thishalf-duplexprotocol issensitivetothedifferenceinvoltagebetweenAandB.IfthevoltageA-Bispositiveand greaterthan200mVthenthedataisaone.IfthevoltageA-Bisnegativeandgreaterthan 200mVthenthedataisaZERO. RS-485isdesignedsothatmanynodescansharethesameAandBsignals.Onlyonenodeis allowedtotransmitatatime.ThisfeatureiswhyRS-485isreferredtoa“multi-dropprotocol.” OneissuethatZ-WorlddiscoveredearlyonwiththeZ180UARTisthatdetectingwhen thelastbitwasfinallyshiftedoutoftheoutputregisterandontoanRS-485buswasvery difficult.ThismadeturningaroundtheRS-485busabittricky.IftheRS-485transmitter weredisabledtooquickly,datawouldbelost.Iftoomuchtimeelapsedbeforethebuswas released,thentheRS-485transmittermightcausebuscontentionwithanothernode. BuildingonZ-World’sexperience,RabbitSemiconductorenabledtheRabbit3000UARTs togenerateaninterruptwhenthelastbitisfinallyshiftedoutontothebus.Thisishowthe RabbitcanquicklyandsafelyturnofftheRS-485transmitter,freeingthebusfortrafficoriginatingfromanothernode. PortsA,B,CandDcanalsobeoperatedinsynchronousmode.Thisissometimescalled “clockedserial”mode. Whenoperatinginsynchronousmode,eithertheRabbit3000oranexternaldevicecangeneratetheclock.IftheRabbit3000generatestheclock,themaximumclockrateisequaltothe systemclockdividedbytwo.Ifanexternaldevicegeneratestheclock,thenthemaximum clockrateislimitedtotheRabbit’ssystemclockdividedbysix.Inthismode,SPIdevices maybeattachedtotheUARTs.
42
TheBasics PortsEandFsupportHDLC/SDLCcommunication.Thisprotocolsupportsframechecking, clockrecoveryanderrorchecking.HDLChasfounduseinhigh-endrouterback-channel communicationinterfaces,andinternalmainframecomputerbusses.HDLCisnotcommonly supportedbymicroprocessorsinRabbit’spricerange.IfHDLCisrequiredforanapplication, adesigneroftenhastopurchasespecialUSARTstoaccommodatetherequirement. TheRabbit3000supportsNRZ,NRZI,Biphase-level(Manchester),Biphase-space(FM0) andBiphase-mark(FM1)encoding.Adigitalphase-lockedloop(DPLL)isusedtorecover thedataclockfromthereceivedsignal. AstandardCRC-CCITTpolynomial(x16+x12+x5+1)isusedtogeneratethecyclicredundancycheck(CRC)code.Frameflagsaregeneratedautomatically.Framingerrorsarealso detectedautomatically. TheHDLCprotocolisusedforhigh-speedcommunicationsusinginfraredIrDAtransceivers inFIRmode. PortAcanbeusedtocold-boot(alsocalledbootstrap)theRabbitprocessor.Normally,upon reset,theprocessorbeginsexecutingcodefromthememorylocatedatphysicaladdress 0x00000.Asageneralrule,Rabbit-basedsystemswillhaveaROMorflashmemorylocated at0x00000.Inthecasethattheflashmemoryisnotprogrammed,orinsystemsthatoperate purelyfromRAM,theprocessorcanbeforcedtoacquiredatafromabootstrapport. SMODE1andSMODE0pinscontrolthebootstrapoperation.Whentheyarebothzero,the processorbootsnormally.WhenSMODE1andSMODE0are{1,0},theRabbitwillboot fromserialportAinasynchronousmode.TheRabbitrequiresanexternaldevicetoprovide theclockforthesynchronousboot.ThisprecludestheuseofanEEPROMasabootdevice. WhenSMODE1andSMODE0are{1,1},theRabbitwillbootfromserialportAinasynchronousmode.Thecommunicationrateis2400baudandisderivedfromthe32.768kHzclock. WhentheRabbitisbootstrapping,itexpectstoseethree-bytemessages,calledtriplets.Each tripletiscomposedofa2-byteaddressandonebyteofdata.EachtripletcausestheRabbit tostorethedatabyteatthe16-bitaddresswiththeexceptionthatifthemostsignificantbit (MSB)oftheaddressisa1,thenthedatawillbewrittenintotheaddressedonboardperipheralregister. ThisallowsabootloaderprogramtobeloadedintoSRAMandtheMMUregisterstobeconfiguredsothattheSRAMappearsatphysicaladdress0x0000. Thesequence0x80,0x24,0x80willcause0x80tobewrittentotheinternalperipheralregister0x24.Thiswillterminatebootstrapoperation;itallowsthenewlyloadedcodetobegin executingataddress0x00000. 2.5.4SlavePort Inthepast,processor-to-processorcommunicationswassomewhatdifficult.Toovercomethis problem,dual-portedRAMhasoftenbeenusedtoprovideasharedspaceaccessibletotwo separateprocessors.Evenwithsiliconpricesonanexponentiallydecreasingcostcurve,dualportingisexpensive.Leadtimesforsuchspecialtyitemscanbelong. 43
Chapter2 Dedicatedserialchannelsareanotherschemeoftenemployedtocommunicatebetween processors.ThisapproachrequiresfewwiresandUARTsarefairlyinexpensive.Formany applications,adedicatedserialportisareasonablesolutiontoprocessor-to-processorcommunication.TheRabbit,aswehavejustseen,sportssixserialports. Sometimes,adesignermaynothaveanextraserialport.Orperhapstheprocessorsarerunningonslow-speedclockstoconservepowerandtheavailableserialportbandwidthisoverly limited.Insuchcases,theRabbitprocessoroffersaprocessor-to-processorparallelcommunicationchannelcalledaslaveport. Thechosenmonikerisabitunfortunate.Oneoftenthinksof“slave”devicesbeingdumb. TheRabbit“slaveport”isbestthoughtofasaspecializedcommunicationsportforhighefficiencyprocessor-to-processorcommunications. TheslaveportallowsaRabbitprocessortobeplacedonasecondRabbitprocessor’sI/Obus andappearasaperipheral.Theslaveportimplementssixregisters.Threeofthesecanbe writtenfromtheexternalbusandreadbythe“slave”processor’sinternalCPU.Theremainingthreeregisterscanbewrittenbytheslaveprocessor’sCPUandreadfromtheexternalbus. Thesesixslaveportregistersdon’tactexactlyasadualportRAM,buttheydoprovidea meansofhigh-speedparallelcommunicationbetweentwoRabbits.Althoughthefollowing examplesshowRabbit-to-Rabbitconnections,thereisnothingthatprohibitstheRabbitslave portfromconnectingtoanotherprocessororevenaCPLDorFPGA. Figure2.4showsadiagramoftheslaveportsignals.Thedatapathis8-bitswide.Thetwo addresssignalsactasregisterselects.The/SWRand/SRDdeterminewhichwaydatawill beflowingonthebus.The/SCSsignalenablestheport./SLAVEATTNcanbeusedbythe Rabbittosignalthe“master”thatthe“slave”requestsatransaction. Theslaveportisversatile;theCPUsmaybe“masters” or“slaves”orevenboth.Figure2.5illustrateshowsuch creativitymightmanifestinadesign. Theslaveportopensupthepossibilityofbuildingvery smartperipheralsyethavingthemcommunicatewithother systemprocessorsovertheI/Obus.Forexample,consider aRabbit“slave”processorthatactsasacommunication subsystem.Theslavemighthaveserialstreamsfromsix separateI/Odevicesconvergeonthesixserialports.The slavecouldnotonlyhandlethebitlevelserialtransactions, butimplementhigherlevelprotocols,datacompression/ decompression,errordetection/correctionsandparsing. Thesystem“master”processormaybeattendingtolocal I/Otasks,control-loopsandMMIresponsibilities.Offloadingthebulkofthecommunicationburdentotheslave processorallowsthemastertoonlyhavetodealwiththe highestleveldetailsofthemessaging. 44
SD0-SD7 SA1 SA0 SWR SRD SCS SLAVEATTN
Rabbit 3000
Figure2.4:TheSlaveport presentsaseamlessinterface tomanyCPUI/Obusses.
TheBasics 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
Figure2.5:Slavescanhaveslaves—thepossibilitiesareendless.
Inadditiontofosteringprocessor-to-processorcommunications,theRabbit’sslaveportmay beusedtocold-boottheprocessor.Uponreset,iftheSMODE1andSMODE0pinsareheld ina{0,1}configuration,theRabbitwillacceptbootcodeovertheslaveportinmuchthe samewaythatbootdatacanbeprovidedoverserialchannelA. 2.5.5InputCapture OneofRabbit’smoreuniquefeaturesisthedualchannelInputCapturemodule.Thisclever bitofengineeringallowstheRabbit3000topreciselymeasurethetimebetweenexternal digitalevents.Thiscanbeusedformeasuringpulsewidthandfrequency.Themodule’sabilitytogenerateinterruptsallowsthedesignengineertoadduptofourexternalinterruptstothe Rabbit-basedsystemevenifthemodule’seventtimingcapabilitiesgounused. Figure2.6showsaconceptualmodelofasingleinputcapturechannel.TheSourceRegister (ICSxR)determineswhichofthebitsfromPortsC,D,ForGwillbethesourceofthestart andstopconditions. TheTriggerRegister(ICTxR)determineswhetherthestartandstopconditionswillberisingedgeorfalling-edgetriggeredortriggeredonanyedge.Thisregisteralsodetermineswhether a“start”or“stop“eventwilllatchthevalueofthe16-bitcounterintotheCaptureRegister. ICTxRcontrolsoptionsforgeneratinganinterruptandstartingandstoppingthecounter. TheInputCaptureControlRegister(ICCR)specifieswhatinterruptpriorityistobeusedby theInputCapturemodule.BothInputCapturechannelssharethisregister. AprogrammayresetthecounterthroughtheControlandStatusRegister.Thisregisteralso containsa“disable/enableinterrupts”bitfortheInputCapturemodule.ByreadingtheICCSR, 45
Chapter2 aprogramisabletodetermineifastartorstopconditionhasoccurredorifthe16-bitInput CaptureCounterhasrolledover.TheICCSRissharedbetweenbothInputCapturechannels. TheonlyregisterssharedbetweenthetwochannelsaretheICCSRandtheICCRregisters. EachInputCaptureChannelhasa16-bitcounteraswellassupportregisters. InFigure2.6,wehaveusedan‘x’todenoteapositioninaregisternamewherea‘1’ora‘2’ maybeplacedtoidentifyaregisterwithaspecificInputCapturechannel.Forexample,the ICT1RistheInputCaptureTriggerRegisterforchannel1,whileICT2RistheTriggerregisterforchannel2. Thesystemperipheralclock(PCLKorPCLK/2)feedstimerA8.TheA8timercandivide itssourcebyn+1,wherenis0to255.ThismeansthehighesttimeresolutionoftheInput Captureis,bestcase,one-halfperiodofPCLK/2. RabbitSemiconductorsuggeststhreeoperatingmodesfortheInputCapturemodule: 1. Thecounterisenabledbythestartconditionandhaltedbythestopcondition.The stopconditionalsolatchesthecountervalueintotheInputCaptureRegisters. 2. Thecounterrunscontinuouslyandthestartandstopconditionslatchthecounter valueintotheCaptureRegisters. 3. Thecounterisstartedbysoftwareinstructionsandhaltedbythestopevent. Modeoneisgreatformeasuringpulsewidthorformeasuringthetimebetweenexternal events.Forexample,apositivegoingpulsecanbemeasuredbyconfiguringtheInputCapturemoduletoacceptarisingedgeasastarteventandthefallingedgeasthestopevent.The InputCaptureCountercanbeclearedandtheInputCapturemoduleconfiguredtostartthe counterwhenthestartevent(risingedgeofourpulse)occurs.Thereisonecaveat:thesystem willstopcountingevenifastopsignaloccursbeforeastartsignal,soyoudoneedtosynchronizeproperly. Onceastopeventhappens,thecounterishalted,andthecontentsarelatchedintotheInput CaptureRegister.TheapplicationprogramneedonlylookattheInputCaptureRegistertosee howlongthepulsewas. Provisionsareincludedtocontendwithcounterrolloverandinterruptgeneration,toalertthe applicationprogramthatthestopeventhasoccurred. Modetwocanbeusedtotimestampevents.Eachstarteventandeachstopeventlatchthe contentsofthecounterintotheInputCaptureRegisters.Inmostcases,theInputCapture modulewillalsobeprogrammedtogenerateaninterrupttoinformtheapplicationprogram thataneventhasoccurredandthedataintheInputCaptureRegistersneedstobeprocessed beforeafutureeventoccursandoverwritesthetimestampintheInputCaptureRegisters. Sincestartandstopeventsdonothavetooccuronthesameexternalpin,thismodecanbe usedsimplytogenerateinterrupts.Thisgivesasystemdesignertheabilitytohaveuptofour moreexternalinterruptsignals(astartandstopeventontwochannels).Thisisasimpleway ofexpandingtheavailableexternalinterruptpins. 46
TheBasics 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
Figure2.6:TheinputcapturechannelsareoneofRabbit’smostversatileI/Ofeatures.
Modethreeallowsthetimertofree-run,andonlythestopeventwillhaltthetimer.Thisis usefulformeasuringthetimebetweentheapplicationprogramclearingtheInputCapture timerandanexternaleventoccurring. 47
Chapter2 Theinputpinsforthestartandstopeventsneednotbethesamepin.Theyneedn’teven beonthesameport.ThetwoInputCapturechannelscanshareanexternalpinasanevent generator.TheRabbitInputCaptureconfigurationregistersallowversatileandcreative configurationoftheInputCapturemodule.Forbitleveldetailsoneachregister,refertothe Rabbit3000MicroprocessorUser’sManualincludedontheCDpackagedwiththisbook. (ThelatestcopyisavailableontheRabbitwebsite.) 2.5.6QuadratureDecoder ThequadraturedecodermoduleisanotherRabbitSemiconductorinnovationthatfelloutof Z-World’slongexperienceintheembeddedmarket.Z-Worldhasknownforyearsthatoptical quadratureencodersarecommonlyusedinrotatingmachineryasfeedbackforshaftposition. InkeepingwithRabbit’sphilosophyof“letthesilicondothework,”theRabbit3000sports an8-bitquadraturedecodermodule. BeforelookingatthedetailsoftheRabbit’squadraturedecoder,let’squicklyreviewwhat quadratureencodingisandwhereitisused. Quadratureencodingusesatwo-bitGraycodetoindicatethedirectionoftravelofamechanicaldevice.Mechanicalandopticalencodersareavailablethatimplementquadratureencoding onrotaryshafts.Theserangeinpricebasedontheangularresolutionofthedeviceaswellas thequalityofthemechanicalconstruction. Allquadratureencoderscontainsomesortofencoderwheel,suchastheoneshowninFigure 2.7.Theencoderwheelisreadopticallyormechanicallyandanelectricalsignalisproducedon twowires.ThetwoelectricalsignalsaredesignatedastheIn-phase(I)signalandQuadrature(Q) signal.InFigure2.7,theI-signalcorrespondstotheouterringandtheQ-signaltotheinnerring. Theideabehindaquadratureencoderisthatthebitsequenceproducedwillpresentunique transitionsdependingonthedirectionofrotation.Unitdistancebinarycodes,suchasthe two-bitreflectiveGrayCodeshowninFigure2.7,areideallysuitedforthispurpose.The codesequenceshowninthetableatthebottomofFigure2.7illustrateshowtheIn-phaseand Quadraturesignalsvaryasafunctionofangulardisplacement. Thecodesequenceinthetableprogressesfromthelefttotherightandthiscorrespondstoa clockwisetraversalofthewheel.Carefulinspectionofthecodesequencewillrevealthatno transitionthatoccursonaclockwisetraversalwillappearinacounter-clockwisetraversal. Forexample,thetransitionofIQfrom{1,1}to{0,1}willonlyappearonaclockwisetraversal.Similarly,thesequence{1,1}to{1,0}willonlyappearonacounter-clockwisetraversal. Figure2.7pickedanarbitraryrotationoftheringtocorrespondto0°(IQ={1,0}).Theencoderwheelwasstructuredsuchthataclockwisetraversal(counterclockwiserotation)will createacodesequencethatwillcausetheRabbit’squadraturedecodemodule’s8-bitcounter toincrement.Likewise,counter-clockwisetraversalwillcauseRabbit’scountertodecrement. Rabbit’squadraturedecoderiseasytouse.Therearetwochannels.Theysharecontroland statusregisters,buteachchannelhasaunique8-bitcountertokeeptrackoftheencoder’s position. 48
TheBasics
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
Figure2.7:Opticalencodersallowthemonitoringofrotationaldirectionand withsomeadditionalprocessing,speedandpositioncanalsobeobtained.
49
Chapter2 Figure2.8showsablockdiagramofthequadraturedecoderchannels.TheIandQinputscan beselectedfromPortF.Somecaremustbetakenwhenhookingupaquadratureencoderto portF,sinceonlypredefinedpinpairscanbeused.PortFbits“0and1”orbits“4and5”may beusedtoconnecttochannel1.PortFbits“2and3”orbits“6and7”maybeusedtoconnecttochannel2.TheoddnumberbitsalwayscorrespondtotheI-signal. Thecontrolregistersallowapplicationcodetodetermineifacounterhasrolledover.The QuadratureDecodermayalsobeconfiguredtogenerateaninterruptwhenacountermakesa transitionbetweenthe0xFFand0x00states.Theinterrupt,ifenabled,willoccurregardless ofthetransitiondirectionbetweenthetwostates(0xFF⇒0x00or0x00⇒0xFF). TheQDCSR(QuadratureDecodeCtrl/StatusRegister)allowsanapplicationprogramtoreset eitherchannel’scounterto0x00.Thisallowsanapplicationprogramto“home”thequadratureencoderwheeltocorrespondtoa“count”of0x00.
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
Figure2.8:Twoindependentchannelsofquadraturedecoding givetheRabbitgreatflexibilityincontrollingrotarysystems. 50
TheBasics ItisnoaccidentthattheInputCaptureModuleandtheQuadratureDecodermoduleboth allowinputsfromPortF.ThepulsewidthofeithertheIn-phaseorQuadraturesignalwill beproportionaltothevelocityoftheencoderwheel.TheInputCaptureModule,shownin Figure2.6,onlyallowsmeasurementsfromtheoddnumberedbitsofPortF(orC,DorG). ThiswillcorrespondtotheI-signalifaquadratureencoderisconnected. AcleverengineerwillcombinetherotationaldisplacementinformationfromtheQuadrature DecoderModulewithvelocitydatagatheredthroughtheInputCaptureModuletogetacompletepictureofthebehaviorofanyrotatingmachinerytheRabbitismonitoringorcontrolling. 2.5.7PulseWidthModulation(PWM)Channels PulseWidthModulationisoneofthemostversatiletoolsavailabletotheembeddedsystem designer.EverythingfromaudiogenerationtomotorcontrolcanbeimplementedwithPWM. Recognizingthis,RabbitSemiconductordevelopedaPWMmodulethatprovidesfourindependentchannelsofPWMoutput.Rabbitwentonestepfurtherthanmanycompanies—the Rabbit’sPWMmoduleallowsthesystemdesigneroptionallytospreadthe“on-state”power overtheoutputwaveform’speriod.Insomeapplications,suchasaudiooutputoraDAC implementedwithalowpassfilter,therippleinthefinalwaveformisreduced. Let’sfirstlookatwhatPWMisandthenseehowRabbit’sPWMmoduleoperates. Figure2.9showsapulseandthedefinitionsforsomecommonterms.Assumingthatenergy inapulseiscontainedcompletelyinthe“ontime”portionofthewaveform,thedutycycleis directlyproportionaltotheenergy.100%dutycycleimpliesasteadystatehighvoltage.0% dutycycleimpliesa“pulse”thatisoffallthetime.Byvaryingthedutycycleofthepulse,we cancausetheenergyinthewaveformtogofrom0to100%. Percent Duty Cycle =
On Time ⋅ 100% Period
Pulse Period On Time
Off Time
Voltage
Time
Figure2.9:PulseWidthModulationisnothingmorethanvaryingdutycycle.
Theterm“pulsewidthmodulation”derivesfromanassumptionthatthepulseperiodisfixed. Inthiscase,the“pulse”isthoughtofthehighpartofthewaveforminFigure2.9.“Ontime” isequivalenttopulse-widthwhenwearetalkingaboutPWM. 51
Chapter2 Somenewcomersconfuse“pulsewidth”with“pulseperiod.”Carefulattentiontonomenclatureanddefinitionsisimportant. TheRabbitPWMmoduleisstraightforward—asingle10-bitfree-runningcountercounts from0x000to0x3FF(0to1023)androllsover.TheapplicationprogramloadsaPWM “width”registerwithavalue“n.”Outof1024counts,thePWM’soutputpulseishighforn+1 counts.Thisgivestheapplicationtheabilitytovarythedutycyclefromabout0.1%to100%. 1024ticksfromtimerA9definetheoverallperiod. Figure2.10showsablockdiagramofthePWMmodule.IntheregisternamesPWMxRand PWLxRweareusingan“x”toindicatethatanumberbetween0and3canbesubstitutedto controlPWMchannel0to3. ToconnectthePWMmoduletoapinonPortF,thePortFfunctionregister(PFFRispart ofPortFandisnotexplicitlyshowninFigure2.10)mustbeprogrammed.Ifanapplication needstorunaPWMoutputallthewaydownto0%duty-cycle,PFFRcanbeprogrammedto disconnectthePWMmodulefromthePortFpinandthecorrespondingPortFpincansubsequentlyprogrammedtobeoff. Figure2.10showsthatonlythreebitsofthePWMLSBregister(PWLxR)actuallygotothe PWMstatemachines.Twoofthesebitsareconcatenatedwiththe8bitsfromthePWMxR registertoformthe10-bitvalueofn.ThethirdbitinPWLxRdeterminestheRabbit’sPWM energyspreaderisenabled. ThePWMfrequencycanbecalculatedfromtheformula F=Fcpu/2/1024/(TAT9R+1) anditisthesameforallfourPWMsignals. 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
Figure2.10:TworegistersareallthatareneededtocontroleachPWMchannel.
52
TheBasics ThereareacoupleofwaystothinkaboutthePWMspreader.Theeasiest,butnotcompletely accurate,wayistosimplythinkofthespreaderasa4×frequencymultiplier.Thereasonthis isnotaccurateisthattheactualtimerticks,andthereforeresolution,donotchangebasedon thespreaderbeingenabledornot. Figure2.11,borrowedfromRabbit’sUser’sManual,showshowthePWMspreaderaffectsa seriesofsampleconfigurations.TheinterestingbitisthattheRabbit’sspreaderautomatically adjuststhepulsewidthstobedifferentlengthstotalingupto“n+1”completeticksofA9so thetotalenergyinagiven1024tickperiodisthesameregardlessofwhetherthespreaderis enabledornot.NoticeinFigure2.11whenn=257,thespreaderautomaticallyalternatesthe short(64count)andlong(65count)pulses. ThemostaccuratewaytodescribethePWMspreaderisasadevicethatbreaksupa1024 countperiodintofourquadrantsanddistributesn+1totalcountsof“ontime”acrossthefour quadrantsasevenlyaspossible. 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
Figure2.11:Thespreaderdistributestheon-timeenergy acrossfourquadrantsinthe1024countperiod.
2.5.8AuxiliaryI/OBus TheAuxiliaryI/OBusisstrokeofgenius!AsZ-Worldlearnedovertheyears,interfacinga bunchofI/Odevicestoamemorybus,especiallyahigh-speedmemorybus,causesallmannerofproblemsassociatedwiththecapacitiveloadingassociatedwiththeI/Odevices.The Rabbit3000givessystemdesignerstheoptiontokeeptheCPU’smemorybusseparatefrom theI/Obus.Thisoccursatthesacrificeof14I/OpinsfromPortsAandB.Furthermore,the useoftheSlavePortandtheAuxiliaryI/Obusismutuallyexclusive. 53
Chapter2 TheAuxiliaryI/Obususesthe8bitsofI/OPortAasadatabus.6bitsfromPortBprovide theleastsignificant6bitsofthe16-bitI/Oaddress.6bitsisonly64uniqueaddresses,butfor manysystemsthisismorethanadequate. WhentheAuxiliaryI/Obusisused,I/Otransactionsoccursimultaneouslyonthemain systembus.Ifadditionaladdresslinesareneeded,theycanbepulledfromthemainbusand buffered.Ifonlyoneortwosignalsareneeded,adesignermightconsiderusingtheUHS familyTinyLogic™buffersfromFairchildSemiconductor.TheseofferasmallSOT-23footprintandonlycostpennies.TexasInstrumentshasasimilarlineofproducts. WhenusingaRabbitCoremoduleinadesign,theAuxiliaryI/OBusisoftentheonlyI/O busavailable.Thiskeepsthetracelengthsofthehigh-speedmemorybusshortandunwanted EMIminimized.TheAuxiliaryI/ObusisoneofRabbit’sstrongestfeaturesforrealworld embeddeddesigns. 2.5.9TimerA Rabbithastwotimermodules—TimerAandTimerB.OfallorRabbit’smodules,thetimer modelscatchthemostflakfromusers.Unfortunately,thetransistorfairyhascomeandgone. WearestuckwiththetimersastheysitintheRabbit3000.Solet’sseehowtheyoperateand howRabbitintendedeachtobeused.Fromthere,thesystemdesignercanmakethemostof what’savailable. TheTimerAmoduleisactually fairlystraightforward.Figure 2.12showsablockdiagram oftheTimerAmodule.Each timer,A1-A10,isan8-bitdowncounterwithanaccompanying reloadregister.Thisallowseach countertodividetheincoming clockby1to256.
PERCLK
A1 ÷2
A3
A4
A5
AquickglanceatFigure2.12 showsthattheoutputofevery timerhasapreferredfunction.In thecasesofA8,A9andA10the timerscannotgenerateaninterruptandareonlyusefulfortheir assignedtask. TheA8,A9andA10timersmay afirstglancelookasgenericas A2–A7,butalastheyarenot. Thesethreeresourcesserveonly theirI/Omodulesandcannot generateinterrupts.Theycannot
A2
Serial E
Serial F
Serial A
Serial B
A6
Serial C
A7
Serial D
A8
Input Capture
A9
PWM
A10
Quadrature Decode
Figure2.12:TimerAconsistsoften8-bittimers. 54
TheBasics beusedasprescalersorbecascadedwithA1.Theyareintendedtoproducerelativelyfast clocksfortheirrespectiveI/Omodules.Theyfulfilltheirpurposewell.Someengineersfindit lesspainfultothinkofthesetimersaspartoftheirassignedmodules. A1canbeusedasaprescalerforA2–A7.However,unlessultraslowbitratesareused,A1 isn’tusuallyneededasaprescalerforbaudrategeneration.A1cangenerateaninterrupt whenitcountsdownto0x00.UnlessA1isbeingusedasaprescaler,itisavailableforgeneral-purposeinterruptgeneration. A2–A7cangenerateaninterruptwhentheircountsreach0x00.Ifthesearenotbeingusedto generatebaudrates,theycanbeusedasgeneral-purposetimebases.Figure2.12showsthat thesetimersmaynotbecascadedtoproducelowfrequencyinterrupts.Furthermore,theTimer Amodulesharesasingleinterruptamongallthetimerswithinterruptsenabled.TheInterrupt ServiceRoutine(ISR)isresponsiblefordeterminingwhichtimer(s)trippedtheinterrupt. RabbitSemiconductorhasbeenverycarefulintheirdesignregardingthebitsthatindicate ifaparticulartimer(A1–A7)hasrolledover.Undernocircumstanceswillabitbedropped ifmultipletimerssimultaneouslyrollover.WhenanISRreadstheregister(TACSR—Timer AControlandStatusRegister)containingtherolloverbits,theISRmustbeexhaustivein theinspectionofthesebitsastheyareclearedinTACSRuponread.Ifmultipletimershave expired,thentheISRmustexecutethecoderequiredtoserviceeachtimerthatexpired. IfadditionaltimersexpirewhiletheISRisrunning,anewinterruptwillbegeneratedonce theISRhascompletedandtheinterruptpriorityislowered.Undernocircumstanceswillthe RabbitTimerAModuleloserolloverinformation. ThetimersA1–A10maynotbereaddirectly.Theymaynotbeusedtotimestampevents. However,theInputCaptureModule,drivenbyA8,canbeusedasatimestamp.TimerBmay alsobereaddirectlybytheCPU.
55
Chapter2 2.5.10TimerB Figure2.13showshowthetwo“timers”B1andB2arearranged.NotshowninFigure2.13 aretwocontrolregistersthatdeterminefromwhichsourcethe10-bitcounterisclockedand whethera“match”willgenerateaninterruptornot. Thereisnomethodtoresetthe10-bitcounter.ThismeansthattouseB1orB2togeneratean interruptonascheduleotherthan1/1024oftheselectedclocksource,theISRmustcompute thenext“match”valuerequiredtogetthenextIRQatthedesiredtime.Whileunorthodox(to bekind),thisarrangementisfunctional. Toreadthevalueofthe10-bitcounter,aspecialprocedure,describedintheRabbitMicroprocessorUser’sManual,mustbefollowed.Thisisrequiredbecausethe10-bitcountercan’t berepresentedinasingle8-bitbyte.Sincetwobyteshavetobereadtoacquirethecounter’s state,thepossibilityexiststhatinthetimebetweenthetworeads,acounterrollovermayhave occurredthusinvalidatingthefirstbyteread. 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
Figure2.13:TimerBisacreativearchitecture. 56
TheBasics Besidesbeingusefulforgeneratingperiodicinterrupts,B1andB2canbothbeusedtosynchronouslyupdateparallelI/O. Bymoststandards,TimerBisabitofapaintouse.Therepeatedcomputationofreloadvaluesisannoying.ThefactthatB1andB2shareacommoncounterandtimebaseisalittleless thanoptimal.However,whatthehardwarelacks,thefirmwarecanmakeup.Thetrade-offis simplyCPUcycles. 2.5.11ClockSpreader Electromagneticregulatorycomplianceisaveryrealissueforcompanies.Testingisexpensive.Redesigningcircuitsinaproductthatisalreadycomplete,justtopassgovernmentEMI tests,isexpensiveandtimeconsuming. RabbitSemiconductorlookedatZ-World’sexperiencewithEMIandcreatedaClockSpreadertoreduceEMIatthesource.MostEMIissuesoccuratharmonicsofthesystemcrystalsor CPUbusperiods. TheClockSpreaderworksbyintroducingaseeminglyrandommodulationonthecrystal oscillator,thusspreadingouttheenergyassociatedwiththeoscillator’sfundamentalandharmonicfrequenciesoveragreaterbandwidth.Thisreducespeakemissionsandmakespassing governmentEMItestsmucheasier. TheClockSpreaderhasthreesettings:OFF,NORMALandSTRONG. Thesettingsoperateasonewouldintuitivelyexpect.OFFallowsthesystemcrystaltoringat themostpurefrequencypossible.Thishasthebenefitofensuringthemostpredictablesystem widetiming.Baudratesderivedfromthecrystalwillbeasaccurateaspossible.Thedisadvantageisthatinthesedaysofever-increasingEMIregulations,thesystemwillalsohavethe highestemissions. TheNORMALandSTRONGmodesintroduceasmallormodestamountofmodulation (spreading).Themoremodulation,themoretheundesiredharmonicenergyisspreadout makingEMCcomplianceeasier. InsomeextremelyraresituationsiftheClockSpreaderissettoSTRONGandthehighest baudratesarebeingusedwithasynchronousserialchannels,somebiterrorsmayoccur.Only veryrarelywilladesignerrunintoabaudrateissue.Ifonedoescropup,theClockSpreader canbedialedbackfromSTRONGtoNORMALoreventoOFF. 2.5.12ClockDoubler TheRabbit3000sportsaClockDoubler.Slowspeedexternalcrystalscanbeusedand doubledbytheRabbit3000.OneapplicationfortheClockDoubleristoallowtheCPUthe optionoftradingpowerconsumptionagainstcomputationalhorsepower. Asystem’spowerconsumptionisroughlylinearlyproportionaltothesystemclockrate.For example,turningontheClockDoubler,theRabbit3000canapproximatelyhalvethetime requiredtocomputearithmeticoperations.However,whentheapplicationisn’t“computing,” theClockDoublercanbedisabled,allowingthepowerconsumedbytheCPUtobereduced by50%. 57
Chapter2 2.5.13MMUandMIU Thememorymanagementunit(MMU)andmemoryinterfaceunit(MIU)combinetogive Rabbit3000theabilitytodirectlyaddresssixmegabytesofmemory.That’sahugememory spaceforalittle8-bitprocessor. Currentlyavailablecompilersonlyhandleuptoa1-megabyteaddressspace.ThisisinkeepingwiththeRabbit’s20-bitphysicaladdressbus.Theoverheadofswappingmemorypages throughtheChipSelect,OutputEnableandWriteEnableorselectiveinversionofA18and A19linesisnotincurred.Nonetheless,cleverengineerscouldusetheRabbittoaddressafull sixmegabytesiftheyarewillingtodothebookkeepingmanually. Beingan8-bitprocessor,Rabbit’sexternalandinternaldatabussesare8bitswide.Thisdoes nottheoreticallyconstrainthesizeoftheexternaladdressbus.Supportingawideaddressbus requirestheCPUtodomultiplefetchesacrossthe8-bitwidedatabustoacquireinstruction addressoperands.
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
Figure2.14:TheMMUcreatessegmentsinthe16-bitlogicaladdress spacethatallowaccesstowindowsinthe20-bitphysicaladdressspace. 58
TheBasics Forexample,a16-bitwideaddressbusrequirestheCPUtoperformthreereadcyclesoverthe 8-bitdatabustofetchacompleteinstructionthatloadsaregisterfromamemorylocation.The firstreadacquiresthe“load”instruction.Thetwofollowingreadcyclesacquiretheaddress. A16-bit(64KB)addressspaceisperfectlygoodformanyapplications.TheTandyTRS-80™ ModelIIIbusinesscomputerranwithonly48KBofstockmemory.The1980sarewellbehind usandmodernsystemsareexpectedtosupportlargermemorymodels. RabbitSemiconductorwasunwillingtotradetheoverheadneededtofetchathirdbytefor eachoperandcontaininganaddress.Toeliminatethisinefficiency,RabbitadoptedanMMU. Thismodulemapsa16-bitlogicaladdressintoa20-bitphysicaladdress.TheCPUinstructionsgenerallyoperateon16-bitaddressesandthereforeonlytworeadcyclesarerequiredto fetchaddresses.Rabbithasalsoincludedsomespecialinstructionsthatoperatedirectlyon 20-bitaddresses.Theseinstructions,whileuseful,areslowerthaninstructionsthatoperateon 16-bitaddresses. TheMMUmapsthesmall16-bitlogicaladdressspaceintothelarger20-bitphysicalspaceby creatingwindows,called“segments.”Figure2.14illustratestheideaofmappingwithsegments. ThesegmentsarenamedmoreorlessforhowDynamicCusesthem.Forthisdiscussion,we arenotconcernedwithhowDynamicCmanipulatestheMMU(thisiscoveredinChapter3). HereweareonlyconcernedwithhowtheRabbit’shardwaretreatstheaddressspace. Thefirstsegment,calledtheRootSegmentorBaseSegment,isforcedtostartatlogical address0x0000andphysicaladdress0x00000. ThesizeofthesecondandthirdsegmentsisdeterminedbythecontentsoftheSEGSIZE register.ThefourleastsignificantbitsofSEGSIZEspecifythelogicaladdresswherethe secondsegment,calledtheDataSegment,starts.ThefourmostsignificantbitsfromSEGSIZEspecifythelogicaladdresswherethethirdsegment,calledtheStackSegment,starts. Thefourthsegment,calledtheXPCSegmentorExtendedMemorySegmentalwaysstartsat logicaladdress0xE000. Uponreceiptofa16-bitlogicaladdress,theMMUdecidestowhichsegmenttheaddress belongs,basedonSEGSIZE.ThentheMMUcomputesa20-bitphysicaladdressbyadding the16-bitlogicaladdresstoasegmentmappingregisteruniquetoeachsegment. Figure2.15showsthethreemappingregistersaccessiblebytheuserandhowtheyareadded tothe16-bitlogicaladdresstoforma20-bitphysicaladdress.TheXPC,STACKSEGand DATASEGregistersare8bits.TheXPCisactuallyaproperCPUregistersothatitcanbe changedquicklybyanapplication.STACKSEG,DATASEGandSEGSIZEareinternal peripheralI/Oregisters. Figure2.15showsthatachangeoftheleastsignificantbitinXPCwillmovetheXPC segmentinphysicalmemoryby4K.Thereasoningbehindthefixed8KsizeoftheXPCis nowexplainable. TheXPCsegmentismappedintophysicalmemorywithagranularitythatisone-halfofits size.ThisallowstheXPCsegmenttobeslidoverlargeslicesofcodeorlargedatastructures inmemorywithoutcompletelylosingsightoftheimagejustviewed. 59
Chapter2 Forexample,ifa12Kspaceinphysicalmemorycontainscode,initiallythe XPCsegmentcouldbemappedover thefirst8K.Asthecodeexecution progressestothe6Kmark,theXPC segmentcouldbeadjustedtoviewthe 4K–12Kportion.Thisallowsthecurrentlyexecutingcode(near6K)tobe visibleinbothmappings. Asitturnsout,thisabilitytoslidea segmentgraduallyacrossphysical memorymakeslifeeasierforcompiler designers.TheZ180MMUdidnothave thisfeature—RabbitSemiconductor addedittotheirMMU. Rabbit’smemorymanagementissplit betweentwomodules.Aswehaveseen, theMMUisresponsibleformapping a16-bitlogicaladdressintoa20-bit physicaladdress.TheMIUcontains potentmojotoimplementseparate instructionanddataspaces,generate externalchipselects,outputenablesand writeenables.TheMIUalsoiswhere theRabbitobtainsthecapabilitytoaccessafullsixmegabytesofmemory. Figure2.15showsthattheMIUhasthe abilitytoselectivelyinvertA16orA19 dependingonwhetheraninstructionor datafetchisoccurring.TheMMIDR (MMUInstruction/DataRegister) controlsthisbehavior.Thisishowthe Rabbitimplementsseparateinstruction anddataspace(I&Dspace). SeparateI&Dspaceonlyappliestothe firsttwosegments.TheStackSegment andXPCsegmentareunaffectedby theselectiveinversionofA16andA19 Figure2.15:TheMMUand MIUformacompletememory managementstructure.
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
TheBasics dictatedbytheMMIDR.ThisarrangementallowstheXPCSegment(andStackSegment)to mapintothephysicalmemorywithoutcomplicationsfromtheMIU. TheMIUisalsoresponsibleforgeneratingthememoryinterfacesignals(/CS0,/CS1,/CS2, /OE0,/OE1,/WE0,/WE1).ThisisdonethroughthefourMemoryBankControlRegisters (MBxCR). Theonemegabytememoryspaceisdividedintofour256Kquadrants.Eachquadranthasa correspondingMBxCR.Theseregistersallowtheapplicationprogramtospecifytheassertion ofthememoryinterfacesignalsasafunctionofthephysicaladdressbeingaccessed.This allowstheRabbittoaccommodateavarietyofmemorydevices. Forexample,considerasystemwitha512KSRAMchipanda512Kflashchip.Alogical approachtomemoryinterfacewouldbetoconfigureMB0CRandMB1CRtoassertthe/CS0. Connect/CS0totheflashchip.ConfigureMB2CRandMB3CRtoassert/CS1foraccesses totop512Kofthememoryspace.Thenconnect/CS1toenabletheSRAM.Boththeflash andSRAMcouldsharetheWriteEnablesand WE0 OutputEnables. OE 0 Next,considerasystemwiththesame512K Memory Device 1 SRAMchip,butonlya256Kflashchip.In CS0 thissystem,weprobablystillwanttoplace theflashchipdownat0x0000(asthat’swhere Memory Device 2 theRabbitwillwanttobootfrom).Wemay stillwanttoplacetheSRAMatthetopofthe 20-bitaddressspace.WeconfigureMB0CRto assertthe/CS0,thenconnect/CS0totheflash chip.Next,configureMB2CRandMB3CR Memory Device 3 toassert/CS1foraccessestothetop512K CS1 ofthememoryspaceandconnect/CS1tothe Memory Device 4 SRAM.Nowwemustdecidewhattodoif theapplicationattemptstoaccessthesecond quadrant(256K–512K).Wemightmapthat quadrantbackontotheflashmemory,orwe mightmapittotheSRAM.Anotherapproach wouldbetoconfigureMB1CRtoassert/CS2, Memory Device 5 thenuse/CS2togenerateanexternalinterrupt. CS2 TheISRcoulddiagnosetheattempttoaccessa Memory Device 6 nonexistentmemorydeviceandsetthesystem WE1 upforagracefulrecovery. OE 1 Nowthatweunderstandhowanapplication programcanmodifytheMBxCRregistersto A0-A19 assigna/CSxtospecificaddressrange,let’s Figure2.16:TheRabbit3000candirectly lookathowwecanusethistoconnectsix connecttosixmemorydevices,eachof 1MBmemorydevicestotheRabbit.Figure whichcanbeuptoonemegabyte. 2.16showssuchanarrangement. 61
Chapter2 Theastuteengineerwill,at thispoint,besquintingand wonderinghowthefour MemoryBankControl Registers(MBxCRs)will generatethreechip-selects acrosssixmegabytesof addressspace.Theanswer is,“notautomagically.” Theapplicationprogram mustdothebookkeeping necessarytokeeptrack ofwhichchipselectsare setupatanygiventime inwhichMBxCR.The applicationprogrammust alsokeepadjustingthe MBxCRconfiguration toswapinthedesired memorydeviceatthedesiredtime.Keepinmind thatthisisnotnormally required,sinceproducts manufacturedbyRabbitSemiconductorhave atmostonemegabyteof memory. TheRabbitMIUoffersan additionalfeaturethatisuseful formanagingmemorysizes greaterthan1MB.Thisis theselectiveinversionofA18 andA19dependingonthe MBxCRs. Figure2.17showshow4MB ofmemorycanbeconnected totheRabbitand,throughthe useofselectiveinversion,256 KBquadrantsineachmemory devicemaybepagedintothe
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
Figure2.17:SelectiveinversionofA18andA19 allowtheRabbittopage256KBquadrantsin 1MBexternalmemorydevicesmanually.
62
TheBasics visiblespace.Again,theapplicationprogramisresponsibleforthepaging,andcarefulhousekeepingwillberequired. InFigure2.17,itisimportanttounderstandthattheselectiveinversionofA18andA19occur aftertheMBxCRregisterhasbeenchosen.InFigure2.15,aswellasinFigure2.17,the addresslinesA18andA19arebroughtfromtheMMUintotheMemoryInterfaceControl block.TheMemoryInterfaceControlblockusesA18andA19todeterminewhichMBxCR isapplicabletothecurrentaddress.Ifselectiveinversionisrequired,thentheinversionis accomplishedontheA18andA19lineinsideoftheMemoryInterfaceControlblockshown inFigure2.15. Figure2.15showsthatA19maybeinvertedaspartoftheseparateI&Dfunctionalitybefore beingdeliveredtotheMemoryInterfaceControlBlock.Forthesakeofunderstandingthe Rabbit’sabilitytoindexlargememoryblocksbypaging,throughtheuseofselectiveinversion ofA18andA19,theseparateI&Dspacefunctionalityshouldbeignoredinitially. Asapracticalmatter,RabbitSemiconductorandZ-Worlddonotofferanycommercial productswithmorethan1MBofmemoryonthemainprocessorbus.DynamicCdoesnot supportthebookkeepingtotrackthemanualpagingofexternalmemorydevicesconfigured asshowninFigures2.16and2.17. RabbitSemiconductorhasbuiltengineeringprototypessportinglargememoryfootprintssuch asthoseshowninFigures2.16and2.17.Thetechnologyexistsforlargememorysupport.It onlyawaitsanapplication. Figure2.15omitsseveralMIUrelatedregisters.TheMemoryTimingControlRegister (MTCR)tellstheMIUhowtoalignthememoryinterfacesignalsagainstthesystemclock,thus allowingtheRabbitmaximumversatilityininterfacingtoalargearrayofavailablememories. TheBreakpoint/DebugControlRegister(BDCR)allowstheMUItocontrolhowRST28 instructionsarehandled.TheRST28instructioncanactasasoftwareinterrupt(commonly calledatrap)fordebugging.Infinalproductioncode,theRST28instructionscanbeleftinthe applicationcodetoensureoverallsystemtimingisthesamebetween“debugging”and“runtime” execution.TheBDCRcancommandRST28tobehaveasaNOP.DynamicCusesthisfeature. 2.5.14GluelessMemoryBus OurdiscussionoftheRabbitarchitecturestartedwiththeCPUandhasprogressedoutward throughthearrayofonboardperipheralsandMMU.Atlastwecometothemicroprocessor’s largestboundary—thememorybus. Onceagain,Rabbit’sdesignerspulledoutallthestopstocreateaneasy-to-useinterface. Thesystemdesignercanplunkdownjustaboutanyold8-bitmemorydeviceandtheRabbit 3000willtalktoit.TheRabbitgeneratesallthecontrolsignalsnecessarytotalktomemory devices—OutputEnables,ChipSelects,WriteEnablesandofcoursetheAddressandData busesareallprovided.Memoryinterfacingisglueless. Figure2.16showsjusthoweasyitistointerfacememorytotheRabbit3000. TheRabbitSemiconductorwebsitemaintainsaTechnicalNote(TN226)withalistofviable flashdevices:http://www.rabbitsemiconductor.com/support/techNotes_whitePapers.shtml 63
Chapter2
2.6InSummary Whenpickingaprocessorforaproject,manyissuesneedtobeconsidered.Somearetechnical.Someareeconomic.Somearerelatedtoriskmanagement.Inallcases,evaluating processorsorcontrollerswillinvolveapples-to-orangescomparisons. RabbitSemiconductorgrewoutofZ-World’svastexperiencebuildingembeddedcontrollers forawidearrayofapplications.Rabbit‘sdesignerscreatedaprocessorcapableofsimplifying manyofthetaskscommonlyfoundinembeddedsystemdesign.RabbitandZ-World,through thedevelopmentofcoremodulesandpackagedcontrollers,havehardwaresolutionsfromthe chipleveltopackagedcontrollers. Z-WorldsupportssoftwaredevelopmentonRabbit-basedsystemswithhighlyintegratedtools dubbedDynamicC™.ThirdpartytoolssuchasthoseofferedbySoftoolsarealsoavailable. Next,wewilllookathowtogetstartedwithaRabbit-basedcoremoduleusingDynamicCas thedevelopmentenvironment.
64
3
CHAPTER
StartingOut InChapter1,wecoveredtheconceptofusingmicroprocessorsversuscores.Wewillstart thischapterwithabriefdescriptionoftheRCM3200RabbitcoreandthengetintotheRabbit developmentenvironment.WewillcoverdevelopmentanddebuggingaspectsofDynamic CandwilljumpintoourfirstRabbitprogram.Wewillhighlightsomeofthedifferences betweenDynamicCandANSIC.Knowingthesedifferenceswillmakeourlifeeasieraswe learntoprogramwithDynamicC.
3.1IntroductiontotheRCM3200RabbitCore Aprocessordoesnotmeanalotbyitself.Thedesignerhastoselecttherightsupportcomponents,suchasmemory,externalperipherals,interfacecomponents,etc.Thedesignerhas tointerfacethesecomponentstotheCPU,anddesignthetimingandthegluelogictomake themallworktogether.Therearedesignrisksinvolvedinundertakingsuchatask,notto mentionthetimeindesigning,prototyping,andtestingsuchasystem. Usingacoremodulesolvesmostoftheseissues.Buyingalow-costmodulethatintegrates alltheseperipheralsmeanssomeonehasalreadytakenthedesignthroughtheprototyping, debugging,andassemblyphases.Inaddition,coremanufacturersgenerallytakeEMIissues intoaccount.Thisallowstheembeddedsystembuildertofocusoninterfaceissuesand applicationcode. AsdiscussedinChapter1,thereareseveraladvantagestousingcores.Thegreatestadvantage isreducedtime-to-market.Insteadofputtingtogetherthefundamentalbuildingblockssuch asCPU,RAMandROM,thedesignercanquicklystartcodingandfocusinsteadontheapplicationtheyaretryingtodevelop. Toillustratehowtouseacoremodule,wewillsetupanRCM3200coremoduleandstep throughthecodedevelopmentprocess. TheRCM3200coreoffersthefollowingfeatures:
TheRabbit3000CPUrunningat44.2MHz 512KofFlashmemoryforcode 512KoffastSRAMforprogramexecution 256KofbatterybackedSRAMfordatastorage Builtinreal-timeclock 65
Chapter3
10/100Base-TEthernet Sixserialports 52bitsofdigitalI/O Operationfrom3.15Vto3.45V
Duringdevelopment,coresmountonprototypingboardssuppliedbyRabbitSemiconductor.AnRCM3200prototypingboardcontainsconnectorsforpowerandI/O,levelshiftersfor serialI/O,aresetswitch,andapairofswitchesandLEDsconnectedtoI/Opins.Ausefulfeatureoftheprototypingboardistheprototypingareathathasboththru-holesandSMTpads. Thisiswheredesignerscanpopulatetheirowndevicesandinterfacethemwiththecore. TheRabbitSemiconductorprototypingboardsaredesignedtoallowasystemdeveloperto buildpreliminarydesignsandwritecodeontheprototypingboard.Thisallowsinitialsystem developmenttooccureveniftheapplication’stargethardwareisnotavailable. Oncefinalhardwareiscomplete,thecoremodulecanbemovedfromtheprototypingboard tothetargethardwareandthesystemsoftwarecanthenbefinalizedandtested.
3.2IntroductiontotheDynamicCDevelopmentEnvironment TheDynamicCdevelopmentsystemincludesaneditor,compiler,downloader,andin-circuit debugger.ThedevelopmenttoolsallowuserstowriteandcompiletheircodeonaWindows platform,anddownloadtheexecutablecodetothecore.DynamicCisapowerfulplatform fordevelopmentanddebugging: Development: DynamicCincludesanintegrateddevelopmentenvironment(IDE).Usersdonot needtobuyoruseseparateeditors,compilers,assemblersorlinkers. DynamicChasanextensivelibraryofdrivers.Formostapplications,designersdo notneedtowritelow-levelperipheralinterfacecode.Theysimplyneedtomakethe rightAPIcalls.Designerscanfocusondevelopingthehigher-levelapplicationrather thanspendtheirtimewritinglow-leveldrivers. DynamicCusesaserialporttodownloadcodeintothetargetcore.Thereisnoneed touseanexpensiveCPUorROMemulator.Usersofmostcoresloadandruncode fromflash. DynamicCisnotANSIC.Wewillhighlightsomeofthedifferencesaswemovealong. Debugging: DynamicChasahostofdebuggingfeatures.InatraditionaldevelopmentenvironmentaCPU emulatorperformsthesefunctions.However,DynamicCperformsthesefunctions,saving thedeveloperhundredsorthousandsofdollarsinemulatorcosts.DynamicC’sdebugging featuresinclude:
Breakpoints—Setbreakpointsthatcanstopprogramflowwhererequired,sothatthe programmercanexamineandchangethestateofvariablesandregistersorfigureout howtheprogramgottoacertainpartofthecode
66
StartingOut
Singlestepping—Stepintooroverfunctionsatasourceormachinecodelevel. Singlesteppingwilllettheprogrammerexamineprogramflow,orvaluesofCPU registers,programvariables,ormemorylocations. Codedisassembly—Thedisassemblywindowdisplaysaddresses,opcodes,mnemonics,andmachinecycletimes.ThiscanhelptheprogrammerexaminehowCcodegot convertedintoassemblylanguage,aswellascalculatehowmanymachinecyclesit maytaketoexecuteasectionofcode. Switchbetweendebuggingatmachinecodelevelandsourcecodelevelbysimply openingorclosingthedisassemblywindow. Watchexpressions—Thiswindowdisplaysvaluesofselectedvariablesorevencomplexexpressions,includingfunctioncalls.Theprogrammercanthereforeexamineor evaluatevaluesofselectedvariablesduringprogramexecution.Watchexpressions canbeupdatedwithorwithoutstoppingprogramexecutionandcanbeusedtotriggertheoperationofhardwaredevicesinthetarget.Usethemouseto“hoverover”a variablenametoexamineitsvalue. Registerwindow—Allprocessorregistersandflagsaredisplayed.Thecontentsof registersmaybemodifiedasneeded. Stackwindow—Showsthecontentsofthetopofthestack. Hexmemorydump—Displaysthecontentsofmemoryatanyaddress. STDIOwindow—printfoutputstothiswindow,andkeyboardinputonthehostPC canbedetectedfordebuggingpurposes.
3.3BriefIntroductiontoDynamicCLibraries DynamicCprovidesextensivelibrariesofdrivers.Low-leveldrivershavealreadybeenwrittenandprovidedforcommondevices.Forinstance,DynamicCdriversforI2C,SPI,various LCDdisplays,keypads,filesystemsonflashmemorydevices,andevenGPSinterfacesare alreadyprovided.AcompleteTCPstackisalsoincludedforcoresthatsupportnetworking. TherearesomedifferencesbetweenDynamicCandANSIC.ThiswillbeespeciallyimportanttoprogrammersportingcodetoaRabbitenvironment.Aswecovervariousaspectsof codedevelopment,wewillhighlightdifferencesbetweenDynamicCandANSIC. SourcecodeforDynamicClibrariesissuppliedwiththeDynamicCdistribution.Although theDynamicClibraryfilesendwitha“.LIB”extension,theseareactuallysourcefilesthat canbeopenedwithatexteditor. Forexample,letusexaminetheLCDlibrary.IfDynamicCisinstalledintoitsdefaultdirectories,wefindanLCDlibraryfileatDynamicC\Lib\Displays\LCD122KEY7.LIB: Thelibraryfiledefinesvariousvariablesandfunctions.BecauseitisanLCDlibrary,wefind functionsthatinitializeadisplayandallowtheprogrammertowritetoanLCD. Lookingatthefunctiondescriptions,theprogrammercanquicklyunderstandhowRabbit’s engineersimplementedeachfunction.Theembeddedsystemsdesignercantailorthelibrary functionstosuitparticularapplicationsandsavetheminseparatelibraries. 67
Chapter3 QuickSummary: DynamicCisnotANSIC DynamicClibraryfilesendwitha“.LIB”extension,andaresourcefilesthat canbeopenedwithatexteditor
3.4MemorySpacesinDynamicC InChapter2,welookedatvariousmemoryspacesusedbytheRabbit.Hereweseehow DynamicCmanipulatestheMMUtoprovideanoptimalmemoryusagefortheapplication. TheRabbithasanexternal8-bitdatabus.Thisallowstheprocessortointerfacetoinexpensive8-bitmemorydevices.Thetrade-offwithasmalldatabusisthemultiplebusaccesses requiredtoreadlargeamountsofdata.Tominimizethetimerequiredtofetchoperands containingaddresseswhilestillprovidingausefulamountofaddressspace,theRabbitusesa 16-bitaddressforallinstructionoperands. A16-bitaddressrequirestworeadcyclesoverthedatabustoacquireanaddressasanoperand.Thisimpliesanaddressspacelimitedto216(65536)bytes.A16-bitaddressspace,while usable,issomewhatlimiting. Toachieveausablememoryspacelargerthan216bytestheRabbit’sdesignersgavethemicroprocessoramemorymanagementunit(MMU).Thisdevicemapsa16-bitlogicaladdresstoa 20-bitphysicaladdress. TheRabbitdesignerscouldhavesimplymadetheRabbit’sinstructionsaccept20-bitaddress operands.Thiswouldrequire3bytestocontaintheoperandsandwouldthereforerequire threefetchesoverthe8-bitdatabustopullinthecomplete20-bitaddress.Thisisa50% penaltyoverthe2fetchesrequiredtogathera16-bitaddress. Manyprogramsfitquitehandilyina16-bitaddressspace.Theperformancepenaltyincurred bymakingalltheinstructionsoperateona20-bitaddressisnotdesirable.TheMMUoffersa compromisebetweenalargeaddressspaceandefficientbusutilization.Goodspeedandcode densityareachievedbyminimizingtheinstructionlength.TheMMUmakesavailablealarge addressspacetoapplicationsrequiringmorethana16-bitaddressspace. TheRabbit3000™Designer’sHandbookcoverstheMMUinexactingdetail.However,most engineersusingtheRabbitonlyneedunderstandtherudimentarydetailsofhowDynamicC usesthefeature-richRabbitMMU. 3.4.1Rabbit’sMemorySegments TheRabbit3000’sMMUmapsfoursegmentsfromthe16-bitlogicaladdressspaceintothe 20-bitphysicaladdressspaceaccessibleonthechip’saddresspins.Thesesegmentsareshown inFigure3.1. InChapter2,weexaminedtheMMUregistersinsomedetail.FurtherinformationisalsoavailableintheRabbit3000documentationavailableontheCDincludedwiththisbook.Wewill 68
StartingOut 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
Figure3.1:TheRabbit3000MMUsegments.
notrehashthedetailsofhowtheMMUperformsthemappingofa16-bitlogicaladdresstoa 20-bitphysicaladdress.OurobjectivehereistounderstandhowDynamicCusestheRabbit’s MMUtocreateaframeworkforembeddedapplicationswritteninCandassemblylanguage. DynamicCusestheavailablesegmentsdifferentlydependingonwhetherseparateinstruction anddataspaceisenabled.FirstwewillconsiderthecasewithoutseparateI&Dspaceenabled. 3.4.2DynamicC’sMemoryUsageWithoutSeparateI&DSpace DynamicC’ssupportofseparateI&Dspaceallowsmuchbettermemoryutilizationthanthe oldermodelwithoutseparateI&Dspace.Thissectionisincludedforthebenefitofengineerswhomayhavetomaintaincodewrittenfortheoldermemorymodel.Newapplications shouldbedevelopedusingseparateI&Dspace.Thenewermemorymodelalmostdoubles theamountofrootmemoryavailabletoanapplication. DynamicCuseseachofthefoursegmentsforspecificpurposes.TheRootSegmentandData Segmentholdthemostfrequentlyaccessedprogramcodeanddata.TheStackSegmentis wherethesystemstackresides.TheExtendedMemorySegmentisusedtoaccesscodeor datathatisplacedoutsideofthememorymappedintothelowerthreesegments. 69
Chapter3 AbitofRabbitterminologyworthrememberingistheterm“rootmemory.”Rootmemory containsthememorypointedtobytheRootsegment,theDataSegmentandtheStackSegment(perRabbit3000MicroprocessorDesigner’sHandbook).ThiscanbeseeninFigure3.1. Anotherbitofnomenclaturetokeepinmindistheword“segment.”Whenweusetheword “segment”wearereferringtothelogicaladdressspacethattheMMUmapstophysical memory.ThisisafunctionoftheRabbit3000chip.Ofcourse,DynamicCsetsuptheMMU registers,buta“segment”isasliceoflogicaladdressspaceandcorrespondinglyareference tothephysicalmemorymapped. Segmentscanberemappedduringruntime.TheXPCsegmentgetsremappedfrequentlyto accessextendedmemory,butmostapplicationsdonotremaptheothersegmentswhilerunning. Thesemanticsmayseemalittlepicky,butthisattentiontodetailwillhelptoenforcethe logicalabstractionsbetweenDynamicC’susageoftheRabbit’shardwareresourcesandthe resourcesthemselves. Anexampleisthephrase“StackSegment”andtheword“stack.”The“StackSegment”is justamappingofasliceofphysicalmemoryintologicaladdressspace.Thereisnointrinsic hardwarerequirementthatthesystem“stack”belocatedinthissegment.The“StackSegment”wassonamedbecauseDynamicChappenstousethisthirdMMUsegmenttoholdthe systemstack.The“StackSegment”isapieceofmemorymappedbytheMMU’sthirdsegment.The“stack”isadatastructurethatcouldbeplacedinanysegment. TheRootSegmentissometimesreferredtoastheBaseSegment.TheRootSegmentmaps toBIOScode,applicationcodeandDynamicCconstants.InmostdesignstheRootSegment ismappedtoflashmemory.TheBIOSisplacedataddress0x00000andgrowsupward.The applicationcodeisplacedabovetheBIOSandgrowstothetopofthesegment.Constantsare intermixedwiththeapplicationcode. DynamicCreferstoexecutablecodeplacedintheRootSegmentas“RootCode.”The DynamicCconstantsarecalled“RootConstants”andarealsostoredintheRootSegment. TheDataSegmentisusedbyDynamicCprimarilytoholdCvariables.TheRabbit3000microprocessorcanactuallyexecutecodefromanysegment;however,DynamicCusestheData Segmentprimarilyfordata.ApplicationdataplacedintheDataSegmentiscalled“RootData.” SomeversionsofDynamicCdosqueezeafewextragoodiesintotheDataSegmentthatone mightnotnormallyassociatewithbeingprogramdata.Theseitemsarenonethelesscritically importanttotheproperfunctioningofanembeddedsystem.AquickglanceatFigure3.2 willrevealthatatthetop1024bytesofthedatasegmentareallocatedtoholdwatch-codefor debuggingandinterruptvectors.FutureversionsofDynamicCmayusemoreorlessspace andmayplacedifferentitemsinthisspace. DynamicCbeginsplacingCvariables(RootData)justbelowthewatch-codeandgrows themdownwardtowardtheRootSegment.Allstaticvariables,eventhoselocaltofunctions placedintheextendedmemory,arelocatedinDataSegment.Thisisimportanttokeepin mindastheDataSegmentcanfillupquickly.
70
StartingOut 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
Figure3.2:DynamicC’susageoftheRabbit3000segments.
DynamicC’sdefaultsettingsallocateapproximately28KbytesfortheDataSegmentand 24KbytesfortheRootSegmentspaces.ThemacroDATAORG,foundinRabbitBios.c, canbemodified,instepsof0x1000,tochangetheboundarybetweenthesetwospaces.Each increaseof0x1000willgain0x1000bytesforcodewithanattendantlossof0x1000fordata. Eachincrementaldecreaseof0x1000willhavetheoppositeeffect. TheStackSegment,asthenameimplies,holdsthesystemstack.Thestackisusedby DynamicCtokeeptrackofreturnaddressesaswellastopasssomevariablesbetweenfunctions.Variablesoftype“auto”alsoresideonthestack.Thesystemstackstartsatthetopof thestacksegmentandgrowsdownward. TheXPCSegment,sometimescalledtheExtendedMemorySegment,allowsaccesstocode anddatathatisstoredinthephysicalmemorydevicesoutsideoftheareaspointedtobythe threesegmentsin“RootMemory.”RootMemoryiscomprisedoftheRootSegment,theData SegmentandtheStackSegment. Thesystem’s“extendedmemory”isallofthememorynotmappedintotheRootMemoryas showninFigure3.1.ExtendedMemoryincludesnotonlythephysicalmemorymappedinto theXPCsegment,butalltheotherphysicalmemoryshowninFigure3.1ingray. Whenwerefertoextendedmemory,wearenotreferringjusttomemorymappedintothe XPCSegment.TheXPCsegmentisthetool(MMUsegment)thatDynamicCusestoaccess allofthesystem’sextendedmemory.WewilluseXMEMinterchangeablywith“extended memory”tomeanallphysicalmemorynotmappedintoRootMemory. 71
Chapter3 Generally,functionscanbeplacedinXMEMorinrootcodespaceinterchangeably.Theonly reasonafunctionmustbeplacedinrootmemoryisifthefunctionisaninterruptserviceroutine(ISR)orifthefunctionmodifiestheMMUmappingoftheXPCregister. Ifanapplicationgrowslarge,movingfunctionstoXMEMisagoodchoiceforincreasing theavailablerootcodespace.RabbitSemiconductorhasanexcellenttechnicalnoteTN219, “RootMemoryUsageReductionTips.”Forengineerswithlargeapplications,thistechnical noteisamustread. AneasymethodtogainmorespaceforRootCodeissimplytoenableseparateI&Dspace, butforwhenthatisnotanoption,movingfunctioncodetoXMEMisthebestalternative. 3.4.3PlacingFunctionsinXMEM AssemblyorCfunctionsmaybeplacedinrootmemoryorextendedmemory.Accessto variablesinCstatementsisnotaffectedbytheplacementofthefunction,sinceallvariables areintheDataSegmentofrootmemory.DynamicCwillautomaticallyplaceCfunctionsin extendedmemoryasrootmemoryfills. Functionsplacedinextendedmemorywillincuraslight12machinecycleexecutionpenalty oncallandreturn.ThisisbecausetheassemblyinstructionsLCALLandLRETtakelongerto executethantheassemblyinstructionsCALLandRET.Ifexecutionspeedisimportant,considerleavingfrequentlycalledfunctionsintherootsegment. Short,frequentlyusedfunctionsmaybedeclaredwiththerootkeywordtoforceDynamic CtoloadtheminRootMemory.Functionsthathaveembeddedassemblythatmodifiesthe MMU’sspecialfunctionregistercalledXPCmustalsobelocatedinRootMemory.Itis alwaysagoodideatousetherootkeywordtoexplicitlytellDynamicCtolocatefunctions inrootmemoryifthefunctionsmustbeplacedinrootmemory. Interruptserviceroutines(ISRs)mustalwaysbelocatedinrootmemory.ISRswillbediscussedindetailinChapter7. DynamicCprovidesthekeywordxmemtoforceafunctionintoextendedmemory.Ifthe applicationprogramisstructuredsuchthatitreallymatterswherefunctionsarelocated,the keywordsrootandxmemshouldbeusedtotellthecompilerexplicitlywheretolocatethe functions.IfDynamicCislefttoitsowndevices,thereisnoguaranteethatdifferentversions ofDynamicCwilllocatefunctionsinthesamememorysegments.Thiscansometimesbean issueforcodemaintenance. Forexample,sayanapplicationisreleasedwithoneversionofDynamicC,andayearlater theapplicationmustbemodified.Ifthexmemandrootkeywordsarecontainedintheapplicationcode,itdoesn’tmatterwhatversionofDynamicCthesecondengineerusestomodify theapplication.Thecompilerwillplacethefunctionsistheintendedmemory—XMEMor RootMemory.
72
StartingOut 3.4.4SeparateInstructionandDataMemory TheRabbit3000microprocessorsupportsaseparatememoryspaceforinstructionsanddata. ByenablingseparateI&Dspaces,DynamicCisessentiallygivendoubletheamountofroot memoryforbothcodeanddata.Thisisapowerfulfeature,andonethatseparatestheRabbit 3000processorsandDynamicCfrommanyotherprocessor/toolcombinationsonthemarket. TheapplicationdeveloperhascontroloverwhetherDynamicCusesseparateinstructionand dataspace(I&Dspace).IntheDynamicCintegrateddevelopmentenvironment(IDE)the engineerneedonlynavigatetheOPTIONS⇒PROJECTOPTIONS⇒COMPILERmenu andusethecheckboxlabeled“enableseparateinstructionanddataspaces”. WhenSeparateI&Dspaceisenabled,someofthetermsZ-WorldusestodescribeMMU segmentsandtheircontentsareslightlyalteredfromtheoldermemorymodelwithoutseparate I&Dspaces.Likewise,someofthemacrodefinitionsinRabbitBios.chavealteredmeanings. Forexample,theDATAORGmacrointheoldermemorymodeltellsthecompilerhowmuch memorytoallocatetotheDataSegment(usedforRootData)andtheRootSegment(usedfor RootCode)andRootConstants.InaseparateI&Dspacemodel,theDATAORGmacrohas noeffectontheamountofmemoryallocatedtocode(instructions),butinstead,tellsthecompilerhowtosplitthedataspacebetweenRootDataandRootConstants.WithseparateI&D spaceenabled,eachincreaseof0x1000willdecreaseRootDataandincreaseRootConstant spacesby0x1000each. ThereasonforthedifferenceinfunctionisanartifactofhowDynamicCusesthesegmentsand howtheMMUmapsmemorywhenseparateI&Dspaceisenabled.Formostsoftwareengineers,itisenoughtoknowthatenablingseparateI&Dspacewillusuallymap44KofSRAM andflashforuseasRootDataandRootConstantsand52KofflashforuseasRootCode. Themoreinquisitivedevelopermaywishtodelvedeeperintothememorymappingscheme. Toaccommodatethis,wewillbrieflycoverhowseparateI&Dspaceworks,butthenittygrittydetailsaretobefoundinChapter2andtheaccompanyingRabbitSemiconductorCD. WhenseparateI&Dspaceisenabled,thelowertwoMMUsegmentsaremappedtodifferent addressspacesinthephysicalmemorydependingonwhetherthefetchisforaninstruction ordata.DynamicCtreatsthelowerMMUtwosegments(theRootSegmentandtheData Segment)asonecombinedlargersegmentforRootCodeduringinstructionfetches.During datafetches,DynamicCusesthelowestMMUsegment(theRootSegment)toaccessRoot Constants.DuringdatafetchesthesecondMMUsegment(theDataSegment)isusedtoaccessRootData. WhenseparateI&Dspaceisenabled,thelowertwoMMUsegmentsarebothmappedtoflash forinstructionfetches,whilefordatafetchesthelowerMMUsegmentismappedtoflash(to storeRootConstants)andthesecondMMUsegmentismappedtoSRAM(tostoreRootData). Thisisanareawhereitiseasytobecomelostormisledbynomenclature.Whenseparate I&Dspaceisenabled,thetermsRootCodeandRootDatameanmoreorlessthesamething tothecompilerinthat“code”and“data”arebeingmanipulated.Buttheunderlyingsegment mappingisverydifferentthanwhenseparateI&Dspaceisnotenabled. 73
Chapter3 WhenseparateI&Dspaceisnotenabled,theRootCodeisonlytobefoundinthephysical memorymappedintothelowestMMUsegment(theRootSegment). WhenseparateI&Dspaceisenabled,theRootCodeisfoundinboththelowerMMUsegments(named“RootSegment”and“DataSegment”).DynamicCknowsthattheseparate I&DfeatureontheRabbit3000allowsbothofthelowerMMUsegmentstomaptoalternate placesinphysicalmemorydependingonthetypeofCPUfetch.DynamicCsetsupthelower MMUsegmentssothattheyBOTHmaptoflashwhenaninstructionisbeingfetched.ThereforeRootCodecanbestoredinphysicalmemorysuchthatDynamicCcanusethetwolower MMUsegmentstoaccessRootCode. ThismayseemcontrarytothesegmentnameofthesecondMMUsegment,theDataSegment.ThereadermustbearinmindthattheMMUsegmentswerenamedbasedontheolder memorymodelwithoutseparateI&Dspace.Inthatmodel,theCPUsegmentnameswere descriptiveofhowDynamicCusedtheMMUsegments.WhentheRabbit3000cameoutand includedtheoptionforseparateI&Dspace,theMMUsegmentswerestillgiventheirlegacy names.WhenseparateI&Dspacewasenabled,DynamicCusedtheMMUsegmentsdifferently,butthesegmentnamesonthemicroprocessorremainedthesame. ThisbringsustohowDynamicCusesthelowertwoMMUsegmentswhenseparateI&D spaceisenabledandadatafetch(orwrite)occurs.Wearealreadyfamiliarwiththeideaof “RootData,”andthisismappedintophysicalmemory(SRAM)throughthesecondMMU segment—theDataSegment. ConstantsareanothertypeofdatawithwhichDynamicCmustcontend.Intheoldermemory modelwithoutseparateI&Dspaceenabled,constants(RootConstants)wereintermixed withthecodeandaccessedbyDynamicCthroughthelowestMMUsegment(theRootSegment).InthenewmemorymodelwithseparateI&Dspaceenabled,DynamicCstillusesthe lowerMMUsegment(therootsegment)toaccessRootConstants.ButwithseparateI&D spaceenabled,whendataaccessesoccur,thelowestMMUsegment(rootsegment)ismapped toaspacewherecodeisnotstored.ThismeansthereismorespacetostoreRootConstantsas theyarenotsharingmemorywithRootCode. RootConstantsmustbestoredinflash.ThisimpliesthatthelowestMMUsegmentismapped intophysicalflashmemoryforbothinstructionanddataaccesses.RootCoderesidesinflash, asdoRootConstants. Giventhisoverview,wecanconsidertheeffectofDATAORGagain.DATAORGisusedto specifythesizeofthefirsttwoMMUsegments.SinceDynamicCmapsthefirsttwoMMU segmentstoRootCodeforinstructionaccesses,andtreatsthefirsttwoMMUsegmentsas onebiglogicaladdressspaceforRootCode,changingDATAORGhasnoeffectonthespace availableforRootCode. NowconsiderthecasewhenseparateI&Dspaceisenabledanddataisbeingaccessed.The lowestMMUsegment(theRootSegment)ismappedintoflashandisusedtoaccessRoot Constants.ThesecondMMUsegment(theDataSegment)ismappedintoSRAMandisused toaccessRootData. 74
StartingOut ChangingDATAORGcanincreaseordecreasethesizeofthefirsttwosegments.Fordata accesses,thismeansthesizeofflashmappedtotheMMU’sfirstsegmentiseithermade largerorsmallerwhilethesecondsegmentisoppositelyaffected.Thismeanstherewillbe moreorlessflashmemorymapped(throughthefirstMMUsegment)forDynamicCtouse forRootConstantswithacorrespondingdecreaseorincreaseinSRAMmapped(throughthe secondMMUsegment)forDynamicCtouseasRootData. WhenseparateI&Dspacesareenabled,thestacksegmentandextendedmemorysegment areunaffected.Thismeansthatthesamesystemstackismappedregardlessofwhether instructionsordataarebeingfetched.Likewise,extendedmemorycanstillbemapped anywhereinphysicalmemorytoaccommodatestoring/retrievingeitherexecutablecodeor applicationdata. FormostengineersitisenoughjusttoknowthatusingseparateI&Dspacegivesthe developerthemostRootmemoryfortheapplication.Intherarecircumstanceinwhichthe memorymodelneedstobetweaked,theDATAORGmacroiseasilyusedtoadjusttheratioof RootDatatoRootConstantspaceavailable.Forthetrulyhardcore,theRabbitdocumentation hasallthedetails. 3.4.5PuttingItAllTogether Wehavespentaconsiderableamountoftimegoingoversegments.InSection3.7.3,wewill lookatcodethatwillreinforcetheseconcepts.
QuickSummary: Logicaladdressesare16-bits PhysicaladdressesexistoutsidetheCPUina20-bitspace TheMMUmapslogicaladdressestophysicaladdressesthroughsegments Dependingonapplicationrequirementssuchasspeedandspace,itmaybe importanttocontrolwherecodeanddataareplaced.DynamicC’sdefaults canbeoverridden,allowingtheprogrammertodecidewheretoplacethese codeelementsinmemory
75
Chapter3
3.5HowCodeisCompiledandRun Let’slookatthetraditionalbuildprocessandcontrastitwithhowDynamicCbuildscode: 3.5.1HowCodeisBuiltinTraditionalDevelopmentEnvironments Theprogrammereditsthecodeinaneditor,oftenpartoftheIDE;theeditorsavesthe sourcefileinatextformat. Theprogrammercompilesthecode,fromwithintheIDE,fromcommandline parameters,orbyusingamakeutility.Theprogrammercaneitherdoa“Compile All,”whichwillcompileallmodules;orthemakeutilityorIDEcanonlycompilethe modulesthatwerechangedsincethelasttimethecodewasbuilt.ThecompilergeneratesobjectcodeandalistfilethatshowshoweachlineofCcodegotcompiledinto oneormorelinesofassemblycode.Unlessspecified,eachobjectmodulehasrelative memoryreferencesandisrelocatablewithinthememoryspace,meaningitcanreside anywhereinmemory.Similarly,eachassemblymodulegetsassembledandgenerates itsownrelocatableobjectmoduleandlistfile. Iftherearenocompilationorassemblyerrors,thelinkerexecutesnext,puttingthe variousobjectmodulestogetherintoasinglebinaryfile.Thelinkerconvertsrelativeaddressesintoabsoluteaddresses,andcreatesasinglebinaryfileoftheentire program.Almostalllinkersnowadaysalsohaveabuilt-inlocatorthatlocatescode intospecificmemorylocations.Thelinkergeneratesamapfilethatshowsanumber ofusefulthings,includingwhereeachobjectmoduleresidesinmemory,howmuch spacedoesthewholeprogramtake,etc.Iflibrarymodulesareutilized,thelinker simplylinksinpre-compiledobjectcodefromthelibraries. Theprogrammercandownloadthebinaryfileintothetargetsystemusingamonitor utility,abootstraploader,usinganEPROMemulator,orbysimplyburningtheimage intoanEPROMandplugginginthedeviceintotheprototypingboard.IfaCPUemulatorisbeingused,theprogrammercansimplydownloadthecodeintotheemulator.
76
StartingOut Figure3.3illustrateshowcodeisbuiltonmostdevelopmentenvironments: 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
Figure3.3:Thetraditionalbuildprocess.
3.5.2HowCodeisBuiltwithDynamicC TheprogrammereditsthecodeintheDynamicCIDE,andsavesthesourcefileina textformat. TheDynamicCIDEcompilesthecode.Ifneeded,theprogrammercancompilefrom commandlineparameters.Unlikemostotherdevelopmentenvironments,DynamicC preferstocompileeverysourcefileandeverylibraryfileforeachbuild.Thereisan optionthatallowstheusertodefineprecompiledfunctions. Thereisnoseparatelinker.Eachbuildresultsinasinglebinaryfile(withthe“.BIN” extension)andamapfile(withthe“.MAP”extension). TheDynamicCIDEdownloadstheexecutablebinaryfileintothetargetsystem usingtheprogrammingcable. 77
Chapter3 Figure3.4illustrateshowcodeisbuiltandrunwithDynamicC: 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
Figure3.4:HowDynamicCbuildscode.
QuickSummary: DynamicCbuildscodedifferentlyfromthetraditionaledit/compile/link/ downloadcycle Eachtimecodeisbuilt,DynamicCalwayscompileseachlibraryfileand eachsourcefile Eachtimecodeisrun,DynamicCdoesacompletebuild. WithintheDynamicCIDE,executableimagescanbedownloadedtoatarget systemthroughasimpleprogrammingcable Reference: Figure 4.2 Title: Build Process with Dynamic C Drawn By: Kamal Hyder Drawn On: 4/24/03
78
StartingOut
3.6SettingUpaPCasanRCM3200DevelopmentSystem BeforewestartusingDynamicCtowritecode,weneedtosetupanRCM3200coremodule andprototypingboard.Thissimpleprocessonlytakesafewminutes. SettingupanRCM3200developmentsystemrequiresfulfillingthefollowingsteps: 1. 2. 3. 4. 5.
UsingtheCD-ROMfoundinthedevelopmentkit,installDynamicConyoursystem. ChooseaCOM(serial)portonyourPCtoconnecttotheRCM3200. AttachtheRCM3200totheprototypingboard. ConnecttheserialprogrammingcablebetweenthePCandthecoremodule. Providepowertotheprototypingboard.
Nowthatthehardwareissetup,weneedtoconfigureDynamicC.SomeRabbitcoremodules areabletoruncodefromfastSRAMinsteadofflash.Thisfeaturecanbeenabledfromthe DynamicC“Options⇒ProjectOptions⇒Compiler”menu.TheRCM3200willrunprogramsfromfastSRAMinsteadofflash. Foroursimpleexamples,itreallydoesn’tmatterwhetherweconfigureDynamicCtogeneratecodethatwillrunfromfastSRAMorfromflash.However,forthesakeofconsistency,we alwaysconfigureDynamicCtoenablecodetoberunfromfastSRAMfortheexamplesin thistextthatusetheRCM3200.
3.7TimetoStartWritingCode! NowthattheRCM3200systemisreadyforsoftwaredevelopment,itistimetorollupthe sleevesandstartwritingcode.Thefirstprogramisverysimple.Theintentofthisexerciseis tomakesurethecomputer(thehostPC)isabletotalktotheRCM3200.Onceweareable tosuccessfullycompileandrunaprogram,wewillexploresomeofDynamicC’sdebugging features,aswellassomedifferencesbetweenDynamicCandANSIC. 3.7.1Project:Everyone’sFirstRabbitProgram Ithasbeencustomaryforcomputerprogrammerstostartfamiliarizingthemselveswitha newlanguageoradevelopmentenvironmentbywritingaprogramthatsimplyprintsastring (“HelloWorld”)onthescreen.Wedojustthat—here’stheprogramlisting: Program3.1:helloWorld.c main() { printf(“HelloWorld”); }
//outputastring
79
Chapter3 Here’showtocompileandruntheRabbitprogram: 1. LaunchDynamicCthroughtheWindowsStartMenu—ortheDynamicCDesktop Icon. 2. Click“File”and“Open”toloadthesourcefile“HELLOWORLD.C.”Thisprogram isfoundontheCD-ROMaccompanyingthisbook. 3. Pressthe“F9”functionkeytorunthecode. Aftercompilingthecode,theIDEloadsitintotheRabbitCore,opensaserialwindowonthe screen,titledthe“STDIOwindow,”andrunstheprogram.Thetext“HelloWorld”appears intheSTDIOwindow.Whentheprogramterminates,theIDEshowsadialogboxthatreads “ProgramTerminated.ExitCode0.” Ifthisdoesn’twork,thefollowingtroubleshootingtipsmaybehelpful:
Thetargetshouldbeready,indicatedbythemessage"BIOSsuccessfullycompiled..." Ifthismessagedidnotappearoracommunicationerroroccurred,recompilethe BIOSbytypingorselectReset Target/Compile BIOSfromthe Compilemenu. Ifthemessage"NoRabbitProcessorDetected"appears,verifythetargetsystemhas powerandtheprogrammingcableisconnectedbetweenthePCandthetarget. Theprogrammingcablemustbeconnectedtothecontroller.Thecoloredwireonthe programmingcableisclosesttopin1ontheprogrammingheaderonthecontroller. MakesureyouusetheconnectorlabeledasPROGandnottheconnectorlabeled DIAG.TheotherendoftheprogrammingcablemustbeconnectedtothePCserial port.TheCOMportspecifiedintheDynamicCOptionsmenumustbethesameas theonetowhichtheprogrammingcableisconnected. Toverifythecorrectserialportisconnectedtothetarget,selectCompile,then Compile BIOS,orpress.Ifthe“BIOSsuccessfullycompiled...”messagedoesnotdisplay,tryadifferentserialportusingtheDynamicCOptionsmenu. Don’tchangeanythinginthismenuexcepttheCOMnumber.Thebaudrateshould be115,200bpsandthestopbitsshouldbe1.
AUsefulDynamicCShortcut “F9”causesDynamicCtodothefollowing:
Compilestheprojectsourcecode Assumingthattherewerenocompilationerrors, – Loadsthecodeintoflashonthetargetboard – Beginsexecutionoftheapplicationcodeonthetargetsystem
80
StartingOut Althoughtheprogramterminates,theIDEisstillcontrollingthetarget.Inthismode,called debugorrunmode,theIDEwillnotlettheprogrammereditthecode.FortheIDEtorelease thetargetandallowediting,weneedtoclosethedebugsessionbyclickingon“Edit”and “EditMode.”Alternatively,pressing“F4”willenterEditMode. AuxiliaryFilesCreatedbyDynamicCDuringCompilation helloWorld.BDListhebinarydownloadimageoftheprogram helloWorld.BRKstoresbreakpointinformation.Itcanbeopenedwithatexteditor toseethenumberofbreakpointsandthepositionofeachbreakpointinthesourcefile helloWorld.HDLisasimpleIntelformatHexdownloadfileoftheprogramimage helloWorld.MAPshowswhatthelabels(variablesandcodereferences)resolveto. Inaddition,itshowsthelengthandoriginofeachmoduleandwhichmemoryspace (RootCode,RootData,orXMEMCode)inwhichthemoduleresides helloWorld.ROMisaprogramimageinaproprietaryformat 3.7.2DynamicC’sDebuggingFeatures: DynamicCofferspowerfuldebuggingfeatures.Thisinnovationeliminatestheneedforan expensivehardwareemulator.ThissectioncoversthebasicsofusingDynamicC’sdebugging features. Program3.2(watchDemo.ContheenclosedCD-ROM)isthesimpleprogramthatwillbe usedtoillustrateDynamicC’sdebuggingfeatures. Program3.2:watchDemo.c voiddelay() { intj; for(j=0;jplot“data.log-352-15:44:06-histogram”withboxes gnuplot>setoutput gnuplot>exit
Formoreinformationontheversatilegnuplotprogram,pleaserefertothegnuplotmanpage orthegnuplothomepagehttp://www.gnuplot.org/. SampleADCNoiseQuantificationandVisualization
HereweputtogethertheworkintheprecedingsectionsandcharacterizethefirstDAQchannelontheRCM3400developmentboard. Sinceourvoltagereferencewas2.5VwehadtheoptionofusingADCgainsettingsof1,2,4 and5.Werantheanalysisforallgainsettings. ThehistogramsshowedthatthedatareportedfromtheADCwastightlyclumpedaroundthe meanvalue.Ineachhistogram,onlytwoorthreebinshaddatainthem.AnidealADCina noiselesssystemwouldonlyreportdatainonebin.Inreallifesystems,ahistogramwithonly twoorthreebinsisexcellent.Figure5.16showsarepresentativehistogramfromourexperiments.ThereportedADCcodesareonthehorizontalaxis.Thenumberofoccurrencesis shownontheverticalaxis.
Figure5.16:GAIN=4,0–5voltinputrange,2.5voltstimulus. 154
InterfacingtotheExternalWorld AnexampleofthetextualoutputfromourPerlscriptisshownhere, Assumingan11bitconverterwith0to20Vinputrange Assuminga2.500voltprecisionreference 1Code=0.00977Volts MeasuredMean(codes)=251.9695 ExpectedMean(codes)=256.0000 offset(volts)=0.039Volts RMSnoise(ADCcodes)=0.172 RMSnoise(volts)=0.0017 Noise(pk-to-pk)(ADCcodes)=0.568 Noise(pk-to-pk)(volts)=0.0055
TheresultsofourfourexperimentsaresummarizedinTable5.2. Table5.2:TheRCM3400performedadmirablyoveralloftherangesmeasured. Gainof1 0-20voltrange
Gainof2 0-19voltrange
Gainof4 0-5voltrange
Gainof5 0-4voltrange
Voltspercode
9.77mV
4.88mV
2.44mV
1.95mV
Measuredoffset
39mV
36mV
34mV
32mV
MeasuredRMS noiseincodes
0.172
0.409
0.403
0.675
MeasuredRMS noiseinvolts
1.7mV
2.4mV
1.0mV
1.3mV
Measured pk-pknoise incodes
0.568
1.617
1.329
2.227
Measured pk-pknoise involts
5.5mV
7.9mV
3.2mV
4.3mV
IftheRMSnoiseonachannelisgreaterthan1bit,wecancomputetheeffectiveresolutionof thechannelfrom, RESOLUTION EFFECTIVE = ADCbits − LOG2 ( NoiseRMS incodes ) TheRCM3400hasRMSnoiselevelssolowastorenderthecomputationofeffectiveresolutionmeaningless.ThelogarithmbecomesnegativeforRMSnoiselevelslessthanonecode. Theconclusionwecandrawfromourexperimentsisthattheeffectiveresolutionofthe RCM3400isafull11bitsforgainsof1,2,4and5. 155
Chapter5 Ifthepeak-to-peaknoiseonachannelisgreaterthan1bit,wecancomputethenoisefree resolutionofthechannelfrom: RESOLUTION NOISEFREE = ADCbits − LOG2 ( NoisePK − PK incodes ) Inourexperiment,weseethattheworstcasepeaktopeaknoiseontheRCM3400channel1 occurswithagainof5andis2.227codes.Fromtheequationabove,wecompute, RESOLUTION NOISEFREEGain=5 = 11 − LOG2 (2.227) = 11 −
ln (2.227) lnn (2 )
= 9.84 bits
OuranalysisoftheRCM3400’sDAQchannelshowedadmirableperformance.Forthis example,weonlyexaminedoneoftheeightsingle-ended(orfourdifferential)DAQchannels ontheRCM3400.AcarefulengineerwillcharacterizeallDAQchannelsusedinasystem. TheDCanalysistechniquespresentedherewillallowanengineertogetagoodfeelforhow accuratelytheADCisreportingsensordataunderidealconditions.Thisisthebestperformance thattheengineercanexpectfromthesystemwithoutcalibrationoraveragingdatasamples.
5.6Conclusion Companiesarerollingmoreandmorefeaturesintosilicon,makingthesystemdesigner’sjob easier.However,thesystemdesignermuststillexercisecautionwhendevisinganinterface betweenrealworldsensorsandaprocessor.IssuesofESD,busloadingandpowerconsumptionstillexistandmustbehandledbythesystemengineer. TheRabbit3000hassimplifiedthebusloadingissuebyprovidinganauxiliaryI/Obus allowingperipheraldevicestobeaddedwithoutundulyloadingthehigh-speedmemorybus. Thecoremoduledesignsaddressissuesofmemoryinterfacing,batterybackup,systemreset, powersupervisionand,onsomecores,analoginterfacing. SomecoremodulesfromRabbitSemiconductorhaveprovidedlownoiseDAQchannels.For example,theRCM3400willdroprightintomanyapplicationsneedingonetoeightsolid 11-bitresolutionDAQchannels.
156
6
CHAPTER
IntroductiontoRabbitAssemblyLanguage Assemblylanguagehaslongbeenafavoriteofprogrammers,formanyreasons.Looking backintohistory,someoftheearlycomputers(suchastheMITSAltair8080)couldonlybe programmedinassemblylanguage.Evensomeindustrialmachines,whichwereconsidered “powerful”fortheirperiod,hadprimitiveprogrammingtechnologiesbypresentdaystandards.Forexample,in1974,theRaytheonRDS500wasoriginallydesignedtotrackmissile trajectoriesbuthadtobeprogrammedoneassemblyinstructionatatime,usingbitswitches onthefrontpanel.Soonafterpersonalcomputersbegantogetpopular,compilersforhigherlevellanguagesbegantoappearforthem,andprogrammerscouldprogramthesemachinesin BASIC,C,FORTRAN,Pascal,andevenForth. Programmersfoundthatcertainthingswerebestdonewithassemblylanguage.Insome cases,high-levellanguagesdidnotgiveprogrammersthelevelofcontroltheydesiredto manipulatemachineinternals,whileinothercases,giventheCPUclockspeedsofthat time,certainthingshappenedtooslowlyunlesstheyweredoneinassemblylanguage.Even higherlevellanguagessuchasCand“simple”languagessuchasBASICallowedprogrammerstomanipulatebitsandbytesandembedassemblylanguagecodebetweenhigher-level statements.Moreover,thelimitedamountofmemoryavailableinsuchsystemsfurthernecessitatedtheneedforefficientcode,thusforcingprogrammerstouseassemblylanguage. Althoughmuchhaschangedfromthosedays,programmersworkingwithmicrocontrollers stillconcludethatsomethingsarebestdonewithassemblylanguage.AlthoughCPUclock speedshavedrasticallyincreasedinthelastthreedecades,mostprogrammerswillagreethat themostefficientcode,inthecontextofexecutiontimeandmemoryusage,isstillwrittenwithassemblylanguage.Whereverthingshavetobedonewithtighttimingconstraints, programmerswillexplorewhethertheyshouldresorttoassemblylanguage.Forexample, usingan8-bitmicrocontrollertoimplementmodemconnectionsequences,aprogrammercan usuallyonlyrelyonassemblylanguagecodetogenerateanddetecttonesintheprecisetime windowsneeded. ThischapterstartswithanoverviewoftheRabbitinstructionset.Subsequentsectionscover someusefulconcepts—passingparametersbetweenassemblylanguageandC,andcoding inamixedC/Assemblyenvironment.Thechapterconcludeswithanumberofprojects, highlightinglow-levelcontrolofon-chipandoff-chipperipherals,aswellastheabilityof assemblylanguagetomakethingshappenincriticaltimewindows.
157
Chapter6
6.1IntroductiontotheRabbit3000InstructionSet Rabbitassemblyinstructionsareusedto:
Loaddataintospecificregistersormemorylocations.Theseinstructionsareused toloaddatafromasourcememorylocationorregisterintoaregister(usuallythe accumulator),performsomeoperations,andtransfertheresultsbackintoaregisteror memorylocation. Exchangecontentsofcertainregisters.Whenoperationsaffectthecontentsofcertain registers,andtheprogrammerwishestosavetheoriginalcontentsoftheregisters, theprogrammercanusetheseexchangeinstructionstoleavetheoriginalregisters alone,workanalternatesetofregisters,andoncetheworkiscompleted,theoriginal registercontentsmayberestored.Whentheregistersare“exchangedout,”(alsocalled “swappedout”)thecontentsoftheoriginalregisterswillbeunaffectedbythepreviousoperations. Pushdataonandoffthestack.Whilekeepingtrackofsubroutines’returnaddresses andtheirparameters,thestackisahandyplacetostoredatatemporarily1.Various instructionsareprovidedto“push”dataonthestackand“pop”itbackintoagiven register.Theprogrammermustbecarefultoremoveasmuchdatafromthestackas waspushedontothestack,otherwiseastackoverflowconditioncandevelop. PerformarithmeticandBooleanoperations.Almostallofthe8-bitarithmeticoperationsinvolvetheaccumulator,while16-bitoperationsinvolve8-bitregisterpairs.The Rabbitevenhasamultiplyinstruction! Testandmanipulateindividualbits.Theseinstructionscomeinespeciallyhandyfor I/Ooperationswhenthecodeneedstoknowifabitgotsetbyanexternalinputor whentheprogramneedstoturnanexternaldeviceonoroffbymanipulatingaport bit.Additionally,bitsofinternalregistersandmemorylocationscanbetested.For example,bitsinthestatusregistercanbetestedtodetermineiftheresultofaprior arithmeticoperationrequiredacarryorborrow. Copyentireblocksofmemory.SomeoftheRabbit’sinstructionsallowtheprogrammertocopyanentireblockofmemoryinasfewasfourinstructions. Jumptoalternatesectionsofcode.Assemblyprogramscanmakebranchingdecisions, usuallybytestingresultsofoperations.Variousinstructionsallowprogrammerstotest forcertainBooleanconditionsandmakebranchingdecisionsaccordingly.
AlthoughtheRabbitinstructionsetisderivedfromtheZ80instructions,quiteafewnew instructionshavebeenaddedtotheRabbit3000microprocessor. TheassemblylanguageinstructionsandtheregisterdesignationsareNOTcasesensitive. However,therearetimeswhenitisadvisabletousespecificcaseinordertomakethecode easiertoread.Forexample,theHLregisterpaircanbeeasilymisinterpretedbythereaderif itisinlowercase:hl.Sometypefontsdonotdowelldifferentiatingbetweenalowercasel andthedigit1. 1
Thisisthemechanismusedtostore“auto”variablesinDynamicCaswellasmostotherCcompilers. 158
IntroductiontoRabbitAssemblyLanguage Rabbitassemblyinstructionsgenerallyconsistofanopcodefollowedbyzeroormoreoperands.Theopcodeisthe“instruction”whiletheoperandsaredata.Operandscantakemany differentforms.Forexample,theymayconsistofa16-bitaddressorbeasinglebit.Table 6.1showshowwerepresentoperandsandresultofoperationsonCPUflags.Thefollowing instructiondescriptionsusetheoperandabbreviationsfromthistable. Table6.1:OperandsusedintheRabbitinstructionset. Operand
b
cc d dd dd’ e
f
m mn n
r,g
ss v xx yy zz
Meaning Bitselect: 000=bit0,001=bit1, 010=bit2,011=bit3, 100=bit4,101=bit5, 110=bit6,111=bit7 Conditioncodeselect: 00=NZ,01=Z, 10=NC,11=C 7-bit(signed)displacement.Expressedintwo’scomplement. Wordregisterselectdestination:00=BC,01=DE,10=HL,11=SP Wordregisterselectalternate:00=BC’,01=DE’,10=HL’ 8-bit(signed)displacementaddedtoPC. Conditioncodeselect: 000=NZ(nonzero),001=Z(zero), 010=NC(noncarry),011=C(carry), 100=LZ2(logicalzero),101=LO3(logicalone), 110=P(signplus),111=M(signminus) MSBofa16-bitconstant. 16-bitconstant. 8-bitconstantorLSBofa16-bitconstant. Byteregisterselect: 000=B,001=C, 010=D,011=E, 100=H,101=L, 111=A Wordregisterselect(source):00=BC,01=DE,10=HL,11=SP Restartaddressselect: 010=0020h,011=0030h, 100=0040h,101=0050h, 111=0070h Wordregisterselect:00=BC,01=DE,10=IX,11=SP Wordregisterselect:00=BC,01=DE,10=IY,11=SP Wordregisterselect:00=BC,01=DE,10=HL,11=AF
Logicalzeroifallfourofthemostsignificantbitsoftheresultare0. Logicaloneifanyofthefourmostsignificantbitsoftheresultare1.
2 3
159
Chapter6 Rabbitassemblyinstructionsaredividedintothefollowinggroups:
LoadandStore − LoadImmediateData − Load&StoretoImmediateAddress − 8-bitIndexedLoadandStore − 16-bitIndexedLoadandStore − RegistertoRegisterMoves ExchangeInstructions StackManipulationInstructions ArithmeticandLogicalOperations − 8-bitArithmeticandLogicalOperations − 16-bitArithmeticandLogicalOperations 8-bitBitSet,ResetandTest 8-bitIncrementandDecrement 8-bitFastAccumulatorOperations 8-bitShiftsandRotates InstructionPrefixes BlockMoveInstructions ControlInstructions—JumpsandCalls MiscellaneousInstructions
Tothosefamiliarwithassemblyprogramming,severalgroupswilllookfamiliar.Mostprocessorshave“loadandstore”and“arithmeticandlogical”instructions.Somegroupswill lookfamiliartoZ-80enthusiastssuchasthegroupcontaining“exchangeinstructions.”However,theRabbitalsohasanumberofuniqueinstructionssuchasthosefoundinthe“block movegroup”and“instructionprefix”group. ThetablespresentedinthistextuseaformatandnomenclatureconsistentwiththeRabbit 2000/3000MicroprocessorInstructionReferenceManual.Eachinstructiondescriptionshows entriesinatablewiththefollowingheadings. Instruction
Clk
A
I
S
Z
V
C
Operation
TheInstructioncolumncontainstheinstructionmnemonicandopcodeformat. TheClkcolumnindicatesthenumberofmachinecyclesrequiredfortheinstructiontoexecute. TheAcolumnindicateswhateffecttheALTDprefixinstructionhasontheinstruction.The followingtableshowsthekeyforthe“A”column. Symbol F R SP
Description ALTDselectsalternateflags ALTDselectsthealternatedestinationregister ALTDoperationisaspecialcase 160
IntroductiontoRabbitAssemblyLanguage TheIcolumnindicateswhateffecttheIOIandIOEprefixinstructionshaveontheinstruction.Thefollowingtableshowsthekeyforthe“I”column. Symbol S D
Description IOIandIOEaffectsource IOIandIOEaffectdestination
TheS,Z,V,andCcolumnscorrespondtotheSign,Zero,OverflowandCarryflags.These arefoundintheRabbit’s“Flags”register(sometimescalledtheStatusorStatusFlagsregister).TheOverflowflagissometimesreferredtoastheLogical/OverfloworLVflaginthe Rabbitprocessordocumentation.Thefollowingtableshowsthekeyforthesymbolsusedin theflagscolumn. Symbol * 0 1 V L
Description Flagaffected Flagunaffected Flagiscleared Flagisset ArithmeticOverflowisstored LogicalResultisstored
6.1.1LoadImmediateData Instructionsthatbelongtothisaddressingmodeloadaconstantintothedestinationregister orregisterpair.Thisiscalledthe“immediate”addressingmodebecausetheconstanttobe loadedimmediatelyfollowstheopcodefortheloadinstruction.Table6.2listsinstructionsin the“immediate”group. Table6.2:Loadimmediatedata. Instruction LDIX,mn LDIY,mn LDdd,mn LDr,n
Clk 8 8 6 4
A
r r
I
S -
Z -
V -
C -
Operation IX=mn IY=mn dd=mn r=n
Thefollowinginstructionsillustratehowimmediatedataisloadedin8and16-bitregisters: ld a,5 ld ix,0x1234
;registeragetsavalueof5(decimal) ;theixregistergetsavalueof1234(hex)
Ifaprogrammerprogramsaloopthatalwaysexecutesafixednumberoftimes,aregistercan besetupastheloopcounterandtheregistercanbeinitializedthefirsttimeusinganimmediateinstruction.Thefollowinginstructionsillustratehowthisisdone:
#defineCOUNTER 240 ld BC,COUNTER
;counterconstant(decimal) ;loadcountervalue
161
Chapter6 6.1.2LoadandStoretoImmediateAddress Inthisaddressingmode,oneoftheoperandsisaregister,whiletheotheroperandisfetched frommemory.Dependingontheinstruction,a16-bitaddressisusedtopointtosourceor destinationdata.Thisinstructionsethastheterm“immediateaddress”initbecausethe16-bit addressimmediatelyfollowstheopcode.Table6.3listsinstructionsinthisgroup. Table6.3:Loadandstoretoimmediateaddress. Instruction
clk
LD(mn),A
10
LDA,(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
LDHL,(mn)
11
s
-
-
-
-
L=(mn);H=(mn+1)
LDIX,(mn)
13
s
-
-
-
-
IXL=(mn);IXH=(mn+1)
LDIY,(mn)
13
s
-
-
-
-
IYL=(mn);IYH=(mn+1)
LDdd,(mn)
13
s
-
-
-
-
ddL=(mn);ddH=(mn+1)
r
r
Theaboveinstructionsareusefulforperformingsimplepointer-basedoperations.The16-bit pointercanstoreorretrieveoneortwobytestoorfrommemory(orI/O,iftheI/Oismemory mapped).Forexample,ifaprogramhastouseanumberofvariables,thevariablescanbe storedinmemorylocationsandtheresultsofoperationscanbestoredinthevariables. Thefollowinginstructionsillustratesomeexamplesofthisinstructiongroup:
#defineBUFFER_SIZE64 char bytestoread int bytecounter
;spcetoallocateforbuffer ;numberofbytestoread ;numberofbytesthathavebeenread
ld
a,(bytestoread)
;findouthowmanybytestoread
ld
(bytecounter),hl
;updatebytecounter
ld
ix,(sp+2+BUFFER_SIZE)
;ix=buffer
6.1.38-bitIndexedLoadandStore Intheirsimplestform,theseinstructionscomeinhandyforperformingpointer-basedoperations.Forinstance,ifaprogramhastoaddasetofvaluesstoredinRAM,theprogrammer cansetuparegisterpairtopointtothestartinglocationofthetableinRAM,readthevalues onebyone,andkeepaddingthemtoadestinationregister. 162
IntroductiontoRabbitAssemblyLanguage Inamorecomplexapplicationofindexedaddressing,thecontentsofanindexregisterare addedtoadisplacementtocomputetheaddressoftheoperand.Thisiswhatthe“IX+d” and“IY+d”instructionsbelowaccomplish,where“d”isthe8-bitdisplacement.Clanguage programmerscanthinkofthe“indexwithdisplacement”modelasastructurewheretheindex registerpointstothebeginningofthestructureandthedisplacementisusedtopointtoelementswithinthestructure.Table6.4lists8-bitindexedinstructions: Table6.4:8-bitindexedloadandstore. Instruction
clk
A
I
S
Z
V
C
Operation
LDA,(BC)
6
r
s
-
-
-
-
A=(BC)
LDA,(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
LDr,(HL)
5
s
-
-
-
-
r=(HL)
r
LD(IX+d),n
11
d
-
-
-
-
(IX+d)=n
LD(IX+d),r
10
d
-
-
-
-
(IX+d)=r
LDr,(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
LDr,(IY+d)
9
s
-
-
-
-
r=(IY+d)
r
r
AsimpleexampleofindexedaddressingwouldbeaprogramthataddsaseriesofcontiguousintegersinRAM.Theprogrammercansetupa16-bitregisterpairtopointtothestartof thetable,andthenloadeachintegerintotheaccumulator.Theprogramcanaddindividual integerstoa32-bitresultvalueinRAM. Thefollowinginstructionsillustrate8-bitindexedloadandstores:
ld ld
a,(iy+8) (ix+8),b
; ;
163
Chapter6 6.1.416-bitIndexedLoadandStore The16-bitloadsandstoresaresimilartotheircounterpartsdescribedinSection6.1.3,except thatinthiscasethesourceordestinationisa16-bitregisterpairortwocontiguouslocations inmemory. Becausetheseinstructionsrequirefetchingaddressesfrommemory,addingadisplacement, andstoringthecontentstoadestinationregisterpair,theinstructionsrequirearelativelylarge numberofclockcyclestoexecute.Table6.5lists16-bitindexedinstructions: Table6.5:16-bitindexedloadandstore. 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
LDHL,(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
LDHL,(SP+n)
9
-
-
-
-
L=(SP+n);H=(SP+n+1)
LDIX,(SP+n)
11
-
-
-
-
IXL=(SP+n);IXH=(SP+n+1)
LDIY,(SP+n)
11
-
-
-
-
IYL=(SP+n);IYH=(SP+n+1)
LD(IX+d),HL
11
d
-
-
-
-
(IX+d)=L;(IX+d+1)=H
LDHL,(IX+d)
9
s
-
-
-
-
L=(IX+d);H=(IX+d+1)
LD(IY+d),HL
13
d
-
-
-
-
(IY+d)=L;(IY+d+1)=H
LDHL,(IY+d)
11
s
-
-
-
-
L=(IY+d);H=(IY+d+1)
r
r
r r
Thefollowinginstructionsutilize16-bitindexedloadandstores:
ld ld
hl,(ix+4) (ix+2),hl
; ;
6.1.5RegistertoRegisterMoves Thisaddressingmodeisalsoreferredtoas“registeraddressing,”sincealltheoperandsare theCPUregisters.Asthenameimplies,theseinstructionscopycontentsofoneregister intoanother—anyofthe8-bitregisterscanbemovedintoanyother8-bitregister.Thisis necessarybecausemostofthemathandlogicaloperationsareperformedinanaccumulator(registerAorregisterpairHL).AsshowninTable6.6,certain16-bitregisterscanalso performamoveintoother16-bitregisters. BecauseregistertoregistermoveshappenwithintheCPUanddonotrequireoperandstobe fetchedfrommemory,theinstructionstakefewclockcycles.
164
IntroductiontoRabbitAssemblyLanguage Table6.6:Registertoregistermoves. Instruction
clk
A
LDr,g
2
LDA,EIR
I
S
Z
V
C
Operation
R
-
-
-
-
r=g(r,ganyofB,C,D,E,H,L,A)
4
fr
*
*
-
-
A=EIR
LDA,IIR
4
fr
*
*
-
-
A=IIR
LDA,XPC
4
R
-
-
-
-
A=MMU
LDEIR,A
4
-
-
-
-
EIR=A
LDIIR,A
4
-
-
-
-
IIR=A
LDXPC,A
4
-
-
-
-
XPC=A
LDHL,IX
4
R
-
-
-
-
HL=IX
LDHL,IY
4
R
-
-
-
-
HL=IY
LDIX,HL
4
-
-
-
-
IX=HL
LDIY,HL
4
-
-
-
-
IY=HL
LDSP,HL
2
-
-
-
-
SP=HL
LDSP,IX
4
-
-
-
-
SP=IX
LDSP,IY
4
-
-
-
-
SP=IY
LDdd',BC
4
-
-
-
-
dd’=BC(dd':BC',DE',HL')
LDdd',DE
4
-
-
-
-
dd’=DE(dd':BC',DE',HL')
Forexample,considertheIIRregister:itpointstoaninterruptvectortablespecifictointernallygeneratedinterrupts.Whenaprogrammerwishestouseinternallygeneratedinterrupts, theIIRregistercannotbeimmediatelyloadedwithavalue—suchanopcodedoesnotexist. Instead,theimmediatevaluecanbeloadedintheaccumulator,andtheaccumulatorcanbe copiedintotheIIR. 6.1.6ExchangeInstructions TheCPUcontainsan“alternateregisterset,”whereregisterpairsAF,HL,BC,andDEcontaintheiralternates,AF",HL',BC',andDE',respectively.Undernormalcircumstances,the programmerdoesnotusethealternateregisterpairs,sinceDynamicCusesthemforitsown purposes. Asthenameindicated,the“Exchange”instructionsswapcontentsof16-bitregisterswith specialalternateregisters.Forinstance,the“EXDE',HL”instructionswapsthecontentsof HLwiththoseofalternateregisterpairDE'.Therearetwospecialcases:
The“EXX”instructiondoesthreeswapswithasingleinstruction:BC,DEandHL. The“EXAF,AF'”instructiontreatstheaccumulatorandflagsregisterasaregister pairandswapsthemoutwiththeiralternateregisters. The“ALTD”prefix,coveredlaterinSection6.1.14,allowsinstructiontodirectly accessthealternateregisters,withoutexchangingalltheregisters
165
Chapter6 Table6.7listsvariousexchangeinstructions. Table6.7:Exchangeinstructions. 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)
EXAF,AF'
2
-
-
-
-
EXDE',HL
2
s
-
-
-
-
AFAF’ if(!ALTD)thenDE'HL elseDE’HL'
EXDE',HL'
4
s
-
-
-
-
EXDE,HL
2
s
-
-
-
-
EXDE,HL'
4
s
-
-
-
-
DEHL'
EXX
2
-
-
-
-
BCBC’;DEDE';HLHL'
DE’HL' if(!ALTD)thenDEHL elseDEHL’
Figure6.1illustrateshowtheregisterpairsareswapped: 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’
Figure6.1:ExchangeInstructions
6.1.7StackManipulationInstructions Itisassumedthereaderknowswhatastackisandhowitworks.Table6.8liststhestack manipulationinstructions.Thefirstinstructioninthisgroup(ADDSP,d)addsaone-byte displacementtothestack.Thisisaquickwayfortheprogrammertomovethestackpointer forwardbyupto255bytes,withoutchangingthecontentsofthestack.Thedisplacementis alwayspositive.Themainreasonfortheinstructionwouldbeforreservingablockofmemoryonthestack. Theotherinstructionsinthisgroupeitherpush16-bitregisterpairsonthestack,orpopthemoff. Theseinstructionsallowtheprogrammertostore(push)dataandvariablesonthestack, andthenreadthemback(popthem)whenrequired.Onehastobecarefulindealingwith thestackwhenworkingwithsubroutinesorinterrupts,sincetheseelementsusethestackto rememberwheretoreturnaftertheyaredone.Forinstance,ifamainprogramhastopass 166
IntroductiontoRabbitAssemblyLanguage datatoasubroutinethroughthestack,thecallingprogramcanpushthedataonthestack. Whenthesubroutineiscalled,itcannotsimplyreadthedatausingthecurrentstackpointer, sincethesubroutinecallhasalteredthestackcontentsandthestackpointer—thestack pointernowhastobeadjustedinordertopoptherelevantdatacorrectly,andthenadjusted againtopointtothereturnaddressofthecallingprogram. InDynamicCitistheresponsibilityoftheCALLINGroutinetorestorethestackpointer oncethecalledroutinehasreturned.ThecalledroutineaccessesthepassedvaluesusingSP relativeindexedaddressing. Table6.8:Stackmanipulationinstructions. Instruction
clk A f
I
S
Z
V
C
Operation
-
-
-
*
SP=SP+d—d=0to255
ADDSP,d
4
POPIP
7
-
-
-
-
IP=(SP);SP=SP+1
POPIX
9
-
-
-
-
IXL=(SP);IXH=(SP+1);SP=SP+2
POPIY
9
-
-
-
-
POPzz
7
-
-
-
-
IYL=(SP);IYH=(SP+1);SP=SP+2 zzL=(SP);zzH=(SP+1); SP=SP+2—zz=BC,DE,HL,AF
PUSHIP
9
-
-
-
-
(SP-1)=IP;SP=SP-1
PUSHIX
12
-
-
-
-
(SP-1)=IXH;(SP-2)=IXL;SP=SP-2
PUSHIY
12
-
-
-
-
PUSHzz
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.88-bitArithmeticandLogicalOperations TheRabbitmicroprocessorperformsthetypicalarithmeticandlogicaloperationsfound inother8-bitprocessors:adds,subtracts,complements,ANDs,ORs,XORs.Thereareno multiplyordivideinstructionsinthisinstructiongroup,althoughthe16-bitinstructiongroup allowsforamultiplyinvolving16-bitregisterpairs. Therearespecialaddandsubtractinstructionsthatalsotakethecarrybitintoaccount. Theseinstructionsworkusingtheaccumulatorandanoperandthatcanbeanimmediatevalue,another8-bitregister,oramemorylocationpointedtobytheHL,IXorIYregisterpairs.
167
Chapter6 Table6.9lists8-bitarithmeticandlogicaloperations. Table6.9:8-bitarithmeticandlogicaloperations. Instruction ADCA,(HL) ADCA,(IX+d) ADCA,(IY+d) ADCA,n ADCA,r ADDA,(HL) ADDA,(IX+d) ADDA,(IY+d) ADDA,n ADDA,r AND(HL) AND(IX+d) AND(IY+d) ANDn ANDr CP(HL) CP(IX+d) CP(IY+d) CPn CPr OR(HL) OR(IX+d) OR(IY+d) ORn ORr SBC(IX+d) SBC(IY+d) SBCA,(HL) SBCA,n SBCA,r SUB(HL) SUB(IX+d) SUB(IY+d) SUBn SUBr XOR(HL) XOR(IX+d) XOR(IY+d) XORn XORr
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(coutif(r-CY)>A) A=A-r-CY(coutif(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]
SBCandCPinstructionoutputinvertedcarry.CissetifA=B.SUBoutputscarryinoppositesensefromSBCandCP.
168
IntroductiontoRabbitAssemblyLanguage Thefollowinginstructionsillustrate8-bitarithmeticandlogicaloperations: #define BIT_MASK cp a,b ; andBIT_MASK ; cp 0x01 ; xor0x80 ;
0xA5
6.1.916-bitArithmeticandLogicalOperations Theseinstructions,showninTable6.10,aresimilartotheir8-bitcounterpartsexceptforone keydifference:becausetheRabbitCPUonlyhas8-bitregisters,the16-bitmathinstructions useregisterpairsasoperands. ThereisamultiplyinstructionthatusesBCandDEasoperandsandstorestheresultinHLand BC.Therearevariousincrement,decrement,androtateinstructionsthatinvolveregisterpairs. Table6.10:16-bitarithmeticandlogicaloperations. Instruction ADCHL,ss ADDHL,ss ADDIX,xx ADDIY,yy ADDSP,d ANDHL,DE ANDIX,DE ANDIY,DE BOOLHL BOOLIX BOOLIY DECIX DECIY DECss INCIX INCIY INCss MUL ORHL,DE ORIX,DE ORIY,DE RLDE RRDE RRHL RRIX RRIY SBCHL,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=0to255 HL=HL&DE IX=IX&DE IY=IY&DE if(HL!=0)HL=1,setflagstomatchHL 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,signed32bitresult.DE unchanged HL=HL|DE—bitwiseor IX=IX|DE IY=IY|DE {CY,DE}={DE,CY}—leftshiftwithCF {DE,CY}={CY,DE} {HL,CY}={CY,HL} {IX,CY}={CY,IX} {IY,CY}={CY,IY} HL=HL-ss-CY(coutif(ss-CY)>hl)
Chapter6 6.1.108-bitBitSet,ResetandTest Thebitmanipulationinstructionsareusedtosetorresetbits,aswellastestthestatusofbits. Consideran8-bitparallelportthatusescertainbitsforoutputs;inordertochangethestatus ofjustonebit,thecontentsoftheportcanberead,onebitchangedusingtheseinstructions, andthecontentscanbewrittenbacktotheport.Thissequenceisoftenreferredtoasareadmodify-writeoperation. Instructionsinthisgroup,showninTable6.11,allowbitstobemanipulatedincertain8-bit registers,ormemorylocationspointedtobyHL,IXandIYregisterpairs. Table6.11:8-bitbitset,resetandtest. Instruction BITb,(HL) BITb,(IX+d)) BITb,(IY+d)) BITb,r RESb,(HL) RESb,(IX+d) RESb,(IY+d) RESb,r SETb,(HL) SETb,(IX+d) SETb,(IY+d) SETb,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
Intheabovetable,“bit”isavaluebetween0and7,where7isthemostsignificantbit. Thefollowinginstructionsillustratevariousbitset,resetandtestoperations: #defineON_BIT setON_BIT,a
4
;bittoturnmotoron ;turnmotoron
bit7,(hl);testbusybit res7,(hl);clearbusybit
6.1.118-bitIncrementandDecrement TheseinstructionsoperateonlocationspointedtobyHL,IXorIYregisterpairs,aswell asdirectlyontheCPU’s8-bitregisters.Anincrementordecrementisalwaysquickerthan an“addoneto”or“subtractonefrom,”respectively,becausenoimmediatedatahastobe loaded.Unlikethe16-bitINC/DECinstructions,their8-bitcounterpartsdoaffectthestatus flags.The8-bitincrementanddecrementinstructionsareshowninTable6.12.
170
IntroductiontoRabbitAssemblyLanguage Table6.12:8-bitincrementanddecrementinstructions. Instruction DEC(HL) DEC(IX+d) DEC(IY+d) DECr INC(HL) INC(IX+d) INC(IY+d) INCr
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.128-bitFastAccumulatorOperations Theseinstructionsareconsidered“fast”becausetheydonotloadimmediatedata,oruse pointerstopointtooperands—everythinghappensrightintheaccumulator.Table6.13lists instructionsinthisgroup. Table6.13:8-bitfastaccumulatoroperations. 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.138-bitShiftsandRotates Theseinstructionsallowbitstobeshiftedorrotatedinandoutofaregisteroramemorylocation.Beforeweproceed,itisimportanttohaveadistinctionbetweenshiftsandrotates:
A“shift”happenswhenbitsinaregisterormemorylocationareshiftedoneplace, leftorright.Thebitshiftedoutgoestothecarryflag,whilea0getsshiftedin.There isaspecialcase(SRA) wherethemostsignificant RL, RLA SLA 0 bitremainsunchanged. C C A“rotate”isdifferentfrom ashiftbecausebitsare shiftedoutthroughthecarry flag,andthenshiftedbackin (seeFigure6.2).Thereare specialcasesaboutwhichbit getsshiftedin,thecarrybit orthebitbeingshiftedout.
RLC, RLCA
C
C
SRA
RR, RRA
C
RRC, RRCA
C
SRL
0
Figure6.2:8-bitshiftsandrotates. 171
C
Chapter6 Therearemanyreasonsaprogrammermaywanttoshiftbits: A“leftshift”hasthesameaffectasa“multiply-by-two”whilea“rightshift”performsa“divide-by-two.”Thisislikelythequickestwaytoachievetheseresults.A programmaycallforreceivingabyte,reversingtheorder,andtransmittingoutthe result.Onceabytehasbeenreceivedandverifiedforcorrectness,itisveryeasyto shiftitsbitsoutthroughthecarryflag,androtatethosebitsintoaseparateregisteror memorylocation.Thiswouldeffectivelyplacebitsinreverseorderintothedestination.Inthecaseofdivisionbytwo,thereisalossofresolution,butwhenperforming integermath,areducedprecisionisinevitable. Programmerssometimesuseparallelportpinstotransmitserialdata,especiallyin caseswheretheserialportsarealreadybeingused.Whentransmittingdataserially, aprogrammerwilltypicallystorethebytetobetransmittedinaparallelregister,and willshifteachbitthathastobetransmitted.Similarly,onthereceivingend,each receivedbitwillbeshiftedinsothatacompletebytecanbeputtogether.Usingport pinsasserialportsinthismannerisoftenreferredtoas“bit-banging”aserialport. Table6.14listsinstructionsinthe“shiftandrotate”group. Table6.14:8-bitshiftsandrotates. Instruction RL(HL) RL(IX+d) RL(IY+d) RLr 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
*
RLCr RR(HL) RR(IX+d) RR(IY+d) RRr 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
*
RRCr SLA(HL) SLA(IX+d) SLA(IY+d) SLAr
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]
IntroductiontoRabbitAssemblyLanguage Table6.14:8-bitshiftsandrotates(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
*
SRAr SRL(HL) SRL(IX+d) SRL(IY+d) SRLr
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.14InstructionPrefixes TheRabbithastwoI/Ospaces:internalI/OregistersandexternalI/Oregisters.Instruction prefixesIOIandIOEcanbeusedtogeneratecodethataccessesinternalorexternalI/Oregistersinsteadofaccessingmemory.Withtheseprefixes,any16-bitmemoryaddressisdecoded asaninternalorexternalI/Oaddress:
TheIOIinstructionprefixcausesthefollowinginstructiontoaccessaninternalI/O portinsteadofmemory.SincetheinternalI/Operipheralsoccupythefirst256bytes oftheinternalI/Ospace,theupperbyteofthe16-bitmemoryaddressisignoredand thelowerbyteisusedtoaccesstheinternalI/Oport.
ThefollowinginstructionprovidesasanexampleoftheIOIprefix.TheuseofshadowregistersisdefinedinSection6.3.7. xor ld ioild
a (PEFRShadow),a (PEFR),a
;portebit1..7inputs,0output ;updateshadowregisterfirst ;setupfunctionregister
ld ld ioild
a,0x01 (PEDDRShadow),a (PEDDR),a
;updateshadowregisterfirst ;setupdatadirectionregister
TheIOEinstructionprefixcausesthefollowinginstructiontoaccessanexternalI/O portinsteadofmemory.SinceexternalI/Operipheralscanbemappedwithina64K space,thefull16-bitaddressisusedtoaccessexternalI/Oports.Bydefault,writesare inhibitedforexternalI/OoperationsandfifteenwaitstatesareaddedforI/Oaccesses.
ThefollowinginstructionprovidesasanexampleoftheALTDprefix: #define
EXT_ADDRESS 0xFC00;externaladdress
ld ld
ioe
hl,EXT_ADDRESS ;setuppointer (hl),a ;putdatavalueexternally
173
Chapter6
TheALTDprefixcausestheimmediatelyfollowinginstructiontoaffectthealternate flags,orusethealternateregistersforthedestinationofthedata,orboth.Usingthis instructionallowstheprogrammertoaccessthealternateregistersetoralternateflags directly,withouttheneedtoexchangealltheregisters.SeeSection6.1.6formore detailsonthealternateregisterset.
ThefollowinginstructionprovidesasanexampleoftheALTDprefix:
altd
ex
de,hl
;de=index(wasinhl’)
InstructionprefixesareshowninTable6.15. Table6.15:Instructionprefixes. Instruction ALTD IOE IOI
clk 2 2 2
A
I
S -
Z -
V -
C -
Operation alternateregisterdestinationfornextInstruction I/Oexternalprefix I/Ointernalprefix
6.1.15BlockMoveInstructions Theseinstructions,listedinTable6.16,areusedtorapidlycopy(ormove)blocksofdata fromonepartofmemorytoanother.Theyworkinthefollowingmanner: BCissetupasthecounter—numberofbytestocopy HLissetuptopointtothesourceblock DEissetuptopointtothedestinationblock Theinstructionsdifferfromeachotherinthefollowingmanner: LDD:
SetupDEtopointtodestinationaddress SetupHLtopointtosourceaddress Onebyteofdataiscopiedfrom(HL)to(DE) bothHLandDEaredecremented
LDDR: SetupDEtopointtohighestaddressofdestinationblock SetupHLtopointtohighestaddressofsourceblock Ablockofdataiscopiedfrom(HL)to(DE);blocksizeisspecifiedbyBC bothHLandDEaredecrementedduringtheoperation LDI:
SetupDEtopointtodestinationaddress SetupHLtopointtosourceaddress Onebyteofdataiscopiedfrom(HL)to(DE) bothHLandDEareincremented
174
IntroductiontoRabbitAssemblyLanguage LDIR:
SetupDEtopointtolowestaddressofdestinationblock SetupHLtopointtolowestaddressofsourceblock Ablockofdataiscopiedfrom(HL)to(DE);blocksizeisspecifiedbyBC bothHLandDEareincrementedduringtheoperation Table6.16:Blockmoveinstructions.
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.16ControlInstructions—JumpsandCalls Instructionsinthisgroupaffectprogramflow,andcanbedividedintothefollowingsections:
UnconditionalJumps:Theseinstructionsstartexecutionfromanotherpointincode. AsTable6.17Aindicates,theseinstructionsstartprogramexecutionfromafixed address(PC=mn),orfromanaddresspointedtotheHL,IXorIYregisterpair.The LJPinstructionisspecialbecauseitallowsajumptobemadetoacomputedaddress inXMEM. Table6.17A:Unconditionaljumpinstructions.
Instruction JPmn JP(HL) JP(IX) JP(IY)
Clk A 7 4 6 6
JRe LJPxpc,mn
I
S -
Z -
V -
C -
5
-
-
-
-
10
-
-
-
-
Operation PC=mn PC=HL PC=IX PC=IY PC=PC+e(ife==0nextseqinstruction isexecuted) XPC=xpc;PC=mn
ConditionalJumps:Theseinstructions,showninTable6.17B,causeajumpifoneof thegivenconditionsismet. Table6.17B:Conditionaljumpinstructions. Instruction JPf,mn JRcc,e
Clk
A 7 5
I
S -
Z -
175
V -
C -
Operation if{f}PC=mn if{cc}PC=PC+e
Chapter6
SubroutineCallscanbemadetoa16-bitaddressusingtheCALLinstruction, ortoacomputedaddressinXMEM,usingtheLCALLinstruction,asshownin Table6.17C. Table6.17C:Subroutinecalls. Instruction
Clk A
I
S
Z
V
C
CALLmn
12
-
-
-
-
LCALLxpc,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)
Returnsfromsubroutinescanbemadeunconditionally,usingtheRETinstruction, orutilizeoneofseveralflagconditionswiththeRETfinstruction.LRETisused toreturnfromsubroutinesstoredinXMEMspace.Theseinstructionsareshownin Table6.17D. Table6.17D:Returninstructions.
Instruction RET RETf
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
InterruptServiceRoutines(ISRs)presentaspecialcase:
Unlikeotherprocessors,theRabbitusesa“Return”(RET)instructionratherthan a“ReturnfromInterrupt”(RETI)instructiontoreturnfromaninterrupt.TheRETI instructionisshowninTable6.17Eanddoesnotseemtoserveanyusefulpurpose.
Inmostcases,aninterruptserviceroutineshouldbeterminatedasfollows:
ipres ret
;restoretheinterruptpriority ;returntointerruptedcode
Aprogrammerdoesnotusuallyhavetodoanythingspecialtousethisinstruction;justputtinga“RET”attheendoftheinterruptserviceroutineissufficient.Chapter7providesmany examplesofthisinstruction. Table6.17E:Returnfrominterruptinstruction. Instruction
clk
RETI
12
A
I
S
Z
V
C
-
-
-
-
176
Operation IP=(SP);PCL=(SP+1); PCH=(SP+2);SP=SP+3
IntroductiontoRabbitAssemblyLanguage
DecrementandJumpifNon-Zero,showninTable6.17F.Thisinstructionisespeciallyhandyforimplementingloops.Anyblockofcodeprecedingthisinstructioncanbe repeated,providedthatitmeetsthefollowingconditions: − Theblockofcodecanexecuteamaximumof256times(ifB=0atthestartofthe count). − Theentirecodeblockthathastoberepeatedmustfitwithinthisdistancelimitation.Thisisbecausetheinstructionperformsarelativejumptowithina256-byte distancefromthecurrentinstruction, Table6.17F:Decrementandjumpinstruction. Instruction DJNZj
Clk 5
A R
I
S -
Z -
V -
C -
Operation B=B-1;if{B!=0}PC=PC+j
Reset(RST)instruction.Thisinstruction,showninTable6.17G,pushesthecurrent ProgramCounterontothestackandthenstartsprogramexecutionfromvectorvin theinterrupttable.Storingthecurrentprogramcounteronthestacktellstheinterrupt wheretoresumeoperationafterservicingtheinterrupt.
Asshowninthetable,RST10,RST18,RST20,RST28,andRST38areavailable.The RST0x28instructionisspecialbecauseittransfersprogramexecutiontotheDynamicC debugkernel.ThisistheonlyresetinstructionusedbyDynamicC. Table6.17G:Resetinstruction. Instruction
Clk
RSTv
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,38only
Chapter6 6.1.17MiscellaneousInstructions Thefollowinginstructionsdonotfitintheabovegroups;abriefdescriptionofeachinstructionisshowninTable6.18: Table6.18:Miscellaneousinstruction. Instruction CCF IPSET0 IPSET1 IPSET2 IPSET3 IPRES LDA,EIR LDA,IIR LDA,XPC LDEIR,A LDIIR,A LDXPC,A NOP POPIP PUSHIP 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 NoOperation IP=(SP);SP=SP+1 (SP-1)=IP;SP=SP-1 CF=1
6.2SomeUniqueRabbitInstructions AlthoughtheRabbit3000instructionsetisderivedfromZ-180instructions,therearesome significantdifferences.TheRabbit3000usessomespecialinstructionsinareasofI/Oand memoryaccess;theseinstructionsdonotexistfortheZ-180.Moreover,theRabbit3000has droppedsomeZ-180instructions;programmerscanrefertotheRabbit3000microprocessor usermanualfordetailsontheseinstructions. 6.2.1InstructionsThatMakeSomeOperationsMoreEfficient TheDJNZinstruction,coveredinTable6.17F,isusefulforrepeatedlyexecutinga blockofcode. TheLDD,LDDR,LDIandLDIRinstructions,coveredinTable6.16,areusefulfor movingablockofmemory. The“LCALLxpc,mn”instruction,showninTable6.17C,allowsajumptoacomputedaddressinXMEM. The16-bitmultiplyinstruction,showninTable6.10,isusefulforgeneratingasigned 32-bitmultiplyoutoftheBCandDEregisters. 6.2.2InstructionsThatBypasstheMemoryManagementUnit Chapter2coveredtheMemoryManagementUnit(MMU)insomedetail.Instructionsin Table6.19bypasstheMMUandaccesstheentirephysicalmemoryspacedirectly.While 178
IntroductiontoRabbitAssemblyLanguage theinstructionsuse16-bitregisterpairs,theytargeta20-bitaddressspace.Thefourmost significantbitsofthe20-bitaddressarederivedfromthefourleastsignificantbitsofthe accumulator(bits3though0). Table6.19:InstructionsthatbypasstheMMU. Instruction
clk
LDP(HL),HL
A
I
S
Z
12
-
-
LDP(IX),HL
12
-
-
LDP(IY),HL
12
-
-
LDPHL,(HL)
10
-
-
LDPHL,(IX)
10
-
-
LDPHL,(IY)
10
-
-
LDP(mn),HL
15
-
-
LDP(mn),IX
15
-
-
LDP(mn),IY
15
-
-
LDPHL,(mn)
13
-
-
LDPIX,(mn)
13
-
-
LDPIY,(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
Chapter6
6.3StartingtoCodeAssemblywithDynamicC DynamicCprovidesalotoffreedomtotheassemblylanguageprogrammer.TherearevariouswaysofincorporatingassemblylanguageinDynamicC:
AssemblylanguagestatementscanbeembeddedinCfunctions Entirefunctionscanbewritteninassemblylanguage Cstatementsmaybeembeddedinassemblycode C-languagevariablesmaybeaccessedbytheassemblycode ParametersmaybepassedbetweenC-languagecodeandassemblycode.Thisis describedinSection6.4.
6.3.1InlineAssemblyCode DynamicCallowstheuseofassemblylanguageanywhereinthecode;theprogrammersimplyhastoput“#asm”beforeand“#endasm”aftertheblockofassemblylanguagecodethat needstobeassembled.Program6.1illustratesthisconceptwithasimpleexample: Program6.1:Inlineassembly. printf(“\nWarningLighton\n”);
//operatinginC
#asm ld set ld ioild #endasm
;getcontentsofshadowregister ;turnLEDon ;updateshadowregister ;writedatatoportg
a,(PGDRShadow) 6,a (PGDRShadow),a (PGDR),a
get_status();
//backtoC
6.3.2“C”Wrappers Ablockofassemblycodecanbeencapsulatedwithina“C”wrapper.Thisistypicallydone whena“C”functionisdefinedinordertocontainassemblycode.Parameterstothe“C” functioncanbepassedintoassemblycodeusingpointers.FunctioninitPortinProgram 6.11providesanexampleofaCwrapperencapsulatingassemblycode. ACfunctioncontainingembeddedassemblycodemayuseaCreturnstatementtoreturn avalue. 6.3.3StandaloneAssembly Programmerscanwriteentirefunctionsinassemblyandbenefitfromthespeedofmachine language.AssemblyfunctionscanbedeclaredjustasCfunctionsandcanreceiveandreturn parameters.Asanexample,Program6.2showshowanassemblyfunctionisusedforfast tablelookupandthevalueisreturnedtotheCcallingprogram.
180
IntroductiontoRabbitAssemblyLanguage Program6.2:Codefragmentshowingstandaloneassemblyfunction. intCRC_lookup(intvalue); main() { inti,j; i=1; j=CRC_lookup(i); } #asm CRC_lookup:: ... ldhl,a//ThereturnvalueisputinHL ret//justbeforethefunctionreturns #endasm
Afewthingstonoteaboutstandaloneassemblyfunctions:
Afunctionprototypeneedstobedeclaredbeforethefunctiondeclaration.Ideally, thefunctionprototypeisdeclaredtowardsthebeginningoftheprogram.Defininga functionprototypeisimportantsothatfunctionsthathavenotbeencompiledmaybe called,andthatthecompilercandotypecheckingonthefunction’sparameters. NotetheuseofdoublecolonsindeclaringfunctionCRC_lookup—thisisDynamic C’sstandardmethodforcallingstandaloneassemblyroutinesfromC.Theassemblyfunctionisidentifiedbythedoublecolon“::”andthedoublecolonconstruction declaresthatthelabelisofglobalscope. CPUregistersusedbytheassemblyprogramdonothavetobesaveduponentering thefunctionorrestoreduponexit.However,aswewillseeinChapter7,ifthestandaloneassemblyfunctionisanInterruptServiceRoutine(ISR),thisruleisreversedand CPUregistersusedbytheISRhavetobesaveduponentryandrestoreduponexit.
181
Chapter6 6.3.4EmbeddingCStatementsinAssembly DynamicCallowsembeddingCstatementsinassembly.TheCstatementshavetobeprecededbya“c”indicatingthattheyaretobecompiledandnotassembled.Thefollowingcode fragmentusesembeddedCcodetoperformsomemathinsomeassemblycode: Program6.3:CodefragmentshowingembeddedCstatementsinassembly. intresult(intx); #defineDIVIDEND57 #defineDIVISOR19 inttemp1,temp2,temp3; /////////////////////////////////////////////////////////// //intresult(intx) //theassemblysubroutinereturnsx+(DIVIDEND/DIVISOR) /////////////////////////////////////////////////////////// #asmdebugroot result:: //HLshouldcontainparameterx
exde,hl
;storexinDE
push
;saveneededregisters
ctemp1=DIVIDEND/DIVISOR; ctemp2=(DIVIDEND*4)/(DIVISOR-3); ctemp3=temp1+temp2;
pop
ld
de
de
a,(temp3)
;restoreneededregisters
;loadglobalvariable
ldh,0 ldl,a addhl,de
;addxandtemp3
#endasm
;returnresultinHLandDE
ret
main() { intx,y,z; for(x=0;x//
ThecurrentTimeis ::
Thecurrenttemperatureis
Inadditiontointegralcomparisons,ZHTMLhasafewspecialexpressionsforifstatements. Theerror()expressionevaluatestotrueifwearedisplayingthispageasaresultofanerror ininput.Weusethistotelltheusertotryenteringthedateandtimeagain.
Theclockwassetsuccessfully.
Theupdating()expressionevaluatestotrueifwearedisplayingthispageastheresultofan attemptedvariableupdate.Thesenestedifstatementswilldisplaya“success”messageifwe haveupdatedwithouterror.
ThisisnotaZHTMLcommand,butratherthebeginningofanHTMLform.Wenoteithere becauseoftheactionandmethodparameters.Theactionparameterindicatesthatafterthe receivedvariableupdatesfromtheformhavebeenprocessed,the“/time.zhtml”pagewill bedisplayed(orinthiscase,redisplayed).Forthewebservertoprocessthevariables,the methodparametermustalwaysbe“POST”.
>
Theabovecodegeneratesthepull-downmenuforthemonth.Notethatthenameoption totheSELECTtagis“time.month”,withoutthe“$”.Thevariablenamemustalways 401
Chapter9 match,butmustnotincludethe“$”signwhengivenasparameterstoaformelement. “count($time.month)”returnsthenumberofoptionsintheselectionvariable,time.month. “selected($time.month,$A)”isanotherspecialifstatementexpression.Itreturnswhether ornotthe$Athoptionisselected.The“print_opt($time.month,$A)”commandoutputsthe stringcorrespondingtothe$Athoption.Thecurrentmonthisselectedbydefaultfortheuser. ”>
Thislinecreatesafieldforthehour.Noticetheprintf()ZHTMLstatement.printf()allows ustoprovideaformatspecifier.Notethatprintf()onlyallowsasinglevariable,unlike printf()inC. Theremainderoftime.zhtmliseithersimilartowhatwehavealreadystudied,orcontains onlystandardHTML. sprinkler.zhtml
Althoughsprinkler.zhtmlisthelongestZHTMLpage,wehavealreadycoveredalmostall oftheZHTMLfeaturesthatituses.Oneinterestingtechniqueusedonthispagehasnothing todowithZHTML.Rather,itusesJavaScript:
Checkthesprinklerstatus
TheaboveJavaScriptfunctionopensanewwindowwithawidthof400pixelsandaheight of200pixels.Thisnewwindowwilldisplaythe“status.zhtml”page.The“AHREF”tag containsthelinktotheJavaScriptopenStatus()functionthatwedefined. Wedisplaythehoursinthetableinaslightlydifferentmannerthanbefore:
402
Networking Theecho()commandisactuallythesameastheprint()commandnotedearlier. Normally,checkboxesinHTMLonlysendavaluetothewebserverwhenthecheckboxis selected.Ifthecheckboxisnotselected,thennovalueforthecorrespondingvariableissent tothewebserver.ThisisproblematicforRabbitWeb.Itcannotdistinguishbetweenavariable thathasnotbeensentbecauseitisnotselected,andavariablethathasnotbeensentbecause itisnotcontainedintheform.Hence,ifauserdeselectsacheckbox,thentheassociatedvariableisnotchangedbyRabbitWebsimplybecauseitdoesnotgetanupdateforthatvariable. Thefollowingcodeimplementsaworkaround: ” value=”1” CHECKED >
The“hidden”INPUTtagabovecreatesavariablethatisalwayssenttothewebserver.Hence, thewebserverwillalwaysreceiveavalueof“0”foreachof$schedule[$B][$A].However,if thecheckboxfor$schedule[$B][$A]isselected,thenitwillalsoreceiveavalueof“1”.The webserverwillacceptthelastvaluereceivedasthedefinitivevalue.Therefore,ifthecheckboxisnotselected,thevariablewillbegiventhevalue“0”;ifitisselected,itwillbegiven thevalue“1”. Notethevarname()commandwithinthenamefieldtotheINPUTtagsabove.Arrayvariables,whengiveninnamefields,mustbemodifiedsothattheycanbesentintheupdateto thewebserver.Thevarname()commandencodesthevariablenamesothatitcanbesent. varname()canbeusedforallvariables,notjustarrayvariables;however,itisnecessaryfor arrayvariables. Theremainderofsprinkler.zhtmlissimilartomaterialthatwehavealreadycovered. 9.13.7ProjectScreenshots Figure9.18Ashowsthestatusscreenthatpresentssprinklerandsensorstatustotheuser.We simulatedrainbyputtingsomedrops ofwaterontherainsensor,andturned onthe“rainyday”rule.Therainsensor, withthethresholdssetat0.650to0.675 volts,workedasexpected. Beforeconnectingthesprinklercontrollertorealsprinklers,weusedprintfsin thecodetosimulatesprinkleroperation. Figure9.18Bshowstheoutputonthe stdioscreenthatcorrespondstosprinklersettingsfromFigure9.18C.
Figure9.18A:Sprinklerstatuspage. 403
Chapter9
Figure9.18B:Statusoutputonthe stdiowindow.
Figure9.18C:Webinterfacefor thesprinklersystem.
Figure9.18Bshowsthatthesimulationstartedwith moistureontherainsensor,andthe“rainyday” rulemadethesprinklersrunforfiveminutes.Asthe moistureonthesensordriedupinthenextninety minutes,althoughthe“rainyday”rulewasstillin affect,thesprinklersrevertedtothestandardwateringintervaloftenminutes,asdefinedintheproject specification. Figure9.18Cshowsthewebinterfaceforthe sprinklersystem.Itdoesnotlooktoodifferentfrom Figure9.17,andprovideslinksforsettingtheclock anddisplayingsensorstatus. 9.13.8FinalThought Wehavesuccessfullymettherequirementsforour web-enabledsprinklercontroller.Nowweconsider otherrequirementsthatmightbeinteresting. Whataretheconsequencesofsomeonehackinginto thesprinklercontroller?Wouldapranksterbesuccessfulinkillingthelawnbyforcingthesprinkler controllertooverorunder-waterwhilethehomeownerisonvacation? Ifthehomenetworkuponwhichthesprinkler controllerresidesisnotsuitablyprotecteditis conceivableamiscreantcouldmaliciouslyalterthe 404
Networking wateringtimes.Thiscouldresultinabrownordeadlawnifthehomeownerisinattentiveor absent.Itcouldalsoresultinahugewasteofwater. Toavoidthissituation,wemightconsidersomeofthefollowingapproaches.
SecureSocketLayer(SSL)-basedauthenticationmightberequiredofauserbefore theusercouldmodifythesystemsettings. Additionalsafeguardssuchashard-codingaminimumandmaximumallowable wateringintervalforeachvalve. Wecouldenforcethateachsprinklerisnotturnedonformorethanatotalofone hourperday. Wecouldaddseparatelayersofauthenticationtotheinterface,toonlyallowcertain userstomodifythesetup,whileotheruserscanonlyviewthesetup.
Anytimesomeonetalksaboutsecurity,anoft-overlookedelementissimplythatofphysical security.Allofthevalvesforthesprinklersystemaresimplysittinginafrontyard.Anindividualintentondoingharm,needonlywalkuptotheexposedvalvesanddisconnectafew wires,unscrewthesolenoids,ormanuallyturnonthevalves.Ifthehomeownerisonvacation,thesameharmisdoneas“hackinginto”thesprinklersystem. Inacaselikethis,theextraeffortoflockingthe“electronic”doorisprobablynotworththe engineering. Anotherissuetoconsideristhatofreliability.Whatdoesitmeanforthesystemtobereliable?Inahomeappliance,itmeansthesystemwilldowhatitistoldandwhenitistold.In thiscase,itmeansthataslongasthesystemhaspower,thecontrollerwillaccuratelykeep trackoftimeandexecutethewateringinstructionsgiventoit. Toimprovereliability,wecouldimprovethepackagingsothedeviceisn’teasilydamaged. WealreadyhaveasmallbatteryontheRCM3400developmentboardthatwillbackupthe RTC,butthewateringscheduleisonlystoredinvolatileRAM.Wecouldensureacopyof thecurrentwateringscheduleisstoredintheFLASH.Uponre-bootafterapoweroutage,the systemcouldthenreloadthemostrecentwateringschedule. Often,peopleliketotalkabout“availability”whendiscussingweb-basedsystems.Generallythistermisappliedtomean“accessibleandoperable”.Thiscanbetiedtoreliability,but oftenmarketersandengineersdrawadistinction. Considerafirewall.Afirewallmayhaveexcellentavailability,butmayhavepoorreliability. Forexample,afirewallmaynevercrash(beinghighlyavailable),butitmaynotstopunauthorizedaccesstoanetwork(notbereliable). Inthecaseofasimplehomeappliance,theenduserwillsee“availability”and“reliability” togetherinto“its-a-working-or-not-ability,”thusrenderingthedistinctionbetweenavailabilityandreliabilitymute.Theenduserwillonlycarethatthedeviceappearstobothwaterthe lawnonqueue,andbeavailableforconfigurationviabrowseratanytime. Safetyisanotherissuetoconsider.Wehavenotfusedanything.Ataminimum,theAC transformershouldbefused.Thiswouldprotectthesystemfromanaccidentalfireshouldthe wiresgoingtoasolenoidshort.Thisisanimperativeinaresidentialsetting. 405
Chapter9 Clearly,thisprojecthasalongroadtobecomingacommercialproduct.However,itdoes illustratethepowerfulfeaturesoftheRabbithardwareandDynamicC’sRabbitWebtools. TheentiresysteminvolvedverylittlehardwaretointerfacetothesensorsandACsolenoids. Thesoftwareissmallandeasytounderstandandmodify. Asfarastheauthorsknow,thereisnoothercombinationofhardwareandsoftwareonthe marketotherthanRabbitandDynamicCwithRabbitWebthatwillallowaprojectofthis scopetobeassembledasquickly,simplyorcompactly.
9.14SomeUseful(andFree!)NetworkingUtilities Weshouldnottakecodelibrariesatfacevalue—thingsdonotsometimesworkasplanned, and,inthenetworkingworld,itisimportanttobeabletoindependentlyobservenetworktrafficwhiledebugging.Thefollowingtoolswillbehandyindebuggingapplications. 9.14.1Ping Thisusefulutilityisfoundonalmostalloperatingsystems,anditssimplicityandubiquity makeitapopularapplication.ThekeyfunctionofPingistoquicklyestablishwhethera deviceonthenetworkisreachable.Moreover,becausePingalsoreportsthelengthoftime betweentheoutgoingrequestandtheresponse,itcanhelpusdeterminenetworkconditions. CuriousprogrammersshouldgoonestepfurtherandexplorehowPingusesICMPmessages toperformitsfunction. Figure9.19AshowshowweusedPinginaWindowsXPcommandwindowtoreachthe defaultgatewayinFigure9.3. C:\>ping192.168.1.1 Pinging192.168.1.1with32bytesofdata: Replyfrom192.168.1.1:bytes=32time=1msTTL=150 Replyfrom192.168.1.1:bytes=32time>12;Uppernibblestartboundary (SEGSIZE),a ;SetSEGSIZE a,(sgatTABLE>>12)–(sgstTABLE>>12) (DATASEG),a ;SetDATASEG
Forthisdiscussion,letusassumesegmentTABLEwillbelinkedatlogicaladdressC000hand physicaladdress70000h.Thethirdinstructiontakestheuppernibbleofthelogicalstartof segmentTABLEandaddsthattothelowernibbleofSTACKSEG.sgstgetsthelogicalstartofa segmentwhilesgatgetsthephysicalstartaddress.ThisORs0ChintoSEGSIZE. 414
Softools—TheThirdPartyTool Thefifthinstructionisonestepmorecomplicated.SincetheRabbitMMUaddsthelogical nibbletothefinaladdress,itmustberemovedfromtheMMUDATASEGregister. Thefirstpartisthephysicaladdressof“TABLEshiftright12,”whichis70h.Thesecondpart isthe0Chusedabove.Thisloads64h(70h–0Ch)intoDATASEG.Iftheprogramloadsaword fromTABLE+1230h,whichis0D123h,the0DhselectsDATASEG,whichaddsDATASEG,64h, to0Dh,getting71h,forafinalruntimeaddressof71123h. Onemightarguethatwecouldsimplyloadthesevaluesimmediatelyintheinstructionswith constants.Theadvantagehereisthat,atlinktime,wecanmovesegmentTABLEtoanylogical address(e.g.,9000hor5000h)andanyphysicaladdressandthecodeaboveworkswithout beingchangedorassembled. Thelinker’sjobistoresolvesymbolsandlocatesegments.Weshouldbeabletodosowithoutchangingthesourcecode. Unfortunately,mostdevelopmenttoolscan’tdocomplexexpressionevaluationatlinktime, andrequiresourcecodechangeswhenchangingthememorylayoutoftheprogram. 10.2.6SAMRabbitMacros Microsoft’sM808080/Z80macroassemblerfromthemid1980’ssetade-factostandard formacrosupport.Yes,evenMicrosoftoncehaditsfingersintheZ80and8-bittoolmarket. Softools’SASMRabbitiscompatiblewithM80anditsmacrosyntax. Macroscanbeusedformanytedioustasks.Theycanalsobeusedforcleverthings,andto takeittotheextreme,evenforthingslikecalculatingchecksumsofprogramstringstodetect programtampering. Here’sapracticalexamplethatcreatesa256-bytelookuptablewiththeparityforthebyte wherethebytewouldbeusedastheindexintothetable.Ifthebytetakenfromthetableis zero,thenthebytehasevenparity,andconverselyisoneforoddparitybytes.Thishasadirect applicationontheRabbit3000asparityfortheserialportsmustbecalculatedinsoftware. Program10.2showsamacrotogeneratetheparitytableandthefastestcodetogenerateparityforabyteinregisterA: Program10.2:Generatingaparitytable. genParityTable .macro byte = 0 .rept 256 par= 0 mask = 1 .rept .if par= .endif mask
8 byte&mask par+1 =
mask