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-26 09:47 PM
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.
2020-01-26 10:18 PM
thank you, I tried it but it still hasn't worked
2020-01-26 10:19 PM
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?