a real-time operating system or rtos is very much like any other operating system with a few important differences over the next few videos i'd like to talk about those differences and how to get started using free rtos we'll go over tasks threads prioritization memory management and inter-task communication let's get started [Music] an operating system is a piece of software that runs on a computer or microcontroller that accomplishes a number of important functions first it is in charge of scheduling background tasks and user applications in most operating systems dozens of background processes are executing at the same time and you probably have several applications open on your phone or computer right now the operating system figures out how to give slices of time to each of these processes so that everything appears to be happening concurrently second it manages a number of virtual resources like files libraries and folders allowing applications and processes to access them when needed third they can manage or provide device drivers for your system these drivers allow the system to read and write from an external disk respond to keyboard and mouse input or draw graphics on your monitor you're probably familiar with general purpose operating systems like windows mac os and linux ios and android also fall into this category most general purpose operating systems are designed with human interaction as the most important feature so the scheduler is designed to prioritize such tasks that may mean some timing deadlines can be missed or pushed back and a little lag in responsiveness especially if it's not really noticed by a human is acceptable additionally the scheduler is often non-deterministic which means we can't know exactly which task will execute when and for how long if you're making something that requires an os and strict timing deadlines like a medical device or engine controller missing a deadline to say fire a spark plug would be catastrophic this is where a real-time operating system or rtos can save the day most r tosses offer many of the same functions as general purpose operating systems but they are designed so that the scheduler can guarantee meeting timing deadlines on the tasks while some r-tosses might provide high-level device drivers you'll usually see things intended for microcontrollers like wi-fi and bluetooth stacks or simple lcd drivers you can also find bare bones r tosses that provide just a scheduler with these you'll need to import or write your own file systems and device drivers note that while most r tosses provide methods for meeting timing deadlines that's not the only reason to use an rtos in your project the ability to run multiple tasks concurrently can be a lifesaver as we'll see in a moment if you've written programs for microcontrollers including arduino you're probably familiar with this kind of structure when your program starts it performs some setup functions and then it executes your tasks in a round robin fashion inside a while forever loop these tasks might read from sensors perform some calculations based on those readings and then say display something on a gorgeous led display this type of simple programming architecture is known as a super loop it's incredibly easy to implement has very little overhead and is great when you only need to accomplish a handful of tasks on your microcontroller in fact there's nothing wrong with the super loop for the vast majority of simple microcontroller projects you're usually better off with this type of program flow as it saves on cpu cycles and memory and it's much easier to debug than using an rtos you might even have created a couple of interrupts to break the flow of execution to handle an external event like a button push or perform some action at specific timed intervals if you need to meet strict timing deadlines for one or two tasks you might be better off using just interrupt service routines to accomplish those tasks with precise timing in my experience if you need to meet a timing deadline of less than about one millisecond your best bet is an interrupt anyway if you're trying to do something with an interval of less than a few hundred nanoseconds you either need an incredibly fast processor or it's time to start looking at custom hardware like an fpga now the unfortunate thing about this super loop architecture is that you cannot execute tasks concurrently if task 2 starts taking a long time and task 3 is your update display task you'll start to experience some lag similarly if you're polling for user input like from a serial terminal or reading a sensor you might miss data if other tasks take too long to fix that we can use the concept of running multiple tasks at the same time on a multi-core processor this is actually physically possible however since many microcontrollers only have one core the cpu time needs to be split up among the tasks we can also give higher priority to some tasks so that they happen sooner or take more cpu time for example if we prioritize the user input task users will likely experience less lag but it might mean background tasks take longer to execute we'll talk more about time slicing and prioritization in a future video note the terminology used here a task is any piece of work we want to get done in code a thread is a unit of cpu utilization with its own program counter and set of memory a process is an instance of a computer program that's being executed usually a process will have one or more threads used to accomplish tasks threads within a process will often share resources like heap memory and can pass resources to each other you'll often find that an rtos is capable of handling only one process but a general purpose os can run many processes note that in free rtos the term task is used to mean something closer to a thread and you'll often see these terms used interchangeably because of this i will use the term task when referring to units of cpu utilization so that it makes sense within the free rtos ecosystem interrupts still work within an rtos so long as they have a higher priority than any of the tasks they will stop code execution for all tasks run your interrupt service routine and return execution to where it left off you can have more than one interrupt but we won't get into handling nested interrupts which can get nasty if you're writing code for a simple 8 or 16-bit microcontroller with 2 kilobytes of ram you're probably best sticking with the super loop approach and using the inexpensive controller to do only a handful of tasks while you can get a simple scheduler to run on such a device the memory and cpu overhead of switching tasks would not leave you with many resources left for your actual program people have ported free rtos to the arduino uno it's definitely a good way to tinker around with it but i don't know how ultimately useful it is as you move into more and more powerful microcontrollers it becomes much more viable to run in rtos as you've got clock cycles and memory to spare for things like a scheduler while you can still run a bare metal super loop on something like an esp32 i usually find that the whole reason i bought something with that much computing power is to run several concurrent tasks which brings me to the ultimate question of why would you want an rtos there are a number of reasons but the best one that comes to mind is for when you need to do several things concurrently an esp32 is capable of handling user input reading and writing to an sd card controlling hardware and crunching numbers all at the same time the big one for something like this is the wireless stack these libraries require large amounts of ram and processing power they also need to respond to events from the network in a short amount of time which means using an rtos can be a massive help in this case it can help you divide up these different features into individual tasks an rtos can be great if you're part of a team working on a larger project you can divide up the tasks among the team members knowing that each one will run concurrently there will be some overhead and debugging to make sure everything works together though over the next few videos i'll go over important rtos concepts like prioritization and resource management we'll practice these concepts by implementing small demo projects in free rtos on the esp32 the reason i chose freertos for this series is because it's currently the most popular real-time operating system for iot devices as shown by this 2018 survey from the eclipse foundation while linux still reigns supreme for most of these devices it and windows are general purpose operating systems with the exception of rt linux you can see that the bare metal super loop architecture is still quite popular and an acceptable solution to many problems free rtos is also free and open source making it easy for you to try out note that as of 2017 amazon has taken over maintenance of the freertos project i also recommend keeping an eye on the zephyr project as it's a relative newcomer to the field backed by the linux foundation the esp32 is a powerful iot microcontroller that's packed with features also you can find inexpensive esp32 development boards any of them should work for this series as long as they have an associated arduino package i know some of you will balk at the idea of using arduino for our coding but hear me out most embedded programmers and electronics tinkerers will have some experience with arduino and if not it's easy to learn because of that it creates a level playing field for anyone wanting to try out rtos concepts i don't need to teach a vendor tool or chip specific libraries on top of trying to teach free rtos because the esp32 runs a modified version of free rtos out of the box there is hardly any setup involved in creating tasks in arduino almost every code example you see including task management semaphores mutexes and so on can easily be ported to your own build system i'll try to point out where the esp rtos differs from vanilla free rtos an rtos may not be the right answer to every firmware problem you run across but sometimes it is the right tool for the job especially when you need to do things like run concurrent tasks and meet timing deadlines at the end of most of these videos i'll issue you a challenge and i encourage you to try it out on your own esp32 or whatever device you're running free rtos on i'll then post a link to my solution so that you can see how your solution compares in the next video i'll show you how to get started with free rtos and create your own task to blink an led [Music] you