diff --git a/ports/esp32s2/common-hal/busio/I2C.c b/ports/esp32s2/common-hal/busio/I2C.c index 391d7323c5..74b3896fc2 100644 --- a/ports/esp32s2/common-hal/busio/I2C.c +++ b/ports/esp32s2/common-hal/busio/I2C.c @@ -90,8 +90,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, #endif - self->semaphore_handle = xSemaphoreCreateBinaryStatic(&self->semaphore); - xSemaphoreGive(self->semaphore_handle); + if (xSemaphoreCreateBinaryStatic(&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; @@ -161,7 +163,7 @@ bool common_hal_busio_i2c_probe(busio_i2c_obj_t *self, uint8_t addr) { } bool common_hal_busio_i2c_try_lock(busio_i2c_obj_t *self) { - self->has_lock = xSemaphoreTake(self->semaphore_handle, 0) == pdTRUE; + self->has_lock = xSemaphoreTake(&self->semaphore, 0) == pdTRUE; return self->has_lock; } @@ -170,7 +172,7 @@ bool common_hal_busio_i2c_has_lock(busio_i2c_obj_t *self) { } void common_hal_busio_i2c_unlock(busio_i2c_obj_t *self) { - xSemaphoreGive(self->semaphore_handle); + xSemaphoreGive(&self->semaphore); self->has_lock = false; } diff --git a/ports/esp32s2/common-hal/busio/I2C.h b/ports/esp32s2/common-hal/busio/I2C.h index d90fb2713a..24a3fd4951 100644 --- a/ports/esp32s2/common-hal/busio/I2C.h +++ b/ports/esp32s2/common-hal/busio/I2C.h @@ -40,7 +40,6 @@ typedef struct { const mcu_pin_obj_t* sda_pin; i2c_port_t i2c_num; StaticSemaphore_t semaphore; - SemaphoreHandle_t semaphore_handle; bool has_lock; } busio_i2c_obj_t; diff --git a/ports/esp32s2/common-hal/busio/SPI.c b/ports/esp32s2/common-hal/busio/SPI.c index 73d7d0595e..a22075cd59 100644 --- a/ports/esp32s2/common-hal/busio/SPI.c +++ b/ports/esp32s2/common-hal/busio/SPI.c @@ -209,9 +209,6 @@ void common_hal_busio_spi_construct(busio_spi_obj_t *self, hal->io_mode = SPI_LL_IO_MODE_NORMAL; - // This must be set after spi_hal_init. - hal->timing_conf = &self->timing_conf; - common_hal_busio_spi_configure(self, 250000, 0, 0, 8); } @@ -261,6 +258,7 @@ bool common_hal_busio_spi_configure(busio_spi_obj_t *self, self->phase = phase; self->bits = bits; self->target_frequency = baudrate; + self->hal_context.timing_conf = &self->timing_conf; esp_err_t result = spi_hal_get_clock_conf(&self->hal_context, self->target_frequency, 128 /* duty_cycle */, @@ -315,6 +313,8 @@ bool common_hal_busio_spi_transfer(busio_spi_obj_t *self, const uint8_t *data_ou spi_hal_context_t* hal = &self->hal_context; hal->send_buffer = NULL; hal->rcv_buffer = NULL; + // Reset timing_conf in case we've moved since the last time we used it. + hal->timing_conf = &self->timing_conf; // This rounds up. size_t dma_count = (len + LLDESC_MAX_NUM_PER_DESC - 1) / LLDESC_MAX_NUM_PER_DESC; for (size_t i = 0; i < dma_count; i++) { diff --git a/ports/esp32s2/common-hal/busio/UART.c b/ports/esp32s2/common-hal/busio/UART.c index f73f5c993d..b0162adba9 100644 --- a/ports/esp32s2/common-hal/busio/UART.c +++ b/ports/esp32s2/common-hal/busio/UART.c @@ -249,6 +249,9 @@ size_t common_hal_busio_uart_read(busio_uart_obj_t *self, uint8_t *data, size_t while (supervisor_ticks_ms64() - start_ticks <= self->timeout_ms) { // Read as many chars as we can right now, up to len. size_t num_read = uart_read_bytes(self->uart_num, data, len, 0); + if (num_read < 0) { + break; + } // Advance pointer in data buffer, and decrease how many chars left to read. data += num_read;