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

lis3dh not working

my nrf52840 interfacing with lis 3dh not reading or writing values...

  • static void log_init(void)
    {
    ret_code_t err_code = NRF_LOG_INIT(NULL);
    APP_ERROR_CHECK(err_code);

    NRF_LOG_DEFAULT_BACKENDS_INIT();
    }

    void acceleration_init()
    {
    lis_gpio_config();
    lis_power_on();
    lis_spi();
    lis_setup();
    lis_caliberation();
    // lis_power_off();

    }

    #include "lis.h"
    #include "nrf_drv_spi.h"
    #include "app_util_platform.h"
    #include "nrf_gpio.h"
    #include "nrf_delay.h"
    #include "boards.h"
    #include "app_error.h"
    #include <string.h>
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"


    #define SPI_INSTANCE 1 /**< SPI instance index. */
    static const nrf_drv_spi_t spi = NRF_DRV_SPI_INSTANCE(SPI_INSTANCE); /**< SPI instance. */
    static volatile bool spi_xfer_done; /**< Flag used to indicate that SPI instance completed the transfer. */
    #define TEST_STRING "Nordic"
    //static uint8_t m_tx_buf[] = TEST_STRING; /**< TX buffer. */
    //static uint8_t m_rx_buf[sizeof(TEST_STRING) + 1]; /**< RX buffer. */
    //static uint8_t WrBuf[4];
    //static const uint8_t m_length = sizeof(WrBuf); /**< Transfer length. */

    /**< Transfer length. */

    #define STATUS_REG_AUX 0x07
    #define OUT_ADC1_L 0x08
    #define OUT_ADC1_H 0x09
    #define OUT_ADC2_L 0x0A
    #define OUT_ADC2_H 0x0B
    #define OUT_ADC3_L 0x0C
    #define OUT_ADC3_H 0x0D
    #define INT_COUNTER_REG 0x0E
    #define WHO_AM_I 0x0F
    #define TEMP_CFG_REG 0x1F
    #define CTRL_REG1 0x20
    #define CTRL_REG2 0x21
    #define CTRL_REG3 0x22
    #define CTRL_REG4 0x23
    #define CTRL_REG5 0x24
    #define CTRL_REG6 0x25
    #define REFERENCE 0x26
    #define STATUS_REG2 0x27
    #define OUT_X 0x29
    #define OUT_Y 0x2B
    #define OUT_Z 0x2D
    #define FIFO_CTRL_REG 0x2E
    #define FIFO_SRC_REG 0x2F
    #define IG1_CFG 0x30
    #define IG1_SOURCE 0x31
    #define IG1_THS 0x32
    #define IG1_DURATION 0x33
    #define IG2_CFG 0x34
    #define IG2_SOURCE 0x35
    #define IG2_THS 0x36
    #define IG2_DURATION 0x37
    #define CLICK_CFG 0x38
    #define CLICK_SRC 0x39
    #define CLICK_THS 0x3A
    #define TIME_LIMIT 0x3B
    #define TIME_LATENCY 0x3C
    #define TIME_WINDOW 0x3D
    #define Act_THS 0x3E
    #define Act_DUR 0x3F
    #define RR 0x80
    #define KK 0x3F
    #define WREG 0x00
    volatile bool transfer_flag;

    void lis_gpio_config()
    {
    nrf_gpio_cfg_output(LIS_VCC);
    // nrf_gpio_cfg_output(LIS_CS);
    // nrf_gpio_pin_set(LIS_CS);
    nrf_gpio_cfg_output(SPI_CS_PIN_LIS);
    nrf_gpio_cfg_sense_input(LIS_INT1,NRF_GPIO_PIN_PULLDOWN,NRF_GPIO_PIN_SENSE_HIGH);
    }

    void lis_power_on()
    {
    nrf_gpio_pin_set(LIS_VCC);
    // nrf_gpio_pin_clear(SPI_CS_PIN_LIS);
    }

    //void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
    // void * p_context)
    //{
    // spi_xfer_done = true;
    // //printf("\r\nTransfer completed\r\n");
    // NRF_LOG_INFO("Transfer completed.");
    // if (rx_buf[0] != 0)
    // {
    // NRF_LOG_INFO(" Received:");
    // NRF_LOG_HEXDUMP_INFO(rx_buf, strlen((const char *)rx_buf));
    // }
    //}

    void spi_event_handler(nrf_drv_spi_evt_t const * p_event,
    void * p_context)
    {
    transfer_flag = true;
    //printf("\r\nTransfer completed\r\n");
    NRF_LOG_INFO("Transfer completed.");
    }


    void lis_spi()
    {

    nrf_drv_spi_config_t spi_config = NRF_DRV_SPI_DEFAULT_CONFIG;
    spi_config.ss_pin = NULL;
    spi_config.miso_pin = SPI_MISO_PIN;
    spi_config.mosi_pin = SPI_MOSI_PIN;
    spi_config.sck_pin = SPI_SCK_PIN;
    APP_ERROR_CHECK(nrf_drv_spi_init(&spi, &spi_config, spi_event_handler, NULL));
    NRF_LOG_INFO("SPI example started.");

    }

    int lis_read_reg(int reg)
    {
    uint8_t RdBuf[4];
    uint8_t WrBuf[4];
    WrBuf[0] = 0x80 | reg;
    transfer_flag = false;
    nrf_gpio_pin_clear(SPI_CS_PIN_LIS);
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, WrBuf, 1, RdBuf, 2));
    while (!transfer_flag)
    {
    __WFE();
    }
    nrf_gpio_pin_set(SPI_CS_PIN_LIS);
    return ((int)RdBuf[1]);
    }
    /**@brief Function is used for writing to the registers of lis
    *
    * @param[in]Function use reg and val parameters to indicate the value and registers
    */

    void lis_write_reg(int reg, int val)
    {

    uint8_t WrBuf[4];
    WrBuf[0] = WREG | reg; //read GPIO reg
    WrBuf[1] = val;
    transfer_flag = false;
    nrf_gpio_pin_clear(SPI_CS_PIN_LIS);
    APP_ERROR_CHECK(nrf_drv_spi_transfer(&spi, WrBuf, 2, NULL, 0));
    while (!transfer_flag)
    {
    __WFE();
    }

    nrf_gpio_pin_set(SPI_CS_PIN_LIS);
    }


    void lis_setup(void)
    {
    uint32_t bit,fdg=0;
    uint8_t id;

    id=lis_read_reg(WHO_AM_I);
    printf("\r\n Device id=%d \r\n",id);
    if(id)
    {
    printf("\r\n lis working \r\n");
    }
    else
    printf("\r\n lis not working \r\n");

    lis_write_reg(TEMP_CFG_REG, 0x00); //ADC & TEMP Disable
    lis_write_reg(CTRL_REG1, 0x17); //1 hz
    //lis_write_reg(CTRL_REG2,
    lis_write_reg(CTRL_REG3, 0x7C); // INTRPT and DRDR enable
    lis_write_reg(CTRL_REG4, 0x00); //Full scale=2g, SPi 4 wire, Self test disable
    lis_write_reg(CTRL_REG5, 0x10); //FIFO enable
    lis_write_reg(CTRL_REG6, 0x18);
    //lis_write_reg(REFERENCE
    lis_write_reg(FIFO_CTRL_REG, 0x20);
    lis_write_reg(IG1_CFG,0x70);
    lis_write_reg(IG1_THS,0x2f);
    //lis_write_reg(IG1_DURATION
    //lis_write_reg(IG2_CFG
    //lis_write_reg(IG2_THS
    //lis_write_reg(IG2_DURATION
    lis_write_reg(CLICK_CFG, 0x2A);
    lis_write_reg(CLICK_THS,0x2f);
    lis_write_reg(TIME_LIMIT,0xff);
    lis_write_reg(TIME_LATENCY, 0x64);
    lis_write_reg(TIME_WINDOW, 0x1f);
    //lis_write_reg(Act_THS
    //lis_write_reg(Act_DUR

    }

    void lis_caliberation()
    {
    int8_t v,bit;

    if(nrf_gpio_pin_read(LIS_INT1))
    {
    v = lis_read_reg(CLICK_SRC);
    bit = (v >> 5) & 1U;

    if(bit)


    NRF_LOG_INFO("Double Tap");
    printf("\r\n double tap working \r\n");
    nrf_delay_ms(200);
    }
    }

    void lis_power_off()
    {
    nrf_gpio_pin_clear(LIS_VCC);
    }

    int main(void)
    { //ret_code_t err_code ;
    // bsp_board_init(BSP_INIT_LEDS);
    log_init();
    printf("\r\n Acceleration started.\r\n");

    acceleration_init();

    }

  • I suggest you scope the SPI bus with a logical analyzer and share the result with us. 

Related