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

Read multiple registers with app_twi schedule

Hi, I would like to know if there's a way to change the size of a transfer with the app_twi library.

For example if in a function I have this code :

static app_twi_transfer_t const transfers[] =
{

    APP_TWI_WRITE(device_address, &register_address, 1, APP_TWI_NO_STOP),
    APP_TWI_READ (device_address, p_buffer,          4, 0)
};

static app_twi_transaction_t const transaction =
{

    .callback            = transaction_callback,
    .p_user_data         = NULL,
    .p_transfers         = transfers,
    .number_of_transfers = sizeof(transfers)/sizeof(transfers[0])
};

APP_ERROR_CHECK(app_twi_schedule(&m_app_twi, &transaction));

I want to be able to change the 4 in the APP_TWI_READ for a variable that is received by the function.

I want to read more than one register at a time with i2c but not always the same number and I don't know how to resize a static or a constant variable.

  • Hi,

    The static statement says that the variable is only directly accessible inside the function and that it is placed in ram rather than on the stack. This means that variable will only be initialized once and can be accessed every time the function is called. The const statement says that it cannot be changed. You can remove the const and you will be able to change it. Just be sure to not change it before the transaction is ended.

    The reason const is used is because you should rather make different functions that do the different read/write instead of having a generic function that can read/write whatever. For example, a function can be read_xyz to read x, y and z accelerations from an accelerometer. If the application has a set of things to do with a TWI sensor it can make functions for each operation.

Related