diff --git a/ports/esp32s2/common-hal/busio/I2C.c b/ports/esp32s2/common-hal/busio/I2C.c index 8372eb8c3053..a1d0b8f9c4f9 100644 --- a/ports/esp32s2/common-hal/busio/I2C.c +++ b/ports/esp32s2/common-hal/busio/I2C.c @@ -53,7 +53,6 @@ void i2c_reset(void) { } } } -static bool i2c_inited[I2C_NUM_MAX]; void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, 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, #endif - if (xSemaphoreCreateBinaryStatic(&self->semaphore) != &self->semaphore) { + if (xSemaphoreCreateMutexStatic(&self->semaphore) != &self->semaphore) { mp_raise_RuntimeError(translate("Unable to create lock")); } - xSemaphoreGive(&self->semaphore); self->sda_pin = sda; self->scl_pin = scl; self->i2c_num = I2C_NUM_MAX; @@ -106,6 +104,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, mp_raise_ValueError(translate("All I2C peripherals are in use")); } i2c_status[self->i2c_num] = STATUS_IN_USE; + + // Delete any previous driver. + i2c_driver_delete(self->i2c_num); + i2c_config_t i2c_conf = { .mode = I2C_MODE_MASTER, .sda_io_num = self->sda_pin->number, @@ -117,23 +119,16 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, .clk_speed = frequency, } }; - esp_err_t result = i2c_param_config(self->i2c_num, &i2c_conf); - if (result != ESP_OK) { - mp_raise_ValueError(translate("Invalid pins")); + if (i2c_param_config(self->i2c_num, &i2c_conf) != ESP_OK) { + mp_raise_ValueError(translate("Invalid frequency")); } - - if (!i2c_inited[self->i2c_num]) { - result = i2c_driver_install(self->i2c_num, - I2C_MODE_MASTER, - 0, - 0, - 0); - if (result != ESP_OK) { - mp_raise_OSError(MP_EIO); - } - i2c_inited[self->i2c_num] = true; - + if (i2c_driver_install(self->i2c_num, + I2C_MODE_MASTER, + 0, + 0, + 0) != ESP_OK) { + mp_raise_OSError(MP_EIO); } claim_pin(sda); @@ -149,12 +144,14 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { return; } - i2c_status[self->i2c_num] = STATUS_FREE; + i2c_driver_delete(self->i2c_num); common_hal_reset_pin(self->sda_pin); common_hal_reset_pin(self->scl_pin); self->sda_pin = NULL; self->scl_pin = NULL; + + i2c_status[self->i2c_num] = STATUS_FREE; } bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { diff --git a/ports/esp32s2/common-hal/wifi/__init__.c b/ports/esp32s2/common-hal/wifi/__init__.c index b8e8f731bd81..a3927b7c3e11 100644 --- a/ports/esp32s2/common-hal/wifi/__init__.c +++ b/ports/esp32s2/common-hal/wifi/__init__.c @@ -109,6 +109,7 @@ void common_hal_wifi_init(void) { wifi_radio_obj_t* self = &common_hal_wifi_radio_obj; self->netif = esp_netif_create_default_wifi_sta(); + self->started = false; // Even though we just called esp_netif_create_default_wifi_sta, // station mode isn't actually ready for use until esp_wifi_set_mode() diff --git a/ports/esp32s2/esp-idf b/ports/esp32s2/esp-idf index ebe7784258d8..f30a865fd1a4 160000 --- a/ports/esp32s2/esp-idf +++ b/ports/esp32s2/esp-idf @@ -1 +1 @@ -Subproject commit ebe7784258d8c10e9cc334ccc00c3fd270746c8b +Subproject commit f30a865fd1a44d880b909b84112f74741412c2ce