cancel
Showing results for 
Search instead for 
Did you mean: 

I need your help with the register based SPI R/Wcode

RongShengWang
Associate II

     I need your help with correcting my the register based SPI code.

     The SPI Master is STM32C031C6 Nucleo Eva board.

     The SPI Slave is STPM34 (Meter IC) Eva Board. 

     Test Result:

1. The HAL Function HAL_SPI_TransmitReceive(...) works, but introduces a long delay.

2. I tried to write the register based SPI code to do the same as the above HAL function,                              but it does not work.

     Could you make some correction on the code below? Or provide me with some working code?  Thank you very much!

 

void SPI_Transmit_Receive (const uint8_t *data_tx, uint8_t *data_rx, uint8_t size)

{

uint8_t size1 = size;

SPI1->CR1 |= (1u << 6); //ENABLE SPI

while (size)

{

    if (size == size1)

    {

       if (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_TXE))

     {

         SPI1->DR = *((uint16_t *)data_tx);

         data_tx += sizeof(uint16_t);

        size -= 2U;

     }

  }

  if (size1 > size)

  {

      if (__HAL_SPI_GET_FLAG(&hspi1, SPI_FLAG_RXNE))

     {

*       (uint16_t *)data_rx = (uint16_t)SPI1->DR;

       data_rx += sizeof(uint16_t);

       size1 -= 2U;

    }

  }

}

     SPI1->CR1 &= ~(1u << 6); //DISABLE SPI

}

0 REPLIES 0