-
-
Notifications
You must be signed in to change notification settings - Fork 8.4k
esp32: Update machine_i2c.c. #17971
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
base: master
Are you sure you want to change the base?
esp32: Update machine_i2c.c. #17971
Conversation
There is not enough space in the application area in the partition table on ESP32C6, which leads to CI failure. Do I need to modify it? |
No, we will fix that separately. |
All right, it's done |
Please check the code formatting CI and fix the failures that it found. |
All right, it's done. |
Signed-off-by: Vincent1-python <pywei201209@163.com> esp32: fix machine_i2c.c format. Signed-off-by: Vincent1-python <pywei201209@163.com> esp32: Update machine_i2c.c. Signed-off-by: Vincent1-python <pywei201209@163.com>
d849fe9
to
2fc1eee
Compare
At present, it runs normally on ESP32P4, and others need help to test. @dpgeorge |
Tried with a Generic ESP32. Accessing a real device (BME280) with hard I2C works. Did you try I2C scan with the ESP32P4? |
It can run normally on my p4.
|
If you connect a I2C target, does i2c.scan() return the target's address? |
It will be.Or you can see if there is any difference between my code and the official example of espidf.
|
I understand your answer in that you did not test your code with a real external I2C target device. That is not good. |
Looking into your code it does not handle the case for scan() properly. For scan only the address will be written. No data. And only the return state of i2c_master_probe() has to be returned. |
ports/esp32/machine_i2c.c
Outdated
return -MP_ENODEV; /* No device at address, return immediately */ | ||
} | ||
/* 1. Create a temporary device handle for this transaction */ | ||
i2c_device_config_t dev_cfg = { |
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.
Is it necessary to create this temporary device handle, or should't it be better created once at instantiation?
And if, it should be set up with the configured scl freq and not a default value.
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.
Uüdate: It is necessary, at least with IDF V5.4.2. IDF V5.5 has an API to change the I2C address of a dev_handle. Then the handle has to be created only once.
ports/esp32/machine_i2c.c
Outdated
} | ||
|
||
int machine_hw_i2c_transfer(mp_obj_base_t *self_in, uint16_t addr, size_t n, mp_machine_i2c_buf_t *bufs, unsigned int flags) { | ||
machine_hw_i2c_obj_t *self = MP_OBJ_TO_PTR(self_in); | ||
|
||
i2c_cmd_handle_t cmd = i2c_cmd_link_create(); | ||
/* 0. Probe the address to see if any device responds */ | ||
esp_err_t err = i2c_master_probe(self->bus_handle, addr, 1000); |
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.
i2c_master_probe() seems to use a fixed I2C freq of 100kHz. In certain situations that may not be appropriate.
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.
It seems not possible to change that. The freq is hard coded in the implementation of i2c_master_probe() .
You can modify it directly. I'm too busy to take care of this PR. @robert-hh |
@Vincent1-python Since I'm not a maintainer I cannot change the code of that PR. Attached is a interim state of the code, |
thank you very much.
|
Tested with a few ESP32 variants: ESP32-Generic, ESP32S2, ESP32S3, ESP32C2, ESP32C3, ESP32C6 and a BME280 I2C target device. All work fine. Attached is an updated copy of the file. No functional changes to the previous one, just text edits. |
thank you very much.
|
Signed-off-by: Vincent1-python <pywei201209@163.com>
Downloaded and tested. As expected it still works. Two things cold be changed:
|
Summary
Replace the legacy ESP-IDF I²C driver (driver/i2c.h) with the new driver/i2c_master.h API for all ESP32-series SoCs.
This change:
Removes dependency on the deprecated peripheral driver that is no longer present in ESP-IDF ≥ 6.0.
Keeps the existing machine.I2C API unchanged, so user scripts do not require modification.
Testing
At present, it runs normally on ESP32P4, and others need help to test.
Trade-offs and Alternatives