0:08
hello everyone welcome to controllers
0:12
tech today in this video I am going to
0:16
walk you through the counting semaphores
0:18
in free rtos like I mentioned in the
0:22
post I am NOT going to use the CMC's API
0:25
and instead I will be using free RTOS
0:29
functions directly if you want to browse
0:32
more videos on our TOS you can check out
0:36
the playlist on the top right corner
0:38
let's start by creating the project in
0:41
stm32 cube ide i am using f 4 4 6 r e
0:54
give the name to the project and click
1:04
this is the cube MX and let's begin the
1:08
setup I am selecting the external
1:11
crystal for the clock let's enable the
1:17
free RT OS enable to use the counting
1:28
semaphore leave everything as it is a
1:38
default task will be created and we
1:42
can't remove it from here
1:46
next select the time-based source
1:49
anything other than systick
1:55
I am enabling you at 2:00 in interrupts
1:59
mode to communicate with the PC let's
2:05
set up the clock now I have 8 megahertz
2:09
crystal on the board and I want the
2:11
controller to run at maximum possible
2:28
click Save to generate the project
2:40
here is the main dot C file first let's
2:45
open the CMC say ciao I am going to
2:49
include all these directly to my project
2:52
although I don't need all of them I am
2:55
including just incase comment out the CM
2:59
sis file now we are not going to use any
3:02
of the CM sis function here
3:10
I need to include few more files that is
3:14
STD lip and string dot H you will see
3:19
their usage in a while I am removing the
3:28
default task related functions that were
3:55
let's start the process by defining the
3:58
task related functions first here HPT
4:02
handler will be the handler for the high
4:04
priority task and HPT task is the entry
4:07
function where the task code will be
4:09
written in the same way I am creating
4:18
three more handlers for the medium
4:20
priority task low priority task and very
4:23
low priority task next we need to create
4:31
a handler for the semaphore I am naming
4:34
it counting sem now I am creating a
4:37
resource which have three values and
4:40
some other variables that are going to
4:43
be used our X data is going to store the
4:47
value received from UART
5:04
first we need to create the semaphore X
5:08
semaphore create counting takes two
5:11
parameters first is the maximum number
5:13
of count you want and second is the
5:16
initial count value I am creating the
5:19
semaphore with three tokens because the
5:22
resource have three values and three
5:24
tasks can access it simultaneously if
5:27
the semaphore is created the handler
5:30
that is counting saime will have any
5:32
other value than null next we need to
5:44
create the tasks X tasks create takes
5:47
the following parameters first is the
5:51
task code or entry function the name
5:55
task parameter priority and the handlers
5:58
address you can give any name to these
6:02
tasks name is not used anywhere in the
6:05
program similarly I have created three
6:08
more tasks with different priorities
6:11
based on their names and finally we will
6:15
start the scheduler now we have to write
6:19
the tasks I will start with high
6:39
it's going to print this string when the
6:42
control enters the task now we will take
6:46
the semaphore X semaphore take have two
6:49
arguments first is the semaphore Handler
6:52
and waiting time port max delay means
6:56
it's going to wait forever for the
6:58
semaphore to become available if the
7:01
semaphore is acquired successfully these
7:03
statements will be executed i teo a is
7:08
the function to convert integer to the
7:10
string first parameters is the integer
7:14
value then the character array and then
7:16
the format type after this I am joining
7:20
all these strings and finally send it to
7:24
increment the index variable and added a
7:28
delay of three seconds
7:39
in the beginning of the task let's
7:42
release three semaphores
7:44
so that three tasks can acquire them
7:57
medium priority task is also going to be
8:00
same other than releasing semaphore one
8:04
of the very important property of
8:05
counting semaphore is that you can
8:07
release the semaphore at any point in
8:11
unlike binary semaphore it's not
8:13
necessary to release the semaphore by
8:15
the tasks which have acquired it
8:32
so I have created four tasks with
8:35
different wait time let's build the code
8:41
there are no errors so flash it to the
8:45
board I am using Hercules for the serial
9:15
let's put breakpoint inside each task
9:33
okay let's run the program as expected
9:37
the HPT will run first and hit the
9:40
breakpoint counting semaphore was
9:43
created and the statement from HPT note
9:48
that at this point there are three
9:50
tokens available as we release them
9:52
while entering the HBT semaphore was
9:56
acquired data was accessed and hbt was
9:59
suspended for 3 seconds semaphore wasn't
10:03
released by the HPT so there are 2
10:06
tokens available at this point
10:08
now the NPT runs accesses data and goes
10:13
into suspension without releasing the
10:22
touken available is one LPT runs
10:26
accesses the data and goes into
10:29
suspension without releasing the
10:31
semaphore again tokens available are
10:34
zero control have entered the VL PT
10:37
let's see what happens now the l PT
10:42
couldn't get the semaphore because there
10:44
is no token available now l PT will come
10:48
out of suspension in one second and
10:50
preempt the VL PT and it will try to
10:53
take the semaphore as there is no
10:56
semaphore token available all these
10:58
tasks will go in the waiting forever
11:34
next let's cup the you are to receive
11:37
one byte of data in the interrupt mode
11:40
once the interrupt is triggered a
11:42
callback function will be called and we
11:45
must write that too if the received data
11:53
is R we will release the semaphore here
11:58
releasing the semaphore from ISR is not
12:02
that straightforward
12:03
first we must create a variable called
12:06
higher priority task woken and
12:08
initialize it with PD false then we have
12:12
to release the semaphore using X
12:14
semaphore give from ISR whose parameter
12:17
will the address of the variable that we
12:19
created just now along with the handler
12:21
the idea behind this is let's say the
12:25
interrupt was triggered when the control
12:27
was in LP t while the interrupt is being
12:30
processed the higher priority task free
12:33
emptied the lower priority
12:35
now when the ISI exits the control
12:39
shouldn't go back to the lower task and
12:41
therefore high priority task woken
12:43
parameter will be automatically set true
12:46
port n switch will do the context switch
12:49
if the variable is true if the variable
12:53
is false the port ends which have no
12:56
effect you must write this in every is
13:06
let's not give these semaphores at the
13:09
beginning also I am going to modify the
13:12
tasks a bit instead of suspending they
13:16
will be deleted at the end passing null
13:25
means delete the current task
13:34
the LPT won't delete but it will keep
13:37
going in the suspension let's build the
13:40
code and flash it to the board
13:50
I forgot to start the reception in the
14:17
skip the breakpoints let's see semaphore
14:23
created successfully HPT runs but
14:27
couldn't get the semaphore because there
14:29
is none available same for the MPT l PT
14:33
and v l PT all tasks are waiting for the
14:38
semaphore token to become available
14:40
now let's and the character are from PC
14:44
to the controller as you see on
14:47
receiving are the semaphore was released
14:49
and the hbt resumes and delete itself
14:53
again MPT resumes and deletes itself l
14:59
PT resumes and delete now only the l PT
15:04
is left and it will keep resuming every
15:17
let's take a look at the another
15:18
important function in the free RT OS
15:32
you xmf or get count gives the number of
15:36
available tokens of the counting
15:38
semaphore I am again using I Toa to
15:42
convert the number to the string and I
15:45
am going to release the semaphores in
15:47
the beginning I am using same setup for
15:51
the other tasks also
16:08
let's build and run the code now so hbt
16:18
task runs and there are three token
16:20
available right now the number of tokens
16:24
decreases because the tasks are not
16:26
releasing the semaphore when the VL PT
16:29
runs there is not token available and
16:32
therefore it will wait for it forever
16:35
when we send the character our the
16:37
semaphore get released and the VL PT
16:40
runs and again go into the suspension
16:42
till the next token become available
16:57
let's not delete the tasks and keep them
17:01
under suspended states
17:16
I am going to release three semaphores
17:20
from the ISR if the character R is
17:23
received let's debug the code at the
17:34
beginning three semaphores were released
17:36
the number of tokens available decreases
17:39
when there is no token available every
17:43
task will be waiting for the semaphore
18:00
when the character are is sent three
18:03
tokens will be released and the three
18:05
top priority tasks will run again these
18:09
tasks will go in the waiting State for
18:11
the semaphore and same thing will happen
18:17
again if I send the character note that
18:21
only the task h PT m PT and l PT runs
18:26
this is because the VL PT tasks get
18:29
preempted while waiting for the
18:31
semaphore this is it about the counting
18:33
semaphore you can implement it according
18:38
remember that counting semaphore can be
18:41
released from anywhere in the code
18:42
irrespective of the task holding it
18:47
those who like to have themed like this
18:50
go to eclipse marketplace click popular
19:08
this is the theme I am using darkest
19:12
dark theme this is it guys I hope you
19:17
understood the concept leave comments in
19:21
case of any doubt to download the code
19:24
go to the link in the description have a