fsl_cmp.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347
  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. #ifndef _FSL_CMP_H_
  35. #define _FSL_CMP_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup cmp
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief CMP driver version 2.0.0. */
  47. #define FSL_CMP_DRIVER_VERSION (MAKE_VERSION(2, 0, 0))
  48. /*@}*/
  49. /*!
  50. * @brief Interrupt enable/disable mask.
  51. */
  52. enum _cmp_interrupt_enable
  53. {
  54. kCMP_OutputRisingInterruptEnable = CMP_SCR_IER_MASK, /*!< Comparator interrupt enable rising. */
  55. kCMP_OutputFallingInterruptEnable = CMP_SCR_IEF_MASK, /*!< Comparator interrupt enable falling. */
  56. };
  57. /*!
  58. * @brief Status flags' mask.
  59. */
  60. enum _cmp_status_flags
  61. {
  62. kCMP_OutputRisingEventFlag = CMP_SCR_CFR_MASK, /*!< Rising-edge on the comparison output has occurred. */
  63. kCMP_OutputFallingEventFlag = CMP_SCR_CFF_MASK, /*!< Falling-edge on the comparison output has occurred. */
  64. kCMP_OutputAssertEventFlag = CMP_SCR_COUT_MASK, /*!< Return the current value of the analog comparator output. */
  65. };
  66. /*!
  67. * @brief CMP Hysteresis mode.
  68. */
  69. typedef enum _cmp_hysteresis_mode
  70. {
  71. kCMP_HysteresisLevel0 = 0U, /*!< Hysteresis level 0. */
  72. kCMP_HysteresisLevel1 = 1U, /*!< Hysteresis level 1. */
  73. kCMP_HysteresisLevel2 = 2U, /*!< Hysteresis level 2. */
  74. kCMP_HysteresisLevel3 = 3U, /*!< Hysteresis level 3. */
  75. } cmp_hysteresis_mode_t;
  76. /*!
  77. * @brief CMP Voltage Reference source.
  78. */
  79. typedef enum _cmp_reference_voltage_source
  80. {
  81. kCMP_VrefSourceVin1 = 0U, /*!< Vin1 is selected as a resistor ladder network supply reference Vin. */
  82. kCMP_VrefSourceVin2 = 1U, /*!< Vin2 is selected as a resistor ladder network supply reference Vin. */
  83. } cmp_reference_voltage_source_t;
  84. /*!
  85. * @brief Configures the comparator.
  86. */
  87. typedef struct _cmp_config
  88. {
  89. bool enableCmp; /*!< Enable the CMP module. */
  90. cmp_hysteresis_mode_t hysteresisMode; /*!< CMP Hysteresis mode. */
  91. bool enableHighSpeed; /*!< Enable High-speed (HS) comparison mode. */
  92. bool enableInvertOutput; /*!< Enable the inverted comparator output. */
  93. bool useUnfilteredOutput; /*!< Set the compare output(COUT) to equal COUTA(true) or COUT(false). */
  94. bool enablePinOut; /*!< The comparator output is available on the associated pin. */
  95. #if defined(FSL_FEATURE_CMP_HAS_TRIGGER_MODE) && FSL_FEATURE_CMP_HAS_TRIGGER_MODE
  96. bool enableTriggerMode; /*!< Enable the trigger mode. */
  97. #endif /* FSL_FEATURE_CMP_HAS_TRIGGER_MODE */
  98. } cmp_config_t;
  99. /*!
  100. * @brief Configures the filter.
  101. */
  102. typedef struct _cmp_filter_config
  103. {
  104. #if defined(FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT) && FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT
  105. bool enableSample; /*!< Using the external SAMPLE as a sampling clock input or using a divided bus clock. */
  106. #endif /* FSL_FEATURE_CMP_HAS_EXTERNAL_SAMPLE_SUPPORT */
  107. uint8_t filterCount; /*!< Filter Sample Count. Available range is 1-7; 0 disables the filter.*/
  108. uint8_t filterPeriod; /*!< Filter Sample Period. The divider to the bus clock. Available range is 0-255. */
  109. } cmp_filter_config_t;
  110. /*!
  111. * @brief Configures the internal DAC.
  112. */
  113. typedef struct _cmp_dac_config
  114. {
  115. cmp_reference_voltage_source_t referenceVoltageSource; /*!< Supply voltage reference source. */
  116. uint8_t DACValue; /*!< Value for the DAC Output Voltage. Available range is 0-63.*/
  117. } cmp_dac_config_t;
  118. #if defined(__cplusplus)
  119. extern "C" {
  120. #endif
  121. /*******************************************************************************
  122. * API
  123. ******************************************************************************/
  124. /*!
  125. * @name Initialization
  126. * @{
  127. */
  128. /*!
  129. * @brief Initializes the CMP.
  130. *
  131. * This function initializes the CMP module. The operations included are as follows.
  132. * - Enabling the clock for CMP module.
  133. * - Configuring the comparator.
  134. * - Enabling the CMP module.
  135. * Note that for some devices, multiple CMP instances share the same clock gate. In this case, to enable the clock for
  136. * any instance enables all CMPs. See the appropriate MCU reference manual for the clock assignment of the CMP.
  137. *
  138. * @param base CMP peripheral base address.
  139. * @param config Pointer to the configuration structure.
  140. */
  141. void CMP_Init(CMP_Type *base, const cmp_config_t *config);
  142. /*!
  143. * @brief De-initializes the CMP module.
  144. *
  145. * This function de-initializes the CMP module. The operations included are as follows.
  146. * - Disabling the CMP module.
  147. * - Disabling the clock for CMP module.
  148. *
  149. * This function disables the clock for the CMP.
  150. * Note that for some devices, multiple CMP instances share the same clock gate. In this case, before disabling the
  151. * clock for the CMP, ensure that all the CMP instances are not used.
  152. *
  153. * @param base CMP peripheral base address.
  154. */
  155. void CMP_Deinit(CMP_Type *base);
  156. /*!
  157. * @brief Enables/disables the CMP module.
  158. *
  159. * @param base CMP peripheral base address.
  160. * @param enable Enables or disables the module.
  161. */
  162. static inline void CMP_Enable(CMP_Type *base, bool enable)
  163. {
  164. if (enable)
  165. {
  166. base->CR1 |= CMP_CR1_EN_MASK;
  167. }
  168. else
  169. {
  170. base->CR1 &= ~CMP_CR1_EN_MASK;
  171. }
  172. }
  173. /*!
  174. * @brief Initializes the CMP user configuration structure.
  175. *
  176. * This function initializes the user configuration structure to these default values.
  177. * @code
  178. * config->enableCmp = true;
  179. * config->hysteresisMode = kCMP_HysteresisLevel0;
  180. * config->enableHighSpeed = false;
  181. * config->enableInvertOutput = false;
  182. * config->useUnfilteredOutput = false;
  183. * config->enablePinOut = false;
  184. * config->enableTriggerMode = false;
  185. * @endcode
  186. * @param config Pointer to the configuration structure.
  187. */
  188. void CMP_GetDefaultConfig(cmp_config_t *config);
  189. /*!
  190. * @brief Sets the input channels for the comparator.
  191. *
  192. * This function sets the input channels for the comparator.
  193. * Note that two input channels cannot be set the same way in the application. When the user selects the same input
  194. * from the analog mux to the positive and negative port, the comparator is disabled automatically.
  195. *
  196. * @param base CMP peripheral base address.
  197. * @param positiveChannel Positive side input channel number. Available range is 0-7.
  198. * @param negativeChannel Negative side input channel number. Available range is 0-7.
  199. */
  200. void CMP_SetInputChannels(CMP_Type *base, uint8_t positiveChannel, uint8_t negativeChannel);
  201. /* @} */
  202. /*!
  203. * @name Advanced Features
  204. * @{
  205. */
  206. #if defined(FSL_FEATURE_CMP_HAS_DMA) && FSL_FEATURE_CMP_HAS_DMA
  207. /*!
  208. * @brief Enables/disables the DMA request for rising/falling events.
  209. *
  210. * This function enables/disables the DMA request for rising/falling events. Either event triggers the generation of
  211. * the DMA request from CMP if the DMA feature is enabled. Both events are ignored for generating the DMA request from the CMP
  212. * if the DMA is disabled.
  213. *
  214. * @param base CMP peripheral base address.
  215. * @param enable Enables or disables the feature.
  216. */
  217. void CMP_EnableDMA(CMP_Type *base, bool enable);
  218. #endif /* FSL_FEATURE_CMP_HAS_DMA */
  219. #if defined(FSL_FEATURE_CMP_HAS_WINDOW_MODE) && FSL_FEATURE_CMP_HAS_WINDOW_MODE
  220. /*!
  221. * @brief Enables/disables the window mode.
  222. *
  223. * @param base CMP peripheral base address.
  224. * @param enable Enables or disables the feature.
  225. */
  226. static inline void CMP_EnableWindowMode(CMP_Type *base, bool enable)
  227. {
  228. if (enable)
  229. {
  230. base->CR1 |= CMP_CR1_WE_MASK;
  231. }
  232. else
  233. {
  234. base->CR1 &= ~CMP_CR1_WE_MASK;
  235. }
  236. }
  237. #endif /* FSL_FEATURE_CMP_HAS_WINDOW_MODE */
  238. #if defined(FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE) && FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE
  239. /*!
  240. * @brief Enables/disables the pass through mode.
  241. *
  242. * @param base CMP peripheral base address.
  243. * @param enable Enables or disables the feature.
  244. */
  245. static inline void CMP_EnablePassThroughMode(CMP_Type *base, bool enable)
  246. {
  247. if (enable)
  248. {
  249. base->MUXCR |= CMP_MUXCR_PSTM_MASK;
  250. }
  251. else
  252. {
  253. base->MUXCR &= ~CMP_MUXCR_PSTM_MASK;
  254. }
  255. }
  256. #endif /* FSL_FEATURE_CMP_HAS_PASS_THROUGH_MODE */
  257. /*!
  258. * @brief Configures the filter.
  259. *
  260. * @param base CMP peripheral base address.
  261. * @param config Pointer to the configuration structure.
  262. */
  263. void CMP_SetFilterConfig(CMP_Type *base, const cmp_filter_config_t *config);
  264. /*!
  265. * @brief Configures the internal DAC.
  266. *
  267. * @param base CMP peripheral base address.
  268. * @param config Pointer to the configuration structure. "NULL" disables the feature.
  269. */
  270. void CMP_SetDACConfig(CMP_Type *base, const cmp_dac_config_t *config);
  271. /*!
  272. * @brief Enables the interrupts.
  273. *
  274. * @param base CMP peripheral base address.
  275. * @param mask Mask value for interrupts. See "_cmp_interrupt_enable".
  276. */
  277. void CMP_EnableInterrupts(CMP_Type *base, uint32_t mask);
  278. /*!
  279. * @brief Disables the interrupts.
  280. *
  281. * @param base CMP peripheral base address.
  282. * @param mask Mask value for interrupts. See "_cmp_interrupt_enable".
  283. */
  284. void CMP_DisableInterrupts(CMP_Type *base, uint32_t mask);
  285. /* @} */
  286. /*!
  287. * @name Results
  288. * @{
  289. */
  290. /*!
  291. * @brief Gets the status flags.
  292. *
  293. * @param base CMP peripheral base address.
  294. *
  295. * @return Mask value for the asserted flags. See "_cmp_status_flags".
  296. */
  297. uint32_t CMP_GetStatusFlags(CMP_Type *base);
  298. /*!
  299. * @brief Clears the status flags.
  300. *
  301. * @param base CMP peripheral base address.
  302. * @param mask Mask value for the flags. See "_cmp_status_flags".
  303. */
  304. void CMP_ClearStatusFlags(CMP_Type *base, uint32_t mask);
  305. /* @} */
  306. #if defined(__cplusplus)
  307. }
  308. #endif
  309. /*!
  310. * @}
  311. */
  312. #endif /* _FSL_CMP_H_ */