Skip to content

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 1 commit into from
Mar 14, 2019
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 15 additions & 7 deletions targets/TARGET_STM/i2c_api.c
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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));
Copy link
Contributor

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

Copy link
Contributor Author

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

Copy link
Contributor Author

Choose a reason for hiding this comment

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

updated PR

i2c_init_internal(obj, sda, scl);
}

void i2c_init_internal(i2c_t *obj, PinName sda, PinName scl)
Copy link
Contributor

Choose a reason for hiding this comment

The 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).
Seems to fail building with a few toolchains

{
struct i2c_s *obj_s = I2C_S(obj);

// Determine the I2C to use
Expand Down Expand Up @@ -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);
}

/*
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
}
Expand Down Expand Up @@ -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 */
Expand Down