0:09
hello everyone welcome to controllers tech this is another video in the stm 32
0:17
register based series and today we will see how to use the spi in master mode
0:23
i will cover both the transmitting and the receiving part in this video i will write the main code for the f4
0:30
controller but i will explain the changes for the blue pill also later you can download the code for both
0:38
of them before we start let's take a look at the reference manual to see some important information
0:48
as you might know sbi works with four pins meso master enslave out is used by
0:55
master to receive data from the device mossy master out slave in is used by
1:02
master to send the data to the device clock is used to keep the master and slave in sync
1:08
to make sure that the data transmission is correct and slave select pin is used to select
1:15
and unselect the slave device spi can be used in different modes
1:21
we will be using the full duplex mode as it's simpler compared to others
1:28
in full duplex mode all four pins are used and there are separate pins for transmit
1:39
whereas in half duplex mode the same line is used for transmit and receive and this makes it a bit more complex
1:46
than the full duplex mode then we have others like transmit only or receive only modes
1:54
since we are doing two-way transmission here these modes are of no use
1:59
the spi can work in multi-slave communication also here we can connect multiple slave
2:06
devices to the same master and later select any device using the
2:11
slave select pin or the nss pin
2:18
we will see rest of these in a while let's start the kyle id and set up our
2:24
program first [Music] i am using f446re here
2:37
select the cm syscor and device header files
2:44
let's add the main file to this project
2:51
and add the main function and a while loop
2:59
first of all we need to copy the clock setup and the delay setup libraries
3:05
i have already covered these you can check out the first two videos in the playlist
3:11
let's copy them inside the project
3:16
folder we also need to include them in our
3:38
and finally include them in the main
3:44
file this is it for the basic setup now let's
3:51
write a function to configure the spi
3:59
here are the steps to do so we enable the clock and configure the
4:04
control registers one and two this is fairly easy
4:09
compared to the i2c or uart let's start by enabling the spi clock
4:16
to do that we need to check the rcc registers
4:23
here the 12 bit of rcc apb b2 enable register controls the spi-1 clock
4:33
let's write a 1 in the 12th position to enable the spi-1 clock
4:38
next we need to configure the control register one
4:51
here we have the spi control register one let's start by configuring the zeroth
5:03
the zeroth bit configures the clock phase and the first bit configures the clock polarity
5:09
what do these things mean to understand this let's check some description in the
5:15
manual this here is the timing diagram for the
5:21
spi basically the clock polarity means how you want to set the idle clock
5:28
if it is set to polarity one the clock will be ideally high and if set to zero the clock will be
5:34
ideal low and similarly the clock phase is responsible for the where the sampling
5:41
starts notice here that when phase is 1 and polarity is 1
5:47
the data sampling starts at first rising edge and afterwards the data will be always
5:53
sampled at every rising edge of the clock when phase is 1 and polarity is 0
5:59
then the data is sampled at the first falling edge and this will continue similarly these
6:05
are the conditions for the phase zero the settings for polarity and phase depends on the slave device
6:14
in this video i am going to use adxl 345 as a slave device for the
6:20
demonstration i have covered this device in my whole tutorials and this article is one of them here is
6:28
the picture from the data sheet of the adxl as you can see its requirement is that
6:34
the clock polarity and clock phase must be one
6:43
so we will set the bits zero and one to one
6:59
next we have the master mode selection obviously we want the master mode
7:04
so set the bit 2 as 1.
7:13
then we have the board rate control bits here we can choose the board rates as
7:18
per the settings given
7:24
according to the adxl data sheet we can choose the maximum board rate of 5
7:29
megahertz i am using spi-1 which is connected to
7:46
with the default setup this apb bus is running at 90 megahertz
7:52
if i change this system clock to 160 megahertz the ap b2 peripheral clock will come
7:58
down to 80 megahertz and using this prescaler of 16 the baud
8:04
rate can be 5 megahertz to make these changes we need to go to
8:21
and to make that change we will change the value of this plln
8:30
if you pay attention to the clock setup the crystal frequency gets divided by 4 and later by 2 this makes it 1
8:38
megahertz so whatever plln value we use the same will be the system clock
8:44
frequency so i will set it as 160.
8:53
now our apb2 peripheral clock is running at 80 megahertz and to keep the spi 1 clock at 5
8:59
megahertz we will use this prescaler of 16. so we will write 3 in the third position
9:15
next we have the spi enable bit which we will set later in the program
9:21
now configure the frame format here we need to send the msb
9:26
first this is again as per the device requirement
9:33
so we will set the bit 7 as zero
9:42
now comes the ssm and ssi bits software slave management we need to see
9:50
the explanation before configuring these bits here is the nss pin management
9:58
basically what happens is if we set the software slave management to 1 the master can control the slave using
10:05
any i o pin but if the software slave management is off then we must use the nss pin to select
10:12
and unselect the slave device here i will use this software slave
10:18
management and later set any pin as the slave select pin so i am going to keep the ssm bit high
10:26
and ssi bit high 2. let's write 1 to the 8th and 9th positions
10:42
next we have the rx only bit this bit sets the full duplex mode then we have
10:49
data frame bit and here we will use the 8-bit data format next we have set up for the crc which we
10:57
will not be touching today
11:06
and at last these bits are used to configure the receive only mode or transmit only mode
11:18
since we are using full duplex and 8-bit data format we will keep the bit 10 and 11 as zeros
11:40
now let's check the control register 2.
11:46
here we have the configuration for dma and the interrupt since we are not using them we will keep
11:53
the control register 2 as 0.
11:58
this completes the spi configuration this spi configuration remains same for
12:04
the blue pill also now let's configure the gpio
12:10
i am going to use pa5 for the clock pa6 for the miso a7 for the mossy and pa 9 for the slave
12:22
select so let's enable the clock for the gpioa
12:32
it's the 0th bit of ahb1 enable register
12:56
now let's go to the gpio registers and start with the first one that is
13:02
mode register here we will choose the alternate function mode to the pin 5
13:08
pin 6 and pin 7. and the output mode for the pin 9 that's
13:15
the slave select pin here we have to write a 2 in the 10th
13:23
12th and 14th positions also write a 1 in the 18th position
13:29
for the pin pa 9 in the output mode
13:41
next one is output type we will leave it to push pull which is the reset value anyway
13:50
then we have to select the speed we will keep them at very high speed
13:56
again modify all the four pins and write a 3 in the 10th 12th 14th and 18th positions
14:19
we don't need to set up anything here
14:32
now comes the alternate function register we need to first check which alternate
14:38
function is needed to set the spi
14:44
here we have alternate function 5 for the spi-1
14:51
since we are using pins 5 6 and 7 we will be using the af are low register
14:58
if you use pins 8 to 15 you have to use afrh register
15:10
so we will use alternate function 5 for all three pins and to do that we will write a 5th to
15:17
20th 24th and 28th positions
15:30
this is it for the gpio configuration this configuration is slightly different
15:36
in blue pill so let's see that one too
15:43
this is the reference manual for heblew pill same spi pins are used here
15:52
this table 25 directs the configuration for different spi pins as we are configuring the master
16:00
mode we will see the gpio configuration for the same
16:05
the clock pin needs to be configured as alternate function push-pull
16:12
similar configuration for the mossy pin whereas the miso should be set up as the
16:17
input floating or input pull-up
16:23
let's see the registers now for the pin numbers up to 7 we will use
16:31
the configuration register low this register uses four bits to configure each pin
16:38
the first two bits are used for mode and the next two are used for configuring that mode
16:43
pin pa5 is the clock pin and it should be set as alternate function push-pull
16:49
so first we will set it mode as output with maximum speed and then in the
16:55
configuration bits we will configure it as alternate function output push-pull
17:03
here you can see the blue pill code first enable the gpio clock
17:10
before modifying the configuration register we must set it to zero
17:17
that's because its reset state is the floating inputs so this zero one will also get added to
17:24
our values now we will shift the 11 by 20 positions
17:31
[Music] this 11 is because we are writing 1 0 1
17:38
1 in these positions
17:44
similar configuration will be used for mossy pin
17:57
for me so we need to select the input mode
18:10
so basically we just have to write a 1 in the 26th position
18:23
and finally i am using pa4 as the output for the slave select pin
18:31
the s-p-i configuration is same as what we covered in the f-4
18:37
let's go back to our s-p-i functions now
18:43
to enable the spi we will write one in the sixth position in the control
18:48
register one i am defining a new function for this and for disabling the spi we will just
18:56
clear that bit in order to select the slave we have to reset the pin
19:01
pa 9 and to release the slave we will set the pin now let's write a
19:08
function to transmit the data this function will take the pointer to the data
19:13
and the size of the data here are the steps we will wait for the txe bit to set in
19:20
the status register and then transmit the data
19:26
let's check this status register
19:38
bit represents transmit buffer empty if this bit is set it indicates that
19:44
there is no data in the transmit buffer and only then we can load the data into this buffer
19:53
so first we will wait for this bit to set and once it is set we will load the data
20:00
into the data register this data will then be transmitted to the shift register
20:05
and the txe bit will again set we need to perform this before sending each byte
20:20
something important is given here after we write the last data byte there is some small delay before the
20:26
busy flag is set and therefore we must wait for the txe bit to set before checking this busy flag
20:46
so we will wait for the txe bit to set and wait for the busy flag to reset
20:58
this busy flag is 7th bit in the status register and it will be set if the spi is busy
21:12
this now is a very important step remember whenever we send the data to
21:17
the spi it sends something in return this is not acknowledgement
21:23
but some random data so before we go on receiving data from the
21:29
slave device we must make sure that the data register is empty
21:34
to do that we will perform a dummy read from the data register also if the overrun flag is set during
21:41
this reception we need to clear it by reading the status register
21:46
this completes the transmit function let's test this part first
21:53
in the main function initialize the clock configure the gpio
22:01
configure the spi enable the spi
22:06
let's define a variable with 3 bytes of data
22:13
spi transmit will transmit these three bytes of data let's build it now
22:21
go to the options and configure the controller
22:43
i have here the logic analyzer set up in the spi mode
22:50
i think i forgot to enable the chip select
22:59
ok select the slave transmit the data and release the slave
23:21
you can see here this spi is configured as per my setup
23:33
let's enable the recording here run the program now
23:39
this should be enough it is showing
23:50
mismatch but just ignore that you can see the chip select pin goes low before the
23:56
transmission and then goes back high afterwards
24:02
as i told you in the beginning when the clock phase is 1 and clock polarity is 1 the data will
24:09
start sampling at the first rising edge this is the first edge where the
24:14
sampling will start i am going to put these markers in every rising edge
24:21
here we have zero zero zero zero zero zero one zero this is the first byte
24:27
transmitted by the master this corresponds to zero cross zero two
24:35
which is exactly what we sent
24:40
then these bytes will be transmitted the second byte is zero zero one one
24:47
zero zero one zero which is zero cross three two and then the last byte we have
24:54
is zero cross f six the chip select is low the entire time
25:02
also the clock goes to high after the data transmission is finished
25:08
this is because we have selected the clock to be ideal high c pole equals to 1.
25:15
that's how the data is transmitted to the slave device and this part of the program works well
25:25
now let's write another function to receive the data from the slave device
25:32
this function will also take two parameters the pointer to the data and the size of
25:38
the data that you want to receive here are the steps to do so first we
25:44
will wait if the spi is busy in communication then we will send
25:49
some dummy data this dummy data is an important step any spi device only
25:56
transmits data after receiving something so we must send some random data to the
26:01
device now after we send the dummy the ixne flag will be set
26:12
this basically indicates that there is some data in the buffer
26:20
now we will read the data from the data register
26:29
this is all for the spi related functions now we need some device from where we
26:35
can read some data i am going to use the adxl accelerometer
26:48
these are some functions for the adxl from my earlier tutorials so i am going to modify them
26:57
all we need to do here is change the whole function with our own
27:10
similarly modify the function for adxl [Music]
27:24
we are going to receive 6 bytes from this device
27:32
let's initialize the adxl
27:47
inside the while loop we will read the data from this device i am going to add some delay also
27:54
this delay won't be accurate since i have changed the main clock but this is not the priority right now
28:02
let's build it and see the result on the
28:21
this much time should be enough
28:28
here we have our result as you can see the data is being received every few
28:34
milliseconds let's check this data now [Music]
28:43
as you can see here we are sending zero cross three one and zero cross zero one
28:49
this data is not clear like i said in the beginning scope is not able to read this first
28:55
byte properly anyway let's check the second byte the transmitted bytes are 0 cross 6 d
29:02
and 0 cross 0 0. this is because we are sending 0 cross 2 d which gets added with 0
29:15
we are sending the same thing again but with zero cross zero eight
29:26
our transmit part is okay let's see the receiving part
29:35
we send the address along with some bytes from the device settings to indicate the read operation
30:00
now you can see as we are sending the dummy byte the device is responding with some data
30:23
this process will keep repeating every few milliseconds
30:31
so our device is working perfectly
30:45
i will include some more calculations from the previous code to check the data in the id itself
31:06
here you can see the values from the accelerometers are being shown
31:11
this is actually acceleration in the x y and z axis this video is not about
31:17
accelerometer so i am not explaining this just check the old video
31:23
where i have explained this in detail so this is it for the spi
31:31
we were able to transmit and receive the data from the device you can use many spi devices with a
31:38
single master controlling one device at a time
31:44
to download the code for f4 and blue pill go to the link in the description leave
31:50
comments in case of any doubt keep watching be safe and have a nice