AnsweredAssumed Answered

SPI with two Boards

Question asked by kassner.marcel on Aug 12, 2015
Latest reply on Aug 18, 2015 by Amel N
Hello,

I've two STM32F4 Boards which I want to send data to each other via SPI. I think that I configured the master board (STM32F411 Nucleo board) well, because at the scope, I "saw" the sended data.

My problem is the receiving of this data with the SPI slave (STM32F429 Discovery board).  I want to receive the data at first in the main loop without an interrupt. 
My questions: is the CS-pin well declared and connected with SPI interface? What is the typical sequence to receive data, which flag should I check? (I read a lot of examples and every example uses another flag, is there an overview about this SPI flags?)

This is my slave-code:
// SPI3 SCK
#define SPI3_SCK_PinSource GPIO_PinSource10
#define SPI3_SCK_Pin GPIO_Pin_10
#define SPI3_SCK_Bank GPIOC
// SPI3 MISO
#define SPI3_MISO_PinSource GPIO_PinSource11
#define SPI3_MISO_Pin GPIO_Pin_11
#define SPI3_MISO_Bank GPIOC
// SPI3 MOSI
#define SPI3_MOSI_PinSource GPIO_PinSource12
#define SPI3_MOSI_Pin GPIO_Pin_12
#define SPI3_MOSI_Bank GPIOC
// SPI3 CS
#define SPI3_CS_PinSource GPIO_PinSource15
#define SPI3_CS_Pin GPIO_Pin_15
#define SPI3_CS_Bank GPIOA

This is the SPI.c:
01./*
02.**
03.**                           SPI3.c
04.**
05.**
06.**********************************************************************/
07./*
08.   Device:     STM32F429 Discovery
09. 
10.**********************************************************************/
11.#include "stm32f4xx_conf.h"
12.#include "SPI3.h"
13.#include "definitions.h"
14. 
15. 
16. 
17.void SPI3_Slave_init()
18.{    /* Clocks für SPI3 enablen*/
19.    RCC_APB1PeriphClockCmd(RCC_APB1Periph_SPI3, ENABLE);
20.    /* Definitionsstruct */
21.    SPI_InitTypeDef SPI_InitTypeDefStruct;
22.    // Baudraten Prescaler gibt Geschwindigkeit an
23.    // 2 = schnellste
24.    SPI_InitTypeDefStruct.SPI_BaudRatePrescaler = SPI_BaudRatePrescaler_8;
25. 
26.    // Richtung der Datenverbindung
27.    SPI_InitTypeDefStruct.SPI_Direction = SPI_Direction_2Lines_FullDuplex;
28.    //Master oder Slave
29.    SPI_InitTypeDefStruct.SPI_Mode = SPI_Mode_Slave;
30.    //Größe der Datenpakete - 8 oder 16 bit
31.    SPI_InitTypeDefStruct.SPI_DataSize = SPI_DataSize_8b;
32.    //ChipSelect durch Hardware oder Software
33.    SPI_InitTypeDefStruct.SPI_NSS = SPI_NSS_Soft;
34.    //Reihenfolge der Daten - Little Endian oder Big Endian (LSB oder MSB)
35.    //Sollte bei beiden Endgeräten identisch sein
36.    //Wikipedia: Byte-Reihenfolge
37.    SPI_InitTypeDefStruct.SPI_FirstBit = SPI_FirstBit_MSB;
38.    //Clock Polarity --> Muss bei beiden Endgeräten identisch sein
39.    SPI_InitTypeDefStruct.SPI_CPOL = SPI_CPOL_Low;
40.    //Definiert Flanke, ab wann Daten empfangen werden soll (1st oder 2nd Edge)
41.    SPI_InitTypeDefStruct.SPI_CPHA = SPI_CPHA_2Edge;
42.    //Wird im Beispiel nicht angewendet
43.    //SPI_InitTypeDefStruct.SPI_CRCPolynomial = ;
44. 
45. 
46.    //SPI initialisieren
47.    SPI_Init(SPI3, &SPI_InitTypeDefStruct);
48. 
49.        /* Clocks für GPIOs enablen*/
50.    RCC_AHB1PeriphClockCmd(RCC_AHB1Periph_GPIOA | RCC_AHB1Periph_GPIOC, ENABLE);
51.    /* GPIO-Initialisierungsstruct*/
52.    GPIO_InitTypeDef GPIO_InitStruct;
53. 
54.     /*-------- Configuring SCK-MOSI-MISO --------*/
55.    GPIO_InitStruct.GPIO_Pin = SPI3_SCK_Pin;
56.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
57.    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
58.    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
59.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
60.    GPIO_Init(SPI3_SCK_Bank, &GPIO_InitStruct);
61. 
62.    GPIO_InitStruct.GPIO_Pin = SPI3_MISO_Pin | SPI3_MOSI_Pin;
63.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_AF;
64.    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
65.    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
66.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_NOPULL;
67.    GPIO_Init(SPI3_MISO_Bank, &GPIO_InitStruct);
68. 
69. 
70.    /*-------- Configuring ChipSelect-Pin --------*/
71.    GPIO_InitStruct.GPIO_Pin = SPI3_CS_Pin;
72.    GPIO_InitStruct.GPIO_Mode = GPIO_Mode_IN;
73.    GPIO_InitStruct.GPIO_Speed = GPIO_Speed_50MHz;
74.    GPIO_InitStruct.GPIO_OType = GPIO_OType_PP;
75.    GPIO_InitStruct.GPIO_PuPd = GPIO_PuPd_UP;
76.    GPIO_Init(SPI3_CS_Bank, &GPIO_InitStruct);
77. 
78. 
79. 
80.    /*Funktion der PINs eindeutig definieren */
81.    GPIO_PinAFConfig(SPI3_SCK_Bank, SPI3_SCK_PinSource, GPIO_AF_SPI3);
82.    GPIO_PinAFConfig(SPI3_MISO_Bank, SPI3_MISO_PinSource, GPIO_AF_SPI3);
83.    GPIO_PinAFConfig(SPI3_MOSI_Bank, SPI3_MOSI_PinSource, GPIO_AF_SPI3);
84. 
85.    
86. 
87. 
88.    /* SPI enablen */
89.    SPI_Cmd(SPI3, ENABLE);
90.     SPI_NSSInternalSoftwareConfig(SPI3, SPI_NSSInternalSoft_Set);
91.}

