@@ -53,7 +53,8 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
5353 uint32_t sda_pinmux , scl_pinmux ;
5454
5555 // Ensure the object starts in its deinit state.
56- self -> sda_pin = NO_PIN ;
56+ common_hal_busio_i2c_mark_deinit (self );
57+
5758 Sercom * sercom = samd_i2c_get_sercom (scl , sda , & sercom_index , & sda_pinmux , & scl_pinmux );
5859 if (sercom == NULL ) {
5960 raise_ValueError_invalid_pins ();
@@ -108,21 +109,28 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
108109 mp_arg_error_invalid (MP_QSTR_frequency );
109110 }
110111
112+ if (i2c_m_sync_enable (& self -> i2c_desc ) != ERR_NONE ) {
113+ common_hal_busio_i2c_deinit (self );
114+ mp_raise_OSError (MP_EIO );
115+ }
116+
111117 self -> sda_pin = sda -> number ;
112118 self -> scl_pin = scl -> number ;
113119 claim_pin (sda );
114120 claim_pin (scl );
115121
116- if (i2c_m_sync_enable (& self -> i2c_desc ) != ERR_NONE ) {
117- common_hal_busio_i2c_deinit (self );
118- mp_raise_OSError (MP_EIO );
119- }
122+ // Prevent bulk sercom reset from resetting us. The finalizer will instead.
123+ never_reset_sercom (self -> i2c_desc .device .hw );
120124}
121125
122126bool common_hal_busio_i2c_deinited (busio_i2c_obj_t * self ) {
123127 return self -> sda_pin == NO_PIN ;
124128}
125129
130+ void common_hal_busio_i2c_mark_deinit (busio_i2c_obj_t * self ) {
131+ self -> sda_pin = NO_PIN ;
132+ }
133+
126134void common_hal_busio_i2c_deinit (busio_i2c_obj_t * self ) {
127135 if (common_hal_busio_i2c_deinited (self )) {
128136 return ;
@@ -133,8 +141,7 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
133141
134142 reset_pin_number (self -> sda_pin );
135143 reset_pin_number (self -> scl_pin );
136- self -> sda_pin = NO_PIN ;
137- self -> scl_pin = NO_PIN ;
144+ common_hal_busio_i2c_mark_deinit (self );
138145}
139146
140147bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
@@ -236,8 +243,6 @@ uint8_t common_hal_busio_i2c_write_read(busio_i2c_obj_t *self, uint16_t addr,
236243}
237244
238245void common_hal_busio_i2c_never_reset (busio_i2c_obj_t * self ) {
239- never_reset_sercom (self -> i2c_desc .device .hw );
240-
241246 never_reset_pin_number (self -> scl_pin );
242247 never_reset_pin_number (self -> sda_pin );
243248}
0 commit comments