fsl_adc_etc.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342
  1. /*
  2. * The Clear BSD License
  3. * Copyright (c) 2016, 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_ADC_ETC_H_
  35. #define _FSL_ADC_ETC_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup adc_etc
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @brief ADC_ETC driver version */
  45. #define FSL_ADC_ETC_DRIVER_VERSION (MAKE_VERSION(2, 0, 0)) /*!< Version 2.0.0. */
  46. /*! @brief The mask of status flags cleared by writing 1. */
  47. #define ADC_ETC_DMA_CTRL_TRGn_REQ_MASK 0xFF0000U
  48. /*!
  49. * @brief ADC_ETC customized status flags mask.
  50. */
  51. enum _adc_etc_status_flag_mask
  52. {
  53. kADC_ETC_Done0StatusFlagMask = 1U,
  54. kADC_ETC_Done1StatusFlagMask = 2U,
  55. kADC_ETC_Done2StatusFlagMask = 4U,
  56. kADC_ETC_ErrorStatusFlagMask = 8U,
  57. };
  58. /*!
  59. * @brief External triggers sources.
  60. */
  61. typedef enum _adc_etc_external_trigger_source
  62. {
  63. /* External XBAR sources. Support HW or SW mode. */
  64. kADC_ETC_Trg0TriggerSource = 0U, /* External XBAR trigger0 source. */
  65. kADC_ETC_Trg1TriggerSource = 1U, /* External XBAR trigger1 source. */
  66. kADC_ETC_Trg2TriggerSource = 2U, /* External XBAR trigger2 source. */
  67. kADC_ETC_Trg3TriggerSource = 3U, /* External XBAR trigger3 source. */
  68. kADC_ETC_Trg4TriggerSource = 4U, /* External XBAR trigger4 source. */
  69. kADC_ETC_Trg5TriggerSource = 5U, /* External XBAR trigger5 source. */
  70. kADC_ETC_Trg6TriggerSource = 6U, /* External XBAR trigger6 source. */
  71. kADC_ETC_Trg7TriggerSource = 7U, /* External XBAR trigger7 source. */
  72. /* External TSC sources. Only support HW mode. */
  73. kADC_ETC_TSC0TriggerSource = 8U, /* External TSC trigger0 source. */
  74. kADC_ETC_TSC1TriggerSource = 9U, /* External TSC trigger1 source. */
  75. } adc_etc_external_trigger_source_t;
  76. /*!
  77. * @brief Interrupt enable/disable mask.
  78. */
  79. typedef enum _adc_etc_interrupt_enable
  80. {
  81. kADC_ETC_InterruptDisable = 0U, /* Disable the ADC_ETC interrupt. */
  82. kADC_ETC_Done0InterruptEnable = 1U, /* Enable the DONE0 interrupt when ADC conversions complete. */
  83. kADC_ETC_Done1InterruptEnable = 2U, /* Enable the DONE1 interrupt when ADC conversions complete. */
  84. kADC_ETC_Done2InterruptEnable = 3U, /* Enable the DONE2 interrupt when ADC conversions complete. */
  85. } adc_etc_interrupt_enable_t;
  86. #if defined(FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL) && FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL
  87. /*!
  88. * @brief DMA mode selection.
  89. */
  90. typedef enum _adc_etc_dma_mode_selection
  91. {
  92. kADC_ETC_TrigDMAWithLatchedSignal = 0U, /* Trig DMA_REQ with latched signal, REQ will be cleared when ACK and source request cleared. */
  93. kADC_ETC_TrigDMAWithPulsedSignal = 1U, /* Trig DMA_REQ with pulsed signal, REQ will be cleared by ACK only. */
  94. } adc_etc_dma_mode_selection_t;
  95. #endif /*FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL*/
  96. /*!
  97. * @brief ADC_ETC configuration.
  98. */
  99. typedef struct _adc_etc_config
  100. {
  101. bool enableTSCBypass; /* If bypass TSC, TSC would trigger ADC directly.
  102. Otherwise TSC would trigger ADC through ADC_ETC. */
  103. bool enableTSC0Trigger; /* Enable external TSC0 trigger. It is valid when enableTSCBypass = false. */
  104. bool enableTSC1Trigger; /* Enable external TSC1 trigger. It is valid when enableTSCBypass = false.*/
  105. #if defined(FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL) && FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL
  106. adc_etc_dma_mode_selection_t dmaMode; /* Select the ADC_ETC DMA mode. */
  107. #endif /*FSL_FEATURE_ADC_ETC_HAS_CTRL_DMA_MODE_SEL*/
  108. uint32_t TSC0triggerPriority; /* External TSC0 trigger priority, 7 is highest, 0 is lowest. */
  109. uint32_t TSC1triggerPriority; /* External TSC1 trigger priority, 7 is highest, 0 is lowest. */
  110. uint32_t clockPreDivider; /* Pre-divider for trig delay and interval. Available range is 0-255.
  111. Clock would be divided by (clockPreDivider+1). */
  112. uint32_t XBARtriggerMask; /* Enable the corresponding trigger source. Available range is trigger0:0x01 to
  113. trigger7:0x80
  114. For example, XBARtriggerMask = 0x7U, which means trigger0, trigger1 and trigger2 is
  115. enabled. */
  116. } adc_etc_config_t;
  117. /*!
  118. * @brief ADC_ETC trigger chain configuration.
  119. */
  120. typedef struct _adc_etc_trigger_chain_config
  121. {
  122. bool enableB2BMode; /* Enable ADC_ETC BackToBack mode. when not enabled B2B mode,
  123. wait until interval delay is reached. */
  124. uint32_t ADCHCRegisterSelect; /* Select relevant ADC_HCx register to trigger. 1U : HC0, 2U: HC1, 4U: HC2 ... */
  125. uint32_t ADCChannelSelect; /* Select ADC sample channel. */
  126. adc_etc_interrupt_enable_t InterruptEnable; /* Enable/disable Interrupt. */
  127. } adc_etc_trigger_chain_config_t;
  128. /*!
  129. * @brief ADC_ETC trigger configuration.
  130. */
  131. typedef struct _adc_etc_trigger_config
  132. {
  133. bool enableSyncMode; /* Enable the sync Mode, In SyncMode ADC1 and ADC2 are controlled by the same trigger source.
  134. In AsyncMode ADC1 and ADC2 are controlled by separate trigger source. */
  135. bool enableSWTriggerMode; /* Enable the sofware trigger mode. */
  136. uint32_t triggerChainLength; /* TRIG chain length to the ADC. 0: Trig length is 1. ... 7: Trig length is 8. */
  137. uint32_t triggerPriority; /* External trigger priority, 7 is highest, 0 is lowest. */
  138. uint32_t sampleIntervalDelay; /* Set sampling interval delay. */
  139. uint32_t initialDelay; /* Set trigger initial delay. */
  140. } adc_etc_trigger_config_t;
  141. /*******************************************************************************
  142. * API
  143. ******************************************************************************/
  144. #if defined(__cplusplus)
  145. extern "C" {
  146. #endif
  147. /*!
  148. * @name Initialization
  149. * @{
  150. */
  151. /*!
  152. * @brief Initialize the ADC_ETC module.
  153. *
  154. * @param base ADC_ETC peripheral base address.
  155. * @param config Pointer to "adc_etc_config_t" structure.
  156. */
  157. void ADC_ETC_Init(ADC_ETC_Type *base, const adc_etc_config_t *config);
  158. /*!
  159. * @brief De-Initialize the ADC_ETC module.
  160. *
  161. * @param base ADC_ETC peripheral base address.
  162. */
  163. void ADC_ETC_Deinit(ADC_ETC_Type *base);
  164. /*!
  165. * @brief Gets an available pre-defined settings for the ADC_ETC's configuration.
  166. * This function initializes the ADC_ETC's configuration structure with available settings. The default values are:
  167. * @code
  168. * config->enableTSCBypass = true;
  169. * config->enableTSC0Trigger = false;
  170. * config->enableTSC1Trigger = false;
  171. * config->TSC0triggerPriority = 0U;
  172. * config->TSC1triggerPriority = 0U;
  173. * config->clockPreDivider = 0U;
  174. * config->XBARtriggerMask = 0U;
  175. * @endCode
  176. *
  177. * @param config Pointer to "adc_etc_config_t" structure.
  178. */
  179. void ADC_ETC_GetDefaultConfig(adc_etc_config_t *config);
  180. /*!
  181. * @brief Set the external XBAR trigger configuration.
  182. *
  183. * @param base ADC_ETC peripheral base address.
  184. * @param triggerGroup Trigger group index.
  185. * @param config Pointer to "adc_etc_trigger_config_t" structure.
  186. */
  187. void ADC_ETC_SetTriggerConfig(ADC_ETC_Type *base, uint32_t triggerGroup, const adc_etc_trigger_config_t *config);
  188. /*!
  189. * @brief Set the external XBAR trigger chain configuration.
  190. * For example, if triggerGroup is set to 0U and chainGroup is set to 1U, which means Trigger0 source's chain1 would be
  191. * configurated.
  192. *
  193. * @param base ADC_ETC peripheral base address.
  194. * @param triggerGroup Trigger group index. Available number is 0~7.
  195. * @param chainGroup Trigger chain group index. Available number is 0~7.
  196. * @param config Pointer to "adc_etc_trigger_chain_config_t" structure.
  197. */
  198. void ADC_ETC_SetTriggerChainConfig(ADC_ETC_Type *base,
  199. uint32_t triggerGroup,
  200. uint32_t chainGroup,
  201. const adc_etc_trigger_chain_config_t *config);
  202. /*!
  203. * @brief Gets the interrupt status flags of external XBAR and TSC triggers.
  204. *
  205. * @param base ADC_ETC peripheral base address.
  206. * @param sourceIndex trigger source index.
  207. *
  208. * @return Status flags mask of trigger. Refer to "_adc_etc_status_flag_mask".
  209. */
  210. uint32_t ADC_ETC_GetInterruptStatusFlags(ADC_ETC_Type *base, adc_etc_external_trigger_source_t sourceIndex);
  211. /*!
  212. * @brief Clears the ADC_ETC's interrupt status falgs.
  213. *
  214. * @param base ADC_ETC peripheral base address.
  215. * @param sourceIndex trigger source index.
  216. * @param mask Status flags mask of trigger. Refer to "_adc_etc_status_flag_mask".
  217. */
  218. void ADC_ETC_ClearInterruptStatusFlags(ADC_ETC_Type *base,
  219. adc_etc_external_trigger_source_t sourceIndex,
  220. uint32_t mask);
  221. /*!
  222. * @brief Enable the DMA corresponding to each trigger source.
  223. *
  224. * @param base ADC_ETC peripheral base address.
  225. * @param triggerGroup Trigger group index. Available number is 0~7.
  226. */
  227. static inline void ADC_ETC_EnableDMA(ADC_ETC_Type *base, uint32_t triggerGroup)
  228. {
  229. /* Avoid clearing status flags at the same time. */
  230. base->DMA_CTRL =
  231. (base->DMA_CTRL | (ADC_ETC_DMA_CTRL_TRIG0_ENABLE_MASK << triggerGroup)) & ~ADC_ETC_DMA_CTRL_TRGn_REQ_MASK;
  232. }
  233. /*!
  234. * @brief Disable the DMA corresponding to each trigger sources.
  235. *
  236. * @param base ADC_ETC peripheral base address.
  237. * @param triggerGroup Trigger group index. Available number is 0~7.
  238. */
  239. static inline void ADC_ETC_DisableDMA(ADC_ETC_Type *base, uint32_t triggerGroup)
  240. {
  241. /* Avoid clearing status flags at the same time. */
  242. base->DMA_CTRL =
  243. (base->DMA_CTRL & ~(ADC_ETC_DMA_CTRL_TRIG0_ENABLE_MASK << triggerGroup)) & ~ADC_ETC_DMA_CTRL_TRGn_REQ_MASK;
  244. }
  245. /*!
  246. * @brief Get the DMA request status falgs. Only external XBAR sources support DMA request.
  247. *
  248. * @param base ADC_ETC peripheral base address.
  249. * @return Mask of external XBAR tirgger's DMA request asserted flags. Available range is trigger0:0x01 to
  250. * trigger7:0x80.
  251. */
  252. static inline uint32_t ADC_ETC_GetDMAStatusFlags(ADC_ETC_Type *base)
  253. {
  254. return (((base->DMA_CTRL) & ADC_ETC_DMA_CTRL_TRGn_REQ_MASK) >> ADC_ETC_DMA_CTRL_TRIG0_REQ_SHIFT);
  255. }
  256. /*!
  257. * @brief Clear the DMA request status falgs. Only external XBAR sources support DMA request.
  258. *
  259. * @param base ADC_ETC peripheral base address.
  260. * @param mask Mask of external XBAR tirgger's DMA request asserted flags. Available range is trigger0:0x01 to
  261. * trigger7:0x80.
  262. */
  263. static inline void ADC_ETC_ClearDMAStatusFlags(ADC_ETC_Type *base, uint32_t mask)
  264. {
  265. base->DMA_CTRL = ((base->DMA_CTRL) & ~ADC_ETC_DMA_CTRL_TRGn_REQ_MASK) | (mask << ADC_ETC_DMA_CTRL_TRIG0_REQ_SHIFT);
  266. }
  267. /*!
  268. * @brief When enable ,all logical will be reset.
  269. *
  270. * @param base ADC_ETC peripheral base address.
  271. * @param enable Enable/Disable the software reset.
  272. */
  273. static inline void ADC_ETC_DoSoftwareReset(ADC_ETC_Type *base, bool enable)
  274. {
  275. if (enable)
  276. {
  277. base->CTRL |= ADC_ETC_CTRL_SOFTRST_MASK;
  278. }
  279. else
  280. {
  281. base->CTRL &= ~ADC_ETC_CTRL_SOFTRST_MASK;
  282. }
  283. }
  284. /*!
  285. * @brief Do software trigger corresponding to each XBAR trigger sources.
  286. * Each XBAR trigger sources can be configured as HW or SW trigger mode. In hardware trigger mode,
  287. * trigger source is from XBAR. In software mode, trigger source is from software tigger. TSC trigger sources
  288. * can only work in hardware trigger mode.
  289. *
  290. * @param base ADC_ETC peripheral base address.
  291. * @param triggerGroup Trigger group index. Available number is 0~7.
  292. */
  293. static inline void ADC_ETC_DoSoftwareTrigger(ADC_ETC_Type *base, uint32_t triggerGroup)
  294. {
  295. assert(triggerGroup < ADC_ETC_TRIGn_CTRL_COUNT);
  296. base->TRIG[triggerGroup].TRIGn_CTRL |= ADC_ETC_TRIGn_CTRL_SW_TRIG_MASK;
  297. }
  298. /*!
  299. * @brief Get ADC conversion result from external XBAR sources.
  300. * For example, if triggerGroup is set to 0U and chainGroup is set to 1U, which means the API would
  301. * return Trigger0 source's chain1 conversion result.
  302. *
  303. * @param base ADC_ETC peripheral base address.
  304. * @param triggerGroup Trigger group index. Available number is 0~7.
  305. * @param chainGroup Trigger chain group index. Available number is 0~7.
  306. * @return ADC conversion result value.
  307. */
  308. uint32_t ADC_ETC_GetADCConversionValue(ADC_ETC_Type *base, uint32_t triggerGroup, uint32_t chainGroup);
  309. #if defined(__cplusplus)
  310. }
  311. #endif
  312. #endif /* _FSL_ADC_ETC_H_ */