Hello,
I'm using BMA253 sensor on custom board with nRF52832.
I've implemented bus write and read functions using nrf_twi_mngr.
Here is code:
/* \Brief: The function is used as I2C bus write * \Return : Status of the I2C write * \param dev_addr : The device address of the sensor * \param reg_addr : Address of the first register, where data is to be written * \param reg_data : It is a value held in the array, * which is written in the register * \param cnt : The no of bytes of data to be written */ static s8 BMA253_TWI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) { s32 iError = BMA2x2_INIT_VALUE; u8 array[TWI_BUFFER_LEN] = {BMA2x2_INIT_VALUE}; u8 stringpos = BMA2x2_INIT_VALUE; array[BMA2x2_INIT_VALUE] = reg_addr; for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++) { array[stringpos + BMA2x2_BUS_READ_WRITE_ARRAY_INDEX] = *(reg_data + stringpos); } nrf_twi_mngr_transfer_t const transfers[] = { NRF_TWI_MNGR_WRITE(dev_addr, array, cnt + 1, 0) }; iError = nrf_twi_mngr_perform(&m_nrf_twi_mngr, NULL, transfers, sizeof(transfers) / sizeof(transfers[0]), NULL); APP_ERROR_CHECK(iError); return (s8)iError; } /* \Brief: The function is used as I2C bus read * \Return : Status of the I2C read * \param dev_addr : The device address of the sensor * \param reg_addr : Address of the first register, where data is going to be read * \param reg_data : This is the data read from the sensor, which is held in an array * \param cnt : The no of data to be read */ static s8 BMA253_TWI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) { s32 iError = BMA2x2_INIT_VALUE; u8 array[TWI_BUFFER_LEN] = {BMA2x2_INIT_VALUE}; u8 stringpos = BMA2x2_INIT_VALUE; array[BMA2x2_INIT_VALUE] = reg_addr; nrf_twi_mngr_transfer_t const transfers[] = { NRF_TWI_MNGR_WRITE(dev_addr, ®_addr, 1, NRF_TWI_MNGR_NO_STOP), NRF_TWI_MNGR_READ (dev_addr, array, cnt, 0) }; iError = nrf_twi_mngr_perform(&m_nrf_twi_mngr, NULL, transfers, sizeof(transfers) / sizeof(transfers[0]), NULL); APP_ERROR_CHECK(iError); for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++) *(reg_data + stringpos) = array[stringpos]; return (s8)iError; }
It is working, but I would like to change `nrf_twi_mngr_perform` function to `nrf_twi_mngr_schedule`.
For this purpose I've added `transaction` structures to my code, and made both `transfers` and `transaction` static.
Here how it looks now:
static void BMA253_TWI_bus_write_cb(ret_code_t result, void * p_user_data) { } static void BMA253_TWI_bus_read_cb(ret_code_t result, void * p_user_data) { } /* \Brief: The function is used as I2C bus write * \Return : Status of the I2C write * \param dev_addr : The device address of the sensor * \param reg_addr : Address of the first register, where data is to be written * \param reg_data : It is a value held in the array, * which is written in the register * \param cnt : The no of bytes of data to be written */ static s8 BMA253_TWI_bus_write(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) { s32 iError = BMA2x2_INIT_VALUE; static u8 array[TWI_BUFFER_LEN] = {BMA2x2_INIT_VALUE}; u8 stringpos = BMA2x2_INIT_VALUE; array[BMA2x2_INIT_VALUE] = reg_addr; for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++) { array[stringpos + BMA2x2_BUS_READ_WRITE_ARRAY_INDEX] = *(reg_data + stringpos); } static nrf_twi_mngr_transfer_t const transfers[] = { NRF_TWI_MNGR_WRITE(dev_addr, array, cnt + 1, 0) }; static nrf_twi_mngr_transaction_t NRF_TWI_MNGR_BUFFER_LOC_IND transaction = { .callback = BMA253_TWI_bus_write_cb, .p_user_data = NULL, .p_transfers = transfers, .number_of_transfers = sizeof(transfers) / sizeof(transfers[0]) }; iError = nrf_twi_mngr_schedule(&m_nrf_twi_mngr, &transaction); APP_ERROR_CHECK(iError); return (s8)iError; } /* \Brief: The function is used as I2C bus read * \Return : Status of the I2C read * \param dev_addr : The device address of the sensor * \param reg_addr : Address of the first register, where data is going to be read * \param reg_data : This is the data read from the sensor, which is held in an array * \param cnt : The no of data to be read */ static s8 BMA253_TWI_bus_read(u8 dev_addr, u8 reg_addr, u8 *reg_data, u8 cnt) { s32 iError = BMA2x2_INIT_VALUE; static u8 array[TWI_BUFFER_LEN] = {BMA2x2_INIT_VALUE}; u8 stringpos = BMA2x2_INIT_VALUE; array[BMA2x2_INIT_VALUE] = reg_addr; static nrf_twi_mngr_transfer_t const transfers[] = { NRF_TWI_MNGR_WRITE(dev_addr, ®_addr, 1, NRF_TWI_MNGR_NO_STOP), NRF_TWI_MNGR_READ (dev_addr, array, cnt, 0) }; static nrf_twi_mngr_transaction_t NRF_TWI_MNGR_BUFFER_LOC_IND transaction = { .callback = BMA253_TWI_bus_read_cb, .p_user_data = NULL, .p_transfers = transfers, .number_of_transfers = sizeof(transfers) / sizeof(transfers[0]) }; iError = nrf_twi_mngr_schedule(&m_nrf_twi_mngr, &transaction); APP_ERROR_CHECK(iError); for (stringpos = BMA2x2_INIT_VALUE; stringpos < cnt; stringpos++) *(reg_data + stringpos) = array[stringpos]; return (s8)iError; }
However I've got this errors:
error: initializer element is not constant NRF_TWI_MNGR_WRITE(dev_addr, array, cnt + 1, 0) error: initializer element is not constant NRF_TWI_MNGR_WRITE(dev_addr, ®_addr, 1, NRF_TWI_MNGR_NO_STOP), error: initializer element is not constant NRF_TWI_MNGR_READ (dev_addr, array, cnt, 0) etc.
What else should I do to proper replace nrf_twi_mngr_perform to nrf_twi_mngr_schedule?