fsl_cmp.h 11 KB

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