1
00:00:00,650 --> 00:00:06,800
Hello, welcome back and this lesson, we're going to see how to work with mutex, I'm going to make

2
00:00:06,800 --> 00:00:08,330
a copy of our last project.

3
00:00:08,750 --> 00:00:13,460
The project in which we created the drive is the project here called Some Drivers.

4
00:00:13,860 --> 00:00:25,460
I'm going to copy this copy and paste over here and I'll rename this to SEMAFO Mutex Twenty on the SEMAFO

5
00:00:25,670 --> 00:00:26,390
Mutex.

6
00:00:30,730 --> 00:00:31,510
It's called Pean.

7
00:00:32,650 --> 00:00:33,280
We had done.

8
00:00:35,970 --> 00:00:41,380
OK, so this is 20 I'm going to open may not see over here, right?

9
00:00:41,730 --> 00:00:48,330
So we're going to create an application similar to the one we had in our binary semaphore project.

10
00:00:49,100 --> 00:00:52,490
I'm going to open one may not see over here.

11
00:00:52,500 --> 00:01:01,580
We already have our ADC SDI files included, so we need to include them over here, the digital read.

12
00:01:01,590 --> 00:01:04,410
And since I read, we're going to come into this out.

13
00:01:05,720 --> 00:01:12,920
Or we just cut it out later, so less great, we going to create a simple application that has a number

14
00:01:12,920 --> 00:01:14,990
of tasks, there is going to be.

15
00:01:16,760 --> 00:01:18,140
There's going to be two tasks.

16
00:01:19,120 --> 00:01:25,330
One task will be for the digital sensor, which is our button, and then another task is going to be

17
00:01:25,330 --> 00:01:32,700
for the analog sensor, which is our ADC that we've created or with initialized.

18
00:01:33,070 --> 00:01:38,550
So I'm going to start off by first creating a semaphore of semaphore here.

19
00:01:38,830 --> 00:01:41,280
Remember, a mutex is a type of semaphore.

20
00:01:41,530 --> 00:01:42,940
So it creates a semaphore handle.

21
00:01:43,120 --> 00:01:43,470
Yeah.

22
00:01:43,490 --> 00:01:45,370
Called Zero SEMAFO.

23
00:01:45,670 --> 00:01:50,740
And this semaphore is going to be used to access the serial port.

24
00:01:51,460 --> 00:01:57,120
Each of the task we need to acquire this semaphore in order to be able to access the serial port.

25
00:01:57,640 --> 00:02:02,650
So once I've done this, I'm going to just start by printing system initializing.

26
00:02:02,650 --> 00:02:07,990
Like I said, we're going to use this to make sure our you know, what is giving us problems when something

27
00:02:07,990 --> 00:02:08,720
is not working.

28
00:02:09,460 --> 00:02:12,570
So once this is done, we said the system initialize in.

29
00:02:13,090 --> 00:02:16,690
And once that is done, we're going to create the mutex semaphore.

30
00:02:18,860 --> 00:02:25,970
Like this, we simply see eczema for Cretz mutex and then the handle is going to be stored in a variable

31
00:02:25,970 --> 00:02:31,250
that we declared up here, OK, once that is done, we're going to create two tasks.

32
00:02:31,610 --> 00:02:35,690
The first task is going to be for the digital sensor, as we said.

33
00:02:36,080 --> 00:02:38,560
So this task is called digital sensor task.

34
00:02:38,570 --> 00:02:40,070
That's the name of the task function.

35
00:02:40,340 --> 00:02:43,040
We simply call this bottom reed and this is the stack size.

36
00:02:43,040 --> 00:02:44,690
You can make the stack size 100.

37
00:02:44,690 --> 00:02:45,170
It's fine.

38
00:02:45,440 --> 00:02:46,580
And it's quite a priority.

39
00:02:46,580 --> 00:02:53,390
Off to the next one is going to be the the one for the analog sensor.

40
00:02:53,450 --> 00:02:58,740
So we simply call in this sense or read and a task function is going to be called analog sensor.

41
00:02:58,760 --> 00:03:01,460
And we've given this a priority one over here.

42
00:03:01,460 --> 00:03:03,410
The priority really doesn't matter.

43
00:03:03,420 --> 00:03:11,570
We can use in a priority configuration, as we shall see, but the reason is for who starts first.

44
00:03:12,570 --> 00:03:14,340
So we would experiment with that.

45
00:03:14,650 --> 00:03:18,020
OK, so once we've done this, we would start the scheduler.

46
00:03:19,860 --> 00:03:22,950
By simply calling our V Task Start scheduler.

47
00:03:23,550 --> 00:03:28,530
So once that is done, we can go ahead and implement the task functions.

48
00:03:29,070 --> 00:03:30,000
We start off with the.

