fsl_dac.c 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  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. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.dac"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Get instance number for DAC module.
  44. *
  45. * @param base DAC peripheral base address
  46. */
  47. static uint32_t DAC_GetInstance(DAC_Type *base);
  48. /*******************************************************************************
  49. * Variables
  50. ******************************************************************************/
  51. /*! @brief Pointers to DAC bases for each instance. */
  52. static DAC_Type *const s_dacBases[] = DAC_BASE_PTRS;
  53. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  54. /*! @brief Pointers to DAC clocks for each instance. */
  55. static const clock_ip_name_t s_dacClocks[] = DAC_CLOCKS;
  56. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  57. /*******************************************************************************
  58. * Codes
  59. ******************************************************************************/
  60. static uint32_t DAC_GetInstance(DAC_Type *base)
  61. {
  62. uint32_t instance;
  63. /* Find the instance index from base address mappings. */
  64. for (instance = 0; instance < ARRAY_SIZE(s_dacBases); instance++)
  65. {
  66. if (s_dacBases[instance] == base)
  67. {
  68. break;
  69. }
  70. }
  71. assert(instance < ARRAY_SIZE(s_dacBases));
  72. return instance;
  73. }
  74. void DAC_Init(DAC_Type *base, const dac_config_t *config)
  75. {
  76. assert(NULL != config);
  77. uint8_t tmp8;
  78. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  79. /* Enable the clock. */
  80. CLOCK_EnableClock(s_dacClocks[DAC_GetInstance(base)]);
  81. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  82. /* Configure. */
  83. /* DACx_C0. */
  84. tmp8 = base->C0 & ~(DAC_C0_DACRFS_MASK | DAC_C0_LPEN_MASK);
  85. if (kDAC_ReferenceVoltageSourceVref2 == config->referenceVoltageSource)
  86. {
  87. tmp8 |= DAC_C0_DACRFS_MASK;
  88. }
  89. if (config->enableLowPowerMode)
  90. {
  91. tmp8 |= DAC_C0_LPEN_MASK;
  92. }
  93. base->C0 = tmp8;
  94. /* DAC_Enable(base, true); */
  95. /* Tip: The DAC output can be enabled till then after user sets their own available data in application. */
  96. }
  97. void DAC_Deinit(DAC_Type *base)
  98. {
  99. DAC_Enable(base, false);
  100. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  101. /* Disable the clock. */
  102. CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]);
  103. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  104. }
  105. void DAC_GetDefaultConfig(dac_config_t *config)
  106. {
  107. assert(NULL != config);
  108. config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
  109. config->enableLowPowerMode = false;
  110. }
  111. void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config)
  112. {
  113. assert(NULL != config);
  114. uint8_t tmp8;
  115. /* DACx_C0. */
  116. tmp8 = base->C0 & ~(DAC_C0_DACTRGSEL_MASK);
  117. if (kDAC_BufferTriggerBySoftwareMode == config->triggerMode)
  118. {
  119. tmp8 |= DAC_C0_DACTRGSEL_MASK;
  120. }
  121. base->C0 = tmp8;
  122. /* DACx_C1. */
  123. tmp8 = base->C1 &
  124. ~(
  125. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  126. DAC_C1_DACBFWM_MASK |
  127. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  128. DAC_C1_DACBFMD_MASK);
  129. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  130. tmp8 |= DAC_C1_DACBFWM(config->watermark);
  131. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  132. tmp8 |= DAC_C1_DACBFMD(config->workMode);
  133. base->C1 = tmp8;
  134. /* DACx_C2. */
  135. tmp8 = base->C2 & ~DAC_C2_DACBFUP_MASK;
  136. tmp8 |= DAC_C2_DACBFUP(config->upperLimit);
  137. base->C2 = tmp8;
  138. }
  139. void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config)
  140. {
  141. assert(NULL != config);
  142. config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
  143. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  144. config->watermark = kDAC_BufferWatermark1Word;
  145. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  146. config->workMode = kDAC_BufferWorkAsNormalMode;
  147. config->upperLimit = DAC_DATL_COUNT - 1U;
  148. }
  149. void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value)
  150. {
  151. assert(index < DAC_DATL_COUNT);
  152. base->DAT[index].DATL = (uint8_t)(0xFFU & value); /* Low 8-bit. */
  153. base->DAT[index].DATH = (uint8_t)((0xF00U & value) >> 8); /* High 4-bit. */
  154. }
  155. void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index)
  156. {
  157. assert(index < DAC_DATL_COUNT);
  158. uint8_t tmp8 = base->C2 & ~DAC_C2_DACBFRP_MASK;
  159. tmp8 |= DAC_C2_DACBFRP(index);
  160. base->C2 = tmp8;
  161. }
  162. void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask)
  163. {
  164. mask &= (
  165. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  166. DAC_C0_DACBWIEN_MASK |
  167. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  168. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  169. base->C0 |= ((uint8_t)mask); /* Write 1 to enable. */
  170. }
  171. void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask)
  172. {
  173. mask &= (
  174. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  175. DAC_C0_DACBWIEN_MASK |
  176. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  177. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  178. base->C0 &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to disable. */
  179. }
  180. uint32_t DAC_GetBufferStatusFlags(DAC_Type *base)
  181. {
  182. return (uint32_t)(base->SR & (
  183. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  184. DAC_SR_DACBFWMF_MASK |
  185. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  186. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK));
  187. }
  188. void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask)
  189. {
  190. mask &= (
  191. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  192. DAC_SR_DACBFWMF_MASK |
  193. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  194. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK);
  195. base->SR &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to clear flags. */
  196. }