'NRF_DRV_TWI_INSTANCE_0' undeclared here (not in a function); did you mean 'NRF_DRV_TWI_INSTANCE_'? while enabling I2C/TWI

Getting this issue while building the code I have tried  error: 'NRF_DRV_TWI_INSTANCE_0' undeclared here  these given options but did not worked out . Please help me with this. I have included all the required driver files also. I am compiling this code in Segger Embedded Studio. I am trying to interface a I2C sensor with NRF52832 dev module.

#define NRF_DRV_TWI_INSTANCE(id)    NRF_DRV_TWI_INSTANCE_(id)
#define NRF_DRV_TWI_INSTANCE_(id)   NRF_DRV_TWI_INSTANCE_ ## id
#if NRFX_CHECK(NRFX_TWIM0_ENABLED)

// <e> NRFX_TWI_ENABLED - nrfx_twi - TWI peripheral driver
//==========================================================
#ifndef NRFX_TWI_ENABLED
#define NRFX_TWI_ENABLED 1
#endif
// <e> TWI_ENABLED - nrf_drv_twi - TWI/TWIM peripheral driver - legacy layer
//==========================================================
#ifndef TWI_ENABLED
#define TWI_ENABLED 1
#endif
// <q> NRFX_TWI0_ENABLED  - Enable TWI0 instance
 

#ifndef NRFX_TWI0_ENABLED
#define NRFX_TWI0_ENABLED 1
#endif

Parents
  • Hi Sarvesh,

    Without knowing more what you are doing, I cannot really tell what is wrong.
    What is your starting point with this code base?
    Is the i2c.c a custom file made by you, or is it from somewhere else?
    What are you including and calling in i2c.c?
    If there are any concern regarding disclosing the code with Nordic, we can go private with this ticket.

    For reference, I just create a new project using the template_peripheral example. I changed the code as below and things just work. I have not tested further if I2C actually works but it should.

    #include "nrf_drv_twi.h"
    
    const nrf_drv_twi_t twi0_instance = NRF_DRV_TWI_INSTANCE(0);
    
    const nrf_drv_twi_config_t twi0_cfg = {
                                  .scl                = 3,
                                  .sda                = 4,
                                  .frequency          = NRF_DRV_TWI_FREQ_400K,
                                  .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
                                  .clear_bus_init     = false,
                                  .hold_bus_uninit    = false,
                                };
    
    void twi0_evt_handler(nrf_drv_twi_evt_t const * p_event,
                          void *                    p_context)
    {
        switch (p_event->type)
        {
            case NRF_DRV_TWI_EVT_DONE:          /* TODO */ break;
            case NRF_DRV_TWI_EVT_ADDRESS_NACK:  /* TODO */ break;
            case NRF_DRV_TWI_EVT_DATA_NACK:     /* TODO */ break;
            default: /* TODO error? */;
        }
    }
    
    /**
     * @brief Function for application main entry.
     */
    int main(void)
    {
        ret_code_t err_code;
        err_code = nrf_drv_twi_init(
                            &twi0_instance,     // nrf_drv_twi_t const *        p_instance,
                            &twi0_cfg,          // nrf_drv_twi_config_t const * p_config,
                            &twi0_evt_handler,  // nrf_drv_twi_evt_handler_t    event_handler,
                            NULL                //void *                       p_context)
                            );
        while (true)
        {
            // Do nothing.
        }
    }

    Best regards,

    Hieu

  • Thanks for your reply.  Issue has been resolved 

Reply Children
No Data
Related