From 5bb5e013563ed6825aa5ca0c93e1b01db7e721cd Mon Sep 17 00:00:00 2001 From: Christian Mauderer Date: Wed, 26 May 2021 16:33:40 +0200 Subject: i2c: Add non blocking read / write This adds the possibility to open an I2C bus with O_NONBLOCK (or set it later via fcntl) to get non-blocking transmissions. This means that if the bus is busy, a read, write or transfer ioctl will return with a EAGAIN errno. --- cpukit/include/dev/i2c/i2c.h | 44 +++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 43 insertions(+), 1 deletion(-) (limited to 'cpukit/include/dev/i2c') diff --git a/cpukit/include/dev/i2c/i2c.h b/cpukit/include/dev/i2c/i2c.h index ac2c369785..5aa45e390c 100644 --- a/cpukit/include/dev/i2c/i2c.h +++ b/cpukit/include/dev/i2c/i2c.h @@ -242,6 +242,16 @@ int i2c_bus_register( const char *bus_path ); +/** + * @brief Try to obtain the bus. + * + * @param[in] bus The bus control. + * + * @retval 0 Successful operation. + * @retval EBUSY if mutex is already locked. + */ +int i2c_bus_try_obtain(i2c_bus *bus); + /** * @brief Obtains the bus. * @@ -259,7 +269,8 @@ void i2c_bus_release(i2c_bus *bus); /** * @brief Transfers I2C messages. * - * The bus is obtained before the transfer and released afterwards. + * The bus is obtained before the transfer and released afterwards. This is the + * same like calling @ref i2c_bus_do_transfer with flags set to 0. * * @param[in] bus The bus control. * @param[in] msgs The messages to transfer. @@ -271,6 +282,37 @@ void i2c_bus_release(i2c_bus *bus); */ int i2c_bus_transfer(i2c_bus *bus, i2c_msg *msgs, uint32_t msg_count); +/** + * @brief Transfers I2C messages with optional flags. + * + * The bus is obtained before the transfer and released afterwards. If the flag + * I2C_BUS_NOBLOCK is set and the bus is already obtained, nothing will be + * transfered and the function returns with an -EAGAIN. + * + * @param[in] bus The bus control. + * @param[in] msgs The messages to transfer. + * @param[in] msg_count The count of messages to transfer. It must be + * positive. + * @param[in] flags Options for the whole transfer. + * + * @retval 0 Successful operation. + * @retval -EAGAIN if @ref I2C_BUS_NOBLOCK is set and the bus is already + * obtained. + * @retval negative Negative error number in case of an error. + */ +int i2c_bus_do_transfer( + i2c_bus *bus, + i2c_msg *msgs, + uint32_t msg_count, + uint32_t flags +); + +/** + * @brief I2C bus transfer flag to indicate that the task should not block if + * the bus is busy on a new transfer. + */ +#define I2C_BUS_NOBLOCK (1u << 0) + /** @} */ /** -- cgit v1.2.3