Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

drivers: i2c: Introduce I2C timeout for SAM0 #79311

Merged
Merged
Show file tree
Hide file tree
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
8 changes: 8 additions & 0 deletions drivers/i2c/Kconfig.sam0
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,11 @@ config I2C_SAM0_DMA_DRIVEN
DMA driven mode requires fewer interrupts to handle the
transaction and ensures that high speed modes are not delayed
by data reloading.

config I2C_SAM0_TRANSFER_TIMEOUT
int "Transfer timeout [ms]"
default 500
help
Timeout in milliseconds used for each I2C transfer.
0 means that the driver should use the K_FOREVER value,
i.e. it should wait as long as necessary.
13 changes: 12 additions & 1 deletion drivers/i2c/i2c_sam0.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,12 @@ LOG_MODULE_REGISTER(i2c_sam0, CONFIG_I2C_LOG_LEVEL);
#define SERCOM_I2CM_CTRLA_MODE_I2C_MASTER SERCOM_I2CM_CTRLA_MODE(5)
#endif

#if CONFIG_I2C_SAM0_TRANSFER_TIMEOUT
#define I2C_TRANSFER_TIMEOUT_MSEC K_MSEC(CONFIG_I2C_SAM0_TRANSFER_TIMEOUT)
#else
#define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
#endif

struct i2c_sam0_dev_config {
SercomI2cm *regs;
const struct pinctrl_dev_config *pcfg;
Expand Down Expand Up @@ -504,7 +510,12 @@ static int i2c_sam0_transfer(const struct device *dev, struct i2c_msg *msgs,
irq_unlock(key);

/* Now wait for the ISR to handle everything */
k_sem_take(&data->sem, K_FOREVER);
ret = k_sem_take(&data->sem, I2C_TRANSFER_TIMEOUT_MSEC);

if (ret != 0) {
ret = -EIO;
goto unlock;
}

if (data->msg.status) {
if (data->msg.status & SERCOM_I2CM_STATUS_ARBLOST) {
Expand Down
Loading