restore install/delete for i2c driver

This commit is contained in:
Dan Halbert 2021-03-07 18:23:14 -05:00
parent 7ab75fa398
commit 26eb429cc6
2 changed files with 18 additions and 22 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,34 +104,29 @@ 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,
.scl_io_num = self->scl_pin->number, .scl_io_num = self->scl_pin->number,
.sda_pullup_en = GPIO_PULLUP_DISABLE, /*!< Internal GPIO pull mode for I2C sda signal*/
.scl_pullup_en = GPIO_PULLUP_DISABLE, /*!< Internal GPIO pull mode for I2C scl signal*/
.master = { .master = {
.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]) { I2C_MODE_MASTER,
result = i2c_driver_install(self->i2c_num, 0,
I2C_MODE_MASTER, 0,
0, 0) != ESP_OK) {
0, mp_raise_OSError(MP_EIO);
0);
if (result != ESP_OK) {
mp_raise_OSError(MP_EIO);
}
i2c_inited[self->i2c_num] = true;
} }
claim_pin(sda); claim_pin(sda);
@ -149,12 +142,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->scl_pin); common_hal_reset_pin(self->scl_pin);
common_hal_reset_pin(self->sda_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()