fsl_cmp.c 8.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285
  1. /*
  2. * Copyright (c) 2015, Freescale Semiconductor, Inc.
  3. * Copyright 2016-2017 NXP
  4. *
  5. * Redistribution and use in source and binary forms, with or without modification,
  6. * are permitted provided that the following conditions are met:
  7. *
  8. * o Redistributions of source code must retain the above copyright notice, this list
  9. * of conditions and the following disclaimer.
  10. *
  11. * o Redistributions in binary form must reproduce the above copyright notice, this
  12. * list of conditions and the following disclaimer in the documentation and/or
  13. * other materials provided with the distribution.
  14. *
  15. * o Neither the name of the copyright holder nor the names of its
  16. * contributors may be used to endorse or promote products derived from this
  17. * software without specific prior written permission.
  18. *
  19. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND
  20. * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
  21. * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  22. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR
  23. * ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
  24. * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
  25. * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
  26. * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
  27. * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  28. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  29. */
  30. #include "fsl_cmp.h"
  31. /*******************************************************************************
  32. * Prototypes
  33. ******************************************************************************/
  34. /*!
  35. * @brief Get instance number for CMP module.
  36. *
  37. * @param base CMP peripheral base address
  38. */
  39. static uint32_t CMP_GetInstance(CMP_Type *base);
  40. /*******************************************************************************
  41. * Variables
  42. ******************************************************************************/
  43. /*! @brief Pointers to CMP bases for each instance. */
  44. static CMP_Type *const s_cmpBases[] = CMP_BASE_PTRS;
  45. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  46. /*! @brief Pointers to CMP clocks for each instance. */
  47. static const clock_ip_name_t s_cmpClocks[] = CMP_CLOCKS;
  48. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  49. /*******************************************************************************
  50. * Codes
  51. ******************************************************************************/
  52. static uint32_t CMP_GetInstance(CMP_Type *base)
  53. {
  54. uint32_t instance;
  55. /* Find the instance index from base address mappings. */
  56. for (instance = 0; instance < ARRAY_SIZE(s_cmpBases); instance++)
  57. {
  58. if (s_cmpBases[instance] == base)
  59. {
  60. break;
  61. }
  62. }
  63. assert(instance < ARRAY_SIZE(s_cmpBases));
  64. return instance;
  65. }
  66. void CMP_Init(CMP_Type *base, const cmp_config_t *config)
  67. {
  68. assert(NULL != config);
  69. uint8_t tmp8;
  70. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  71. /* Enable the clock. */
  72. CLOCK_EnableClock(s_cmpClocks[CMP_GetInstance(base)]);
  73. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  74. /* Configure. */
  75. CMP_Enable(base, false); /* Disable the CMP module during configuring. */
  76. /* CMPx_CR1. */
  77. tmp8 = base->CR1 & ~(CMP_CR1_PMODE_MASK | CMP_CR1_INV_MASK | CMP_CR1_COS_MASK | CMP_CR1_OPE_MASK);
  78. if (config->enableHighSpeed)
  79. {
  80. tmp8 |= CMP_CR1_PMODE_MASK;
  81. }
  82. if (config->enableInvertOutput)
  83. {
  84. tmp8 |= CMP_CR1_INV_MASK;
  85. }
  86. if (config->useUnfilteredOutput)
  87. {
  88. tmp8 |= CMP_CR1_COS_MASK;
  89. }
  90. if (config->enablePinOut)
  91. {
  92. tmp8 |= CMP_CR1_OPE_MASK;
  93. }
  94. #if defined(FSL_FEATURE_CMP_HAS_TRIGGER_MODE) && FSL_FEATURE_CMP_HAS_TRIGGER_MODE
  95. if (config->enableTriggerMode)
  96. {
  97. tmp8 |= CMP_CR1_TRIGM_MASK;
  98. }
  99. else
  100. {
  101. tmp8 &= ~CMP_CR1_TRIGM_MASK;
  102. }
  103. #endif /* FSL_FEATURE_CMP_HAS_TRIGGER_MODE */
  104. base->CR1 = tmp8;
  105. /* CMPx_CR0. */
  106. tmp8 = base->CR0 & ~CMP_CR0_HYSTCTR_MASK;
  107. tmp8 |= CMP_CR0_HYSTCTR(config->hysteresisMode);
  108. base->CR0 = tmp8;
  109. CMP_Enable(base, config->enableCmp); /* Enable the CMP module after configured or not. */
  110. }
  111. void CMP_Deinit(CMP_Type *base)
  112. {
  113. /* Disable the CMP module. */
  114. CMP_Enable(base, false);
  115. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  116. /* Disable the clock. */
  117. CLOCK_DisableClock(s_cmpClocks[CMP_GetInstance(base)]);
  118. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  119. }
  120. void CMP_GetDefaultConfig(cmp_config_t *config)
  121. {
  122. assert(NULL != config);
  123. config->enableCmp = true; /* Enable the CMP module after initialization. */
  124. config->hysteresisMode = kCMP_HysteresisLevel0;
  125. config->enableHighSpeed = false;
  126. config->enableInvertOutput = false;
  127. config->useUnfilteredOutput = false;
  128. config->enablePinOut = false;
  129. #if defined(FSL_FEATURE_CMP_HAS_TRIGGER_MODE) && FSL_FEATURE_CMP_HAS_TRIGGER_MODE
  130. config->enableTriggerMode = false;
  131. #endif /* FSL_FEATURE_CMP_HAS_TRIGGER_MODE */
  132. }
  133. void CMP_SetInputChannels(CMP_Type *base, uint8_t positiveChannel, uint8_t negativeChannel)
  134. {
  135. uint8_t tmp8 = base->MUXCR;
  136. tmp8 &= ~(CMP_MUXCR_PSEL_MASK | CMP_MUXCR_MSEL_MASK);
  137. tmp8 |= CMP_MUXCR_PSEL(positiveChannel) | CMP_MUXCR_MSEL(negativeChannel);
  138. base->MUXCR = tmp8;
  139. }
  140. #if defined(FSL_FEATURE_CMP_HAS_DMA) && FSL_FEATURE_CMP_HAS_DMA
  141. void CMP_EnableDMA(CMP_Type *base, bool enable)
  142. {
  143. uint8_t tmp8 = base->SCR & ~(CMP_SCR_CFR_MASK | CMP_SCR_CFF_MASK); /* To avoid change the w1c bits. */
  144. if (enable)
  145. {
  146. tmp8 |= CMP_SCR_DMAEN_MASK;
  147. }
  148. else
  149. {
  150. tmp8 &= ~CMP_SCR_DMAEN_MASK;
  151. }
  152. base->SCR = tmp8;
  153. }
  154. #endif /* FSL_FEATURE_CMP_HAS_DMA */
  155. void CMP_SetFilterConfig(CMP_Type *base, const cmp_filter_config_t *config)
  156. {
  157. assert(NULL != config);
  158. uint8_t tmp8;
  159. #if defined(FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT) && FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT
  160. /* Choose the clock source for sampling. */
  161. if (config->enableSample)
  162. {
  163. base->CR1 |= CMP_CR1_SE_MASK; /* Choose the external SAMPLE clock. */
  164. }
  165. else
  166. {
  167. base->CR1 &= ~CMP_CR1_SE_MASK; /* Choose the internal divided bus clock. */
  168. }
  169. #endif /* FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT */
  170. /* Set the filter count. */
  171. tmp8 = base->CR0 & ~CMP_CR0_FILTER_CNT_MASK;
  172. tmp8 |= CMP_CR0_FILTER_CNT(config->filterCount);
  173. base->CR0 = tmp8;
  174. /* Set the filter period. It is used as the divider to bus clock. */
  175. base->FPR = CMP_FPR_FILT_PER(config->filterPeriod);
  176. }
  177. void CMP_SetDACConfig(CMP_Type *base, const cmp_dac_config_t *config)
  178. {
  179. uint8_t tmp8 = 0U;
  180. if (NULL == config)
  181. {
  182. /* Passing "NULL" as input parameter means no available configuration. So the DAC feature is disabled.*/
  183. base->DACCR = 0U;
  184. return;
  185. }
  186. /* CMPx_DACCR. */
  187. tmp8 |= CMP_DACCR_DACEN_MASK; /* Enable the internal DAC. */
  188. if (kCMP_VrefSourceVin2 == config->referenceVoltageSource)
  189. {
  190. tmp8 |= CMP_DACCR_VRSEL_MASK;
  191. }
  192. tmp8 |= CMP_DACCR_VOSEL(config->DACValue);
  193. base->DACCR = tmp8;
  194. }
  195. void CMP_EnableInterrupts(CMP_Type *base, uint32_t mask)
  196. {
  197. uint8_t tmp8 = base->SCR & ~(CMP_SCR_CFR_MASK | CMP_SCR_CFF_MASK); /* To avoid change the w1c bits. */
  198. if (0U != (kCMP_OutputRisingInterruptEnable & mask))
  199. {
  200. tmp8 |= CMP_SCR_IER_MASK;
  201. }
  202. if (0U != (kCMP_OutputFallingInterruptEnable & mask))
  203. {
  204. tmp8 |= CMP_SCR_IEF_MASK;
  205. }
  206. base->SCR = tmp8;
  207. }
  208. void CMP_DisableInterrupts(CMP_Type *base, uint32_t mask)
  209. {
  210. uint8_t tmp8 = base->SCR & ~(CMP_SCR_CFR_MASK | CMP_SCR_CFF_MASK); /* To avoid change the w1c bits. */
  211. if (0U != (kCMP_OutputRisingInterruptEnable & mask))
  212. {
  213. tmp8 &= ~CMP_SCR_IER_MASK;
  214. }
  215. if (0U != (kCMP_OutputFallingInterruptEnable & mask))
  216. {
  217. tmp8 &= ~CMP_SCR_IEF_MASK;
  218. }
  219. base->SCR = tmp8;
  220. }
  221. uint32_t CMP_GetStatusFlags(CMP_Type *base)
  222. {
  223. uint32_t ret32 = 0U;
  224. if (0U != (CMP_SCR_CFR_MASK & base->SCR))
  225. {
  226. ret32 |= kCMP_OutputRisingEventFlag;
  227. }
  228. if (0U != (CMP_SCR_CFF_MASK & base->SCR))
  229. {
  230. ret32 |= kCMP_OutputFallingEventFlag;
  231. }
  232. if (0U != (CMP_SCR_COUT_MASK & base->SCR))
  233. {
  234. ret32 |= kCMP_OutputAssertEventFlag;
  235. }
  236. return ret32;
  237. }
  238. void CMP_ClearStatusFlags(CMP_Type *base, uint32_t mask)
  239. {
  240. uint8_t tmp8 = base->SCR & ~(CMP_SCR_CFR_MASK | CMP_SCR_CFF_MASK); /* To avoid change the w1c bits. */
  241. if (0U != (kCMP_OutputRisingEventFlag & mask))
  242. {
  243. tmp8 |= CMP_SCR_CFR_MASK;
  244. }
  245. if (0U != (kCMP_OutputFallingEventFlag & mask))
  246. {
  247. tmp8 |= CMP_SCR_CFF_MASK;
  248. }
  249. base->SCR = tmp8;
  250. }