diff options
| author | Hierophect <hierophect@gmail.com> | 2020-01-08 13:03:25 -0500 |
|---|---|---|
| committer | Hierophect <hierophect@gmail.com> | 2020-01-08 13:03:25 -0500 |
| commit | 90613aa5fb97344004bd59601dde927d7804702b (patch) | |
| tree | bb2864241f167d3fc0a1e4249c849de5ba9b5eea | |
| parent | b0f08ba465f2585d03e445b920a86ceda1a20ea5 (diff) | |
more cleanup and style syncing
| -rw-r--r-- | ports/stm32f4/common-hal/busio/I2C.c | 78 | ||||
| -rw-r--r-- | ports/stm32f4/common-hal/busio/SPI.c | 62 | ||||
| -rw-r--r-- | ports/stm32f4/common-hal/busio/UART.c | 22 | ||||
| -rw-r--r-- | ports/stm32f4/common-hal/pulseio/PWMOut.c | 46 |
4 files changed, 130 insertions, 78 deletions
diff --git a/ports/stm32f4/common-hal/busio/I2C.c b/ports/stm32f4/common-hal/busio/I2C.c index dc82c19a0..4aff3b822 100644 --- a/ports/stm32f4/common-hal/busio/I2C.c +++ b/ports/stm32f4/common-hal/busio/I2C.c @@ -35,35 +35,28 @@ #include "supervisor/shared/translate.h" #include "common-hal/microcontroller/Pin.h" -STATIC bool reserved_i2c[3]; -STATIC bool never_reset[3]; +#define MAX_I2C 3 +STATIC bool reserved_i2c[MAX_I2C]; +STATIC bool never_reset_i2c[MAX_I2C]; -void i2c_reset(void) { - //Note: I2Cs are also forcibly reset in construct, due to silicon error - #ifdef I2C1 - reserved_i2c[0] = false; - __HAL_RCC_I2C1_CLK_DISABLE(); - #endif - #ifdef I2C2 - reserved_i2c[1] = false; - __HAL_RCC_I2C2_CLK_DISABLE(); - #endif - #ifdef I2C3 - reserved_i2c[2] = false; - __HAL_RCC_I2C3_CLK_DISABLE(); - #endif -} +#define ALL_CLOCKS 0xFF +STATIC void i2c_clock_enable(uint8_t mask); +STATIC void i2c_clock_disable(uint8_t mask); -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; +//-------- +//COMMON HAL +//-------- - 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 i2c_reset(void) { + uint16_t never_reset_mask = 0x00; + for(int i=0;i<MAX_I2C;i++) { + if (!never_reset_i2c[i]) { + reserved_i2c[i] = false; + } else { + never_reset_mask |= 1<<i; } } + spi_clock_disable(ALL_CLOCKS & ~(never_reset_mask)); } void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, @@ -85,7 +78,6 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, i2c_taken = true; continue; } - self->scl = &mcu_i2c_scl_list[j]; self->sda = &mcu_i2c_sda_list[i]; break; @@ -176,6 +168,18 @@ void common_hal_busio_i2c_construct(busio_i2c_obj_t *self, claim_pin(scl); } +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; + } + } +} + bool common_hal_busio_i2c_deinited(busio_i2c_obj_t *self) { return self->sda->pin == mp_const_none; } @@ -252,3 +256,27 @@ uint8_t common_hal_busio_i2c_read(busio_i2c_obj_t *self, uint16_t addr, uint8_t *data, size_t len) { return HAL_I2C_Master_Receive(&(self->handle), (uint16_t)(addr<<1), data, (uint16_t)len, 500) == HAL_OK ? 0 : MP_EIO; } + +STATIC void i2c_clock_enable(uint8_t mask) { + #ifdef I2C1 + if (mask & 1<<0) __HAL_RCC_I2C1_CLK_ENABLE(); + #endif + #ifdef I2C2 + if (mask & 1<<1) __HAL_RCC_I2C2_CLK_ENABLE(); + #endif + #ifdef I2C3 + if (mask & 1<<2) __HAL_RCC_I2C3_CLK_ENABLE(); + #endif +} + +STATIC void i2c_clock_disable(uint8_t mask) { + #ifdef I2C1 + if (mask & 1<<0) __HAL_RCC_I2C1_CLK_DISABLE(); + #endif + #ifdef I2C2 + if (mask & 1<<1) __HAL_RCC_I2C2_CLK_DISABLE(); + #endif + #ifdef I2C3 + if (mask & 1<<2) __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 32089887f..09d382bc0 100644 --- a/ports/stm32f4/common-hal/busio/SPI.c +++ b/ports/stm32f4/common-hal/busio/SPI.c @@ -46,6 +46,10 @@ STATIC bool never_reset_spi[MAX_SPI]; STATIC void spi_clock_enable(uint8_t mask); STATIC void spi_clock_disable(uint8_t mask); +//-------- +//STATICS +//-------- + STATIC uint32_t get_busclock(SPI_TypeDef * instance) { //SPI2 and 3 are on PCLK1, if they exist. #ifdef SPI2 @@ -57,11 +61,41 @@ STATIC uint32_t get_busclock(SPI_TypeDef * instance) { return HAL_RCC_GetPCLK2Freq(); } +STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, uint32_t busclock) { + static const uint32_t baud_map[8][2] = { + {2,SPI_BAUDRATEPRESCALER_2}, + {4,SPI_BAUDRATEPRESCALER_4}, + {8,SPI_BAUDRATEPRESCALER_8}, + {16,SPI_BAUDRATEPRESCALER_16}, + {32,SPI_BAUDRATEPRESCALER_32}, + {64,SPI_BAUDRATEPRESCALER_64}, + {128,SPI_BAUDRATEPRESCALER_128}, + {256,SPI_BAUDRATEPRESCALER_256} + }; + size_t i = 0; + uint16_t divisor; + do { + divisor = baud_map[i][0]; + if (baudrate >= (busclock/divisor)) { + *prescaler = divisor; + return baud_map[i][1]; + } + i++; + } while (divisor != 256); + //only gets here if requested baud is lower than minimum + *prescaler = 256; + return SPI_BAUDRATEPRESCALER_256; +} + +//-------- +//COMMON HAL +//-------- + void spi_reset(void) { uint16_t never_reset_mask = 0x00; for(int i=0;i<MAX_SPI;i++) { if (!never_reset_spi[i]) { - reserved_spi[i] = 0x00; + reserved_spi[i] = false; } else { never_reset_mask |= 1<<i; } @@ -202,32 +236,6 @@ void common_hal_busio_spi_deinit(busio_spi_obj_t *self) { self->miso = mp_const_none; } -STATIC uint32_t stm32_baud_to_spi_div(uint32_t baudrate, uint16_t * prescaler, uint32_t busclock) { - static const uint32_t baud_map[8][2] = { - {2,SPI_BAUDRATEPRESCALER_2}, - {4,SPI_BAUDRATEPRESCALER_4}, - {8,SPI_BAUDRATEPRESCALER_8}, - {16,SPI_BAUDRATEPRESCALER_16}, - {32,SPI_BAUDRATEPRESCALER_32}, - {64,SPI_BAUDRATEPRESCALER_64}, - {128,SPI_BAUDRATEPRESCALER_128}, - {256,SPI_BAUDRATEPRESCALER_256} - }; - size_t i = 0; - uint16_t divisor; - do { - divisor = baud_map[i][0]; - if (baudrate >= (busclock/divisor)) { - *prescaler = divisor; - return baud_map[i][1]; - } - i++; - } while (divisor != 256); - //only gets here if requested baud is lower than minimum - *prescaler = 256; - return SPI_BAUDRATEPRESCALER_256; -} - bool common_hal_busio_spi_configure(busio_spi_obj_t *self, uint32_t baudrate, uint8_t polarity, uint8_t phase, uint8_t bits) { //This resets the SPI, so check before updating it redundantly diff --git a/ports/stm32f4/common-hal/busio/UART.c b/ports/stm32f4/common-hal/busio/UART.c index aa792dae1..71867ac63 100644 --- a/ports/stm32f4/common-hal/busio/UART.c +++ b/ports/stm32f4/common-hal/busio/UART.c @@ -47,13 +47,9 @@ STATIC void uart_clock_enable(uint16_t mask); STATIC void uart_clock_disable(uint16_t mask); STATIC void uart_assign_irq(busio_uart_obj_t* self, USART_TypeDef* USARTx); -void uart_reset(void) { - for (uint8_t i = 0; i < MAX_UART; i++) { - reserved_uart[i] = false; - MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL; - } - uart_clock_disable(ALL_UARTS); -} +//-------- +//STATICS +//-------- STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eval, int uart_index, bool uart_taken) { @@ -70,6 +66,18 @@ STATIC USART_TypeDef * assign_uart_or_throw(busio_uart_obj_t* self, bool pin_eva } } +//-------- +//COMMON HAL +//-------- + +void uart_reset(void) { + for (uint8_t i = 0; i < MAX_UART; i++) { + reserved_uart[i] = false; + MP_STATE_PORT(cpy_uart_obj_all)[i] = NULL; + } + uart_clock_disable(ALL_UARTS); +} + void common_hal_busio_uart_construct(busio_uart_obj_t* self, const mcu_pin_obj_t* tx, const mcu_pin_obj_t* rx, uint32_t baudrate, uint8_t bits, uart_parity_t parity, uint8_t stop, mp_float_t timeout, diff --git a/ports/stm32f4/common-hal/pulseio/PWMOut.c b/ports/stm32f4/common-hal/pulseio/PWMOut.c index 16b1b5b89..2a071d078 100644 --- a/ports/stm32f4/common-hal/pulseio/PWMOut.c +++ b/ports/stm32f4/common-hal/pulseio/PWMOut.c @@ -44,6 +44,10 @@ 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 @@ -87,6 +91,10 @@ 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<TIM_BANK_ARRAY_LEN;i++) { @@ -100,25 +108,6 @@ void pwmout_reset(void) { tim_clock_disable(ALL_CLOCKS & ~(never_reset_mask)); } -void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { - for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) { - if (mcu_tim_banks[i] == self->handle.Instance) { - never_reset_tim[i] = true; - never_reset_pin_number(self->tim->pin->port, self->tim->pin->number); - break; - } - } -} - -void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { - for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) { - if (mcu_tim_banks[i] == self->handle.Instance) { - never_reset_tim[i] = false; - break; - } - } -} - pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, const mcu_pin_obj_t* pin, uint16_t duty, @@ -241,6 +230,25 @@ pwmout_result_t common_hal_pulseio_pwmout_construct(pulseio_pwmout_obj_t* self, return PWMOUT_OK; } +void common_hal_pulseio_pwmout_never_reset(pulseio_pwmout_obj_t *self) { + for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) { + if (mcu_tim_banks[i] == self->handle.Instance) { + never_reset_tim[i] = true; + never_reset_pin_number(self->tim->pin->port, self->tim->pin->number); + break; + } + } +} + +void common_hal_pulseio_pwmout_reset_ok(pulseio_pwmout_obj_t *self) { + for(size_t i = 0 ; i < TIM_BANK_ARRAY_LEN; i++) { + if (mcu_tim_banks[i] == self->handle.Instance) { + never_reset_tim[i] = false; + break; + } + } +} + bool common_hal_pulseio_pwmout_deinited(pulseio_pwmout_obj_t* self) { return self->tim == mp_const_none; } |
