Skip to content

[i2c, sw, hal] Added functions to calculate timing params for all supported speeds and correct SCL high time#565

Open
KinzaQamar wants to merge 2 commits into
lowRISC:mainfrom
KinzaQamar:scl_high_time_calc
Open

[i2c, sw, hal] Added functions to calculate timing params for all supported speeds and correct SCL high time#565
KinzaQamar wants to merge 2 commits into
lowRISC:mainfrom
KinzaQamar:scl_high_time_calc

Conversation

@KinzaQamar
Copy link
Copy Markdown
Contributor

@KinzaQamar KinzaQamar commented May 18, 2026

First commit takes care that the resultant SCL high cycles must satisfy below equations:
** scl_high_cycles >= 4 (to support correct function in clock streching)
** scl_high_cycles = scl_period_cycles - (rise_cycles + fall_cycles
+ scl_low_cycles)

Second commit added calculations for each supported timing parameters

@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch from b7055d9 to a5d3443 Compare May 18, 2026 18:02
@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch from a5d3443 to dce994d Compare May 18, 2026 18:04
@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch 2 times, most recently from b625e70 to c2f861e Compare May 18, 2026 20:42
Copy link
Copy Markdown
Collaborator

@engdoreis engdoreis left a comment

Choose a reason for hiding this comment

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

I think that we should bite the bullet and port the Opentitan dif_i2c to mocha. Starting by the timing functions.

Comment thread sw/device/lib/hal/i2c.c Outdated
@KinzaQamar
Copy link
Copy Markdown
Contributor Author

I think that we should bite the bullet and port the Opentitan dif_i2c to mocha. Starting by the timing functions.

You're right. I'll do this in a separate PR

@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch 2 times, most recently from 5456433 to d1fcf2d Compare May 19, 2026 14:25
@KinzaQamar KinzaQamar requested a review from engdoreis May 19, 2026 14:30
@KinzaQamar
Copy link
Copy Markdown
Contributor Author

I think that we should bite the bullet and port the Opentitan dif_i2c to mocha. Starting by the timing functions.

You're right. I'll do this in a separate PR

I've done it as part of this PR

@KinzaQamar KinzaQamar changed the title [i2c, sw, hal] Added a function to calculate SCL high cycles [i2c, sw, hal] Added functions to calculate timing params for all supported speeds and correct SCL high time May 19, 2026
@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch 2 times, most recently from d709543 to a7e6856 Compare May 20, 2026 21:00
Copy link
Copy Markdown
Contributor

@ziuziakowska ziuziakowska left a comment

Choose a reason for hiding this comment

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

Just some style nits.

Comment thread sw/device/lib/hal/i2c.h Outdated
Comment on lines +91 to +105
typedef enum { standard_mode, fast_mode, fast_plus_mode } i2c_speed_t;

typedef struct {
uint32_t rise_cycles;
uint32_t fall_cycles;
uint32_t scl_low_cycles;
uint32_t scl_high_cycles;
uint32_t scl_period_cycles;
uint32_t setup_start_cycles;
uint32_t hold_start_cycles;
uint32_t setup_data_cycles;
uint32_t hold_data_cycles;
uint32_t setup_stop_cycles;
uint32_t bus_free_time_cycles;
} i2c_timing_params_cycles_t;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I would prefer that enums and structs are not typedef-ed out (with some exceptions, e.g volatile-qualified pointers for devices like i2c_t)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These typedefs are going to be used as function arguments and return types. Isn't it nice to make them a type and use them wherever required?

Comment thread sw/device/lib/hal/i2c.h Outdated
Comment thread sw/device/lib/hal/i2c.h
uint32_t hold_data_cycles;
uint32_t setup_stop_cycles;
uint32_t bus_free_time_cycles;
} i2c_timing_params_cycles_t;
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I think cycles in the type name is redundant as the units are in the struct member names already. Perhaps this should be i2c_timing_parameters?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Just naming it i2c_timing_paramaters will get misunderstood as timing parameters in ns

Comment thread sw/device/lib/hal/i2c.c Outdated
Comment thread sw/device/lib/hal/i2c.c Outdated
@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch 4 times, most recently from b625e70 to fb1bd95 Compare May 21, 2026 14:02
It takes care that the resultant SCL high cycles must satisfy below
equations:
** scl_high_cycles >= 4 (to support correct function in clock
streching)
** scl_high_cycles = scl_period_cycles - (rise_cycles + fall_cycles
                     + scl_low_cycles)

Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
OT's I2C block supports Standard, Fast and Fast Plus modes.

Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
@KinzaQamar KinzaQamar force-pushed the scl_high_time_calc branch from fb1bd95 to c00e4a9 Compare May 21, 2026 14:05
@KinzaQamar KinzaQamar requested a review from ziuziakowska May 21, 2026 14:53
@KinzaQamar
Copy link
Copy Markdown
Contributor Author

Hi @ziuziakowska

Thanks for your review. I've addressed your comments and left some questions.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants