drivers: i2c: i2c_gecko: add exclusive access

- Add exclusive and atomic access to resource

Fixes #79110

Signed-off-by: Romain Pelletant <romainp@kickmaker.net>
This commit is contained in:
Romain Pelletant 2024-09-27 14:33:50 +02:00 committed by David Leach
parent f4e07d15d6
commit 8ee66a151e
1 changed files with 15 additions and 0 deletions

View File

@ -37,6 +37,7 @@ struct i2c_gecko_config {
struct i2c_gecko_data {
struct k_sem device_sync_sem;
struct k_sem bus_lock;
uint32_t dev_config;
#if defined(CONFIG_I2C_TARGET)
struct i2c_target_config *target_cfg;
@ -68,6 +69,8 @@ static int i2c_gecko_configure(const struct device *dev, uint32_t dev_config_raw
return -EINVAL;
}
k_sem_take(&data->bus_lock, K_FOREVER);
data->dev_config = dev_config_raw;
i2cInit.freq = baudrate;
@ -77,6 +80,8 @@ static int i2c_gecko_configure(const struct device *dev, uint32_t dev_config_raw
I2C_Init(base, &i2cInit);
k_sem_give(&data->bus_lock);
return 0;
}
@ -93,6 +98,8 @@ static int i2c_gecko_transfer(const struct device *dev, struct i2c_msg *msgs, ui
return 0;
}
k_sem_take(&data->bus_lock, K_FOREVER);
seq.addr = addr << 1;
do {
@ -137,6 +144,8 @@ static int i2c_gecko_transfer(const struct device *dev, struct i2c_msg *msgs, ui
} while (num_msgs);
finish:
k_sem_give(&data->bus_lock);
if (ret != i2cTransferDone) {
ret = -EIO;
}
@ -145,10 +154,16 @@ finish:
static int i2c_gecko_init(const struct device *dev)
{
struct i2c_gecko_data *data = dev->data;
const struct i2c_gecko_config *config = dev->config;
uint32_t bitrate_cfg;
int error;
/* Initialize mutex to guarantee that each transaction
* is atomic and has exclusive access to the I2C bus
*/
k_sem_init(&data->bus_lock, 1, 1);
CMU_ClockEnable(config->clock, true);
error = pinctrl_apply_state(config->pcfg, PINCTRL_STATE_DEFAULT);