Skip to content
Closed
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 41 additions & 0 deletions drivers/i2c/i2c_ll_stm32_v2.c
Original file line number Diff line number Diff line change
Expand Up @@ -745,17 +745,26 @@ static inline int msg_done(const struct device *dev,
{
const struct i2c_stm32_config *cfg = dev->config;
I2C_TypeDef *i2c = cfg->i2c;
int64_t start_time = k_uptime_get();

/* Wait for transfer to complete */
while (!LL_I2C_IsActiveFlag_TC(i2c) && !LL_I2C_IsActiveFlag_TCR(i2c)) {
if (check_errors(dev, __func__)) {
return -EIO;
}
if ((k_uptime_get() - start_time) >
STM32_I2C_TRANSFER_TIMEOUT_MSEC) {
return -ETIMEDOUT;
}
}
/* Issue stop condition if necessary */
if (current_msg_flags & I2C_MSG_STOP) {
LL_I2C_GenerateStopCondition(i2c);
while (!LL_I2C_IsActiveFlag_STOP(i2c)) {
if ((k_uptime_get() - start_time) >
STM32_I2C_TRANSFER_TIMEOUT_MSEC) {
return -ETIMEDOUT;
}
}

LL_I2C_ClearFlag_STOP(i2c);
Expand All @@ -772,6 +781,7 @@ static int stm32_i2c_msg_write(const struct device *dev, struct i2c_msg *msg,
I2C_TypeDef *i2c = cfg->i2c;
unsigned int len = 0U;
uint8_t *buf = msg->buf;
int64_t start_time = k_uptime_get();

msg_init(dev, msg, next_msg_flags, slave, LL_I2C_REQUEST_WRITE);

Expand All @@ -785,6 +795,11 @@ static int stm32_i2c_msg_write(const struct device *dev, struct i2c_msg *msg,
if (check_errors(dev, __func__)) {
return -EIO;
}

if ((k_uptime_get() - start_time) >
STM32_I2C_TRANSFER_TIMEOUT_MSEC) {
return -ETIMEDOUT;
}
}

LL_I2C_TransmitData8(i2c, *buf);
Expand All @@ -802,6 +817,7 @@ static int stm32_i2c_msg_read(const struct device *dev, struct i2c_msg *msg,
I2C_TypeDef *i2c = cfg->i2c;
unsigned int len = 0U;
uint8_t *buf = msg->buf;
int64_t start_time = k_uptime_get();

msg_init(dev, msg, next_msg_flags, slave, LL_I2C_REQUEST_READ);

Expand All @@ -811,6 +827,10 @@ static int stm32_i2c_msg_read(const struct device *dev, struct i2c_msg *msg,
if (check_errors(dev, __func__)) {
return -EIO;
}
if ((k_uptime_get() - start_time) >
STM32_I2C_TRANSFER_TIMEOUT_MSEC) {
return -ETIMEDOUT;
}
}

*buf = LL_I2C_ReceiveData8(i2c);
Expand Down Expand Up @@ -1181,5 +1201,26 @@ int stm32_i2c_transaction(const struct device *dev,
msg.len = rest;
} while (rest > 0U);

#ifndef CONFIG_I2C_STM32_INTERRUPT
struct i2c_stm32_data *data = dev->data;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

missing

Suggested change
const struct i2c_stm32_config *cfg = dev->config;
I2C_TypeDef *i2c = cfg->i2c;

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JarmouniA i guess we "just" need to backport #91184 first

Copy link
Contributor

@JarmouniA JarmouniA Jul 23, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@JarmouniA i guess we "just" need to backport #91184 first

That won't work, cfg will be unused after the 1st commit. The proper thing would be to squash the 2 commits from the 2 PRs, but I don't knw if that's allowed in backports.

if (ret == -ETIMEDOUT) {
if (LL_I2C_IsEnabledReloadMode(i2c)) {
LL_I2C_DisableReloadMode(i2c);
}
#if defined(CONFIG_I2C_TARGET)
data->master_active = false;
if (!data->slave_attached && !data->smbalert_active) {
LL_I2C_Disable(i2c);
}
#else
if (!data->smbalert_active) {
LL_I2C_Disable(i2c);
}
#endif
return -EIO;
}
#endif /* !CONFIG_I2C_STM32_INTERRUPT */

return ret;
}