fsl_dac.h 13 KB

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