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

initial TWI in nrf52832

I have hard time in start a twi in nrf52832 with SDK11.0, I can't initial twi ,whatever i do ,when i build is still an error ,here is my code:

static const nrf_drv_twi_t iic0_acc0_instance = NRF_DRV_TWI_INSTANCE(0);
//const nrf_drv_twi_t m_twi_mma_7660 = NRF_DRV_TWI_INSTANCE(0);

void IIC_init(void)
{
		uint32_t err_code;
		const nrf_drv_twi_config_t iic0_acc0_config = {
		   .scl                = IIC_SCL_PIN,
       .sda                = IIC_SDA_PIN,
       .frequency          = NRF_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_LOW
		};
		err_code = nrf_drv_twi_init(&iic0_acc0_instance,&iic0_acc0_config,NULL,NULL);
		APP_ERROR_CHECK(err_code);
		nrf_drv_twi_enable(&iic0_acc0_instance);
}

when i build it ,there has an error: TWI.png

  • So the compiler is telling you that nrf_drv_twi.c has an error with the message Wrong configuration.. In your other question you got to this part:

    #if (defined(TWIM_IN_USE) && defined(TWI_IN_USE))
        // TWIM and TWI combined
        #define CODE_FOR_TWIM(code) if (p_instance->use_easy_dma) { code }
        #define CODE_FOR_TWI(code)  else { code }
    #elif (defined(TWIM_IN_USE) && !defined(TWI_IN_USE))
        // TWIM only
        #define CODE_FOR_TWIM(code) { code }
        #define CODE_FOR_TWI(code)
    #elif (!defined(TWIM_IN_USE) && defined(TWI_IN_USE))
        // TWI only
        #define CODE_FOR_TWIM(code)
        #define CODE_FOR_TWI(code)  { code }
    #else
        #error "Wrong configuration."
    #endif
    

    So TWIM_IN_USE and/or TWI_IN_USE need to be defined for you to be able to compile the file. They are defined in nrf_drv_twi.h:

    // This set of macros makes it possible to exclude parts of code when one type
    // of supported peripherals is not used.
    #if ((TWI0_ENABLED == 1 && TWI0_USE_EASY_DMA == 1) || \
         (TWI1_ENABLED == 1 && TWI1_USE_EASY_DMA == 1))
        #define TWIM_IN_USE
    #endif
    #if ((TWI0_ENABLED == 1 && TWI0_USE_EASY_DMA != 1) || \
         (TWI1_ENABLED == 1 && TWI1_USE_EASY_DMA != 1))
        #define TWI_IN_USE
    #endif
    

    Which leads us to you needing to #define a combination of those macros. The SDK collects driver definitions in \components\drivers_nrf\config\nrf_drv_config.h:

    #define TWI0_ENABLED 0
    
    #if (TWI0_ENABLED == 1)
    #define TWI0_USE_EASY_DMA 0
    
    #define TWI0_CONFIG_FREQUENCY    NRF_TWI_FREQ_100K
    #define TWI0_CONFIG_SCL          0
    #define TWI0_CONFIG_SDA          1
    #define TWI0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
    
    #define TWI0_INSTANCE_INDEX      0
    #endif
    
    #define TWI1_ENABLED 0
    
    #if (TWI1_ENABLED == 1)
    #define TWI1_USE_EASY_DMA 0
    
    #define TWI1_CONFIG_FREQUENCY    NRF_TWI_FREQ_100K
    #define TWI1_CONFIG_SCL          0
    #define TWI1_CONFIG_SDA          1
    #define TWI1_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
    
    #define TWI1_INSTANCE_INDEX      (TWI0_ENABLED)
    #endif
    
    #define TWI_COUNT                (TWI0_ENABLED + TWI1_ENABLED)
    

    The frequency, scl/sda pins and irq priority are used in a nrf_drv_twi.h macro:

    /**
     * @brief TWI master driver instance default configuration.
     */
    #define NRF_DRV_TWI_DEFAULT_CONFIG(id)                            \
    {                                                                 \
        .frequency          = CONCAT_3(TWI, id, _CONFIG_FREQUENCY),   \
        .scl                = CONCAT_3(TWI, id, _CONFIG_SCL),         \
        .sda                = CONCAT_3(TWI, id, _CONFIG_SDA),         \
        .interrupt_priority = CONCAT_3(TWI, id, _CONFIG_IRQ_PRIORITY) \
    }
    

    So you don't need to care about those if you don't want to.

    Based on your code, all you need to do is change #define TWI0_ENABLED 0 to 1 and you're hopefully one step closer to making it work.

  • In this mode ,I have changed

    #define TWI0_ENABLED 1 
    #if (TWI0_ENABLED == 1)
    #define TWI0_USE_EASY_DMA 0
    
    #define TWI0_CONFIG_FREQUENCY    NRF_TWI_FREQ_100K
    #define TWI0_CONFIG_SCL          0
    #define TWI0_CONFIG_SDA          1
    #define TWI0_CONFIG_IRQ_PRIORITY APP_IRQ_PRIORITY_LOW
    
    #define TWI0_INSTANCE_INDEX      0
    #endif
    

    But when I build it ,there is this error too.

  • Did you clean / rebuild it? Keil sometimes messes up when macros are changed and the old values are still used in compilation.

  • I have rebuiled it ,but it is always "wrong configration".

  • That really shouldn't be possible though. If #define TWI0_ENABLED 1 exists and the file defining it is included, it should compile. Did you use an example project as a base for your app? If you did, did you change the nrf_drv_config.h in the example project's \config\ folder (instead of the path I listed before)?

Related