From bc9c8cd7a052c03301995edafde62385ec7fb8a8 Mon Sep 17 00:00:00 2001 From: Kevin O'Connor Date: Sun, 28 Jul 2019 17:55:57 -0400 Subject: [PATCH] stm32f4: Only enable peripherals once Add is_enabled_pclock() and only initialize spi and adc once during configuration. Signed-off-by: Kevin O'Connor --- src/stm32f4/adc.c | 20 +++++++++++--------- src/stm32f4/clock.c | 17 +++++++++++++++++ src/stm32f4/internal.h | 1 + src/stm32f4/spi.c | 10 ++++++---- 4 files changed, 35 insertions(+), 13 deletions(-) diff --git a/src/stm32f4/adc.c b/src/stm32f4/adc.c index 00eac8f0..07d03003 100644 --- a/src/stm32f4/adc.c +++ b/src/stm32f4/adc.c @@ -34,15 +34,17 @@ gpio_adc_setup(uint32_t pin) } // Enable the ADC - enable_pclock(ADC1_BASE); - uint32_t aticks = 3; // 56 adc cycles - ADC1->SMPR1 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9) - | (aticks << 12) | (aticks << 15) | (aticks << 18) - | (aticks << 21) | (aticks << 24)); - ADC1->SMPR2 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9) - | (aticks << 12) | (aticks << 15) | (aticks << 18) - | (aticks << 21) | (aticks << 24) | (aticks << 27)); - ADC1->CR2 = ADC_CR2_ADON; + if (!is_enabled_pclock(ADC1_BASE)) { + enable_pclock(ADC1_BASE); + uint32_t aticks = 3; // 56 adc cycles + ADC1->SMPR1 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9) + | (aticks << 12) | (aticks << 15) | (aticks << 18) + | (aticks << 21) | (aticks << 24)); + ADC1->SMPR2 = (aticks | (aticks << 3) | (aticks << 6) | (aticks << 9) + | (aticks << 12) | (aticks << 15) | (aticks << 18) + | (aticks << 21) | (aticks << 24) | (aticks << 27)); + ADC1->CR2 = ADC_CR2_ADON; + } gpio_peripheral(pin, GPIO_ANALOG, 0); diff --git a/src/stm32f4/clock.c b/src/stm32f4/clock.c index bbd58aa5..b5fd16f8 100644 --- a/src/stm32f4/clock.c +++ b/src/stm32f4/clock.c @@ -28,6 +28,23 @@ enable_pclock(uint32_t periph_base) } } +// Check if a peripheral clock has been enabled +int +is_enabled_pclock(uint32_t periph_base) +{ + if (periph_base < APB2PERIPH_BASE) { + uint32_t pos = (periph_base - APB1PERIPH_BASE) / 0x400; + return RCC->APB1ENR & (1<APB2ENR & (1<AHB1ENR & (1<