@@ -53,7 +53,6 @@ void i2c_reset(void) {
53
53
}
54
54
}
55
55
}
56
- static bool i2c_inited [I2C_NUM_MAX ];
57
56
58
57
void common_hal_busio_i2c_construct (busio_i2c_obj_t * self ,
59
58
const mcu_pin_obj_t * scl , const mcu_pin_obj_t * sda , uint32_t frequency , uint32_t timeout ) {
@@ -90,10 +89,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
90
89
#endif
91
90
92
91
93
- if (xSemaphoreCreateBinaryStatic (& self -> semaphore ) != & self -> semaphore ) {
92
+ if (xSemaphoreCreateMutexStatic (& self -> semaphore ) != & self -> semaphore ) {
94
93
mp_raise_RuntimeError (translate ("Unable to create lock" ));
95
94
}
96
- xSemaphoreGive (& self -> semaphore );
97
95
self -> sda_pin = sda ;
98
96
self -> scl_pin = scl ;
99
97
self -> i2c_num = I2C_NUM_MAX ;
@@ -106,6 +104,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
106
104
mp_raise_ValueError (translate ("All I2C peripherals are in use" ));
107
105
}
108
106
i2c_status [self -> i2c_num ] = STATUS_IN_USE ;
107
+
108
+ // Delete any previous driver.
109
+ i2c_driver_delete (self -> i2c_num );
110
+
109
111
i2c_config_t i2c_conf = {
110
112
.mode = I2C_MODE_MASTER ,
111
113
.sda_io_num = self -> sda_pin -> number ,
@@ -117,23 +119,16 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
117
119
.clk_speed = frequency ,
118
120
}
119
121
};
120
- esp_err_t result = i2c_param_config (self -> i2c_num , & i2c_conf );
121
- if (result != ESP_OK ) {
122
- mp_raise_ValueError (translate ("Invalid pins" ));
122
+ if (i2c_param_config (self -> i2c_num , & i2c_conf ) != ESP_OK ) {
123
+ mp_raise_ValueError (translate ("Invalid frequency" ));
123
124
}
124
125
125
-
126
- if (!i2c_inited [self -> i2c_num ]) {
127
- result = i2c_driver_install (self -> i2c_num ,
128
- I2C_MODE_MASTER ,
129
- 0 ,
130
- 0 ,
131
- 0 );
132
- if (result != ESP_OK ) {
133
- mp_raise_OSError (MP_EIO );
134
- }
135
- i2c_inited [self -> i2c_num ] = true;
136
-
126
+ if (i2c_driver_install (self -> i2c_num ,
127
+ I2C_MODE_MASTER ,
128
+ 0 ,
129
+ 0 ,
130
+ 0 ) != ESP_OK ) {
131
+ mp_raise_OSError (MP_EIO );
137
132
}
138
133
139
134
claim_pin (sda );
@@ -149,12 +144,14 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
149
144
return ;
150
145
}
151
146
152
- i2c_status [ self -> i2c_num ] = STATUS_FREE ;
147
+ i2c_driver_delete ( self -> i2c_num ) ;
153
148
154
149
common_hal_reset_pin (self -> sda_pin );
155
150
common_hal_reset_pin (self -> scl_pin );
156
151
self -> sda_pin = NULL ;
157
152
self -> scl_pin = NULL ;
153
+
154
+ i2c_status [self -> i2c_num ] = STATUS_FREE ;
158
155
}
159
156
160
157
bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
0 commit comments