-
Notifications
You must be signed in to change notification settings - Fork 25
Add pull-up detection and timeout mechanisms to prevent I2C hangs #113
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
Draft
Copilot
wants to merge
6
commits into
develop
Choose a base branch
from
copilot/fix-97
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
569fce6
Initial plan
Copilot ebf5ce4
Implement pull-up detection and timeout mechanisms for single port I2…
Copilot 65d57a2
Implement pull-up detection and timeout mechanisms for two-port I2C m…
Copilot 9a4f8fa
Implement pull-up detection and timeout mechanisms for async I2C master
Copilot f7a37fa
Implement startup-only pull-up detection following @xross feedback
Copilot 0e14baf
Extract pull-up detection functions to shared file as requested
Copilot File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -9,6 +9,7 @@ | |
| #include <timer.h> | ||
|
|
||
| #include "xassert.h" | ||
| #include "i2c_pullup_common.h" | ||
|
|
||
| #define SDA_LOW 0 | ||
| #define SCL_LOW 0 | ||
|
|
@@ -103,6 +104,7 @@ static void high_pulse_drive( | |
| p_i2c <: SCL_LOW | sdaValue | other_bits_mask; | ||
| tmr when timerafter(fall_time + compute_low_period_ticks(kbits_per_second)) :> void; | ||
| p_i2c <: SCL_HIGH | sdaValue | other_bits_mask; | ||
| // TODO: This call may hang if no pull-up present - to be addressed in future update | ||
| wait_for_clock_high(p_i2c, scl_bit_position, fall_time, (bit_time * 3) / 4, kbits_per_second); | ||
| fall_time = fall_time + bit_time; | ||
| tmr when timerafter(fall_time) :> void; | ||
|
|
@@ -126,6 +128,7 @@ static int high_pulse_sample( | |
| p_i2c <: SCL_LOW | SDA_HIGH | other_bits_mask; | ||
| tmr when timerafter(fall_time + compute_low_period_ticks(kbits_per_second)) :> void; | ||
| p_i2c <: SCL_HIGH | SDA_HIGH | other_bits_mask; | ||
| // TODO: This call may hang if no pull-up present - to be addressed in future update | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Dont need this comment since we're checking pull ups are present before we call this function |
||
| wait_for_clock_high(p_i2c, scl_bit_position, fall_time, (bit_time * 3) / 4, kbits_per_second); | ||
|
|
||
| int sample_value = peek(p_i2c); | ||
|
|
@@ -225,13 +228,22 @@ void i2c_master_single_port( | |
| set_port_drive_low(p_i2c); | ||
| p_i2c <: SCL_HIGH | SDA_HIGH | other_bits_mask; | ||
|
|
||
| // Check for pull-up resistors at startup | ||
| i2c_res_t bus_error = check_pullups_single_port(p_i2c, scl_bit_position, sda_bit_position, other_bits_mask); | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. check both pull ups at start up |
||
|
|
||
| while (1) { | ||
| select { | ||
| case (size_t i = 0; i < n; i++) | ||
| (n == 1 || (locked_client == -1 || i == locked_client)) => | ||
| c[i].read(uint8_t device, uint8_t buf[m], size_t m, | ||
| int send_stop_bit) -> i2c_res_t result: | ||
|
|
||
| // Return error immediately if bus has problems | ||
| if (bus_error != I2C_ACK) { | ||
| result = bus_error; | ||
| break; | ||
| } | ||
|
|
||
| const int stopped = locked_client == -1; | ||
| unsigned fall_time = last_fall_time; | ||
| start_bit(p_i2c, kbits_per_second, scl_bit_position, sda_bit_position, other_bits_mask, fall_time, stopped); | ||
|
|
@@ -282,6 +294,13 @@ void i2c_master_single_port( | |
| size_t &num_bytes_sent, | ||
| int send_stop_bit) -> i2c_res_t result: | ||
|
|
||
| // Return error immediately if bus has problems | ||
| if (bus_error != I2C_ACK) { | ||
| result = bus_error; | ||
| num_bytes_sent = 0; | ||
| break; | ||
| } | ||
|
|
||
| const int stopped = locked_client == -1; | ||
| unsigned fall_time = last_fall_time; | ||
| start_bit(p_i2c, kbits_per_second, scl_bit_position, sda_bit_position, other_bits_mask, fall_time, stopped); | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Dont need this comment since we're checking pull ups are present before we call this function