0:09
after receiving a lot of requests I am
0:12
starting a new tutorial series covering
0:15
how to use the stm32 was ai2c slave
0:19
the slave mode is harder to use compared
0:24
here the device is always looking for
0:26
the data and it should be able to handle
0:28
the transmit and receive requests
0:31
the amount of data sent by the master is
0:34
unknown so the slave should be able to
0:37
it should also take care of different
0:39
errors that could arise during
0:43
we will cover everything that I just
0:46
it is not possible to cover everything
0:48
in one video so I have decided to make a
0:52
we will take small steps in each video
0:55
and by the time the series ends we
0:58
should have a slave device ready for
1:01
let's start the queue IDE and create a
1:09
sdm-32f103c8 controller
1:11
give some name to the project and click
1:15
the clock setup will be as usual
1:18
the system is clocked by the external 8
1:20
megahertz Crystal and it is running at
1:26
let's configure the i2c now
1:31
the mode is kept to standard and the i2c
1:34
clock is at 100 000 Hertz
1:37
everything is kept to default for now
1:40
the address length is set to 7-bit
1:43
the Dual address allows the device to
1:46
have two different addresses and
1:48
communicate with two different Masters
1:50
it is disabled for now
1:53
the address of the slave let's set it to
1:57
remember that this is a 7-bit address
2:00
we will talk about the clock stretching
2:02
and general call address in another
2:06
today we are just focusing on the
2:10
go to the nvic tab and enable the event
2:16
so this is it for the configuration
2:18
let's generate the project now
2:22
let's create a new source file for the
2:24
i2c slave where we will write all the
2:27
functions needed for the device
2:31
also create a header file just in case
2:34
required in the future videos
2:39
now copy the i2c handle type from the
2:44
include the main file and the i2c header
2:49
Now define the i2c handle as the
2:53
I am defining the receive buffer size as
2:56
6 bytes and creating a buffer to save
3:02
let's take a look at the functions
3:04
available to i2c slave
3:16
here we have the i2c enable listen
3:18
function in the interrupt mode
3:21
it is used to enter the slave device in
3:24
the listen mode where it can check for
3:26
any incoming requests by the master
3:29
we will call this inside the main
3:32
once the slave receives some request
3:35
from the master an interrupt will
3:37
trigger and the listen complete callback
3:40
let's write this callback function in
3:43
ri2c slave source file
3:45
here we will simply put the device in
3:48
the listen mode again so that it can
3:50
again look for the request from the
3:53
we have another callback function here
3:57
it is called when the address by the
4:00
master matches the slave address we set
4:03
the parameters of this function are the
4:06
i2c instance the transfer Direction
4:08
which basically determines if the master
4:10
is requesting a read or a right and the
4:15
for now we will just focus on receiving
4:20
so first check if the transfer direction
4:22
is the transmit Direction
4:25
that would mean the master is
4:27
transmitting the data to the slave and
4:29
not requesting from it
4:31
if the master wants to transmit data the
4:34
slave will receive it and save it in the
4:37
to receive the data we will use the
4:42
here I am using the i2c sequential
4:45
receive in the interrupt mode
4:48
the first parameter is the i2c instance
4:51
so we can just use this one
4:54
then we have the buffer the data size
4:56
and at last the transfer option
5:03
the transfer option I am using is i2c
5:08
and if the transmit direction is such
5:10
that the master is requesting the data
5:13
from the slave call the error Handler
5:16
for the first few videos we will just
5:19
focus on receiving the data
5:21
let's see the transfer option it plays
5:24
the most important role while using the
5:28
you can check their description in the
5:30
whole i2c source file
5:33
here are the different transfer options
5:36
and they are used with the sequential
5:41
for today's video we are using the first
5:45
this basically means there is no
5:47
sequential operation and the function is
5:50
just being used normally as in a
5:54
here the last frame indicates that the
5:57
slave will send a neck response after it
5:59
has received six bytes
6:01
as long as the master sends six bytes or
6:04
less the slave will happily receive the
6:08
but when the master sends more than six
6:10
bytes at once an error will trigger in
6:14
we will talk more about the errors in
6:19
we have other frames here and the
6:21
important ones for the receiving
6:23
purposes are the first frame the next
6:25
frame and the last frame
6:28
we will see them in the upcoming videos
6:32
since we are receiving the data in the
6:34
interrupt mode once the slave has
6:37
finished receiving six bytes an
6:39
interrupt will trigger and the slave
6:41
receive complete callback will be called
6:44
here we don't need to do anything
6:46
special today so let's just increment
6:50
if there is some error while receiving
6:52
the data the error callback will be
6:56
we are not talking about errors in
6:58
today's video so let's just enable the
7:01
listen mode again here
7:03
all right let's build the code
7:07
if you take a look at the initialization
7:09
function the device's own address it's
7:13
we set the 7-bit address 12 hex which
7:16
was then converted to 8-bit address by
7:18
Shifting the address to the left by one
7:21
so that is it for the code let's debug
7:26
I have added the RX data to the live
7:29
expression so that we can see the buffer
7:33
for the master I am using the cjmc UFT
7:38
you can simply use another MCU as the
7:41
master and send the data as shown in the
7:44
picture the device address is 12 hex
7:47
shifted to the left by 1 bit
7:49
and we will just call the whole i2c
7:52
Master transmit function to transmit the
7:54
data anyway I am using a master device
7:57
and I have the python script to send the
8:04
this here is the slave address which is
8:14
i2c Wright simply transmits the data and
8:17
here I am transmitting 4 bytes
8:22
the slave has successfully received the
8:25
data and stored it in the buffer now I
8:28
am transmitting two bytes
8:31
you can see it is also received by the
8:40
here we have transmitted six bytes
8:44
let's add the count variable to the live
8:48
it's set to 1 because I transmitted six
8:52
whenever the slave receives the set
8:54
number of bytes the slave receive
8:56
complete callback is called and the
8:58
count variable is incremented
9:00
this is a very important piece of
9:04
since we are set the transfer option to
9:07
be the first and last frame when the
9:09
slave receives six bytes it will trigger
9:11
the last frame option and it will send a
9:14
neck response to the master indicating
9:16
that it does not want to receive more
9:19
subsequently the receive complete
9:23
if the master sends less than six bytes
9:26
the last frame is never triggered and
9:28
slave never sends a neck response also
9:31
the receive complete callback is never
9:34
let's see what happens when the master
9:36
sends seven bytes of data
9:40
here you see the master didn't receive
9:42
any neck response from the slave
9:45
this is because the whole Library can
9:47
handle one extra byte received given
9:49
that it is compensated in the next
9:53
anyway let's not go into this right now
9:55
as we will talk about errors later
9:59
basically if the master sends more than
10:01
expected data the slave will send a
10:05
so to keep the things working for now is
10:08
to program the master to send less data
10:10
than what the slave is expecting
10:13
of course we will take care of this in
10:18
and we will also discuss the errors
10:20
which arise when the slave receives less
10:24
everything will be covered but one step
10:28
so this is it for today's video
10:31
I hope you understood some basic things
10:34
about writing the i2c slave code
10:37
we will continue this in the next video
10:41
the link to download the code is in the
10:45
leave comments in case of any doubt
10:48
keep watching and have a nice day ahead