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

Please add uint32_t getClock() to the Wire library #11457

Open
RobTillaart opened this issue Apr 25, 2021 · 1 comment
Open

Please add uint32_t getClock() to the Wire library #11457

RobTillaart opened this issue Apr 25, 2021 · 1 comment
Labels
feature request A request to make an enhancement (not a bug fix) Library: Wire The Wire Arduino library

Comments

@RobTillaart
Copy link

The Wire library has a function void setClock(uint32_t frequency) to set the I2C clock frequency.

It would be useful for (library) developers to be able to read the current clock frequency so I could drive a device at maximum frequency and reset the I2C bus afterwards.

some_device_call()
{
  uint32_t prev_freq = Wire.getClock();

  Wire.setClock(_my_max_freq);
  // do my I2C thing

  Wire.setClock(prev_freq);

  return whatever;
}

The ESP32 does already support a getClock() function,

For AVR based devices it could look like

FILE: Wire.h

//  class TwoWire : public Stream
...
uint32_t getClock();

FILE: Wire.cpp

uint32_t  TwoWire::getClock()
{
 uint32_t speed = F_CPU / ((TWBR * 2) + 16);
 return speed;
}

Yes the value may not be exact what was set by setClock() but it would be the actual frequency.

@per1234 per1234 added feature request A request to make an enhancement (not a bug fix) Library: Wire The Wire Arduino library labels Apr 25, 2021
@RobTillaart
Copy link
Author

RobTillaart commented Jan 21, 2022

Just to mention I implemented a related workaround in this library - https://github.com/RobTillaart/AGS02MA
This sensor has an I2C bus working at 30KHz.
Setting the bus to this value would slow down all other I2C devices quite a bit.

Having a WIre.getClock() would make life definitely easier.

One might add a define like ARDUINO__I2C__GETCLOCK so it can be tested compile-time if function exists.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
feature request A request to make an enhancement (not a bug fix) Library: Wire The Wire Arduino library
Projects
None yet
Development

No branches or pull requests

2 participants