cancel
Showing results for 
Search instead for 
Did you mean: 

SPI loopbacking not working

MDeva.1
Associate II

I am using nucle-h745ziq ,SPI1 as Full duplex master and SPI4 as Full duplex salve.And both have same freuncy 25MHZ .And spi1 prescalar is 2.But SPI4 is not start the communication.Both structure parameter are same.

In spi.c libabry I am uing if(HAL_SPI_Transmit(&hspi4,sentDataS2lots1, 1,10)==HAL_ERROR)

this function for transmission.In that libabry I debugg and get issue in belo function.

 else

   {

    /* Timeout management */

    if ((((HAL_GetTick() - tickstart) >= Timeout) && (Timeout != HAL_MAX_DELAY)) || (Timeout == 0U))

    {

     /* Call standard close procedure with error check */

     SPI_CloseTransfer(hspi);

     /* Process Unlocked */

     __HAL_UNLOCK(hspi);

     SET_BIT(hspi->ErrorCode, HAL_SPI_ERROR_TIMEOUT);

     hspi->State = HAL_SPI_STATE_READY;

     

     return HAL_ERROR;

     //printf("3\n\r");

    }

i am unablle to undestand what is error.I make the connection of SPI 1 MISO to SPI4 MISO,

SPI1 MOSI to SPI4 MOSI, CLk to clk.And I also intercahnge the connection i tried but unableto receive .Can you help me?

9 REPLIES 9
SNorm.1
Associate III

I'm not an expert, I'm a beginner. Maybe this will help you?

​https://stackoverflow.com/questions/64554043/loopback-spi-in-stm32f411re

I am also use spi1 as full duplex master and connect the SPI moSI to SPI1 MISO but no data is receive. can you please tell me the connection of SPI for loopbacking?

MISO-MISO, MOSI-MOSI, SCK-SCK, and NSS of slave should be pulled low somehow, e.g. by connecting it to a GPIO output.

JW

I interface the SPI3 as full duplex mode and SPI1 as full duplex salve.And I connect MOSI of SPI1 to SPI3 and MISO SPI1 TO SPI3 and clk to clk.And NSS pin of spi1 as output low. I do the right connection? or I have mistaken in connection

TDK
Guru

> And both have same freuncy 25MHZ 

The slave clock speed should be 2x of the master. Probably not the issue here though.

And as JW points out, you need a way to synchronize the start of the data. Typically a CS/NSS line is used for this.

https://en.wikipedia.org/wiki/Serial_Peripheral_Interface

If you feel a post has answered your question, please click "Accept as Solution".

> SPI1 as full duplex slave

> And NSS pin of spi1 as output low

You don't control the peripheral's (SPI's) pins direction, the peripheral does that. And NSS for slave is input. You have to pull it down externally.

JW

PS. In the initial post, you've said SPI1 is master. Moving target is harder to hit.

Here is my configuration and code

 /* Initialize all configured peripherals */

 MX_GPIO_Init();

 MX_SPI1_Init();

 MX_SPI3_Init();

 MX_USART3_UART_Init();

 /* USER CODE BEGIN 2 */

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_RESET);

 HAL_SPI_Transmit(&hspi3, Tx_SPI4to1, 2, 10);

 HAL_SPI_Receive(&hspi3, Rx_SPI4to1, 2, 10);

 HAL_SPI_Transmit(&hspi1, Tx_SPI1to4, 2, 10);

 HAL_SPI_Receive(&hspi1, Rx_SPI1to4, 2, 10);

 HAL_GPIO_WritePin(GPIOA, GPIO_PIN_4, GPIO_PIN_SET);

this my code .I make the connection SPI1 MISO to SPI3 MISo and SPI1 TO spi3 mosi AND CLK TO CLK BUT UNABLE TO RECEIVE DATA.Can you help resolving this issu.0693W00000BbKMyQAN.png0693W00000BbKMjQAN.png0693W00000BbKNDQA3.pngHere is configuration.That i done.Where I make mistake.

Observe the signals using logic analyzer or oscilloscope. Read out and check registers content.

NSS is an important signal for slave.

JW

TDK
Guru

HAL_SPI_Transmit and HAL_SPI_Receive are blocking functions. You slave needs to be ready to send/receive data before BEFORE the master starts sending a clock. In your code, you're using blocking function for both master and slave, which has no hope of working.

You'll need to use a non-blocking mode DMA for slave or master.

HAL_SPI_Transmit(&hspi3, Tx_SPI4to1, 2, 10);
 HAL_SPI_Receive(&hspi3, Rx_SPI4to1, 2, 10);
 
 HAL_SPI_Transmit(&hspi1, Tx_SPI1to4, 2, 10);
 HAL_SPI_Receive(&hspi1, Rx_SPI1to4, 2, 10);

I'm also assuming you meant to say SPI3 rather than SPI4.

If you feel a post has answered your question, please click "Accept as Solution".