arduino / arduino/ArduinoCore-avr

I2C clock cannot go below 30MHz as prescaler is not calculated

Open
#495 1 comment 0 reactions 0 assignees View on GitHub
Dominant language
C
Stars
1.5k
Forks
1.1k
PR merge metrics
No merged PRs in 30d

Description

https://github.com/arduino/ArduinoCore-avr/blob/master/libraries/Wire/src/utility/twi.c#L139
doesn't calculate/change the TWSR prescaler, which means that we can't get below approx 16mhz / 255*2 = 30KHz
i'm happy to submit a PR, would look like this (TWSR is write-only on the two lowest bits)

```
// calculate TWBR correctly
uint8_t prescaler = 1;
uint32_t atwbr = ((F_CPU / desiredclk) - 16) / 2;
if (atwbr <= 255) {
prescaler = 1;
TWSR = 0x0;
} else if (atwbr <= 1020) {
atwbr /= 4;
prescaler = 4;
TWSR = 0x1;
} else if (atwbr <= 4080) {
atwbr /= 16;
prescaler = 16;
TWSR = 0x2;
} else if (atwbr <= 16320) {
atwbr /= 64;
prescaler = 64;
TWSR = 0x3;
}
TWBR = atwbr;
```

Contributor guide

No contributing guide indexed for this repository

Research direction

Start in libraries/Wire/src/utility/twi.c around line 139 and review how the desired clock is converted into TWBR and how TWSR is configured. Verify the prescaler cases for lower I2C frequencies and confirm that the resulting clock can fall below the current limit without changing unrelated behavior.

Written by the indexing model from the issue text.

Assessment

Tech stack
c
Domain
embedded-iot
Issue type
Bug
Difficulty
2/5
Estimated time
1-3 hours
Activity status
Stale
Clarity
Clearly specified
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.