diff --git a/boards/fox/include/periph_conf.h b/boards/fox/include/periph_conf.h index 862ae18b74244b7b40131a28ce667f57a9fd6857..a16e98617f06a686a3281e4e28b97899501daeff 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 264092245aeb3f5e6d2463d522c6dd64e1ae3b0b..3156013cf00561950a7514dcc1f4c332b99a2426 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 d4740507ad9b92a3b47608ee7147bcc153081144..d283e9b3fb50edcb3da9539771c81ce60425f486 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 9415101da43ff2f7fe5457a00dcb354c87da8346..27a21eb2dfbcd938a82c96b5a49427bbab604e7c 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 2d810dd8402f2dbf46732b2cd99b101bb0ee779c..7c26190a7a33eeabe001a395aedd263ad00e7555 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 a94d1bdc70cb2b5fbb8ef0b1fb4c6cfe56970518..71501978141091d88076a185bfb0d021ced7238b 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 36179579d313d96a77ee4b438edec009675928fe..3aca8fac9772593aa580986477aa3e5f452ec9b0 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 f546df389422eab52becf2304e466c304790202b..5acca6d40cc9b6961a297272cca29d2040b08b24 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 248b23e4c3591940cb54a3ebaa25cdc6cb3b5a12..ae57b27575a23c92f92622c619d430155aa65ea0 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 35cedddd3b956e6abf769a166653021495187ceb..a686ff1fd129f34e153638621a81ae032b030eb8 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 027f7f934dec3b18c25b5586321df941a16900fc..2452ed5427f402835826d26395ab98d53888b8c4 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 17ff48a76910580d583e9c0008efc582e7e0218d..d1c5057b1298e67ab75e7ccaa2de573212ccf083 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 d497874cde14a75183a119bec81f34e0cc7bb6c7..264742e349a243c850c706d163278eaa0a7ea4b8 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 fc901fbe9f0dbc51d2e57a0a8e161b8f82c97218..e9abc3a0a50842addd5ac8ae978436b3bd48d296 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 e031a834be66cc49b0e33323627358d2edead994..d250802c9355aa9d0ef5bca1041ce02d838bded0 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 a578f124effb5c38d2ae0253dc8bbbc6ae807475..e9416e66b7d631bbd31140699331b55b5e9419fe 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 f6c390eab197a253d30c45a35d47278246cc2267..2fd2ee29915b53071cc815a2c7cb6701e32a7d56 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 81445818e1908b42e0ae502f2f6ec5f65b2f5fd6..109e9da631779c725852d848e7e1b0ecf7f8c630 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 b13a7515c8b14a2ab68b42851c0fae6929d99d8a..0559e1acf5195ffec37f37dda4c3f4cca252cde8 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 4da3d71dbaa3a815efc8ee3a0567828366fd0d8c..8b24176b14e6075782b7c32fc1c65b6f7b3b22ba 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 34beb8a95af8e196a3d8765fdb43c1c9a73305e0..28b630fe3b57f4919d5a6ddb66a8b43d14c36214 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 9de5baea76e8a41363e9e0383097d91fdd099c87..94f81a563b9d4fded0b3ee6ec97ba7cedaa66d25 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));