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

How to use lis2dh12 with latest SDK 15.3 (SPI)

Hi I would like to use sdk15.3 with the \nRF5_SDK_15.3.0_59ac345\components\drivers_ext\lis2dh12 but I need to have it with SPI. Is this possible?

  • The code I posted above is all the code required to get it working. The difficult part is navigating sd_config.h. I have the following enabled (all others set to '0')::

    #define NRFX_SPI2_ENABLED 1
    #define NRFX_SPIM2_ENABLED 1
    #define SPI2_ENABLED 1
    #define SPI2_USE_EASY_DMA 1
    

    The various macros mess with these settings, so I don't actually know if they are correct, but it seems to work!

    Start by reading the Id code:

    // Device identification register - LIS3DH has same code as LIS2DH12
    #define WHO_AM_I        0x0F
    #define I_AM_LIS3DH     0x33
    #define I_AM_LIS2DH     0x33
    
    /**
     * @brief SPI event handler indicating SPI transfer has completed
     * @param event
     */
    static void acc_spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *p_context)
    {
        mAccPacketTransferComplete = true;
    }
    
    /**
     * @brief Function Send a command to read Id
     * This function sends a one-byte command to ACC via SPI
     * @param[in] none
     */
    static uint8_t AccWhoAmI(void)
    {
        uint8_t tx_buf[] = {0x80|LIS2DH12_WHO_AM_I, 0xFF};
        uint8_t rx_buf[sizeof(tx_buf)];
        uint16_t length = sizeof(tx_buf);
    
        memset(rx_buf, '?', length);
        mAccPacketTransferComplete = false;
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&mAccSpiInstance, tx_buf, length, rx_buf, length));
        // Check for successful transfer
        while (!mAccPacketTransferComplete) ;
        // Confirm we have a connection, should be I_AM_LIS2DH
        return rx_buf[1];
    }
    

  • Hi hmolesworth,

    thanks, but the lis2dh12 from the drivers_ext are using TWI can we just use it without changing anything? 

    ret_code_t lis2dh12_cfg_commit(lis2dh12_instance_t * p_inst)
    {
        ASSERT(p_inst != NULL);
        ret_code_t err;
        p_inst->ctrl0 &= ~LIS2DH12_CTRL_REG0_VALID_MASK;
        p_inst->ctrl0 |= LIS2DH12_CTRL_REG0_VALID_SET;
    
        uint8_t ctrl_msg[] = {
            LIS2DH12_REG_CTRL_REG0 | LIS2DH12_AUTO_INCR_MASK,
            p_inst->ctrl0,
            p_inst->temp_cfg,
            p_inst->ctrl1,
            p_inst->ctrl2,
            p_inst->ctrl3,
            p_inst->ctrl4,
            p_inst->ctrl5,
            p_inst->ctrl6,
            p_inst->reference
        };
        err = nrf_twi_sensor_write(p_inst->p_sensor_data,
                                   p_inst->sensor_addr,
                                   ctrl_msg,
                                   ARRAY_SIZE(ctrl_msg),
                                   true);
        RETURN_IF_ERR(err);
        uint8_t fifo_msg[] = {
            LIS2DH12_REG_FIFO_CTRL | LIS2DH12_AUTO_INCR_MASK,
            p_inst->fifo_ctrl,
            0,
            p_inst->int1_cfg,
            0,
            p_inst->int1_ths,
            p_inst->int1_dur,
            p_inst->int2_cfg,
            0,
            p_inst->int2_ths,
            p_inst->int2_dur,
            p_inst->click_cfg
        };
        err = nrf_twi_sensor_write(p_inst->p_sensor_data,
                                   p_inst->sensor_addr,
                                   fifo_msg,
                                   ARRAY_SIZE(fifo_msg),
                                   true);
        RETURN_IF_ERR(err);
    
        uint8_t time_msg[] = {
            LIS2DH12_REG_CLICK_THS | LIS2DH12_AUTO_INCR_MASK,
            p_inst->click_ths,
            p_inst->time_lim,
            p_inst->latency,
            p_inst->time_win,
            p_inst->act_ths,
            p_inst->act_dur
        };
        err = nrf_twi_sensor_write(p_inst->p_sensor_data,
                                   p_inst->sensor_addr,
                                   time_msg,
                                   ARRAY_SIZE(time_msg),
                                   true);
        return err;

    I am really confuse that where should I start from what you said.

    // Device identification register - LIS3DH has same code as LIS2DH12
    #define WHO_AM_I        0x0F
    #define I_AM_LIS3DH     0x33
    #define I_AM_LIS2DH     0x33
    
    /**
     * @brief SPI event handler indicating SPI transfer has completed
     * @param event
     */
    static void acc_spi_event_handler(nrf_drv_spi_evt_t const * p_event, void *p_context)
    {
        mAccPacketTransferComplete = true;
    }
    
    /**
     * @brief Function Send a command to read Id
     * This function sends a one-byte command to ACC via SPI
     * @param[in] none
     */
    static uint8_t AccWhoAmI(void)
    {
        uint8_t tx_buf[] = {0x80|LIS2DH12_WHO_AM_I, 0xFF};
        uint8_t rx_buf[sizeof(tx_buf)];
        uint16_t length = sizeof(tx_buf);
    
        memset(rx_buf, '?', length);
        mAccPacketTransferComplete = false;
        APP_ERROR_CHECK(nrf_drv_spi_transfer(&mAccSpiInstance, tx_buf, length, rx_buf, length));
        // Check for successful transfer
        while (!mAccPacketTransferComplete) ;
        // Confirm we have a connection, should be I_AM_LIS2DH
        return rx_buf[1];
    }

    For instance, there do you mean to put these code? in the main.c ? or the lis2dh12 driver.

    Thank you very much !

    sorry that I am really confuse.

  • there is no SPIM for my case, I am using 15.3 SDK nRF5_SDK_15.3.0_59ac345\examples\peripheral\spi_master_using_nrf_spi_mngr

    Is this the same? but there is a LCD with the example which makes the code more complex.

  • Don't use any of the i2c files and settings; SPI is much simpler and I see you don't have i2c anyway ..

  • Hi,

    I believe this is twi mean i2C nrf_twi_sensor_write(p_inst->p_sensor_data,p_inst->sensor_addr,time_msg,ARRAY_SIZE(time_msg),true); can you please let me know how exactly you make this work for spi? where should the place to add your code in. I really need this urgently. 

    Thanks!

Related