This thread has been locked.

If you have a related question, please click the "Ask a related question" button in the top right corner. The newly created question will be automatically linked to this question.

AFE49I30: Problem on communicating with I2C interface.

Part Number: AFE49I30
Other Parts Discussed in Thread: ADS1292R

I have written a code for initializing and controlling AFE49I30. I have carefully read the datasheet. The simple driver file (.c file) is already completed. However, MCU starts the connection with AFE49I30, but the connection is unsuccessful. I use nrf52840 as MCU. Since writing the driver for this chip can be complex, can you look at my code for any possible mistakes? The main file belongs to a previous project that uses ADS1292R. However, the possible problems may be found in driver files (AFE49I30.c and AFE49I30.h) that can be found in the attachment. the relevant function calls in the main file are:
twi_init();
init_AFE49I30();
AFE49I30_read_all_regs();
AFE49I30_read_data((uint32_t *)&AFE_raw_data);

Please keep this inquiry private as this chip has NDA restrictions.

/************************************************************************
                          include libraries

************************************************************************/

#include <string.h>
#include "nrf_drv_twi.h"
#include "nrf_delay.h"
#include "boards.h"
#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"
#include "AFE49I30.h"
//#include "nrf_drv_gpiote.h"
//#include "app_error.h"
//#include "app_util_platform.h"
//#include "nrf_gpio.h"
//#include "nrf_drv_spi.h"



/**********************************************************************
                           AFE49I30 Global Variables										                                  											  *

/**********************************************************************/
/* Common addresses definition */
#define AFE49I30_ADDR          0x5B

#define page0_size             58
#define page1_size             120


uint8_t AFE49I30_page1_address[page1_size];
uint32_t AFE49I30_page1_data[page1_size];
uint8_t AFE49I30_page0_address[page0_size];
uint32_t AFE49I30_page0_data[page0_size];
/**********************************************************************
                            I2C define

/*********************************************************************/
/* 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);
/* Indicates if operation on TWI has ended. */
static volatile bool m_xfer_done = false;
static int twi_error=0;
/* Buffer for samples read fromsensor. */
static uint32_t m_sample;




/**********************************************************************
                           AFE49I30 functions									                                  											  *

/**********************************************************************/

/*--------------------------------------------------------------*/
/**
 * @brief Function for reading data from AFE49I30 through I2C
 *
 * @param[in] register address
 */
