Skip to main content
kriscicluna
Associate
February 4, 2017
Question

SPI Master-Slave Interface STM32

  • February 4, 2017
  • 1 reply
  • 482 views
Posted on February 04, 2017 at 16:52

Hi All, I am trying to interface two STM32 discovery boards one as master and one as slave using the following code snippets. Sometimes the data seems to arrive correctly at other times it is corrupted. I just call the mySPI_SendData and mySPI_GetData functions from the main loop. Any helps with this issue? Thanks.

void mySPI_SendData(uint8_t adress, uint16_t data){

GPIO_ResetBits(GPIOA, GPIO_Pin_15);

while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE));

SPI_I2S_SendData(SPI3, data);

while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE));

SPI_I2S_ReceiveData(SPI3);

 

GPIO_SetBits(GPIOA, GPIO_Pin_15);

}

uint16_t mySPI_GetData(uint16_t adress){

while ((SPI_NSS_bit == 1)|(SPI_NSS_bit == -1))

{

SPI_NSS_bit = GPIO_ReadInputDataBit(GPIOA, GPIO_Pin_15);

}

while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE));

SPI_I2S_ReceiveData(SPI3); //Clear RXNE bit

while(!SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE));

SPI_I2S_SendData(SPI3, 0x0000); //Dummy byte to generate clock

return SPI_I2S_ReceiveData(SPI3);

}

void mySPI_Init(void){

RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);

SPI_InitTypeDef SPI_InitTypeDefStruct;

SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;

SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Slave;

SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_16b;

SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_High;

SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_1Edge;

SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;

SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_256;

SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;

SPI_Init(SPI3, &SPI_InitTypeDefStruct);

RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOE , ENABLE);

GPIO_InitTypeDef GPIO_InitTypeDefStruct;

/*-------- Configuring SCK PC10, MISO PC11, MOSI PC12 --------*/

GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_10 | GPIO_Pin_11 | GPIO_Pin_12;

GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_AF;

GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;

GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;

GPIO_Init(GPIOC, &GPIO_InitTypeDefStruct);

GPIO_InitTypeDefStruct.GPIO_Pin = GPIO_Pin_15;

GPIO_InitTypeDefStruct.GPIO_Mode = GPIO_Mode_IN;

GPIO_InitTypeDefStruct.GPIO_Speed = GPIO_Speed_50MHz;

GPIO_InitTypeDefStruct.GPIO_PuPd = GPIO_PuPd_UP;

GPIO_InitTypeDefStruct.GPIO_OType = GPIO_OType_PP;

GPIO_Init(GPIOA, &GPIO_InitTypeDefStruct);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource10, GPIO_AF_SPI3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource11, GPIO_AF_SPI3);

GPIO_PinAFConfig(GPIOC, GPIO_PinSource12, GPIO_AF_SPI3);

//GPIO_PinAFConfig(GPIOA, GPIO_PinSource15, GPIO_AF_SPI3);

//GPIO_SetBits(GPIOA, GPIO_Pin_15);

SPI_Cmd(SPI3, ENABLE);

}

#stm32f4-discovery
This topic has been closed for replies.

1 reply

Tesla DeLorean
Guru
February 4, 2017
Posted on February 04, 2017 at 23:49

Corrupt the data how exactly?

I'm not going to muddle through mixed snippets of code. Provide complete code for both ends, and examples of data transmitted vs received.

The SPL should have examples for SPI-to-SPI

Tips, Buy me a coffee, or three.. PayPal VenmoUp vote any posts that you find helpful, it shows what's working..