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

Build generic function with TWI read

I take the TWI master example of nordic, and try to build I2C read API that can be use from otherr places.

The function look like this

uint32_t Serial_read_2BytesReg(uint8_t instance,uint8_t dev_add, uint16_t reg_add,void const * p_buffer,size_t size, nrf_twi_mngr_callback_t callback_fn )
{
uint32_t error;
nrf_twi_mngr_t * p_nrf_twi_mngr_instance;

uint8_t reg_addr_uint8[]= { ((uint8_t)(reg_add>>8)),((uint8_t)(reg_add))};

nrf_twi_mngr_transfer_t transfers[] ={
{ /* write sequence */
.p_data = reg_addr_uint8, // the reg address should be read from
.length = 2, // size of reg address in uint8_t units
.operation = (((dev_add) << 1) | 0), // address of the device include the write command
.flags = NRF_TWI_MNGR_NO_STOP // no stop continue to the read
},
{ /* read sequence */
.p_data = (uint8_t*)p_buffer, // buffer to place the receive data read
.length = size, // size of data to read in uint8 units
.operation = (((dev_add) << 1) | 1), // address of the device include the read command
.flags = 0 // stop communication
}};
nrf_twi_mngr_transaction_t NRF_TWI_MNGR_BUFFER_LOC_IND transaction =
{
.callback = callback_fn,
.p_user_data = NULL,
.p_transfers = transfers,
.number_of_transfers = sizeof(transfers) / sizeof(transfers[0])
};

switch(instance)
{
#if TWI0_ENABLED
case 0:
p_nrf_twi_mngr_instance = &m_nrf_twi_mngr_sec;
break;
#endif // TWI0_ENABLE
#if TWI1_ENABLED
case 1:
p_nrf_twi_mngr_instance = &m_nrf_twi_mngr;
break;
#endif
default:
p_nrf_twi_mngr_instance = NULL;
break;
}
if ( p_nrf_twi_mngr_instance == NULL)
{
return !NRF_SUCCESS;
}
error = nrf_twi_mngr_schedule(p_nrf_twi_mngr_instance, &transaction);

return error; // NRF_SUCCESS =0 all other error
}

When I stop before the nrf_twi_mngr_schedule I check the contain of the register especially the transfer and transaction and this is what they have in:

config_I2C_gnr.xlsx

I have problem with the first write build. 

It build the device address with write (0x74) and then the reg add is 0x240b. I get the 0x24 but then 0x70 . It continue with the device address read (0x75) and then send 2 byes clk. I see it return 0x00 and the NACK but on my SWI don't get to the callback and I jump into error on HardFault_Handler.

Is there any clue what I did wrong in the function?

Bar.
 

Parents Reply Children
  • Bar said:

    I use the TWI callback.

    I don't know which SWI it use for the I2C.

    Ok, then it's the I2C interrupt.

    Bar said:
    How can I get information from the hardfault handler when it jump there?

    You should use the hardfault handler from the hardfault handling library. Just add the following files to your build and enable the handler in sdk_config.h: 

     - nRF5_SDK_15.3.0_59ac345\components\libraries\hardfault\hardfault_implementation.c

     - nRF5_SDK_15.3.0_59ac345\components\libraries\hardfault\nrf52\handler\hardfault_handler_*.c 

    This handler will attempt to determine the cause and print out the error information. 

Related