0
\$\begingroup\$

I'm trying to do simple SPI communication on STM32 Discovery, HAL lib is used. I'm trying to send some data via SPI1 and to receive same data on SPI3 (SPI1 = Full Duplex Master, SPI3 = Full Duplex Slave). For transmit polling method is used and for receive DMA circular. Communication is on 1 device (just one Discovery is used). Code stuck here:

while(HAL_SPI_GetState(&hspi3) != HAL_SPI_STATE_READY){}

Can someone explain me why code stuck there?

uint8_t rxSpi[10] = {0};
uint8_t txSpi[10] = "123456788";
    int main(void)
    {
        HAL_Init();
        SystemClock_Config();

        MX_GPIO_Init();
        MX_DMA_Init();
        MX_SPI1_Init();
        MX_SPI3_Init();
        MX_USART2_UART_Init();

        if(HAL_SPI_Receive_DMA(&hspi3, rxSpi, 10) != HAL_OK)
        {
            Error_Handler();
        }
        while(HAL_SPI_GetState(&hspi3) != HAL_SPI_STATE_READY){}

        while (1)
        {
            if(HAL_SPI_Transmit(&hspi1, txSpi, 10, 100) != HAL_OK)
            {
                Error_Handler();
            }
            while(HAL_SPI_GetState(&hspi1) != HAL_SPI_STATE_READY){}
            HAL_Delay(1000);
        }
    }

If I just send data (receive initialize is commented), on logic analyzer I got this, basically there is missing small interval between two bytes. enter image description here

\$\endgroup\$
1
  • \$\begingroup\$ What is the actual SPI state code? You can read the code of actual SPI state with HAL_SPI_GetState() and see what is actually going on. BTW, why do you wait for HAL_SPI_STATE_READY after HAL_SPI_Receive_DMA()? Your SPI receiver is already initialized properly and ready, what you have checked with if before HAL_SPI_Receive_DMA(). \$\endgroup\$
    – cyclone125
    May 7, 2020 at 9:27

1 Answer 1

1
\$\begingroup\$

You should look at the API documentation or code. If you tell the SPI3 to receive 10 bytes via DMA, the state will be BUSY_RX until it has received those bytes. That is why the state is not ready.

\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.