Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This discussion has been locked.
You can no longer post new replies to this discussion. If you have a question you can start a new discussion

hold_bus_uninit setting overrides due to nrf_drv_twi_config_t passed as nrfx_twim_config_t

hello ,

During my twim0 initialization, I have configured clear_bus_init as true and hold_bus_uninit  as false. But i always get hold_bus_uninit  as true.

then I dig into nrf_drv_twi.c file , i found that in nrf_drv_twi_init function , function gets my settings as "nrf_drv_twi_config_t const * p_config,"

but then calls nrfx_twim_init function with (nrfx_twim_config_t const *)p_config,...

so 

nrfx_twim_config_t structure's the last member is hold_bus_uninit, 

typedef struct
{
uint32_t scl; ///< SCL pin number.
uint32_t sda; ///< SDA pin number.
nrf_twim_frequency_t frequency; ///< TWIM frequency.
uint8_t interrupt_priority; ///< Interrupt priority.
bool hold_bus_uninit; ///< Hold pull up state on GPIO pins after uninit.
} nrfx_twim_config_t;

but the our data was a  nrf_drv_twi_config_t  has one more element that is  clear_bus_init...

typedef struct
{
uint32_t scl; ///< SCL pin number.
uint32_t sda; ///< SDA pin number.
nrf_drv_twi_frequency_t frequency; ///< TWI frequency.
uint8_t interrupt_priority; ///< Interrupt priority.
bool clear_bus_init; ///< Clear bus during init.
bool hold_bus_uninit; ///< Hold pull up state on gpio pins after uninit.
} nrf_drv_twi_config_t;

so when we cast nrf_drv_twi_config_t as nrfx_twim_config_t , we are writing "nrf_drv_twi_config_t 's clear_bus_init"  over "nrfx_twim_config_t 's hold_bus_uninit"

Could you please confirm , i am using sdk16 but i just check sdk17 also has no any definition for clear_bus_init in nrfx_twim_config_t  structure.

ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const *        p_instance,
                            nrf_drv_twi_config_t const * p_config,
                            nrf_drv_twi_evt_handler_t    event_handler,
                            void *                       p_context)
{
    uint32_t inst_idx = p_instance->inst_idx;
    m_handlers[inst_idx] = event_handler;
    m_contexts[inst_idx] = p_context;

    if(p_config->clear_bus_init)
    {
        /* Send clocks (max 9) until slave device back from stuck mode */
        twi_clear_bus(p_config);
    }

    ret_code_t result = 0;
    if (NRF_DRV_TWI_USE_TWIM)
    {
        result = nrfx_twim_init(&p_instance->u.twim,
                                (nrfx_twim_config_t const *)p_config,
                                event_handler ? twim_evt_handler : NULL,
                                (void *)inst_idx);
    }
    else if (NRF_DRV_TWI_USE_TWI)
    {
        result = nrfx_twi_init(&p_instance->u.twi,
                               (nrfx_twi_config_t const *)p_config,
                               event_handler ? twi_evt_handler : NULL,
                               (void *)inst_idx);
    }
    return result;
}

  • Hi,

    I was not able to reproduce the issue you described. I just tried with the \nRF5_SDK_16.0.0_98a08e2\examples\peripheral\twi_sensor\ example which sets .clear_bus_init to false by default.

    Here you can see the config argument that ends up being passed to the nrfx_twim_init() function:

    Could you try the same?

    Thanks,

    Vidar

  • I was able to reproduce with that example in SDK17 . 

    Plese just set the config like this ;

    void twi_init (void)
    {
        ret_code_t err_code;
    
        const nrf_drv_twi_config_t twi_lm75b_config = {
           .scl                = ARDUINO_SCL_PIN,
           .sda                = ARDUINO_SDA_PIN,
           .frequency          = NRF_DRV_TWI_FREQ_100K,
           .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
           .clear_bus_init     = true,
           .hold_bus_uninit    = false
        };
    
        err_code = nrf_drv_twi_init(&m_twi, &twi_lm75b_config, twi_handler, NULL);
        APP_ERROR_CHECK(err_code);
    
        nrf_drv_twi_enable(&m_twi);
    }

    then you can watch by a debugger p_config in the nrfx_twim_init will has hold_bus_uninit as true .

  • This has been reported before. On devzone and also on github. It's been like this since nrfx replaced the nrf_drv_* drivers.

    https://github.com/NordicSemiconductor/nrfx/issues/47

    I still have a comment in my source that I fixed this in SDK 15.2...

    MCB, just modify the nrf_drv_twi_init -function. I remove the cast and copied the elements so they end up correctly.

  • Thanks for the comment, ! I understand the problem now. , can you try what suggested above?

    Something like this:

    #include <string.h>
    ret_code_t nrf_drv_twi_init(nrf_drv_twi_t const *        p_instance,
                                nrf_drv_twi_config_t const * p_config,
                                nrf_drv_twi_evt_handler_t    event_handler,
                                void *                       p_context)
    {
        uint32_t inst_idx = p_instance->inst_idx;
        m_handlers[inst_idx] = event_handler;
        m_contexts[inst_idx] = p_context;
    
        if(p_config->clear_bus_init)
        {
            /* Send clocks (max 9) until slave device back from stuck mode */
            twi_clear_bus(p_config);
        }
    
        ret_code_t result = 0;
        if (NRF_DRV_TWI_USE_TWIM)
        {
            nrfx_twim_config_t nrfx_twim_config;
            //Workaround for https://github.com/NordicSemiconductor/nrfx/issues/47 
            memcpy(&nrfx_twim_config, p_config, sizeof(nrfx_twim_config));
            nrfx_twim_config.hold_bus_uninit = p_config->hold_bus_uninit;
    
            result = nrfx_twim_init(&p_instance->u.twim,
                                    &nrfx_twim_config,
                                    event_handler ? twim_evt_handler : NULL,
                                    (void *)inst_idx);
        }

  • Thank you guys.

    Actually was able to solve the problem and I know what was the problematic code part. I have wrote details in  my the first message.i just wanted a confirmation. And thanks now confirmed.

    My solution is  adding clear_bus_init in to 

    nrfx_twim_config_t struct as same as the order of 

    nrf_drv_twi_config_t

    Thank you all.

Related