An Operating System on a $4 Board? | Getting Started with FreeRTOS on the Raspberry Pi Pico

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
what would you do if i asked you to write a program for your pi pico that flashed a red led every 300 milliseconds a different green led every 500 milliseconds also handle serial transmission on the pi pico z bus all at the same time when the user presses a button read a value off of the adc bus you may solve this by setting some comparative counter timers for the leds and handling the serial and button pushes via pin change interrupts but as your solution grows it becomes extremely monolithic and hard to modify adding or removing features causes you to have to rethink the entire design of your system a simpler solution and what we're going to talk about today would be to use an operating system specifically we'll be talking about free rtos an open source and free to use real-time operating system or rtos and how we can use it on the raspberry pi pico a real-time operating system is like a normal operating system in that it appears that it allows a system to execute multiple tasks at the same time in reality a cpu core can only execute one thread of execution at a time by using a regularly timed scheduler to quickly switch between threads of execution an operating system allows you to create multiple tasks that appear to happen all at once specific to a real-time operating system is the idea of task priority where one task may be more critical than another and as a result will preempt the execution of lower priority tasks the idea is that instead of evenly sharing the time slots of execution the programmer can give certain tasks higher priority of execution when competing for time and make the outcome of the program more predictable if we're using an rtos for the original scenario when the user presses a button that would be a higher priority task than the leds because the user's input must be processed and as a result the user pressing a button would preempt blinking the led we don't have to dive deep into the internals of free rtos or other rtos today that alone could take several videos to get through all you need to know is that free rtos gives you the ability to run multiple tasks at the same time on a microcontroller with that being said let's get the programming also before i forget i wanted to let everyone know that we started a discord for our channel the link is in the description go check the server out hang out with other members of the low-level gang and ask questions if you get stuck in these tutorials all right so here i have the blink led tutorial source code up in its current state this code makes the led on the rb 2040 blink pretty straight forward today we'll be changing this to include free rtos and have the rtos blank two separate leds the original pin 25 led and one on pin 2 at different intervals at the same time first we need to clone the source code repo of the free rtos kernel the kernel source code can be found at this url which is in the description box using rttosis is different than using normal operating systems where instead of being given an executable that runs as a kernel our tosses ship a source code where you compile in all of your tasks to create one large binary next we need to set up our cmic file to compile free rtos as a static library compiling it as a static library allows us to build it into our program again as one binary and eventually one uf2 file that we can put onto the raspberry pi pico we create a glob in cmake which is just a set of files specified by a wildcard so star.c also we include the porting code that ports free rtos to an arm cortex m0 processor which the rp2040 core is based off of finally we include the source code for heap four so our tosses normally come with several heap implementations based on memory or timing constraints or speed constraints heap 4 works for us in this case next we add a line saying that our project blink requires free rtos to be compiled in finally we add our include directories so the compiler knows where to look for our header files okay awesome now our sema file is complete let's write some c so the first change is that we need to include a few additional header files the first is freertos.h which brings in all the apis for the os itself like the scheduler and all those goodies next we will include free rtosconfig.h which is a header file to where we can tweak specific variables in our rtos deployment for example the minimum stack size preemptive versus cooperative scheduling and execution etc finally we include task.h which is a header file that includes exports for the free rtos task api next we want to take the code that blinks our green led and move it to its own separate function this function will be executed as a task meaning the rtos will schedule it against other tasks based on its priority okay now that the code lives in its own function and eventually its own task we need to tell the os to execute that function as a task to do this we will use the free rtos task create api a link to additional documentation for the freertos api can be found below in the description the function x task create takes a few arguments the first being the function to be called as a task the second being the name of the task as tracked by the os next is the size of the stack so if the function that we're calling or the task uses more memory than this number the os will throw an exception next is a pointer to the arguments for this function we don't have any arguments for the blink led example so there's no arguments here in this case so null next is the task priority so this being an led blink is a fairly low priority task and as a result we'll make it idle priority idle priority means that only execute this task if there is nothing else to do so if the system is at idle run this task and then finally we specify a pointer to a task handle this task handle will become a handle that points to this task's information block we can use this handle in the future to stop the task delete it restart it etc another modification we have to make is we have to change sleep millisecond to the task delay v task delay is a free rtos api call that will allow other tasks to execute in the background while this test sleeps now that we have a task created we start the scheduler once the scheduler executes no code below it should execute the scheduler runs forever and executes the created tasks from the task list based on their stated priority and current running state okay so with that we should be able to c make our build system compile our program and upload it to the pico okay and as usual to make our projects we make a build directory with cmake into the previous directory and then we can make the uf2 file uh this takes a minute to compile but not too long you are compiling an entire operating system here okay once that completes you're able to copy the uf2 file onto the raspberry pi pico and then once that's on there you should see that your raspberry pi pico is in fact blinking you have one led that's blinking on and off as it's supposed to um cool so now the question is how do we make another led get involved from the process well modifying the code is actually fairly simple we literally just have to copy and paste the green led task code after we create a new pin so copy this code name it red led task and maybe we'll make it 10 times a second instead of once every second i did name it red led instead so we got to change that now we just copy the instantiation of a task call it something else and then make a new task handle and then make sure we set the pin to a gpio out which i failed to do um great so with that code you can build that and put it onto your raspberry pi pico um unfortunately in the creation of this video i actually bricked my raspberry pi pico uh i wired an led in reverse and then grounded it to the board and that actually fried the processor so in the future i will have a short that comes out that shows you guys the final product but in the meantime this should work for you um if you have success do me a favor drop a comment let me know how it goes if you guys like this video do me a favor hit like hit subscribe and i'll see you guys next time bye
Info
Channel: Low Level Learning
Views: 66,244
Rating: undefined out of 5
Keywords: rtos, raspberry pi, pico, rpi, microcontroller, arduino, maker, craft, hobby, electronics, wires, temperature, safety, project, board, electric, leds, led, thonny, python, micropython, os, ide, onewire, ds18b20, circuitpython, review, launch, measure, probe, rp2040, specs, specifications, how to, guide, programming, Pico emulation, retro games raspberry pi pico, etaprime, eta prime, raspberry pi pico, arm cortex m0+, low cost
Id: 5pUY7xVE2gU
Channel Id: undefined
Length: 7min 44sec (464 seconds)
Published: Sat Jun 05 2021
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.