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

Error in establishing communication between ADT7410 and nRF52840.

Hey hi,

I am trying to establish communication between ADT7410 and nRF52840 using I2C that is the TWI module.

I have successfully checked the address of the ADT7410 with nRF52840.

The output of so is as follow:

00> <info> app: Application Started
00>
00> <info> app: Entered the twi_init loop
00>
00> <info> app: twi is been initalized
00>
00> <info> app: error_code is : 0x0
00>
00> <info> app: Successfully detected a device at address : 0x48
00>

But unable to get the temperature value from the sensor.

Tried debugging through NRF_LOG_INFO

the program doesn't print "The Error code 2 is %x" from the ADT7410_set_mode.

ADT7410_set_mode function is as follow:

void ADT7410_set_mode(void)
{
NRF_LOG_INFO("We have entered ADT7410 set mode");
NRF_LOG_FLUSH();
ret_code_t err_code;

/* Writing to LM75B_REG_CONF "0" set temperature sensor in NORMAL mode. */
uint8_t reg[2] = {ADT7410_REG_CONF, NORMAL_MODE};
err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg, sizeof(reg), false);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("The Error code 1 is %x",err_code);
NRF_LOG_FLUSH();
while (m_xfer_done == false);

/* Writing to pointer byte. */
reg[0] = ADT7410_REG_TEMP;
m_xfer_done = false;
err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg,1, false);
APP_ERROR_CHECK(err_code);
NRF_LOG_INFO("The Error code 2 is %x",err_code);
NRF_LOG_FLUSH();
while (m_xfer_done == false);
}

Please do guide me to solve this issue.

Parents
  • So what debugging have you done to find out what's going on?

    Have you looked at the I2C wires with an analyser or scope to see what's happening?

    Have you carefully checked against the ADT7410 datasheet that you have everything correct - both hardware and the I2C messages?

  • Yes, I have checked the hardware with DSO, and it's working perfectly.

    /**
     * Copyright (c) 2015 - 2020, 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 "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    /* TWI instance ID. */
    #define TWI_INSTANCE_ID     0
    //#define APP_UART_DRIVER_INSTANCE 1
    
    /* Common addresses definition for temperature sensor. */
    #define ADT7410_ADDR          (0x48U)
    
    #define ADT7410_REG_TEMP      0x00U
    #define ADT7410_REG_STATUS    0x02U
    #define ADT7410_REG_CONF      0x03U
    
    /* Mode for ADT7410. */
    #define NORMAL_MODE 0x40U
    
    /* Indicates if operation on TWI has ended. */
    static volatile bool m_xfer_done = false;
    
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    /* Buffer for samples read from temperature sensor. */
    static uint8_t m_sample;
    
    /**
     * @brief Function for setting active mode on MMA7660 accelerometer.
     */
    void ADT7410_set_mode(void)
    {
    	  NRF_LOG_INFO("We have entered ADT7410 set mode");
    	  NRF_LOG_FLUSH();
        ret_code_t err_code;
         
        /* Writing to LM75B_REG_CONF "0" set temperature sensor in NORMAL mode. */
        uint8_t reg[2] = {ADT7410_REG_CONF, NORMAL_MODE};
        err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg, sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
    		NRF_LOG_INFO("The Error code 1 is %x",err_code);
    	  NRF_LOG_FLUSH();
        while (m_xfer_done == false);
    
        /* Writing to pointer byte. */
        reg[0] = ADT7410_REG_TEMP;
        m_xfer_done = false;
        err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg,1, false);
        APP_ERROR_CHECK(err_code);
    		NRF_LOG_INFO("The Error code 2 is %x",err_code);
    	  NRF_LOG_FLUSH();
        while (m_xfer_done == false);
    }
    
    /**
     * @brief Function for handling data from temperature sensor.
     *
     * @param[in] temp          Temperature in Celsius degrees read from sensor.
     */
    __STATIC_INLINE void data_handler(uint8_t temp)
    {
        NRF_LOG_INFO("Temperature: %d Celsius degrees.", temp);
    	  NRF_LOG_FLUSH();
    }
    
    /**
     * @brief TWI events handler.
     */
     void twi_handler(nrf_drv_twi_evt_t const * p_event, void * p_context)
    {
        switch (p_event->type)
        {
            case NRF_DRV_TWI_EVT_DONE:
    					  NRF_LOG_INFO("We have entered NRF_DRV_TWI_EVT_DONE");
    				    NRF_LOG_FLUSH();
                if (p_event->xfer_desc.type == NRF_DRV_TWI_XFER_RX)
                {
                    data_handler(m_sample);
                }
                m_xfer_done = true;
                break;
            default:
                break;
        }
    }
    
    /**
     * @brief UART initialization.
     */
    void twi_init (void)
    {
    	  NRF_LOG_INFO("We have entered twi_init");
    	  NRF_LOG_FLUSH();
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_400K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi);
    }
    
    /**
     * @brief Function for reading data from temperature sensor.
     */
    static void read_sensor_data()
    {
    	  NRF_LOG_INFO("We have entered read_sensor_data ");
    	  NRF_LOG_FLUSH();
        m_xfer_done = false;
    
        /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
        ret_code_t err_code = nrf_drv_twi_rx(&m_twi, ADT7410_ADDR, &m_sample, 2);
        APP_ERROR_CHECK(err_code);
    	  NRF_LOG_INFO("The Error code 3 is %x",err_code);
    	  NRF_LOG_FLUSH();
    }
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
        NRF_LOG_DEFAULT_BACKENDS_INIT();
    
        NRF_LOG_INFO("\r\nTWI sensor example started.");
        NRF_LOG_FLUSH();
        twi_init();
        ADT7410_set_mode();
    
        while (true)
        {
            nrf_delay_ms(500);
    
            do
            {
                __WFE();
            }while (m_xfer_done == false);
    				
    				NRF_LOG_INFO("Operatino of receiving is completed ");
    	      NRF_LOG_FLUSH();
    
            read_sensor_data();
            NRF_LOG_FLUSH();
        }
    }
    
    /** @} */
    

  • It is entering ADT7410_set_mode but unable to do anything in it.

    void ADT7410_set_mode(void)
    {
    NRF_LOG_INFO("We have entered ADT7410 set mode");
    NRF_LOG_FLUSH();
    ret_code_t err_code;

    /* Writing to ADT7410_REG_CONF "0" set temperature sensor in NORMAL mode. */
    uint8_t reg1[2] = {ADT7410_REG_CONF, NORMAL_MODE};
    err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg1, sizeof(reg1), false);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("The Error code 1 is %x",err_code);
    NRF_LOG_FLUSH();
    while (m_xfer_done == false);

    /* Writing to pointer byte. */
    uint8_t reg2[2] = {ADT7410_REG_TEMP,0x00};
    m_xfer_done = false;
    err_code = nrf_drv_twi_tx(&m_twi, ADT7410_ADDR, reg2,1, false);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("The Error code 2 is %x",err_code);
    NRF_LOG_FLUSH();
    while (m_xfer_done == false);
    }

  • It is entering ADT7410_set_mode but unable to do anything in it

    What, exactly, do you mean by that?

  • I have posted the complete code previously only, just specifying the part of it.

    There is an error while setting the configuration or while asking for temperature value because there specific NRF_LOG_INFO are not getting print on RTT.

    Till that point, everything is getting perfectly print on RTT.

  • always post code using 'Insert code', otherwise it appears as a mess - as in your recent post.

    There is an error

    What error, exactly?

    Step through the code -using the debugger - to see exactly what is happening

  • Debugger stops at 51st line of the code which is while  "(m_xfer_done == false);".

    Which simply signifies that the transfer is not taking place properly.

    I have tried my best to read the ADT7410 datasheet, don't know if miss something.

    Any possible guidance will be very helpful.

