Skip to content
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

wrong error management in i2C functions? #1774

Closed
camelator opened this issue Jul 24, 2022 · 5 comments · Fixed by #1775
Closed

wrong error management in i2C functions? #1774

camelator opened this issue Jul 24, 2022 · 5 comments · Fixed by #1775
Assignees
Labels
Milestone

Comments

@camelator
Copy link

camelator commented Jul 24, 2022

I have an issue I don't know where it comes from... After some seconds, the I2C line remains high... but calling I2C functions always return good results.

1/ During debugging my problem I can see that the i2c_master_read function returns I2C_OK although the I2C line is crashed:
if the result of the first receive call is not HAL_OK, it returns I2C_OK because there is no else block attached to the first 'if' , instead of returning an error code.

2/ and it is the same issue within the function i2c_master_write:
it returns I2C_OK although the result of the first call to Transmit_IT is not HAL_OK

I am not sure but I think the impact of the change on the read function is neutral but some other changes should be done in the requestFrom function:

3/ if the i2c_master_write function returns an error code, it is well managed in the endTransmission function but not in the request_from function.
having endTransmission(false) without error management means you can try to read data even the write is not done.


i2c_status_e i2c_master_read(i2c_t *obj, uint8_t dev_address, uint8_t *data, uint16_t size)
{
  i2c_status_e ret = I2C_OK;
  uint32_t tickstart = HAL_GetTick();
  uint32_t delta = 0;
  uint32_t err = 0;

#if defined(I2C_OTHER_FRAME)
  uint32_t XferOptions = obj->handle.XferOptions; // save XferOptions value, because handle can be modified by HAL, which cause issue in case of NACK from slave
#endif

#if defined(I2C_OTHER_FRAME)
  if (HAL_I2C_Master_Seq_Receive_IT(&(obj->handle), dev_address, data, size, XferOptions) == HAL_OK) {
#else
  if (HAL_I2C_Master_Receive_IT(&(obj->handle), dev_address, data, size) == HAL_OK) {
#endif
    // wait for transfer completion
    while ((HAL_I2C_GetState(&(obj->handle)) != HAL_I2C_STATE_READY) && (delta < I2C_TIMEOUT_TICK)) {
      delta = (HAL_GetTick() - tickstart);
      if (HAL_I2C_GetError(&(obj->handle)) != HAL_I2C_ERROR_NONE) {
        break;
      }
    }

    err = HAL_I2C_GetError(&(obj->handle));
    if ((delta >= I2C_TIMEOUT_TICK)
        || ((err & HAL_I2C_ERROR_TIMEOUT) == HAL_I2C_ERROR_TIMEOUT)) {
      ret = I2C_TIMEOUT;
    } else {
      if ((err & HAL_I2C_ERROR_AF) == HAL_I2C_ERROR_AF) {
        ret = I2C_NACK_DATA;
      } else if (err != HAL_I2C_ERROR_NONE) {
        ret = I2C_ERROR;
      }
    }
  }
  return ret;
}

@fpistm fpistm added this to To do in STM32 core based on ST HAL via automation Jul 25, 2022
@fpistm fpistm added this to the 2.3.1/2.4.0 milestone Jul 25, 2022
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Jul 26, 2022
Previously, i2c_master_write and i2c_master_read returned I2C_OK if
first HAL call returned HAL_BUSY which was not correct.
Now make sure the i2c is ready, which guarantees a good
initialization of the read or write sequence.

Fixes stm32duino#1774

Signed-off-by: Frederic Pillon <[email protected]>
@fpistm
Copy link
Member

fpistm commented Jul 26, 2022

Hi @camelator

I've made a fix. Could you try it with your use case?

Thanks

@fpistm fpistm self-assigned this Jul 26, 2022
@camelator
Copy link
Author

I can't see your changes. Where did you put it? Which branch?

@ABOSTM
Copy link
Contributor

ABOSTM commented Jul 26, 2022

Fix is available in a Github Pull Request:
#1775

@fpistm
Copy link
Member

fpistm commented Jul 26, 2022

And it is referenced by the issue 😉

fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Jul 27, 2022
Previously, i2c_master_write and i2c_master_read returned I2C_OK if
first HAL call returned HAL_BUSY which was not correct.
Now make sure the i2c is ready, which guarantees a good
initialization of the read or write sequence.

Fixes stm32duino#1774

Signed-off-by: Frederic Pillon <[email protected]>
fpistm added a commit to fpistm/Arduino_Core_STM32 that referenced this issue Jul 29, 2022
Previously, i2c_master_write and i2c_master_read returned I2C_OK if
first HAL call returned HAL_BUSY which was not correct.
Now make sure the i2c is ready, which guarantees a good
initialization of the read or write sequence.

Fixes stm32duino#1774

Signed-off-by: Frederic Pillon <[email protected]>
STM32 core based on ST HAL automation moved this from To do to Done Jul 29, 2022
fpistm added a commit that referenced this issue Jul 29, 2022
Previously, i2c_master_write and i2c_master_read returned I2C_OK if
first HAL call returned HAL_BUSY which was not correct.
Now make sure the i2c is ready, which guarantees a good
initialization of the read or write sequence.

Fixes #1774

Signed-off-by: Frederic Pillon <[email protected]>
@camelator
Copy link
Author

Sorry for the delay, but to me the fix is nott complete:
as I wrote, the function "request_from" should be changed as well, as it can read data although wrong data sent. So the data may be inconsistent.

cparata pushed a commit to cparata/Arduino_Core_STM32 that referenced this issue Jan 31, 2023
Previously, i2c_master_write and i2c_master_read returned I2C_OK if
first HAL call returned HAL_BUSY which was not correct.
Now make sure the i2c is ready, which guarantees a good
initialization of the read or write sequence.

Fixes stm32duino#1774

Signed-off-by: Frederic Pillon <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
Development

Successfully merging a pull request may close this issue.

3 participants