2014-11-16 22:31:59 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2014 Simon Brummer
|
2016-03-14 20:10:58 +01:00
|
|
|
* 2015-2016 Freie Universität Berlin
|
2014-11-16 22:31:59 +01:00
|
|
|
*
|
2016-03-14 20:10:58 +01:00
|
|
|
* 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 details.
|
2014-11-16 22:31:59 +01:00
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
2017-10-01 21:55:59 +02:00
|
|
|
* @ingroup cpu_stm32_common
|
2017-06-27 12:45:50 +02:00
|
|
|
* @ingroup drivers_periph_dac
|
2014-11-16 22:31:59 +01:00
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @brief Low-level DAC driver implementation
|
|
|
|
*
|
2016-03-14 20:10:58 +01:00
|
|
|
* @author Simon Brummer <simon.brummer@haw-hamburg.de>
|
|
|
|
* @author Hauke Petersen <hauke.petersen@fu-berlin.de>
|
2014-11-16 22:31:59 +01:00
|
|
|
*
|
|
|
|
* @}
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include "cpu.h"
|
2017-06-27 12:45:50 +02:00
|
|
|
#include "assert.h"
|
2014-11-16 22:31:59 +01:00
|
|
|
#include "periph/dac.h"
|
|
|
|
|
2017-06-27 12:45:50 +02:00
|
|
|
/* DAC channel enable bits */
|
|
|
|
#ifdef DAC_CR_EN2
|
|
|
|
#define EN_MASK (DAC_CR_EN1 | DAC_CR_EN2)
|
2016-03-15 10:49:48 +01:00
|
|
|
#else
|
2017-06-27 12:45:50 +02:00
|
|
|
#define EN_MASK (DAC_CR_EN1)
|
2016-03-15 10:49:48 +01:00
|
|
|
#endif
|
2014-11-16 22:31:59 +01:00
|
|
|
|
2017-06-27 12:45:50 +02:00
|
|
|
/* get RCC bit */
|
|
|
|
#ifdef RCC_APB1ENR_DAC1EN
|
|
|
|
#define RCC_BIT (RCC_APB1ENR_DAC1EN)
|
|
|
|
#else
|
|
|
|
#define RCC_BIT (RCC_APB1ENR_DACEN)
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* deduct DAC device from given line channel */
|
|
|
|
static inline DAC_TypeDef *dev(dac_t line)
|
|
|
|
{
|
|
|
|
#if defined(DAC2)
|
|
|
|
return (dac_config[line].chan > 1) ? DAC2 : DAC1;
|
|
|
|
#elif defined (DAC1)
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void) line;
|
2017-06-27 12:45:50 +02:00
|
|
|
return DAC1;
|
|
|
|
#else
|
build: fix unused parameter errors
cpu, sam0_common: fix unused parameter in periph/spi
cpu, kinetis_common: fix unused parameter in periph/spi
cpu, cc2538: fix unused param in periph/i2c
cpu, cc2538: fix unused param in periph/spi
cpu, sam3: fix unused param in periph/spi
cpu, stm32_common: fix unused param in periph/pm
cpu, stm32f3: fix unused params in periph/i2c
cpu, nrf5x_common: fix unused param in periph/gpio
cpu, nrf5x_common: fix unused param in periph/spi
cpu, lpc2387: fix unused params in periph/spi
cpu, cc2538: fix unused params in radio/netdev
cpu, cc2650: fix unused params in periph/uart
cpu, lm4f120: fix unused param in periph/spi
cpu, lm4f120: fix unused params in periph/timer
cpu, lm4f120: fix unused params in periph/uart
cpu, stm32_common: fix unused params in periph/dac
cpu, stm32l0: fix unused params in periph/i2c
cpu, msp430fxyz: fix unused params in periph/uart
cpu, mips: fix unused params
cpu, cc430: fix unused-params in periph/timer
cpu, msp430fxyz: fix unused params in periph/spi
drivers, cc2420: fix unused param
cpu, mips32r2_common: fix unused params in periph/timer
cpu, cc2538: fix unused-param in periph/i2c
cpu, mips32r2_common: fix unused-param in periph/timer
cpu, msp430fxyz: fix unused params in periph/timer
cpu, atmega_common: fix unused params in periph/spi
driver, nrfmin: fix unused params
cpu, cc2538_rf: fix unused params
driver, netdev_ieee802514: fix unused param
cpu, mip_pic32m: fix unused params
cpu, lpc2387: fix unused params in periph/pwm
tests/driver_sdcard_spi: fix unused params
cpu, sam3: fix unused param in periph/pwm
tests/driver_dynamixel: fix unused params, and style issues
cpu, cc430: fix unused param in periph/rtc
cpu, atmega_common: fix unused params in periph/i2c
2017-10-31 12:09:11 +01:00
|
|
|
(void) line;
|
2017-06-27 12:45:50 +02:00
|
|
|
return DAC;
|
|
|
|
#endif
|
|
|
|
}
|
2014-11-16 22:31:59 +01:00
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
int8_t dac_init(dac_t line)
|
2014-11-16 22:31:59 +01:00
|
|
|
{
|
2016-03-14 20:10:58 +01:00
|
|
|
if (line >= DAC_NUMOF) {
|
2017-06-27 11:43:58 +02:00
|
|
|
return DAC_NOLINE;
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
/* configure pin */
|
|
|
|
gpio_init_analog(dac_config[line].pin);
|
2017-06-27 12:45:50 +02:00
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
/* reset output and enable the line's channel */
|
|
|
|
dac_poweron(line);
|
2017-06-27 12:45:50 +02:00
|
|
|
dac_set(line, 0);
|
|
|
|
|
2017-06-27 11:43:58 +02:00
|
|
|
return DAC_OK;
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
void dac_set(dac_t line, uint16_t value)
|
2014-11-16 22:31:59 +01:00
|
|
|
{
|
2017-06-27 12:45:50 +02:00
|
|
|
assert(line < DAC_NUMOF);
|
|
|
|
|
|
|
|
/* scale set value to 12-bit */
|
|
|
|
value = (value >> 4);
|
|
|
|
|
2016-03-15 10:49:48 +01:00
|
|
|
#ifdef DAC_DHR12R2_DACC2DHR
|
2017-06-27 12:45:50 +02:00
|
|
|
if (dac_config[line].chan & 0x01) {
|
|
|
|
dev(line)->DHR12R2 = value;
|
2016-03-14 20:10:58 +01:00
|
|
|
}
|
|
|
|
else {
|
2017-06-27 12:45:50 +02:00
|
|
|
dev(line)->DHR12R1 = value;
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
2016-03-15 10:49:48 +01:00
|
|
|
#else
|
2017-06-27 12:45:50 +02:00
|
|
|
dev(line)->DHR12R1 = value;
|
2016-03-15 10:49:48 +01:00
|
|
|
#endif
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
void dac_poweron(dac_t line)
|
2014-11-16 22:31:59 +01:00
|
|
|
{
|
2017-06-27 12:45:50 +02:00
|
|
|
assert(line < DAC_NUMOF);
|
|
|
|
|
|
|
|
/* enable the DAC's clock */
|
|
|
|
#if defined(DAC2)
|
|
|
|
periph_clk_en(APB1, (dac_config[line].chan > 1) ?
|
|
|
|
RCC_APB1ENR_DAC2EN : RCC_APB1ENR_DAC1EN);
|
|
|
|
#else
|
|
|
|
periph_clk_en(APB1, RCC_BIT);
|
|
|
|
#endif
|
|
|
|
|
|
|
|
/* enable corresponding DAC channel */
|
|
|
|
dev(line)->CR |= (1 << (16 * (dac_config[line].chan & 0x01)));
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
void dac_poweroff(dac_t line)
|
2014-11-16 22:31:59 +01:00
|
|
|
{
|
2017-06-27 12:45:50 +02:00
|
|
|
assert(line < DAC_NUMOF);
|
|
|
|
|
|
|
|
/* disable corresponding channel */
|
|
|
|
dev(line)->CR &= ~(1 << (16 * (dac_config[line].chan & 0x01)));
|
|
|
|
|
|
|
|
/* disable the DAC's clock in case no channel is active anymore */
|
|
|
|
if (!(dev(line)->CR & EN_MASK)) {
|
|
|
|
#if defined(DAC2)
|
|
|
|
periph_clk_dis(APB1, (dac_config[line].chan > 1) ?
|
|
|
|
RCC_APB1ENR_DAC2EN : RCC_APB1ENR_DAC1EN);
|
|
|
|
#else
|
|
|
|
periph_clk_dis(APB1, RCC_BIT);
|
|
|
|
#endif
|
|
|
|
}
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|