fsl_dac12.c 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200
  1. /*
  2. * Copyright (c) 2016, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2020 NXP
  4. * All rights reserved.
  5. *
  6. * SPDX-License-Identifier: BSD-3-Clause
  7. */
  8. #include "fsl_dac12.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.dac12"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. /*!
  17. * @brief Get instance number for DAC12 module.
  18. *
  19. * @param base DAC12 peripheral base address
  20. */
  21. static uint32_t DAC12_GetInstance(DAC_Type *base);
  22. /*******************************************************************************
  23. * Variables
  24. ******************************************************************************/
  25. /*! @brief Pointers to DAC bases for each instance. */
  26. static DAC_Type *const s_dac12Bases[] = DAC_BASE_PTRS;
  27. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  28. /*! @brief Pointers to DAC clocks for each instance. */
  29. static const clock_ip_name_t s_dac12Clocks[] = DAC_CLOCKS;
  30. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  31. /*******************************************************************************
  32. * Codes
  33. ******************************************************************************/
  34. static uint32_t DAC12_GetInstance(DAC_Type *base)
  35. {
  36. uint32_t instance;
  37. /* Find the instance index from base address mappings. */
  38. for (instance = 0; instance < ARRAY_SIZE(s_dac12Bases); instance++)
  39. {
  40. if (s_dac12Bases[instance] == base)
  41. {
  42. break;
  43. }
  44. }
  45. assert(instance < ARRAY_SIZE(s_dac12Bases));
  46. return instance;
  47. }
  48. /*!
  49. * brief Get hardware information about this module.
  50. *
  51. * param base DAC12 peripheral base address.
  52. * param info Pointer to info structure, see to #dac12_hardware_info_t.
  53. */
  54. void DAC12_GetHardwareInfo(DAC_Type *base, dac12_hardware_info_t *info)
  55. {
  56. assert(NULL != info);
  57. info->fifoSizeInfo =
  58. (dac12_fifo_size_info_t)(uint32_t)((DAC_PARAM_FIFOSZ_MASK & base->PARAM) >> DAC_PARAM_FIFOSZ_SHIFT);
  59. }
  60. /*!
  61. * brief Initialize the DAC12 module.
  62. *
  63. * param base DAC12 peripheral base address.
  64. * param config Pointer to configuration structure, see to #dac12_config_t.
  65. */
  66. void DAC12_Init(DAC_Type *base, const dac12_config_t *config)
  67. {
  68. assert(NULL != config);
  69. uint32_t tmp32;
  70. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  71. /* Enable the clock. */
  72. CLOCK_EnableClock(s_dac12Clocks[DAC12_GetInstance(base)]);
  73. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  74. tmp32 = DAC_CR_WML(config->fifoWatermarkLevel); /* FIFO watermark level. */
  75. switch (config->fifoWorkMode) /* FIFO work mode. */
  76. {
  77. case kDAC12_FIFOWorkAsNormalMode:
  78. tmp32 |= DAC_CR_FIFOEN_MASK;
  79. break;
  80. case kDAC12_FIFOWorkAsSwingMode:
  81. tmp32 |= DAC_CR_FIFOEN_MASK | DAC_CR_SWMD_MASK;
  82. break;
  83. default: /* kDAC12_FIFODisabled. */
  84. break;
  85. }
  86. tmp32 |= DAC_CR_DACRFS(config->referenceVoltageSource) /* Reference voltage source. */
  87. | DAC_CR_TRGSEL(config->fifoTriggerMode); /* Trigger mode. */
  88. base->CR = tmp32;
  89. /* DACx_CR2. */
  90. tmp32 = 0U;
  91. /* Reference voltage current. */
  92. switch (config->referenceCurrentSource)
  93. {
  94. case kDAC12_ReferenceCurrentSourceAlt0:
  95. tmp32 |= DAC_CR2_IREF_MASK;
  96. break;
  97. case kDAC12_ReferenceCurrentSourceAlt1:
  98. tmp32 |= DAC_CR2_IREF1_MASK;
  99. break;
  100. case kDAC12_ReferenceCurrentSourceAlt2:
  101. tmp32 |= DAC_CR2_IREF2_MASK;
  102. break;
  103. default: /* kDAC12_ReferenceCurrentSourceDisabled */
  104. break;
  105. }
  106. /* Speed mode. */
  107. switch (config->speedMode)
  108. {
  109. case kDAC12_SpeedMiddleMode:
  110. tmp32 |= DAC_CR2_BFMS_MASK;
  111. break;
  112. case kDAC12_SpeedHighMode:
  113. tmp32 |= DAC_CR2_BFHS_MASK;
  114. break;
  115. default: /* kDAC12_SpeedLowMode */
  116. break;
  117. }
  118. /* DAC buffered mode needs OPAMP enabled. DAC unbuffered mode needs OPAMP disabled. */
  119. if (config->enableAnalogBuffer)
  120. {
  121. tmp32 |= DAC_CR2_BFEN_MASK; /* OPAMP is used as buffer. */
  122. }
  123. else
  124. {
  125. tmp32 |= DAC_CR2_OEN_MASK; /* Output buffer is bypassed. */
  126. }
  127. base->CR2 = tmp32;
  128. #if !(defined(FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER) && FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER)
  129. base->ITRM = DAC_ITRM_TRIM(config->currentReferenceInternalTrimValue);
  130. #endif /* FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER */
  131. }
  132. /*!
  133. * brief De-initialize the DAC12 module.
  134. *
  135. * param base DAC12 peripheral base address.
  136. */
  137. void DAC12_Deinit(DAC_Type *base)
  138. {
  139. DAC12_Enable(base, false);
  140. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  141. CLOCK_DisableClock(s_dac12Clocks[DAC12_GetInstance(base)]);
  142. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  143. }
  144. /*!
  145. * brief Initializes the DAC12 user configuration structure.
  146. *
  147. * This function initializes the user configuration structure to a default value. The default values are:
  148. * code
  149. * config->fifoWatermarkLevel = 0U;
  150. * config->fifoWorkMode = kDAC12_FIFODisabled;
  151. * config->referenceVoltageSource = kDAC12_ReferenceVoltageSourceAlt1;
  152. * config->fifoTriggerMode = kDAC12_FIFOTriggerByHardwareMode;
  153. * config->referenceCurrentSource = kDAC12_ReferenceCurrentSourceAlt0;
  154. * config->speedMode = kDAC12_SpeedLowMode;
  155. * config->speedMode = false;
  156. * config->currentReferenceInternalTrimValue = 0x4;
  157. * endcode
  158. * param config Pointer to the configuration structure. See "dac12_config_t".
  159. */
  160. void DAC12_GetDefaultConfig(dac12_config_t *config)
  161. {
  162. assert(NULL != config);
  163. /* Initializes the configure structure to zero. */
  164. (void)memset(config, 0, sizeof(*config));
  165. config->fifoWatermarkLevel = 0U;
  166. config->fifoWorkMode = kDAC12_FIFODisabled;
  167. config->referenceVoltageSource = kDAC12_ReferenceVoltageSourceAlt1;
  168. config->fifoTriggerMode = kDAC12_FIFOTriggerByHardwareMode;
  169. config->referenceCurrentSource = kDAC12_ReferenceCurrentSourceAlt0;
  170. config->speedMode = kDAC12_SpeedLowMode;
  171. config->enableAnalogBuffer = false;
  172. #if !(defined(FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER) && FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER)
  173. config->currentReferenceInternalTrimValue = 0x4;
  174. #endif /* FSL_FEATURE_DAC12_HAS_NO_ITRM_REGISTER */
  175. }