In this guide, we shall discuss what is SPI and how transmit single byte over SPI.
In this guide, we will cover the following:
- What is SPI
- SPI pin setup
- SPI initialize
- SPI transmite function
- Result
1. What is SPI
SPI, which stands for Serial Peripheral Interface, is a standard with a very specific hardware interface. A connection is between a master and a slave, with the master typical being a processor, and the slave being a peripheral such as a sensor, flash memory device, or a modem chip. It can also be used for processor to processor communications, but in this case, an additional handshake signal is often used. There are normally four signals between a master and a slave, the first is a clock signal, and this signal is always driven by the master, regard which device is transmitting. The second line is a data line for data going from the master to the slave, and this is designated as the master output, slave input line, are MOSI for short. It connects the SPI data out connection on the master and to the SPI data in connection on the slave.
The next conductor is for data in the opposite direction and is labelled as the master input slave output line, are MISO for short. The last conductor is slave select with the slave chip selecting input, and is active low.
If you have more than one slave, with the first being perhaps a sensor of some kind, the slave will be dedicated to slave 1. If you add a second sensor, the top 3 interface line will be shared, but it dedicates for slave’s line will be required for the second device, and the same is true of course for each of additional slave device.
Most processors have a maxim of 4 slave selected lines. The four lines could be used effectively as a multiplexed address lines to access more than 4 slaves. You cannot have more than one master on the bus, since the interface is not support coordination between two masters as to which one is controlling the bus.
Transmissions are typically sent as a sequence of bytes, but without a formal protocol, there is nothing restricting communication being byte based. Typical by frame sizes, are in 8 to 32 bits range. Also note the bytes and packets are not acknowledged as they are in i2c, and you could have a master synching communicating with the slave but, you don’t really know of your communications are being received OK. However, some slave devices will echo bytes sent to it, which provides an acknowledgement to the master. it is how the data lines are synchronized with a clock signal.
Clock Polarity and Phasing
There are four different modes available, one mode each combination clocking in a low state and high state, with a data being read in a rising edge or falling edge of the clock signal. For modes 0 and 1, the clock is low in idle, which is referred to as clock and 0, For modes 2 and 3 then the clock is in high state when idle, so it has polarity , one , For modes 0 and 2, the data will be sampled by the receiving device on the leading edge of a clock signal. Relative to the idle state, which is referred to a clock phase of zero. So for mode 0, this means the rising edge of the clock and for mode 2, means the following edge of the clock, the other two modes, use a clock phase 1 which means that trailing edge of clock as a returns to an idle state. And this translates to a following edge for mode 1 and the rising edge for mode 3. Mode 0 is the most commonly supported setting. If multiple slaves in the same bus, you may have to reconfigure the settings for the master to which modes when you want to communicate with a different slave.
2. SPI pin setup:
First thing first, we need to locate the pins of SPI1 in STM32F411 Nucleo-64. We can find those information in the datasheet of our microcontrol.
From the table above, we conclude that SPI has alternate function 5 and the following pins:
Pin | SPI Function |
PA5 | SCK (Serial Clock) |
PA6 | MISO (Master In Slave Out) |
PA7 | MOSI (Master Out Slave In) |
After than we can start by enabling clock access to GPIOA as following:
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; //enable clock for GPIOA
Then setting PA5, PA6 and PA7 as alternate function mode as following
GPIOA->MODER|=GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1; //set PA5, PA6 and PA7 to alternate function mode
Then we can set which AF shall be used. From the table we find that, it is alternate function 5. Hence, the AF can be set at following
GPIOA->AFR[0]|=(0x05<<20)|(0x05<<24)|(0x05<<28);
3. SPI initialising:
We start off by locating which bus SPI is connected to. We can find this in the datasheet.
So, from the block diagram, SPI1 is connected to APB2 bus. Hence we can enable clock access as following:
RCC->APB2ENR|=RCC_APB2ENR_SPI1EN; //enable clock access to SPI1
Now, we can start configure the SPI as following:
We start by enabling software slave management as following:
SPI1->CR1|=SPI_CR1_SSM|SPI_CR1_SSI; //software slave management
Then we select the SPI in master mode as following:
SPI1->CR1|=SPI_CR1_MSTR;// set SPI as master mode
Finally we enable the SPI as following:
SPI1->CR1|=SPI_CR1_SPE; //enable SPI peripheral
4. SPI transmit function
we can transmit data as following:
while(!(SPI1->SR&SPI_SR_TXE)){;} // wait to transmision buffer to be emplty SPI1->DR= c; //Send data to SPI Data register while(!(SPI1->SR&SPI_SR_BSY)){;} //wait untill SPI bus is free
Entire code
#include "stm32f4xx.h" // Device header void spi_write(unsigned char c){ while(!(SPI1->SR&SPI_SR_TXE)){;} // wait to transmision buffer to be emplty SPI1->DR= c; //Send data to SPI Data register while(!(SPI1->SR&SPI_SR_BSY)){;} //wait untill SPI bus is free } int main(void) { //enable clock for GPIOA RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; //set PA5, PA6 and PA7 to alternate function mode GPIOA->MODER|=GPIO_MODER_MODE5_1|GPIO_MODER_MODE6_1|GPIO_MODER_MODE7_1; //set which type of alternate function is GPIOA->AFR[0]|=(0x05<<20)|(0x05<<24)|(0x05<<28); //enable clock access to SPI1 RCC->APB2ENR|=RCC_APB2ENR_SPI1EN; //set software slave managment SPI1->CR1|=SPI_CR1_SSM|SPI_CR1_SSI; //set SPI in master mode SPI1->CR1|=SPI_CR1_MSTR; //enable SPI peripheral SPI1->CR1|=SPI_CR1_SPE; while(1) { spi_write('A'); } }
5. Results:
If you have Logic Analyzer, you will be see the transmitted data is A.
Add Comment