fsl_vref.c 8.8 KB

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