0:08
hello and welcome to controllers etch I
0:12
have decided to continue the videos on
0:15
stm32 dual core mcus and for another
0:18
couple of weeks I will release videos on
0:21
intercore communication using different
0:25
I have already made a video on how to
0:27
get started with the dual core MCU where
0:30
I covered how to program the MCU and how
0:35
today we will start with the hardware
0:37
semaphores also known as HSM
0:40
as their name suggests HSM is just like
0:43
the binary semaphores we covered in the
0:46
rtos but these are built on the hardware
0:50
they are not used for the data transfer
0:52
but rather used for notification
0:54
synchronization and for the shared
0:58
today we will see how to use the
1:00
hardware semaphore for the inter core
1:02
notification for the core
1:04
synchronization and how to use it to
1:06
block the shared resource
1:08
let's take a look at the reference
1:14
here we have the topic Hardware
1:17
this block provides 32 register-based
1:20
semaphores along with a non-blocking
1:22
mechanism to lock to semaphores
1:25
there are two ways to lock a semaphore
1:28
in a two-step lock we need to write the
1:31
core ID and the process ID followed by a
1:36
and in a one-step lock we read the core
1:39
ID from the semaphore
1:41
we will be using the whole functions to
1:44
lock and release the semaphores so it
1:46
will be a bit easier for us
1:48
each semaphore can generate an interrupt
1:51
and semaphores are released only when
1:53
core ID and process ID matches with
1:58
so there are 32 semaphores in total
2:01
the process ID can be 8-bit in size and
2:05
the core ID can be 4-bit
2:07
you can read more about the semaphores
2:10
but we don't need too much info for
2:12
today's tutorial so I will skip this
2:15
let's start the queue IDE and create a
2:23
stm32h745 discovery board
2:26
give some name to the project and click
2:32
let me clear the pin outs first
2:34
I have already covered the clock set up
2:37
in the getting started video and I am
2:39
following the same here
2:41
the system is clocked by the external 25
2:44
megahertz Crystal and is running at 400
2:47
megahertz for the M7 core and 200
2:50
megahertz for the M4 core
2:53
there are two LEDs on board and I will
2:56
use them to demonstrate the inter core
2:59
LEDs are connected to the pins Pi 13 and
3:03
pj2 so I am setting them as output
3:07
now we need to assign these pins to
3:11
I am assigning I13 to the M7 core and J2
3:18
these pins are inverted so we will pull
3:20
them high initially so that the LEDs
3:23
will be off by default
3:25
we also need to enable the interrupt for
3:29
the nvic-1 is for the M7 core and two
3:35
I am enabling the interrupt for the
3:40
that's it for the cube MX click save to
3:45
here we have the two main files for the
3:50
let's build them both before proceeding
3:58
we will start with the core notification
4:02
here the M7 core will send notification
4:07
the M7 will first take the semaphore
4:10
with the ID 0. this is already defined
4:13
by default as the pre-generated code
4:16
also uses this semaphore number zero
4:19
after acquiring the semaphore we will
4:22
toggle the LED 10 times with a delay of
4:29
then the semaphore will be released the
4:32
fast take function is used to take the
4:34
semaphore without mentioning the process
4:38
when we use this function the process
4:40
IDE is automatically assumed to be zero
4:44
when using the release function the
4:46
process ID must be mentioned and that's
4:49
why it is set to zero
4:55
now in the M4 core we will use the
4:58
notification sent by the M7 core
5:01
let's create a variable and assign it
5:03
the value 0. if the note if received is
5:07
1 means the notification has been
5:09
received we will simply toggle a LED
5:13
we need to activate the HSM notification
5:20
we have activated the notification for
5:22
the hsm0 so when the M7 core will
5:25
release the semaphore an interrupt will
5:27
trigger and the HSM free callback will
5:36
inside the Callback we will set the
5:38
variable note if received to 1 and
5:41
activate the notification again
5:43
let's understand it one more time
5:46
the note if receive it set to zero so
5:49
the code can't run in the beginning
5:52
the M7 core will toggle the LED 10 times
5:55
and then release the semaphore
5:58
this will trigger the interrupt in the
6:00
M4 core the note if receive variable
6:03
will be set to 1 and the second LED will
6:08
this process will keep running forever
6:11
let's build both the files
6:14
now we will create the debug
6:16
configuration so make sure you are in
6:21
now click on the Run configuration and
6:25
the configuration should be generated
6:30
go to the debug Tab and check all to all
6:37
now go to the startup Tab and click the
6:41
from the drop down menu select the cm4
6:44
option make sure everything is checked
6:49
now we need to move the CM7
6:52
configuration to the top here
6:54
everything is done click apply and run
6:59
this will load the code to both the
7:01
cores but we need to manually reset the
7:04
board after the loading has been
7:14
here the red LED is controlled by the M7
7:17
core and the green is controlled by the
7:21
the red LED is blinking five times and
7:24
then the green LED toggles once
7:27
basically the M7 core is toggling the
7:30
red LED 10 times then it is sending the
7:33
notification to the M4 core and finally
7:36
the M4 core toggles the green LED once
7:39
so we were able to make one course and
7:42
the notification to the second core
7:44
using the hardware semaphores
7:46
next we will see how to synchronize both
7:50
in this case the M7 core will first run
7:53
and finish its job then the M4 core will
7:58
in this way the cores will be
8:00
synchronized where the M4 core cannot
8:02
run until the M7 has finished working
8:05
here we will first write the code for
8:09
let me toggle this part and before
8:11
uploading the final code I will comment
8:17
the M7 will toggle the LED 10 times
8:21
then it will take the semaphore and
8:30
now let's go to the M4 main file
8:43
here we will wait for the semaphore to
8:46
be taken this function will return 1
8:49
once the semaphore is taken the control
8:52
will wait at this line until it happens
8:55
and once it does return 1 we will turn
8:58
the LED on for two seconds and then turn
9:02
let's do the same thing for the M7 core
9:04
also it will turn the LED on for 2
9:07
seconds and then turn it off
9:15
then it will take the semaphore and this
9:17
function will return 1.
9:19
let's build and Flash the code to the
9:29
here you can see the red LED is on for
9:32
two seconds and then the green LED is on
9:35
for another two seconds
9:37
basically the M7 core is turning the LED
9:40
on in the meantime the M4 is waiting for
9:43
the semaphore to be taken
9:45
when the red LED turns off the M7 core
9:49
acquires the semaphore the M4 core can
9:52
now proceed with turning the green LED
9:55
so the M4 core starts its job only after
9:58
the M7 core has finished its
10:01
we were able to synchronize the cores to
10:04
do their job sequentially now we will
10:06
see how to block the shared resource
10:08
using the hardware semaphore
10:10
we have covered something similar as the
10:12
binary semaphore in the rtos tutorial
10:16
where the semaphore must be taken before
10:19
using the resource and this is exactly
10:21
what we will do here also
10:24
this time I am using the function hm
10:26
take and it takes two parameters
10:30
the semaphore ID is still the same and I
10:33
am using the process ID 12.
10:36
this is just a random process ID I have
10:40
this statement will wait until the
10:42
semaphore is acquired and once it does
10:45
we will toggle the LED 20 times with a
10:48
delay of 100 milliseconds
10:51
now the semaphore will be released but
10:53
make sure you use the same ID that you
10:56
have used while locking the semaphore
10:58
we will use the same code in the M4 core
11:01
but with a small difference of blinking
11:03
time just to differentiate between which
11:06
core is blinking the LED
11:08
the shared resource in this case is
11:10
going to be the LED connected to the pin
11:13
Pi 13 which is currently defined in the
11:18
we will Define it in the M4 file also
11:22
let's build and Flash this code to our
11:32
here you can see the LED is blinking
11:34
fast 10 times and then it's blinking
11:38
basically when the M7 core acquires the
11:41
semaphore it blinks the LED at a faster
11:44
rate and when the M4 core acquires it it
11:47
blinks the LED at a slower rate
11:50
so the shared resource that is the LED
11:53
is accessible by both the cores and they
11:56
need to acquire the semaphore before
12:00
so we saw how we can use the hardware
12:02
semaphore to send the notification
12:04
between the cores how to synchronize the
12:06
cores and how to use the shared resource
12:11
this is it for the video
12:13
I hope you understood it
12:15
I will continue this series and more
12:18
videos about inter core communication
12:20
will come in the next few weeks
12:23
we will see how to share the memory
12:25
region between the cores how to use the
12:28
resource manager utility to transfer
12:32
the link to download the code is in the
12:35
description of this video
12:37
leave comments in case of any doubt
12:40
keep watching and have a nice day ahead