diff --git a/cpu/stm32f2/include/periph_cpu.h b/cpu/stm32f2/include/periph_cpu.h index ed3a5b2e77..7be5c1a557 100644 --- a/cpu/stm32f2/include/periph_cpu.h +++ b/cpu/stm32f2/include/periph_cpu.h @@ -152,6 +152,11 @@ typedef struct { } uart_conf_t; /** @} */ +/** + * @brief Available number of ADC devices + */ +#define ADC_DEVS (2U) + /** * @brief ADC channel configuration data */ @@ -161,6 +166,21 @@ typedef struct { uint8_t chan; /**< CPU ADC channel connected to the pin */ } adc_conf_t; +/** + * @brief Override the ADC resolution configuration + * @{ + */ +#define HAVE_ADC_RES_T +typedef enum { + ADC_RES_6BIT = 0x03000000, /**< ADC resolution: 6 bit */ + ADC_RES_8BIT = 0x02000000, /**< ADC resolution: 8 bit */ + ADC_RES_10BIT = 0x01000000, /**< ADC resolution: 10 bit */ + ADC_RES_12BIT = 0x00000000, /**< ADC resolution: 12 bit */ + ADC_RES_14BIT = 1, /**< ADC resolution: 14 bit (not supported) */ + ADC_RES_16BIT = 2 /**< ADC resolution: 16 bit (not supported)*/ +} adc_res_t; +/** @} */ + /** * @brief DAC line configuration data */ @@ -179,6 +199,13 @@ typedef struct { */ void gpio_init_af(gpio_t pin, gpio_af_t af); +/** + * @brief Configure the given pin to be used as ADC input + * + * @param[in] pin pin to configure + */ +void gpio_init_analog(gpio_t pin); + /** * @brief Power on the DMA device the given stream belongs to * diff --git a/cpu/stm32f2/lpm_arch.c b/cpu/stm32f2/lpm_arch.c index 381cf4cd89..d16d3293e0 100644 --- a/cpu/stm32f2/lpm_arch.c +++ b/cpu/stm32f2/lpm_arch.c @@ -34,14 +34,41 @@ enum lpm_mode lpm_arch_set(enum lpm_mode target) switch (target) { case LPM_ON: /* STM Run mode */ + current_mode = LPM_ON; break; case LPM_IDLE: /* STM Sleep mode */ + current_mode = LPM_IDLE; + /* Reset SLEEPDEEP bit of system control block */ + SCB->SCR &= ~(SCB_SCR_SLEEPDEEP_Msk); + /* Enter sleep mode */ + __WFI(); break; case LPM_SLEEP: /* STM Stop mode */ + current_mode = LPM_SLEEP; + /* Clear PDDS and LPDS bits to enter stop mode on */ + /* deepsleep with voltage regulator on */ + PWR->CR &= ~(PWR_CR_PDDS | PWR_CR_LPDS); + /* Set SLEEPDEEP bit of system control block */ + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; + /* Enter stop mode */ + __WFI(); break; case LPM_POWERDOWN: /* STM Standby mode */ /* Fall-through */ case LPM_OFF: /* STM Standby mode */ + current_mode = LPM_POWERDOWN; + /* Set PDDS to enter standby mode on deepsleep and clear flags */ + PWR->CR |= (PWR_CR_PDDS | PWR_CR_CWUF | PWR_CR_CSBF); + /* Enable WKUP pin to use for wakeup from standby mode */ + PWR->CSR |= PWR_CSR_EWUP; + /* Set SLEEPDEEP bit of system control block */ + SCB->SCR |= SCB_SCR_SLEEPDEEP_Msk; +#if defined ( __CC_ARM ) + /* Ensure that store operations are completed */ + __force_stores(); +#endif + /* Enter standby mode */ + __WFI(); break; default: break; diff --git a/cpu/stm32f2/periph/adc.c b/cpu/stm32f2/periph/adc.c index de59178cbc..8025ccadf9 100644 --- a/cpu/stm32f2/periph/adc.c +++ b/cpu/stm32f2/periph/adc.c @@ -1,5 +1,5 @@ /* - * Copyright (C) 2015 Engineering-Spirit + * Copyright (C) 2016 Engineering-Spirit * * This file is subject to the terms and conditions of the GNU Lesser General * Public License v2.1. See the file LICENSE in the top level directory for more @@ -13,173 +13,126 @@ * @file * @brief Low-level ADC driver implementation * + * @author Hauke Petersen * @author Nick v. IJzendoorn * * @} */ #include "cpu.h" +#include "mutex.h" #include "periph/adc.h" #include "periph_conf.h" -/* guard in case that no ADC device is defined */ -#if ADC_NUMOF +/** + * @brief Maximum allowed ADC clock speed + */ +#define MAX_ADC_SPEED (12000000U) -typedef struct { - int max_value; -} adc_config_t; +/** + * @brief Load the ADC configuration + * @{ + */ +#ifdef ADC_CONFIG +static const adc_conf_t adc_config[] = ADC_CONFIG; +#else +static const adc_conf_t adc_config[] = {}; +#endif -adc_config_t adc_config[ADC_NUMOF]; +/** + * @brief Allocate locks for all three available ADC devices + */ +static mutex_t locks[] = { +#if ADC_DEVS > 1 + MUTEX_INIT, +#endif +#if ADC_DEVS > 2 + MUTEX_INIT, +#endif + MUTEX_INIT +}; -int adc_init(adc_t dev, adc_precision_t precision) +static inline ADC_TypeDef *dev(adc_t line) { - ADC_TypeDef *adc = 0; + return (ADC_TypeDef *)(ADC1_BASE + (adc_config[line].dev << 8)); +} - adc_poweron(dev); +static inline void prep(adc_t line) +{ + mutex_lock(&locks[adc_config[line].dev]); + RCC->APB2ENR |= (RCC_APB2ENR_ADC1EN << adc_config[line].dev); +} - switch (dev) { -#if ADC_0_EN - case ADC_0: - adc = ADC_0_DEV; - ADC_0_PORT_CLKEN(); - ADC_0_PORT->MODER |= (3 << (ADC_0_CH0_PIN * 2) | 3 << (ADC_0_CH1_PIN * 2)); - break; -#endif -#if ADC_1_EN - case ADC_1: - adc = ADC_1_DEV; - ADC_1_PORT_CLKEN(); - ADC_1_PORT->MODER |= (3 << (ADC_1_CH0_PIN * 2) | 3 << (ADC_1_CH1_PIN * 2)); - break; -#endif - default: - return -1; +static inline void done(adc_t line) +{ + RCC->APB2ENR &= ~(RCC_APB2ENR_ADC1EN << adc_config[line].dev); + mutex_unlock(&locks[adc_config[line].dev]); +} + +int adc_init(adc_t line) +{ + uint32_t clk_div = 2; + + /* check if the line is valid */ + if (line >= ADC_NUMOF) { + return -1; } - /* reset control registers */ - adc->CR1 = 0; - adc->CR2 = 0; - adc->SQR1 = 0; + /* lock and power-on the device */ + prep(line); - /* set precision */ - - switch (precision) { - case ADC_RES_6BIT: - adc->CR1 |= ADC_CR1_RES_0 | ADC_CR1_RES_1; - adc_config[dev].max_value = 0x3f; - break; - case ADC_RES_8BIT: - adc->CR1 |= ADC_CR1_RES_1; - adc_config[dev].max_value = 0xff; - break; - case ADC_RES_10BIT: - adc->CR1 |= ADC_CR1_RES_0; - adc_config[dev].max_value = 0x3ff; - break; - case ADC_RES_12BIT: - adc_config[dev].max_value = 0xfff; - break; - case ADC_RES_14BIT: - case ADC_RES_16BIT: - adc_poweroff(dev); - return -1; + /* configure the pin */ + gpio_init_analog(adc_config[line].pin); + /* set clock prescaler to get the maximal possible ADC clock value */ + for (clk_div = 2; clk_div < 8; clk_div += 2) { + if ((CLOCK_CORECLOCK / clk_div) <= MAX_ADC_SPEED) { break; + } } - - /* set clock prescaler */ - ADC->CCR = (3 << 16); /* ADC clock = 10,5MHz */ + ADC->CCR = ((clk_div / 2) - 1) << 16; /* enable the ADC module */ - adc->CR2 |= ADC_CR2_ADON; + dev(line)->CR2 = ADC_CR2_ADON; + /* check if this channel is an internal ADC channel, if so + * enable the internal temperature and Vref */ + if (adc_config[line].chan == 16 || adc_config[line].chan == 17) { + /* check if the internal channels are configured to use ADC1 */ + if (dev(line) != ADC1) { + return -3; + } + + ADC->CCR |= ADC_CCR_TSVREFE; + } + + /* free the device again */ + done(line); return 0; } -int adc_sample(adc_t dev, int channel) +int adc_sample(adc_t line, adc_res_t res) { - ADC_TypeDef *adc = 0; + int sample; - switch (dev) { -#if ADC_0_EN - case ADC_0: - adc = ADC_0_DEV; - switch (channel) { - case 0: - adc->SQR3 = ADC_0_CH0 & 0x1f; - break; - case 1: - adc->SQR3 = ADC_0_CH1 & 0x1f; - break; - default: - return -1; - } - break; -#endif -#if ADC_1_EN - case ADC_1: - adc = ADC_1_DEV; - switch (channel) { - case 0: - adc->SQR3 = ADC_1_CH0 & 0x1f; - break; - case 1: - adc->SQR3 = ADC_1_CH1 & 0x1f; - break; - default: - return -1; - } - break; -#endif + /* check if resolution is applicable */ + if (res < 0xff) { + return -1; } - /* start single conversion */ - adc->CR2 |= ADC_CR2_SWSTART; - /* wait until conversion is complete */ - while (!(adc->SR & ADC_SR_EOC)); - /* read and return result */ - return (int)adc->DR; -} + /* lock and power on the ADC device */ + prep(line); -void adc_poweron(adc_t dev) -{ - switch (dev) { -#if ADC_0_EN - case ADC_0: - ADC_0_CLKEN(); - break; -#endif -#if ADC_1_EN - case ADC_1: - ADC_1_CLKEN(); - break; -#endif - } -} + /* set resolution and conversion channel */ + dev(line)->CR1 = res; + dev(line)->SQR3 = adc_config[line].chan; + /* start conversion and wait for results */ + dev(line)->CR2 |= ADC_CR2_SWSTART; + while (!(dev(line)->SR & ADC_SR_EOC)) {} + /* finally read sample and reset the STRT bit in the status register */ + sample = (int)dev(line)->DR; -void adc_poweroff(adc_t dev) -{ - switch (dev) { -#if ADC_0_EN - case ADC_0: - ADC_0_CLKDIS(); - break; -#endif -#if ADC_1_EN - case ADC_1: - ADC_1_CLKDIS(); - break; -#endif - } -} + /* power off and unlock device again */ + done(line); -int adc_map(adc_t dev, int value, int min, int max) -{ - return (int)adc_mapf(dev, value, (float)min, (float)max); + return sample; } - -float adc_mapf(adc_t dev, int value, float min, float max) -{ - return ((max - min) / ((float)adc_config[dev].max_value)) * value; -} - -#endif /* ADC_NUMOF */