efm32_dac.h 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Digital to Analog Converter (DAC) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 1.3.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2010 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * This source code is the property of Energy Micro AS. The source and compiled
  12. * code may only be used on Energy Micro "EFM32" microcontrollers.
  13. *
  14. * This copyright notice may not be removed from the source code nor changed.
  15. *
  16. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  17. * obligation to support this Software. Energy Micro AS is providing the
  18. * Software "AS IS", with no express or implied warranties of any kind,
  19. * including, but not limited to, any implied warranties of merchantability
  20. * or fitness for any particular purpose or warranties against infringement
  21. * of any proprietary rights of a third party.
  22. *
  23. * Energy Micro AS will not be liable for any consequential, incidental, or
  24. * special damages, or any other relief, or for any claim by any third party,
  25. * arising from your use of this Software.
  26. *
  27. ******************************************************************************/
  28. #ifndef __EFM32_DAC_H
  29. #define __EFM32_DAC_H
  30. #ifdef __cplusplus
  31. extern "C" {
  32. #endif
  33. #include <stdbool.h>
  34. #include "efm32.h"
  35. /***************************************************************************//**
  36. * @addtogroup EFM32_Library
  37. * @{
  38. ******************************************************************************/
  39. /***************************************************************************//**
  40. * @addtogroup DAC
  41. * @{
  42. ******************************************************************************/
  43. /*******************************************************************************
  44. ******************************** ENUMS ************************************
  45. ******************************************************************************/
  46. /** Conversion mode. */
  47. typedef enum
  48. {
  49. dacConvModeContinuous = _DAC_CTRL_CONVMODE_CONTINUOUS, /**< Continuous mode. */
  50. dacConvModeSampleHold = _DAC_CTRL_CONVMODE_SAMPLEHOLD, /**< Sample/hold mode. */
  51. dacConvModeSampleOff = _DAC_CTRL_CONVMODE_SAMPLEOFF /**< Sample/shut off mode. */
  52. } DAC_ConvMode_TypeDef;
  53. /**
  54. * Low pass filter cut-off frequency. Refer to datasheet for filter
  55. * characteristics.
  56. */
  57. typedef enum
  58. {
  59. dacLPFreq0 = _DAC_CTRL_LPFFREQ_FREQ0, /**< Cut-off frequency at FREQ0. */
  60. dacLPFreq1 = _DAC_CTRL_LPFFREQ_FREQ1, /**< Cut-off frequency at FREQ1. */
  61. dacLPFreq2 = _DAC_CTRL_LPFFREQ_FREQ2, /**< Cut-off frequency at FREQ2. */
  62. dacLPFreq3 = _DAC_CTRL_LPFFREQ_FREQ3 /**< Cut-off frequency at FREQ3. */
  63. } DAC_LPFreq_TypeDef;
  64. /** Output mode. */
  65. typedef enum
  66. {
  67. dacOutputDisable = _DAC_CTRL_OUTMODE_DISABLE, /**< Output to pin and ADC disabled. */
  68. dacOutputPin = _DAC_CTRL_OUTMODE_PIN, /**< Output to pin only. */
  69. dacOutputADC = _DAC_CTRL_OUTMODE_ADC, /**< Output to ADC only */
  70. dacOutputPinADC = _DAC_CTRL_OUTMODE_PINADC /**< Output to pin and ADC. */
  71. } DAC_Output_TypeDef;
  72. /** Peripheral Reflex System signal used to trigger single sample. */
  73. typedef enum
  74. {
  75. dacPRSSELCh0 = _DAC_CH0CTRL_PRSSEL_PRSCH0, /**< PRS channel 0. */
  76. dacPRSSELCh1 = _DAC_CH0CTRL_PRSSEL_PRSCH1, /**< PRS channel 1. */
  77. dacPRSSELCh2 = _DAC_CH0CTRL_PRSSEL_PRSCH2, /**< PRS channel 2. */
  78. dacPRSSELCh3 = _DAC_CH0CTRL_PRSSEL_PRSCH3, /**< PRS channel 3. */
  79. dacPRSSELCh4 = _DAC_CH0CTRL_PRSSEL_PRSCH4, /**< PRS channel 4. */
  80. dacPRSSELCh5 = _DAC_CH0CTRL_PRSSEL_PRSCH5, /**< PRS channel 5. */
  81. dacPRSSELCh6 = _DAC_CH0CTRL_PRSSEL_PRSCH6, /**< PRS channel 6. */
  82. dacPRSSELCh7 = _DAC_CH0CTRL_PRSSEL_PRSCH7 /**< PRS channel 7. */
  83. } DAC_PRSSEL_TypeDef;
  84. /** Reference voltage for DAC. */
  85. typedef enum
  86. {
  87. dacRef1V25 = _DAC_CTRL_REFSEL_1V25, /**< Internal 1.25V bandgap reference. */
  88. dacRef2V5 = _DAC_CTRL_REFSEL_2V5, /**< Internal 2.5V bandgap reference. */
  89. dacRefVDD = _DAC_CTRL_REFSEL_VDD /**< VDD reference. */
  90. } DAC_Ref_TypeDef;
  91. /** Refresh interval. */
  92. typedef enum
  93. {
  94. dacRefresh8 = _DAC_CTRL_REFRSEL_8CYCLES, /**< Refresh every 8 prescaled cycles. */
  95. dacRefresh16 = _DAC_CTRL_REFRSEL_16CYCLES, /**< Refresh every 16 prescaled cycles. */
  96. dacRefresh32 = _DAC_CTRL_REFRSEL_32CYCLES, /**< Refresh every 32 prescaled cycles. */
  97. dacRefresh64 = _DAC_CTRL_REFRSEL_64CYCLES /**< Refresh every 64 prescaled cycles. */
  98. } DAC_Refresh_TypeDef;
  99. /*******************************************************************************
  100. ******************************* STRUCTS ***********************************
  101. ******************************************************************************/
  102. /** DAC init structure, common for both channels. */
  103. typedef struct
  104. {
  105. /** Refresh interval. Only used if REFREN bit set for a DAC channel. */
  106. DAC_Refresh_TypeDef refresh;
  107. /** Low pass cut-off frequency. Only applicable if @p lpEnable true. */
  108. DAC_LPFreq_TypeDef lpFreq;
  109. /** Reference voltage to use. */
  110. DAC_Ref_TypeDef reference;
  111. /** Output mode */
  112. DAC_Output_TypeDef outMode;
  113. /** Conversion mode. */
  114. DAC_ConvMode_TypeDef convMode;
  115. /**
  116. * Prescaler used to get DAC clock. Derived as follows:
  117. * DACclk=HFPERclk/(2^prescale). The DAC clock should be <= 1MHz.
  118. */
  119. uint8_t prescale;
  120. /** Enable/disable use of low pass filter on output. */
  121. bool lpEnable;
  122. /** Enable/disable reset of prescaler on ch0 start. */
  123. bool ch0ResetPre;
  124. /** Enable/disable output enable control by CH1 PRS signal. */
  125. bool outEnablePRS;
  126. /** Enable/disable sine mode. */
  127. bool sineEnable;
  128. /** Select if single ended or differential mode. */
  129. bool diff;
  130. } DAC_Init_TypeDef;
  131. /** Default config for DAC init structure. */
  132. #define DAC_INIT_DEFAULT \
  133. { dacRefresh8, /* Refresh every 8 prescaled cycles. */ \
  134. dacLPFreq0, /* Cut-off at freq0 (if cut-off enabled). */ \
  135. dacRef1V25, /* 1.25V internal reference. */ \
  136. dacOutputPin, /* Output to pin only. */ \
  137. dacConvModeContinuous, /* Continuous mode. */ \
  138. 0, /* No prescaling. */ \
  139. false, /* Do not enable low pass filter. */ \
  140. false, /* Do not reset prescaler on ch0 start. */ \
  141. false, /* DAC output enable always on. */ \
  142. false, /* Disable sine mode. */ \
  143. false /* Single ended mode. */ \
  144. }
  145. /** DAC channel init structure. */
  146. typedef struct
  147. {
  148. /** Enable channel. */
  149. bool enable;
  150. /**
  151. * Peripheral reflex system trigger enable. If false, channel is triggered
  152. * by writing to CHnDATA.
  153. */
  154. bool prsEnable;
  155. /**
  156. * Enable/disable automatic refresh of channel. Refresh interval must be
  157. * defined in common control init, please see DAC_Init().
  158. */
  159. bool refreshEnable;
  160. /**
  161. * Peripheral reflex system trigger selection. Only applicable if @p prsEnable
  162. * is enabled.
  163. */
  164. DAC_PRSSEL_TypeDef prsSel;
  165. } DAC_InitChannel_TypeDef;
  166. /** Default config for DAC channel init structure. */
  167. #define DAC_INITCHANNEL_DEFAULT \
  168. { false, /* Leave channel disabled when init done. */ \
  169. false, /* Disable PRS triggering. */ \
  170. false, /* Channel not refreshed automatically. */ \
  171. dacPRSSELCh0 /* Select PRS ch0 (if PRS triggering enabled). */ \
  172. }
  173. /*******************************************************************************
  174. ***************************** PROTOTYPES **********************************
  175. ******************************************************************************/
  176. void DAC_Enable(DAC_TypeDef *dac, unsigned int ch, bool enable);
  177. void DAC_Init(DAC_TypeDef *dac, const DAC_Init_TypeDef *init);
  178. void DAC_InitChannel(DAC_TypeDef *dac,
  179. const DAC_InitChannel_TypeDef *init,
  180. unsigned int ch);
  181. /***************************************************************************//**
  182. * @brief
  183. * Clear one or more pending DAC interrupts.
  184. *
  185. * @param[in] dac
  186. * Pointer to DAC peripheral register block.
  187. *
  188. * @param[in] flags
  189. * Pending DAC interrupt source to clear. Use a logical OR combination of
  190. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  191. ******************************************************************************/
  192. static __INLINE void DAC_IntClear(DAC_TypeDef *dac, uint32_t flags)
  193. {
  194. dac->IFC = flags;
  195. }
  196. /***************************************************************************//**
  197. * @brief
  198. * Disable one or more DAC interrupts.
  199. *
  200. * @param[in] dac
  201. * Pointer to DAC peripheral register block.
  202. *
  203. * @param[in] flags
  204. * DAC interrupt sources to disable. Use a logical OR combination of
  205. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  206. ******************************************************************************/
  207. static __INLINE void DAC_IntDisable(DAC_TypeDef *dac, uint32_t flags)
  208. {
  209. dac->IEN &= ~(flags);
  210. }
  211. /***************************************************************************//**
  212. * @brief
  213. * Enable one or more DAC interrupts.
  214. *
  215. * @note
  216. * Depending on the use, a pending interrupt may already be set prior to
  217. * enabling the interrupt. Consider using DAC_IntClear() prior to enabling
  218. * if such a pending interrupt should be ignored.
  219. *
  220. * @param[in] dac
  221. * Pointer to DAC peripheral register block.
  222. *
  223. * @param[in] flags
  224. * DAC interrupt sources to enable. Use a logical OR combination of
  225. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  226. ******************************************************************************/
  227. static __INLINE void DAC_IntEnable(DAC_TypeDef *dac, uint32_t flags)
  228. {
  229. dac->IEN |= flags;
  230. }
  231. /***************************************************************************//**
  232. * @brief
  233. * Get pending DAC interrupt flags.
  234. *
  235. * @note
  236. * The event bits are not cleared by the use of this function.
  237. *
  238. * @param[in] dac
  239. * Pointer to DAC peripheral register block.
  240. *
  241. * @return
  242. * DAC interrupt sources pending. A logical OR combination of valid
  243. * interrupt flags for the DAC module (DAC_IF_nnn).
  244. ******************************************************************************/
  245. static __INLINE uint32_t DAC_IntGet(DAC_TypeDef *dac)
  246. {
  247. return(dac->IF);
  248. }
  249. /***************************************************************************//**
  250. * @brief
  251. * Set one or more pending DAC interrupts from SW.
  252. *
  253. * @param[in] dac
  254. * Pointer to DAC peripheral register block.
  255. *
  256. * @param[in] flags
  257. * DAC interrupt sources to set to pending. Use a logical OR combination of
  258. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  259. ******************************************************************************/
  260. static __INLINE void DAC_IntSet(DAC_TypeDef *dac, uint32_t flags)
  261. {
  262. dac->IFS = flags;
  263. }
  264. uint8_t DAC_PrescaleCalc(uint32_t dacFreq, uint32_t hfperFreq);
  265. void DAC_Reset(DAC_TypeDef *dac);
  266. /** @} (end addtogroup DAC) */
  267. /** @} (end addtogroup EFM32_Library) */
  268. #ifdef __cplusplus
  269. }
  270. #endif
  271. #endif /* __EFM32_DAC_H */