1 /***************************************************************************//**
\r
3 * @brief Digital to Analog Converter (DAC) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2014 Silicon Labs, http://www.silabs.com</b>
\r
8 *******************************************************************************
\r
10 * Permission is granted to anyone to use this software for any purpose,
\r
11 * including commercial applications, and to alter it and redistribute it
\r
12 * freely, subject to the following restrictions:
\r
14 * 1. The origin of this software must not be misrepresented; you must not
\r
15 * claim that you wrote the original software.
\r
16 * 2. Altered source versions must be plainly marked as such, and must not be
\r
17 * misrepresented as being the original software.
\r
18 * 3. This notice may not be removed or altered from any source distribution.
\r
20 * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Silicon Labs has no
\r
21 * obligation to support this Software. Silicon Labs is providing the
\r
22 * Software "AS IS", with no express or implied warranties of any kind,
\r
23 * including, but not limited to, any implied warranties of merchantability
\r
24 * or fitness for any particular purpose or warranties against infringement
\r
25 * of any proprietary rights of a third party.
\r
27 * Silicon Labs will not be liable for any consequential, incidental, or
\r
28 * special damages, or any other relief, or for any claim by any third party,
\r
29 * arising from your use of this Software.
\r
31 ******************************************************************************/
\r
37 #include "em_device.h"
\r
38 #include "em_assert.h"
\r
40 #if defined(DAC_COUNT) && (DAC_COUNT > 0)
\r
42 #include <stdbool.h>
\r
49 /***************************************************************************//**
\r
50 * @addtogroup EM_Library
\r
52 ******************************************************************************/
\r
54 /***************************************************************************//**
\r
57 ******************************************************************************/
\r
59 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
61 /** Validation of DAC register block pointer reference for assert statements. */
\r
62 #define DAC_REF_VALID(ref) ((ref) == DAC0)
\r
66 /*******************************************************************************
\r
67 ******************************** ENUMS ************************************
\r
68 ******************************************************************************/
\r
70 /** Conversion mode. */
\r
73 dacConvModeContinuous = _DAC_CTRL_CONVMODE_CONTINUOUS, /**< Continuous mode. */
\r
74 dacConvModeSampleHold = _DAC_CTRL_CONVMODE_SAMPLEHOLD, /**< Sample/hold mode. */
\r
75 dacConvModeSampleOff = _DAC_CTRL_CONVMODE_SAMPLEOFF /**< Sample/shut off mode. */
\r
76 } DAC_ConvMode_TypeDef;
\r
81 dacOutputDisable = _DAC_CTRL_OUTMODE_DISABLE, /**< Output to pin and ADC disabled. */
\r
82 dacOutputPin = _DAC_CTRL_OUTMODE_PIN, /**< Output to pin only. */
\r
83 dacOutputADC = _DAC_CTRL_OUTMODE_ADC, /**< Output to ADC only */
\r
84 dacOutputPinADC = _DAC_CTRL_OUTMODE_PINADC /**< Output to pin and ADC. */
\r
85 } DAC_Output_TypeDef;
\r
88 /** Peripheral Reflex System signal used to trigger single sample. */
\r
91 dacPRSSELCh0 = _DAC_CH0CTRL_PRSSEL_PRSCH0, /**< PRS channel 0. */
\r
92 dacPRSSELCh1 = _DAC_CH0CTRL_PRSSEL_PRSCH1, /**< PRS channel 1. */
\r
93 dacPRSSELCh2 = _DAC_CH0CTRL_PRSSEL_PRSCH2, /**< PRS channel 2. */
\r
94 dacPRSSELCh3 = _DAC_CH0CTRL_PRSSEL_PRSCH3, /**< PRS channel 3. */
\r
95 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH4 )
\r
96 dacPRSSELCh4 = _DAC_CH0CTRL_PRSSEL_PRSCH4, /**< PRS channel 4. */
\r
98 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH5 )
\r
99 dacPRSSELCh5 = _DAC_CH0CTRL_PRSSEL_PRSCH5, /**< PRS channel 5. */
\r
101 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH6 )
\r
102 dacPRSSELCh6 = _DAC_CH0CTRL_PRSSEL_PRSCH6, /**< PRS channel 6. */
\r
104 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH7 )
\r
105 dacPRSSELCh7 = _DAC_CH0CTRL_PRSSEL_PRSCH7, /**< PRS channel 7. */
\r
107 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH8 )
\r
108 dacPRSSELCh8 = _DAC_CH0CTRL_PRSSEL_PRSCH8, /**< PRS channel 8. */
\r
110 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH9 )
\r
111 dacPRSSELCh9 = _DAC_CH0CTRL_PRSSEL_PRSCH9, /**< PRS channel 9. */
\r
113 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH10 )
\r
114 dacPRSSELCh10 = _DAC_CH0CTRL_PRSSEL_PRSCH10, /**< PRS channel 10. */
\r
116 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH11 )
\r
117 dacPRSSELCh11 = _DAC_CH0CTRL_PRSSEL_PRSCH11, /**< PRS channel 11. */
\r
119 } DAC_PRSSEL_TypeDef;
\r
122 /** Reference voltage for DAC. */
\r
125 dacRef1V25 = _DAC_CTRL_REFSEL_1V25, /**< Internal 1.25V bandgap reference. */
\r
126 dacRef2V5 = _DAC_CTRL_REFSEL_2V5, /**< Internal 2.5V bandgap reference. */
\r
127 dacRefVDD = _DAC_CTRL_REFSEL_VDD /**< VDD reference. */
\r
131 /** Refresh interval. */
\r
134 dacRefresh8 = _DAC_CTRL_REFRSEL_8CYCLES, /**< Refresh every 8 prescaled cycles. */
\r
135 dacRefresh16 = _DAC_CTRL_REFRSEL_16CYCLES, /**< Refresh every 16 prescaled cycles. */
\r
136 dacRefresh32 = _DAC_CTRL_REFRSEL_32CYCLES, /**< Refresh every 32 prescaled cycles. */
\r
137 dacRefresh64 = _DAC_CTRL_REFRSEL_64CYCLES /**< Refresh every 64 prescaled cycles. */
\r
138 } DAC_Refresh_TypeDef;
\r
141 /*******************************************************************************
\r
142 ******************************* STRUCTS ***********************************
\r
143 ******************************************************************************/
\r
145 /** DAC init structure, common for both channels. */
\r
148 /** Refresh interval. Only used if REFREN bit set for a DAC channel. */
\r
149 DAC_Refresh_TypeDef refresh;
\r
151 /** Reference voltage to use. */
\r
152 DAC_Ref_TypeDef reference;
\r
155 DAC_Output_TypeDef outMode;
\r
157 /** Conversion mode. */
\r
158 DAC_ConvMode_TypeDef convMode;
\r
161 * Prescaler used to get DAC clock. Derived as follows:
\r
162 * DACclk=HFPERclk/(2^prescale). The DAC clock should be <= 1MHz.
\r
166 /** Enable/disable use of low pass filter on output. */
\r
169 /** Enable/disable reset of prescaler on ch0 start. */
\r
172 /** Enable/disable output enable control by CH1 PRS signal. */
\r
175 /** Enable/disable sine mode. */
\r
178 /** Select if single ended or differential mode. */
\r
180 } DAC_Init_TypeDef;
\r
182 /** Default config for DAC init structure. */
\r
183 #define DAC_INIT_DEFAULT \
\r
184 { dacRefresh8, /* Refresh every 8 prescaled cycles. */ \
\r
185 dacRef1V25, /* 1.25V internal reference. */ \
\r
186 dacOutputPin, /* Output to pin only. */ \
\r
187 dacConvModeContinuous, /* Continuous mode. */ \
\r
188 0, /* No prescaling. */ \
\r
189 false, /* Do not enable low pass filter. */ \
\r
190 false, /* Do not reset prescaler on ch0 start. */ \
\r
191 false, /* DAC output enable always on. */ \
\r
192 false, /* Disable sine mode. */ \
\r
193 false /* Single ended mode. */ \
\r
197 /** DAC channel init structure. */
\r
200 /** Enable channel. */
\r
204 * Peripheral reflex system trigger enable. If false, channel is triggered
\r
205 * by writing to CHnDATA.
\r
210 * Enable/disable automatic refresh of channel. Refresh interval must be
\r
211 * defined in common control init, please see DAC_Init().
\r
213 bool refreshEnable;
\r
216 * Peripheral reflex system trigger selection. Only applicable if @p prsEnable
\r
219 DAC_PRSSEL_TypeDef prsSel;
\r
220 } DAC_InitChannel_TypeDef;
\r
222 /** Default config for DAC channel init structure. */
\r
223 #define DAC_INITCHANNEL_DEFAULT \
\r
224 { false, /* Leave channel disabled when init done. */ \
\r
225 false, /* Disable PRS triggering. */ \
\r
226 false, /* Channel not refreshed automatically. */ \
\r
227 dacPRSSELCh0 /* Select PRS ch0 (if PRS triggering enabled). */ \
\r
231 /*******************************************************************************
\r
232 ***************************** PROTOTYPES **********************************
\r
233 ******************************************************************************/
\r
235 void DAC_Enable(DAC_TypeDef *dac, unsigned int ch, bool enable);
\r
236 void DAC_Init(DAC_TypeDef *dac, const DAC_Init_TypeDef *init);
\r
237 void DAC_InitChannel(DAC_TypeDef *dac,
\r
238 const DAC_InitChannel_TypeDef *init,
\r
240 void DAC_ChannelOutputSet(DAC_TypeDef *dac,
\r
241 unsigned int channel,
\r
244 /***************************************************************************//**
\r
246 * Set the output signal of DAC channel 0 to a given value.
\r
249 * This function sets the output signal of DAC channel 0 by writing @p value
\r
250 * to the CH0DATA register.
\r
253 * Pointer to DAC peripheral register block.
\r
256 * Value to write to the channel 0 output register CH0DATA.
\r
257 ******************************************************************************/
\r
258 __STATIC_INLINE void DAC_Channel0OutputSet( DAC_TypeDef *dac,
\r
261 EFM_ASSERT(value<=_DAC_CH0DATA_MASK);
\r
262 dac->CH0DATA = value;
\r
266 /***************************************************************************//**
\r
268 * Set the output signal of DAC channel 1 to a given value.
\r
271 * This function sets the output signal of DAC channel 1 by writing @p value
\r
272 * to the CH1DATA register.
\r
275 * Pointer to DAC peripheral register block.
\r
278 * Value to write to the channel 1 output register CH1DATA.
\r
279 ******************************************************************************/
\r
280 __STATIC_INLINE void DAC_Channel1OutputSet( DAC_TypeDef *dac,
\r
283 EFM_ASSERT(value<=_DAC_CH1DATA_MASK);
\r
284 dac->CH1DATA = value;
\r
288 /***************************************************************************//**
\r
290 * Clear one or more pending DAC interrupts.
\r
293 * Pointer to DAC peripheral register block.
\r
296 * Pending DAC interrupt source to clear. Use a bitwise logic OR combination of
\r
297 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
298 ******************************************************************************/
\r
299 __STATIC_INLINE void DAC_IntClear(DAC_TypeDef *dac, uint32_t flags)
\r
305 /***************************************************************************//**
\r
307 * Disable one or more DAC interrupts.
\r
310 * Pointer to DAC peripheral register block.
\r
313 * DAC interrupt sources to disable. Use a bitwise logic OR combination of
\r
314 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
315 ******************************************************************************/
\r
316 __STATIC_INLINE void DAC_IntDisable(DAC_TypeDef *dac, uint32_t flags)
\r
318 dac->IEN &= ~(flags);
\r
322 /***************************************************************************//**
\r
324 * Enable one or more DAC interrupts.
\r
327 * Depending on the use, a pending interrupt may already be set prior to
\r
328 * enabling the interrupt. Consider using DAC_IntClear() prior to enabling
\r
329 * if such a pending interrupt should be ignored.
\r
332 * Pointer to DAC peripheral register block.
\r
335 * DAC interrupt sources to enable. Use a bitwise logic OR combination of
\r
336 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
337 ******************************************************************************/
\r
338 __STATIC_INLINE void DAC_IntEnable(DAC_TypeDef *dac, uint32_t flags)
\r
344 /***************************************************************************//**
\r
346 * Get pending DAC interrupt flags.
\r
349 * The event bits are not cleared by the use of this function.
\r
352 * Pointer to DAC peripheral register block.
\r
355 * DAC interrupt sources pending. A bitwise logic OR combination of valid
\r
356 * interrupt flags for the DAC module (DAC_IF_nnn).
\r
357 ******************************************************************************/
\r
358 __STATIC_INLINE uint32_t DAC_IntGet(DAC_TypeDef *dac)
\r
364 /***************************************************************************//**
\r
366 * Set one or more pending DAC interrupts from SW.
\r
369 * Pointer to DAC peripheral register block.
\r
372 * DAC interrupt sources to set to pending. Use a bitwise logic OR combination
\r
373 * of valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
374 ******************************************************************************/
\r
375 __STATIC_INLINE void DAC_IntSet(DAC_TypeDef *dac, uint32_t flags)
\r
380 uint8_t DAC_PrescaleCalc(uint32_t dacFreq, uint32_t hfperFreq);
\r
381 void DAC_Reset(DAC_TypeDef *dac);
\r
383 /** @} (end addtogroup DAC) */
\r
384 /** @} (end addtogroup EM_Library) */
\r
390 #endif /* defined(DAC_COUNT) && (DAC_COUNT > 0) */
\r
392 #endif /* __EM_DAC_H */
\r