how to get a repeated start on I2C

i need to get a repeated start on I2C write.

on nrf52840 i used twi driver and it worked just fine there was  flag you could set to get repeated start.

 err_code = nrf_drv_twi_tx(&m_LC709203_twi, LC709203_ADDR, m_tx_LC709203_data,1, true);  // send with a repeated start at the end
but i could not find this flag in the I2c commands for NrfconnectSDK . on nrf5340.
the nrf5340 does not have TWI hardware driver so I looked a the TWIM driver .  I wrote my own nrfx_twim driver but then read that the I2C functions use the twim hardware 
so there would be a conflict.  I got a suggestion to use the    flag from nordic support and began to look into the 
code for the i2c write and found that it actually calls what looks like the TWIM transfer function so I wrote the following new functions .
static inline int i2c_write_dt_repeated(const struct i2c_dt_spec *spec,
			       const uint8_t *buf, uint32_t num_bytes)
{
	return i2c_write_repeated(spec->bus, buf, num_bytes, spec->addr);
}
/**
 * @brief Write a set amount of data to an I2C device.
 *
 * This routine writes a set amount of data synchronously.
 *
 * @param dev Pointer to the device structure for an I2C controller
 * driver configured in controller mode.
 * @param buf Memory pool from which the data is transferred.
 * @param num_bytes Number of bytes to write.
 * @param addr Address to the target I2C device for writing.
 *
 * @retval 0 If successful.
 * @retval -EIO General input / output error.
 */
static inline int i2c_write_repeated(const struct device *dev, const uint8_t *buf,
			    uint32_t num_bytes, uint16_t addr)
{
	struct i2c_msg msg;

	msg.buf = (uint8_t *)buf;
	msg.len = num_bytes;
	msg.flags = I2C_MSG_WRITE | I2C_MSG_RESTART;

	return i2c_transfer(dev, &msg, 1, addr);
}

then i called the functions with 

/**
 * @brief Function for setting the active register on battery fuel gauge.
 */
void LC709203_write_command_byte(uint8_t command_byte)
{
    int err_code;



   /* Writing to pointer byte. */
    m_tx_LC709203_data[0] = command_byte;  // command byte which gets sent before every read






    // "true" means send with repeated start at the end
//    err_code = nrf_drv_twi_tx(&m_LC709203_twi, LC709203_ADDR, m_tx_LC709203_data,1, true);  // send with a repeated start at the end

// I noticed that i2c_write_dt calls
 // i2c_write which calls i2c_transfer which lets me 
 // put the I2C_MSG_RESTART flag on the buffer
 
 
int err_code = i2c_write_dt_repeated(&lc709_0b_,
			       m_tx_LC709203_data, sizeof(m_tx_LC709203_data));

}

the one thing i am not sure of is the write complete when the I2C transfer function completes or is the code non blocking and i need to check for some kind of "done flag'.

my old  nrfx_twi driver had to have a done flag checked.

let me know if you think this will work. 

phil

Related