Merge pull request #4387 from dhalbert/esp32s2-i2c-bug

Esp32-S2 I2C fixes
This commit is contained in:
Dan Halbert 2021-03-12 10:26:56 -05:00 committed by GitHub
commit bbb1a8b7fa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 18 additions and 20 deletions

View File

@ -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, 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) { 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 #endif
if (xSemaphoreCreateBinaryStatic(&self->semaphore) != &self->semaphore) { if (xSemaphoreCreateMutexStatic(&self->semaphore) != &self->semaphore) {
mp_raise_RuntimeError(translate("Unable to create lock")); mp_raise_RuntimeError(translate("Unable to create lock"));
} }
xSemaphoreGive(&self->semaphore);
self->sda_pin = sda; self->sda_pin = sda;
self->scl_pin = scl; self->scl_pin = scl;
self->i2c_num = I2C_NUM_MAX; 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")); mp_raise_ValueError(translate("All I2C peripherals are in use"));
} }
i2c_status[self->i2c_num] = STATUS_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 = { i2c_config_t i2c_conf = {
.mode = I2C_MODE_MASTER, .mode = I2C_MODE_MASTER,
.sda_io_num = self->sda_pin->number, .sda_io_num = self->sda_pin->number,
@ -117,24 +119,17 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
.clk_speed = frequency, .clk_speed = frequency,
} }
}; };
esp_err_t result = i2c_param_config(self->i2c_num, &i2c_conf); if (i2c_param_config(self->i2c_num, &i2c_conf) != ESP_OK) {
if (result != ESP_OK) { mp_raise_ValueError(translate("Invalid frequency"));
mp_raise_ValueError(translate("Invalid pins"));
} }
if (i2c_driver_install(self->i2c_num,
if (!i2c_inited[self->i2c_num]) {
result = i2c_driver_install(self->i2c_num,
I2C_MODE_MASTER, I2C_MODE_MASTER,
0, 0,
0, 0,
0); 0) != ESP_OK) {
if (result != ESP_OK) {
mp_raise_OSError(MP_EIO); mp_raise_OSError(MP_EIO);
} }
i2c_inited[self->i2c_num] = true;
}
claim_pin(sda); claim_pin(sda);
claim_pin(scl); claim_pin(scl);
@ -149,12 +144,14 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
return; 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->sda_pin);
common_hal_reset_pin(self->scl_pin); common_hal_reset_pin(self->scl_pin);
self->sda_pin = NULL; self->sda_pin = NULL;
self->scl_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) { bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) {

View File

@ -109,6 +109,7 @@ void common_hal_wifi_init(void) {
wifi_radio_obj_t* self = &common_hal_wifi_radio_obj; wifi_radio_obj_t* self = &common_hal_wifi_radio_obj;
self->netif = esp_netif_create_default_wifi_sta(); self->netif = esp_netif_create_default_wifi_sta();
self->started = false;
// Even though we just called esp_netif_create_default_wifi_sta, // Even though we just called esp_netif_create_default_wifi_sta,
// station mode isn't actually ready for use until esp_wifi_set_mode() // station mode isn't actually ready for use until esp_wifi_set_mode()

@ -1 +1 @@
Subproject commit ebe7784258d8c10e9cc334ccc00c3fd270746c8b Subproject commit f30a865fd1a44d880b909b84112f74741412c2ce