2020-01-23 06:23 PM
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);
2020-01-23 10:17 PM
Don't see an error in your setup. How is your wiring? Star-topology? What are the GPIOs you are toggling?
2020-01-23 11:54 PM
thank you, yes i use star wiring, i try to transmit character from slave but master only reads slave 1 and slave 2 unreadable
2020-01-24 12:05 AM
can you tell me the problem and are there any suggestions?
2020-01-24 12:10 AM
How do you select a slave?
2020-01-24 12:24 AM
by setting the pin as output and connecting to the ss pin on the master
2020-01-24 01:03 AM
hou about vcc and gnd? does it need to be connected to the same source?
2020-01-24 01:21 AM
gnd: yes. Don't tie several vcc supplies together, vcc can be local to each board.
2020-01-26 07:03 PM
ok thank you, Does it mean I can't use the same battery source for every board?
2020-01-26 09:28 PM
The same source for several boards is fine.