Hello, welcome back and this lesson, we're going to see how to work with mutex, I'm going to make
a copy of our last project.
The project in which we created the drive is the project here called Some Drivers.
I'm going to copy this copy and paste over here and I'll rename this to SEMAFO Mutex Twenty on the SEMAFO
Mutex.
It's called Pean.
We had done.
OK, so this is 20 I'm going to open may not see over here, right?
So we're going to create an application similar to the one we had in our binary semaphore project.
I'm going to open one may not see over here.
We already have our ADC SDI files included, so we need to include them over here, the digital read.
And since I read, we're going to come into this out.
Or we just cut it out later, so less great, we going to create a simple application that has a number
of tasks, there is going to be.
There's going to be two tasks.
One task will be for the digital sensor, which is our button, and then another task is going to be
for the analog sensor, which is our ADC that we've created or with initialized.
So I'm going to start off by first creating a semaphore of semaphore here.
Remember, a mutex is a type of semaphore.
So it creates a semaphore handle.
Yeah.
Called Zero SEMAFO.
And this semaphore is going to be used to access the serial port.
Each of the task we need to acquire this semaphore in order to be able to access the serial port.
So once I've done this, I'm going to just start by printing system initializing.
Like I said, we're going to use this to make sure our you know, what is giving us problems when something
is not working.
So once this is done, we said the system initialize in.
And once that is done, we're going to create the mutex semaphore.
Like this, we simply see eczema for Cretz mutex and then the handle is going to be stored in a variable
that we declared up here, OK, once that is done, we're going to create two tasks.
The first task is going to be for the digital sensor, as we said.
So this task is called digital sensor task.
That's the name of the task function.
We simply call this bottom reed and this is the stack size.
You can make the stack size 100.
It's fine.
And it's quite a priority.
Off to the next one is going to be the the one for the analog sensor.
So we simply call in this sense or read and a task function is going to be called analog sensor.
And we've given this a priority one over here.
The priority really doesn't matter.
We can use in a priority configuration, as we shall see, but the reason is for who starts first.
So we would experiment with that.
OK, so once we've done this, we would start the scheduler.
By simply calling our V Task Start scheduler.
So once that is done, we can go ahead and implement the task functions.
We start off with the.
The task function for the digital sensor, so I'll come over here.
It's called Digital Sensor Task Open, Close, and then our infinite loop in here like this.
And then we have one for the analog sound, so.
Open, close, and then our infinite loop here like this.
So actually, inside our digital central task, we can initialize our GPU there, so cutaway you in
it.
And then bring it inside digital sensor over here, not in the infinite loop, but we want to run this
once, so we put it in here.
And then we're going to initialize the ADC in the analog central task, so cut this.
And then I'll bring it down here.
Like this, OK, so once this is done.
In our digital sense or task, we going to read the bottom states, so this line that we had previously
beaten states, of course, read digital.
So I'm going to cut this.
And then I'll put it in our digital senso.
And then.
Over here, you our.
Analog Senseor.
I'll cut this and put it in our analog or task such that each Senso, each task reads its own Senso.
OK, so so each task would read it senseor and then the task would try to grab the SEMAFO grab to try
to grab the semaphore.
OK.
SEMAFO.
We see.
If ximo for take.
And then we basically call in our semaphore take function, we pass the SEMAFO Hondo here, and then
the amount of time over here, we are checking if it's true.
So if the SEMAFO is not available, wait five ticks for the scheduler to see if it becomes free.
So, wait, five takes off the schedule, are we using ticks here?
We've not converted this to milliseconds.
This is fine.
So we're saying if it's not available, wait for five ticks and if.
If it's not available, wait for five ticks, OK?
And if you receive it, then print this, you know, a zero port.
Right, if.
If this is true, meaning you've been able to take the semaphore, then this block will print.
And then over here, we can put a bit of delay for stability, OK, task.
DeLay one year like this.
OK, so.
Once we've done that, we have to give the semaphore, remember, I said in the in the binary some of
what I said, there is a way that we can take the semaphore without giving it.
Well, this is not the method over here, although it is called a mutex semaphore.
When we take it, we still have to give it.
It's not.
So still, we use our semaphore teak and semaphore give.
I know I mentioned there's a way to use one function rather than use both take and then give.
This is not the method.
OK.
So once we've done this, we're going to give the semaphore, remember, you've got to give the symbol
for using our bathroom analogy once you've taken the key to enter the bathroom.
You have to retain the key.
So we give the SEMAFO so any other task can take the SEMAFO and use the support as well.
The only difference between this and the other one we had is we declared this as semaphore creates mutex
OK, so once this is done, we're going to use the same method for.
Our analog.
This is our analog task.
We take the SEMAFO.
We have this over here.
And then.
Sorry about that.
So first we read the census data, we store it in its variable, and then we take the SEMAFO.
And then once we've taken the semaphore, we're going to print this and so value.
We simply prints of value because now we have access to the, uh, the Ewart's, we can print the sense
of value and then we give the semaphore.
Like this.
In here, once that is that we can add V task, delay one for stability.
OK, so this one, we have digital sensor task.
Takes and then its uses it and then gives and then this one takes, except that this time we use an
actual ADC values and the button state.
OK, so this is what we have.
What we have to do is take our function prototypes to the top.
I'll copy analog since I taught, uh, task over here.
And then we do the same for digital sensor task.
Right, so we're looking good.
OK, let's build and see what we have a click over here to build.
OK.
Over here, we have an error and then a couple of warnings error.
First it says, OK, we've not included our semaphore at age four, so we've got to include that.
So I'm going to come over here to Simms's storage open declaration and then.
I'll come over here and say include.
It is spelled s e m p h r, I think that the H.
OK, let's run and see, perhaps the E is not required.
OK, no error, so the spelling is good, this is the name of the semaphore file to these errors, these
warnings, as I specify, a full percentage.
So I'm going to make this percentage L.
D and over here, percentage L d that's run and see.
Dispute what, as I say, one zero one warning.
The warning.
Let's see what I say, percentage go the expect argument of long and.
OK, what was the first warning then, control s.
And control is.
Let's see what data types do we have here?
We have you and.
OK, 32 and you and eight run this.
And we have one warning I see this is the sense of value.
For much percentage, the expected argument of end.
OK, so I'm simply going to change this to I can type can start to end or change this to enter simply
it doesn't make a difference here.
Click over here to build.
OK, we have no one in.
OK.
OK, so I'm going to open real time.
I'm going to select the port, not open this port on the side, and then I'm going to right click and
then I'll see.
Debuggers, ASTM 32 and then iSelect number 20 here.
And then OK.
It's open in SWICH.
And then side by side.
I'm going to click to run over here like this.
OK, as you can see, we get to know.
Our sense of value as well as our partner states, I'm going to press the button and you can see where
we see zero our exits to the other side.
So if I move it, you will see the value change as well.
But you can experiment that on your own.
OK, so this is how our mutex is allowing us to access the single resource, which is why you out in
an orderly manner if we comment out the semaphore taking some of Augier functions.
We realize that the data will be jumbled up in the display.
OK.
So.
Like I said, the the the priority really doesn't matter here we started with digital sensor hub in
a priority of two.
OK, we have to set this up, but before we go, I will show you a difference, the difference between
this one, the difference between a SEMAFO create a creating SEMAFO in binary.
Some of what we had you realize that over here we didn't have to give the SEMAFO.
You see, when we created the SEMAFO, we didn't give it, the first thing we did was check if it's
available, whereas in our binary semaphore we had this issue, we had a deadlock.
I'll show you this is how binary some of our projects are.
Click over here to open.
And then I'll open the door, may not see for.
It's open in a computerized behave in slow.
OK, so this is how binary SEMAFO.
You see over here, we had to give the SEMAFO in a way like basketball, someone has to toss the ball
in the air so that one team will grab it over here.
We had to give the SEMAFO before we take it.
Right, but when we create it as a mutex, we need not do that.
Over here, we didn't give the some of what we simply took it.
OK, so we said the the priority here has no difference.
So I'll just set this also to priority one.
So they both have the same priority of one less run and see if it makes a difference to be able to build
within.
And I'm going to download onto the board by clicking here.
Then click to switch.
OK, so I'm going to run the code, click over here to run.
Going to open our ports, as you can see, it's working the same way, pressed the push button and as
you can see, we are reading about zeros.
OK, right, so that's all there is for SEMAFO Mutex you have any questions, leave them in the questions
and answers area and I'll see you later.
Have a nice day.