drivers: add mutex to the i2c-gpio driver.

Mutexs  were added to improve the overlapping phenomenon
because there is a part that shares the i2c line.

Signed-off-by: yeongnam chu <yeongnam.chu@assaabloy.com>
This commit is contained in:
yeongnam chu 2023-05-02 16:37:02 +09:00 committed by Fabio Baltieri
parent 609bb002e6
commit 5a6813519e

View file

@ -21,9 +21,9 @@
* When the SDA pin is read it must return the state of the physical hardware
* line, not just the last state written to it for output.
*
* The SCL pin should be configured in the same manner as SDA, or, if it is known
* that the hardware attached to pin doesn't attempt clock stretching, then the
* SCL pin may be a push/pull output.
* The SCL pin should be configured in the same manner as SDA, or, if it is
* known that the hardware attached to pin doesn't attempt clock stretching,
* then the SCL pin may be a push/pull output.
*/
#include <zephyr/device.h>
@ -47,6 +47,7 @@ struct i2c_gpio_config {
/* Driver instance data */
struct i2c_gpio_context {
struct i2c_bitbang bitbang; /* Bit-bang library data */
struct k_mutex mutex;
};
static void i2c_gpio_set_scl(void *io_context, int state)
@ -81,24 +82,45 @@ static const struct i2c_bitbang_io io_fns = {
static int i2c_gpio_configure(const struct device *dev, uint32_t dev_config)
{
struct i2c_gpio_context *context = dev->data;
int rc;
return i2c_bitbang_configure(&context->bitbang, dev_config);
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_configure(&context->bitbang, dev_config);
k_mutex_unlock(&context->mutex);
return rc;
}
static int i2c_gpio_transfer(const struct device *dev, struct i2c_msg *msgs,
uint8_t num_msgs, uint16_t slave_address)
{
struct i2c_gpio_context *context = dev->data;
int rc;
return i2c_bitbang_transfer(&context->bitbang, msgs, num_msgs,
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_transfer(&context->bitbang, msgs, num_msgs,
slave_address);
k_mutex_unlock(&context->mutex);
return rc;
}
static int i2c_gpio_recover_bus(const struct device *dev)
{
struct i2c_gpio_context *context = dev->data;
int rc;
return i2c_bitbang_recover_bus(&context->bitbang);
k_mutex_lock(&context->mutex, K_FOREVER);
rc = i2c_bitbang_recover_bus(&context->bitbang);
k_mutex_unlock(&context->mutex);
return rc;
}
static struct i2c_driver_api api = {
@ -151,6 +173,12 @@ static int i2c_gpio_init(const struct device *dev)
return err;
}
err = k_mutex_init(&context->mutex);
if (err) {
LOG_ERR("Failed to create the i2c lock mutex : %d", err);
return err;
}
return 0;
}