Skip to main content
SSRIN.1
Associate III
May 5, 2021
Question

Help with setting up SPI communication between 2 F767ZI Nucleo board in interrupt mode ?

Hi Guys, I am new to embedded programming. I am currently trying to transmit data between 2 Nucleo boards via SPI in interrupt Mode. The problem is I am getting gibberish data in my master mode Nucleo board and transmission stops in slave mode with an error. I am trying to experiment that from 2 Nucleo boards one shall be master and the other slave. I send a float value from master to slave in SPI, where it's multiplied by 2 and is sent back to master from slave via SPI.

Below is SPI initialization for the slave board

static void MX_SPI1_Init(void)
 
{
 
hspi1.Instance = SPI1;
 
hspi1.Init.Mode = SPI_MODE_SLAVE;
 
hspi1.Init.Direction = SPI_DIRECTION_2LINES;
 
hspi1.Init.DataSize = SPI_DATASIZE_8BIT;
 
hspi1.Init.CLKPolarity = SPI_POLARITY_LOW;
 
hspi1.Init.CLKPhase = SPI_PHASE_1EDGE;
 
hspi1.Init.NSS = SPI_NSS_HARD_INPUT;
 
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 
hspi1.Init.CRCPolynomial = 7;
 
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
 
hspi1.Init.NSSPMode = SPI_NSS_PULSE_DISABLE;
 
if (HAL_SPI_Init(&hspi1) != HAL_OK)
 
{
 
Error_Handler();}
 
}
 
 

The main program in slave board:

int main(void)
 
{
 
HAL_Init();
 
SystemClock_Config();
 
MX_GPIO_Init();
 
//MX_ETH_Init();
 
MX_USART3_UART_Init();
 
MX_USB_OTG_FS_PCD_Init();
 
MX_SPI1_Init();
 
char msg[50];
 
while(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));
 
sprintf(msg,"Starting SPI Slave \r\n");
 
HAL_UART_Transmit(&huart3,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);
 
int input, output = 0;
 
while (1)
 
{
 
if(HAL_SPI_Receive_IT(&hspi1, (uint8_t *)&result,sizeof(result))!= HAL_OK)
 
 { sprintf(msg,"Slave exchange failed \r\n");
HAL_UART_Transmit(&huart3,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);
 
Error_Handler();
 
 }
else
 
 {
HAL_SPI_Transmit_IT(&hspi1, (uint8_t *)&input,sizeof(input));
 
}
 
HAL_Delay(10);
}
 
}
 

SPI Initialisation for the master board:

static void MX_SPI1_Init(void)
 
{
 
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_1EDGE;
 
hspi1.Init.NSS = SPI_NSS_HARD_OUTPUT;
 
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_64;
 
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
 
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
 
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
 
hspi1.Init.CRCPolynomial = 7;
 
hspi1.Init.CRCLength = SPI_CRC_LENGTH_DATASIZE;
 
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
 
if (HAL_SPI_Init(&hspi1) != HAL_OK)
 
{
 
Error_Handler();
 
}
 
}
 

The main program for the master board:

int main(void)
 
{
 
HAL_Init();
 
SystemClock_Config();
 
MX_GPIO_Init();
 
//MX_ETH_Init();
 
MX_USART3_UART_Init();
 
MX_USB_OTG_FS_PCD_Init();
 
MX_SPI1_Init();
 
char msg[50];
 
while(!HAL_GPIO_ReadPin(GPIOC, GPIO_PIN_13));
 
sprintf(msg,"Starting SPI Demo \r\n");
 
HAL_UART_Transmit(&huart3,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);
 
while (1)
 
{ HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);
 
if(HAL_SPI_Transmit_IT(&hspi1, (uint8_t *)&input,sizeof(input))!= HAL_OK)
 
{ sprintf(msg,"Master exchange failed \r\n");
 
HAL_UART_Transmit(&huart3,(uint8_t*)msg,strlen(msg),HAL_MAX_DELAY);
 
Error_Handler();
 
}
 
else
 
{
 
HAL_SPI_Receive_IT(&hspi1, (uint8_t *)&result,sizeof(result));
 
}
 
HAL_Delay(10);
}
 
}

I have been stuck with this issue for 3 days now. I really need help in solving this issue.

This topic has been closed for replies.

3 replies

TDK
May 6, 2021

You'll need to manage the CS line manually in order to synchronize the start of the data and ensure the slave is ready to send prior to that line going low.

I would start with blocking mode and graduate to interrupt mode once that works.

"If you feel a post has answered your question, please click ""Accept as Solution""."
Ons KOOLI
Visitor II
May 6, 2021

Hi @SSRIN.1​ ,

Please refer to the STM32CubeF7 FW package under directory: Projects\STM32F767ZI-Nucleo\Examples\SPI

Best Regards,

Ons?

SSRIN.1
SSRIN.1Author
Associate III
May 7, 2021

Thank you! I wasn't aware of this :)