CMSIS-RTOSTutorialIntroductionThistutorialisanexcerptfrom“TheDesignersGuidetotheCortex-MProcessorFamily”byTrevorMartinandisreproducedwithpermissionofElsevier.FormoredetailspleaseseetheFurtherReadingsectionattheendofthistutorial.InthistutorialwearegoingtolookatusingasmallfootprintRTOSrunningonaCortex-Mbasedmicrocontroller.SpecificallywearegoingtouseanRTOSthatmeetsthe‘CortexMicrocontrollerInterfaceStandard’(CMSIS)RTOSSpecification.ThisspecificationdefinesastandardRTOSAPIforusewithCortex-Mbasedmicrocontrollers.TheCMSIS-RTOSAPIprovidesuswithallthefeatureswewillneedtodevelopwithanRTOS,weonlyneedtolearnitonceandthencanuseitacrossaverywiderangeofdevices.CMSIS-RTOSalsoprovidesastandardinterfaceformorecomplexframeworks(JavaVirtualMachine,UML).Itisalsoastandardinterfaceforanyonewantingtodevelopreusablesoftwarecomponents.IfyouarenewtousinganRTOSittakesabitofpracticetogetusedtoworkingwithanRTOSbutonceyouhavemadetheleaptheadvantagesaresuchthatyouwillnotwanttoreturntowritingbaremetalcode.GettingStarted-InstallingthetoolsToruntheexamplesinthistutorial,itisfirstnecessarytoinstalltheMDK-ARMtoolchain.FirstdownloadtheMDK-CoreVersion5usingtheembeddedURLbelowandruntheinstallationfile.‘DeviceFamilyPack’.Thisisacollectionofsupportfilessuchasstartupcode,flashprogrammingalgorithmsanddebuggersupportthatallowyoutodevelopwithaspecificmicrocontrollerfamily.TheMDK-ARMtoolchainconsistsofaCoreInstallation(IDE,CompilerandDebugger)plusadditionalsoftwarepacksaddedthroughapackinstaller2CMSIS-RTOSTutorialIntheexerciseswearegoingtouseanSTM32F103RBsoweneedtoinstallsupportforthisdeviceusingthe‘PackInstaller’withintheµVisionIDE.WhentheMDK-Corefinishesinstallingthepackinstallerwillstartautomatically,alternativelyyoucanstarttheµVisionIDEandaccessPackInstallerfromthetoolbarbypressingtheiconshownbelowOncethepackinstallerisopenitwillconnecttocloudbasedpackdatabaseanddisplaytheavailabledevicepacks.SelecttheKeil::STM32F1xx_DFPandpresstheinstallbutton.ThiswilltakeafewminutestodownloadandinstalltheSTM32F1xxsupportfiles.IfthepackinstallerhasanyproblemsaccessingtheremotepackyoucandownloaditmanuallyusingtheURLbelow‘CMSIS_RTOS_Tutorial’.OncethepackhasstartedinstallingclicknextHereyoumustacceptthelicenseandagainclicknexttocontinuetheinstallation4CMSIS-RTOSTutorialWhatHardwaredoIneed?Simpleanswer:none!TheKeiltoolchaincontainssimulatorsforeachoftheCortex-Mprocessors.Italsocontainsfullsimulationmodels(CPU+Peripherals)forsomeoftheearlierCortex-Mmicrocontrollers.ThismeanswecanruntheexamplesinthedebuggerusingthesimulationmodelsandexploreeveryaspectofusingtheRTOS.InfactthismethodofworkingisabetterwayoflearninghowtousetheRTOSthangoingstraighttoarealmicrocontroller.OverviewInthistutorialwewillfirstlookatsettingupanintroductoryRTOSprojectforaCortex-Mbasedmicrocontroller.Next,wewillgothrougheachoftheRTOSprimitivesandhowtheyinfluencethedesignofourapplicationcode.Finally,whenwehaveaclearunderstandingoftheRTOSfeatures,wewilltakeacloserlookattheRTOSconfigurationoptions.IfyouareusedtoprogrammingamicrocontrollerwithoutusinganRTOSi.e.baremetal,therearetwokeythingstounderstandasyouworkthroughthistutorial.InthefirstsectionwewillfocusoncreatingandmanagingThreads.Thekeyconcepthereistoconsiderthemrunningasparallelconcurrentobjects.Inthesecondsectionwewilllookathowtocommunicatebetweenthreads.Inthissectionthekeyconceptissynchronizationoftheconcurrentthreads.CMSIS-RTOSTutorialFirststepswithCMSIS-RTOSTheRTOSitselfconsistsofaschedulerwhichsupportsround-robin,pre-emptiveandco-operativemultitaskingofprogramthreads,aswellastimeandmemorymanagementservices.Inter-threadcommunicationissupportedbyadditionalRTOSobjects,includingsignaltriggering,semaphores,mutexandamailboxsystem.Aswewillsee,interrupthandlingcanalsobeaccomplishedbyprioritizedthreadswhicharescheduledbytheRTOSkernel.AccessingtheCMSIS-RTOSAPIToaccessanyoftheCMSIS-RTOSfeaturesinourapplicationcodeitisnecessarytoincludethefollowingheaderfile#includecmsis_os.hThisheaderfileismaintainedbyARMaspartoftheCMSIS-RTOSstandard.FortheCMSIS-RTOSKeilRTXthisisthedefaultAPI.OtherRTOSwillhavetheir