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

nrf52832 SPI to builtin LIS2DH accelerometer

Hi, I have the D52QD2M4IA-A chip (from dynastream) that has inside nrf52832 chip. It has also builtin an onboard LIS2DH accelerometer. However when I am trying to connect to it through SPI interface this seems not to respond back. Can Anyone help?

  • My code is as follows:

    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include <stdbool.h>
    #include <stdint.h>
    #include "nrf_delay.h"
    #include "nrf_gpio.h"
    #include "boards.h"
    #include "nrf_temp.h"
    #include "app_error.h"
    
    #if defined(BOARD_PCA10040)
    #define SPI_CS_PIN   27 /**< SPI CS Pin.*/
    #elif defined(BOARD_PCA10036) 
    #define SPI_CS_PIN   29 /**< SPI CS Pin.*/
    #elif defined(BOARD_PCA10028)
    #define SPI_CS_PIN   4  /**< SPI CS Pin.*/
    #else
    #error "Example is not supported on that board."
    #endif
    #define SPI_INSTANCE  0
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE);
    static volatile bool spi_xfer_done;  /**< Flag used to indicate that SPI instance completed the transfer. */
    
    
    void spi_event_handler(nrf_drv_spi_evt_t const * p_event)
    {
        spi_xfer_done = true;
    
    }
    
    int main(void)
    {
    
    	uint32_t err_code = NRF_SUCCESS;
    	uint8_t reg;
    		nrf_gpio_cfg_output(17);
    	
    		nrf_delay_ms(1);
    	nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG(SPI_INSTANCE);
    	spi_config.frequency = NRF_DRV_SPI_FREQ_1M;
    	spi_config.bit_order = NRF_DRV_SPI_BIT_ORDER_LSB_FIRST;
    	spi_config.ss_pin = SPI_CS_PIN;
    		nrf_delay_ms(1);
    	err_code = nrf_drv_spi_init(&spi, &spi_config, spi_event_handler);
    		nrf_delay_ms(1);
    	if (err_code != NRF_SUCCESS)
      {
        // Initialization failed. Take recovery action.
    		reg = 0x01;
      }
    	 APP_ERROR_CHECK(err_code);
    	
    	uint8_t OUT_X_L_addr = 0x28;       // The address of the register you want to write
    	uint8_t WHO_AM_I = 0x0F;
      uint8_t tx_data[2] = {0x00, 0x00}; // Transmit register
      uint8_t rx_data[2] = {0x00, 0x00}; // Receive register
    
    	  while (true)
        {
    			 tx_data[0] = ( WHO_AM_I | 0x80 ); //Add the RW bit to the address.
    		
    	    spi_xfer_done   = false;
    			nrf_delay_ms(1);
    						APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, tx_data, 2, rx_data, 2));
    			if (rx_data[1]>0){
    				nrf_gpio_pin_write(17,1);
    				nrf_delay_ms(1000);
    			}
    			nrf_gpio_pin_write(17,0);
    	
    			
    			nrf_delay_ms(500);
    
        }
    		
    }
    
  • My code doesn't change the speed or the bit order, are you sure you need to change them ?

    Also. The WHO_AM_I reg is 0x0F and you need to set the MSB for a read operation

    PS. I ran my code in async mode, but that should not make any difference

  • Ok without changing the bit order I Was able to send WHO_AM_I reg (= 0x0F ) and to get back the value of 33. Now how I can go on to read more data? I am trying to read the OUT_X_L register but no lack (I am getting 00 value). Do I have to set something prior? Is there any sample code to get the xyz accelerometer values?

  • Well, you could read the technical reference

    Or other people have written code for this device that you could port e.g.

    github.com/.../LIS2DH

  • I know this is a bit old, but for future reference, make sure you set the LIS2DH operation mode to low power, normal or high resolution before trying to read or you will get 0 when reading the axis values. By default it is in powered down mode. To do that write CTRL_REG1[3] (LPen) and CTRL_REG4[3] (HR) bits with your desired configuration.

Related