Skip to main content
HAlau.1
Associate II
January 24, 2020
Question

How to config multi slave using SPI in STM32?

  • January 24, 2020
  • 10 replies
  • 2230 views

Hello I'm try to connect 1 master and 2 slave, but failed, I use stm32f427VI, and use Truestudio for programing, thanks for your help.

/* SPI4 init function */ //SLAVE1
static void MX_SPI4_Init(void)
{
 
 /* SPI4 parameter configuration*/
 hspi4.Instance = SPI4;
 hspi4.Init.Mode = SPI_MODE_SLAVE;
 hspi4.Init.Direction = SPI_DIRECTION_2LINES;
 hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi4.Init.CLKPhase = SPI_PHASE_2EDGE;
 hspi4.Init.NSS = SPI_NSS_SOFT;
 hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi4.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi4) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
}
/* SPI4 init function */ //SLAVE2
static void MX_SPI4_Init(void)
{
 
 /* SPI4 parameter configuration*/
 hspi4.Instance = SPI4;
 hspi4.Init.Mode = SPI_MODE_SLAVE;
 hspi4.Init.Direction = SPI_DIRECTION_2LINES;
 hspi4.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi4.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi4.Init.CLKPhase = SPI_PHASE_2EDGE;
 hspi4.Init.NSS = SPI_NSS_SOFT;
 hspi4.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi4.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi4.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi4.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi4) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 }
 
}
/* SPI1 init function */ //MASTER
static void MX_SPI1_Init(void)
{
 
 /* SPI1 parameter configuration*/
 hspi1.Instance = SPI1;
 hspi1.Init.Mode = SPI_MODE_MASTER;
 hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 hspi1.Init.CLKPhase = SPI_PHASE_2EDGE;
 hspi1.Init.NSS = SPI_NSS_SOFT;
 hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_16;
 hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 hspi1.Init.CRCPolynomial = 10;
 if (HAL_SPI_Init(&hspi1) != HAL_OK)
 {
 _Error_Handler(__FILE__, __LINE__);
 {
 asm("bkpt 255");
 }
 }
 
}
/* Includes --------------------Receive from slave2----------------------------------------*/
	 __HAL_SPI_ENABLE(&hspi1);
	 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8,GPIO_PIN_RESET);
	 HAL_SPI_Receive (&hspi1, outputBuffer2, 6, 50);
	 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_8,GPIO_PIN_SET);
	 STM_Serial_Print("|");
	 STM_Serial_Print(outputBuffer2);
	 STM_Serial_Print("\r\n");
	 HAL_Delay(1000);
	 STM_Serial_Print("|");
	 STM_Serial_Print("data slave2");
	 STM_Serial_Print("\r\n");
	 HAL_Delay(1000);
/* Includes --------------------Receive from slave1----------------------------------------*/
	 __HAL_SPI_ENABLE(&hspi1);
	 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7,GPIO_PIN_RESET);
	 HAL_SPI_Receive (&hspi1, outputBuffer, 6, 50);
	 HAL_GPIO_WritePin(GPIOE, GPIO_PIN_7,GPIO_PIN_SET);
	 STM_Serial_Print("|");
	 STM_Serial_Print(outputBuffer);
	 STM_Serial_Print("\r\n");
	 HAL_Delay(1000);
	 STM_Serial_Print("|");
	 STM_Serial_Print("data slave1");
	 STM_Serial_Print("\r\n");
	 HAL_Delay(1000);

    This topic has been closed for replies.

    10 replies

    KnarfB
    Super User
    January 24, 2020

    Don't see an error in your setup. How is your wiring? Star-topology? What are the GPIOs you are toggling?

    HAlau.1
    HAlau.1Author
    Associate II
    January 24, 2020

    thank you, yes i use star wiring, i try to transmit character from slave but master only reads slave 1 and slave 2 unreadable

    HAlau.1
    HAlau.1Author
    Associate II
    January 24, 2020

    can you tell me the problem and are there any suggestions?

    KnarfB
    Super User
    January 24, 2020

    How do you select a slave?

    HAlau.1
    HAlau.1Author
    Associate II
    January 24, 2020

    by setting the pin as output and connecting to the ss pin on the master

    HAlau.1
    HAlau.1Author
    Associate II
    January 24, 2020

    hou about vcc and gnd? does it need to be connected to the same source?

    KnarfB
    Super User
    January 24, 2020

    gnd: yes. Don't tie several vcc supplies together, vcc can be local to each board.

    HAlau.1
    HAlau.1Author
    Associate II
    January 27, 2020

    ok thank you, Does it mean I can't use the same battery source for every board?

    KnarfB
    Super User
    January 27, 2020

    The same source for several boards is fine.

    HAlau.1
    HAlau.1Author
    Associate II
    January 27, 2020

    thank you, I tried it but it still hasn't worked

    S.Ma
    Principal
    January 27, 2020

    All devices should use same digital output voltages as SPI is push pull type.

    Master use SW NSS and you can use any 2 GPIO for NSS1 and NSS2

    Slaves should use HW NSS to make sure their outputs automatically disable themselves when not selected. (otherwise, both slaves will talk simultaneously)

    Create an EXTI interrupt on slaves' NSS pin (yes you can even if it maynot be a HAL or LL function) to get event when the comm just started or has just finished.

    The Cube examples shows the peripheral is working, not how to use it in a typical application.

    Note: If the bitrate is above 2MHz and packet size can be over 256, you could use DMA with cyclic buffer (no interrupt) on the slave side, and regular DMA for master side (interrupt when communication is complete).

    For master, the communication completion and events should be on the reception complete, not the transmission.

    HAlau.1
    HAlau.1Author
    Associate II
    January 27, 2020

    thank you, I new for this, what do you mean SW NSS, HW NSS and how to use it in program?

    and how to make interrupt on the slave whether by making the SS slave pin which was previously only GPIO output becomes EXTI?