Skip to content

Commit 26d3365

Browse files
committed
samd: diagnose out of range I2C frequency
The frequency divisor is limited to 255, which gives 48MHz/2/255 ~= 94.1kHz as the lowest speed. Without this change, values below this cut-off gave higher frequencies instead, which didn't appear to have any relation to the frequency value requested. Closes: #4883
1 parent cabe96e commit 26d3365

File tree

1 file changed

+7
-0
lines changed
  • ports/atmel-samd/common-hal/busio

1 file changed

+7
-0
lines changed

ports/atmel-samd/common-hal/busio/I2C.c

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,13 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
119119
// clkrate is always 0. baud_rate is in kHz.
120120

121121
// Frequency must be set before the I2C device is enabled.
122+
// The maximum frequency divisor gives a clock rate of around 48MHz/2/255
123+
// but set_baudrate does not diagnose this problem. (This is not the
124+
// exact cutoff, but no frequency well under 100kHz is available)
125+
if (frequency < 95000) {
126+
mp_raise_ValueError(translate("Unsupported baudrate"));
127+
}
128+
122129
if (i2c_m_sync_set_baudrate(&self->i2c_desc, 0, frequency / 1000) != ERR_NONE) {
123130
reset_pin_number(sda->number);
124131
reset_pin_number(scl->number);

0 commit comments

Comments
 (0)