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

Want to send value on a particular register using SPI

void spi_read_write(0x1E,0x00) ==> output 0x10 as return value of register read.

Here I want to send value 0x90 as register value in the argument of spi_read_write(0x1E, 0x90) function, after  sending this it is retaining  0x10 as a rx_data buffer. Here is my code

static spi_rx_data[2] = {0x00,0x00};

void init(void)
{
    spi_read_write(ACCEL_WHO_M_I,0x00);  //this is working perfectly
    spi_read_write(ACCEL_REG_CTRL0,0x00); //return value spi_rx_data = 0xff,0x10
    
    spi_read_write(ACCEL_REG_CTRL0,0x90); //return value spi_rx_data = 0xff,0x10 also returning 0x10 previous value instead of 0x90
}

void spi_read_write(uint8_t register_pointer, uint8_t register_value)
{
    ret_code_t err_code;
    uint8_t data[2] ={0};
    uint8_t rx_buf;
   
    memset(spi_rx_data,0x00,sizeof(spi_rx_data));
    
    register_pointer |= 0x80;
    
    data[0] = register_pointer;
    data[1] = register_value;
    
    
    err_code = nrf_drv_spi_transfer(&spi, data,sizeof(data), spi_rx_data,sizeof(spi_rx_data));
    NRF_LOG_HEXDUMP_INFO(spi_rx_data,sizeof(spi_rx_data));
    APP_ERROR_CHECK(err_code);  
   
} 

  • I am working with LIS2DH12 connected with nrf51822 taiyo udan board. I did whatever you say but still I am getting output spi_read_write(ACCEL_REG_CTRL0,0x00) (before) : FF 10

    after  spi_read_write(ACCEL_REG_CTRL0,0x90) : FF 10

  • Are you sure that the write operation will echo back the value written to the register? What happens if you read/write it once again? Can you post your entire code you used for writing/reading the registers?

  • Yes .......I found the solution of my problem. I was masking MSB bit of a register for both read and write operation, but it is needed for read operation only. 

    void spi_read(uint8_t register_pointer)
    {
        ret_code_t err_code;
        uint8_t tx_data;
       
        memset(spi_rx_data,0x00,sizeof(spi_rx_data));
        register_pointer |= 0x80;   //this is needed for reading operation
        tx_data = register_pointer;
       
        err_code = nrf_drv_spi_transfer(&spi, &tx_data,1, spi_rx_data,sizeof(spi_rx_data));
        NRF_LOG_HEXDUMP_INFO(spi_rx_data,sizeof(spi_rx_data));
        APP_ERROR_CHECK(err_code);  
       
    }
    
    void spi_write(uint8_t register_pointer, uint8_t register_value)
    {
        ret_code_t err_code;
        memset(spi_rx_data,0x00,sizeof(spi_rx_data));
        
        data[0] = register_pointer;    //no need of masking higher bit of register
        data[1] = register_value;
        
        err_code = nrf_drv_spi_transfer(&spi, data,sizeof(data), spi_rx_data,sizeof(spi_rx_data));
        NRF_LOG_HEXDUMP_INFO(spi_rx_data,sizeof(spi_rx_data));
        APP_ERROR_CHECK(err_code);  
    } 
    
    
    

Related