From 2f277eba3ede51f51a1279a3a34a07f94750013b Mon Sep 17 00:00:00 2001 From: gamblor21 Date: Sat, 13 Nov 2021 15:19:54 -0600 Subject: [PATCH] Cleanup and i2c fix --- shared-module/displayio/__init__.c | 30 +++++++++++++++++++++++---- shared-module/is31fl3741/is31fl3741.c | 14 +++++++++++-- shared-module/is31fl3741/is31fl3741.h | 1 + 3 files changed, 39 insertions(+), 6 deletions(-) diff --git a/shared-module/displayio/__init__.c b/shared-module/displayio/__init__.c index ca181406d7..32f6c853a1 100644 --- a/shared-module/displayio/__init__.c +++ b/shared-module/displayio/__init__.c @@ -223,11 +223,33 @@ void reset_displays(void) { #endif #if CIRCUITPY_IS31FL3741 } else if (displays[i].is31fl3741.base.type == &is31fl3741_is31fl3741_type) { - is31fl3741_is31fl3741_obj_t *pm = &displays[i].is31fl3741; - if (!any_display_uses_this_framebuffer(&pm->base)) { - common_hal_is31fl3741_is31fl3741_deinit(pm); + is31fl3741_is31fl3741_obj_t *is31 = &displays[i].is31fl3741; + if (((uint32_t)is31->i2c) < ((uint32_t)&displays) || + ((uint32_t)is31->i2c) > ((uint32_t)&displays + CIRCUITPY_DISPLAY_LIMIT)) { + busio_i2c_obj_t *original_i2c = is31->i2c; + #if BOARD_I2C + // We don't need to move original_i2c if it is the board.I2C object because it is + // statically allocated already. (Doing so would also make it impossible to reference in + // a subsequent VM run.) + if (original_i2c == common_hal_board_get_i2c()) { + continue; + } + #endif + memcpy(&is31->inline_i2c, original_i2c, sizeof(busio_i2c_obj_t)); + is31->i2c = &is31->inline_i2c; + // Check for other displays that use the same i2c bus and swap them too. + /*for (uint8_t j = i + 1; j < CIRCUITPY_DISPLAY_LIMIT; j++) { + if (displays[i].i2cdisplay_bus.base.type == &displayio_i2cdisplay_type && + displays[i].i2cdisplay_bus.bus == original_i2c) { + displays[i].i2cdisplay_bus.bus = &i2c->inline_bus; + } + }*/ + } + + if (!any_display_uses_this_framebuffer(&is31->base)) { + common_hal_is31fl3741_is31fl3741_deinit(is31); } else { - common_hal_is31fl3741_is31fl3741_set_paused(pm, true); + common_hal_is31fl3741_is31fl3741_set_paused(is31, true); } #endif #if CIRCUITPY_SHARPDISPLAY diff --git a/shared-module/is31fl3741/is31fl3741.c b/shared-module/is31fl3741/is31fl3741.c index e95537dade..9006de177e 100644 --- a/shared-module/is31fl3741/is31fl3741.c +++ b/shared-module/is31fl3741/is31fl3741.c @@ -54,6 +54,10 @@ void common_hal_is31fl3741_is31fl3741_construct(is31fl3741_is31fl3741_obj_t *sel self->i2c = i2c; self->device_address = addr; + common_hal_busio_i2c_never_reset(self->i2c); + // Our object is statically allocated off the heap so make sure the bus object lives to the end + // of the heap as well. + gc_never_free(self->i2c); common_hal_is31fl3741_is31fl3741_reconstruct(self, framebuffer); } @@ -83,14 +87,14 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s common_hal_displayio_is31fl3741_begin_transaction(self); - uint8_t command = 0xFC; + uint8_t command = 0xFC; // device ID common_hal_busio_i2c_write(self->i2c, self->device_address, &command, 1, false); uint8_t data = 0; common_hal_busio_i2c_read(self->i2c, self->device_address, &data, 1); is31fl3741_send_reset(self->i2c, self->device_address); is31fl3741_send_enable(self->i2c, self->device_address); - is31fl3741_set_current(self->i2c, self->device_address, 0x08); + is31fl3741_set_current(self->i2c, self->device_address, 0xFF); // set scale to max for all for (int i; i < 351; i++) { @@ -103,6 +107,12 @@ void common_hal_is31fl3741_is31fl3741_reconstruct(is31fl3741_is31fl3741_obj_t *s } void common_hal_is31fl3741_is31fl3741_deinit(is31fl3741_is31fl3741_obj_t *self) { + common_hal_displayio_is31fl3741_end_transaction(self); // in case we still had a lock + + if (self->i2c == &self->inline_i2c) { + common_hal_busio_i2c_deinit(self->i2c); + } + self->base.type = NULL; // If a framebuffer was passed in to the constructor, NULL the reference diff --git a/shared-module/is31fl3741/is31fl3741.h b/shared-module/is31fl3741/is31fl3741.h index f1f4be3c10..f45778e964 100644 --- a/shared-module/is31fl3741/is31fl3741.h +++ b/shared-module/is31fl3741/is31fl3741.h @@ -37,6 +37,7 @@ typedef struct { mp_buffer_info_t bufinfo; uint16_t bufsize, width, height, scale_width, scale_height; busio_i2c_obj_t *i2c; + busio_i2c_obj_t inline_i2c; uint8_t device_address; uint8_t bit_depth; bool paused;