cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication between two controllers

sarun
Associate III

I am trying to figure out SPI communication between two microcontrollers: I have an STM32H755 Nucleo and a Nucleo G47RE. I tested basic communication by shorting MOSI and MISO on both boards separately, and it works in full duplex master mode. Both core are running at 100Mhz The G4 is the slave and it is receiving some data, but it is corrupted. What could be the potential reason?

sarun_3-1718101043409.png

 

These are my configurations and code:

/*****************************************************
* Slave side
****************************************************/

sarun_2-1718100868669.png

 

// Main Function
int main(void) {
HAL_Init();

// Configure the system clock
SystemClock_Config();

// Initialize all configured peripherals
MX_GPIO_Init();
MX_SPI2_Init();
MX_SPI1_Init();
MX_LPUART1_UART_Init();
MX_I2C1_Init();

// Start SPI reception in interrupt mode
HAL_SPI_Receive_IT(&hspi1, rx_buffer, sizeof(tx_buffer));

// Infinite loop
while (1) {
// #define FULL_DUPLEX
#ifdef FULL_DUPLEX
HAL_SPI_TransmitReceive_IT(&hspi1, tx_buffer, rx_buffer, sizeof(tx_buffer));
#endif

if (rx_flg) {
rx_flg = false;
HAL_UART_Transmit_IT(&hlpuart1, rx_buffer, sizeof(tx_buffer));
HAL_SPI_Receive_IT(&hspi1, rx_buffer, sizeof(tx_buffer));
}
}
}

// SPI1 Initialization
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();
}
}

// SPI Receive Complete Callback
void HAL_SPI_RxCpltCallback(SPI_HandleTypeDef *hspi) {
rx_flg = true;
}

// GPIO EXTI Callback
void HAL_GPIO_EXTI_Callback(uint16_t GPIO_Pin) {
if (GPIO_Pin == B1_Pin) {
PB_stat = true;
} else {
__NOP();
}
}

/*******************
* Master Code
*******************/

sarun_0-1718100703317.pngsarun_1-1718100751506.png

// Main Function
int main(void) {
HAL_Init();
SystemClock_Config();

// Initialize all configured peripherals
MX_GPIO_Init();
MX_SPI1_Init();

// Initialize LEDs
BSP_LED_Init(LED_GREEN);
BSP_LED_Init(LED_YELLOW);
BSP_LED_Init(LED_RED);

// Initialize USER push-button
BSP_PB_Init(BUTTON_USER, BUTTON_MODE_EXTI);

// Initialize COM1 port
COM_InitTypeDef BspCOMInit;
BspCOMInit.BaudRate = 115200;
BspCOMInit.WordLength = COM_WORDLENGTH_8B;
BspCOMInit.StopBits = COM_STOPBITS_1;
BspCOMInit.Parity = COM_PARITY_NONE;
BspCOMInit.HwFlowCtl = COM_HWCONTROL_NONE;
if (BSP_COM_Init(COM1, &BspCOMInit) != BSP_ERROR_NONE) {
Error_Handler();
}

// Print welcome message
printf("Welcome to STM32 world!\n\r");

// Infinite loop
while (1) {
if (switch_state) {
BSP_LED_On(LED2);
status = HAL_SPI_Transmit_IT(&hspi1, tx_buffer, sizeof(tx_buffer));
printf("status=%d\n", status);
HAL_Delay(500);
BSP_LED_Off(LED2);
HAL_Delay(500);
}

if (rx_flg) {
rx_flg = false;
printf("%s", rx_buffer);
}
}
}

// SPI1 Initialization
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_SOFT;
hspi1.Init.BaudRatePrescaler = SPI_BAUDRATEPRESCALER_256;
hspi1.Init.FirstBit = SPI_FIRSTBIT_MSB;
hspi1.Init.TIMode = SPI_TIMODE_DISABLE;
hspi1.Init.CRCCalculation = SPI_CRCCALCULATION_DISABLE;
hspi1.Init.CRCPolynomial = 0x0;
hspi1.Init.NSSPMode = SPI_NSS_PULSE_ENABLE;
hspi1.Init.NSSPolarity = SPI_NSS_POLARITY_LOW;
hspi1.Init.FifoThreshold = SPI_FIFO_THRESHOLD_01DATA;
hspi1.Init.TxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.RxCRCInitializationPattern = SPI_CRC_INITIALIZATION_ALL_ZERO_PATTERN;
hspi1.Init.MasterSSIdleness = SPI_MASTER_SS_IDLENESS_00CYCLE;
hspi1.Init.MasterInterDataIdleness = SPI_MASTER_INTERDATA_IDLENESS_00CYCLE;
hspi1.Init.MasterReceiverAutoSusp = SPI_MASTER_RX_AUTOSUSP_DISABLE;
hspi1.Init.MasterKeepIOState = SPI_MASTER_KEEP_IO_STATE_DISABLE;
hspi1.Init.IOSwap = SPI_IO_SWAP_DISABLE;

if (HAL_SPI_Init(&hspi1) != HAL_OK) {
Error_Handler();
}
}

// SPI Transmit Complete Callback
void HAL_SPI_TxCpltCallback(SPI_HandleTypeDef *hspi) {
tx_flg = true;
BSP_LED_Toggle(LED1);
}

// Push Button Callback
void BSP_PB_Callback(Button_TypeDef Button) {
if (Button == BUTTON_USER) {
BspButtonState = BUTTON_PRESSED;
switch_state ^= 1;
if (switch_state) {
BSP_LED_On(LED3);
} else {
BSP_LED_Off(LED3);
}
}
}
1 ACCEPTED SOLUTION

Accepted Solutions
KDJEM.1
ST Employee

Hi @sarun ,

Could you please refer to SPI examples and check the master and the slave configurations:

-STM32CubeH7/Projects/NUCLEO-H745ZI-Q/Examples/SPI/SPI_FullDuplex_ComIT at master · STMicroelectronics/STM32CubeH7 · GitHub

-STM32CubeG4/Projects/NUCLEO-G474RE/Examples/SPI/SPI_FullDuplex_ComIT_Slave at master · STMicroelectronics/STM32CubeG4 · GitHub

May Getting started with SPI - stm32mcu can help you.

I hope this help you!

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

View solution in original post

3 REPLIES 3
KDJEM.1
ST Employee

Hi @sarun ,

Could you please refer to SPI examples and check the master and the slave configurations:

-STM32CubeH7/Projects/NUCLEO-H745ZI-Q/Examples/SPI/SPI_FullDuplex_ComIT at master · STMicroelectronics/STM32CubeH7 · GitHub

-STM32CubeG4/Projects/NUCLEO-G474RE/Examples/SPI/SPI_FullDuplex_ComIT_Slave at master · STMicroelectronics/STM32CubeG4 · GitHub

May Getting started with SPI - stm32mcu can help you.

I hope this help you!

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.

Hello @KDJEM.1 ,

Thank you for the input. I am pretty much new to the STM environment, and I am slowly getting the hang of it. Based on your input, I referred to the example and just disabled the slave-side hardware NSS. Viola! It started working.

KDJEM.1
ST Employee

Hi @sarun ,

Glad to know that the issue is solved and welcome to STM environment :).

If you have a question, a request, a support need...,  Please don't hesitate to create a post in STCommunity.

Thank you for your contribution in the community.

Kaouthar

To give better visibility on the answered topics, please click on Accept as Solution on the reply which solved your issue or answered your question.