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
  • That specific STM32 function will only ever work properly with the low 8 bits of data:

     SpiHandle[obj->SpiId].Instance->DR = ( uint16_t ) ( outData & 0xFF );

    Note the AND 0xFF part in this line.

    The actual amount of tranferred bits (per DR write) is in the SPI configuration somewhere - anything but 8 bits is highly unlikely.

    You can safely port this function to the 8 bits used in NRF hardware.

  • Thanks for your reply, so if I want exacly the same I do this :

        uint8_t rxData = 0;
      
        if (obj->initialized != true)
            return;
        
        tx1 = outData
        tx2 = 0xFF
    
        nrf_drv_spi_transfer(&obj->Instance, tx1, 1, NULL, 0);
        nrf_drv_spi_transfer(&obj->Instance, tx2, 1, NULL, 0);
    
        nrf_drv_spi_transfer(&obj->Instance, NULL, 0, rx1,1);
        nrf_drv_spi_transfer(&obj->Instance, NULL, 0, rx2,1);
    
        rxData = rx1 & rx2;
    
        return( rxData );

    Is that right? 

Reply
  • Thanks for your reply, so if I want exacly the same I do this :

        uint8_t rxData = 0;
      
        if (obj->initialized != true)
            return;
        
        tx1 = outData
        tx2 = 0xFF
    
        nrf_drv_spi_transfer(&obj->Instance, tx1, 1, NULL, 0);
        nrf_drv_spi_transfer(&obj->Instance, tx2, 1, NULL, 0);
    
        nrf_drv_spi_transfer(&obj->Instance, NULL, 0, rx1,1);
        nrf_drv_spi_transfer(&obj->Instance, NULL, 0, rx2,1);
    
        rxData = rx1 & rx2;
    
        return( rxData );

    Is that right? 

Children
Related