em_dac.c 8.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. /***************************************************************************//**
  2. * @file
  3. * @brief Digital to Analog Coversion (DAC) Peripheral API
  4. * @author Energy Micro AS
  5. * @version 3.0.0
  6. *******************************************************************************
  7. * @section License
  8. * <b>(C) Copyright 2012 Energy Micro AS, http://www.energymicro.com</b>
  9. *******************************************************************************
  10. *
  11. * Permission is granted to anyone to use this software for any purpose,
  12. * including commercial applications, and to alter it and redistribute it
  13. * freely, subject to the following restrictions:
  14. *
  15. * 1. The origin of this software must not be misrepresented; you must not
  16. * claim that you wrote the original software.
  17. * 2. Altered source versions must be plainly marked as such, and must not be
  18. * misrepresented as being the original software.
  19. * 3. This notice may not be removed or altered from any source distribution.
  20. *
  21. * DISCLAIMER OF WARRANTY/LIMITATION OF REMEDIES: Energy Micro AS has no
  22. * obligation to support this Software. Energy Micro AS is providing the
  23. * Software "AS IS", with no express or implied warranties of any kind,
  24. * including, but not limited to, any implied warranties of merchantability
  25. * or fitness for any particular purpose or warranties against infringement
  26. * of any proprietary rights of a third party.
  27. *
  28. * Energy Micro AS will not be liable for any consequential, incidental, or
  29. * special damages, or any other relief, or for any claim by any third party,
  30. * arising from your use of this Software.
  31. *
  32. ******************************************************************************/
  33. #include "em_dac.h"
  34. #include "em_cmu.h"
  35. #include "em_assert.h"
  36. #include "em_bitband.h"
  37. /***************************************************************************//**
  38. * @addtogroup EM_Library
  39. * @{
  40. ******************************************************************************/
  41. /***************************************************************************//**
  42. * @addtogroup DAC
  43. * @brief Digital to Analog Coversion (DAC) Peripheral API
  44. * @{
  45. ******************************************************************************/
  46. /*******************************************************************************
  47. ******************************* DEFINES ***********************************
  48. ******************************************************************************/
  49. /** @cond DO_NOT_INCLUDE_WITH_DOXYGEN */
  50. /** Validation of DAC channel for assert statements. */
  51. #define DAC_CH_VALID(ch) ((ch) <= 1)
  52. /** Max DAC clock */
  53. #define DAC_MAX_CLOCK 1000000
  54. /** @endcond */
  55. /*******************************************************************************
  56. ************************** GLOBAL FUNCTIONS *******************************
  57. ******************************************************************************/
  58. /***************************************************************************//**
  59. * @brief
  60. * Enable/disable DAC channel.
  61. *
  62. * @param[in] dac
  63. * Pointer to DAC peripheral register block.
  64. *
  65. * @param[in] ch
  66. * Channel to enable/disable.
  67. *
  68. * @param[in] enable
  69. * true to enable DAC channel, false to disable.
  70. ******************************************************************************/
  71. void DAC_Enable(DAC_TypeDef *dac, unsigned int ch, bool enable)
  72. {
  73. volatile uint32_t *reg;
  74. EFM_ASSERT(DAC_REF_VALID(dac));
  75. EFM_ASSERT(DAC_CH_VALID(ch));
  76. if (!ch)
  77. {
  78. reg = &(dac->CH0CTRL);
  79. }
  80. else
  81. {
  82. reg = &(dac->CH1CTRL);
  83. }
  84. BITBAND_Peripheral(reg, _DAC_CH0CTRL_EN_SHIFT, (unsigned int)enable);
  85. }
  86. /***************************************************************************//**
  87. * @brief
  88. * Initialize DAC.
  89. *
  90. * @details
  91. * Initializes common parts for both channels. In addition, channel control
  92. * configuration must be done, please refer to DAC_InitChannel().
  93. *
  94. * @note
  95. * This function will disable both channels prior to configuration.
  96. *
  97. * @param[in] dac
  98. * Pointer to DAC peripheral register block.
  99. *
  100. * @param[in] init
  101. * Pointer to DAC initialization structure.
  102. ******************************************************************************/
  103. void DAC_Init(DAC_TypeDef *dac, const DAC_Init_TypeDef *init)
  104. {
  105. uint32_t tmp;
  106. EFM_ASSERT(DAC_REF_VALID(dac));
  107. /* Make sure both channels are disabled. */
  108. BITBAND_Peripheral(&(dac->CH0CTRL), _DAC_CH0CTRL_EN_SHIFT, 0);
  109. BITBAND_Peripheral(&(dac->CH1CTRL), _DAC_CH0CTRL_EN_SHIFT, 0);
  110. /* Load proper calibration data depending on selected reference */
  111. switch (init->reference)
  112. {
  113. case dacRef2V5:
  114. dac->CAL = DEVINFO->DAC0CAL1;
  115. break;
  116. case dacRefVDD:
  117. dac->CAL = DEVINFO->DAC0CAL2;
  118. break;
  119. default: /* 1.25V */
  120. dac->CAL = DEVINFO->DAC0CAL0;
  121. break;
  122. }
  123. tmp = ((uint32_t)(init->refresh) << _DAC_CTRL_REFRSEL_SHIFT) |
  124. (((uint32_t)(init->prescale) << _DAC_CTRL_PRESC_SHIFT) & _DAC_CTRL_PRESC_MASK) |
  125. ((uint32_t)(init->reference) << _DAC_CTRL_REFSEL_SHIFT) |
  126. ((uint32_t)(init->outMode) << _DAC_CTRL_OUTMODE_SHIFT) |
  127. ((uint32_t)(init->convMode) << _DAC_CTRL_CONVMODE_SHIFT);
  128. if (init->ch0ResetPre)
  129. {
  130. tmp |= DAC_CTRL_CH0PRESCRST;
  131. }
  132. if (init->outEnablePRS)
  133. {
  134. tmp |= DAC_CTRL_OUTENPRS;
  135. }
  136. if (init->sineEnable)
  137. {
  138. tmp |= DAC_CTRL_SINEMODE;
  139. }
  140. if (init->diff)
  141. {
  142. tmp |= DAC_CTRL_DIFF;
  143. }
  144. dac->CTRL = tmp;
  145. }
  146. /***************************************************************************//**
  147. * @brief
  148. * Initialize DAC channel.
  149. *
  150. * @param[in] dac
  151. * Pointer to DAC peripheral register block.
  152. *
  153. * @param[in] init
  154. * Pointer to DAC initialization structure.
  155. *
  156. * @param[in] ch
  157. * Channel number to initialize.
  158. ******************************************************************************/
  159. void DAC_InitChannel(DAC_TypeDef *dac,
  160. const DAC_InitChannel_TypeDef *init,
  161. unsigned int ch)
  162. {
  163. uint32_t tmp;
  164. EFM_ASSERT(DAC_REF_VALID(dac));
  165. EFM_ASSERT(DAC_CH_VALID(ch));
  166. tmp = (uint32_t)(init->prsSel) << _DAC_CH0CTRL_PRSSEL_SHIFT;
  167. if (init->enable)
  168. {
  169. tmp |= DAC_CH0CTRL_EN;
  170. }
  171. if (init->prsEnable)
  172. {
  173. tmp |= DAC_CH0CTRL_PRSEN;
  174. }
  175. if (init->refreshEnable)
  176. {
  177. tmp |= DAC_CH0CTRL_REFREN;
  178. }
  179. if (ch)
  180. {
  181. dac->CH1CTRL = tmp;
  182. }
  183. else
  184. {
  185. dac->CH0CTRL = tmp;
  186. }
  187. }
  188. /***************************************************************************//**
  189. * @brief
  190. * Calculate prescaler value used to determine DAC clock.
  191. *
  192. * @details
  193. * The DAC clock is given by: HFPERCLK / (prescale ^ 2).
  194. *
  195. * @param[in] dacFreq DAC frequency wanted. The frequency will automatically
  196. * be adjusted to be below max allowed DAC clock.
  197. *
  198. * @param[in] hfperFreq Frequency in Hz of reference HFPER clock. Set to 0 to
  199. * use currently defined HFPER clock setting.
  200. *
  201. * @return
  202. * Prescaler value to use for DAC in order to achieve a clock value
  203. * <= @p dacFreq.
  204. ******************************************************************************/
  205. uint8_t DAC_PrescaleCalc(uint32_t dacFreq, uint32_t hfperFreq)
  206. {
  207. uint32_t ret;
  208. /* Make sure selected DAC clock is below max value */
  209. if (dacFreq > DAC_MAX_CLOCK)
  210. {
  211. dacFreq = DAC_MAX_CLOCK;
  212. }
  213. /* Use current HFPER frequency? */
  214. if (!hfperFreq)
  215. {
  216. hfperFreq = CMU_ClockFreqGet(cmuClock_HFPER);
  217. }
  218. /* Iterate in order to determine best prescale value. Only a few possible */
  219. /* values. We start with lowest prescaler value in order to get first */
  220. /* equal or below wanted DAC frequency value. */
  221. for (ret = 0; ret <= (_DAC_CTRL_PRESC_MASK >> _DAC_CTRL_PRESC_SHIFT); ret++)
  222. {
  223. if ((hfperFreq >> ret) <= dacFreq)
  224. break;
  225. }
  226. return((uint8_t)ret);
  227. }
  228. /***************************************************************************//**
  229. * @brief
  230. * Reset DAC to same state as after a HW reset.
  231. *
  232. * @param[in] dac
  233. * Pointer to ADC peripheral register block.
  234. ******************************************************************************/
  235. void DAC_Reset(DAC_TypeDef *dac)
  236. {
  237. /* Disable channels, before resetting other registers. */
  238. dac->CH0CTRL = _DAC_CH0CTRL_RESETVALUE;
  239. dac->CH1CTRL = _DAC_CH1CTRL_RESETVALUE;
  240. dac->CTRL = _DAC_CTRL_RESETVALUE;
  241. dac->IEN = _DAC_IEN_RESETVALUE;
  242. dac->IFC = _DAC_IFC_MASK;
  243. dac->CAL = DEVINFO->DAC0CAL0;
  244. dac->BIASPROG = _DAC_BIASPROG_RESETVALUE;
  245. /* Do not reset route register, setting should be done independently */
  246. }
  247. /** @} (end addtogroup DAC) */
  248. /** @} (end addtogroup EM_Library) */