You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 

322 line
9.7 KiB

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