0:12
this is the fifth video in the
0:15
stm32i2c slave series and as mentioned
0:18
in the previous video today we will
0:20
program the slave to store the data in
0:23
we will Define some registers for the
0:26
slave and the master will request the
0:28
data to be written in a particular
0:29
register or it can also write multiple
0:32
data bytes starting from a particular
0:36
we will start working with the code from
0:40
here you can see the i2c slave source
0:43
file contains the same code where we
0:47
let's define 10 registers for the slave
0:51
I am initializing the registers with the
0:56
the rest of the code will remain
0:59
when the address match callback is
1:01
called the slave will request for one
1:06
and when this one byte data is received
1:09
the slave receive callback is called
1:12
here if the slave is receiving the last
1:15
bite we will call the receive function
1:17
with the option of the last frame
1:20
otherwise it is called with the option
1:24
if the RX count is equal to the buffer
1:26
size the slave cannot receive any more
1:29
data and we will simply call the
1:31
function to process the data
1:33
everything I mentioned just now has been
1:36
covered in the part 3 video so if you
1:38
don't understand please watch that video
1:42
now we will write the function to
1:46
the start register number is stored in
1:48
the first byte of the RX buffer
1:51
the total number of registers the master
1:53
wants to write is equal to the RX count
1:58
the RX count is equal to the total
2:00
number of bytes received in the RX
2:03
this also contains the first byte which
2:06
is the register address
2:09
that's why we subtract 1 from the RX
2:11
count and this gives us the total data
2:14
bytes in the RX buffer
2:16
if you want to program the master you
2:18
can use the function i2c mem right to
2:21
write the data into the registers
2:31
the six represents the start register
2:33
one represents that the start register
2:36
size is one byte long
2:38
the TX data contains the data Master
2:43
4 is the total number of bytes the
2:45
master wants to write
2:47
so this function will write the four
2:49
bytes of data into the slave registers
2:52
starting from the register 6.
2:55
when the slave receives this request the
2:58
first byte of the RX buffer will contain
3:02
this 4 will be the number of registers
3:06
now calculate the address of the end
3:09
this is equal to the start register plus
3:12
the number of registers minus 1.
3:16
the minus 1 is because the register
3:18
address starts from zero
3:21
there are a total of 10 registers whose
3:24
address varies from 0 to 9.
3:26
since 9 it is the last register if the
3:29
end register is exceeding 9 we will call
3:34
let's define the index variable with the
3:38
this will keep track of how many bytes
3:40
has been read from the RX buffer and it
3:43
starts with one because the zeroth byte
3:45
contains the register address
3:51
now inside the for Loop we will copy the
3:54
data from the RX buffer to the i2c
3:57
the loop will repeat as many times as
4:00
the number of registers we are updating
4:04
since we have 10 registers in total
4:06
let's update the RX buffer size to 11.
4:10
that's it let's build and debug the code
4:13
now I have added the register database
4:16
in the live expression
4:19
the right to function is the same as the
4:22
memory write function I explained
4:25
the first byte is the register address
4:27
and then we have the data bytes
4:30
so here the master wants to write five
4:33
bytes of data starting from the register
4:35
4. here at the RX buffer you can see
4:39
that the first byte received is the
4:41
register address and then the next five
4:43
bytes are the data bytes
4:45
the error counter is incremented as
4:48
there was an error because the slave
4:49
received less than 11 bytes
4:52
the i2c registers has been updated with
4:55
the new data starting from register 4
4:57
and all the way up to register 8.
5:01
let's try sending the data starting from
5:07
here you can see the updated registers
5:10
if we want to update all the registers
5:13
at once we can send the 10 bytes of data
5:15
starting from register 0.
5:19
you can see the updated registers in the
5:23
let's say I want to send five bytes of
5:26
data starting from the register 6.
5:31
we will go into the error Handler in
5:34
this is because the end register value
5:36
will be 10 and it exceeds 9.
5:39
so if you want this to work properly the
5:42
master should send the data as per the
5:44
register defined in the slave
5:47
if the data exceeds the available
5:48
registers the error Handler will be
5:53
this is it for the video
5:55
I hope you understood how to store the
5:57
data in the memory of a slave device
6:00
in the next video we will see how the
6:03
slave will respond to the read query
6:07
the link to download the code is in the
6:09
description of the video
6:11
leave comments in case of any doubt
6:14
keep watching and have a nice day ahead