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?

Parents
  • 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);
    
        }
    		
    }
    
Reply
  • 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);
    
        }
    		
    }
    
Children
No Data
Related