#8. SPI in STM32 using Registers || Tx and Rx || Full-duplex mode

Video Statistics and Information

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