uint32_t AFE_Reg_Read(uint8_t reg_address)
{
    ret_code_t err_code;
    uint8_t configData[3];
    signed long retVal;

    err_code = nrf_drv_twi_tx(&m_twi, AFE49I30_ADDR, (uint8_t *)&reg_address, 1, true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);

    //I2C_read
    err_code = nrf_drv_twi_rx(&m_twi, AFE49I30_ADDR, (uint8_t *)&configData, 3);
    APP_ERROR_CHECK(err_code);

    retVal = configData[0];
    retVal = (retVal << 8) | configData[1];
    retVal = (retVal << 8) | configData[2];
    //if (reg_address >= 0x2A && reg_address <= 0x2F)
    //{
    //  if (retVal & 0x00200000)  // check if the ADC value is positive or negative
    //  {
    //    retVal &= 0x003FFFFF;   // convert it to a 22 bit value
    //    return (retVal^0xFFC00000);
    //  }
    //}
    retVal &= 0x003FFFFF;   // convert it to a 22 bit value
    return retVal;
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for writting data to AFE49I30 through I2C
 *
 * @param[in] register address and its content
 */
void AFE_Reg_Write (uint8_t reg_address, uint32_t data)
{
    ret_code_t err_code;
    uint8_t configData[3];
    //NRF_LOG_INFO("%x",data);

    configData[0]=(uint8_t) ((data & 0x00FFFFFF) >>16);
    configData[1]=(uint8_t) ((data & 0x0000FFFF) >>8);
    configData[2]=(uint8_t) (data & 0x000000FF);
    //I2C_write
    err_code = nrf_drv_twi_tx(&m_twi, AFE49I30_ADDR, (uint8_t *)&reg_address, 1, true);
    APP_ERROR_CHECK(err_code);
    NRF_LOG_INFO("test0,%x,%x,%x",AFE49I30_ADDR,reg_address,err_code);
    while (m_xfer_done == false)
    {
    //NRF_LOG_INFO("twi_error:%d",twi_error);
    }
    NRF_LOG_INFO("test1");
    err_code = nrf_drv_twi_tx(&m_twi, AFE49I30_ADDR, (uint8_t *)&configData, 3, true);
    APP_ERROR_CHECK(err_code);
    while (m_xfer_done == false);
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for page selection of AFE49I30.
 *
 * @param[in] page number
 */
void AFE49I30_page_sel(short page_num)
{
  uint8_t address=0x01;
  uint32_t page1=0x00000001;
  uint32_t page0=0x00000000;
  if(page_num==1) AFE_Reg_Write(address, page1);
  else if(page_num==0) AFE_Reg_Write(address, page0);
  else;
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for reading data from AFE49I30.
 *
 * @param[out] raw data
 */
void AFE49I30_read_data(uint32_t *raw_data)
{
  uint8_t FIFO_add = 0xFF;
  for (int i=0;i<Num_Phase;i++)
  {
    raw_data[i] = AFE_Reg_Read(FIFO_add); 
  }
  return;
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for reading AFE49I30 all registers.
 *
 */
void AFE49I30_read_all_regs(void)
{
  short page_num;
  nrf_delay_ms(1);
  uint32_t current_read;
  //---------read page1 registers------
  page_num=1;
  AFE49I30_page_sel(page_num);
  for (int i=0;i<page1_size;i++)
  {
    current_read = AFE_Reg_Read(AFE49I30_page1_address[i]); 
    NRF_LOG_INFO("reg:%x,%x\r\n",AFE49I30_page1_address[i], current_read); 
  }
  //---------read page0 registers------
  page_num=0;
  AFE49I30_page_sel(page_num);
  for (int i=0;i<page0_size;i++)
  {
    current_read = AFE_Reg_Read(AFE49I30_page0_address[i]);
    NRF_LOG_INFO("reg:%x,%x\r\n",AFE49I30_page0_address[i], current_read);  
  }
  //---------read enable register-------
  uint8_t enable_register_add=0x1D;
  current_read = AFE_Reg_Read(enable_register_add);  
  NRF_LOG_INFO("reg:%x,%x\r\n",enable_register_add, current_read);
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for initializing AFE49I30.
 *
 */
void init_AFE49I30(void)
{
  short page_num;
  nrf_delay_ms(1);
  AFE49I30_Hard_reset();
  nrf_delay_ms(1);
  //---------write page1 registers--------
  page_num=1;
  AFE49I30_page_sel(page_num);
  NRF_LOG_INFO("test2");
  for (int i=0;i<page1_size;i++)
  {
    NRF_LOG_INFO("test3");
    NRF_LOG_INFO("reg:%x,%x\r\n",AFE49I30_page1_address[i],AFE49I30_page1_data[i]);
    AFE_Reg_Write(AFE49I30_page1_address[i], AFE49I30_page1_data[i]);
  }
  //---------write page0 registers------
  page_num=0;
  AFE49I30_page_sel(page_num);
  for (int i=0;i<page0_size;i++)
  { 
    AFE_Reg_Write(AFE49I30_page0_address[i], AFE49I30_page0_data[i]);  
  }
  //---------write enable register-------
  uint8_t enable_register_add=0x1D;
  uint32_t enable_register_dat=0x00C000A0;
  AFE_Reg_Write(enable_register_add,enable_register_dat);  
}


/*--------------------------------------------------------------*/
/**
 * @brief Function for handling data.
 *
 * @param[in] temp
 */
__STATIC_INLINE void data_handler(uint32_t temp)
{
    NRF_LOG_INFO("Data is: %d", 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;
        case NRF_DRV_TWI_EVT_ADDRESS_NACK:
            twi_error = 1;
            break;
        case NRF_DRV_TWI_EVT_DATA_NACK:
            twi_error = 2;
            break;
        default:
            break;
    }
}


/*--------------------------------------------------------------*/
/**
 * @brief TWI initialization.
 *
 */
void twi_init (void)
{
    ret_code_t err_code;
    const nrf_drv_twi_config_t twi_config = {
       .scl                = SCL1,
       .sda                = SDA1,
       .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, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);
    nrf_drv_twi_enable(&m_twi);
    // To secure correct signal levels on the pins used by the TWI master when the system is in OFF mode, and
    // when the TWI master is disabled, SDA and SCL pins must be configured in the GPIO peripheral as follows:
    // SCL As specified in PSEL.SCL: Input H0D1
    // SDA As specified in PSEL.SDA: Input H0D1
    nrf_gpio_cfg(SDA1, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_H0D1, NRF_GPIO_PIN_NOSENSE);
    nrf_gpio_cfg(SCL1, NRF_GPIO_PIN_DIR_INPUT, NRF_GPIO_PIN_INPUT_CONNECT, NRF_GPIO_PIN_PULLUP, NRF_GPIO_PIN_H0D1, NRF_GPIO_PIN_NOSENSE);
    ////////#define  PIN_VDD_ENV    (22)
    ////////#define  PIN_R_PULLUP   ( 0+32)

    ////////nrf_gpio_pin_set(PIN_VDD_ENV);
    ////////nrf_gpio_pin_set(PIN_R_PULLUP);
    ////////// Power up and wait for voltage to rise
    ////////nrf_gpio_cfg(PIN_VDD_ENV,  NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0H1, NRF_GPIO_PIN_NOSENSE);
    ////////nrf_gpio_cfg(PIN_R_PULLUP, NRF_GPIO_PIN_DIR_OUTPUT, NRF_GPIO_PIN_INPUT_DISCONNECT, NRF_GPIO_PIN_NOPULL, NRF_GPIO_PIN_S0H1, NRF_GPIO_PIN_NOSENSE);
    nrf_delay_ms(20);
}


/*--------------------------------------------------------------*/
/**
 * @AFE49I30 hardware reset
 *
 */
void AFE49I30_Hard_reset(void)
{
  nrf_gpio_pin_set(AFE49I30_reset_pin);
  nrf_delay_us(30);
  nrf_gpio_pin_clear(AFE49I30_reset_pin);
  nrf_delay_us(30);
  nrf_gpio_pin_set(AFE49I30_reset_pin);
}
/*--------------------------------------------------------------*/

/*****************************************************************************
 
                    AFE49I30 page1 registers                                

*****************************************************************************/
uint8_t AFE49I30_page1_address[page1_size] = {
0x20,
0x21,
0x22,
0x23,
0x24,
0x25,
0x26,
0x27,
0x28,
0x29,
0x2A,
0x2B,
0x2C,
0x2D,
0x2E,
0x2F,
0x30,
0x31, 
0x32, 
0x33, 
0x34, 
0x35, 
0x36, 
0x37, 
0x38, 
0x39, 
0x3A, 
0x3B, 
0x3C, 
0x3D, 
0x3E, 
0x3F, 
0x40, 
0x41, 
0x42, 
0x43, 
0x44, 
0x45, 
0x46, 
0x47, 
0x48, 
0x49, 
0x4A, 
0x4B, 
0x4C, 
0x4D, 
0x4E, 
0x4F, 
0x50, 
0x51, 
0x52, 
0x53, 
0x54, 
0x55, 
0x56, 
0x57, 
0x58, 
0x59, 
0x5A, 
0x5B, 
0x5C, 
0x5D, 
0x5E, 
0x5F, 
0x60, 
0x61, 
0x62, 
0x63, 
0x64, 
0x65, 
0x66, 
0x67, 
0x68, 
0x69, 
0x6A, 
0x6B, 
0x6C, 
0x6D, 
0x6E, 
0x6F, 
0x70, 
0x71, 
0x72, 
0x73, 
0x74, 
0x75, 
0x76, 
0x77, 
0x78, 
0x79, 
0x7A, 
0x7B, 
0x7C, 
0x7D, 
0x7E, 
0x7F, 
0x80, 
0x81, 
0x82, 
0x83, 
0x84, 
0x85, 
0x86, 
0x87, 
0x88, 
0x89, 
0x8A, 
0x8B, 
0x8C, 
0x8D, 
0x8E, 
0x8F, 
0x90, 
0x91, 
0x92, 
0x93, 
0x94, 
0x95, 
0x96, 
0x97
};

uint32_t AFE49I30_page1_data[page1_size] = {
0x010000, 
0x0077B1, 
0x02000E, 
0x0017B0, 
0x050000, 
0x010001, 
0x00B4B1, 
0x02000E, 
0x0014B0, 
0x850014, 
0x010000, 
0x0054B1, 
0x06000E, 
0x0014B0, 
0x050000, 
0x000000, 
0x000021, 
0x080002, 
0x000020, 
0x000000, 
0x010000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x010000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x040000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x040000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x080000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x080000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x080000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x080000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x020000, 
0x000000, 
0x000000, 
0x000000, 
0x000000
};


/********************************************************************
 
                    AFE49I30 page0 registers (except 1Dh)                                

*********************************************************************/
uint8_t AFE49I30_page0_address[page0_size] = {
0x00, 
0x01, 
0x02, 
0x03, 
0x04,  
//0x1D, 
0x1E, 
0x23, 
0x24, 
0x25, 
0x29,  
0x42,  
0x4B, 
0x4E, 
0x4F, 
0x50, 
0x51, 
0x57, 
0x58, 
0x61, 
0x62, 
0x63, 
0x6C, 
0x6D, 
0x72, 
0x73,  
0x78, 
0x79, 
0x80,  
0x88, 
0x8A, 
0x8B, 
0x8C, 
0x8D, 
0x8E, 
0x8F, 
0x91, 
0x92, 
0x93, 
0x94, 
0x95, 
0x96, 
0x97, 
0x98, 
0x99, 
0x9A, 
0x9C, 
0x9D, 
0x9E, 
0xA0, 
0xA1, 
0xA2, 
0xA4, 
0xA5, 
0xA6, 
0xA9,
0xAA,
0xB4
};


uint32_t AFE49I30_page0_data[page0_size] = {
0x000000, 		
0x000000, 
0x600000, 
0x000000, 
0x000000, 
//0xC000A0, 
0x000000, 
0x068000, 
0x002000, 
0x000000, 
0x050000, 
0x0010E0,  
0x000000, 
0xFE8004, 
0x010004, 
0x100000, 
0x000000, 
0x000000, 
0x000000, 
0x080000, 
0x800000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x047C3C, 
0x80E005, 
0x00E005, 
0x000000, 
0x000003, 
0x000000, 
0x000100, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x000000, 
0x000084, 
0x000050, 
0x000000, 
0x000023,  
0x000000, 
0x200355, 
0x000000, 
0x000400, 
0x000015, 
0x000000, 
0x000400, 
0x000015, 
0x000000, 
0x000400, 
0x000015, 
0x000000, 
0x000000, 
0x000000, 
0x000000
};

#ifndef AFE49I30_H_
#define AFE49I30_H_


/******************************************************************************
                               AFE49I30 global definitions
******************************************************************************/
#define Num_Phase           6

/******************************************************************************
                               AFE49I30 pins definition
******************************************************************************/
#define AFE49I30_reset_pin  16
#define AFE49I30_DRDY_pin   25



/******************************************************************************
                               AFE49I30 global functions
******************************************************************************/
void twi_init (void);
void init_AFE49I30(void);
void AFE49I30_read_all_regs(void);
void AFE49I30_read_data(uint32_t *raw_data);
void AFE49I30_Hard_reset(void);



#endif /*AFE49I30_H_*/