fsl_vref.c 8.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234
  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_vref.h"
  35. /*******************************************************************************
  36. * Prototypes
  37. ******************************************************************************/
  38. /*!
  39. * @brief Gets the instance from the base address
  40. *
  41. * @param base VREF peripheral base address
  42. *
  43. * @return The VREF instance
  44. */
  45. static uint32_t VREF_GetInstance(VREF_Type *base);
  46. /*******************************************************************************
  47. * Variables
  48. ******************************************************************************/
  49. /*! @brief Pointers to VREF bases for each instance. */
  50. static VREF_Type *const s_vrefBases[] = VREF_BASE_PTRS;
  51. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  52. /*! @brief Pointers to VREF clocks for each instance. */
  53. static const clock_ip_name_t s_vrefClocks[] = VREF_CLOCKS;
  54. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  55. /*******************************************************************************
  56. * Code
  57. ******************************************************************************/
  58. static uint32_t VREF_GetInstance(VREF_Type *base)
  59. {
  60. uint32_t instance;
  61. /* Find the instance index from base address mappings. */
  62. for (instance = 0; instance < ARRAY_SIZE(s_vrefBases); instance++)
  63. {
  64. if (s_vrefBases[instance] == base)
  65. {
  66. break;
  67. }
  68. }
  69. assert(instance < ARRAY_SIZE(s_vrefBases));
  70. return instance;
  71. }
  72. void VREF_Init(VREF_Type *base, const vref_config_t *config)
  73. {
  74. assert(config != NULL);
  75. uint8_t reg = 0U;
  76. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  77. /* Ungate clock for VREF */
  78. CLOCK_EnableClock(s_vrefClocks[VREF_GetInstance(base)]);
  79. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  80. /* Configure VREF to a known state */
  81. #if defined(FSL_FEATURE_VREF_HAS_CHOP_OSC) && FSL_FEATURE_VREF_HAS_CHOP_OSC
  82. /* Set chop oscillator bit */
  83. base->TRM |= VREF_TRM_CHOPEN_MASK;
  84. #endif /* FSL_FEATURE_VREF_HAS_CHOP_OSC */
  85. /* Get current SC register */
  86. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  87. reg = base->VREFH_SC;
  88. #else
  89. reg = base->SC;
  90. #endif/* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  91. /* Clear old buffer mode selection bits */
  92. reg &= ~VREF_SC_MODE_LV_MASK;
  93. /* Set buffer Mode selection and Regulator enable bit */
  94. reg |= VREF_SC_MODE_LV(config->bufferMode) | VREF_SC_REGEN(1U);
  95. #if defined(FSL_FEATURE_VREF_HAS_COMPENSATION) && FSL_FEATURE_VREF_HAS_COMPENSATION
  96. /* Set second order curvature compensation enable bit */
  97. reg |= VREF_SC_ICOMPEN(1U);
  98. #endif /* FSL_FEATURE_VREF_HAS_COMPENSATION */
  99. /* Enable VREF module */
  100. reg |= VREF_SC_VREFEN(1U);
  101. /* Update bit-field from value to Status and Control register */
  102. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  103. base->VREFH_SC = reg;
  104. #else
  105. base->SC = reg;
  106. #endif/* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  107. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  108. reg = base->VREFL_TRM;
  109. /* Clear old select external voltage reference and VREFL (0.4 V) reference buffer enable bits */
  110. reg &= ~(VREF_VREFL_TRM_VREFL_EN_MASK | VREF_VREFL_TRM_VREFL_SEL_MASK);
  111. /* Select external voltage reference and set VREFL (0.4 V) reference buffer enable */
  112. reg |= VREF_VREFL_TRM_VREFL_SEL(config->enableExternalVoltRef) | VREF_VREFL_TRM_VREFL_EN(config->enableLowRef);
  113. base->VREFL_TRM = reg;
  114. #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  115. #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
  116. reg = base->TRM4;
  117. /* Clear old select internal voltage reference bit (2.1V) */
  118. reg &= ~VREF_TRM4_VREF2V1_EN_MASK;
  119. /* Select internal voltage reference (2.1V) */
  120. reg |= VREF_TRM4_VREF2V1_EN(config->enable2V1VoltRef);
  121. base->TRM4 = reg;
  122. #endif /* FSL_FEATURE_VREF_HAS_TRM4 */
  123. /* Wait until internal voltage stable */
  124. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  125. while ((base->VREFH_SC & VREF_SC_VREFST_MASK) == 0)
  126. #else
  127. while ((base->SC & VREF_SC_VREFST_MASK) == 0)
  128. #endif/* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  129. {
  130. }
  131. }
  132. void VREF_Deinit(VREF_Type *base)
  133. {
  134. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  135. /* Gate clock for VREF */
  136. CLOCK_DisableClock(s_vrefClocks[VREF_GetInstance(base)]);
  137. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  138. }
  139. void VREF_GetDefaultConfig(vref_config_t *config)
  140. {
  141. assert(config);
  142. /* Set High power buffer mode in */
  143. #if defined(FSL_FEATURE_VREF_MODE_LV_TYPE) && FSL_FEATURE_VREF_MODE_LV_TYPE
  144. config->bufferMode = kVREF_ModeHighPowerBuffer;
  145. #else
  146. config->bufferMode = kVREF_ModeTightRegulationBuffer;
  147. #endif /* FSL_FEATURE_VREF_MODE_LV_TYPE */
  148. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  149. /* Select internal voltage reference */
  150. config->enableExternalVoltRef = false;
  151. /* Set VREFL (0.4 V) reference buffer disable */
  152. config->enableLowRef = false;
  153. #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  154. #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
  155. /* Disable internal voltage reference (2.1V) */
  156. config->enable2V1VoltRef = false;
  157. #endif /* FSL_FEATURE_VREF_HAS_TRM4 */
  158. }
  159. void VREF_SetTrimVal(VREF_Type *base, uint8_t trimValue)
  160. {
  161. uint8_t reg = 0U;
  162. /* Set TRIM bits value in voltage reference */
  163. reg = base->TRM;
  164. reg = ((reg & ~VREF_TRM_TRIM_MASK) | VREF_TRM_TRIM(trimValue));
  165. base->TRM = reg;
  166. /* Wait until internal voltage stable */
  167. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  168. while ((base->VREFH_SC & VREF_SC_VREFST_MASK) == 0)
  169. #else
  170. while ((base->SC & VREF_SC_VREFST_MASK) == 0)
  171. #endif/* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */
  172. {
  173. }
  174. }
  175. #if defined(FSL_FEATURE_VREF_HAS_TRM4) && FSL_FEATURE_VREF_HAS_TRM4
  176. void VREF_SetTrim2V1Val(VREF_Type *base, uint8_t trimValue)
  177. {
  178. uint8_t reg = 0U;
  179. /* Set TRIM bits value in voltage reference (2V1) */
  180. reg = base->TRM4;
  181. reg = ((reg & ~VREF_TRM4_TRIM2V1_MASK) | VREF_TRM4_TRIM2V1(trimValue));
  182. base->TRM4 = reg;
  183. /* Wait until internal voltage stable */
  184. while ((base->SC & VREF_SC_VREFST_MASK) == 0)
  185. {
  186. }
  187. }
  188. #endif /* FSL_FEATURE_VREF_HAS_TRM4 */
  189. #if defined(FSL_FEATURE_VREF_HAS_LOW_REFERENCE) && FSL_FEATURE_VREF_HAS_LOW_REFERENCE
  190. void VREF_SetLowReferenceTrimVal(VREF_Type *base, uint8_t trimValue)
  191. {
  192. /* The values 111b and 110b are NOT valid/allowed */
  193. assert((trimValue != 0x7U) && (trimValue != 0x6U));
  194. uint8_t reg = 0U;
  195. /* Set TRIM bits value in low voltage reference */
  196. reg = base->VREFL_TRM;
  197. reg = ((reg & ~VREF_VREFL_TRM_VREFL_TRIM_MASK) | VREF_VREFL_TRM_VREFL_TRIM(trimValue));
  198. base->VREFL_TRM = reg;
  199. /* Wait until internal voltage stable */
  200. while ((base->VREFH_SC & VREF_SC_VREFST_MASK) == 0)
  201. {
  202. }
  203. }
  204. #endif /* FSL_FEATURE_VREF_HAS_LOW_REFERENCE */