Skip to content

I2c frequency #6444

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

Closed
1 task done
fary99 opened this issue Mar 18, 2022 · 17 comments
Closed
1 task done

I2c frequency #6444

fary99 opened this issue Mar 18, 2022 · 17 comments
Assignees
Labels
Area: ESP-IDF related ESP-IDF related issues Area: Peripherals API Relates to peripheral's APIs. Status: Solved
Milestone

Comments

@fary99
Copy link
Contributor

fary99 commented Mar 18, 2022

Board

esp32

Device Description

esp32

Hardware Configuration

not have

Version

v2.0.1

IDE Name

Arduino IDE

Operating System

windows10

Flash frequency

80

PSRAM enabled

no

Upload speed

921600

Description

I want to change the I2C clock, but I find that if the frequency exceeds 100K, the IIC frequency will not change to the size I set. For example, if I set 400K, it can only reach about 350K. Is there any way to optimize it?

Sketch

#include "Wire.h"

void setup() {
  Serial.begin(115200);
  Wire.begin(SDA,SCL,(uint32_t)409600);
}

void loop() {
  byte error, address;
  int nDevices = 0;

  Serial.println("Scanning for I2C devices ...");
  for(address = 0x01; address < 0x7f; address++){
    Wire.beginTransmission(address);
    error = Wire.endTransmission();
  }
}

Debug Message

not have

Other Steps to Reproduce

not have

I have checked existing issues, online documentation and the Troubleshooting Guide

  • I confirm I have checked existing issues, online documentation and Troubleshooting guide.
@fary99 fary99 added the Status: Awaiting triage Issue is waiting for triage label Mar 18, 2022
@Jason2866
Copy link
Collaborator

Please try latest Arduino git version. Many changes since version 2.0.1
No support anymore for v2.0.1. Release of new version 2.0.3 will be in a few days

@VojtechBartoska
Copy link
Contributor

Hi @fary99, did you try it with v2.0.2?

@VojtechBartoska VojtechBartoska added Resolution: Awaiting response Waiting for response of author and removed Status: Awaiting triage Issue is waiting for triage labels Mar 24, 2022
@fary99
Copy link
Contributor Author

fary99 commented Mar 29, 2022

Tried it. Same thing

@VojtechBartoska VojtechBartoska added Status: Test needed Issue needs testing Area: Peripherals API Relates to peripheral's APIs. and removed Resolution: Awaiting response Waiting for response of author labels Mar 29, 2022
@VojtechBartoska
Copy link
Contributor

Hello @fary99, thanks for your test.

@PilnyTomas Can you please test this issue on the latest master? In the meantime.

@PilnyTomas
Copy link
Contributor

Hi, what exactly does it means that the frequency does not change - are you checking it on an oscilloscope, or how?
Right now I can check it only via software and it seems ok. The maximum frequency is 1 million - anything above that will set that 1 million.
Please try to call Wire.getClock() to see what the software has to say about the requested clock frequency.
It is also good practice to check the result of functions. Wire.begin returns true if started successfully.

@PilnyTomas PilnyTomas added the Resolution: Awaiting response Waiting for response of author label Mar 30, 2022
@VojtechBartoska VojtechBartoska moved this to Under investigation in Arduino ESP32 Core Project Roadmap Mar 30, 2022
@PilnyTomas
Copy link
Contributor

Hi again, we already checked the issue on the oscilloscope and indeed the real output CLK frequency is off by a lot. We will investigate...

@VojtechBartoska VojtechBartoska removed the Resolution: Awaiting response Waiting for response of author label Mar 31, 2022
@fary99
Copy link
Contributor Author

fary99 commented Apr 1, 2022

Hi again, we already checked the issue on the oscilloscope and indeed the real output CLK frequency is off by a lot. We will investigate...

