From 9da5723c594f98bf7d6681d3df4178c56d3cde9f Mon Sep 17 00:00:00 2001 From: Hierophect Date: Thu, 19 Sep 2019 16:32:38 -0400 Subject: [PATCH] F412 support, cleanup --- ports/stm32f4/common-hal/busio/I2C.c | 16 +++---- .../peripherals/stm32f4/stm32f412zx/periph.c | 43 ++++++++++++++++--- .../peripherals/stm32f4/stm32f412zx/periph.h | 24 ++++++++--- 3 files changed, 62 insertions(+), 21 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/I2C.c b/ports/stm32f4/common-hal/busio/I2C.c index d16688ba64..a7112b6f72 100644 --- a/ports/stm32f4/common-hal/busio/I2C.c +++ b/ports/stm32f4/common-hal/busio/I2C.c @@ -35,6 +35,7 @@ #include "common-hal/microcontroller/Pin.h" void i2c_reset(void) { + //TODO: implement something better than eratta workaround. } void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, @@ -63,11 +64,7 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, I2Cx = mcu_i2c_banks[self->sda->i2c_index-1]; } else { mp_raise_RuntimeError(translate("Invalid I2C pin selection")); - } - - //not it - // HAL_GPIO_DeInit(pin_port(self->sda->pin->port), pin_mask(self->sda->pin->number)); - // HAL_GPIO_DeInit(pin_port(self->scl->pin->port), pin_mask(self->scl->pin->number)); + } //Start GPIO for each pin GPIO_InitTypeDef GPIO_InitStruct = {0}; @@ -85,6 +82,9 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, GPIO_InitStruct.Alternate = self->scl->altfn_index; HAL_GPIO_Init(pin_port(scl->port), &GPIO_InitStruct); + //Fix for HAL error caused by soft reboot GPIO init SDA pin voltage drop. See Eratta. + //Must be in this exact spot or I2C will get stuck in infinite loop. + //TODO: delet #ifdef I2C1 __HAL_RCC_I2C1_FORCE_RESET(); HAL_Delay(2); @@ -120,14 +120,10 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, self->handle.Init.OwnAddress2 = 0; self->handle.Init.GeneralCallMode = I2C_GENERALCALL_DISABLE; self->handle.Init.NoStretchMode = I2C_NOSTRETCH_DISABLE; - if(HAL_I2C_DeInit(&(self->handle)) != HAL_OK) { - mp_raise_RuntimeError(translate("I2C DeInit Error")); - } else { - mp_printf(&mp_plat_print, "I2C DEINIT OK\n"); - } if(HAL_I2C_Init(&(self->handle)) != HAL_OK) { mp_raise_RuntimeError(translate("I2C Init Error")); } else { + //TODO: remove post testing mp_printf(&mp_plat_print, "I2C INIT OK\n"); } claim_pin(sda); diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c index d3c4a8317b..9195e8e2e6 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c +++ b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.c @@ -34,11 +34,42 @@ //const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, SDA(PB,11), SCL(PB,10)); //const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, SDA(PB,9), SCL(PB,8)); -//WARNING: The STM32F412 has a bunch of repeated ones on AF9 so BEWARE +// I2C -const mcu_i2c_periph_obj_t periph_I2C1_0 = I2C(1, 0, &pin_PB07, &pin_PB06); -const mcu_i2c_periph_obj_t periph_I2C1_1 = I2C(1, 1, &pin_PB09, &pin_PB08); -const mcu_i2c_periph_obj_t periph_I2C2 = I2C(1, NO_ALT, &pin_PB11, &pin_PB10); //not on LQFP100 -const mcu_i2c_periph_obj_t periph_I2C3 = I2C(1, NO_ALT, &pin_PB09, &pin_PB08); +I2C_TypeDef * mcu_i2c_banks[3] = {I2C1, I2C2, I2C3}; -const mcu_i2c_periph_obj_t* mcu_i2c_list[4] = {&periph_I2C1_0,&periph_I2C1_1,&periph_I2C3}; +//SDA Pins +const mcu_i2c_sda_obj_t per_SDA_1B07 = I2C_SDA(1, 4, &pin_PB07); +const mcu_i2c_sda_obj_t per_SDA_1B09 = I2C_SDA(1, 4, &pin_PB09); +const mcu_i2c_sda_obj_t per_SDA_2B11 = I2C_SDA(2, 4, &pin_PB11); //not on LQFP100 +const mcu_i2c_sda_obj_t per_SDA_2B09 = I2C_SDA(2, 9, &pin_PB09); +const mcu_i2c_sda_obj_t per_SDA_2B03 = I2C_SDA(2, 9, &pin_PB03); +const mcu_i2c_sda_obj_t per_SDA_3C09 = I2C_SDA(3, 4, &pin_PC09); +const mcu_i2c_sda_obj_t per_SDA_3B04 = I2C_SDA(3, 9, &pin_PB04); +const mcu_i2c_sda_obj_t per_SDA_3B08 = I2C_SDA(3, 9, &pin_PB08); + +//SCL Pins +const mcu_i2c_scl_obj_t per_SCL_1B06 = I2C_SCL(1, 4, &pin_PB06); +const mcu_i2c_scl_obj_t per_SCL_1B08 = I2C_SCL(1, 4, &pin_PB08); +const mcu_i2c_scl_obj_t per_SCL_2B10 = I2C_SCL(2, 4, &pin_PB10); +const mcu_i2c_scl_obj_t per_SCL_3B08 = I2C_SCL(3, 4, &pin_PA08); + +const mcu_i2c_sda_obj_t* mcu_i2c_sda_list[8] = { + &per_SDA_1B07, + &per_SDA_1B09, + &per_SDA_2B11, + &per_SDA_2B09, + &per_SDA_2B03, + &per_SDA_3C09, + &per_SDA_3B04, + &per_SDA_3B08 +}; + +const mcu_i2c_scl_obj_t* mcu_i2c_scl_list[4] = { + &per_SCL_1B06, + &per_SCL_1B08, + &per_SCL_2B10, + &per_SCL_3B08 +}; + +//SPI, UART, Etc diff --git a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.h b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.h index 0626686196..475fe9f1ef 100644 --- a/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.h +++ b/ports/stm32f4/peripherals/stm32f4/stm32f412zx/periph.h @@ -27,11 +27,25 @@ #ifndef MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H #define MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H -extern const mcu_i2c_periph_obj_t periph_I2C1_0; -extern const mcu_i2c_periph_obj_t periph_I2C1_1; -extern const mcu_i2c_periph_obj_t periph_I2C2; -extern const mcu_i2c_periph_obj_t periph_I2C3; +//I2C +extern I2C_TypeDef * mcu_i2c_banks[3]; + +extern const mcu_i2c_sda_obj_t per_SDA_1B07; +extern const mcu_i2c_sda_obj_t per_SDA_1B09; +extern const mcu_i2c_sda_obj_t per_SDA_2B11; +extern const mcu_i2c_sda_obj_t per_SDA_2B09; +extern const mcu_i2c_sda_obj_t per_SDA_2B03; +extern const mcu_i2c_sda_obj_t per_SDA_3C09; +extern const mcu_i2c_sda_obj_t per_SDA_3B04; +extern const mcu_i2c_sda_obj_t per_SDA_3B08; + +extern const mcu_i2c_scl_obj_t per_SCL_1B06; +extern const mcu_i2c_scl_obj_t per_SCL_1B08; +extern const mcu_i2c_scl_obj_t per_SCL_2B10; +extern const mcu_i2c_scl_obj_t per_SCL_3B08; + +extern const mcu_i2c_sda_obj_t* mcu_i2c_sda_list[8]; +extern const mcu_i2c_scl_obj_t* mcu_i2c_scl_list[4]; -extern const mcu_i2c_periph_obj_t* mcu_i2c_list[4]; #endif // MICROPY_INCLUDED_STM32F4_PERIPHERALS_STM32F411VE_PERIPH_H \ No newline at end of file