fsl_dac.c 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  4. * Copyright 2016-2017 NXP
  5. * All rights reserved.
  6. *
  7. * Redistribution and use in source and binary forms, with or without modification,
  8. * are permitted (subject to the limitations in the disclaimer below) provided
  9. * that the following conditions are met:
  10. *
  11. * o Redistributions of source code must retain the above copyright notice, this list
  12. * of conditions and the following disclaimer.
  13. *
  14. * o Redistributions in binary form must reproduce the above copyright notice, this
  15. * list of conditions and the following disclaimer in the documentation and/or
  16. * other materials provided with the distribution.
  17. *
  18. * o Neither the name of the copyright holder nor the names of its
  19. * contributors may be used to endorse or promote products derived from this
  20. * software without specific prior written permission.
  21. *
  22. * NO EXPRESS OR IMPLIED LICENSES TO ANY PARTY'S PATENT RIGHTS ARE GRANTED BY THIS LICENSE.
  23. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  24. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  25. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  26. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  27. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  28. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  29. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  30. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  31. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  32. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  33. */
  34. #include "fsl_dac.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Get instance number for DAC module.
  40. *
  41. * @param base DAC peripheral base address
  42. */
  43. static uint32_t DAC_GetInstance(DAC_Type *base);
  44. /*******************************************************************************
  45. * Variables
  46. ******************************************************************************/
  47. /*! @brief Pointers to DAC bases for each instance. */
  48. static DAC_Type *const s_dacBases[] = DAC_BASE_PTRS;
  49. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  50. /*! @brief Pointers to DAC clocks for each instance. */
  51. static const clock_ip_name_t s_dacClocks[] = DAC_CLOCKS;
  52. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  53. /*******************************************************************************
  54. * Codes
  55. ******************************************************************************/
  56. static uint32_t DAC_GetInstance(DAC_Type *base)
  57. {
  58. uint32_t instance;
  59. /* Find the instance index from base address mappings. */
  60. for (instance = 0; instance < ARRAY_SIZE(s_dacBases); instance++)
  61. {
  62. if (s_dacBases[instance] == base)
  63. {
  64. break;
  65. }
  66. }
  67. assert(instance < ARRAY_SIZE(s_dacBases));
  68. return instance;
  69. }
  70. void DAC_Init(DAC_Type *base, const dac_config_t *config)
  71. {
  72. assert(NULL != config);
  73. uint8_t tmp8;
  74. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  75. /* Enable the clock. */
  76. CLOCK_EnableClock(s_dacClocks[DAC_GetInstance(base)]);
  77. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  78. /* Configure. */
  79. /* DACx_C0. */
  80. tmp8 = base->C0 & ~(DAC_C0_DACRFS_MASK | DAC_C0_LPEN_MASK);
  81. if (kDAC_ReferenceVoltageSourceVref2 == config->referenceVoltageSource)
  82. {
  83. tmp8 |= DAC_C0_DACRFS_MASK;
  84. }
  85. if (config->enableLowPowerMode)
  86. {
  87. tmp8 |= DAC_C0_LPEN_MASK;
  88. }
  89. base->C0 = tmp8;
  90. /* DAC_Enable(base, true); */
  91. /* Tip: The DAC output can be enabled till then after user sets their own available data in application. */
  92. }
  93. void DAC_Deinit(DAC_Type *base)
  94. {
  95. DAC_Enable(base, false);
  96. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  97. /* Disable the clock. */
  98. CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]);
  99. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  100. }
  101. void DAC_GetDefaultConfig(dac_config_t *config)
  102. {
  103. assert(NULL != config);
  104. config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
  105. config->enableLowPowerMode = false;
  106. }
  107. void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config)
  108. {
  109. assert(NULL != config);
  110. uint8_t tmp8;
  111. /* DACx_C0. */
  112. tmp8 = base->C0 & ~(DAC_C0_DACTRGSEL_MASK);
  113. if (kDAC_BufferTriggerBySoftwareMode == config->triggerMode)
  114. {
  115. tmp8 |= DAC_C0_DACTRGSEL_MASK;
  116. }
  117. base->C0 = tmp8;
  118. /* DACx_C1. */
  119. tmp8 = base->C1 &
  120. ~(
  121. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  122. DAC_C1_DACBFWM_MASK |
  123. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  124. DAC_C1_DACBFMD_MASK);
  125. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  126. tmp8 |= DAC_C1_DACBFWM(config->watermark);
  127. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  128. tmp8 |= DAC_C1_DACBFMD(config->workMode);
  129. base->C1 = tmp8;
  130. /* DACx_C2. */
  131. tmp8 = base->C2 & ~DAC_C2_DACBFUP_MASK;
  132. tmp8 |= DAC_C2_DACBFUP(config->upperLimit);
  133. base->C2 = tmp8;
  134. }
  135. void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config)
  136. {
  137. assert(NULL != config);
  138. config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
  139. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  140. config->watermark = kDAC_BufferWatermark1Word;
  141. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  142. config->workMode = kDAC_BufferWorkAsNormalMode;
  143. config->upperLimit = DAC_DATL_COUNT - 1U;
  144. }
  145. void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value)
  146. {
  147. assert(index < DAC_DATL_COUNT);
  148. base->DAT[index].DATL = (uint8_t)(0xFFU & value); /* Low 8-bit. */
  149. base->DAT[index].DATH = (uint8_t)((0xF00U & value) >> 8); /* High 4-bit. */
  150. }
  151. void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index)
  152. {
  153. assert(index < DAC_DATL_COUNT);
  154. uint8_t tmp8 = base->C2 & ~DAC_C2_DACBFRP_MASK;
  155. tmp8 |= DAC_C2_DACBFRP(index);
  156. base->C2 = tmp8;
  157. }
  158. void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask)
  159. {
  160. mask &= (
  161. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  162. DAC_C0_DACBWIEN_MASK |
  163. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  164. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  165. base->C0 |= ((uint8_t)mask); /* Write 1 to enable. */
  166. }
  167. void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask)
  168. {
  169. mask &= (
  170. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  171. DAC_C0_DACBWIEN_MASK |
  172. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  173. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  174. base->C0 &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to disable. */
  175. }
  176. uint32_t DAC_GetBufferStatusFlags(DAC_Type *base)
  177. {
  178. return (uint32_t)(base->SR & (
  179. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  180. DAC_SR_DACBFWMF_MASK |
  181. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  182. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK));
  183. }
  184. void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask)
  185. {
  186. mask &= (
  187. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  188. DAC_SR_DACBFWMF_MASK |
  189. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  190. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK);
  191. base->SR &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to clear flags. */
  192. }