Hello, welcome back.
And this lesson, we going to create another variation of our last project, which is our Ewart's our
interrupt, we saw how to receive a single byte.
What if we want to receive a packet and the package is made up of, say, 10 bytes.
How do we construct this?
So let's see one way that this can be achieved.
Right.
So I'm going to make a copy of the last project, number twenty six over here.
I'll copy and paste this.
I'll call this number twenty seven.
And then.
I simply call you are you are Torex receiving Puckett's?
Puckett's basically.
OK.
I'm going to open this number 27 call over here, and this is our main NFR.
OK, we still going to be using our interrupts.
We wouldn't get rid of that.
OK, so let's say this time, rather than just receive a single byte, we want to make make sure the
bytes count is, let's say 10 before we we send it.
And we would we are calling it Puckett's essentially OK.
So what we're going to do is we go into.
Update our you at.
IREX enwrapped function, and we would need this function, take two argument, one would be a pointer
to the Buffa to store the content and then the other argument will be the buffer length.
But before we do that, we're going to declare a number of global static variables here.
We're going to have one to tell us what IREX is in progress or not, what a reception is in progress.
Another one will be to hold the are excellent the length of the pocket or the buffer if you meet and
then a pointer to the buffer and then an iterator.
So we're going to have these three this new three variables here.
This one is the are excellent.
This will be whether we want to receive five bytes, ten bytes and then a pointer to the buffer where
we want to store the received data and then the iterator, of course, we would need to apply a loop
here.
OK, once we've done the we're going to update the, like I said, the T rex in its function.
This is going to be the new IREX in its function is going to take.
To argument this is a pointer to that buffer where we want to store what we receive and then the buffer
land, I'm going to copy this new function prototype and bring it to the top here, because we have
the old one here.
Is the old one, the old one.
OK, we cleaned it.
In a way, I'll put this new function prototype here.
OK, so.
Why do we have this, OK, because it's got this sort of border.
So when are we going to do this in this function?
The first thing we're going to do is check if the if our buffer is valid, if the buffer is not pointing
to another location, we've got to check if it's valid.
And then we would initialize our global static's.
We.
We want to check.
Over here.
We want to make sure our X is not in progress, we said our X in progress because one is the flag that
we used to indicate reception is in progress.
So we want to see if we start.
If our in progress is false and.
If our progress is false and our buffa.
We see it over here and.
Buffer is not equal to normal, meaning the buffer is valid, if that's the case, then we want to go
ahead and initialize our system for enwrapped.
And then we want to return zero for success, so I say return to zero over here else.
We could just see return one or return minus one here.
OK, so this is.
There's a better way of doing it.
We come in check, OK?
We've not started receiving yet and our buffer is not now.
So what we want to do is set our course in progress to one and we want to set our bluebottles.
You want to set a length that buffer and the iterator.
So we simply set the global and this global variable here.
We want to set it to the argument when I set the arguments past.
So what a user passes here becomes our excellent and then the buffer points that they use apostacy,
it becomes our IREX buffer that we define over here and the iterator is zero.
And once we've done the OK, everything remains the same.
We have our our enwrapped enabled, then we've given it a priority.
And then with a neighborhood at an end.
Vic.
OK, so once that is done, now we go to our enwrapped service routine and see what we need to do there.
So this is our interrupt service routine or interrupt request handler, OK.
Over here, we don't need a queue anymore.
What we would need, we would employ here is a SEMAFO, we need a SEMAFO that would be sent to our handler
task to let him know that we received the entire packet.
So our cue here, we would just remove that and we would have a SEMAFO here that would help us know
that we've sent all the data and this will show us we've already seen how to use semaphores, which
tasks this will show us how we can apply semaphores in interrupt.
So we'll use the interrupt service routine.
API for semaphores rather than our standard SEMAFO API, so to do this, I'll start off by creating
the SEMAFO Hodler over here.
It's global, so it's called IREX.
That's the name of the SEMAFO.
So we're going to come back to our enwrapped request handler over here.
Currently, our enwrapped request handler just receives the data, installs it in temp file.
OK.
And over here, this is we had our excuse sent from Isar over here and we deleted it.
So what are we going to do is whenever we receive data, we're going to take this data and store it
in our IREX buffer.
OK, so the data is going to be stored in the our buffer and the index will be iterated as well so that
we can store the next piece of data in the next location.
So then once we store the data in the aggregate buffer and we increment the index, we have to check
whether the index is greater than or equal to the the are excellent.
If that's the case, then we can send our SEMAFO.
If that's the case, we can simply see if irex.
Yes.
If it's greater or we can see if it's equal to it's all the same.
So if that's the case we simply said we see, OK, irex in progress, we set it to false or zero, we
use it, we use an integer for this variable so we set it to force O0 like this.
OK, and then we send our SEMAFO and whenever you are sending you also you still have to indicate whether
you want a higher priority task to be woken or not.
So over here we see X semafo give from ESR this the SEMAFO handle, and this is where we want to wake
up a higher priority task or not.
We said not to focus just like we did.
And like I said, I shouldn't have any effect yet since we have a single task.
OK, so once that is done, we come over here and then we load the the Processo, OK?
So check this.
We received the data.
We keep storing it in our buffer.
When the buffer iterator is equal to the IREX length, which is the length that we expect, then we
set our progress to zero and then we send a SEMAFO to indicate we have the entire Puckett's.
So then once the SEMAFO is sent, let's go to our handler task to see how this will be dealt with.
I'll come over here.
This is our task in the previous project, the handler task simply received the data using the key was
simply received in the queue.
OK, over here, what we're going to do is we're going to expect the semaphore.
When we received a semaphore, then we would want to do something to the data.
We want to prints the data, processing, the data or whatever we want.
So what we're going to do is we're going to have a buffer here.
I'm going to have to buffer here.
And the reason the buffer will be, too, is because we wouldn't we wouldn't be using our you what?
I'm going to use this buffer to store the IREX code.
And what I mean by the IREX good is whether the data was received successfully or whether the data is
corrupt or whatever time outlooked.
We're going to store that information in this buffer and the actual data will be stored in our data
here so we can come over here and.
Let's see, for our example, we have we have this value, I'm going to define expected length over
here.
Let's see.
Oh, defined as expected, expected.
Peatland.
So this is the expected length of the Puckett's.
OK.
So we've set us five, so the pocket length is five.
I'm going to come over here.
Where we sit, here we are.
I come over here and initialize our code.
I'm going to set the data, lente the IREX data, let's see our IREX data buffer has more capacity.
It's got more space than our expected length.
We just had expected lend to five.
We can change that value, but our IREX data above has space for twenty 20 items.
So we can leave this.
It doesn't have to be the same.
OK, so the first thing we do when we enter our.
When we enter our Honda task.
To.
Just set everything here, we make sure we initialize it to zero, so I'm simply going to use a follow
up to achieve that.
So four inch equals zero eyes, less than the Buffa size, we simply set everything to zero.
OK, so once that is done.
We go into sets the time out because we want we said, OK, we expect the data, the entire pocket to
OK within five thousand homes, which is essentially five seconds, we expect the entire Buffa to be
the entire package to be received within this, OK?
And this this is our oad start.
IREX, this starts IREX enwrapped function, we don't need this, remember, we've changed our stats,
IREX interrupts function.
Now it's expects to document the buffer in the buffer length.
So we're going to call our state our enwrapped function over here.
We're going to see.
Starts on school.
On the school in Rob.
And the first argument is the.
Buffer, which we said when asteroids in our states are here.
The second argument is the length.
So the length of the buffer, we can pass 20 over.
Actually, it would make more sense if we make the buffer size the same as the expected length.
Now, simplify things, we would need to write less conditions.
So I'll copy our expected picket line here.
And that's going to be the size of our IREX data.
So I'll push this year.
And then our Étretat over here.
So if you close your eyes less than expected puppetland and then over here when we come starts IREX
interrupt, this is our IREX data.
This is the land expected pocket size, OK.
So this one will do, but we need to call this we need to call this repeatedly.
You know, while one loop, so this over here, I'll cut it and put it over here right this bit, we
do not need it anymore.
So we said this is just the initialization.
Then go over it in our IREX handler, we initialize our you AT4 are in it and then we initialize the
buffer to store the received, uh, data.
We call in this IREX data, we set everything to zero and then we defined a time out value that we want
to give.
OK, and then when we came over here in our one loop we see stats Ulriksen robt, we passed our buffer
and then the length.
Now we're going to take the SEMAFO that was sent or the SEMAFO that will be set will be sent from the
interrupt request handler.
We're going to check if the SEMAFO can be taken.
So we see ximo photic are done the name of the SEMAFO and then the timeout which is this time of value.
We say if this is pass, meaning if we are able to take it then.
We can print received or something.
We can if this is passed, then we can print a particular code.
OK, so over here we can check what other data lenders are expected.
So we'll see if.
Expected PKC lenth.
Is equal to our IREX index, remember, we have this other global variable called index, the Iterator,
which moves through the Buffa indices, OK, it's expected LENTE is equal to the current are iterator.
Then we know we've received all the data.
We can see Sprint and Sprint here, this used to take a string and place it in a buffer for us.
So go clean, see sprint over here and what we want to do is this is our buffer.
The reason I'm using sprint efforts, we're going to be using our Ewart's in IREX mode, so we cannot
print that.
So we're going to take our code, the code here, because it could be a word as simple as received.
OK, so we're going to take this story to you.
That's what this means, Sprint F means.
Take this and put it in this buffer.
Okay, so if the expected length is equal to the other idea we want to store this year when a prince
received.
What if it's not El's?
We want to print let's data so we can see.
As princess over here.
And then we still want to store here.
You know, our xcode by far and over here we can see.
Data are for error and we could see.
Maybe we expected.
A particular number, and then we've got a particular number we can see expected.
The percentage I bite's.
Actually.
I simply say lenth miss much, let's not complicate complicated things decided on the right phrase to
type here.
OK, length mismatch.
OK.
And over here.
So this is what happens.
So meaning if we are able to receive the data within this time out, we check whether the length is
the same, because the IREX iterator would be the number of the last data item we check.
If that's it, we say, OK, our xcode received.
If it's not the same, we see a length mismatch.
What if we are not able to received within?
We're not able to receive within the the declared time out value.
OK, so we're going to write on this condition here to deal with a C else.
We want to see Asprin F and over here we can see printed time out.
You can see our xcode.
Time out over here like this.
OK, so we're looking good.
Looks long, but we're looking good.
OK.
OK, so let's build and see what we have a click here to build.
We have to anomaly's.
OK, we've now included semaphores in this project, so we would come over here to our systems as well
as the fall right.
Click open declaration and scroll down here and I'm going to include some of the rage we spelled s m,
p, h, r and R, OK, s.
The schoolbook.
Quick to run, quick to build first.
It's built successfully.
I'm going to right click this.
Debuggers estimated to double click this to happen, I say, OK.
Click the switch.
OK, so I'm going to remove this, receive Buffa, remove all and what I'm going to do is.
I'm going to add our data and our expert exposure, since we know we are starting to receive data in
our school, our our data and our exposure.
That is where they received stuff will be kept.
So I copied this.
Our xcode.
And then IREX data, if we want to check the data itself.
OK, and I'm going to open real time, real time is already here.
I'm going to expand our Xcode.
We gave it a length of 50.
OK, we may need to shorten this.
So I click play here and then I'm going to press a number of letters on my keyboard.
I've selected this type in area of real time.
The reason we don't see what we write is because we've no, we're not echoing what we typing.
OK, so I'll press a number of keys.
Let me see what I need to first open the port.
OK, nothing is happening, I've pressed.
You see, when I press a key, we see here, you see you see the lights here showing that I'm trying
to transmit from here, OK, but there is nothing in our IREX.
Could the ACARS data?
Our data has nothing as well, so I'm going to press you stop here.
Let's see where that's our program and.
Config assets, PKU, hahaha, I've seen this error before, it could be that we didn't create we have
a queue somewhere or we have a SEMAFO that we didn't create.
Yes, whenever you don't create a feature and you try to use it, you end up at a part of the code.
Indeed the SEMAFO wasn't created and just bear with me.
I've made this error before.
We just, we just declare the handle of the semaphore without creating it.
So let's create the SEMAFO.
So when we come over here, we create the SEMAFO by saying XML for creates binary and we store the handle
in the handle.
We declared over here.
OK, let's stop this.
OK, so click over here to build again.
It's built successfully.
Click here to go to Debugger.
Click here to switch.
OK, here we go, I'm going to expand this hour Xcode.
And I'm going to open this.
This is real time is still the going to click to run and then I'm going to press a number of keys on
the keyboard.
OK, we're still transmitting yet.
Nothing to see.
OK, we're going to come over here, but they're going to come over here.
Oh, here is a it took a while timeout.
OK, so at least something is working.
We didn't receive the data, but we did receive a timeout.
This is timeout t i m e o u t o uti.
OK, timeout.
Let's see our data buffer.
OK.
We received the data.
We did receive the data.
OK, I know what is happening here.
Let's.
Less comment out our spring time out, so we said if we don't receive within the timeout print, time
out.
I'm going to comment this out first.
It could be that.
We received the data and the integrity is good, but it's just a bit late, so we don't get to see,
you know, we don't we end up seeing time out as the code in a way.
Let's comment this out and see if we can receive the data with this call received or lenth mismatch
before we come in and able this a click here to view it.
It's viewed successfully or click here to go to the debugger.
OK.
So I'm going to click to run and then I'm going to type number of keys, OK, received.
Right.
So we've been able to receive I just typed what you would call gibberish, so let's see that.
Yes, you can see USFS, OK?
So the idea is I have to type fast enough, otherwise there'll be a time out.
OK.
So now let's go back, let's increase the timeout amount.
I'm going to increase the timeout from.
Let's say five seconds.
To eight seconds, OK?
Quick Hits Bude.
And then we get to go to the debugger.
And I'm going to type one, two, three, four, five, just so you know.
I'm going to come over here, expand the code.
Well, let's see, we did in a neighborhood, a time out asprin, if sorry, but let's go back and enable
the experience of full time art is still committed.
Art s click here to build.
Let's build successfully go to debugger.
Switch.
OK, explain this.
And we can increase the buffer here, since the code doesn't require 50 bytes so we can increase the
OK.
And I'm going to come over here, press this and then quickly.
OK, received.
So I was able to type fast enough.
And you can see one, two, three, four, five.
OK, so this is it over here.
The the lesson or the take away here is we use in the semaphores to do this for us.
The SEMAFO we are using has to be give from ESR.
It's not just our standard SEMAFO gif.
So we've seen how to use Qs in uninterest service routine, how to use semaphores.
That's all there is for this lesson and I'll see you in the next lesson.
Have a nice day.