Working, tested with two i2c busses

This commit is contained in:
Bruce Segal 2020-12-07 17:57:54 -08:00
parent 22ad76bb48
commit 571c063c2a

View File

@ -49,11 +49,11 @@ void never_reset_i2c(i2c_port_t num) {
void i2c_reset(void) { void i2c_reset(void) {
for (i2c_port_t num = 0; num < I2C_NUM_MAX; num++) { for (i2c_port_t num = 0; num < I2C_NUM_MAX; num++) {
if (i2c_status[num] == STATUS_IN_USE) { if (i2c_status[num] == STATUS_IN_USE) {
i2c_driver_delete(num);
i2c_status[num] = STATUS_FREE; i2c_status[num] = STATUS_FREE;
} }
} }
} }
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) {
@ -121,6 +121,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
if (result != ESP_OK) { if (result != ESP_OK) {
mp_raise_ValueError(translate("Invalid pins")); mp_raise_ValueError(translate("Invalid pins"));
} }
if (!i2c_inited[self->i2c_num]) {
result = i2c_driver_install(self->i2c_num, result = i2c_driver_install(self->i2c_num,
I2C_MODE_MASTER, I2C_MODE_MASTER,
0, 0,
@ -129,6 +132,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self,
if (result != 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);
@ -143,7 +149,6 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) {
return; return;
} }
i2c_driver_delete(self->i2c_num);
i2c_status[self->i2c_num] = STATUS_FREE; i2c_status[self->i2c_num] = STATUS_FREE;
common_hal_reset_pin(self->sda_pin); common_hal_reset_pin(self->sda_pin);