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();
}

  • Hello Enkavak,

    Could you debug your code and set a breakpoint in the error handler? (it would be really helpful to see what causes this error)

    You should also turn on RTT logging in "sdk_config.h" and run RTT viewer (if you haven't).

    //==========================================================
    // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
    //==========================================================
    #ifndef NRF_LOG_BACKEND_RTT_ENABLED
    #define NRF_LOG_BACKEND_RTT_ENABLED 1
    #endif

    Are you using the newest SDKv15, I highly recommend that you check out the TWI Sensor example and use that as reference.

    - why do you choose to not have a "twi_handler" in in nrf_drv_twi_init()?

  • Hello I set a break point in the error handler and I get the result below.

    I do not want to use twi_handler.

    I enabled the RTT viewer. However I get nothing. It is not working. Is there a document about How tou use RTT viewer ?

  • Hi Enkavak,

    Here is the documentation for how to use the logger module.

    In short, you should enable it in "sdk_config.h":

    //==========================================================
    // <e> NRF_LOG_ENABLED - Logging module for nRF5 SDK
    //==========================================================
    #ifndef NRF_LOG_ENABLED
    #define NRF_LOG_ENABLED 1
    #endif
    //==========================================================
    // <e> NRF_LOG_BACKEND_RTT_ENABLED - nrf_log_backend_rtt - Log RTT backend
    //==========================================================
    #ifndef NRF_LOG_BACKEND_RTT_ENABLED
    #define NRF_LOG_BACKEND_RTT_ENABLED 1
    #endif

    Then open "J-Link RTT viewer" and chose these settings:

    Then you should be able to see info messages in the terminal:

    A trick to get more information from your application is to increase the "severity level" in "sdk_config.h" to 4:

    // <o> NRF_LOG_DEFAULT_LEVEL  - Default Severity level
     
    // <0=> Off 
    // <1=> Error 
    // <2=> Warning 
    // <3=> Info 
    // <4=> Debug 
    
    #ifndef NRF_LOG_DEFAULT_LEVEL
    #define NRF_LOG_DEFAULT_LEVEL 4
    #endif

    Please try to enable RTT and see if you get out any logs that can be helpful.

    Also make sure that you follow these tips.

     

  • RTT Response

    0> <info> app: Debug logging for UART over RTT started.
    0> <error> app: ERROR 33281 [NRF_ERROR_DRV_TWI_ERR_ANACK] at E:\Yedek\nRF5_SDK\examples\ble_peripheral\ble_app_uart\rtc.c:27
    0> PC at: 0x00034FED
    0> <error> app: End of error report

  • Hi Enkavak,

    Well, one suggestion is that you could search if something is on the TWI address 0x32 (use the twi scanner as reference)

    If not, then do not use the TWI. 

     

    From your RTT output:

    Set a breakpoint at line 27 in rtc.c file and debug the code and check the call stack to see what functions directs you into the error.

Related