From ae22305869f382c8a1cc088fbf3cb12bcc2d5a6c Mon Sep 17 00:00:00 2001 From: Hierophect Date: Wed, 8 Jan 2020 15:55:53 -0500 Subject: [PATCH] Corrections to I2C, style --- ports/stm32f4/common-hal/busio/I2C.c | 25 ++---- ports/stm32f4/common-hal/busio/SPI.c | 5 +- ports/stm32f4/common-hal/busio/UART.c | 1 + ports/stm32f4/common-hal/pulseio/PWMOut.c | 104 +++++++++++++++------- 4 files changed, 80 insertions(+), 55 deletions(-) diff --git a/ports/stm32f4/common-hal/busio/I2C.c b/ports/stm32f4/common-hal/busio/I2C.c index 46c9728a8b..37c13a9c4d 100644 --- a/ports/stm32f4/common-hal/busio/I2C.c +++ b/ports/stm32f4/common-hal/busio/I2C.c @@ -35,6 +35,7 @@ #include "supervisor/shared/translate.h" #include "common-hal/microcontroller/Pin.h" +//arrays use 0 based numbering: I2C1 is stored at index 0 #define MAX_I2C 3 STATIC bool reserved_i2c[MAX_I2C]; STATIC bool never_reset_i2c[MAX_I2C]; @@ -93,10 +94,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, } } - //Note: clock reset must be before GPIO init, due to I2C soft reboot issue - i2c_clock_enable(1<<(self->sda->i2c_index - 1)); - reserved_i2c[self->sda->i2c_index - 1] = true; - //Start GPIO for each pin GPIO_InitTypeDef GPIO_InitStruct = {0}; GPIO_InitStruct.Pin = pin_mask(sda->number); @@ -113,22 +110,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); - //still needed? - #ifdef I2C1 - __HAL_RCC_I2C1_FORCE_RESET(); - HAL_Delay(2); - __HAL_RCC_I2C1_RELEASE_RESET(); - #endif - #ifdef I2C2 - __HAL_RCC_I2C2_FORCE_RESET(); - HAL_Delay(2); - __HAL_RCC_I2C2_RELEASE_RESET(); - #endif - #ifdef I2C2 - __HAL_RCC_I2C3_FORCE_RESET(); - HAL_Delay(2); - __HAL_RCC_I2C3_RELEASE_RESET(); - #endif + //Note: clock reset must be before GPIO init, due to I2C soft reboot issue + i2c_clock_enable(1<<(self->sda->i2c_index - 1)); + reserved_i2c[self->sda->i2c_index - 1] = true; self->handle.Instance = I2Cx; self->handle.Init.ClockSpeed = 100000; @@ -139,6 +123,7 @@ 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; + self->handle.State = HAL_I2C_STATE_RESET; if(HAL_I2C_Init(&(self->handle)) != HAL_OK) { mp_raise_RuntimeError(translate("I2C Init Error")); } diff --git a/ports/stm32f4/common-hal/busio/SPI.c b/ports/stm32f4/common-hal/busio/SPI.c index 0d1bcb168e..78c6352636 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -39,13 +39,12 @@ // Note that any bugs introduced in this file can cause crashes at startup // for chips using external SPI flash. -#define MAX_SPI 6 //TODO; replace this as part of periph cleanup -#define ALL_CLOCKS 0xFF - //arrays use 0 based numbering: SPI1 is stored at index 0 +#define MAX_SPI 6 STATIC bool reserved_spi[MAX_SPI]; STATIC bool never_reset_spi[MAX_SPI]; +#define ALL_CLOCKS 0xFF STATIC void spi_clock_enable(uint8_t mask); STATIC void spi_clock_disable(uint8_t mask); diff --git a/ports/stm32f4/common-hal/busio/UART.c b/ports/stm32f4/common-hal/busio/UART.c index 44ef5efdd2..a3e2d96beb 100644 --- a/ports/stm32f4/common-hal/busio/UART.c +++ b/ports/stm32f4/common-hal/busio/UART.c @@ -40,6 +40,7 @@ #define ALL_UARTS 0xFFFF +//arrays use 0 based numbering: UART1 is stored at index 0 STATIC bool reserved_uart[MAX_UART]; int errflag; //Used to restart read halts diff --git a/ports/stm32f4/common-hal/pulseio/PWMOut.c b/ports/stm32f4/common-hal/pulseio/PWMOut.c index 2a071d0784..309848236d 100644 --- a/ports/stm32f4/common-hal/pulseio/PWMOut.c +++ b/ports/stm32f4/common-hal/pulseio/PWMOut.c @@ -44,10 +44,6 @@ STATIC bool never_reset_tim[TIM_BANK_ARRAY_LEN]; STATIC void tim_clock_enable(uint16_t mask); STATIC void tim_clock_disable(uint16_t mask); -//-------- -//STATICS -//-------- - // Get the frequency (in Hz) of the source clock for the given timer. // On STM32F405/407/415/417 there are 2 cases for how the clock freq is set. // If the APB prescaler is 1, then the timer clock is equal to its respective @@ -91,10 +87,6 @@ STATIC void timer_get_optimal_divisors(uint32_t*period, uint32_t*prescaler, } } -//-------- -//COMMON HAL -//-------- - void pwmout_reset(void) { uint16_t never_reset_mask = 0x00; for(int i=0;i