1 /***************************************************************************//**
\r
3 * @brief Digital to Analog Converter (DAC) peripheral API
\r
5 *******************************************************************************
\r
7 * <b>(C) Copyright 2015 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
33 #ifndef __SILICON_LABS_EM_DAC_H__
\r
34 #define __SILICON_LABS_EM_DAC_H__
\r
36 #include "em_device.h"
\r
38 #if defined(DAC_COUNT) && (DAC_COUNT > 0)
\r
40 #include "em_assert.h"
\r
41 #include <stdbool.h>
\r
48 /***************************************************************************//**
\r
49 * @addtogroup EM_Library
\r
51 ******************************************************************************/
\r
53 /***************************************************************************//**
\r
56 ******************************************************************************/
\r
58 /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
\r
60 /** Validation of DAC register block pointer reference for assert statements. */
\r
61 #define DAC_REF_VALID(ref) ((ref) == DAC0)
\r
65 /*******************************************************************************
\r
66 ******************************** ENUMS ************************************
\r
67 ******************************************************************************/
\r
69 /** Conversion mode. */
\r
72 dacConvModeContinuous = _DAC_CTRL_CONVMODE_CONTINUOUS, /**< Continuous mode. */
\r
73 dacConvModeSampleHold = _DAC_CTRL_CONVMODE_SAMPLEHOLD, /**< Sample/hold mode. */
\r
74 dacConvModeSampleOff = _DAC_CTRL_CONVMODE_SAMPLEOFF /**< Sample/shut off mode. */
\r
75 } DAC_ConvMode_TypeDef;
\r
80 dacOutputDisable = _DAC_CTRL_OUTMODE_DISABLE, /**< Output to pin and ADC disabled. */
\r
81 dacOutputPin = _DAC_CTRL_OUTMODE_PIN, /**< Output to pin only. */
\r
82 dacOutputADC = _DAC_CTRL_OUTMODE_ADC, /**< Output to ADC only */
\r
83 dacOutputPinADC = _DAC_CTRL_OUTMODE_PINADC /**< Output to pin and ADC. */
\r
84 } DAC_Output_TypeDef;
\r
87 /** Peripheral Reflex System signal used to trigger single sample. */
\r
90 dacPRSSELCh0 = _DAC_CH0CTRL_PRSSEL_PRSCH0, /**< PRS channel 0. */
\r
91 dacPRSSELCh1 = _DAC_CH0CTRL_PRSSEL_PRSCH1, /**< PRS channel 1. */
\r
92 dacPRSSELCh2 = _DAC_CH0CTRL_PRSSEL_PRSCH2, /**< PRS channel 2. */
\r
93 dacPRSSELCh3 = _DAC_CH0CTRL_PRSSEL_PRSCH3, /**< PRS channel 3. */
\r
94 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH4 )
\r
95 dacPRSSELCh4 = _DAC_CH0CTRL_PRSSEL_PRSCH4, /**< PRS channel 4. */
\r
97 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH5 )
\r
98 dacPRSSELCh5 = _DAC_CH0CTRL_PRSSEL_PRSCH5, /**< PRS channel 5. */
\r
100 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH6 )
\r
101 dacPRSSELCh6 = _DAC_CH0CTRL_PRSSEL_PRSCH6, /**< PRS channel 6. */
\r
103 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH7 )
\r
104 dacPRSSELCh7 = _DAC_CH0CTRL_PRSSEL_PRSCH7, /**< PRS channel 7. */
\r
106 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH8 )
\r
107 dacPRSSELCh8 = _DAC_CH0CTRL_PRSSEL_PRSCH8, /**< PRS channel 8. */
\r
109 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH9 )
\r
110 dacPRSSELCh9 = _DAC_CH0CTRL_PRSSEL_PRSCH9, /**< PRS channel 9. */
\r
112 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH10 )
\r
113 dacPRSSELCh10 = _DAC_CH0CTRL_PRSSEL_PRSCH10, /**< PRS channel 10. */
\r
115 #if defined( _DAC_CH0CTRL_PRSSEL_PRSCH11 )
\r
116 dacPRSSELCh11 = _DAC_CH0CTRL_PRSSEL_PRSCH11, /**< PRS channel 11. */
\r
118 } DAC_PRSSEL_TypeDef;
\r
121 /** Reference voltage for DAC. */
\r
124 dacRef1V25 = _DAC_CTRL_REFSEL_1V25, /**< Internal 1.25V bandgap reference. */
\r
125 dacRef2V5 = _DAC_CTRL_REFSEL_2V5, /**< Internal 2.5V bandgap reference. */
\r
126 dacRefVDD = _DAC_CTRL_REFSEL_VDD /**< VDD reference. */
\r
130 /** Refresh interval. */
\r
133 dacRefresh8 = _DAC_CTRL_REFRSEL_8CYCLES, /**< Refresh every 8 prescaled cycles. */
\r
134 dacRefresh16 = _DAC_CTRL_REFRSEL_16CYCLES, /**< Refresh every 16 prescaled cycles. */
\r
135 dacRefresh32 = _DAC_CTRL_REFRSEL_32CYCLES, /**< Refresh every 32 prescaled cycles. */
\r
136 dacRefresh64 = _DAC_CTRL_REFRSEL_64CYCLES /**< Refresh every 64 prescaled cycles. */
\r
137 } DAC_Refresh_TypeDef;
\r
140 /*******************************************************************************
\r
141 ******************************* STRUCTS ***********************************
\r
142 ******************************************************************************/
\r
144 /** DAC init structure, common for both channels. */
\r
147 /** Refresh interval. Only used if REFREN bit set for a DAC channel. */
\r
148 DAC_Refresh_TypeDef refresh;
\r
150 /** Reference voltage to use. */
\r
151 DAC_Ref_TypeDef reference;
\r
154 DAC_Output_TypeDef outMode;
\r
156 /** Conversion mode. */
\r
157 DAC_ConvMode_TypeDef convMode;
\r
160 * Prescaler used to get DAC clock. Derived as follows:
\r
161 * DACclk=HFPERclk/(2^prescale). The DAC clock should be <= 1MHz.
\r
165 /** Enable/disable use of low pass filter on output. */
\r
168 /** Enable/disable reset of prescaler on ch0 start. */
\r
171 /** Enable/disable output enable control by CH1 PRS signal. */
\r
174 /** Enable/disable sine mode. */
\r
177 /** Select if single ended or differential mode. */
\r
179 } DAC_Init_TypeDef;
\r
181 /** Default config for DAC init structure. */
\r
182 #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
225 false, /* Leave channel disabled when init done. */ \
\r
226 false, /* Disable PRS triggering. */ \
\r
227 false, /* Channel not refreshed automatically. */ \
\r
228 dacPRSSELCh0 /* Select PRS ch0 (if PRS triggering enabled). */ \
\r
232 /*******************************************************************************
\r
233 ***************************** PROTOTYPES **********************************
\r
234 ******************************************************************************/
\r
236 void DAC_Enable(DAC_TypeDef *dac, unsigned int ch, bool enable);
\r
237 void DAC_Init(DAC_TypeDef *dac, const DAC_Init_TypeDef *init);
\r
238 void DAC_InitChannel(DAC_TypeDef *dac,
\r
239 const DAC_InitChannel_TypeDef *init,
\r
241 void DAC_ChannelOutputSet(DAC_TypeDef *dac,
\r
242 unsigned int channel,
\r
245 /***************************************************************************//**
\r
247 * Set the output signal of DAC channel 0 to a given value.
\r
250 * This function sets the output signal of DAC channel 0 by writing @p value
\r
251 * to the CH0DATA register.
\r
254 * Pointer to DAC peripheral register block.
\r
257 * Value to write to the channel 0 output register CH0DATA.
\r
258 ******************************************************************************/
\r
259 __STATIC_INLINE void DAC_Channel0OutputSet( DAC_TypeDef *dac,
\r
262 EFM_ASSERT(value<=_DAC_CH0DATA_MASK);
\r
263 dac->CH0DATA = value;
\r
267 /***************************************************************************//**
\r
269 * Set the output signal of DAC channel 1 to a given value.
\r
272 * This function sets the output signal of DAC channel 1 by writing @p value
\r
273 * to the CH1DATA register.
\r
276 * Pointer to DAC peripheral register block.
\r
279 * Value to write to the channel 1 output register CH1DATA.
\r
280 ******************************************************************************/
\r
281 __STATIC_INLINE void DAC_Channel1OutputSet( DAC_TypeDef *dac,
\r
284 EFM_ASSERT(value<=_DAC_CH1DATA_MASK);
\r
285 dac->CH1DATA = value;
\r
289 /***************************************************************************//**
\r
291 * Clear one or more pending DAC interrupts.
\r
294 * Pointer to DAC peripheral register block.
\r
297 * Pending DAC interrupt source to clear. Use a bitwise logic OR combination of
\r
298 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
299 ******************************************************************************/
\r
300 __STATIC_INLINE void DAC_IntClear(DAC_TypeDef *dac, uint32_t flags)
\r
306 /***************************************************************************//**
\r
308 * Disable one or more DAC interrupts.
\r
311 * Pointer to DAC peripheral register block.
\r
314 * DAC interrupt sources to disable. Use a bitwise logic OR combination of
\r
315 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
316 ******************************************************************************/
\r
317 __STATIC_INLINE void DAC_IntDisable(DAC_TypeDef *dac, uint32_t flags)
\r
319 dac->IEN &= ~flags;
\r
323 /***************************************************************************//**
\r
325 * Enable one or more DAC interrupts.
\r
328 * Depending on the use, a pending interrupt may already be set prior to
\r
329 * enabling the interrupt. Consider using DAC_IntClear() prior to enabling
\r
330 * if such a pending interrupt should be ignored.
\r
333 * Pointer to DAC peripheral register block.
\r
336 * DAC interrupt sources to enable. Use a bitwise logic OR combination of
\r
337 * valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
338 ******************************************************************************/
\r
339 __STATIC_INLINE void DAC_IntEnable(DAC_TypeDef *dac, uint32_t flags)
\r
345 /***************************************************************************//**
\r
347 * Get pending DAC interrupt flags.
\r
350 * The event bits are not cleared by the use of this function.
\r
353 * Pointer to DAC peripheral register block.
\r
356 * DAC interrupt sources pending. A bitwise logic OR combination of valid
\r
357 * interrupt flags for the DAC module (DAC_IF_nnn).
\r
358 ******************************************************************************/
\r
359 __STATIC_INLINE uint32_t DAC_IntGet(DAC_TypeDef *dac)
\r
365 /***************************************************************************//**
\r
367 * Get enabled and pending DAC interrupt flags.
\r
368 * Useful for handling more interrupt sources in the same interrupt handler.
\r
371 * Pointer to DAC peripheral register block.
\r
374 * Interrupt flags are not cleared by the use of this function.
\r
377 * Pending and enabled DAC interrupt sources.
\r
378 * The return value is the bitwise AND combination of
\r
379 * - the OR combination of enabled interrupt sources in DACx_IEN_nnn
\r
380 * register (DACx_IEN_nnn) and
\r
381 * - the OR combination of valid interrupt flags of the DAC module
\r
383 ******************************************************************************/
\r
384 __STATIC_INLINE uint32_t DAC_IntGetEnabled(DAC_TypeDef *dac)
\r
388 /* Store DAC->IEN in temporary variable in order to define explicit order
\r
389 * of volatile accesses. */
\r
392 /* Bitwise AND of pending and enabled interrupts */
\r
393 return dac->IF & ien;
\r
397 /***************************************************************************//**
\r
399 * Set one or more pending DAC interrupts from SW.
\r
402 * Pointer to DAC peripheral register block.
\r
405 * DAC interrupt sources to set to pending. Use a bitwise logic OR combination
\r
406 * of valid interrupt flags for the DAC module (DAC_IF_nnn).
\r
407 ******************************************************************************/
\r
408 __STATIC_INLINE void DAC_IntSet(DAC_TypeDef *dac, uint32_t flags)
\r
413 uint8_t DAC_PrescaleCalc(uint32_t dacFreq, uint32_t hfperFreq);
\r
414 void DAC_Reset(DAC_TypeDef *dac);
\r
416 /** @} (end addtogroup DAC) */
\r
417 /** @} (end addtogroup EM_Library) */
\r
423 #endif /* defined(DAC_COUNT) && (DAC_COUNT > 0) */
\r
424 #endif /* __SILICON_LABS_EM_DAC_H__ */
\r