This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

Spi transfert uint16_t

Hi, 

I trying to convert a stm32 function code to nRF52. The goal is to use a spi periperal on my custom board. 

In this application we need to have 2 bytes transfert. That why I need to have an uint16_t function. 

In spi exemple and driver nrf_drv_spi, we have this function nrf_drv_spi_transfer. ANd this function take only uint8_t parameters. 

This is stm32 function : 

uint16_t SpiInOut( Spi_t *obj, uint16_t outData )
{
    uint8_t rxData = 0;

    if( ( obj == NULL ) || ( SpiHandle[obj->SpiId].Instance ) == NULL )
    {
        assert_param( FAIL );
    }

    __HAL_SPI_ENABLE( &SpiHandle[obj->SpiId] );

    CRITICAL_SECTION_BEGIN( );

    while( __HAL_SPI_GET_FLAG( &SpiHandle[obj->SpiId], SPI_FLAG_TXE ) == RESET );
    SpiHandle[obj->SpiId].Instance->DR = ( uint16_t ) ( outData & 0xFF );

    while( __HAL_SPI_GET_FLAG( &SpiHandle[obj->SpiId], SPI_FLAG_RXNE ) == RESET );
    rxData = ( uint16_t ) SpiHandle[obj->SpiId].Instance->DR;

    CRITICAL_SECTION_END( );

    return( rxData );
}

Another advantage of this function, that we can send and receve SPI information !

Do you have some advise/help to help me for this translation? 

Sani300

Parents Reply
  • If i have understand STM 32 code : 

    while( __HAL_SPI_GET_FLAG( &SpiHandle[obj->SpiId], SPI_FLAG_TXE ) == RESET );
    SpiHandle[obj->SpiId].Instance->DR = ( uint16_t ) ( outData & 0xFF );

    Send by SPI : OutData & 0xFF

    while( __HAL_SPI_GET_FLAG( &SpiHandle[obj->SpiId], SPI_FLAG_RXNE ) == RESET );
    rxData = ( uint16_t ) SpiHandle[obj->SpiId].Instance->DR;

    Receive by SPI : reply from slave

    New code : 

    Build tx frame : 

          uint8_t rxData = 0;
          
          uint8_t tx[1];
          uint8_t rx[1];
    
          tx[0] = outData & 0xFF;

    Send and receive frame by SPI 

          nrf_drv_spi_transfer(&obj->Instance, tx, 1, rx, 1);
    
          rxData = *rx;
    
          return( rxData );

Children
No Data
Related