@@ -66,20 +66,26 @@ void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) {
66
66
}
67
67
}
68
68
69
-
70
69
void common_hal_busio_i2c_construct (busio_i2c_obj_t * self ,
71
70
const mcu_pin_obj_t * scl , const mcu_pin_obj_t * sda , uint32_t frequency , uint32_t timeout ) {
72
71
73
72
//match pins to I2C objects
74
73
I2C_TypeDef * I2Cx ;
75
-
76
74
uint8_t sda_len = sizeof (mcu_i2c_sda_list )/sizeof (* mcu_i2c_sda_list );
77
75
uint8_t scl_len = sizeof (mcu_i2c_scl_list )/sizeof (* mcu_i2c_scl_list );
76
+ bool i2c_taken = false;
77
+
78
78
for (uint i = 0 ; i < sda_len ;i ++ ) {
79
79
if (mcu_i2c_sda_list [i ].pin == sda ) {
80
80
for (uint j = 0 ; j < scl_len ;j ++ ) {
81
81
if ((mcu_i2c_scl_list [j ].pin == scl )
82
82
&& (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
+
83
89
self -> scl = & mcu_i2c_scl_list [j ];
84
90
self -> sda = & mcu_i2c_sda_list [i ];
85
91
break ;
@@ -92,11 +98,11 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
92
98
if (self -> sda != NULL && self -> scl != NULL ) {
93
99
I2Cx = mcu_i2c_banks [self -> sda -> i2c_index - 1 ];
94
100
} 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
+ }
100
106
}
101
107
102
108
//Start GPIO for each pin
0 commit comments