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
|
|
|
*/
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @ingroup cpu_stm32f4
|
|
|
|
* @{
|
|
|
|
*
|
|
|
|
* @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"
|
|
|
|
#include "periph/dac.h"
|
|
|
|
#include "periph_conf.h"
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
/* only compile this, if the CPU has a DAC */
|
|
|
|
#ifdef DAC
|
2014-11-16 22:31:59 +01:00
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
/**
|
|
|
|
* @brief Get the DAC configuration from the board (if configured)
|
|
|
|
* @{
|
|
|
|
*/
|
|
|
|
#ifdef DAC_CONFIG
|
|
|
|
static const dac_conf_t dac_config[] = DAC_CONFIG;
|
|
|
|
#else
|
|
|
|
static const dac_conf_t dac_config[] = {};
|
2014-11-16 22:31:59 +01:00
|
|
|
#endif
|
2016-03-14 20:10:58 +01:00
|
|
|
/** @} */
|
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) {
|
|
|
|
return -1;
|
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);
|
|
|
|
/* enable the DAC's clock */
|
|
|
|
RCC->APB1ENR |= RCC_APB1ENR_DACEN;
|
|
|
|
/* reset output and enable the line's channel */
|
|
|
|
dac_set(line, 0);
|
|
|
|
dac_poweron(line);
|
2014-11-16 22:31:59 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
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
|
|
|
{
|
2016-03-14 20:10:58 +01:00
|
|
|
value = (value >> 4); /* scale to 12-bit */
|
|
|
|
if (dac_config[line].chan) {
|
|
|
|
DAC->DHR12R2 = value;
|
|
|
|
}
|
|
|
|
else {
|
|
|
|
DAC->DHR12R1 = value;
|
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
|
|
|
{
|
2016-03-14 20:10:58 +01:00
|
|
|
DAC->CR |= (1 << (16 * dac_config[line].chan));
|
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
|
|
|
{
|
2016-03-14 20:10:58 +01:00
|
|
|
DAC->CR &= ~(1 << (16 * dac_config[line].chan));
|
2014-11-16 22:31:59 +01:00
|
|
|
}
|
|
|
|
|
2016-03-14 20:10:58 +01:00
|
|
|
#endif /* DAC */
|