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

Handling ret_code_t errors (SDK14.2)

I'm trying to understand how to handle error codes from the twi driver at the moment. When I hold my I2C slave in reset and try to configure it  nrf_drv_twi_tx() returns 0x8201 which I was able to figure out how to decode by looking through the sdk_errors.h file. However, I'm not sure what the best way to handle these errors in the code is. In my case where I'm trying to do an I2C read I expect either a success or a nack.

Should I mask the return code and check the LSB only?

Is there standard way of checking these codes?

Should my I2C peripheral initialization functions return ret_code_t or handle it directly with APP_ERROR_CHECK?

Finally, is there a more graceful way of using APP_ERROR_CHECK() without causing a reset?

I'm sure there are many ways to do this but I'm wondering what the recommended approach is for the cleanest code.

  • Hi,

    Error code 0x8201 means NRF_ERROR_DRV_TWI_ERR_ANACK, and the possible return codes from the function call is given in the function API docuementation. I'm not sure what you mean by "Should I mask the return code and check the LSB only?". Why would you mask out part of the error code?

    Not all error codes are non-recoverable. You need to decide this when you write your application. If it is acceptable in your application to for instance get an address NACK, you can mask out these errors while passing the rest of the error codes to APP_ERROR_CHECK(), using a if-check:

    ret_code_t err_code = nrf_drv_twi_tx(....);
    if(err_code != NRF_ERROR_DRV_TWI_ERR_ANACK)
    {
        APP_ERROR_CHECK(err_code);
    }

    There is no need to reset the device, This is just the default recovery method used in the SDK. You can write your own app_error_fault_handler function, that will handle all general error codes based on your opinion of the seriousness/recoverability.

    Best regards,
    Jørgen

Related