fsl_dac.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  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_DAC_H_
  35. #define _FSL_DAC_H_
  36. #include "fsl_common.h"
  37. /*!
  38. * @addtogroup dac
  39. * @{
  40. */
  41. /*******************************************************************************
  42. * Definitions
  43. ******************************************************************************/
  44. /*! @name Driver version */
  45. /*@{*/
  46. /*! @brief DAC driver version 2.0.1. */
  47. #define FSL_DAC_DRIVER_VERSION (MAKE_VERSION(2, 0, 1))
  48. /*@}*/
  49. /*!
  50. * @brief DAC buffer flags.
  51. */
  52. enum _dac_buffer_status_flags
  53. {
  54. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  55. kDAC_BufferWatermarkFlag = DAC_SR_DACBFWMF_MASK, /*!< DAC Buffer Watermark Flag. */
  56. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  57. kDAC_BufferReadPointerTopPositionFlag = DAC_SR_DACBFRPTF_MASK, /*!< DAC Buffer Read Pointer Top Position Flag. */
  58. kDAC_BufferReadPointerBottomPositionFlag = DAC_SR_DACBFRPBF_MASK, /*!< DAC Buffer Read Pointer Bottom Position
  59. Flag. */
  60. };
  61. /*!
  62. * @brief DAC buffer interrupts.
  63. */
  64. enum _dac_buffer_interrupt_enable
  65. {
  66. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  67. kDAC_BufferWatermarkInterruptEnable = DAC_C0_DACBWIEN_MASK, /*!< DAC Buffer Watermark Interrupt Enable. */
  68. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  69. kDAC_BufferReadPointerTopInterruptEnable = DAC_C0_DACBTIEN_MASK, /*!< DAC Buffer Read Pointer Top Flag Interrupt
  70. Enable. */
  71. kDAC_BufferReadPointerBottomInterruptEnable = DAC_C0_DACBBIEN_MASK, /*!< DAC Buffer Read Pointer Bottom Flag
  72. Interrupt Enable */
  73. };
  74. /*!
  75. * @brief DAC reference voltage source.
  76. */
  77. typedef enum _dac_reference_voltage_source
  78. {
  79. kDAC_ReferenceVoltageSourceVref1 = 0U, /*!< The DAC selects DACREF_1 as the reference voltage. */
  80. kDAC_ReferenceVoltageSourceVref2 = 1U, /*!< The DAC selects DACREF_2 as the reference voltage. */
  81. } dac_reference_voltage_source_t;
  82. /*!
  83. * @brief DAC buffer trigger mode.
  84. */
  85. typedef enum _dac_buffer_trigger_mode
  86. {
  87. kDAC_BufferTriggerByHardwareMode = 0U, /*!< The DAC hardware trigger is selected. */
  88. kDAC_BufferTriggerBySoftwareMode = 1U, /*!< The DAC software trigger is selected. */
  89. } dac_buffer_trigger_mode_t;
  90. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  91. /*!
  92. * @brief DAC buffer watermark.
  93. */
  94. typedef enum _dac_buffer_watermark
  95. {
  96. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD) && FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD
  97. kDAC_BufferWatermark1Word = 0U, /*!< 1 word away from the upper limit. */
  98. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_1_WORD */
  99. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS
  100. kDAC_BufferWatermark2Word = 1U, /*!< 2 words away from the upper limit. */
  101. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_2_WORDS */
  102. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS
  103. kDAC_BufferWatermark3Word = 2U, /*!< 3 words away from the upper limit. */
  104. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_3_WORDS */
  105. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS) && FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS
  106. kDAC_BufferWatermark4Word = 3U, /*!< 4 words away from the upper limit. */
  107. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_4_WORDS */
  108. } dac_buffer_watermark_t;
  109. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  110. /*!
  111. * @brief DAC buffer work mode.
  112. */
  113. typedef enum _dac_buffer_work_mode
  114. {
  115. kDAC_BufferWorkAsNormalMode = 0U, /*!< Normal mode. */
  116. #if defined(FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE) && FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE
  117. kDAC_BufferWorkAsSwingMode, /*!< Swing mode. */
  118. #endif /* FSL_FEATURE_DAC_HAS_BUFFER_SWING_MODE */
  119. kDAC_BufferWorkAsOneTimeScanMode, /*!< One-Time Scan mode. */
  120. #if defined(FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE) && FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE
  121. kDAC_BufferWorkAsFIFOMode, /*!< FIFO mode. */
  122. #endif /* FSL_FEATURE_DAC_HAS_BUFFER_FIFO_MODE */
  123. } dac_buffer_work_mode_t;
  124. /*!
  125. * @brief DAC module configuration.
  126. */
  127. typedef struct _dac_config
  128. {
  129. dac_reference_voltage_source_t referenceVoltageSource; /*!< Select the DAC reference voltage source. */
  130. bool enableLowPowerMode; /*!< Enable the low-power mode. */
  131. } dac_config_t;
  132. /*!
  133. * @brief DAC buffer configuration.
  134. */
  135. typedef struct _dac_buffer_config
  136. {
  137. dac_buffer_trigger_mode_t triggerMode; /*!< Select the buffer's trigger mode. */
  138. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  139. dac_buffer_watermark_t watermark; /*!< Select the buffer's watermark. */
  140. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  141. dac_buffer_work_mode_t workMode; /*!< Select the buffer's work mode. */
  142. uint8_t upperLimit; /*!< Set the upper limit for the buffer index.
  143. Normally, 0-15 is available for a buffer with 16 items. */
  144. } dac_buffer_config_t;
  145. /*******************************************************************************
  146. * API
  147. ******************************************************************************/
  148. #if defined(__cplusplus)
  149. extern "C" {
  150. #endif
  151. /*!
  152. * @name Initialization
  153. * @{
  154. */
  155. /*!
  156. * @brief Initializes the DAC module.
  157. *
  158. * This function initializes the DAC module including the following operations.
  159. * - Enabling the clock for DAC module.
  160. * - Configuring the DAC converter with a user configuration.
  161. * - Enabling the DAC module.
  162. *
  163. * @param base DAC peripheral base address.
  164. * @param config Pointer to the configuration structure. See "dac_config_t".
  165. */
  166. void DAC_Init(DAC_Type *base, const dac_config_t *config);
  167. /*!
  168. * @brief De-initializes the DAC module.
  169. *
  170. * This function de-initializes the DAC module including the following operations.
  171. * - Disabling the DAC module.
  172. * - Disabling the clock for the DAC module.
  173. *
  174. * @param base DAC peripheral base address.
  175. */
  176. void DAC_Deinit(DAC_Type *base);
  177. /*!
  178. * @brief Initializes the DAC user configuration structure.
  179. *
  180. * This function initializes the user configuration structure to a default value. The default values are as follows.
  181. * @code
  182. * config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
  183. * config->enableLowPowerMode = false;
  184. * @endcode
  185. * @param config Pointer to the configuration structure. See "dac_config_t".
  186. */
  187. void DAC_GetDefaultConfig(dac_config_t *config);
  188. /*!
  189. * @brief Enables the DAC module.
  190. *
  191. * @param base DAC peripheral base address.
  192. * @param enable Enables or disables the feature.
  193. */
  194. static inline void DAC_Enable(DAC_Type *base, bool enable)
  195. {
  196. if (enable)
  197. {
  198. base->C0 |= DAC_C0_DACEN_MASK;
  199. }
  200. else
  201. {
  202. base->C0 &= ~DAC_C0_DACEN_MASK;
  203. }
  204. }
  205. /* @} */
  206. /*!
  207. * @name Buffer
  208. * @{
  209. */
  210. /*!
  211. * @brief Enables the DAC buffer.
  212. *
  213. * @param base DAC peripheral base address.
  214. * @param enable Enables or disables the feature.
  215. */
  216. static inline void DAC_EnableBuffer(DAC_Type *base, bool enable)
  217. {
  218. if (enable)
  219. {
  220. base->C1 |= DAC_C1_DACBFEN_MASK;
  221. }
  222. else
  223. {
  224. base->C1 &= ~DAC_C1_DACBFEN_MASK;
  225. }
  226. }
  227. /*!
  228. * @brief Configures the CMP buffer.
  229. *
  230. * @param base DAC peripheral base address.
  231. * @param config Pointer to the configuration structure. See "dac_buffer_config_t".
  232. */
  233. void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config);
  234. /*!
  235. * @brief Initializes the DAC buffer configuration structure.
  236. *
  237. * This function initializes the DAC buffer configuration structure to default values. The default values are as follows.
  238. * @code
  239. * config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
  240. * config->watermark = kDAC_BufferWatermark1Word;
  241. * config->workMode = kDAC_BufferWorkAsNormalMode;
  242. * config->upperLimit = DAC_DATL_COUNT - 1U;
  243. * @endcode
  244. * @param config Pointer to the configuration structure. See "dac_buffer_config_t".
  245. */
  246. void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config);
  247. /*!
  248. * @brief Enables the DMA for DAC buffer.
  249. *
  250. * @param base DAC peripheral base address.
  251. * @param enable Enables or disables the feature.
  252. */
  253. static inline void DAC_EnableBufferDMA(DAC_Type *base, bool enable)
  254. {
  255. if (enable)
  256. {
  257. base->C1 |= DAC_C1_DMAEN_MASK;
  258. }
  259. else
  260. {
  261. base->C1 &= ~DAC_C1_DMAEN_MASK;
  262. }
  263. }
  264. /*!
  265. * @brief Sets the value for items in the buffer.
  266. *
  267. * @param base DAC peripheral base address.
  268. * @param index Setting the index for items in the buffer. The available index should not exceed the size of the DAC buffer.
  269. * @param value Setting the value for items in the buffer. 12-bits are available.
  270. */
  271. void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value);
  272. /*!
  273. * @brief Triggers the buffer using software and updates the read pointer of the DAC buffer.
  274. *
  275. * This function triggers the function using software. The read pointer of the DAC buffer is updated with one step
  276. * after this function is called. Changing the read pointer depends on the buffer's work mode.
  277. *
  278. * @param base DAC peripheral base address.
  279. */
  280. static inline void DAC_DoSoftwareTriggerBuffer(DAC_Type *base)
  281. {
  282. base->C0 |= DAC_C0_DACSWTRG_MASK;
  283. }
  284. /*!
  285. * @brief Gets the current read pointer of the DAC buffer.
  286. *
  287. * This function gets the current read pointer of the DAC buffer.
  288. * The current output value depends on the item indexed by the read pointer. It is updated either
  289. * by a software trigger or a hardware trigger.
  290. *
  291. * @param base DAC peripheral base address.
  292. *
  293. * @return The current read pointer of the DAC buffer.
  294. */
  295. static inline uint8_t DAC_GetBufferReadPointer(DAC_Type *base)
  296. {
  297. return ((base->C2 & DAC_C2_DACBFRP_MASK) >> DAC_C2_DACBFRP_SHIFT);
  298. }
  299. /*!
  300. * @brief Sets the current read pointer of the DAC buffer.
  301. *
  302. * This function sets the current read pointer of the DAC buffer.
  303. * The current output value depends on the item indexed by the read pointer. It is updated either by a
  304. * software trigger or a hardware trigger. After the read pointer changes, the DAC output value also changes.
  305. *
  306. * @param base DAC peripheral base address.
  307. * @param index Setting an index value for the pointer.
  308. */
  309. void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index);
  310. /*!
  311. * @brief Enables interrupts for the DAC buffer.
  312. *
  313. * @param base DAC peripheral base address.
  314. * @param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
  315. */
  316. void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask);
  317. /*!
  318. * @brief Disables interrupts for the DAC buffer.
  319. *
  320. * @param base DAC peripheral base address.
  321. * @param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
  322. */
  323. void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask);
  324. /*!
  325. * @brief Gets the flags of events for the DAC buffer.
  326. *
  327. * @param base DAC peripheral base address.
  328. *
  329. * @return Mask value for the asserted flags. See "_dac_buffer_status_flags".
  330. */
  331. uint32_t DAC_GetBufferStatusFlags(DAC_Type *base);
  332. /*!
  333. * @brief Clears the flags of events for the DAC buffer.
  334. *
  335. * @param base DAC peripheral base address.
  336. * @param mask Mask value for flags. See "_dac_buffer_status_flags_t".
  337. */
  338. void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask);
  339. /* @} */
  340. #if defined(__cplusplus)
  341. }
  342. #endif
  343. /*!
  344. * @}
  345. */
  346. #endif /* _FSL_DAC_H_ */