Yes, the IIC frequency can not exceed 100K set in the library file, but I have seen the introduction of ESP-IDF, the IIC frequency can exceed 100K. So I modified the source code so that it could be set to be greater than 100K. Then I used the logic analyzer to capture the frequency of IIC, but it was different from the actual. Looking forward to your solution. Here is the code I modified
esp_err_t i2cInit(uint8_t i2c_num, int8_t sda, int8_t scl, uint32_t frequency){
if(i2c_num >= SOC_I2C_NUM){
return ESP_ERR_INVALID_ARG;
}
#if !CONFIG_DISABLE_HAL_LOCKS
if(bus[i2c_num].lock == NULL){
bus[i2c_num].lock = xSemaphoreCreateMutex();
if(bus[i2c_num].lock == NULL){
log_e("xSemaphoreCreateMutex failed");
return ESP_ERR_NO_MEM;
}
}
//acquire lock
if(xSemaphoreTake(bus[i2c_num].lock, portMAX_DELAY) != pdTRUE){
log_e("could not acquire lock");
return ESP_FAIL;
}
#endif
if(bus[i2c_num].initialized){
log_e("bus is already initialized");
return ESP_FAIL;
}
//-----------------------------This is modified-----------------------//
if(!frequency){
frequency = 100000UL;
}
//--------------------------------------------------------------------//

@dalbert2
Copy link

dalbert2 commented Apr 24, 2022

Confirmed on an oscilloscope that the I2C bus frequency is significantly lower than configured. For example, when Wire() is initialized at 400kbps, SCL is clocked around 360kHz. When Wire.begin() is initialized at 450kbps, SCL is around 384kHz.

I'm using IO21, IO22, 2K pull-ups on SDA and SCL, and current Arduino Core:
SDK:v4.4-dev-3569-g6a7d83af19-dirty
IDF:v4.4-dev-3569-g6a7d83af19-dirty

I was also surprised to see the ESP32 not assert SDA synchronously with SCL for the first bit after an ACK. It looks like glitching at 400kbps and is clearer at 100kbps (both shown below). For the bit after an ACK, SDA floats briefly before the ESP32 drives it low. Is it possible that this is contributing to ESP32 I2C instability at higher clock speeds? This happens to the next data bit after each ACK. Is there a reason the ESP32 doesn't drive SDA low (for a 0 bit) concurrently with falling SCL?

ESP32_I2C_glitch_after_ACK_400kbps
ESP32_I2C_glitch_after_ACK_100kbps

@dalbert2
Copy link

dalbert2 commented Apr 25, 2022

I tried upstream (4.4-beta1) and I2C reliability is dramatically better.
SDK:v4.4-beta1-189-ga79dc75f0a
IDF:v4.4-beta1-189-ga79dc75f0a

When I initialize wire at 400kbps: Wire.begin(IO_SDA, IO_SCL, (uint32_t)400000);
SCL is clocked around 333kbps, but transactions are reliable (which they are not in 4.4-dev)

@PilnyTomas
Copy link
Contributor

Hi, we are investigating this issue with ESP-IDF team.
IDF-4960

@PilnyTomas PilnyTomas added Area: ESP-IDF related ESP-IDF related issues Status: Pending and removed Status: Test needed Issue needs testing labels May 3, 2022
@VojtechBartoska VojtechBartoska moved this from Under investigation to In Progress in Arduino ESP32 Core Project Roadmap May 4, 2022
@VojtechBartoska VojtechBartoska moved this from In Progress to In Review in Arduino ESP32 Core Project Roadmap May 30, 2022
@VojtechBartoska
Copy link
Contributor

Status update: Issue is solved in ESP-IDF, we will let you know when it's reflected to Arduino core.

@dalbert2
Copy link

Thank you @VojtechBartoska, I'm actually working on switching to ESP-IDF, so I'll try it there.

@VojtechBartoska VojtechBartoska added this to the 2.0.5 milestone Jul 27, 2022
@VojtechBartoska VojtechBartoska added Status: Test needed Issue needs testing and removed Status: Pending labels Jul 27, 2022
@VojtechBartoska
Copy link
Contributor

@PilnyTomas Can you please just quickly retest this? It's supposed to be solved.

@VojtechBartoska VojtechBartoska moved this from In Review to Todo in Arduino ESP32 Core Project Roadmap Jul 27, 2022
@PilnyTomas
Copy link
Contributor

The IDF fix has improved the situation, but it is still not exactly matching the requested frequency.
For example:
Requested 500 000Hz
Before the fix, with 10k Ohm external pullup: 430 000Hz
After the fix, with 10k Ohm external pullup: 460 000Hz
image

@dpharris
Copy link

dpharris commented Aug 1, 2022 via email

@PilnyTomas
Copy link
Contributor

Hi everyone,
The IDF fix is available for Arduino users on a branch idf-release/v4.4 git checkout idf-release/v4.4 and it should be merged in next version (?) @VojtechBartoska

I have retested the frequency response to the external pull-up value and it seems that lower values correspond better to the requester frequency - try using values less than 5kOhm - 1k should be pretty good.

image

Also according to Technical Reference Manual the supported speed is up to 400kbit/s (see page 278)

image

@VojtechBartoska VojtechBartoska moved this from Todo to In Review in Arduino ESP32 Core Project Roadmap Aug 8, 2022
@VojtechBartoska
Copy link
Contributor

Yes, this will be included for 2.0.5 milestone.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Area: ESP-IDF related ESP-IDF related issues Area: Peripherals API Relates to peripheral's APIs. Status: Solved
Projects
Development

No branches or pull requests

6 participants