What’s SPI Interface
Serial communication protocol initially developed by Motorola that permits communication involving any digital device which supports clocked sequential streams. SPI utilizes a master-slave way of communicating that permits high-speed information flowing.
Instead of only using just two wires, SPI should utilize at least 4 screws. As there may be several slave devices in an SPI execution, the true number of hints or wires utilized would be +3 where n the amount of slave devices in usage.
A few extra Importantdetailsabout SPI before we proceed:
SPI is synchronous
SPI is a simple protocol using small overhead
SPI affirms full-duplex communicating
SPI communicating Doesn’t Have a way to acknowledge mentor leak controller
SPI Doesn’twork much board space
Full-duplex communicating, both utilize”sequential” communicating in some manner, and they’re both just acceptable for short-distance use instances, there aren’t that many similarities outside that. Thus, what would be the gaps between UART versus SPI? The solution is that there are numerous; we shall cover lots of these here.
Gear vs routine
Among the biggest gaps in thisis, a kind of hardware whereas SPI is a routine protocol. Wheneveryou’rehandling the principles of getting things to function within an embedded system, this is sometimes easy to forget about. But, UART is a genuine piece of hardware (that a microchip) while SPI is a converter or protocol for communicating.
Quantity of hooks
UART uses two hooks while SPI When designing a method that means that when pins/traces are infrequent, SPI might not be an appealing alternative.
Amount of devices which can Be linked
A corollary to the Number of hooks, UART, ImplementingjustTx, and Rx for communicating communication, is effectively confined to 1 1 communication. SPI, on the flip side, can leverage its own master/slave paradigm to empower a lot of communications.
SPI is considered ably faster compared to UART. Sometimes, an SPI alternative maybe three times faster compared to the usual UART alternative.
In almost any engineering undertaking, the Price Of given alternatives a large driver of alternative. Broadly, SPI is less costly than UART.
Generally, SPI apparatustakeUpcomparatively less distance than UART processors. ThisimpliesuseinstanceswhereThere’slimitedboardspacemight be better served with SPI.
SPI communication with STM32F103RB
The data transfer rate for the STM32F103RB microcontroller 18 Mb s. For this case, the SPI1 interface will be used. The STM microcontroller has the hardware pins SPI_SCK, SPI_MISO, SPI_MOSI. You should find in the technical documentation of the product where these pins are located. The pin configuration in this model is as follows:
PA5 – SPI1_SCK
PA6 – SPI1_MISO
PA7 – SPI1_MOSI
The CS line is responsible for the hardware control of the communication line. In this model, the CS pin is placed on the PA4 line. However, in this configuration example for the Master, it is enough to send the data frame with logical 0 at the start of transmission and logical 1 at the end of the data frame. Therefore, the normal GPIO transmission line will be used in this example. Thisconfigurationwillallowmorethan one device supporting SPI in the Slaveconnectiontype to be connected to the system. Hardware control for CS transmission is more convenient for the implementation of devices in the slave type.
SPI initialization for STM32
The first step in initializingis running the clocks.
The second step is to set the pin for a specific microcontroller model.
gpio.GPIO_Pin = GPIO_Pin_5|GPIO_Pin_7; // SCK, MOSI
gpio.GPIO_Mode = GPIO_Mode_AF_PP;
gpio.GPIO_Speed = GPIO_Speed_50MHz;
The control mode was set to push-pull, i.e. similar to the UART interface for the TX transmission line. The line’s sampling speed was set to 50 MHz. The default setting is 2MHz. Thisistoolowfor a data transfer value is available for this model at 18 Mb/s.
The next configuration point is the MISO line setting, which is the input line for SPI communication. The configurational so resembles that of the UART interface for the RX line.
|gpio.GPIO_Pin = GPIO_Pin_6; // MISO|
gpio.GPIO_Mode = GPIO_Mode_IN_FLOATING;
The last setup point is to define the CS line. In thisexample, software controlisused, soany STM microcontroller PIN isselected, whichiscurrentlyfree.
|gpio.GPIO_Pin = GPIO_Pin_0; // CS|
gpio.GPIO_Mode = GPIO_Mode_Out_PP;
SPI communication initialization was completed by setting a logical value of 1 for the CS line. As a result, the CS line remains inactive, activation will take place after issuing the logic signal 0.
SPI first run
After the SPI interface initialization process for hardware pins, you can go to the SPI interface startup process. The process of starting the SPI interface is as follows:
- Definition of the SPI_InitTypeDefvariable
- Initialize to default values with SPI_StructInit
- Setting the configuration of the SPI interface
- Calling the SPI_Initinterface
- Setting the operatingmode in thiscase, SPI_Mode_Master
- Selection of the SPI_NSS_Soft program mode
- Using a divider 16 to adjust the sampling rate to 4 MHz from a base value of 64 MHz
spi.SPI_Mode = SPI_Mode_Master;
spi.SPI_NSS = SPI_NSS_Soft;
spi.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_16;