Getting Started With STM32 and Nucleo Part 3: FreeRTOS - How To Run Multiple Threads w/ CMSIS-RTOS

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
[Applause] [Music] an operating system or OS is a piece of software that manages other software and hardware on a computer a real-time operating system often abbreviated artis is the type of operating system intended to meet hard deadlines you'll often want to use an operating system when you want to run more than one piece of software on a system at a time let's say we're developing an operating system for a phone like Android or iOS and let's say a user wants to stream a movie so we might break down that streaming experience into two functions downloading chunks of the movie from the internet which we'll call job one and displaying each chunk to the user which we'll call job two these jobs can be individual programs or concurrently running pieces in one program known as threads let's also assume that our smartphone here has only one processor core available as a result the processor will need to jump between job one and job two fairly quickly to give the user a seamless experience while watching a movie I've listed a third job here which is the underlying software in the operating system whose job is to swap between these programs or threads this is known as context switching and it can sometimes be time-consuming as the operating system needs to save things like the current register states memory locations and the program counter note that the focus for most general purpose operating systems like our phone example here is on user experience if job one drops a packet or job to needs to skip a frame to catch up things might stutter slightly but most users shouldn't notice or care too much let's see how this differs from a real-time operating system modern automobiles are packed with microcontrollers running specialized operating systems to manage functions throughout the vehicle these microcontrollers are often packaged in a protective case along with other electronics in what's known as an electronic control unit or ECU now let's say you're working on the ECU that controls the brakes for a new car with automatic emergency braking under normal conditions you might simply be pulling for pressure applied to the brake pedal to assist the driver with the braking system and turning on the taillights however let's say you get notification that one of the distance sensors in the car detects an impending crash a new high priority job preempts the regular job and begins to automatically apply the brakes in this situation time is critical frames can't be dropped or you risk injuring humans as a result we need to use a real-time operating system to ensure that strict timing deadlines can be met by our high priority jobs note that we lose some computing time to overhead incurred by the scheduler which is the piece of the operating system that determines which job should run next once the high priority job is done the processor can return to running the lower priority job we normally see real-time operating systems on microcontrollers whereas we usually see general purpose operating systems on micro processors we can generalize the type of processor and operating system we might need based on these factors if you don't need a lot of speed and only need to run one job a simple eight or 16-bit microcontroller is often good enough because these normally don't run any type of operating system you'll often see writing code for these referred to as bare metal programming while you can do bare metal programming for the beefier 32-bit microcontrollers like our stm32 line you'll likely run into situations where you want to run multiple jobs at the same time and assign priorities to them in that case you'll want a real-time operating system when you enter into the microprocessor world use your experience is often the priority as you need to worry about an interface and several layers of memory management like registers RAM and a hard drive here a general purpose operating system is appropriate such as Linux Windows Mac OS Android and iOS in addition to cars real-time operating systems can be found in Mars rovers pacemakers and lots of IOT devices if you need to meet critical timing deadlines want to run multiple tasks and enjoy having some code portability and artists can help the idea of running multiple jobs at the same time on one processor core is known as concurrent programming if you've done concurrent programming before on a computer you might be familiar with the concept two threads which are just individual pieces of a program that run independently of each other in my previous examples what I call jobs might just be threads in one program we are going to look at free artists which is a very popular open-source real-time operating system in 2017 Amazon took over the free artists project and now distributes it under the very permissive MIT license stm32 cube IDE comes with free artists bundled in its libraries which makes using it quite easy on the left side of the site you can click free artists and get a link to its API here we can find the functions we need to create and manage tasks tasks are what free artists calls its jobs something analogous to threads the good news is that if we use the cube MX software to set up free artists for us we won't need to worry much about these specific functions however this reference is here if you need to know underlying free artists stuff for our purposes we'll want to use the CM sis artists library you might remember this diagram from the first episode where we talked about using stm32 s hal library this time we're going to be using an artists specifically we're going to use free artists as our real-time operating system kernel with this framework we'll want to make calls to the CM sis artists API which acts as an abstraction layer to free artists CM sis artists is maintained by arm and we'll be using version 2 on the side under reference and CM sis our toast API v2 you can see the functions we have available to us will mostly be relying on these functions to control free artists notice the terminology difference CM says artist refers to the individual jobs as threads whereas free artists called them tasks that can help us know if we're dealing with the CMC artists abstraction layer or the underlying free artists in stm32 cube IDE start a new stm32 project once again I'll be using the new Clio L 4 7 6 RG development board so I'll give the project a name with something like free artist blinky will want to use two different tasks to control the onboard led which has already been configured for us on port a pin five in the categories tab go to middleware and click on free artists change the interface from disabled to see emesis artists v2 in the configuration pane we can see that most of the parameters are defined for us these settings should be good enough for an initial test program the important thing here is that preemption is enabled this allows the scheduler to stop tasks from running in order to run another task of higher priority go to the tasks and queues tab you can see that we already have one task and defined for us let's change its name to blink 0-1 and change the entry function to start blink 0-1 an entry function is just a function that gets called in order to start a task or thread notice that the priority is set to OS normal priority this is the default priority we can assign tasks to be a higher or lower priority than this but we'll leave this one a default for now we could have just one task but that's no different than a single forever loop so add another task and call it blink zero to change the entry function to start blink zero to click on the priority drop-down list to see the available priorities we can assign to this task higher priorities tell the scheduler that this task should take priority however we'll set this task to just below normal so that our first task takes priority save the file and we get a warning about changing the time base Cystic is a special timer in most arm cortex-m processors that's usually reserved for calculating time slices in an artist by default Cystic triggers and interrupts every one millisecond since we're using a fixed priority artists with pre emption tasks with higher priority will be executed first if several tasks have the same priority they will be executed in a round robin type fashion on each systick interrupt the scheduler will stop the current task to execute the next task with the same or higher priority the problem is that the stm32 Hal API uses the Cystic timer to determine how much time has passed since restart this is useful for things like the functions hell delay and Hal get tick how requires this timer to at a very high priority in order to keep somewhat accurate time at least without a real-time clock free artists on the other hand requires that the time base for its scheduler is of a much lower priority one easy fix for this is to use a different timer for hell and leave Cystic for free artists so we go into system core sis and find where the stm32 hel selects the timer for its time base timers 6 & 7 are often extremely basic timers with little configuration parameters so they're best used for basic counting just like we need for the hal time base so I'll go with timer 6 save again and cube em X should generate code without any warnings this time you can see that a middlewares folder and free artists files were added to our project open up main see at the top you can see that cube MX automatically declared the two tasks entry functions for us in main you can see that some oh s kernel functions were called which are part of CMC artists farther down you can see how threads are defined we give each thread a name priority and stack size this struct is then passed into the OS new thread function along with the name of our entry function to that thread such as start blink 0 1 finally OS kernel start is called which begins running the scheduler and calls the entry functions to our threads at this point the artis has control of the system and we should never exit from this function as a result you don't want to write any code passed it including in the wild one loop scroll down to find the templates for our thread entry functions each thread should have a place to run some setup code and a forever loop in the forever loop we're simply going to toggle our led just like we did in the first episode for the first thread I'm going to toggle the led every 500 milliseconds notice that I'm using OS delay here which is a CMS SR toss function instead of the usual Hal delay OS delay allows the scheduler to run other tasks while we wait whereas Hal delay might not ideally we should never leave this forever loop unless the thread is terminated however it's usually a good idea to add a call to OS thread terminate tell the scheduler to gracefully terminate and clean up this particular thread in case something happens like you accidentally call break inside the forever loop let's repeat this same process in the start blink zero to function note that this thread has its own separate forever loop to us users it will appear as if the two threads are running at the same time if there's any question as to which should run first blink zero one will take priority since we well set its priority higher the difference here is that I'm going to change OS delay to 600 milliseconds this should create an interesting effect as the two threads compete to toggle the same LED at different times once again we tell the thread to terminate itself gracefully if it ever leaves the forever loop let's build the project and start the debugger inside the debugger press the resume button the ld2 LED on your nucleo board should begin blinking and changing its duty cycle in distinct steps thanks to the two threads toggling it at different times this is just the beginning of running multiple threads on a microcontroller how to share resources and synchronize these threads through things like queues mutexes and semaphores would take an entirely separate video series but for now have fun hacking on your stm32 with free artists and please subscribe if you'd like to keep up with more videos like this [Music] [Music]
Info
Channel: DigiKey
Views: 139,924
Rating: undefined out of 5
Keywords: ST-LINK, Digi-Key, Shawn Hymel, FreeRTOS, CMSIS-RTOS Interface
Id: OPrcpbKNSjU
Channel Id: undefined
Length: 12min 52sec (772 seconds)
Published: Fri Sep 20 2019
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.