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

i2c_master handle NACK (IDFGH-12083) #13142

Conversation

MatthiasKunnen
Copy link

Closes #13134

In summary, this PR:

  • Resolves a deadlock on s_i2c_synchronous_transaction failure
  • Handles NACK received on the slave address when using i2c_master_receive
  • Handles NACK received on the slave address and write bytes when using i2c_master_transmit and i2c_master_transmit_receive

TODO

i2c_master_probe reports incorrect NOT FOUND if previous transaction failed

I noticed that after dealing with an unexpected NACK (on slave address or write byte) on i2c_master_transmit, the i2c_master_probe function will report NOT_FOUND on an existing address. This is seems to be due to some missing cleanup or resetting of initial state when starting i2c_master_probe.

I was planning on continuing to develop this PR until the probe issue was resolved and was looking into writing tests. Since then, @mythbuster5 has voiced that they would add NACK checking in #13136 (comment). Wanting to prevent duplication of effort, I'm not sure if I should continue further development on this PR.

I look forward to feedback, both on the current approach and if/how to continue.

Now, when device address NACK is encountered using i2c_master_receive,
the transaction will be aborted with a stop signal.

See https://www.github.com/espressif/esp-idf/issues/13134.
Aborts transaction if device address or write byte is NACK'ed on
i2c_master_transmit and i2c_master_transmit_receive.

See https://www.github.com/espressif/esp-idf/issues/13134.
When a transaction error occurred, the bus would be blocked
indefinitely.
@CLAassistant
Copy link

CLAassistant commented Feb 8, 2024

CLA assistant check
All committers have signed the CLA.

Copy link

github-actions bot commented Feb 8, 2024

Messages
📖 You might consider squashing your 3 commits (simplifying branch history).

👋 Hello MatthiasKunnen, we appreciate your contribution to this project!


📘 Please review the project's Contributions Guide for key guidelines on code, documentation, testing, and more.

🖊️ Please also make sure you have read and signed the Contributor License Agreement for this project.

Click to see more instructions ...


This automated output is generated by the PR linter DangerJS, which checks if your Pull Request meets the project's requirements and helps you fix potential issues.

DangerJS is triggered with each push event to a Pull Request and modify the contents of this comment.

Please consider the following:
- Danger mainly focuses on the PR structure and formatting and can't understand the meaning behind your code or changes.
- Danger is not a substitute for human code reviews; it's still important to request a code review from your colleagues.
- Addressing info messages (📖) is strongly recommended; they're less critical but valuable.
- To manually retry these Danger checks, please navigate to the Actions tab and re-run last Danger workflow.

Review and merge process you can expect ...


We do welcome contributions in the form of bug reports, feature requests and pull requests via this public GitHub repository.

This GitHub project is public mirror of our internal git repository

1. An internal issue has been created for the PR, we assign it to the relevant engineer.
2. They review the PR and either approve it or ask you for changes or clarifications.
3. Once the GitHub PR is approved, we synchronize it into our internal git repository.
4. In the internal git repository we do the final review, collect approvals from core owners and make sure all the automated tests are passing.
- At this point we may do some adjustments to the proposed change, or extend it by adding tests or documentation.
5. If the change is approved and passes the tests it is merged into the default branch.
5. On next sync from the internal git repository merged change will appear in this public GitHub repository.

Generated by 🚫 dangerJS against 3ba1bab

@espressif-bot espressif-bot added the Status: Opened Issue is new label Feb 8, 2024
@github-actions github-actions bot changed the title i2c_master handle NACK i2c_master handle NACK (IDFGH-12083) Feb 8, 2024
@KJ7LNW
Copy link
Contributor

KJ7LNW commented Feb 11, 2024

Hi @MatthiasKunnen, thanks for writing up this PR!

I just tested this PR with the i2c async callback as follows, and it doesn't seem to work as expected. First, this is how we tested without the PR applied:

Without the PR

  1. Start querying an i2c RTC clock, it works.
  2. Register an async i2c callback with i2c_master_register_event_callbacks
  3. Unplug the clock from the i2c bus: the data returned becomes all 0xFF's, which is expected.
  4. The callback from above never provides I2C_EVENT_NACK, it always claims I2C_EVENT_DONE.
  5. Plug the i2c clock back into the bus (hot), and it continues to update: The 0xFF's return to proper time data.

With the PR

This is the same procedure with this PR applied:

  1. Start querying an i2c RTC clock, it works.
  2. Register an async i2c callback with i2c_master_register_event_callbacks
  3. Unplug the clock from the i2c bus: the data returned becomes all 0x00's, which may be intended, but it changes the "no data" behavior from the current implementation. I don't care one way or the other about what the data looks like in the error case, provided that we can get a NACK indicated somehow.
  4. The callback from above never provides I2C_EVENT_NACK, it always claims I2C_EVENT_DONE.
  5. Plug the i2c clock back into the bus (hot), and it no longer updates.
    - N.B.: I know i2c hot-plug is not generally supported, but without this PR the i2c bus does recover from the disconnect/reconnect.

TL;DR
This PR doesn't fix NACK detection for async (I've not tested sync), and it introduces a hang when the bus is compromised.

Do you know what may have caused the hang, and how might the NACK detection be fixed for asynchronous requests?

This is our callback:

bool esp32_i2c_dev_callback(i2c_master_dev_handle_t i2c_dev, const i2c_master_event_data_t *evt_data, void *arg)
{
       // i2c_req_t is our own i2c request tracking structure.  For the purpose of this example, 
       // its just a way of passing `evt_data->event` outside of the ISR:
       i2c_req_t *req = arg;

       req->esp_callback_event = evt_data->event;
       if (evt_data->event == I2C_EVENT_DONE)
               req->status = i2cTransferDone;
       else if (evt_data->event == I2C_EVENT_NACK) // <- never happens
               req->status = i2cTransferError;
       else
               req->status = -evt_data->event;

       return false;
}

@MatthiasKunnen
Copy link
Author

Since I read that the async API was still experimental, I hadn't tested it before pausing development.

I'll have a look and see what can be done.

@KJ7LNW
Copy link
Contributor

KJ7LNW commented Feb 15, 2024

I'll have a look and see what can be done.

Thanks, I appreciate it. Here is a simple test routine that implements both sync and async side-by-side if it helps you with testing:

https://github.com/KJ7LNW/esp32-i2c-test

@mythbuster5
Copy link
Collaborator

I merged 016877b, so this is gonna fixed. Thanks for your contribution, I will close this one.

@espressif-bot espressif-bot added Status: Done Issue is done internally Resolution: Done Issue is done internally and removed Status: Opened Issue is new labels Nov 6, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Resolution: Done Issue is done internally Status: Done Issue is done internally
Projects
None yet
Development

Successfully merging this pull request may close these issues.

I²C NACK on write/address byte does not error using i2c_master (IDFGH-12073)
5 participants