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

Hard Fault using TWI_SCANNER example

Hello,

Trying to run the TWI_SCANNER example on a pca10100 hard faults. The .emProject file has been modified per the instructions at https://devzone.nordicsemi.com/f/nordic-q-a/57816/ble_app_beacon-not-available-for-pca10100/234466#234466


The fault is consistently at this location:

The registers are as follows at this point in the code:

Code is as follows:

#include <stdio.h>
#include "boards.h"
#include "app_util_platform.h"
#include "app_error.h"
#include "nrf_drv_twi.h"



#include "nrf_log.h"
#include "nrf_log_ctrl.h"
#include "nrf_log_default_backends.h"

/* TWI instance ID. */
#if TWI0_ENABLED
#define TWI_INSTANCE_ID     0
#elif TWI1_ENABLED
#define TWI_INSTANCE_ID     1
#endif

 /* Number of possible TWI addresses. */
 #define TWI_ADDRESSES      127

/* TWI instance. */
static const nrf_drv_twi_t m_twi = NRF_DRV_TWI_INSTANCE(TWI_INSTANCE_ID);


/**
 * @brief TWI initialization.
 */
void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = 26,
       .sda                = 27,
       .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, app_error_handler_bare, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}


/**
 * @brief Function for main application entry.
 */
int main(void)
{
    ret_code_t err_code;
    uint8_t address;
    uint8_t sample_data;
    bool detected_device = false;

    APP_ERROR_CHECK(NRF_LOG_INIT(NULL));
    NRF_LOG_DEFAULT_BACKENDS_INIT();

    NRF_LOG_INFO("TWI scanner started.");
    NRF_LOG_FLUSH();
    twi_init();

    for (address = 1; address <= TWI_ADDRESSES; address++)
    {
        err_code = nrf_drv_twi_rx(&m_twi, address, &sample_data, sizeof(sample_data));
        if (err_code == NRF_SUCCESS)
        {
            detected_device = true;
            NRF_LOG_INFO("TWI device detected at address 0x%x.", address);
        }
        NRF_LOG_FLUSH();
    }

    if (!detected_device)
    {
        NRF_LOG_INFO("No device was found.");
        NRF_LOG_FLUSH();
    }

    //while (true)
    //{
    //    NRF_LOG_INFO("No device was found.");
    //}
}

However, even removing all of the code (i.e. leaving void main(void){} completely empty except for an empty while loop) results in the exact same hard fault. I get no build errors, and the code deploys without error or fault via J-Link.

Would appreciate some guidance.

Best,

S.

Parents Reply Children
  • Hi Håkon,

    Thanks so much for the reply.

    The issue was in trying to use the pca10056 solution; I got no hard faults when I switch to the 10040 solution and run the same code.

    However, I now detect a valid TWI device at 0x00 - this is strange, as I was expecting a device between 0x08 and 0x77.

    Note: this happens when I switch pins - I actually have two i2c devices, but am only reading one at a time right now.

    Is this device address valid?

    Lastly, I'm relying on the nrf52833's internal pullups to set the signals high. It looks like this is happening by default - can you confirm?

    Best wishes and thanks,

    S.

  • To clarify the language "This happens when I switch pins", what I mean is I get the same address (0x00) even when I manually change the SCL and SDA pins to address two different devices.

    Example:
    Device 1) SCL:P0.21, SDA:P0.20 - in this instance I expect an address between 0x2a or 0x2b
    Device 2) SCL:P0.26, SDA:P0.27 - in this instance I expect one of six addresses, none of them are 0x00

    Yet, both return the same address (0x00) when I run the code.

  • Hi,

     

    scath said:
    However, I now detect a valid TWI device at 0x00 -

    you should start scanning from address=1.

    scath said:
    To clarify the language "This happens when I switch pins", what I mean is I get the same address (0x00) even when I manually change the SCL and SDA pins to address two different devices.

    Example:
    Device 1) SCL:P0.21, SDA:P0.20 - in this instance I expect an address between 0x2a or 0x2b
    Device 2) SCL:P0.26, SDA:P0.27 - in this instance I expect one of six addresses, none of them are 0x00

    Have you scoped the lines to see what happens?

    scath said:
    The issue was in trying to use the pca10056 solution; I got no hard faults when I switch to the 10040 solution and run the same code.

    Did you remove the pca10056 system files and add system_nrf52833 and nrf52833-startup files?

    scath said:
    Lastly, I'm relying on the nrf52833's internal pullups to set the signals high. It looks like this is happening by default - can you confirm?

    Yes, nrfx_twim.c enables pull-ups in the pad.

     

    Kind regards,

    Håkon

Related