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

Hard fault when calling nrf_drv_twi_tx(). nRF52832.

I'm trying to use an MMA8652FC accelerometer with the nRF52832.  When I call the function nrf_drv_twi_tx() the board hard faults, stopping in <SDK>\components\libraries\util\app_error_weak.c line 100.  I'm not sure what is happening and I don't get any output from the debugger.

Code:
uint8_t read_mma8652fc_reg(uint16_t addr) // TODO: Fix
{
    ret_code_t err_code;
    const uint8_t* caddr = (uint8_t*)&addr;
    uint8_t value;

    err_code = nrf_drv_twi_tx(&m_twi, MMA8652FC_ADDR, caddr, sizeof *caddr, true); // <-- Crash in this function
    APP_ERROR_CHECK(err_code);

    if (err_code == NRF_SUCCESS)
    {
        err_code = nrf_drv_twi_rx(&m_twi, MMA8652FC_ADDR, (uint8_t*)&value, 1);
        APP_ERROR_CHECK(err_code);
    }

    return value;
}

void init_twi(void)
{
    ret_code_t err_code;

    const nrf_drv_twi_config_t twi_MMA8652FC_config = {
    .scl = ACCELEROMETER_SCL_PIN,
    .sda = ACCELEROMETER_SDA_PIN,
    .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_MMA8652FC_config, twi_handler, NULL);
    APP_ERROR_CHECK(err_code);

    nrf_drv_twi_enable(&m_twi);
}

Parents
  • Hi,

     

        err_code = nrf_drv_twi_tx(&m_twi, MMA8652FC_ADDR, caddr, sizeof *caddr, true); // <-- Crash in this function
        APP_ERROR_CHECK(err_code);

    What is the returned error code? Have you ensured that you have the correct address?

     

    Kind regards,

    Håkon 

  • Hello,

    It never returns to the function, so I do not know the error code.
    And yes, I verified the address in the data sheet.  It couldn't have been changed because it states it is factory set and to have a different address it must be requested.

    I have debug output now.  I get this:
    <info> app: Debug logging for UART over RTT started.
    <info> app: Connected
    <info> app: Data len is set to 0xF4(244)
    <error> app: ERROR 17 [NRF_ERROR_BUSY] at :0
    PC at: 0x00000000
    <error> app: End of error report

  • Hi,

     

    ConnorOsborn said:
    <error> app: ERROR 17 [NRF_ERROR_BUSY] at :0

     The nrf_drv_twi has been configured in non-blocking mode (ie: by providing a handler to the nrf_drv_twi_init() function), and the result is that the previous TWI bus transaction isn't finished yet when you're calling the next one. Since the library functions you're using assumes blocking, I would recommend that you init the driver in blocking mode: 

     

    err_code = nrf_drv_twi_init(&m_twi, &twi_MMA8652FC_config, NULL, NULL);

     

    Kind regards,

    Håkon

  • After I switch the handle  to NULL I still get the same issue.  I would assume I should see something on the bus and clk happening but it always stays high on the scope.  I have check that I am using the correct pins as well.

    Edit: When I load up the twi_sensor example, I still see no change on SDA or SCL.  They stay high.  I have tried P0.09, P0.10 and P019, P0.20

  • Hi,

     

    ConnorOsborn said:
    They stay high.  I have tried P0.09, P0.10 and P019, P0.20

     P0.09 and P0.10 is the NFC pins. P0.19 and P0.20 are normally connected to LEDs on the DK, which can be problematic when you're trying to sink current (ie: then driving the pin to GND), so I would recommend that you try a GPIO that is unused, like P0.23 and P0.24. Flip over the DK, and you can see which pins are currently in use.

     

    Could you try this and see if it starts working?

     

    Kind regards,

    Håkon

Reply
  • Hi,

     

    ConnorOsborn said:
    They stay high.  I have tried P0.09, P0.10 and P019, P0.20

     P0.09 and P0.10 is the NFC pins. P0.19 and P0.20 are normally connected to LEDs on the DK, which can be problematic when you're trying to sink current (ie: then driving the pin to GND), so I would recommend that you try a GPIO that is unused, like P0.23 and P0.24. Flip over the DK, and you can see which pins are currently in use.

     

    Could you try this and see if it starts working?

     

    Kind regards,

    Håkon

Children
No Data
Related