0:09
everyone welcome to controllers Tech
0:13
today in this video I am going to
0:15
introduce you guys to the concept of
0:19
R A realtime operating system is a very
0:23
important part of embedded
0:25
systems the main idea behind our to is
0:29
to perform multiple tasks at the same
0:32
time today I will show you the benefit
0:35
of using an OS how to create tasks in
0:39
freeer and how to handle
0:42
priorities I am using stm32 Cube ID and
0:46
I will start by creating a project in
1:18
I am choosing version one because it is
1:20
supported by majority of the
1:23
microcontrollers in the setting leave
1:25
everything as it is make sure that the
1:30
enabled we will use preemption based
1:33
scheduling in which a higher priority
1:36
task can take control from a lower one
1:39
you can Google about it go to tasks and
1:42
cues Tab and here you will see a
1:46
task here you will see some details
1:49
about it its name priority level stack
1:55
function I will explain all these along
2:00
I will create one task here and another
2:05
itself I am going to name it as task 2
2:09
its priority will be normal stack size
2:15
128 and entry point will be task 2 in it
2:20
everything else unchanged and task is
2:23
created this is it for the free rtos
2:27
part the rest of the setup is as
2:41
usual I am also using you up to show the
2:44
importance of priority
2:53
levels let's set the clock for the
3:08
and everything is done click save so we
3:12
got a warning saying that we shouldn't
3:14
use CIS tick with the AR
3:21
toes so go to sis and change the time
3:33
I am also using these two pins as output
3:36
to demonstrate some stuff click save now
3:50
generate open the main. C
3:53
file first I will explain some r related
3:58
things OS thread ID stored the unique ID
4:03
of the thread all the operations that we
4:06
will do related to any thread will
4:10
ID next the entry points of the thread
4:15
defined these are basically the main
4:20
tasks you have to write your task
4:22
related code in it next inside the main
4:26
function the main task is created
4:30
first the task is defined and then it is
4:34
created o thread death takes the
4:39
arguments the first is the name of the
4:42
task then the entry point of the
4:45
task then the priority of the
4:49
task then the instance and the stack
4:53
size once the task is defined we need to
4:56
create it using OS thread create
5:00
and after successful creation the thread
5:03
ID is stored in the relevant variable
5:06
that we defined in the beginning at last
5:11
called after this point the scheduler
5:14
will overtake and the control does not
5:16
reach beside this line I will
5:19
demonstrate that in a while if you
5:22
scroll down you will see that the entry
5:25
points are defined here like I said we
5:28
have to write our code in these
5:31
functions I will do that in few
5:34
minutes but let me start by writing the
5:36
code inside the while loop so here I
5:41
will try to toggle PA a0 and pa1 after
6:21
application we don't need to modify
6:23
anything just choose okay
6:41
fine let's run it and see so as expected
6:46
there is no movement on the
6:48
oscilloscope like I mentioned once the
6:51
kernel starts the shedul takes over and
6:54
the control doesn't reach the while
6:56
loop so I will comment out the line here
7:02
start let's run the program
7:27
again yeah so we have a wave with time
7:33
milliseconds actually what I wanted to
7:35
do is to generate a wave of 1
7:38
millisecond on PA a0 pin and another
7:42
wave with the period of 2 milliseconds
7:44
on pa1 pin but obviously it's not going
7:48
to happen by simply writing in a while
7:51
loop so here comes the RTO
8:05
in the default task I am going to toggle
8:07
the pin pa0 with a period of 1
8:11
millisecond and in the task two toggle
8:15
the pin pa1 with a period of 2
8:26
milliseconds at this point scheduler
8:29
want and it's going to schedule the
8:32
entire process in a way that every task
8:35
gets the time they the defined for let's
9:05
right now the pin is connected to pa0
9:09
and you can see that the period is 1
9:12
millisecond now I am switching the PIN
9:14
to pa1 and the period changes to 2
9:18
milliseconds so here both the tasks even
9:22
with a delay as small as 1 millisecond
9:25
can run simultaneously
9:43
this is the benefit of using our T over
9:47
programming but obviously this is not it
9:52
let's see the basic most types of
9:54
problems that we face with the
9:57
RT let me explain you the role of
10:00
priorities now first I will Define a
10:04
function to send some data to the
10:06
uart this function is supposed to run
10:23
task I will Define another function to
10:26
send data to you art and this one will
10:28
run into task two before proceeding any
10:32
further I will create another task here
10:36
to do so first we have to create a
10:40
task then we have to create an entry
10:44
task I will name it as task three in it
10:49
next inside the main function we have to
10:53
first so OS thread def is used and the
10:57
parameters will be the name of the task
11:00
the entry point of the task the priority
11:03
instance and at last the stack
11:06
size then we will create the thread and
11:09
assign the ID to the task 3
11:16
handle and at last write some code in
11:20
the entry function of the task make sure
11:23
that you write everything in a while
11:25
loop these tasks are not defined to
11:28
handle return so you should always have
11:30
everything inside an infinite
11:33
Loop just like the other two I am also
11:36
going to write one you out function for
11:43
task it's time to write our code now so
11:48
the default task is going to send the
11:50
data to the uart every 1
11:52
second and task two is going to send the
11:57
seconds let's compile this and see how
12:23
situation as you can see that each task
12:26
is printing every second but this is not
12:29
what we opted for we wanted the default
12:32
task to print every second and tasked
12:35
two to print every 2
12:48
seconds let's test this between the
12:51
default task and the task three which
12:53
runs every 3 seconds
13:17
okay as you can see the default task
13:20
prints twice and then in the third
13:22
second both default and task three
13:25
prints together here the code works all
13:28
right because the delay is
13:41
large but what if the time delay is same
13:45
tasks so here I will use all three tasks
13:49
to transmit with the same delay of 1
13:57
second let's compile the code and debug
14:06
it as you can see even though the data
14:10
is being transmitted it's not what we
14:14
for every task Waits 3 seconds to
14:17
transmit again but we wanted all of them
14:22
second this happens because they all are
14:26
art to avoid these situ situations with
14:30
the shared resources we will use the
14:39
priorities currently they all have the
14:43
priorities I will change the priority of
14:45
task two to the above normal and the
14:49
task one to below normal so finally we
14:53
have three tasks all with different
14:55
priorities and sharing the same resource
14:59
let's see what happens
15:23
now as you can see now that all three of
15:26
them are printing at the same time and
15:30
second this is exactly what we were
15:32
looking for also note that tasked two
15:36
prints first then default task and at
15:39
last task three the reason behind this
15:42
is the task 2 priority is highest as it
15:46
is above normal then default task which
15:48
is normal and at last task three which
15:51
is below normal so they are executed in
15:55
the same order as their
15:57
priorities this is it
15:59
guys I hope you understood some basic
16:02
concepts here this was just the
16:05
introduction about what our to is on how
16:08
to handle some basic function in it I
16:12
recommend that you read about our
16:15
to we will continue this series of
16:18
tutorials time to time you can download
16:22
the code from the link in the