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.
This commit is contained in:
Christian Mauderer
2021-06-22 13:51:17 +02:00
parent b47dbbc5f7
commit 5bb5e01356
3 changed files with 203 additions and 7 deletions
+43 -1
View File
@@ -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)
/** @} */
/**