Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

NRF52810 initializing TWI (I2C)

Hello, I am having problems getting the TWI to work. The NRF52810 needs the TWI to communicate with a EEPROM module.

Thus far ive set:

#include "nrf_drv_twi.h"

#define PIN_SDA	 		25
#define PIN_SCL 		28

/* TWI instance ID. */
#if TWI0_ENABLED
#define TWI_INSTANCE_ID     0
#elif TWI1_ENABLED
#define TWI_INSTANCE_ID     1
#endif

static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);

void InitTWI()
{
	
	const nrf_drv_twi_config_t TWI_CONFIG	=	
	{																 
			.frequency          = NRF_DRV_TWI_FREQ_400K, 																 
			.scl                = PIN_SCL,                                               
			.sda                = PIN_SDA,                                               
			.interrupt_priority = APP_IRQ_PRIORITY_HIGH,                       
			.clear_bus_init     = false	
	};

	nrf_drv_twi_init(&m_twi, &TWI_CONFIG, NULL, NULL);
	nrf_drv_twi_enable(&m_twi);
}

Then I try to do the TWI Scanner example but I get no activity (with scope) on the SCL or SDA line. 

Is there something I forgot?

Parents
  • Hi,

    You only initialize the TWI driver in the code snippet in your question. Since there is no code that actually starts a transaction, it is as expected that you don't see any activity on the SCL and SDA lines. You mention the TWI Scanner example, and there you can see that it sets up a read transaction for every address. Using that code (after first initializing as you do), you will see activity on the lines:

        for (address = 1; address <= TWI_ADDRESSES; address++)
        {
            err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
            if (err_code == NRF_SUCCESS)
            {
                detected_device = true;
                NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
            }
            NRF_LOG_FLUSH();
        }

  • This program is added to my project and when debugging it seems like the program gets stuck in a loop of twi_rx_start_transfer and the "hw_timout" part. Any idea what causes this?

    Also do I need TWI, TWIM or TWIS when interfacing the NRF52810 with an I2C eeprom IC?

  • Hi,

    Can you show your code and describe your HW setup and pin configuration? The hw_timeout should normally never happen (which is why it leads to an NRFX_ERROR_INTERNAL / NRF_ERROR_INTERNAL). What debugging have you done? Have you checked the signal on the SCL or SDA lines with a logic analyzer?

    trevortjes said:
    Also do I need TWI, TWIM or TWIS when interfacing the NRF52810 with an I2C eeprom IC?

    In TWI /I2C there is always one master and one or more slaves. The communication is always initiated by the master, so an EEPROM will typically be a slave, and the MCU will be a master. So in that case, you should use the TWIM or TWI peripheral, which are both masters. Using the driver there is no significant difference between TWI and TWIM. If you configure that you want to use DMA, then the TWIM peripheral is used by the driver. If not, then TWI is used. Generally, you should use DMA (TWIM) unless you have a good reason for not doing so.

Reply
  • Hi,

    Can you show your code and describe your HW setup and pin configuration? The hw_timeout should normally never happen (which is why it leads to an NRFX_ERROR_INTERNAL / NRF_ERROR_INTERNAL). What debugging have you done? Have you checked the signal on the SCL or SDA lines with a logic analyzer?

    trevortjes said:
    Also do I need TWI, TWIM or TWIS when interfacing the NRF52810 with an I2C eeprom IC?

    In TWI /I2C there is always one master and one or more slaves. The communication is always initiated by the master, so an EEPROM will typically be a slave, and the MCU will be a master. So in that case, you should use the TWIM or TWI peripheral, which are both masters. Using the driver there is no significant difference between TWI and TWIM. If you configure that you want to use DMA, then the TWIM peripheral is used by the driver. If not, then TWI is used. Generally, you should use DMA (TWIM) unless you have a good reason for not doing so.

Children
Related