And the main.c:
01./*
02.**
03.**                           Main.c
04.**
05.**
06.**********************************************************************/
07./*
08.   Device:     STM32F429 Discovery
09. 
10. 
11.**********************************************************************/
12.#include "stm32f4xx_conf.h"
13.#include "stm32f4xx_spi.h"
14.#include "definitions.h"
15.#include "SPI3.h"
16.#include "stdbool.h"
17. 
18.uint8_t received_data;
19.GPIO_InitTypeDef GPIO_InitStruct;
20. uint8_t rxData;
21. bool a=false;
22. 
23.int main(void)
24.{
25.    //System Initialization (Clock Setup and stuff like that)
26.    SystemInit();
27. 
28.    //initUserLED();
29. 
30.    //Enable GPIO Clock
31.    SPI3_Slave_init();
32. 
33.    while(1)
34.    {
35. 
36.        rxData=0x00;
37.        if (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_RXNE) == SET){
38.            a=true;// Application never reaches here!
39.            rxData = SPI3->DR;
40.        }
41.        if (SPI_I2S_GetFlagStatus(SPI3, SPI_I2S_FLAG_TXE) == SET){
42.            if (a){
43.            SPI1->DR = 0xAB;
44.            a=false;
45.            }
46.        }
47. 
48.    }
49.}

In the main.c, I never reach the part, where the bool a is set. What is the main routine to receive 1 byte, many bytes?

The master is always sending 1 byte every cycle (tx=0xB5 // only test value) and on the scope I can see the 0xB5.

Thanks for your help!

Outcomes