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

How to determine if peripheral has been initialized

Hello,

I'm working on an application in which I have to dynamically switch between a TWI master peripheral and SPI master peripheral that use the same peripheral ID (our application requires this, since we need the extra buses). Is there a way to check whether a peripheral has been previously initialized or not? I have found that simply disabling a peripheral that is not currently initialized leads to subtle, tricky issues that cause the SPI drivers to crash, so I would very much like to know if there is a way to check if a peripheral has been initialized. If not, I can set up a series of flags/locks to do it myself, but it would be nice if there was a default implementation.

Thank you,

  • Charles
Parents
  • Hi,

    The easiest solution would probably be to just read the ENABLE register.

    It will return 0 when disabled, and 7 when enabled(initialized)

    NRF_LOG_INFO("NRF_SPIM0->ENABLE: %d ", NRF_SPIM0->ENABLE);
    

    The SPI/TWI drivers have a control block that holds all the driver instance local data, where the e.g. the state is kept track of. So another option could be to add a function in nrf_drv_spi.c that returns the state value, e.g. something like this:

    ret_code_t nrf_drv_spi_is_instance_initialized(nrf_drv_spi_t     const * const p_instance)
    {
        spi_control_block_t * p_cb  = &m_cb[p_instance->drv_inst_idx];
        
        if(p_cb->state != NRF_DRV_STATE_UNINITIALIZED)
        {
            return true;
        }
        else
            return false;
    }
    
Reply
  • Hi,

    The easiest solution would probably be to just read the ENABLE register.

    It will return 0 when disabled, and 7 when enabled(initialized)

    NRF_LOG_INFO("NRF_SPIM0->ENABLE: %d ", NRF_SPIM0->ENABLE);
    

    The SPI/TWI drivers have a control block that holds all the driver instance local data, where the e.g. the state is kept track of. So another option could be to add a function in nrf_drv_spi.c that returns the state value, e.g. something like this:

    ret_code_t nrf_drv_spi_is_instance_initialized(nrf_drv_spi_t     const * const p_instance)
    {
        spi_control_block_t * p_cb  = &m_cb[p_instance->drv_inst_idx];
        
        if(p_cb->state != NRF_DRV_STATE_UNINITIALIZED)
        {
            return true;
        }
        else
            return false;
    }
    
Children
No Data
Related