0:07
hello everyone welcome to controllers
0:11
tech today in this video we will see how
0:16
to use mutex in a very simple way this
0:19
is part one of the video and it will
0:21
only cover the basics of mutex at basic
0:25
level mutex is very similar to binary
0:28
semaphore you can check out my video on
0:31
binary semaphore maybe that will help
0:34
you better understand the mutex also
0:37
let's start by creating a project in
0:50
type the name for the project and click
1:02
here is the cube MX first
1:07
I am enabling the external crystal for
1:10
the clock as we are using free RTOS we
1:14
must select the time base other than
1:16
Cystic enable the UART to communicate
1:20
with computer enable the free RT OS
1:34
I am leaving everything as it is here
1:41
this task will be created by default
1:44
let's see the clock setup now select the
1:49
external crystal which is of 8 megahertz
1:51
and I want the system to run at 100
2:03
click Save to generate the code
2:21
as I am NOT going to use the CMC's
2:24
functions copy all these and we will
2:27
include them in our project separately
2:31
other than these we also need string dot
2:42
we don't need these right now also CMS
2:47
SH is not required I am removing the
2:55
default task related functions
3:05
let's begin now first of all we need to
3:09
define the mutex handler let's call it
3:13
simple mutex next define the task
3:17
handlers also I am going to use three
3:29
next the functions where the task code
3:38
now first we will create the mutex using
3:42
ex semaphore create mutex if the mutex
3:46
was created successfully it will return
3:49
any value other than null and in that
3:52
case we will print this string now we
4:00
will create the tasks ex task create
4:03
takes the following parameters task code
4:07
where the task code is written name of
4:11
the task stack size parameter priority
4:14
and the handler I am assigning a
4:17
priority of 3 to the HPT and lower
4:20
priorities for the other tasks now start
4:24
the scheduler we don't need all three
4:27
tasks for this case right now now we
4:31
will write the task related code
4:49
this string will be printed when the
4:51
control enters the HPT task
5:04
let's create a function which will acts
5:06
as a critical section for this tutorial
5:09
that's why before performing any
5:12
operations we need to take the mutex
5:14
first using X semaphore take function
5:18
whose parameters are a handler of the
5:20
semaphore or mutex and the time for
5:23
which the function will wait in case the
5:26
mutex is not available
5:27
once the mutex is acquired the string
5:31
will be sent to the UART
5:32
and then we will release the mutex let's
5:40
add a delay of two seconds here so that
5:43
we can better understand the blocking
5:48
we will send this string to the UART
5:54
and we will call that function here
6:03
after the control comes back from the
6:06
function this string will be printed to
6:08
indicate the end of the task and finally
6:23
this task will suspend for 1500
6:28
same code is written for the medium
6:30
priority task also just some minor
6:34
changes let's build it now we got some
6:51
warning about the datatypes let's leave
6:56
it in debug our code okay
7:01
I am using Hercules for the serial
7:17
let's set the breakpoints at these
7:19
locations this is to check the flow of
7:23
the control let's run it now obviously
7:33
the control will enter the higher task
7:35
first you can see the string is printed
7:38
on the console now the call to the
7:42
function send UART the string got
7:45
printed successfully HP T acquired the
7:49
mutex printed the string and released
7:52
the mutex the HPT task goes into
7:56
suspension for 1500 milliseconds the
8:00
control entered the medium priority task
8:03
now the entry string is printed here now
8:07
the call to the function send UART
8:13
the MPT string was printed and the
8:17
function will wait for the two seconds
8:19
delay to complete before releasing the
8:32
by then the HPT task will wake up and
8:36
preempt the MPT it will print the entry
8:51
and try to take the mutex but the mutex
8:57
is still held by the MPT so the higher
9:00
tasks have to wait for the mutex to
9:03
become available once it does the HPT
9:07
will take the mutex print the string and
9:10
release the mutex HPT will exit now the
9:16
control will go back to MPT and it will
9:19
also exit same thing will happen if we
9:22
run the code again and this cycle will
9:30
continue if we let it run freely this is
9:34
how mutex works it's basically a mutual
9:38
exclusion from accessing the same
9:40
resource by many tasks if you know about
9:43
binary semaphore this is exactly how
9:46
binary semaphore works - although there
9:50
is a small difference between them and
9:52
it's about priority inversion and
9:54
priority inheritance I have to end this
9:58
video here otherwise it will be too long
10:01
I will release another video explaining
10:04
the difference between binary semaphore
10:05
and mutex and also between priority
10:09
inheritance and priority inversion keep
10:12
watching and have a nice day