cancel
Showing results for 
Search instead for 
Did you mean: 

SPI communication between two Nucleo G431-KB boards not working

ArnauReyes
Associate

I'm getting introduced to the STM32 environment and I have been trying to use SPI protocol to send messages between these two boards. My setup is as follows:


_legacyfs_online_stmicro_images_0693W00000bjMMCQA2.pngAnd in the CubeIDE both boards have this setup, with the difference that the master board has the selection of Full duplex master in SPI and the slave Full duplex slave. The clocks are configured the same way in both boards and I believe I'm not leaving anything.


_legacyfs_online_stmicro_images_0693W00000bjMMHQA2.pngThe master code is the following:

#include "main.h"
#include <stdio.h>
 
SPI_HandleTypeDef hspi1;
 
UART_HandleTypeDef huart2;
 
uint8_t data_rx[1];
uint8_t data_tx[1];
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_SPI1_Init();
 
  while (1)
  {
	  data_tx[0] = 3;
	  HAL_SPI_Transmit(&hspi1, data_tx, sizeof(data_tx), 5000);
  }
}

The slave code is the following:

#include "main.h"
#include <stdio.h>
 
SPI_HandleTypeDef hspi1;
UART_HandleTypeDef huart2;
 
uint8_t data_rx[1];
 
void SystemClock_Config(void);
static void MX_GPIO_Init(void);
static void MX_USART2_UART_Init(void);
static void MX_SPI1_Init(void);
 
int main(void)
{
  HAL_Init();
  SystemClock_Config();
 
  MX_GPIO_Init();
  MX_USART2_UART_Init();
  MX_SPI1_Init();
 
  while (1)
  {
      HAL_SPI_Receive(&hspi1, data_rx, sizeof(data_rx),5000);
  }
}

I'm trying to make it as simple as possible but with these codes and the setup I have detailed before I'm not getting anything at data_rx. I don't know what is wrong as there is not much to see.

Any recommendations to use SPI protocol in this context will be appreciated, thank you.

7 REPLIES 7

Observe signals using oscilloscope/LA.

Peter BENSCH
ST Employee

...and please check MOSI/MISO:

  • MOSI = Master Out Slave In
  • MISO = Master In Slave Out

Currently you have connected output with output and input with input.

So you would have to cross both.

[edit]my assumption was wrong, the wiring is correct[/edit]

Regards

/Peter

In order 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.

> Currently you have connected output with output and input with input.

I see MOSI from master connected to MOSI of slave, and MISO from slave connected to MISO of master, so it's input to output and output to input as it's supposed to be.

JW

Oops, you are right, Jan, I was confused with another communication.

In order 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.
Bob S
Principal

How do you have the NSS configured for master and slave (show your MX_SPI1_Init code)? Also make sure that the slave it running (and waiting) before you start the master. Off hand I am not sure how the SPI hardware behaves if it were to start receiving in the middle of a packet. You should add a delay in the master's loop to make it easier to debug (so there is a gap between packets). And, as always, check the return values from the HAL calls - probably not an issue here but a good habit.

Dthum.1
Associate III

do you not think that you need to make the CS(chip select) pin from high to low for communication?

first, make the cs pin from high to low

and then try the same, I guess it will solve your issue if I am not wrong then.

I don't know why I didn't think about this. This fixed the communication problem, as the master is now sending messages to the slave and the slave recieves them correctly. The issue I have now is to send a message from the slave to the master.