Reply Children
  • signifies that the transfer is not taking place properly

    Specifically, that m_xfer_done is never getting set to true.

    So check your code to see where it should get set to true - is that code ever called?

    Again, you really need to see what's happening on the wires - is there any activity at all? if there is, is it correct? etc, etc ... ?

  • There is no activity on the wire, I have checked that using DSO.

    In SHT21 there was SCL and SDA signal that was passed to SHT21 by nRF52840, but SHT21 was not responding. There was a small burst of signal, which I use to hold and then expand in DSO.

    Whereas here there are no signals that are coming out of nRF52840.

  • When did it change from ADT7410 to SHT21 ?

    There is no activity on the wire

    So you need to find out why that is!

    Are the lines high or low?

    It could be a broken wire - so make sure you check as close as possible to the nRF, and also check as close as possible to the ADT7410 (or is it a SHT21 now?)

    Have you checked that you can actually set the pins high & low from code?

    In  your opening post:

    I have successfully checked the address of the ADT7410 with nRF52840

    so how, exactly, did you do that?

    You also said in an early reply:

    I have checked the hardware with DSO, and it's working perfectly

    So what's changed?

  • #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 "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    /* TWI instance ID. */
    #define TWI_INSTANCE_ID     0
    
    /* TWI instance. */
    static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);
    
    /**
     * @brief UART initialization.
     */
    void twi_init (void)
    {
      NRF_LOG_INFO("Entered the twi_init loop");
    	NRF_LOG_FLUSH();
    	ret_code_t err_code;
    
       const nrf_drv_twi_config_t twi_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
        APP_ERROR_CHECK(err_code);
        
        nrf_drv_twi_enable(&m_twi);
    }
    
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
        ret_code_t err_code;
    	
    	  uint8_t address     = (0x48);
    	  uint8_t status      = (0x02);
    	  uint8_t sample_data = (0x00);
    	
    	  APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    	
    	  NRF_LOG_DEFAULT_BACKENDS_INIT();
    	
    	  NRF_LOG_INFO("Application Started");
    	  NRF_LOG_FLUSH();
    	
    		twi_init();
    		NRF_LOG_INFO("twi is been initalized");
    	  NRF_LOG_FLUSH();
    	
    			 err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
    	     APP_ERROR_CHECK(err_code);
    			 NRF_LOG_INFO("error_code is : 0x%x",err_code);
    			 NRF_LOG_FLUSH();
    	
    	if(err_code == NRF_SUCCESS)
    	{
    		NRF_LOG_INFO("Successfully detected a device at address : 0x%x", address);
        NRF_LOG_FLUSH();
    	}
    	
        while (true)
        {
    			//Empty Loop.
        }
    }
    
    /** @} */

    Code for checking the address of the ADT7410 with nRF52840.

    The hardware is ADT7410 only.

    Have checked each and every connection it is working perfectly, the error is from the software side only.

    It isn't transmitting the signal properly and that why the NRF_DRV_TWI_EVT_DONE event is never raised.

  • Also, the SCL line is low and the SDA line is high that's the only thing that can be observed on DSO.

    I am using the twi_init function as it is, nothing is changing on it, so there should not be any error in initializing the pins.

Related