Skip to content

Commit b0f08ba

Browse files
committed
Close algorithm edge case to match SPI
1 parent 776c9b0 commit b0f08ba

File tree

1 file changed

+13
-7
lines changed
  • ports/stm32f4/common-hal/busio

1 file changed

+13
-7
lines changed

ports/stm32f4/common-hal/busio/I2C.c

Lines changed: 13 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -66,20 +66,26 @@ void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
6666
}
6767
}
6868

69-
7069
void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
7170
const mcu_pin_obj_t* scl, const mcu_pin_obj_t* sda, uint32_t frequency, uint32_t timeout) {
7271

7372
//match pins to I2C objects
7473
I2C_TypeDef * I2Cx;
75-
7674
uint8_t sda_len = sizeof(mcu_i2c_sda_list)/sizeof(*mcu_i2c_sda_list);
7775
uint8_t scl_len = sizeof(mcu_i2c_scl_list)/sizeof(*mcu_i2c_scl_list);
76+
bool i2c_taken = false;
77+
7878
for(uint i=0; i<sda_len;i++) {
7979
if (mcu_i2c_sda_list[i].pin == sda) {
8080
for(uint j=0; j<scl_len;j++) {
8181
if ((mcu_i2c_scl_list[j].pin == scl)
8282
&& (mcu_i2c_scl_list[j].i2c_index == mcu_i2c_sda_list[i].i2c_index)) {
83+
//keep looking if the I2C is taken, could be another SCL that works
84+
if(reserved_i2c[mcu_i2c_scl_list[i].i2c_index-1]) {
85+
i2c_taken = true;
86+
continue;
87+
}
88+
8389
self->scl = &mcu_i2c_scl_list[j];
8490
self->sda = &mcu_i2c_sda_list[i];
8591
break;
@@ -92,11 +98,11 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
9298
if(self->sda!=NULL && self->scl!=NULL ) {
9399
I2Cx = mcu_i2c_banks[self->sda->i2c_index-1];
94100
} else {
95-
mp_raise_RuntimeError(translate("Invalid I2C pin selection"));
96-
}
97-
98-
if(reserved_i2c[self->sda->i2c_index-1]) {
99-
mp_raise_RuntimeError(translate("Hardware busy, try alternative pins"));
101+
if (i2c_taken) {
102+
mp_raise_ValueError(translate("Hardware busy, try alternative pins"));
103+
} else {
104+
mp_raise_ValueError(translate("Invalid I2C pin selection"));
105+
}
100106
}
101107

102108
//Start GPIO for each pin

0 commit comments

Comments
 (0)