libopencm3
A free/libre/open-source firmware library for various ARM Cortex-M3 microcontrollers.
dac_common_v2.c
Go to the documentation of this file.
1/** @addtogroup dac_file DAC peripheral API
2 * @ingroup peripheral_apis
3
4@author @htmlonly &copy; @endhtmlonly 2020 Ben Brewer <ben.brewer@codethink.co.uk>
5
6LGPL License Terms @ref lgpl_license
7 */
8
9/*
10 * This file is part of the libopencm3 project.
11 *
12 * Copyright (C) 2020 Ben Brewer
13 *
14 * This library is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License as published by
16 * the Free Software Foundation, either version 3 of the License, or
17 * (at your option) any later version.
18 *
19 * This library is distributed in the hope that it will be useful,
20 * but WITHOUT ANY WARRANTY; without even the implied warranty of
21 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 * GNU Lesser General Public License for more details.
23 *
24 * You should have received a copy of the GNU Lesser General Public License
25 * along with this library. If not, see <http://www.gnu.org/licenses/>.
26 */
27
28/**@{*/
29
31
32/** @brief DAC Channel Output Buffer Enable.
33
34Enable a digital to analog converter channel output drive buffer. This is an
35optional amplifying buffer that provides additional drive for the output
36signal. The buffer is enabled by default after a reset and needs to be
37explicitly disabled if required.
38
39@param[in] dac the base address of the DAC. @ref dac_reg_base
40@param[in] channel with DAC mask. @ref dac_channel_id
41*/
42void dac_buffer_enable(uint32_t dac, int channel)
43{
44 switch (channel) {
45 case DAC_CHANNEL1:
46 DAC_MCR(dac) &= ~DAC_MCR_MODE1_UNBUFFERED;
47 break;
48 case DAC_CHANNEL2:
49 DAC_MCR(dac) &= ~DAC_MCR_MODE2_UNBUFFERED;
50 break;
54 break;
55 default:
56 break;
57 }
58}
59
60/** @brief DAC Channel Output Buffer Disable.
61
62Disable a digital to analog converter channel output drive buffer. Disabling
63this will reduce power consumption slightly and will increase the output
64impedance of the DAC. The buffers are enabled by default after a reset.
65
66@param[in] dac the base address of the DAC. @ref dac_reg_base
67@param[in] channel with DAC mask. @ref dac_channel_id
68*/
69void dac_buffer_disable(uint32_t dac, int channel)
70{
71 switch (channel) {
72 case DAC_CHANNEL1:
74 break;
75 case DAC_CHANNEL2:
77 break;
81 break;
82 default:
83 break;
84 }
85}
86
87/** @brief DAC Channel Output Mode.
88
89Each DAC channel can be configured in Normal mode or Sample and hold mode. The
90output buffer can be enabled to allow a high drive capability. Before enabling
91output buffer, the voltage offset needs to be calibrated. This calibration is
92performed at the factory (loaded after reset) and can be adjusted by software
93during application operation.
94
95@note This must be called before enabling the DAC as the settings will then
96become read-only.
97
98@param[in] dac the base address of the DAC. @ref dac_reg_base
99@param[in] mode Taken from @ref dac_mode2_sel or @ref dac_mode1_sel or
100a logical OR of one of each of these to set both channels simultaneously.
101*/
102void dac_set_mode(uint32_t dac, uint32_t mode)
103{
104 DAC_MCR(dac) |= mode;
105}
106
107/** @brief Check if DAC channel is ready to receive data.
108
109@param[in] dac the base address of the DAC. @ref dac_reg_base
110@param[in] channel with DAC mask. @ref dac_channel_id
111*/
112bool dac_is_ready(uint32_t dac, int channel)
113{
114 uint32_t mask = 0;
115 if (channel & DAC_CHANNEL1) {
116 mask |= DAC_SR_DAC1RDY;
117 }
118 if (channel & DAC_CHANNEL2) {
119 mask |= DAC_SR_DAC2RDY;
120 }
121
122 return (DAC_SR(dac) & mask) != 0;
123}
124
125/** @brief Wait until DAC channel is ready to receive data.
126
127@param[in] dac the base address of the DAC. @ref dac_reg_base
128@param[in] channel with DAC mask. @ref dac_channel_id
129*/
130void dac_wait_on_ready(uint32_t dac, int channel)
131{
132 while (!dac_is_ready(dac, channel));
133}
134
135/** @brief High frequency interface mode selection.
136
137If the AHB frequency of the DAC is above 80MHz then this value needs setting
138to an appropriate value.
139
140@param[in] dac the base address of the DAC. @ref dac_reg_base
141@param[in] hfsel uint32_t with appropriate HFSEL mask.
142*/
143void dac_set_high_frequency_mode(uint32_t dac, uint32_t hfsel)
144{
145 uint32_t reg32 = DAC_MCR(dac);
147 reg32 |= hfsel;
148 DAC_MCR(dac) = reg32;
149}
150/**@}*/
151
#define DAC_CHANNEL1
#define DAC_CHANNEL_BOTH
#define DAC_CHANNEL2
void dac_wait_on_ready(uint32_t dac, int channel)
Wait until DAC channel is ready to receive data.
void dac_set_mode(uint32_t dac, uint32_t mode)
DAC Channel Output Mode.
void dac_buffer_disable(uint32_t dac, int channel)
DAC Channel Output Buffer Disable.
Definition: dac_common_v2.c:69
bool dac_is_ready(uint32_t dac, int channel)
Check if DAC channel is ready to receive data.
void dac_buffer_enable(uint32_t dac, int channel)
DAC Channel Output Buffer Enable.
Definition: dac_common_v2.c:42
void dac_set_high_frequency_mode(uint32_t dac, uint32_t hfsel)
High frequency interface mode selection.
#define DAC_MCR_HFSEL_MASK
#define DAC_MCR_HFSEL_SHIFT
#define DAC_MCR_MODE2_UNBUFFERED
#define DAC_MCR_MODE1_UNBUFFERED
#define DAC_SR(dac)
DAC status register.
#define DAC_MCR(dac)
DAC mode control register (DAC_MCR)
Definition: dac_common_v2.h:48
#define DAC_SR_DAC2RDY
DAC channel2 ready status bit.
#define DAC_SR_DAC1RDY
DAC channel1 ready status bit.