0:09
hello and welcome to controllers Tech
0:12
this is the third video in the i2c slave
0:15
series and today we will modify the
0:18
slave driver a little bit more
0:20
in the previous video we saw how to
0:22
receive data in a circular buffer and we
0:25
were able to receive any amount of data
0:27
the master was sending
0:29
it was a good method for receiving data
0:31
but it was hard to process the received
0:35
this is because the previous data in the
0:37
buffer is overwritten by the new data
0:39
and we should process it before it
0:43
there is a better alternative and today
0:46
we will cover it in this video
0:48
if you remember the idle line interrupt
0:51
from the uart video this will be easier
0:53
for you to understand
0:56
basically we will call function to
0:58
process the data in two different
1:02
the first situation is if the master
1:04
stops sending data but the data size is
1:07
less than the defined size
1:10
and the second is if the master sends
1:12
the data equal to or more than the
1:17
if the data size is more than the
1:19
defined size the slave will respond with
1:26
I will continue the project from where I
1:29
left in the previous video
1:31
here is the i2c source file from the
1:36
let's modify the receive complete
1:39
here the RX count variable will still
1:42
increment but the rest of the code will
1:44
only execute if the RX count is less
1:49
now since we don't want to receive more
1:51
than six bytes of data so when the RX
1:54
count is equal to 5 we will call the i2c
1:57
receive function to receive one byte of
1:59
data but with the option of the last
2:02
otherwise call the receive function with
2:05
the option of the next frame
2:10
let's understand this execution
2:14
assume that the master is sending nine
2:16
bytes of data and the slave has already
2:21
the receive complete callback is called
2:24
and the RX count will increment to 4.
2:27
it is still less than the buffer size so
2:30
this Loop will execute
2:32
since our X count is 4 this statement is
2:35
false hence the control will go in the
2:40
we will again receive one byte of data
2:42
with the option of the next frame
2:45
we have received five bytes of data and
2:50
this condition is still true
2:53
now this statement becomes true and
2:56
hence the control will come inside this
2:59
here we will receive one byte of data
3:01
with the option of the last frame
3:04
now we have received six bytes of data
3:07
and the receive complete callback will
3:11
the RX count will increment the six
3:15
since the RX count is 6 now this
3:17
condition will become false and nothing
3:20
will happen after this
3:22
so this way the reception will stop when
3:25
the six bytes have been received
3:27
I also want to add one more feature here
3:31
whenever the master sends a new data the
3:36
this way the new data will always be
3:38
stored from the beginning of the RX
3:40
buffer now let's add the option to
3:43
process the data as I mentioned the data
3:46
will be processed in two scenarios
3:49
the first one is when the slave has
3:51
finished sending six bytes of data or
3:55
so we will write another condition here
3:58
if the RX count is equal to 6 we will
4:01
call the function process data the
4:04
second scenario is when the slave stops
4:07
sending it less than six bytes
4:10
we already know that an error is
4:12
triggered in this case
4:14
so inside the error callback first we
4:17
will get the error code
4:18
and if the error code is 4 which implies
4:21
the acknowledgment failure we will
4:29
create a separate function to process
4:32
the data and so whatever you want with
4:35
here the new data is always stored at
4:38
the beginning of the RX buffer and the
4:40
received data size is equal to the RX
4:44
I will show this in the debugger let's
4:47
build and debug the code
4:53
I have prepared the terminal for sending
4:55
the data let's send four bytes of data
5:05
let me put a break point at this
5:10
all right we hit the break point
5:13
the master sent only four bytes so the
5:16
RX count variable will be equal to four
5:18
and the valid data is in the first four
5:20
bytes of the RX buffer here you can
5:23
check the call sequence and as you can
5:26
see the process function was called from
5:30
all right let's send another four bytes
5:37
notice that unlike the previous video
5:40
this time the data is stored again from
5:52
now the first two bytes were overwritten
5:55
since the RX count is equal to 2 the
5:58
valid data is the first two bytes of the
6:01
RX buffer now we will send six bytes at
6:09
the process function was called again
6:11
but this time it was called from the
6:13
receive callback function
6:19
now let's see what happens when we send
6:33
here The Knack is sent by the slave
6:36
you can see the six bytes were received
6:39
and stored in the buffer but then the
6:41
slave sent the Knack response after that
6:43
the process data function will be called
6:46
after receiving the six bytes
6:48
the i2c becomes unusable after this as
6:52
it does not automatically recover from
6:54
if you check further in the debugger you
6:57
can see the BTF flag is set the BTF flag
7:01
is set when the data transfer has
7:03
finished according to the slave but
7:05
there is still some data in the data
7:09
this is exactly what is happening in
7:12
the slave wants to receive six bytes and
7:15
it has already received it
7:17
but the master sent nine bytes so there
7:20
is still some data in the data register
7:23
we will handle these flags in future
7:27
so now you can just increase the RX
7:29
buffer size to a high value and
7:34
as long as the data sent by the master
7:36
is less than or equal to the RX buffer
7:39
size there shouldn't be any problem
7:41
this is it for the video
7:43
I hope you understood the concept
7:46
I am covering different methods of
7:48
receiving data and it's up to you which
7:51
method you want to implement
7:53
we will continue it in the next video
7:55
where we will write the memory registers
8:01
leave comments in case of any doubt
8:04
the link to download the code is in the
8:08
keep watching and have a nice day ahead