efm32_dac.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Digital to Analog Converter (DAC) peripheral API for EFM32.
  4. * @author Energy Micro AS
  5. * @version 2.0.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. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  44. /** Validation of DAC register block pointer reference for assert statements. */
  45. #define DAC_REF_VALID(ref) ((ref) == DAC0)
  46. /** @endcond */
  47. /*******************************************************************************
  48. ******************************** ENUMS ************************************
  49. ******************************************************************************/
  50. /** Conversion mode. */
  51. typedef enum
  52. {
  53. dacConvModeContinuous = _DAC_CTRL_CONVMODE_CONTINUOUS, /**< Continuous mode. */
  54. dacConvModeSampleHold = _DAC_CTRL_CONVMODE_SAMPLEHOLD, /**< Sample/hold mode. */
  55. dacConvModeSampleOff = _DAC_CTRL_CONVMODE_SAMPLEOFF /**< Sample/shut off mode. */
  56. } DAC_ConvMode_TypeDef;
  57. /** Output mode. */
  58. typedef enum
  59. {
  60. dacOutputDisable = _DAC_CTRL_OUTMODE_DISABLE, /**< Output to pin and ADC disabled. */
  61. dacOutputPin = _DAC_CTRL_OUTMODE_PIN, /**< Output to pin only. */
  62. dacOutputADC = _DAC_CTRL_OUTMODE_ADC, /**< Output to ADC only */
  63. dacOutputPinADC = _DAC_CTRL_OUTMODE_PINADC /**< Output to pin and ADC. */
  64. } DAC_Output_TypeDef;
  65. /** Peripheral Reflex System signal used to trigger single sample. */
  66. typedef enum
  67. {
  68. dacPRSSELCh0 = _DAC_CH0CTRL_PRSSEL_PRSCH0, /**< PRS channel 0. */
  69. dacPRSSELCh1 = _DAC_CH0CTRL_PRSSEL_PRSCH1, /**< PRS channel 1. */
  70. dacPRSSELCh2 = _DAC_CH0CTRL_PRSSEL_PRSCH2, /**< PRS channel 2. */
  71. dacPRSSELCh3 = _DAC_CH0CTRL_PRSSEL_PRSCH3, /**< PRS channel 3. */
  72. dacPRSSELCh4 = _DAC_CH0CTRL_PRSSEL_PRSCH4, /**< PRS channel 4. */
  73. dacPRSSELCh5 = _DAC_CH0CTRL_PRSSEL_PRSCH5, /**< PRS channel 5. */
  74. dacPRSSELCh6 = _DAC_CH0CTRL_PRSSEL_PRSCH6, /**< PRS channel 6. */
  75. dacPRSSELCh7 = _DAC_CH0CTRL_PRSSEL_PRSCH7 /**< PRS channel 7. */
  76. } DAC_PRSSEL_TypeDef;
  77. /** Reference voltage for DAC. */
  78. typedef enum
  79. {
  80. dacRef1V25 = _DAC_CTRL_REFSEL_1V25, /**< Internal 1.25V bandgap reference. */
  81. dacRef2V5 = _DAC_CTRL_REFSEL_2V5, /**< Internal 2.5V bandgap reference. */
  82. dacRefVDD = _DAC_CTRL_REFSEL_VDD /**< VDD reference. */
  83. } DAC_Ref_TypeDef;
  84. /** Refresh interval. */
  85. typedef enum
  86. {
  87. dacRefresh8 = _DAC_CTRL_REFRSEL_8CYCLES, /**< Refresh every 8 prescaled cycles. */
  88. dacRefresh16 = _DAC_CTRL_REFRSEL_16CYCLES, /**< Refresh every 16 prescaled cycles. */
  89. dacRefresh32 = _DAC_CTRL_REFRSEL_32CYCLES, /**< Refresh every 32 prescaled cycles. */
  90. dacRefresh64 = _DAC_CTRL_REFRSEL_64CYCLES /**< Refresh every 64 prescaled cycles. */
  91. } DAC_Refresh_TypeDef;
  92. /*******************************************************************************
  93. ******************************* STRUCTS ***********************************
  94. ******************************************************************************/
  95. /** DAC init structure, common for both channels. */
  96. typedef struct
  97. {
  98. /** Refresh interval. Only used if REFREN bit set for a DAC channel. */
  99. DAC_Refresh_TypeDef refresh;
  100. /** Reference voltage to use. */
  101. DAC_Ref_TypeDef reference;
  102. /** Output mode */
  103. DAC_Output_TypeDef outMode;
  104. /** Conversion mode. */
  105. DAC_ConvMode_TypeDef convMode;
  106. /**
  107. * Prescaler used to get DAC clock. Derived as follows:
  108. * DACclk=HFPERclk/(2^prescale). The DAC clock should be <= 1MHz.
  109. */
  110. uint8_t prescale;
  111. /** Enable/disable use of low pass filter on output. */
  112. bool lpEnable;
  113. /** Enable/disable reset of prescaler on ch0 start. */
  114. bool ch0ResetPre;
  115. /** Enable/disable output enable control by CH1 PRS signal. */
  116. bool outEnablePRS;
  117. /** Enable/disable sine mode. */
  118. bool sineEnable;
  119. /** Select if single ended or differential mode. */
  120. bool diff;
  121. } DAC_Init_TypeDef;
  122. /** Default config for DAC init structure. */
  123. #define DAC_INIT_DEFAULT \
  124. { dacRefresh8, /* Refresh every 8 prescaled cycles. */ \
  125. dacRef1V25, /* 1.25V internal reference. */ \
  126. dacOutputPin, /* Output to pin only. */ \
  127. dacConvModeContinuous, /* Continuous mode. */ \
  128. 0, /* No prescaling. */ \
  129. false, /* Do not enable low pass filter. */ \
  130. false, /* Do not reset prescaler on ch0 start. */ \
  131. false, /* DAC output enable always on. */ \
  132. false, /* Disable sine mode. */ \
  133. false /* Single ended mode. */ \
  134. }
  135. /** DAC channel init structure. */
  136. typedef struct
  137. {
  138. /** Enable channel. */
  139. bool enable;
  140. /**
  141. * Peripheral reflex system trigger enable. If false, channel is triggered
  142. * by writing to CHnDATA.
  143. */
  144. bool prsEnable;
  145. /**
  146. * Enable/disable automatic refresh of channel. Refresh interval must be
  147. * defined in common control init, please see DAC_Init().
  148. */
  149. bool refreshEnable;
  150. /**
  151. * Peripheral reflex system trigger selection. Only applicable if @p prsEnable
  152. * is enabled.
  153. */
  154. DAC_PRSSEL_TypeDef prsSel;
  155. } DAC_InitChannel_TypeDef;
  156. /** Default config for DAC channel init structure. */
  157. #define DAC_INITCHANNEL_DEFAULT \
  158. { false, /* Leave channel disabled when init done. */ \
  159. false, /* Disable PRS triggering. */ \
  160. false, /* Channel not refreshed automatically. */ \
  161. dacPRSSELCh0 /* Select PRS ch0 (if PRS triggering enabled). */ \
  162. }
  163. /*******************************************************************************
  164. ***************************** PROTOTYPES **********************************
  165. ******************************************************************************/
  166. void DAC_Enable(DAC_TypeDef *dac, unsigned int ch, bool enable);
  167. void DAC_Init(DAC_TypeDef *dac, const DAC_Init_TypeDef *init);
  168. void DAC_InitChannel(DAC_TypeDef *dac,
  169. const DAC_InitChannel_TypeDef *init,
  170. unsigned int ch);
  171. /***************************************************************************//**
  172. * @brief
  173. * Clear one or more pending DAC interrupts.
  174. *
  175. * @param[in] dac
  176. * Pointer to DAC peripheral register block.
  177. *
  178. * @param[in] flags
  179. * Pending DAC interrupt source to clear. Use a bitwise logic OR combination of
  180. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  181. ******************************************************************************/
  182. static __INLINE void DAC_IntClear(DAC_TypeDef *dac, uint32_t flags)
  183. {
  184. dac->IFC = flags;
  185. }
  186. /***************************************************************************//**
  187. * @brief
  188. * Disable one or more DAC interrupts.
  189. *
  190. * @param[in] dac
  191. * Pointer to DAC peripheral register block.
  192. *
  193. * @param[in] flags
  194. * DAC interrupt sources to disable. Use a bitwise logic OR combination of
  195. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  196. ******************************************************************************/
  197. static __INLINE void DAC_IntDisable(DAC_TypeDef *dac, uint32_t flags)
  198. {
  199. dac->IEN &= ~(flags);
  200. }
  201. /***************************************************************************//**
  202. * @brief
  203. * Enable one or more DAC interrupts.
  204. *
  205. * @note
  206. * Depending on the use, a pending interrupt may already be set prior to
  207. * enabling the interrupt. Consider using DAC_IntClear() prior to enabling
  208. * if such a pending interrupt should be ignored.
  209. *
  210. * @param[in] dac
  211. * Pointer to DAC peripheral register block.
  212. *
  213. * @param[in] flags
  214. * DAC interrupt sources to enable. Use a bitwise logic OR combination of
  215. * valid interrupt flags for the DAC module (DAC_IF_nnn).
  216. ******************************************************************************/
  217. static __INLINE void DAC_IntEnable(DAC_TypeDef *dac, uint32_t flags)
  218. {
  219. dac->IEN |= flags;
  220. }
  221. /***************************************************************************//**
  222. * @brief
  223. * Get pending DAC interrupt flags.
  224. *
  225. * @note
  226. * The event bits are not cleared by the use of this function.
  227. *
  228. * @param[in] dac
  229. * Pointer to DAC peripheral register block.
  230. *
  231. * @return
  232. * DAC interrupt sources pending. A bitwise logic OR combination of valid
  233. * interrupt flags for the DAC module (DAC_IF_nnn).
  234. ******************************************************************************/
  235. static __INLINE uint32_t DAC_IntGet(DAC_TypeDef *dac)
  236. {
  237. return(dac->IF);
  238. }
  239. /***************************************************************************//**
  240. * @brief
  241. * Set one or more pending DAC interrupts from SW.
  242. *
  243. * @param[in] dac
  244. * Pointer to DAC peripheral register block.
  245. *
  246. * @param[in] flags
  247. * DAC interrupt sources to set to pending. Use a bitwise logic OR combination
  248. * of valid interrupt flags for the DAC module (DAC_IF_nnn).
  249. ******************************************************************************/
  250. static __INLINE void DAC_IntSet(DAC_TypeDef *dac, uint32_t flags)
  251. {
  252. dac->IFS = flags;
  253. }
  254. uint8_t DAC_PrescaleCalc(uint32_t dacFreq, uint32_t hfperFreq);
  255. void DAC_Reset(DAC_TypeDef *dac);
  256. /** @} (end addtogroup DAC) */
  257. /** @} (end addtogroup EFM32_Library) */
  258. #ifdef __cplusplus
  259. }
  260. #endif
  261. #endif /* __EFM32_DAC_H */