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

Acceleraton sensor with nrf52DK

Hello,

Im trying to interface LIS3DH with nRF52DK referring this example https://devzone.nordicsemi.com/f/nordic-q-a/34710/lis2dh12-driver-issue-in-nrf-sdk-v15-0.

Im getting the following result, the control doesnt enter "print_identity " function and the results are as follows,

Can you help me find out how to read the values of acceleration with this code and interpret the value read?

/**
 * Copyright (c) 2015 - 2018, Nordic Semiconductor ASA
 * 
 * All rights reserved.
 * 
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 * 
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 * 
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 * 
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 * 
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 * 
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 * 
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 * 
 */
/** @file
 * @defgroup tw_sensor_example main.c
 * @{
 * @ingroup nrf_twi_example
 * @brief TWI Sensor Example main file.
 *
 * This file contains the source code for a sample application using TWI.
 *
 */




#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#include "app_gpiote.h"

#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

#include "lis2dh12.h"

#include "nrf_twi_mngr.h"

/* TWI instance ID. */
#define TWI_INSTANCE_ID             0

#define MAX_PENDING_TRANSACTIONS    33

#define LIS2DH12_MIN_QUEUE_SIZE     32




NRF_TWI_MNGR_DEF(m_nrf_twi_mngr, MAX_PENDING_TRANSACTIONS, TWI_INSTANCE_ID);

NRF_TWI_SENSOR_DEF(m_nrf_twi_sensor, &m_nrf_twi_mngr, LIS2DH12_MIN_QUEUE_SIZE);

LIS2DH12_INSTANCE_DEF(m_lis2dh12, &m_nrf_twi_sensor, LIS2DH12_BASE_ADDRESS_LOW);

static uint8_t m_sample = 0;

void print_identity(ret_code_t r, void *p_register_data)
{
    NRF_LOG_INFO("Identity: %d", *((uint8_t *)p_register_data));
}

/**
 * @brief Function for main application entry.
 */
int main(void)
{
    uint32_t err;

    nrf_drv_twi_config_t const config = {
       .scl                = 27,
       .sda                = 26,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_LOWEST,
       .clear_bus_init     = false
    };


    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("TWI sensor example started.");
    NRF_LOG_FLUSH();

    err = nrf_twi_mngr_init(&m_nrf_twi_mngr, &config);
    APP_ERROR_CHECK(err);

    err = nrf_twi_sensor_init(&m_nrf_twi_sensor);
    APP_ERROR_CHECK(err);

    err = lis2dh12_init(&m_lis2dh12);
    APP_ERROR_CHECK(err);

    NRF_LOG_FLUSH();
    APP_ERROR_CHECK(err);
 
    while (true)
    {
        nrf_delay_ms(500);
         NRF_LOG_INFO("Here3.");
        err = lis2dh12_who_am_i_read(&m_lis2dh12, print_identity, &m_sample);
        APP_ERROR_CHECK(err);
        NRF_LOG_FLUSH();
    }
}



























































































































/**
 * Copyright (c) 2017 - 2019, Nordic Semiconductor ASA
 *
 * All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without modification,
 * are permitted provided that the following conditions are met:
 *
 * 1. Redistributions of source code must retain the above copyright notice, this
 *    list of conditions and the following disclaimer.
 *
 * 2. Redistributions in binary form, except as embedded into a Nordic
 *    Semiconductor ASA integrated circuit in a product or a software update for
 *    such product, must reproduce the above copyright notice, this list of
 *    conditions and the following disclaimer in the documentation and/or other
 *    materials provided with the distribution.
 *
 * 3. Neither the name of Nordic Semiconductor ASA nor the names of its
 *    contributors may be used to endorse or promote products derived from this
 *    software without specific prior written permission.
 *
 * 4. This software, with or without modification, must only be used with a
 *    Nordic Semiconductor ASA integrated circuit.
 *
 * 5. Any software provided in binary form under this license must not be reverse
 *    engineered, decompiled, modified and/or disassembled.
 *
 * THIS SOFTWARE IS PROVIDED BY NORDIC SEMICONDUCTOR ASA "AS IS" AND ANY EXPRESS
 * OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
 * OF MERCHANTABILITY, NONINFRINGEMENT, AND FITNESS FOR A PARTICULAR PURPOSE ARE
 * DISCLAIMED. IN NO EVENT SHALL NORDIC SEMICONDUCTOR ASA OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE
 * GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
 *
 */

#include "lis2dh12.h"

#define RETURN_IF_ERR(err)  \
    if (err != NRF_SUCCESS) \
    {                       \
        return err;         \
    }

ret_code_t lis2dh12_init(lis2dh12_instance_t * p_inst)
{
    ASSERT(p_inst != NULL);
    memset(&p_inst->temp_cfg, 0, &p_inst->act_dur - &p_inst->temp_cfg);
    p_inst->ctrl0     = LIS2DH12_CTRL_REG0_VALID_SET;
    p_inst->ctrl1     = 0x07;
    p_inst->ctrl4     = 0x80;

    return lis2dh12_cfg_commit(p_inst);
}

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;
}

