0:07
hello everyone welcome to controllers
0:11
tech today in this video I am going to
0:15
show you how to use Q in free RT OS we
0:20
will cover the entire topic in two to
0:22
three videos today I will start with the
0:26
simple Q let's say we have a Q to hold
0:30
five integers task a is a sender task
0:34
and task please receive a task task
0:38
ascends an integer to the queue as the
0:41
queue is empty the data will acquire the
0:44
first place when task ascends another
0:47
data and it will occupy the second place
0:50
if the receiver task B reads the data
0:53
from the queue it will read from the
0:55
head of the queue once the data is read
0:59
it will be removed from the queue and
1:01
all the contents in the queue will shift
1:03
right this is how the queue works at the
1:09
let's start by creating a project in
1:16
I am using stm32f4 for 6r II give some
1:23
name to the project and click finish
1:42
I am selecting external clock for the
1:44
MCU select a time-based source
1:48
anything other than Cystic Here I am
2:02
you are to is to communicate with the
2:13
enable the free RTOS version one leave
2:18
everything default we have one default
2:21
task created here let's go to the clock
2:33
setup now I have 8 megahertz crystal on
2:39
HSE means that we are taking input from
2:42
external crystal I want the MCU to run
2:46
at maximum clock the setup is done just
2:50
save it so that the code can be
3:09
I am NOT going to use the CM sis related
3:18
functions that's why we need to manually
3:20
include all the RTOS related files these
3:24
are already present inside the folder we
3:27
just need to include them in our code we
3:33
can remove the CM sis now other than
3:37
these I am including string dot H for
3:39
the string related operations let's
3:51
remove all the default task related
3:53
functions which were generated by
4:18
we have to define the task handlers
4:20
first there are going to be two center
4:24
tasks and one receiver task next we need
4:36
to define the queue handler I am naming
4:39
it's simple queue these are the task
4:42
functions where the task code will be
4:46
written inside the main function first
4:49
of all we need to create the q xq create
4:59
takes two parameters first is the length
5:03
of the queue this queue can hold up to
5:07
five elements and next is the size of
5:10
each element I want the queue to hold
5:13
the integer elements that's why integer
5:17
size if there is some error while the
5:20
creation of the queue it will return
5:22
zero and we will display this string on
5:55
if the queue was created successfully it
5:58
will return anything other than zero and
6:01
in that case we will display this string
6:15
next we need to create three tasks X
6:25
tasks create takes the following
6:27
parameters the task code that we define
6:31
here some name for the task stack size
6:39
the parameter that you want to pass to
6:42
the task priority of this task and last
6:47
is the handler for this task this is a
6:51
high priority task so I have given a
6:53
priority of 3 to it in the same way I am
6:57
going to create a low priority task with
6:59
the priority of 2 but this time I am
7:02
going to pass some parameter to this
7:04
task if you take a look at the
7:06
definition of X tasks create the
7:08
parameter is of type void pointer that's
7:12
what we need to pass into this task so I
7:16
am sending a void pointer to this number
7:19
next we will create a receiver task with
7:22
the least priority of 1
7:46
this is to receive serial data from the
7:49
computer in interrupt mode I still need
7:53
to define the RX data
8:02
and at last we are going to start the
8:21
here is the high-priority sender task
8:25
this number will be sent to the queue to
8:29
convert the milliseconds into the ticks
8:31
when the control enters HPT this string
8:35
will be sent via UART
8:46
send the data into the q xq send takes
8:50
the following parameters the first is
8:53
the cue handler next is the address of
8:57
the data that you want to send and last
9:00
is the ticks to wait before the timeout
9:03
I am sending this number to the queue
9:06
and I want the task to wait forever for
9:09
the space to become available in case
9:11
the queue is full if the data is sent
9:14
successfully it will return PD pass and
9:17
we will print this on the console
9:30
then the task will go into suspension
9:37
next is the lower priority sender task
9:41
remember that I passed a number as the
9:43
parameter while the task was created
9:56
to send will get the value from the
9:59
parameter we will store the value in the
10:08
to send variable next this string will
10:12
be printed on the serial console to
10:14
indicate that the control has entered
10:16
the LPT task next the number will be
10:24
sent to the queue if the queue is full
10:27
the task will wait forever for the space
10:30
to become available then the task will
10:42
go into the suspension next is the
10:53
function for the receiver task first I
10:57
have created a variable to save the data
11:14
we are going to send this string to the
11:16
console next we are going to read the
11:19
data from the q xq receive takes the
11:23
following parameters the handler to the
11:27
q the variable where the data must be
11:30
saved the number of ticks to wait for
11:33
the data to become available in case the
11:36
queue is empty if there is some error in
11:39
receiving data it will not return return
11:41
P D true and we can display this string
11:44
then or else the data received will be
11:48
sent to the console after this the
11:51
receiver task will go into the
11:53
suspension for five seconds let's build
12:07
there seems to be some error about s
12:10
print F I forgot to include the library
12:14
for that let's build the code and debug
12:41
I am using Hercules for the serial
13:08
I am letting it run for some time
13:21
let's pause it now so the integer Q was
13:32
created successfully now when the
13:35
scheduler runs the control will first go
13:38
to the high priority task
13:40
it's a sender task and it will send 222
13:45
to the Q you can see the current queue
13:48
status on the right after leaving high
13:51
priority tasks the control will enter
13:54
the low priority sender task I will also
13:57
send the number 111 to the Q and the
14:01
number will be copied as shown in the
14:02
picture next the receiver task will run
14:06
and it will read one data item from the
14:09
queue the data read is 222 as you can
14:13
see in the console also the items in the
14:17
queue will shift to the right and you
14:19
can see the updated Q on the right now
14:23
after 1 seconds LPT task will wake up
14:26
and sends 111 to the queue now after
14:32
both hbt and LPT will wake up but HPT
14:36
will run first and they both will send
14:38
the respective numbers to the queue
14:40
again after one second LPT wakes up and
14:44
right 111 to the queue now the queue is
14:48
full and when hbt wakes up it's not able
14:52
to write the data into the queue and so
14:54
it will be blocked until some space is
14:56
available in the queue same will happen
14:59
for the lower task now when the receiver
15:03
task runs it will read and remove one
15:05
item from the queue one space will be
15:09
available in the queue high priority
15:11
task will preempt the receiver task and
15:13
write the data into the queue and go
15:16
into suspension for two seconds control
15:19
will re-enter the receiver task and it
15:21
will execute rest of the task it will
15:24
print the number received from the queue
15:26
and go into suspension for five seconds
15:30
now the high priority task will run and
15:33
it will try to write the number into the
15:35
queue as the queue is full it will go
15:38
into the block state five seconds later
15:42
when the receiver task wakes up same
15:44
thing happens again and it will keep
15:47
happening next we will see how can we
15:55
send the data into the queue from the
15:57
interrupt service routine I am writing
16:05
the receive callback function for the
16:07
UART this is the number that I am going
16:14
to send to the queue if the data
16:17
received is our we must first define the
16:21
high priority tasks woken to false
16:28
I am sending data using XQ send to front
16:32
from ISR this will send the data to the
16:35
front of the queue the parameters of
16:38
this function are queue handler the data
16:41
to be sent and the high priority tasks
16:43
woken note that there is no waiting
16:47
period if the queue is full this
16:50
function will simply timeout and at last
16:58
we must call the port end switching is
17:01
our if the high priority task woken was
17:04
set true this will do the context
17:07
switching looks like there is some
17:30
we have to pass the address of this
17:33
variable here all is good now let's
17:48
I have sent the character our and we can
17:56
see the string also printed here let's
18:02
see what happened here in each step
18:05
integer Q was created high priority task
18:09
runs and write the number into the queue
18:13
LPT runs and write another number
18:16
receive a task reads and removes the
18:19
first number from the queue again LPT
18:23
runs and writes another number to the
18:25
queue if we'll keep going on until the
18:29
data are is received from the UART the
18:35
number one two three four five six seven
18:38
eight nine is sent to the front of the
18:40
queue and the rest of the items will
18:42
shift back when the control enters the
18:45
receiver task it will read this number
18:47
as it's the first number in the queue
18:50
after reading the number will be removed
18:53
from the queue and rest of the
18:55
processing of the queue will continue in
18:57
the usual order in this video we saw how
19:00
to create integer q and how to pass data
19:03
between the tasks using this queue in
19:07
the next video I will cover the point at
19:09
Q so that we can send the entire strings
19:13
in the queue we will also see how to use
19:16
the malloc function in our TOS that will
19:20
solve our problem with the s printf and
19:22
the RTOS you can download the code from
19:27
the link in the description leave
19:30
comments in case of any doubt have a