From 012b3bdf1869114a383baeef29300b588c234d81 Mon Sep 17 00:00:00 2001 From: Hierophect Date: Tue, 3 Dec 2019 16:18:49 -0500 Subject: [PATCH] Add I2C never reset, SPI bugfix --- ports/stm32f4/common-hal/busio/I2C.c | 25 +++++++++++++++++++++---- ports/stm32f4/common-hal/busio/SPI.c | 3 ++- 2 files changed, 23 insertions(+), 5 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/I2C.c b/ports/stm32f4/common-hal/busio/I2C.c index c2f5dbe0a2..d2292845ea 100644 --- a/ports/stm32f4/common-hal/busio/I2C.c +++ b/ports/stm32f4/common-hal/busio/I2C.c @@ -36,6 +36,7 @@ #include "common-hal/microcontroller/Pin.h" STATIC bool reserved_i2c[3]; +STATIC bool never_reset[3]; void i2c_reset(void) { //Note: I2Cs are also forcibly reset in construct, due to silicon error @@ -48,11 +49,24 @@ void i2c_reset(void) { __HAL_RCC_I2C2_CLK_DISABLE(); #endif #ifdef I2C3 - reserved_i2c[3] = false; + reserved_i2c[2] = false; __HAL_RCC_I2C3_CLK_DISABLE(); #endif } +void common_hal_busio_i2c_never_reset(busio_i2c_obj_t *self) { + for (size_t i = 0 ; i < MP_ARRAY_SIZE(mcu_i2c_banks); i++) { + if (self->handle.Instance == mcu_i2c_banks[i]) { + never_reset[i] = true; + + never_reset_pin_number(self->scl->pin->port, self->scl->pin->number); + never_reset_pin_number(self->sda->pin->port, self->scl->pin->number); + break; + } + } +} + + 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) { @@ -166,19 +180,22 @@ void common_hal_busio_i2c_deinit(busio_i2c_obj_t *self) { } #ifdef I2C1 if(self->handle.Instance==I2C1) { - reserved_i2c[0] = 0; + never_reset[0] = false; + reserved_i2c[0] = false; __HAL_RCC_I2C1_CLK_DISABLE(); } #endif #ifdef I2C2 if(self->handle.Instance==I2C2) { - reserved_i2c[1] = 0; + never_reset[1] = false; + reserved_i2c[1] = false; __HAL_RCC_I2C2_CLK_DISABLE(); } #endif #ifdef I2C3 if(self->handle.Instance==I2C3) { - reserved_i2c[3] = 0; + never_reset[2] = false; + reserved_i2c[2] = false; __HAL_RCC_I2C3_CLK_DISABLE(); } #endif diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index b5517f470f..1142657b7f 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -191,7 +191,8 @@ bool common_hal_busio_spi_deinited(busio_spi_obj_t *self) { void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { spi_clock_disable(1<<(self->sck->spi_index - 1)); - reserved_spi[self->sck->spi_index - 1] = true; + reserved_spi[self->sck->spi_index - 1] = false; + never_reset_spi[self->sck->spi_index - 1] = false; reset_pin_number(self->sck->pin->port,self->sck->pin->number); reset_pin_number(self->mosi->pin->port,self->mosi->pin->number);