ret_code_t lis2dh12_data_read(lis2dh12_instance_t * p_inst,
                              lis2dh12_data_cb_t    user_cb,
                              lis2dh12_data_t *     p_data,
                              uint8_t               samples)
{
    ASSERT(p_inst != NULL);
    return nrf_twi_sensor_reg_read(p_inst->p_sensor_data,
                                   p_inst->sensor_addr,
                                   LIS2DH12_REG_OUT_X_L | LIS2DH12_AUTO_INCR_MASK,
                                   (nrf_twi_sensor_reg_cb_t) user_cb,
                                   (uint8_t *) p_data,
                                   samples * LIS2DH12_BYTES_PER_SAMPLE);
}

ret_code_t lis2dh12_temp_enable(lis2dh12_instance_t * p_inst, bool temp_en)
{
    ASSERT(p_inst != NULL);
    if (temp_en == true)
    {
        NRF_TWI_SENSOR_REG_SET(p_inst->temp_cfg, LIS2DH12_TEMP_EN_MASK, LIS2DH12_TEMP_EN_POS, 3);
    }
    else
    {
        NRF_TWI_SENSOR_REG_SET(p_inst->temp_cfg, LIS2DH12_TEMP_EN_MASK, LIS2DH12_TEMP_EN_POS, 0);
    }

    uint8_t send_msg[] = {
        LIS2DH12_REG_TEMP_CFG_REG,
        p_inst->temp_cfg
    };

    return nrf_twi_sensor_write(p_inst->p_sensor_data,
                                p_inst->sensor_addr,
                                send_msg,
                                ARRAY_SIZE(send_msg),
                                true);

}

ret_code_t lis2dh12_temp_read(lis2dh12_instance_t * p_inst,
                              lis2dh12_temp_cb_t    user_cb,
                              int16_t *             p_temp)
{
    ASSERT(p_inst != NULL);
    return nrf_twi_sensor_reg_read(p_inst->p_sensor_data,
                                   p_inst->sensor_addr,
                                   LIS2DH12_REG_OUT_TEMP_L | LIS2DH12_AUTO_INCR_MASK,
                                   (nrf_twi_sensor_reg_cb_t) user_cb,
                                   (uint8_t *) p_temp,
                                   LIS2DH12_BYTES_PER_TEMP);
}
lis2dh12.h

Parents
  • Hi,

    It does not look like the function lis2dh12_who_am_i_read is implemented in the lis2dh12.c file. Have you implemented this function elsewhere?

    Best regards,
    Jørgen

  • I cannot see any obvious issues that should cause this. Can you upload the full project, for us to reproduce and debug this?

  • I tested the project, but I'm seeing the handler being called:

    Note that I got some errors due to missing source files and include paths. Specifically, the nrf_tw_mngr.c file seems to be located in a strange location in the project you uploaded (components/drivers_nrf/twi_master/nrf_twi_mngr.c). I included it from the default location (components/libraries/twi_mngr/nrf_twi_mngr.c). I also did not have anything connected to the TWI pins when running my test.

  • Hello,

    i strated from beginning in new SDK folder, and have changed the path as you mentioned. 

    I copied and created a custom twi sensor folder.

    Copied the main.c file. 

    add .c files 

    add .h files

    Replaced the flash_placement file

    add below code in sdk_config

    #ifndef NRF_TWI_MNGR_ENABLED
    #define NRF_TWI_MNGR_ENABLED 1
    #endif

    #ifndef NRF_QUEUE_ENABLED
    #define NRF_QUEUE_ENABLED 1
    #endif

    1. should the twi manager be enabled , below is the snapshot of how it looks.

    If this needs to be enabled. How to do so?

    When i debug, i get the below result in debug window. (with no hardware connection to sensor)

    <info> app: TWI sensor example started.
    <info> twi_sensor: Sensor addr: 0x18 Write length 10
    <info> twi_sensor: Sensor addr: 0x18 Write length 12
    <info> twi_sensor: Sensor addr: 0x18 Write length 7
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <info> twi_sensor: Sensor addr: 0x18
    Read reg addr: 0x0F, bytes 1
    <warning> twi_sensor: Memory not allocated.
    <error> app: Fatal error

    How to resolve this?

  • You need to enable it in sdk_config.h:

    // <q> NRF_TWI_MNGR_ENABLED  - nrf_twi_mngr - TWI transaction manager
     
    
    #ifndef NRF_TWI_MNGR_ENABLED
    #define NRF_TWI_MNGR_ENABLED 1
    #endif

  • As you can see in the steps i have followed, i have included this in the sdk_config. But still i face the problem. Can you explain in detail the steps, you followed . Because the control doesnt enter the function print_identity with my code and current settings.

  • If you do not get any compilation errors, the library is either not used, or it is compiled correctly. SES sometimes does not update the GUI unless you close and reopen the file, it may look grayed out even if it is included. Are you calling the functions from twi_mngr in the application?

Reply Children
Related