cancel
Showing results for 
Search instead for 
Did you mean: 

Problem using SPI on NUCLEO-L476RG

MSzab.2
Associate

Hi there everyone,

I want to use SPI to communicate with an IMU, but I am having trouble communicating with any SPI devices. This is how my SPI is set.

0693W00000ANdwjQAD.png 

This is a snippet of my simple SPI test code.

/* USER CODE BEGIN 2 */
  	  for (uint8_t i = 0;i<100;i++)
  		  {
  	  	  HAL_GPIO_TogglePin(GPIOC,GPIO_PIN_0);
  	  	  HAL_Delay(1);
  		  }
  	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
 
  /* USER CODE END 2 */
 
  /* Infinite loop */
  /* USER CODE BEGIN WHILE */
  while (1)
  {
    /* USER CODE END WHILE */
 
    /* USER CODE BEGIN 3 */
  	  HAL_Delay(100);
  	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_RESET);
  	  HAL_SPI_Transmit(&hspi2, MOSI_data, 1, 1);
  	  HAL_SPI_Receive(&hspi2, MISO_data, 1, 10);
  	  HAL_GPIO_WritePin(GPIOC, GPIO_PIN_0, GPIO_PIN_SET);
  }

I toggle the /CS pin to make sure, that the device is in SPI communication mode. Than I send a command, to send an address and read the incoming data. I tried every SPI mode.

Well, it doesn't work. There is no data output. I looked at the signal with oscilloscope and this is what happens.

Yellow trace is always SCK. This is in SPI mode "10".

This is SCK and /CS. All looks OK.

0693W00000ANdznQAD.png 

This is SCK and SDI. Notice, that the change of data, the edge is exactly in sync with the SCK edge. I don't think this is correct. I think, that data should be log. 1 or log. 0 while there is the clock edge.

0693W00000ANe07QAD.png0693W00000ANe0bQAD.png0693W00000ANe0mQAD.png 

Is there any way to manipulate this?

EDIT: Forgot to mention. The SDO is at log. 0, 0V. Connected to the NUCLEO or not, without any pull-up or pull-down.

0693W00000ANeTsQAL.png When I disconnect it from NUCLEO and connect a 10k pull- up, this happens:

0693W00000ANeY9QAL.png 

Thank you.

2 REPLIES 2

The devices on SPI bus sample on one SCK edge, and shift on the other. Sure, there is a delay between the shift edge and respective data change, but you have to look at nanoseconds range.

You may have confused the pins. The unwritten standard is to denote SPI data pins as MISO and MOSI, that means Master-In-Slave-Out. This uniquely denotes the direction for each of the 4 pins (2 pins on master and 2 pins on slave). I understand that it's not your fault to call pins SDI and SDO, but these don't denote properly the directionality, so you have to read very carefully the datasheet of the attached device to understand, whether SDI is MOSI or MISO.

JW

MSzab.2
Associate

Those are some good suggestions.

Here is a photo of SCK and MOSI at 10 ns range. They are crossing each perfectly in the middle.

0693W00000ANfiGQAT.png 

Sorry for mixing terms.

SPI2_MOSI pin (PC3) on NUCLEO is connected to SDI pin on the IMU.

SPI2_MISO pin (PC2) on NUCLEO is connected to SDO pin in the IMU.

SPI2_SCK pin (PB10) on NUCLEO is connected to SCL pin on IMU.

IMU_CS pin (custom GPIO PC0) on NUCLEO is connected to nCS pin on IMU.