arduino / arduino/Arduino

Please add uint32_t getClock() to the Wire library

Open
#11,457 1 comment 2 reactions 0 assignees View on GitHub
feature request Library: Wire
Dominant language
Java
Stars
14.6k
Forks
7k
PR merge metrics
No merged PRs in 30d

Description

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.

```cpp
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
```cpp
// class TwoWire : public Stream
...
uint32_t getClock();
```

FILE: Wire.cpp
```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.

Contributor guide

Open the contributing guide

Research direction

Start with the Wire.h and Wire.cpp files named in the issue, and compare the existing AVR Wire implementation with the ESP32 getClock() support mentioned. Add the public getClock() declaration and AVR implementation, then verify that it reports the current I2C clock frequency and preserves the usage shown in the issue.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp
Domain
embedded-iot
Issue type
Feature
Difficulty
2/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.