Skip to content

Commit 4288962

Browse files
kowalewskijannashif
authored andcommitted
drivers: i2c: Introduce I2C timeout for SAM0
Adds configurable timeout for I2C transactions for SAM0 SoCs. Signed-off-by: Jan Kowalewski <[email protected]>
1 parent fe42154 commit 4288962

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

drivers/i2c/Kconfig.sam0

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,3 +18,11 @@ config I2C_SAM0_DMA_DRIVEN
1818
DMA driven mode requires fewer interrupts to handle the
1919
transaction and ensures that high speed modes are not delayed
2020
by data reloading.
21+
22+
config I2C_SAM0_TRANSFER_TIMEOUT
23+
int "Transfer timeout [ms]"
24+
default 500
25+
help
26+
Timeout in milliseconds used for each I2C transfer.
27+
0 means that the driver should use the K_FOREVER value,
28+
i.e. it should wait as long as necessary.

drivers/i2c/i2c_sam0.c

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,12 @@ LOG_MODULE_REGISTER(i2c_sam0, CONFIG_I2C_LOG_LEVEL);
2424
#define SERCOM_I2CM_CTRLA_MODE_I2C_MASTER SERCOM_I2CM_CTRLA_MODE(5)
2525
#endif
2626

27+
#if CONFIG_I2C_SAM0_TRANSFER_TIMEOUT
28+
#define I2C_TRANSFER_TIMEOUT_MSEC K_MSEC(CONFIG_I2C_SAM0_TRANSFER_TIMEOUT)
29+
#else
30+
#define I2C_TRANSFER_TIMEOUT_MSEC K_FOREVER
31+
#endif
32+
2733
struct i2c_sam0_dev_config {
2834
SercomI2cm *regs;
2935
const struct pinctrl_dev_config *pcfg;
@@ -504,7 +510,12 @@ static int i2c_sam0_transfer(const struct device *dev, struct i2c_msg *msgs,
504510
irq_unlock(key);
505511

506512
/* Now wait for the ISR to handle everything */
507-
k_sem_take(&data->sem, K_FOREVER);
513+
ret = k_sem_take(&data->sem, I2C_TRANSFER_TIMEOUT_MSEC);
514+
515+
if (ret != 0) {
516+
ret = -EIO;
517+
goto unlock;
518+
}
508519

509520
if (data->msg.status) {
510521
if (data->msg.status & SERCOM_I2CM_STATUS_ARBLOST) {

0 commit comments

Comments
 (0)