Working with STM32F7 and SPI (Serial Peripheral Interface): Send a byte

pastedImage_0.png

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.

pastedImage_0.png

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.

pastedImage_1.png

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.

pastedImage_2.png

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
PA5SCK (Serial Clock)
PA6MISO (Master In Slave Out)
PA7MOSI (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

Now, we need to set the SPI data width to 8-bit, and this can be achieved from CR2 as following:

SPI1->CR2|=(0x07<<8);

And finally, we can enable the SPI peripheral:

SPI1->CR1|=SPI_CR1_SPE; //enable SPI peripheral 

4. SPI transmit function

we can transmit data as following:

void spi_write ( uint8_t c){
	while(!(SPI1->SR&SPI_SR_TXE)){;} // wait to transmision buffer to be emplty
		
	*(volatile uint8_t *)&SPI1->DR=c;
 		while((SPI1->SR&SPI_SR_BSY)){;}
}

Note: we need to type cast the SPI1->DR register to be 8-bit length

Hence, the code is as following:

#include "stm32f7xx.h"                  // Device header
#include "stdint.h"
void spi_init(void);

void delay(int ms);




void spi_write ( uint8_t c){
	while(!(SPI1->SR&SPI_SR_TXE)){;} // wait to transmision buffer to be emplty
		
	*(volatile uint8_t *)&SPI1->DR=c;
 		while((SPI1->SR&SPI_SR_BSY)){;}
}



int main(void){
	

spi_init();
	
while(1)
{


spi_write(0x41);
delay(100);
}

}


void spi_init(void)
	
{
RCC->AHB1ENR|=RCC_AHB1ENR_GPIOAEN; //enable clock forn gpio a
RCC->APB2ENR|=RCC_APB2ENR_SPI1EN; //enable clock for spi1
//GPIOA->MODER=0;
GPIOA->MODER|=GPIO_MODER_MODER5_1|GPIO_MODER_MODER6_1|GPIO_MODER_MODER7_1;
GPIOA->AFR[0]|=(0x05<<20)|(0x05<<24)|(0x05<<28);
SPI1->CR1=0;
SPI1->CR1|=SPI_CR1_SSM|SPI_CR1_SSI|SPI_CR1_MSTR; 
SPI1->CR2|=(0x07<<8);
SPI1->CR1|=SPI_CR1_SPE;


}


void delay(int ms)
{
	SysTick->LOAD=16000-1;
	SysTick->VAL=0;
	SysTick->CTRL=0x5;
		for (int i=0;i<ms;i++)
		{
			while(!(SysTick->CTRL &0x10000)){}
		}
	SysTick->CTRL=0;
}

5. Results:

Once you upload the code, and connect an logic analyzer, you should get this

Happy coding 🙂

Add Comment

Your email address will not be published. Required fields are marked *