From c894ad12b5a0e515a45d0cdc2cd355d1f4ab559b Mon Sep 17 00:00:00 2001 From: Jordan Yates Date: Sun, 1 Aug 2021 19:56:12 +1000 Subject: [PATCH] i2c: add `_dt` variants of i2c functions Add variants of all i2c transfer functions that accept an `i2c_dt_spec` as the bus specifier. This helps reduce code duplication in device drivers. Signed-off-by: Jordan Yates --- include/drivers/i2c.h | 189 ++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 189 insertions(+) diff --git a/include/drivers/i2c.h b/include/drivers/i2c.h index f732a9fdfb2..13d875e9f78 100644 --- a/include/drivers/i2c.h +++ b/include/drivers/i2c.h @@ -396,6 +396,25 @@ static inline int z_impl_i2c_transfer(const struct device *dev, return api->transfer(dev, msgs, num_msgs, addr); } +/** + * @brief Perform data transfer to another I2C device in master mode. + * + * This is equivalent to: + * + * i2c_transfer(spec->bus, msgs, num_msgs, spec->addr); + * + * @param spec I2C specification from devicetree. + * @param msgs Array of messages to transfer. + * @param num_msgs Number of messages to transfer. + * + * @return a value from i2c_transfer() + */ +static inline int i2c_transfer_dt(const struct i2c_dt_spec *spec, + struct i2c_msg *msgs, uint8_t num_msgs) +{ + return i2c_transfer(spec->bus, msgs, num_msgs, spec->addr); +} + /** * @brief Recover the I2C bus * @@ -564,6 +583,25 @@ static inline int i2c_write(const struct device *dev, const uint8_t *buf, return i2c_transfer(dev, &msg, 1, addr); } +/** + * @brief Write a set amount of data to an I2C device. + * + * This is equivalent to: + * + * i2c_write(spec->bus, buf, num_bytes, spec->addr); + * + * @param spec I2C specification from devicetree. + * @param buf Memory pool from which the data is transferred. + * @param num_bytes Number of bytes to write. + * + * @return a value from i2c_write() + */ +static inline int i2c_write_dt(const struct i2c_dt_spec *spec, + const uint8_t *buf, uint32_t num_bytes) +{ + return i2c_write(spec->bus, buf, num_bytes, spec->addr); +} + /** * @brief Read a set amount of data from an I2C device. * @@ -590,6 +628,25 @@ static inline int i2c_read(const struct device *dev, uint8_t *buf, return i2c_transfer(dev, &msg, 1, addr); } +/** + * @brief Read a set amount of data from an I2C device. + * + * This is equivalent to: + * + * i2c_read(spec->bus, buf, num_bytes, spec->addr); + * + * @param spec I2C specification from devicetree. + * @param buf Memory pool that stores the retrieved data. + * @param num_bytes Number of bytes to read. + * + * @return a value from i2c_read() + */ +static inline int i2c_read_dt(const struct i2c_dt_spec *spec, + uint8_t *buf, uint32_t num_bytes) +{ + return i2c_read(spec->bus, buf, num_bytes, spec->addr); +} + /** * @brief Write then read data from an I2C device. * @@ -625,6 +682,32 @@ static inline int i2c_write_read(const struct device *dev, uint16_t addr, return i2c_transfer(dev, msg, 2, addr); } +/** + * @brief Write then read data from an I2C device. + * + * This is equivalent to: + * + * i2c_write_read(spec->bus, spec->addr, + * write_buf, num_write, + * read_buf, num_read); + * + * @param spec I2C specification from devicetree. + * @param write_buf Pointer to the data to be written + * @param num_write Number of bytes to write + * @param read_buf Pointer to storage for read data + * @param num_read Number of bytes to read + * + * @return a value from i2c_write_read() + */ +static inline int i2c_write_read_dt(const struct i2c_dt_spec *spec, + const void *write_buf, size_t num_write, + void *read_buf, size_t num_read) +{ + return i2c_write_read(spec->bus, spec->addr, + write_buf, num_write, + read_buf, num_read); +} + /** * @brief Read multiple bytes from an internal address of an I2C device. * @@ -654,6 +737,29 @@ static inline int i2c_burst_read(const struct device *dev, buf, num_bytes); } +/** + * @brief Read multiple bytes from an internal address of an I2C device. + * + * This is equivalent to: + * + * i2c_burst_read(spec->bus, spec->addr, start_addr, buf, num_bytes); + * + * @param spec I2C specification from devicetree. + * @param start_addr Internal address from which the data is being read. + * @param buf Memory pool that stores the retrieved data. + * @param num_bytes Number of bytes to read. + * + * @return a value from i2c_burst_read() + */ +static inline int i2c_burst_read_dt(const struct i2c_dt_spec *spec, + uint8_t start_addr, + uint8_t *buf, + uint32_t num_bytes) +{ + return i2c_burst_read(spec->bus, spec->addr, + start_addr, buf, num_bytes); +} + /** * @brief Write multiple bytes to an internal address of an I2C device. * @@ -694,6 +800,29 @@ static inline int i2c_burst_write(const struct device *dev, return i2c_transfer(dev, msg, 2, dev_addr); } +/** + * @brief Write multiple bytes to an internal address of an I2C device. + * + * This is equivalent to: + * + * i2c_burst_write(spec->bus, spec->addr, start_addr, buf, num_bytes); + * + * @param spec I2C specification from devicetree. + * @param start_addr Internal address to which the data is being written. + * @param buf Memory pool from which the data is transferred. + * @param num_bytes Number of bytes being written. + * + * @return a value from i2c_burst_write() + */ +static inline int i2c_burst_write_dt(const struct i2c_dt_spec *spec, + uint8_t start_addr, + const uint8_t *buf, + uint32_t num_bytes) +{ + return i2c_burst_write(spec->bus, spec->addr, + start_addr, buf, num_bytes); +} + /** * @brief Read internal register of an I2C device. * @@ -718,6 +847,25 @@ static inline int i2c_reg_read_byte(const struct device *dev, value, sizeof(*value)); } +/** + * @brief Read internal register of an I2C device. + * + * This is equivalent to: + * + * i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value); + * + * @param spec I2C specification from devicetree. + * @param reg_addr Address of the internal register being read. + * @param value Memory pool that stores the retrieved register value. + * + * @return a value from i2c_reg_read_byte() + */ +static inline int i2c_reg_read_byte_dt(const struct i2c_dt_spec *spec, + uint8_t reg_addr, uint8_t *value) +{ + return i2c_reg_read_byte(spec->bus, spec->addr, reg_addr, value); +} + /** * @brief Write internal register of an I2C device. * @@ -745,6 +893,25 @@ static inline int i2c_reg_write_byte(const struct device *dev, return i2c_write(dev, tx_buf, 2, dev_addr); } +/** + * @brief Write internal register of an I2C device. + * + * This is equivalent to: + * + * i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value); + * + * @param spec I2C specification from devicetree. + * @param reg_addr Address of the internal register being written. + * @param value Value to be written to internal register. + * + * @return a value from i2c_reg_write_byte() + */ +static inline int i2c_reg_write_byte_dt(const struct i2c_dt_spec *spec, + uint8_t reg_addr, uint8_t value) +{ + return i2c_reg_write_byte(spec->bus, spec->addr, reg_addr, value); +} + /** * @brief Update internal register of an I2C device. * @@ -785,6 +952,28 @@ static inline int i2c_reg_update_byte(const struct device *dev, return i2c_reg_write_byte(dev, dev_addr, reg_addr, new_value); } +/** + * @brief Update internal register of an I2C device. + * + * This is equivalent to: + * + * i2c_reg_update_byte(spec->bus, spec->addr, reg_addr, mask, value); + * + * @param spec I2C specification from devicetree. + * @param reg_addr Address of the internal register being updated. + * @param mask Bitmask for updating internal register. + * @param value Value for updating internal register. + * + * @return a value from i2c_reg_update_byte() + */ +static inline int i2c_reg_update_byte_dt(const struct i2c_dt_spec *spec, + uint8_t reg_addr, uint8_t mask, + uint8_t value) +{ + return i2c_reg_update_byte(spec->bus, spec->addr, + reg_addr, mask, value); +} + /** * @brief Dump out an I2C message *