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

How to read ADXL362 register values in nrf52?

I am using SPI0 of nrf52.Following is the code:

main.c

Parents Reply Children
  • Hi, I am able to read the register value of ADXL362 accelerometer into nrf52 with slight modification with previous code.Modifications are:

    void read(nrf_drv_spi_t const * p_instance, uint8_t reg){
    	m_tx_data_spi[0] = 0x0B;
    	m_tx_data_spi[1] = reg;
    	m_tx_data_spi[2] = 0x00;
    //	m_rx_data_spi[2] = 0;
    	uint32_t err_code = nrf_drv_spi_transfer(p_instance,
        m_tx_data_spi, 3, m_rx_data_spi, 3);
    
    print(m_rx_data_spi[2]);
    

    }

        void print(unsigned char p)
      {
               static char buff[4]={0};
    	sprintf(buff,"%x",p);
    	app_uart_put(buff[0]);
    	app_uart_put(buff[1]);
    	app_uart_put(0x0A);
    	app_uart_put(0x0D);
    nrf_delay_ms(DELAY_MS*10);
     }
    
  • But I am facing Problem in writing the values into the ADXL362 registers using SPI_master code of nrf52. I am using UART for displaying register values on terminal.This is my main function:

    int main(void)
      {
         // Setup bsp module.
         bsp_configuration();
        LEDS_CONFIGURE(LEDS_MASK);
    LEDS_OFF(LEDS_MASK);
    uint32_t err_code;
    	
    
    	const app_uart_comm_params_t comm_params =
      {
          RX_PIN_NUMBER,
          TX_PIN_NUMBER,
          RTS_PIN_NUMBER,
          CTS_PIN_NUMBER,
          APP_UART_FLOW_CONTROL_ENABLED,
          false,
          UART_BAUDRATE_BAUDRATE_Baud115200
      };
    
    		APP_UART_FIFO_INIT(&comm_params,
                         UART_RX_BUF_SIZE,
                         UART_TX_BUF_SIZE,
                         uart_error_handle,
                         APP_IRQ_PRIORITY_LOW,
                         err_code);
    
    APP_ERROR_CHECK(err_code);
    		
    	
    for (;;)
    {
    			
        #if (SPI0_ENABLED == 1) 
        if (m_transfer_completed)
        {
          m_transfer_completed = false;	
    				switch_state();
    				nrf_delay_ms(DELAY_MS);
    															
        }
        #endif // (SPI0_ENABLED == 1) 
    			
    }
    

    }

    I am writing the values in ADXL362 registers using following function.When I write value in particular register individually one at time and then read it back then it working.But when when I perform write operation for all register simultaneously an then read any one of them then it giving me zero value.Following is the code:

      static void switch_state(void)
    {
    nrf_drv_spi_t const * p_instance;
    	nrf_drv_spi_config_t const * p_config;
    
    switch (m_spi_master_ex_state)
    {
        #if (SPI0_ENABLED == 1)
        case TEST_STATE_SPI0_LSB:
            p_instance = &m_spi_master_0;
            spi_master_init(p_instance, true);
            break;
    
        case TEST_STATE_SPI0_MSB:
            p_instance = &m_spi_master_0;
            spi_master_init(p_instance, false);
            break;
        #endif // (SPI0_ENABLED == 1)
    
        default:
            return;
    }
    	
    	
    	write(p_instance, 0x25,0x03);
    	nrf_delay_ms(DELAY_MS);
    	write(p_instance, 0x23,0x58);
    	nrf_delay_ms(DELAY_MS);
    		
    	read(p_instance, 0x23);
    
    		
    

    }

    #endif // (SPI0_ENABLED == 1)
    
  • I don't see the write() function you have in your code. Also in your read() function you start a transfer with nrf_drv_spi_transfer() and then already in the next line you try to read the rx buffer. At this point the SPI will not yet have completed the transfer and hence, you will not be able to read new data. You need to wait until the SPI interrupt handler is called with a message saying the transfer is complete. Or you can initiate the SPI driver in blocking mode. Then nrf_drv_spi_transfer() will not return before the transfer is done and it is safe to read the data in the next line. You can use blocking mode by writing nrf_drv_spi_init(p_instance, &config, NULL);

  • Hi Martin, Yes you are right.Actually I was not waiting for flag "m_transfer_completed" to generate.That is when "m_transfer_completed=true" then only I can send next byte to the ADXL362 register. As per your comment I have modified my code and now I can communicate (Read as well as Write) with ADXL362.Thanks for your comment.

Related