49
00:03:31,900 --> 00:03:36,110
The task function for the digital sensor, so I'll come over here.

50
00:03:37,840 --> 00:03:45,550
It's called Digital Sensor Task Open, Close, and then our infinite loop in here like this.

51
00:03:48,620 --> 00:03:51,800
And then we have one for the analog sound, so.

52
00:03:56,660 --> 00:03:59,480
Open, close, and then our infinite loop here like this.

53
00:04:04,180 --> 00:04:10,930
So actually, inside our digital central task, we can initialize our GPU there, so cutaway you in

54
00:04:10,930 --> 00:04:11,200
it.

55
00:04:12,270 --> 00:04:18,480
And then bring it inside digital sensor over here, not in the infinite loop, but we want to run this

56
00:04:18,480 --> 00:04:20,750
once, so we put it in here.

57
00:04:23,020 --> 00:04:28,870
And then we're going to initialize the ADC in the analog central task, so cut this.

58
00:04:31,470 --> 00:04:32,840
And then I'll bring it down here.

59
00:04:35,240 --> 00:04:38,390
Like this, OK, so once this is done.

60
00:04:40,250 --> 00:04:46,070
In our digital sense or task, we going to read the bottom states, so this line that we had previously

61
00:04:46,070 --> 00:04:47,930
beaten states, of course, read digital.

62
00:04:48,160 --> 00:04:49,340
So I'm going to cut this.

63
00:04:50,580 --> 00:04:53,250
And then I'll put it in our digital senso.

64
00:04:54,630 --> 00:04:55,230
And then.

65
00:04:59,090 --> 00:05:00,560
Over here, you our.

66
00:05:04,570 --> 00:05:05,620
Analog Senseor.

67
00:05:07,240 --> 00:05:15,060
I'll cut this and put it in our analog or task such that each Senso, each task reads its own Senso.

68
00:05:15,400 --> 00:05:27,880
OK, so so each task would read it senseor and then the task would try to grab the SEMAFO grab to try

69
00:05:27,880 --> 00:05:28,900
to grab the semaphore.

70
00:05:30,070 --> 00:05:30,550
OK.

71
00:05:33,100 --> 00:05:33,820
SEMAFO.

72
00:05:35,630 --> 00:05:36,500
We see.

73
00:05:37,510 --> 00:05:39,460
If ximo for take.

74
00:05:40,490 --> 00:05:46,340
And then we basically call in our semaphore take function, we pass the SEMAFO Hondo here, and then

75
00:05:46,880 --> 00:05:51,730
the amount of time over here, we are checking if it's true.

76
00:05:51,980 --> 00:05:58,430
So if the SEMAFO is not available, wait five ticks for the scheduler to see if it becomes free.

77
00:05:59,460 --> 00:06:02,490
So, wait, five takes off the schedule, are we using ticks here?

78
00:06:02,490 --> 00:06:04,270
We've not converted this to milliseconds.

79
00:06:04,290 --> 00:06:04,820
This is fine.

80
00:06:05,040 --> 00:06:09,810
So we're saying if it's not available, wait for five ticks and if.

81
00:06:12,810 --> 00:06:15,530
If it's not available, wait for five ticks, OK?

82
00:06:16,130 --> 00:06:20,940
And if you receive it, then print this, you know, a zero port.

83
00:06:22,660 --> 00:06:23,830
Right, if.

84
00:06:25,090 --> 00:06:30,550
If this is true, meaning you've been able to take the semaphore, then this block will print.

85
00:06:31,580 --> 00:06:37,040
And then over here, we can put a bit of delay for stability, OK, task.

86
00:06:39,120 --> 00:06:41,100
DeLay one year like this.

87
00:06:44,260 --> 00:06:46,270
OK, so.

88
00:06:47,390 --> 00:06:52,640
Once we've done that, we have to give the semaphore, remember, I said in the in the binary some of

89
00:06:52,640 --> 00:06:56,870
what I said, there is a way that we can take the semaphore without giving it.

90
00:06:56,940 --> 00:07:01,490
Well, this is not the method over here, although it is called a mutex semaphore.

91
00:07:01,910 --> 00:07:03,890
When we take it, we still have to give it.

92
00:07:04,220 --> 00:07:04,730
It's not.

93
00:07:05,030 --> 00:07:09,710
So still, we use our semaphore teak and semaphore give.

94
00:07:09,890 --> 00:07:17,430
I know I mentioned there's a way to use one function rather than use both take and then give.

95
00:07:17,810 --> 00:07:19,160
This is not the method.

96
00:07:19,230 --> 00:07:19,700
OK.

97
00:07:23,800 --> 00:07:29,770
So once we've done this, we're going to give the semaphore, remember, you've got to give the symbol

