TWI hard faulting

Been working to get the TWI communicating to a LIS331HH and been having tons of trouble getting it functioning.

I do an init like the example

void twi_init (void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_config = {
       .scl                = SCL_PORT,
       .sda                = SDA_PORT,
       .frequency          = NRF_DRV_TWI_FREQ_100K,
       .interrupt_priority = APP_IRQ_PRIORITY_MID,
       .clear_bus_init     = false
    };

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

    nrf_drv_twi_enable(&m_twi);
}

then when i try to do any tx/rx

    ret_code_t err_code;
    uint8_t twidat = 0;
    err_code = nrf_drv_twi_rx(&m_twi,0x18,&twidat,sizeof(twidat));
    APP_ERROR_CHECK(err_code);

following the code by stepping in it goes to this function and hardfaults

__STATIC_INLINE void nrf_twi_int_enable(NRF_TWI_Type * p_reg,
                                        uint32_t       mask)
{
    p_reg->INTENSET = mask;
}

thanks,

Kevin

Parents
  • Hi,

    How is m_twi defined in your application?

    Are you triggering the TX/RX operations from main or interrupt context?

    Do you use a SoftDevice in your application? 

    Which SDK version are you using?

    Best regards,
    Jørgen

  • Hi Jørgen, thank you for taking the time to look into this.

    1) it's defined globally at the top of one of our .h files;

    which is included at the bottom of our main includes section;

    2) we've tried several methods, i'm currently down to a simple example to try to get something working and hoping to built from there-this is currently called in main after are setup;

    3) S112, i think we originally copied this project from the PCA10040e S112 blinky example

    4) nRF5_SDK_17.1.0_ddde560

    we have a git repo if that helps.

  • 2) we've tried several methods, i'm currently down to a simple example to try to get something working and hoping to built from there-this is currently called in main after are setup;

    It looks to me like you in this example are calling nrf_drv_twi_rx, without first initializing the TWI driver, is that correct?

    Since you have declared m_twi as static in a header file, you should not access it from multiple .c files (e.g. main.c and salt_lick_accel.c), as each file will have its own copy of the object. This means that if you initialize m_twi using a function in salt_lick_accel.c, the m_twi object in main.c will still be uninitialized.

    If you can upload a .zip file of your project here, that would make it easier for us to reproduce/debug the issue.

    Best regards,
    Jørgen

Reply
  • 2) we've tried several methods, i'm currently down to a simple example to try to get something working and hoping to built from there-this is currently called in main after are setup;

    It looks to me like you in this example are calling nrf_drv_twi_rx, without first initializing the TWI driver, is that correct?

    Since you have declared m_twi as static in a header file, you should not access it from multiple .c files (e.g. main.c and salt_lick_accel.c), as each file will have its own copy of the object. This means that if you initialize m_twi using a function in salt_lick_accel.c, the m_twi object in main.c will still be uninitialized.

    If you can upload a .zip file of your project here, that would make it easier for us to reproduce/debug the issue.

    Best regards,
    Jørgen

Children
Related