cancel
Showing results for 
Search instead for 
Did you mean: 

How to config multi slave using SPI in STM32?

HAlau.1
Associate II

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);

12 REPLIES 12
KnarfB
Principal III

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

HAlau.1
Associate II

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
Associate II

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

KnarfB
Principal III

How do you select a slave?

HAlau.1
Associate II

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

HAlau.1
Associate II

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

KnarfB
Principal III

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

HAlau.1
Associate II

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

KnarfB
Principal III

The same source for several boards is fine.