Skip to content

drivers: i2c: Add support for clock stretching in the i2c-gpio module. #90076

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

Merged

Conversation

bas-archembedded
Copy link
Contributor

Some I2C peripherals like TI charger or gauge chips need support for I2C clock stretching. This patch includes that and makes these modules usable with I2C emulation over GPIO.

@github-actions github-actions bot requested a review from teburd May 16, 2025 17:13
@bas-archembedded bas-archembedded force-pushed the dev/add_i2c_clock_stretching branch from 21f73b6 to 182d0f0 Compare May 16, 2025 17:20
Copy link
Collaborator

@teburd teburd left a comment

Choose a reason for hiding this comment

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

Seems reasonable to me, would suggest using microsecond timeouts not millisecond, and using the WAIT_FOR macro, but otherwise looks nice

@teburd teburd requested a review from henrikbrixandersen May 16, 2025 18:22
@bas-archembedded bas-archembedded force-pushed the dev/add_i2c_clock_stretching branch from 182d0f0 to 5426ec9 Compare May 20, 2025 11:07
@bas-archembedded bas-archembedded requested a review from teburd May 20, 2025 11:09
@bas-archembedded bas-archembedded force-pushed the dev/add_i2c_clock_stretching branch from 5426ec9 to 77a922f Compare May 20, 2025 11:12
teburd
teburd previously approved these changes May 20, 2025
@@ -60,6 +64,8 @@ int i2c_bitbang_configure(struct i2c_bitbang *context, uint32_t dev_config)
return -ENOTSUP;
}

context->delays[T_CLK_STR] =
MS_TO_SYS_CLOCK_HW_CYCLES(CONFIG_I2C_GPIO_CLOCK_STRETCHING_TIMEOUT_MS);

Choose a reason for hiding this comment

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

Shouldn't this also be guarded by CONFIG_I2C_GPIO_CLOCK_STRETCHING? Do you have a different perspective on this?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This is now obsolete since it was transformed to WAIT_US() macro.

@@ -28,7 +32,7 @@ struct i2c_bitbang_io {
struct i2c_bitbang {
const struct i2c_bitbang_io *io;
void *io_context;
uint32_t delays[2];
uint32_t delays[3];

Choose a reason for hiding this comment

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

can you define ARRAY_SIZE as a macro that sets the size to 3 when clock stretching is enabled, and to 2 otherwise

For example :
#define I2C_GPIO_SIZE 2
#define I2C_GPIO_CLK_STR_SIZE 3

##ifdef CONFIG_I2C_GPIO_CLOCK_STRETCHING
#define ARRAY_SIZE I2C_GPIO_CLK_STR_SIZE
else
#define ARRAY_SIZE I2C_GPIO_SIZE
#endif

Some I2C peripherals like TI charger or gauge chips need support for I2C
clock stretching. This patch includes that and makes these modules
usable with I2C emulation over GPIO.

Signed-off-by: Bas van Loon <[email protected]>
@bas-archembedded bas-archembedded force-pushed the dev/add_i2c_clock_stretching branch from 77a922f to 5630707 Compare May 28, 2025 08:40
@pdgendt pdgendt dismissed their stale review May 28, 2025 08:48

Comments addressed

Copy link

@nashif nashif merged commit e7dd7ab into zephyrproject-rtos:main May 31, 2025
26 checks passed
@bas-archembedded bas-archembedded deleted the dev/add_i2c_clock_stretching branch May 31, 2025 12:31
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants