fsl_dcdc.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2017, NXP
  4. * All rights reserved.
  5. *
  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 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_dcdc.h"
  35. /* Component ID definition, used by tools. */
  36. #ifndef FSL_COMPONENT_ID
  37. #define FSL_COMPONENT_ID "platform.drivers.dcdc_1"
  38. #endif
  39. /*******************************************************************************
  40. * Prototypes
  41. ******************************************************************************/
  42. /*!
  43. * @brief Get instance number for DCDC module.
  44. *
  45. * @param base DCDC peripheral base address
  46. */
  47. static uint32_t DCDC_GetInstance(DCDC_Type *base);
  48. /*******************************************************************************
  49. * Variables
  50. ******************************************************************************/
  51. /*! @brief Pointers to DCDC bases for each instance. */
  52. static DCDC_Type *const s_dcdcBases[] = DCDC_BASE_PTRS;
  53. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  54. /*! @brief Pointers to DCDC clocks for each instance. */
  55. static const clock_ip_name_t s_dcdcClocks[] = DCDC_CLOCKS;
  56. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  57. /*******************************************************************************
  58. * Code
  59. ******************************************************************************/
  60. static uint32_t DCDC_GetInstance(DCDC_Type *base)
  61. {
  62. uint32_t instance;
  63. /* Find the instance index from base address mappings. */
  64. for (instance = 0; instance < ARRAY_SIZE(s_dcdcBases); instance++)
  65. {
  66. if (s_dcdcBases[instance] == base)
  67. {
  68. break;
  69. }
  70. }
  71. assert(instance < ARRAY_SIZE(s_dcdcBases));
  72. return instance;
  73. }
  74. void DCDC_Init(DCDC_Type *base)
  75. {
  76. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  77. /* Enable the clock. */
  78. CLOCK_EnableClock(s_dcdcClocks[DCDC_GetInstance(base)]);
  79. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  80. }
  81. void DCDC_Deinit(DCDC_Type *base)
  82. {
  83. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  84. /* Disable the clock. */
  85. CLOCK_DisableClock(s_dcdcClocks[DCDC_GetInstance(base)]);
  86. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  87. }
  88. void DCDC_SetClockSource(DCDC_Type *base, dcdc_clock_source_t clockSource)
  89. {
  90. uint32_t tmp32;
  91. /* Configure the DCDC_REG0 register. */
  92. tmp32 = base->REG0 &
  93. ~(DCDC_REG0_XTAL_24M_OK_MASK | DCDC_REG0_DISABLE_AUTO_CLK_SWITCH_MASK | DCDC_REG0_SEL_CLK_MASK |
  94. DCDC_REG0_PWD_OSC_INT_MASK);
  95. switch (clockSource)
  96. {
  97. case kDCDC_ClockInternalOsc:
  98. tmp32 |= DCDC_REG0_DISABLE_AUTO_CLK_SWITCH_MASK;
  99. break;
  100. case kDCDC_ClockExternalOsc:
  101. /* Choose the external clock and disable the internal clock. */
  102. tmp32 |= DCDC_REG0_DISABLE_AUTO_CLK_SWITCH_MASK | DCDC_REG0_SEL_CLK_MASK | DCDC_REG0_PWD_OSC_INT_MASK;
  103. break;
  104. case kDCDC_ClockAutoSwitch:
  105. /* Set to switch from internal ring osc to xtal 24M if auto mode is enabled. */
  106. tmp32 |= DCDC_REG0_XTAL_24M_OK_MASK;
  107. break;
  108. default:
  109. break;
  110. }
  111. base->REG0 = tmp32;
  112. }
  113. void DCDC_GetDefaultDetectionConfig(dcdc_detection_config_t *config)
  114. {
  115. assert(NULL != config);
  116. config->enableXtalokDetection = false;
  117. config->powerDownOverVoltageDetection = true;
  118. config->powerDownLowVlotageDetection = false;
  119. config->powerDownOverCurrentDetection = true;
  120. config->powerDownPeakCurrentDetection = true;
  121. config->powerDownZeroCrossDetection = true;
  122. config->OverCurrentThreshold = kDCDC_OverCurrentThresholdAlt0;
  123. config->PeakCurrentThreshold = kDCDC_PeakCurrentThresholdAlt0;
  124. }
  125. void DCDC_SetDetectionConfig(DCDC_Type *base, const dcdc_detection_config_t *config)
  126. {
  127. assert(NULL != config);
  128. uint32_t tmp32;
  129. /* Configure the DCDC_REG0 register. */
  130. tmp32 = base->REG0 &
  131. ~(DCDC_REG0_XTALOK_DISABLE_MASK | DCDC_REG0_PWD_HIGH_VOLT_DET_MASK | DCDC_REG0_PWD_CMP_BATT_DET_MASK |
  132. DCDC_REG0_PWD_OVERCUR_DET_MASK | DCDC_REG0_PWD_CUR_SNS_CMP_MASK | DCDC_REG0_PWD_ZCD_MASK |
  133. DCDC_REG0_CUR_SNS_THRSH_MASK | DCDC_REG0_OVERCUR_TRIG_ADJ_MASK);
  134. tmp32 |= DCDC_REG0_CUR_SNS_THRSH(config->PeakCurrentThreshold) |
  135. DCDC_REG0_OVERCUR_TRIG_ADJ(config->OverCurrentThreshold);
  136. if (false == config->enableXtalokDetection)
  137. {
  138. tmp32 |= DCDC_REG0_XTALOK_DISABLE_MASK;
  139. }
  140. if (config->powerDownOverVoltageDetection)
  141. {
  142. tmp32 |= DCDC_REG0_PWD_HIGH_VOLT_DET_MASK;
  143. }
  144. if (config->powerDownLowVlotageDetection)
  145. {
  146. tmp32 |= DCDC_REG0_PWD_CMP_BATT_DET_MASK;
  147. }
  148. if (config->powerDownOverCurrentDetection)
  149. {
  150. tmp32 |= DCDC_REG0_PWD_OVERCUR_DET_MASK;
  151. }
  152. if (config->powerDownPeakCurrentDetection)
  153. {
  154. tmp32 |= DCDC_REG0_PWD_CUR_SNS_CMP_MASK;
  155. }
  156. if (config->powerDownZeroCrossDetection)
  157. {
  158. tmp32 |= DCDC_REG0_PWD_ZCD_MASK;
  159. }
  160. base->REG0 = tmp32;
  161. }
  162. void DCDC_GetDefaultLowPowerConfig(dcdc_low_power_config_t *config)
  163. {
  164. assert(NULL != config);
  165. config->enableOverloadDetection = true;
  166. config->enableAdjustHystereticValue = false;
  167. config->countChargingTimePeriod = kDCDC_CountChargingTimePeriod8Cycle;
  168. config->countChargingTimeThreshold = kDCDC_CountChargingTimeThreshold32;
  169. }
  170. void DCDC_SetLowPowerConfig(DCDC_Type *base, const dcdc_low_power_config_t *config)
  171. {
  172. assert(NULL != config);
  173. uint32_t tmp32;
  174. /* Configure the DCDC_REG0 register. */
  175. tmp32 = base->REG0 &
  176. ~(DCDC_REG0_EN_LP_OVERLOAD_SNS_MASK | DCDC_REG0_LP_HIGH_HYS_MASK | DCDC_REG0_LP_OVERLOAD_FREQ_SEL_MASK |
  177. DCDC_REG0_LP_OVERLOAD_THRSH_MASK);
  178. tmp32 |= DCDC_REG0_LP_OVERLOAD_FREQ_SEL(config->countChargingTimePeriod) |
  179. DCDC_REG0_LP_OVERLOAD_THRSH(config->countChargingTimeThreshold);
  180. if (config->enableOverloadDetection)
  181. {
  182. tmp32 |= DCDC_REG0_EN_LP_OVERLOAD_SNS_MASK;
  183. }
  184. if (config->enableAdjustHystereticValue)
  185. {
  186. tmp32 |= DCDC_REG0_LP_HIGH_HYS_MASK;
  187. }
  188. base->REG0 = tmp32;
  189. }
  190. uint32_t DCDC_GetstatusFlags(DCDC_Type *base)
  191. {
  192. uint32_t tmp32 = 0U;
  193. if (DCDC_REG0_STS_DC_OK_MASK == (DCDC_REG0_STS_DC_OK_MASK & base->REG0))
  194. {
  195. tmp32 |= kDCDC_LockedOKStatus;
  196. }
  197. return tmp32;
  198. }
  199. void DCDC_ResetCurrentAlertSignal(DCDC_Type *base, bool enable)
  200. {
  201. if (enable)
  202. {
  203. base->REG0 |= DCDC_REG0_CURRENT_ALERT_RESET_MASK;
  204. }
  205. else
  206. {
  207. base->REG0 &= ~DCDC_REG0_CURRENT_ALERT_RESET_MASK;
  208. }
  209. }
  210. void DCDC_GetDefaultLoopControlConfig(dcdc_loop_control_config_t *config)
  211. {
  212. assert(NULL != config);
  213. config->enableCommonHysteresis = false;
  214. config->enableCommonThresholdDetection = false;
  215. config->enableInvertHysteresisSign = false;
  216. config->enableRCThresholdDetection = false;
  217. config->enableRCScaleCircuit = 0U;
  218. config->complementFeedForwardStep = 0U;
  219. config->controlParameterMagnitude = 2U;
  220. config->integralProportionalRatio = 2U;
  221. }
  222. void DCDC_SetLoopControlConfig(DCDC_Type *base, const dcdc_loop_control_config_t *config)
  223. {
  224. assert(NULL != config);
  225. uint32_t tmp32;
  226. /* Configure the DCDC_REG1 register. */
  227. tmp32 = base->REG1 & ~(DCDC_REG1_LOOPCTRL_EN_HYST_MASK | DCDC_REG1_LOOPCTRL_HST_THRESH_MASK);
  228. if (config->enableCommonHysteresis)
  229. {
  230. tmp32 |= DCDC_REG1_LOOPCTRL_EN_HYST_MASK;
  231. }
  232. if (config->enableCommonThresholdDetection)
  233. {
  234. tmp32 |= DCDC_REG1_LOOPCTRL_HST_THRESH_MASK;
  235. }
  236. base->REG1 = tmp32;
  237. /* configure the DCDC_REG2 register. */
  238. tmp32 = base->REG2 &
  239. ~(DCDC_REG2_LOOPCTRL_HYST_SIGN_MASK | DCDC_REG2_LOOPCTRL_RCSCALE_THRSH_MASK |
  240. DCDC_REG2_LOOPCTRL_EN_RCSCALE_MASK | DCDC_REG2_LOOPCTRL_DC_FF_MASK | DCDC_REG2_LOOPCTRL_DC_R_MASK |
  241. DCDC_REG2_LOOPCTRL_DC_C_MASK);
  242. tmp32 |= DCDC_REG2_LOOPCTRL_DC_FF(config->complementFeedForwardStep) |
  243. DCDC_REG2_LOOPCTRL_DC_R(config->controlParameterMagnitude) |
  244. DCDC_REG2_LOOPCTRL_DC_C(config->integralProportionalRatio) |
  245. DCDC_REG2_LOOPCTRL_EN_RCSCALE(config->enableRCScaleCircuit);
  246. if (config->enableInvertHysteresisSign)
  247. {
  248. tmp32 |= DCDC_REG2_LOOPCTRL_HYST_SIGN_MASK;
  249. }
  250. if (config->enableRCThresholdDetection)
  251. {
  252. tmp32 |= DCDC_REG2_LOOPCTRL_RCSCALE_THRSH_MASK;
  253. }
  254. base->REG2 = tmp32;
  255. }
  256. void DCDC_SetMinPowerConfig(DCDC_Type *base, const dcdc_min_power_config_t *config)
  257. {
  258. assert(NULL != config);
  259. uint32_t tmp32;
  260. tmp32 = base->REG3 & ~DCDC_REG3_MINPWR_DC_HALFCLK_MASK;
  261. if (config->enableUseHalfFreqForContinuous)
  262. {
  263. tmp32 |= DCDC_REG3_MINPWR_DC_HALFCLK_MASK;
  264. }
  265. base->REG3 = tmp32;
  266. }
  267. void DCDC_AdjustTargetVoltage(DCDC_Type *base, uint32_t VDDRun, uint32_t VDDStandby)
  268. {
  269. uint32_t tmp32;
  270. /* Unlock the step for the output. */
  271. base->REG3 &= ~DCDC_REG3_DISABLE_STEP_MASK;
  272. /* Configure the DCDC_REG3 register. */
  273. tmp32 = base->REG3 & ~(DCDC_REG3_TARGET_LP_MASK | DCDC_REG3_TRG_MASK);
  274. tmp32 |= DCDC_REG3_TARGET_LP(VDDStandby) | DCDC_REG3_TRG(VDDRun);
  275. base->REG3 = tmp32;
  276. /* DCDC_STS_DC_OK bit will be de-asserted after target register changes. After output voltage settling to new
  277. * target value, DCDC_STS_DC_OK will be asserted. */
  278. while (DCDC_REG0_STS_DC_OK_MASK != (DCDC_REG0_STS_DC_OK_MASK & base->REG0))
  279. {
  280. }
  281. }
  282. void DCDC_SetInternalRegulatorConfig(DCDC_Type *base, const dcdc_internal_regulator_config_t *config)
  283. {
  284. assert(NULL != config);
  285. uint32_t tmp32;
  286. /* Configure the DCDC_REG1 register. */
  287. tmp32 = base->REG1 & ~(DCDC_REG1_REG_FBK_SEL_MASK | DCDC_REG1_REG_RLOAD_SW_MASK);
  288. tmp32 |= DCDC_REG1_REG_FBK_SEL(config->feedbackPoint);
  289. if (config->enableLoadResistor)
  290. {
  291. tmp32 |= DCDC_REG1_REG_RLOAD_SW_MASK;
  292. }
  293. base->REG1 = tmp32;
  294. }
  295. void DCDC_BootIntoDCM(DCDC_Type *base)
  296. {
  297. base->REG0 &= ~(DCDC_REG0_PWD_ZCD_MASK | DCDC_REG0_PWD_CMP_OFFSET_MASK);
  298. base->REG2 = (~DCDC_REG2_LOOPCTRL_EN_RCSCALE_MASK & base->REG2) | DCDC_REG2_LOOPCTRL_EN_RCSCALE(0x4U) |
  299. DCDC_REG2_DCM_SET_CTRL_MASK;
  300. }
  301. void DCDC_BootIntoCCM(DCDC_Type *base)
  302. {
  303. base->REG0 = (~DCDC_REG0_PWD_CMP_OFFSET_MASK & base->REG0) | DCDC_REG0_PWD_ZCD_MASK;
  304. base->REG2 = (~DCDC_REG2_LOOPCTRL_EN_RCSCALE_MASK & base->REG2) | DCDC_REG2_LOOPCTRL_EN_RCSCALE(0x3U);
  305. }