Skip to content

[I2C, hal, SW] Restructured and refactored the functions#535

Open
KinzaQamar wants to merge 4 commits into
lowRISC:mainfrom
KinzaQamar:i2c_sw_modif
Open

[I2C, hal, SW] Restructured and refactored the functions#535
KinzaQamar wants to merge 4 commits into
lowRISC:mainfrom
KinzaQamar:i2c_sw_modif

Conversation

@KinzaQamar
Copy link
Copy Markdown
Contributor

@KinzaQamar KinzaQamar commented May 7, 2026

Taking the transfer checking part out from the i2c_read_byte() and i2c_write_byte() functions. It is needed when you want to controller wants to read / write multiple bytes.

Merge #545 first as a dependency on this PR

@KinzaQamar KinzaQamar marked this pull request as draft May 7, 2026 20:26
@KinzaQamar KinzaQamar force-pushed the i2c_sw_modif branch 8 times, most recently from c431f00 to a0269e1 Compare May 8, 2026 14:49
@KinzaQamar KinzaQamar marked this pull request as ready for review May 8, 2026 15:38
Comment thread sw/device/examples/i2c.c Outdated
Comment thread sw/device/lib/hal/i2c.c Outdated
@KinzaQamar KinzaQamar marked this pull request as draft May 11, 2026 19:33
@KinzaQamar KinzaQamar force-pushed the i2c_sw_modif branch 8 times, most recently from 49e9862 to ab7f1dc Compare May 12, 2026 12:20
@KinzaQamar KinzaQamar marked this pull request as ready for review May 12, 2026 12:20
Comment thread sw/device/lib/hal/i2c.c Outdated
Comment thread sw/device/lib/hal/i2c.c Outdated
Comment thread sw/device/lib/hal/i2c.c Outdated
@KinzaQamar KinzaQamar force-pushed the i2c_sw_modif branch 3 times, most recently from 79c9fa4 to bb82b9a Compare May 13, 2026 15:08
Taking the transfer checking part out from the i2c_read_byte()
and i2c_write_byte() functions. It is needed when you want to
controller wants to read / write multiple bytes.

Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
This is suggested in programmer's guide.

As a precautionary step, reset the FMT fifo before sending any transfer

Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
@KinzaQamar
Copy link
Copy Markdown
Contributor Author

KinzaQamar commented May 13, 2026

Tested examples/i2c.c on an FPGA server:

image

Comment thread sw/device/lib/hal/i2c.c
if (i == (num_wr_bytes - 1u)) {
fdata_reg.stop = 1u;
}
VOLATILE_WRITE(i2c->fdata, fdata_reg);
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

you should check that the fifo is not full on every iteration, otherwise it would overflow when num_wr_bytes is large.

Comment thread sw/device/lib/hal/i2c.c Outdated
bool check_wr_xfer_status(i2c_t i2c)
{
// Wait for transaction to complete and report simple succeed / fail
for (uint32_t ii = 0; ii < 10000000ul /*arbitrary number*/; ii++) {
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

why we need to check the timeout? The arbitrary number of iterations is brittle. Since this code is only used for testing, and tests already have timeout mechanims. I would replace the for loop for a while (true)

Same for the function below.

Comment thread sw/device/lib/hal/i2c.c Outdated
VOLATILE_WRITE(i2c->fdata, fdata_reg);
}

bool check_wr_xfer_status(i2c_t i2c)
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

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

The check name sugests that there's no loop.

Suggested change
bool check_wr_xfer_status(i2c_t i2c)
bool wait_for_completion(i2c_t i2c)

Same for the function below.

Signed-off-by: Kinza Qamar <kqzaman@lowrisc.org>
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