98
00:07:29,770 --> 00:07:34,400
for using our bathroom analogy once you've taken the key to enter the bathroom.

99
00:07:34,960 --> 00:07:36,130
You have to retain the key.

100
00:07:38,070 --> 00:07:44,390
So we give the SEMAFO so any other task can take the SEMAFO and use the support as well.

101
00:07:45,240 --> 00:07:51,540
The only difference between this and the other one we had is we declared this as semaphore creates mutex

102
00:07:52,090 --> 00:07:57,420
OK, so once this is done, we're going to use the same method for.

103
00:07:58,600 --> 00:07:59,740
Our analog.

104
00:08:01,870 --> 00:08:03,280
This is our analog task.

105
00:08:04,060 --> 00:08:05,170
We take the SEMAFO.

106
00:08:09,480 --> 00:08:10,620
We have this over here.

107
00:08:13,040 --> 00:08:13,610
And then.

108
00:08:18,160 --> 00:08:18,970
Sorry about that.

109
00:08:19,630 --> 00:08:28,180
So first we read the census data, we store it in its variable, and then we take the SEMAFO.

110
00:08:30,140 --> 00:08:34,160
And then once we've taken the semaphore, we're going to print this and so value.

111
00:08:36,490 --> 00:08:43,960
We simply prints of value because now we have access to the, uh, the Ewart's, we can print the sense

112
00:08:43,960 --> 00:08:46,180
of value and then we give the semaphore.

113
00:08:48,830 --> 00:08:49,400
Like this.

114
00:08:50,800 --> 00:08:58,210
In here, once that is that we can add V task, delay one for stability.

115
00:08:59,200 --> 00:09:03,760
OK, so this one, we have digital sensor task.

116
00:09:06,540 --> 00:09:13,650
Takes and then its uses it and then gives and then this one takes, except that this time we use an

117
00:09:13,950 --> 00:09:17,320
actual ADC values and the button state.

118
00:09:17,900 --> 00:09:19,950
OK, so this is what we have.

119
00:09:19,950 --> 00:09:23,440
What we have to do is take our function prototypes to the top.

120
00:09:23,490 --> 00:09:27,060
I'll copy analog since I taught, uh, task over here.

121
00:09:28,440 --> 00:09:31,320
And then we do the same for digital sensor task.

122
00:09:40,100 --> 00:09:42,170
Right, so we're looking good.

123
00:09:44,690 --> 00:09:48,770
OK, let's build and see what we have a click over here to build.

124
00:09:50,850 --> 00:09:51,290
OK.

125
00:09:52,400 --> 00:09:57,460
Over here, we have an error and then a couple of warnings error.

126
00:09:57,500 --> 00:10:05,120
First it says, OK, we've not included our semaphore at age four, so we've got to include that.

127
00:10:08,620 --> 00:10:15,490
So I'm going to come over here to Simms's storage open declaration and then.

128
00:10:16,820 --> 00:10:19,460
I'll come over here and say include.

129
00:10:22,120 --> 00:10:28,780
It is spelled s e m p h r, I think that the H.

130
00:10:37,400 --> 00:10:40,880
OK, let's run and see, perhaps the E is not required.

131
00:10:43,020 --> 00:10:49,080
OK, no error, so the spelling is good, this is the name of the semaphore file to these errors, these

132
00:10:49,320 --> 00:10:52,670
warnings, as I specify, a full percentage.

133
00:10:53,340 --> 00:10:55,560
So I'm going to make this percentage L.

134
00:10:55,740 --> 00:11:00,120
D and over here, percentage L d that's run and see.

135
00:11:01,950 --> 00:11:05,360
Dispute what, as I say, one zero one warning.

136
00:11:05,700 --> 00:11:06,200
The warning.

137
00:11:08,980 --> 00:11:14,170
Let's see what I say, percentage go the expect argument of long and.

138
00:11:16,210 --> 00:11:19,870
OK, what was the first warning then, control s.

139
00:11:21,030 --> 00:11:22,620
And control is.

140
00:11:23,780 --> 00:11:26,010
Let's see what data types do we have here?

141
00:11:26,390 --> 00:11:27,380
We have you and.

142
00:11:29,460 --> 00:11:32,760
OK, 32 and you and eight run this.

143
00:11:34,250 --> 00:11:39,470
And we have one warning I see this is the sense of value.

144
00:11:41,610 --> 00:11:44,750
For much percentage, the expected argument of end.

145
00:11:45,900 --> 00:11:51,660
OK, so I'm simply going to change this to I can type can start to end or change this to enter simply

146
00:11:52,230 --> 00:11:53,670
it doesn't make a difference here.

147
00:11:54,090 --> 00:11:55,140
Click over here to build.

