From bd9b3a269c89ebbcd56fbbb261ea64a2dc8b9164 Mon Sep 17 00:00:00 2001 From: DipSwitch <dipswitch@ownage4u.nl> Date: Tue, 15 Mar 2016 10:49:48 +0100 Subject: [PATCH] cpu/stm32/perip/dac: Move DAC implementation to stm32_common/periph since all STM32 DAC's have the same basics --- boards/fox/include/periph_conf.h | 7 +++++ boards/iotlab-m3/include/periph_conf.h | 7 +++++ boards/limifrog-v1/include/periph_conf.h | 7 +++++ boards/nucleo-f091/include/periph_conf.h | 7 +++++ boards/nucleo-f103/include/periph_conf.h | 7 +++++ boards/nucleo-f303/include/periph_conf.h | 7 +++++ boards/nucleo-f334/include/periph_conf.h | 7 +++++ boards/nucleo-l1/include/periph_conf.h | 7 +++++ boards/spark-core/include/periph_conf.h | 7 +++++ boards/stm32f0discovery/include/periph_conf.h | 7 +++++ boards/stm32f3discovery/Makefile.features | 1 + boards/stm32f3discovery/include/periph_conf.h | 11 +++++++ boards/stm32f4discovery/include/periph_conf.h | 6 ++-- cpu/stm32_common/include/periph_cpu_common.h | 7 +++++ cpu/{stm32f4 => stm32_common}/periph/dac.c | 29 ++++++++++++++++--- cpu/stm32f0/include/periph_cpu.h | 16 +++++----- cpu/stm32f1/include/periph_cpu.h | 15 +++++----- cpu/stm32f3/include/periph_cpu.h | 9 ++++++ cpu/stm32f3/periph/gpio.c | 9 ++++++ cpu/stm32f4/include/periph_cpu.h | 7 ----- cpu/stm32l1/include/periph_cpu.h | 8 +++++ cpu/stm32l1/periph/gpio.c | 9 ++++++ 22 files changed, 168 insertions(+), 29 deletions(-) rename cpu/{stm32f4 => stm32_common}/periph/dac.c (73%) diff --git a/boards/fox/include/periph_conf.h b/boards/fox/include/periph_conf.h index 862ae18b74..a16e98617f 100644 --- a/boards/fox/include/periph_conf.h +++ b/boards/fox/include/periph_conf.h @@ -53,6 +53,13 @@ extern "C" { #define ADC_NUMOF (0) /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/iotlab-m3/include/periph_conf.h b/boards/iotlab-m3/include/periph_conf.h index 264092245a..3156013cf0 100644 --- a/boards/iotlab-m3/include/periph_conf.h +++ b/boards/iotlab-m3/include/periph_conf.h @@ -131,6 +131,13 @@ static const uart_conf_t uart_config[] = { #define SPI_0_MISO_PIN GPIO_PIN(PORT_A,6) /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @name Real time counter configuration * @{ diff --git a/boards/limifrog-v1/include/periph_conf.h b/boards/limifrog-v1/include/periph_conf.h index d4740507ad..d283e9b3fb 100644 --- a/boards/limifrog-v1/include/periph_conf.h +++ b/boards/limifrog-v1/include/periph_conf.h @@ -44,6 +44,13 @@ extern "C" { #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/nucleo-f091/include/periph_conf.h b/boards/nucleo-f091/include/periph_conf.h index 9415101da4..27a21eb2df 100644 --- a/boards/nucleo-f091/include/periph_conf.h +++ b/boards/nucleo-f091/include/periph_conf.h @@ -109,6 +109,13 @@ extern "C" { #define ADC_NUMOF (0) /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + #ifdef __cplusplus } #endif diff --git a/boards/nucleo-f103/include/periph_conf.h b/boards/nucleo-f103/include/periph_conf.h index 2d810dd840..7c26190a7a 100644 --- a/boards/nucleo-f103/include/periph_conf.h +++ b/boards/nucleo-f103/include/periph_conf.h @@ -56,6 +56,13 @@ extern "C" { #define ADC_NUMOF (0) /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/nucleo-f303/include/periph_conf.h b/boards/nucleo-f303/include/periph_conf.h index a94d1bdc70..7150197814 100755 --- a/boards/nucleo-f303/include/periph_conf.h +++ b/boards/nucleo-f303/include/periph_conf.h @@ -40,6 +40,13 @@ extern "C" { #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY_1 /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/nucleo-f334/include/periph_conf.h b/boards/nucleo-f334/include/periph_conf.h index 36179579d3..3aca8fac97 100644 --- a/boards/nucleo-f334/include/periph_conf.h +++ b/boards/nucleo-f334/include/periph_conf.h @@ -39,6 +39,13 @@ extern "C" { #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY_1 /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/nucleo-l1/include/periph_conf.h b/boards/nucleo-l1/include/periph_conf.h index f546df3894..5acca6d40c 100644 --- a/boards/nucleo-l1/include/periph_conf.h +++ b/boards/nucleo-l1/include/periph_conf.h @@ -43,6 +43,13 @@ extern "C" { #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/spark-core/include/periph_conf.h b/boards/spark-core/include/periph_conf.h index 248b23e4c3..ae57b27575 100644 --- a/boards/spark-core/include/periph_conf.h +++ b/boards/spark-core/include/periph_conf.h @@ -46,6 +46,13 @@ #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY_2 /** @} */ + /** + * @brief DAC configuration + * @{ + */ + #define DAC_NUMOF (0) + /** @} */ + /** * @name ADC configuration * @{ diff --git a/boards/stm32f0discovery/include/periph_conf.h b/boards/stm32f0discovery/include/periph_conf.h index 35cedddd3b..a686ff1fd1 100644 --- a/boards/stm32f0discovery/include/periph_conf.h +++ b/boards/stm32f0discovery/include/periph_conf.h @@ -109,6 +109,13 @@ extern "C" { #define ADC_NUMOF (6) /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_NUMOF (0) +/** @} */ + /** * @name SPI configuration * @{ diff --git a/boards/stm32f3discovery/Makefile.features b/boards/stm32f3discovery/Makefile.features index 027f7f934d..2452ed5427 100644 --- a/boards/stm32f3discovery/Makefile.features +++ b/boards/stm32f3discovery/Makefile.features @@ -1,5 +1,6 @@ # Put defined MCU peripherals here (in alphabetical order) FEATURES_PROVIDED += periph_cpuid +FEATURES_PROVIDED += periph_dac FEATURES_PROVIDED += periph_gpio FEATURES_PROVIDED += periph_i2c FEATURES_PROVIDED += periph_pwm diff --git a/boards/stm32f3discovery/include/periph_conf.h b/boards/stm32f3discovery/include/periph_conf.h index 17ff48a769..d1c5057b12 100644 --- a/boards/stm32f3discovery/include/periph_conf.h +++ b/boards/stm32f3discovery/include/periph_conf.h @@ -38,6 +38,17 @@ extern "C" { #define CLOCK_FLASH_LATENCY FLASH_ACR_LATENCY_1 /** @} */ +/** + * @brief DAC configuration + * @{ + */ +#define DAC_CONFIG { \ + { GPIO_PIN(PORT_A, 4), 0, 0 }, \ +} + +#define DAC_NUMOF (1) +/** @} */ + /** * @brief Timer configuration * @{ diff --git a/boards/stm32f4discovery/include/periph_conf.h b/boards/stm32f4discovery/include/periph_conf.h index d497874cde..264742e349 100644 --- a/boards/stm32f4discovery/include/periph_conf.h +++ b/boards/stm32f4discovery/include/periph_conf.h @@ -140,9 +140,9 @@ static const uart_conf_t uart_config[] = { * PIN, DAC channel * @{ */ -#define DAC_CONFIG { \ - {GPIO_PIN(PORT_A, 4), 0}, \ - {GPIO_PIN(PORT_A, 5), 1} \ +#define DAC_CONFIG { \ + { GPIO_PIN(PORT_A, 4), 0 }, \ + { GPIO_PIN(PORT_A, 5), 1 }, \ } #define DAC_NUMOF (2) diff --git a/cpu/stm32_common/include/periph_cpu_common.h b/cpu/stm32_common/include/periph_cpu_common.h index fc901fbe9f..e9abc3a0a5 100644 --- a/cpu/stm32_common/include/periph_cpu_common.h +++ b/cpu/stm32_common/include/periph_cpu_common.h @@ -81,6 +81,13 @@ void periph_clk_en(uint8_t bus, uint32_t mask); */ void periph_clk_dis(uint8_t bus, uint32_t mask); +/** + * @brief Configure the given pin to be used as ADC input + * + * @param[in] pin pin to configure + */ +void gpio_init_analog(gpio_t pin); + #ifdef __cplusplus } #endif diff --git a/cpu/stm32f4/periph/dac.c b/cpu/stm32_common/periph/dac.c similarity index 73% rename from cpu/stm32f4/periph/dac.c rename to cpu/stm32_common/periph/dac.c index e031a834be..d250802c93 100644 --- a/cpu/stm32f4/periph/dac.c +++ b/cpu/stm32_common/periph/dac.c @@ -8,7 +8,7 @@ */ /** - * @ingroup cpu_stm32f4 + * @ingroup cpu_stm32_common * @{ * * @file @@ -25,7 +25,13 @@ #include "periph_conf.h" /* only compile this, if the CPU has a DAC */ -#ifdef DAC +#if defined(DAC) || defined(DAC1) + +#ifdef DAC2 +#define _DAC(line) (dac_config[line].dac ? DAC2 : DAC1) +#else +#define _DAC(line) DAC +#endif /** * @brief Get the DAC configuration from the board (if configured) @@ -47,7 +53,16 @@ int8_t dac_init(dac_t line) /* configure pin */ gpio_init_analog(dac_config[line].pin); /* enable the DAC's clock */ +#if defined(DAC2) + RCC->APB1ENR |= dac_config[line].dac + ? RCC_APB1ENR_DAC2EN + : RCC_APB1ENR_DAC1EN; +#elif defined(DAC1) + RCC->APB1ENR |= RCC_APB1ENR_DAC1EN; +#else RCC->APB1ENR |= RCC_APB1ENR_DACEN; +#endif + /* reset output and enable the line's channel */ dac_set(line, 0); dac_poweron(line); @@ -57,12 +72,18 @@ int8_t dac_init(dac_t line) void dac_set(dac_t line, uint16_t value) { value = (value >> 4); /* scale to 12-bit */ +#ifdef DAC_DHR12R2_DACC2DHR if (dac_config[line].chan) { - DAC->DHR12R2 = value; + _DAC(line)->DHR12R2 = value; } else { - DAC->DHR12R1 = value; + _DAC(line)->DHR12R1 = value; } +#else + (void) line; + + _DAC(line)->DHR12R1 = value; +#endif } void dac_poweron(dac_t line) diff --git a/cpu/stm32f0/include/periph_cpu.h b/cpu/stm32f0/include/periph_cpu.h index a578f124ef..e9416e66b7 100644 --- a/cpu/stm32f0/include/periph_cpu.h +++ b/cpu/stm32f0/include/periph_cpu.h @@ -20,7 +20,6 @@ #define PERIPH_CPU_H #include "periph_cpu_common.h" -#include "cpu.h" #ifdef __cplusplus extern "C" { @@ -107,6 +106,14 @@ typedef struct { uint8_t chan; /**< internal channel the pin is connected to */ } adc_conf_t; +/** + * @brief DAC line configuration data + */ +typedef struct { + gpio_t pin; /**< pin connected to the line */ + uint8_t chan; /**< DAC device used for this line */ +} dac_conf_t; + /** * @brief Configure the alternate function for the given pin * @@ -117,13 +124,6 @@ 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); - #ifdef __cplusplus } #endif diff --git a/cpu/stm32f1/include/periph_cpu.h b/cpu/stm32f1/include/periph_cpu.h index f6c390eab1..2fd2ee2991 100644 --- a/cpu/stm32f1/include/periph_cpu.h +++ b/cpu/stm32f1/include/periph_cpu.h @@ -147,6 +147,14 @@ typedef struct { uint8_t irqn; /**< interrupt number */ } uart_conf_t; +/** + * @brief DAC line configuration data + */ +typedef struct { + gpio_t pin; /**< pin connected to the line */ + uint8_t chan; /**< DAC device used for this line */ +} dac_conf_t; + /** * @brief Configure the alternate function for the given pin * @@ -157,13 +165,6 @@ typedef struct { */ void gpio_init_af(gpio_t pin, gpio_af_out_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); - #ifdef __cplusplus } #endif diff --git a/cpu/stm32f3/include/periph_cpu.h b/cpu/stm32f3/include/periph_cpu.h index 81445818e1..109e9da631 100644 --- a/cpu/stm32f3/include/periph_cpu.h +++ b/cpu/stm32f3/include/periph_cpu.h @@ -85,6 +85,15 @@ typedef enum { GPIO_AF15 /**< use alternate function 14 */ } gpio_af_t; +/** + * @brief DAC line configuration support + */ +typedef struct { + gpio_t pin; /**< pin connected to the line */ + uint8_t dac; /**< The DAC to use, 0 or 1 */ + uint8_t chan; /**< DAC device used for this line */ +} dac_conf_t; + #ifdef __cplusplus } #endif diff --git a/cpu/stm32f3/periph/gpio.c b/cpu/stm32f3/periph/gpio.c index b13a7515c8..0559e1acf5 100644 --- a/cpu/stm32f3/periph/gpio.c +++ b/cpu/stm32f3/periph/gpio.c @@ -146,6 +146,15 @@ void gpio_init_af(gpio_t pin, gpio_af_t af) port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4)); } +void gpio_init_analog(gpio_t pin) +{ + /* enable clock, needed as this function can be used without calling + * gpio_init first */ + RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin)); + /* set to analog mode */ + _port(pin)->MODER |= (0x3 << (2 * _pin_num(pin))); +} + void gpio_irq_enable(gpio_t pin) { EXTI->IMR |= (1 << _pin_num(pin)); diff --git a/cpu/stm32f4/include/periph_cpu.h b/cpu/stm32f4/include/periph_cpu.h index 4da3d71dba..8b24176b14 100644 --- a/cpu/stm32f4/include/periph_cpu.h +++ b/cpu/stm32f4/include/periph_cpu.h @@ -163,13 +163,6 @@ 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/stm32l1/include/periph_cpu.h b/cpu/stm32l1/include/periph_cpu.h index 34beb8a95a..28b630fe3b 100644 --- a/cpu/stm32l1/include/periph_cpu.h +++ b/cpu/stm32l1/include/periph_cpu.h @@ -87,6 +87,14 @@ typedef enum { GPIO_AF14 /**< use alternate function 14 */ } gpio_af_t; +/** + * @brief DAC line configuration data + */ +typedef struct { + gpio_t pin; /**< pin connected to the line */ + uint8_t chan; /**< DAC device used for this line */ +} dac_conf_t; + /** * @brief Configure the alternate function for the given pin * diff --git a/cpu/stm32l1/periph/gpio.c b/cpu/stm32l1/periph/gpio.c index 9de5baea76..94f81a563b 100644 --- a/cpu/stm32l1/periph/gpio.c +++ b/cpu/stm32l1/periph/gpio.c @@ -148,6 +148,15 @@ void gpio_init_af(gpio_t pin, gpio_af_t af) port->AFR[(pin_num > 7) ? 1 : 0] |= (af << ((pin_num & 0x07) * 4)); } +void gpio_init_analog(gpio_t pin) +{ + /* enable clock, needed as this function can be used without calling + * gpio_init first */ + RCC->AHBENR |= (RCC_AHBENR_GPIOAEN << _port_num(pin)); + /* set to analog mode */ + _port(pin)->MODER |= (0x3 << (2 * _pin_num(pin))); +} + void gpio_irq_enable(gpio_t pin) { EXTI->IMR |= (1 << _pin_num(pin)); -- GitLab