-
Notifications
You must be signed in to change notification settings - Fork 3k
Fix for i2c_t object not being initialized to 0 causing timeout #9895
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
Merged
Changes from all commits
Commits
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -79,6 +79,9 @@ static I2C_HandleTypeDef *i2c_handles[I2C_NUM]; | |
*/ | ||
#define FLAG_TIMEOUT ((int)0x1000) | ||
|
||
/* Declare i2c_init_internal to be used in this file */ | ||
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl); | ||
|
||
/* GENERIC INIT and HELPERS FUNCTIONS */ | ||
|
||
#if defined(I2C1_BASE) | ||
|
@@ -260,7 +263,12 @@ void i2c_sw_reset(i2c_t *obj) | |
|
||
void i2c_init(i2c_t *obj, PinName sda, PinName scl) | ||
{ | ||
memset(obj, 0, sizeof(*obj)); | ||
i2c_init_internal(obj, sda, scl); | ||
} | ||
|
||
void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl) | ||
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. Actually I think you may need to declare this function at the beginning of the file (might be a static one). |
||
{ | ||
struct i2c_s *obj_s = I2C_S(obj); | ||
|
||
// Determine the I2C to use | ||
|
@@ -454,7 +462,7 @@ void i2c_reset(i2c_t *obj) | |
/* As recommended in i2c_api.h, mainly send stop */ | ||
i2c_stop(obj); | ||
/* then re-init */ | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
} | ||
|
||
/* | ||
|
@@ -508,7 +516,7 @@ int i2c_stop(i2c_t *obj) | |
* re-init HAL state | ||
*/ | ||
if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) { | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
} | ||
|
||
return 0; | ||
|
@@ -584,7 +592,7 @@ int i2c_stop(i2c_t *obj) | |
#if DEVICE_I2CSLAVE | ||
if (obj_s->slave) { | ||
/* re-init slave when stop is requested */ | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
return 0; | ||
} | ||
#endif | ||
|
@@ -627,7 +635,7 @@ int i2c_stop(i2c_t *obj) | |
/* In case of mixed usage of the APIs (unitary + SYNC) | ||
* re-init HAL state */ | ||
if (obj_s->XferOperation != I2C_FIRST_AND_LAST_FRAME) { | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
} | ||
|
||
return 0; | ||
|
@@ -791,7 +799,7 @@ int i2c_read(i2c_t *obj, int address, char *data, int length, int stop) | |
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { | ||
DEBUG_PRINTF(" TIMEOUT or error in i2c_read\r\n"); | ||
/* re-init IP to try and get back in a working state */ | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
} else { | ||
count = length; | ||
} | ||
|
@@ -845,7 +853,7 @@ int i2c_write(i2c_t *obj, int address, const char *data, int length, int stop) | |
if ((timeout == 0) || (obj_s->event != I2C_EVENT_TRANSFER_COMPLETE)) { | ||
DEBUG_PRINTF(" TIMEOUT or error in i2c_write\r\n"); | ||
/* re-init IP to try and get back in a working state */ | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
} else { | ||
count = length; | ||
} | ||
|
@@ -907,7 +915,7 @@ void HAL_I2C_ErrorCallback(I2C_HandleTypeDef *hi2c) | |
DEBUG_PRINTF("HAL_I2C_ErrorCallback:%d, index=%d\r\n", (int) hi2c->ErrorCode, obj_s->index); | ||
|
||
/* re-init IP to try and get back in a working state */ | ||
i2c_init(obj, obj_s->sda, obj_s->scl); | ||
i2c_init_internal(obj, obj_s->sda, obj_s->scl); | ||
|
||
#if DEVICE_I2CSLAVE | ||
/* restore slave address */ | ||
|
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.
sorry but i2c_init is also used internally in several places in this file in order to reset the IP in case of communication issues. When resetting the IP, we don't want to lose the current frequency.
That's why we actually check the value of obj_s->hz
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.
Ok yes good catch, I'll post an alternate fix
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.
updated PR