Beware that this post is related to an SDK in maintenance mode
More Info: Consider nRF Connect SDK for new designs
This post is older than 2 years and might not be relevant anymore
More Info: Consider searching for newer posts

MCU crashes due to TWI

Hello,

       I am having a trouble with my code. There is an RTC sensor Which has the address 0x32. When the sensor is connected to the TWI lines, there is no problem and it works perfect. When I disconnect the sensor, the MCU crashes and "app_error_fault_handler" is called.

      How can I elliminate this problem ? I need to write a working code even if there is no sensor on the TWI line.  

Best Regards

void twi_read(void)
{
    ret_code_t err_code;

    m_sample[0] = 0x00;
    err_code = nrf_drv_twi_tx(&m_twi, 0x32, &m_sample[0], 1,false);
    APP_ERROR_CHECK(err_code);

    err_code = nrf_drv_twi_rx(&m_twi, 0x32, &m_sample[0], 10);
    APP_ERROR_CHECK(err_code);

    m_sample[0] = (((m_sample[0]>>4)*10)+(m_sample[0]&0x0f));
    m_sample[1] = (((m_sample[1]>>4)*10)+(m_sample[1]&0x0f));
    m_sample[2] = (((m_sample[2]>>4)*10)+(m_sample[2]&0x0f));

    NRF_LOG_INFO("%d:%d:%d\r\n", m_sample[2],m_sample[1],m_sample[0]);
    NRF_LOG_FLUSH();

}

void twi_check(void)
{
    ret_code_t err_code;
    int address;

    for (address = 1; address <= 0x80; address++)
    {
        err_code = nrf_drv_twi_rx(&m_twi, address, &m_sample[0], sizeof(m_sample));
        if (err_code == NRF_SUCCESS)
        {
            NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
        }
        NRF_LOG_FLUSH();
    }
}

void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = 27,
       .sda                = 26,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_HIGH,
       .clear_bus_init     = false
    };

    err_code = nrf_drv_twi_init(&m_twi, &twi_config, NULL, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);

    twi_check();
}

Parents Reply Children
No Data
Related