148
00:11:56,290 --> 00:11:57,880
OK, we have no one in.

149
00:11:59,630 --> 00:12:00,100
OK.

150
00:12:02,850 --> 00:12:05,190
OK, so I'm going to open real time.

151
00:12:08,350 --> 00:12:16,690
I'm going to select the port, not open this port on the side, and then I'm going to right click and

152
00:12:16,690 --> 00:12:17,830
then I'll see.

153
00:12:19,860 --> 00:12:23,790
Debuggers, ASTM 32 and then iSelect number 20 here.

154
00:12:24,610 --> 00:12:25,390
And then OK.

155
00:12:29,510 --> 00:12:31,700
It's open in SWICH.

156
00:12:34,820 --> 00:12:36,530
And then side by side.

157
00:12:39,010 --> 00:12:41,680
I'm going to click to run over here like this.

158
00:12:42,810 --> 00:12:45,090
OK, as you can see, we get to know.

159
00:12:46,240 --> 00:12:52,360
Our sense of value as well as our partner states, I'm going to press the button and you can see where

160
00:12:52,360 --> 00:12:55,650
we see zero our exits to the other side.

161
00:12:55,660 --> 00:12:58,000
So if I move it, you will see the value change as well.

162
00:12:58,270 --> 00:13:00,880
But you can experiment that on your own.

163
00:13:01,340 --> 00:13:10,480
OK, so this is how our mutex is allowing us to access the single resource, which is why you out in

164
00:13:10,480 --> 00:13:16,180
an orderly manner if we comment out the semaphore taking some of Augier functions.

165
00:13:16,480 --> 00:13:19,390
We realize that the data will be jumbled up in the display.

166
00:13:20,170 --> 00:13:20,590
OK.

167
00:13:22,110 --> 00:13:22,530
So.

168
00:13:24,670 --> 00:13:31,390
Like I said, the the the priority really doesn't matter here we started with digital sensor hub in

169
00:13:31,390 --> 00:13:33,190
a priority of two.

170
00:13:34,470 --> 00:13:41,070
OK, we have to set this up, but before we go, I will show you a difference, the difference between

171
00:13:41,070 --> 00:13:47,520
this one, the difference between a SEMAFO create a creating SEMAFO in binary.

172
00:13:47,520 --> 00:13:52,470
Some of what we had you realize that over here we didn't have to give the SEMAFO.

173
00:13:53,440 --> 00:13:58,930
You see, when we created the SEMAFO, we didn't give it, the first thing we did was check if it's

174
00:13:58,930 --> 00:14:03,920
available, whereas in our binary semaphore we had this issue, we had a deadlock.

175
00:14:05,230 --> 00:14:08,070
I'll show you this is how binary some of our projects are.

176
00:14:08,080 --> 00:14:09,340
Click over here to open.

177
00:14:10,850 --> 00:14:13,700
And then I'll open the door, may not see for.

178
00:14:17,680 --> 00:14:21,970
It's open in a computerized behave in slow.

179
00:14:22,690 --> 00:14:25,120
OK, so this is how binary SEMAFO.

180
00:14:26,710 --> 00:14:31,990
You see over here, we had to give the SEMAFO in a way like basketball, someone has to toss the ball

181
00:14:31,990 --> 00:14:35,340
in the air so that one team will grab it over here.

182
00:14:35,350 --> 00:14:37,840
We had to give the SEMAFO before we take it.

183
00:14:39,200 --> 00:14:43,460
Right, but when we create it as a mutex, we need not do that.

184
00:14:44,390 --> 00:14:47,150
Over here, we didn't give the some of what we simply took it.

185
00:14:48,790 --> 00:14:53,140
OK, so we said the the priority here has no difference.

186
00:14:53,170 --> 00:14:55,870
So I'll just set this also to priority one.

187
00:14:56,890 --> 00:15:02,460
So they both have the same priority of one less run and see if it makes a difference to be able to build

188
00:15:02,460 --> 00:15:02,620
within.

189
00:15:09,130 --> 00:15:12,490
And I'm going to download onto the board by clicking here.

190
00:15:14,940 --> 00:15:15,930
Then click to switch.

191
00:15:21,530 --> 00:15:26,360
OK, so I'm going to run the code, click over here to run.

192
00:15:29,980 --> 00:15:35,260
Going to open our ports, as you can see, it's working the same way, pressed the push button and as

193
00:15:35,260 --> 00:15:36,960
you can see, we are reading about zeros.

194
00:15:37,820 --> 00:15:44,750
OK, right, so that's all there is for SEMAFO Mutex you have any questions, leave them in the questions

195
00:15:44,750 --> 00:15:46,730
and answers area and I'll see you later.

196
00:15:46,940 --> 00:15:47,740
Have a nice day.
