TWI Scanner Example is not Working in Custom Board (NRF52840)

Hi,

1. Here I am using the nrf52840 in my customized board, in parallel I have nrf52840 dev board. In My custom Onboard debugger is not available so I am using SWD to load the program from NRF52840 DK board to Custom Board (NRF52840).

2. I am loading TWI Scanner Example program to my custom board, from that nothing is displayed on terminal, But If I am flashing same TWI scanner example to Dev boards its working and its displaying like TWI Scanner Started But in my custom board nothing is displayed. 

3. I have mention I2C pin as P0.06 & P0.07, and also I have mention UART in my custom board as P1.11(TX), P1.12(RX), P1.10(CTS), P0.30(RTS).

My doubt why log is not displaying anything in my custom board. and one more doubt also whether I have to mention custom board UART pins in TWI Scanner Example??

I have raised this questions in my previous thread, there is no reply that's why I posed this thread. Please anyone help me on this. I am waiting for your quick response...

Thanks in advance.

  • Hi,

    Thanks for the reply

    You want your UART data (not TWI data) to pass through the FTDI converter and then to your computer's COM terminal. It is the UART lines that you need to connect to the oscilloscope and not the TWI lines------------> Yeah I want to read some ADC data , that ADC having I2C communication, that's why I am using TWI Scanner example, whatever ADC data I am reading, same data only I have to send through UART. This is my task. 

  • Can you please tell me, In  sdk.config.h, whether I have to change anything, to print some data in UART console. please suggest me.

  • namitha K M said:
    2. I have checked that TWI pins in oscilloscope, I did not find any data for both SCL pins , SDA pins and also UART pins.

    You have mentioned that you cannot see any data on the UART pins using Oscilloscope. You need to first figure out why the UART pins does not give any data?

    Are you using XTAL for your HFCLK? If yes, then you might need to start the HFCLK manually. Check this thread for more info. Brute force start can be done like below before starting to configure the UART

    /* Start 16 MHz crystal oscillator */
        NRF_CLOCK->EVENTS_HFCLKSTARTED = 0;
        NRF_CLOCK->TASKS_HFCLKSTART    = 1;
    
        /* Wait for the external oscillator to start up */
        while (NRF_CLOCK->EVENTS_HFCLKSTARTED == 0)
        {
            // Do nothing.
        

  • Jansi, Please read other devzone post regarding why the UART is not able to transmit data to the pins. This does not seem to be an issue with the FTDI converter or your connections, the issue seems more basic that the UART on your custom board is not sending any bytes at all at the source level.

  • Hi,

    Now I am getting some data on RTT viewer, not in termite. But now i am not getting any data for I2C .I have mention my slave address, its detecting the address also, and configuration register I calculated, mention in code also. Is there any register need to mention?. Can you please tell me how to calculate the register value in datasheets (ex:- I am using ADC - MCP3428).

    I will attach my recent code in below attachment and datasheet. can you please help me on this.

    #include <stdbool.h>
    #include <stdint.h>
    #include <string.h>
    #include "nrf.h"
    
    #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 "SEGGER_RTT.h"
    
    #include "nrf_log.h"
    #include "nrf_log_ctrl.h"
    #include "nrf_log_default_backends.h"
    
    
    #define TWI_SCL_M NRF_GPIO_PIN_MAP(0,6)
    #define TWI_SDA_M NRF_GPIO_PIN_MAP(0,7)
    
    #define ADC_ADDRESS_LEN 1
    #define Slave_Address 0xD0
    //#define ADC_WHO_AM_I   0x68U
    
    #define REG_CFG          0x90
    #define REG_RESET        0x06
    #define REG_LATCH        0x04
    #define REG_CONV         0x08
    #define REG_Write        0x06
    #define REG_Read         0x06
    
      uint8_t sample_data = 0x00; // sample data initialized with 0 value.
    
    /* TWI instance ID. */
    #define TWI_INSTANCE_ID     0
    
    
    //#define LM75B_REG_TEMP      0x00U
    //#define LM75B_REG_CONF      0x01U
    //#define LM75B_REG_THYST     0x02U
    //#define LM75B_REG_TOS       0x03U
    
    
    
    /* Mode for LM75B. */
    #define CONTINUOUS_CONVERSION_MODE 0
    
    /* 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 LM75B_set_mode(void)
    {
        ret_code_t err_code;
    
        /* Writing to LM75B_REG_CONF "0" set temperature sensor in NORMAL mode. */
        uint8_t reg[2] = {REG_CFG, CONTINUOUS_CONVERSION_MODE};
    	//	memcpy(&reg,m_sample,ADC_ADDRESS_LEN);
        err_code = nrf_drv_twi_tx(&m_twi, Slave_Address, reg, sizeof(reg), false);
        APP_ERROR_CHECK(err_code);
        while (m_xfer_done == false);
    
        /* Writing to pointer byte. */
      //  reg[0] =0x00;
      //  m_xfer_done = false;
       // err_code = nrf_drv_twi_tx(&m_twi, Slave_Address, reg, 1, false);
       // APP_ERROR_CHECK(err_code);
        //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("ADC Value is: %d\r\n.", temp);
    }
    
    /**
     * @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:
                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)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = TWI_SCL_M,
           .sda                = TWI_SDA_M,
           .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_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()
    {
    	
    	  ret_code_t err_code;
        m_xfer_done = false;
    //	uint8_t reg=0x00;
      //err_code = nrf_drv_twi_tx(&m_twi, Slave_Address, &reg, sizeof(reg), false);
        //APP_ERROR_CHECK(err_code);
        /* Read 1 byte from the specified address - skip 3 bits dedicated for fractional part of temperature. */
        err_code = nrf_drv_twi_rx(&m_twi, Slave_Address, &m_sample, sizeof(m_sample));
        APP_ERROR_CHECK(err_code);
    	    NRF_LOG_INFO(0, "%d ", m_sample);
    
    }
    
    /**
     * @brief Function for main application entry.
     */
    int main(void)
    {
    	
    	  ret_code_t err_code; // a variable to hold error code value
    
        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();
        LM75B_set_mode();
    	  NRF_LOG_INFO(0, "Printing over RTT!\r\n");
    
    
    	
    	//  err_code = nrf_drv_twi_rx(&m_twi, Slave_Address, &sample_data, sizeof(sample_data));
    
    	 //if(err_code == NRF_SUCCESS) // if reading data was successful
      //{
    
        //NRF_LOG_INFO("Successfully detected a device at address: 0x%x", Slave_Address); // let the users know its working
      
        //}
    	//else
    //	{
    	//	NRF_LOG_INFO("No device found");
    	//}
        while (true)
        {
         //   nrf_delay_ms(500);
    
            do
            {
                __WFE();
            }while (m_xfer_done == false);
    
            read_sensor_data();
            NRF_LOG_FLUSH();
        }
    }
    
    /** @} */
    

    https://www.mouser.in/datasheet/2/268/22226a-81911.pdf..

    Thanks in advance Mr. Susheel.

Related