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

Nrf52832 + SDK 12.3 + SD 132 : TWI Not Working. No Output

Hi All,

I am trying to use TWI on NRF52832 using SDK 12.3 + SD132 but without any success.

For some reason, there is no signal coming out on the configured SDA + SCL pins. I checked using a logic analyzer. I tried using both the blocking and non blocking mode without any success. The pins that I am using are

SDA : 13

SCL : 12

My sdk_config.h section is

// <e> TWI_ENABLED - nrf_drv_twi - TWI/TWIM peripheral driver
//==========================================================
#ifndef TWI_ENABLED
#define TWI_ENABLED 1
#endif
#if  TWI_ENABLED
// <o> TWI_DEFAULT_CONFIG_FREQUENCY  - Frequency
 
// <26738688=> 100k 
// <67108864=> 250k 
// <104857600=> 400k 

#ifndef TWI_DEFAULT_CONFIG_FREQUENCY
#define TWI_DEFAULT_CONFIG_FREQUENCY 26738688
#endif

// <q> TWI_DEFAULT_CONFIG_CLR_BUS_INIT  - Enables bus clearing procedure during init
 

#ifndef TWI_DEFAULT_CONFIG_CLR_BUS_INIT
#define TWI_DEFAULT_CONFIG_CLR_BUS_INIT 0
#endif

// <q> TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT  - Enables bus holding after uninit
 

#ifndef TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT
#define TWI_DEFAULT_CONFIG_HOLD_BUS_UNINIT 0
#endif

// <o> TWI_DEFAULT_CONFIG_IRQ_PRIORITY  - Interrupt priority
 

// <i> Priorities 0,2 (nRF51) and 0,1,4,5 (nRF52) are reserved for SoftDevice
// <0=> 0 (highest) 
// <1=> 1 
// <2=> 2 
// <3=> 3 
// <4=> 4 
// <5=> 5 
// <6=> 6 
// <7=> 7 

#ifndef TWI_DEFAULT_CONFIG_IRQ_PRIORITY
#define TWI_DEFAULT_CONFIG_IRQ_PRIORITY 7
#endif

// <e> TWI0_ENABLED - Enable TWI0 instance
//==========================================================
#ifndef TWI0_ENABLED
#define TWI0_ENABLED 1
#endif
#if  TWI0_ENABLED
// <q> TWI0_USE_EASY_DMA  - Use EasyDMA (if present)
 

#ifndef TWI0_USE_EASY_DMA
#define TWI0_USE_EASY_DMA 1
#endif

#endif //TWI0_ENABLED
// </e>

// <e> TWI1_ENABLED - Enable TWI1 instance
//==========================================================
#ifndef TWI1_ENABLED
#define TWI1_ENABLED 0
#endif
#if  TWI1_ENABLED
// <q> TWI1_USE_EASY_DMA  - Use EasyDMA (if present)
 

#ifndef TWI1_USE_EASY_DMA
#define TWI1_USE_EASY_DMA 0
#endif

#endif //TWI1_ENABLED
// </e>

// <e> TWI_CONFIG_LOG_ENABLED - Enables logging in the module.
//==========================================================
#ifndef TWI_CONFIG_LOG_ENABLED
#define TWI_CONFIG_LOG_ENABLED 0
#endif
#if  TWI_CONFIG_LOG_ENABLED
// <o> TWI_CONFIG_LOG_LEVEL  - Default Severity level
 
// <0=> Off 
// <1=> Error 
// <2=> Warning 
// <3=> Info 
// <4=> Debug 

#ifndef TWI_CONFIG_LOG_LEVEL
#define TWI_CONFIG_LOG_LEVEL 3
#endif

// <o> TWI_CONFIG_INFO_COLOR  - ANSI escape code prefix.
 
// <0=> Default 
// <1=> Black 
// <2=> Red 
// <3=> Green 
// <4=> Yellow 
// <5=> Blue 
// <6=> Magenta 
// <7=> Cyan 
// <8=> White 

#ifndef TWI_CONFIG_INFO_COLOR
#define TWI_CONFIG_INFO_COLOR 0
#endif

// <o> TWI_CONFIG_DEBUG_COLOR  - ANSI escape code prefix.
 
// <0=> Default 
// <1=> Black 
// <2=> Red 
// <3=> Green 
// <4=> Yellow 
// <5=> Blue 
// <6=> Magenta 
// <7=> Cyan 
// <8=> White 

#ifndef TWI_CONFIG_DEBUG_COLOR
#define TWI_CONFIG_DEBUG_COLOR 0
#endif

#endif //TWI_CONFIG_LOG_ENABLED
// </e>

#endif //TWI_ENABLED
// </e>

My TWI initialization code is 

bool INTERNAL_I2C_Init(void)
{
    //INITIALZE INTERNAL_I2CTWIMASTER_TWI_INST_USE_EASY_DMA

    //ONLY INITIALIZE IF NOT ALREADY DONE
    if(s_initialized)
    {
        return true;
    }

    //INITIALIZE DRIVER
    ret_code_t ret;
    const nrf_drv_twi_config_t config = {
                                            .scl = BSP_EXTERNAL_DS3231_SCL,
                                            .sda = BSP_EXTERNAL_DS3231_SDA,
                                            .frequency = NRF_TWI_FREQ_100K,
                                            .interrupt_priority = APP_IRQ_PRIORITY_LOW,
                                            .clear_bus_init = false
                                        };
    
    do
    {
        ret = nrf_drv_twi_init(&s_internal_twi_handle, 
                                &config, 
                                s_internal_i2c_event_handler, 
                                NULL);
        if(ret != NRF_SUCCESS)
        {
            break;
        }
        nrf_delay_ms(2000);
        nrf_drv_twi_enable(&s_internal_twi_handle);
        nrf_delay_ms(2000);
        UTIL_DebugPrint(MODULE_NAME_I2C"%s OK\n", __FUNCTION__);
        s_initialized = true;
        return true;
    }while(0);

    UTIL_DebugPrint(MODULE_NAME_I2C"%s FAIL\n", __FUNCTION__);
    return false;
}

and the write function is 

bool INTERNAL_I2C_WriteData(uint8_t i2c_add, uint8_t reg_add, uint8_t* buff, uint8_t len)
{
    //SEND THE SPECIFIED DATA OF SPECIFIED LENGTH TO THE SPECIFIED REGISTER ADDRESS
    //THROUGH I2C INTERFACE

    //WAIT FOR ONGOING OPERATOIN TO FINISH
    while(s_operation_ongoing)
    {

    }
    
    ret_code_t ret;
    uint8_t final_data[len + 1];

    //CREATE DATA BUFFER
    final_data[0] = reg_add;
    memcpy(&final_data[1], (const void * restrict)buff, len);

    UTIL_DebugPrint(MODULE_NAME_I2C"A\n");

    //SLA+W + REGISTER_ADDRESS + WRITE_DATA + STOP
    s_operation_ongoing = true;
    ret = nrf_drv_twi_tx(&s_internal_twi_handle,
                            i2c_add,
                            final_data,
                            (len + 1),
                            false);
    UTIL_DebugPrint(MODULE_NAME_I2C"tx ret code %u\n", ret);
    if(ret != NRF_SUCCESS)
    {
        return false;
    }
    UTIL_DebugPrint(MODULE_NAME_I2C"B\n");
    return true;
}

When I write some data to the twi device (DS3231), the i2c write function returns with the data is never actually sent out. I checked the i2c device and it is working fine. I am able to communicate to it using bus pirate.

Not sure what I could be doing wrong here. Any help is appreciated.

Related