@@ -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,34 +104,29 @@ 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 ,
112
114
.scl_io_num = self -> scl_pin -> number ,
113
- .sda_pullup_en = GPIO_PULLUP_DISABLE , /*!< Internal GPIO pull mode for I2C sda signal*/
114
- .scl_pullup_en = GPIO_PULLUP_DISABLE , /*!< Internal GPIO pull mode for I2C scl signal*/
115
115
116
116
.master = {
117
117
.clk_speed = frequency ,
118
118
}
119
119
};
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" ));
120
+ if (i2c_param_config (self -> i2c_num , & i2c_conf ) != ESP_OK ) {
121
+ mp_raise_ValueError (translate ("Invalid frequency" ));
123
122
}
124
123
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
-
124
+ if (i2c_driver_install (self -> i2c_num ,
125
+ I2C_MODE_MASTER ,
126
+ 0 ,
127
+ 0 ,
128
+ 0 ) != ESP_OK ) {
129
+ mp_raise_OSError (MP_EIO );
137
130
}
138
131
139
132
claim_pin (sda );
@@ -149,12 +142,14 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
149
142
return ;
150
143
}
151
144
152
- i2c_status [ self -> i2c_num ] = STATUS_FREE ;
145
+ i2c_driver_delete ( self -> i2c_num ) ;
153
146
154
- common_hal_reset_pin (self -> sda_pin );
155
147
common_hal_reset_pin (self -> scl_pin );
148
+ common_hal_reset_pin (self -> sda_pin );
156
149
self -> sda_pin = NULL ;
157
150
self -> scl_pin = NULL ;
151
+
152
+ i2c_status [self -> i2c_num ] = STATUS_FREE ;
158
153
}
159
154
160
155
bool common_hal_busio_i2c_probe (busio_i2c_obj_t * self , uint8_t addr ) {
0 commit comments