Non puoi selezionare più di 25 argomenti Gli argomenti devono iniziare con una lettera o un numero, possono includere trattini ('-') e possono essere lunghi fino a 35 caratteri.
 
 
 

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