dac.c
1.49 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
/*
* Copyright (C) 2015 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
* details.
*/
/**
* @ingroup cpu_stm32f2
* @{
*
* @file
* @brief Low-level DAC driver implementation
*
* @author Nick v. IJzendoorn <nijzendoorn@engineering-spirit.nl>
*
* @}
*/
#include "cpu.h"
#include "periph/dac.h"
#include "periph_conf.h"
/* guard in case that no DAC device is defined */
#if DAC_NUMOF
/**
* @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[] = {};
#endif
/** @} */
int8_t dac_init(dac_t line)
{
if (line >= DAC_NUMOF) {
return -1;
}
/* configure pin */
gpio_init_analog(dac_config[line].pin);
/* enable the DAC's clock */
periph_clk_en(APB1, RCC_APB1ENR_DACEN);
/* reset output and enable the line's channel */
dac_set(line, 0);
dac_poweron(line);
return 0;
}
void dac_set(dac_t line, uint16_t value)
{
value = (value >> 4); /* scale to 12-bit */
if (dac_config[line].chan) {
DAC->DHR12R2 = value;
}
else {
DAC->DHR12R1 = value;
}
}
void dac_poweron(dac_t line)
{
DAC->CR |= (1 << (16 * dac_config[line].chan));
}
void dac_poweroff(dac_t line)
{
DAC->CR &= ~(1 << (16 * dac_config[line].chan));
}
#endif /* DAC_NUMOF */