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.
 
 
 

301 rivejä
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. #include "fsl_dac.h"
  9. /* Component ID definition, used by tools. */
  10. #ifndef FSL_COMPONENT_ID
  11. #define FSL_COMPONENT_ID "platform.drivers.dac"
  12. #endif
  13. /*******************************************************************************
  14. * Prototypes
  15. ******************************************************************************/
  16. /*!
  17. * @brief Get instance number for DAC module.
  18. *
  19. * @param base DAC peripheral base address
  20. */
  21. static uint32_t DAC_GetInstance(DAC_Type *base);
  22. /*******************************************************************************
  23. * Variables
  24. ******************************************************************************/
  25. /*! @brief Pointers to DAC bases for each instance. */
  26. static DAC_Type *const s_dacBases[] = DAC_BASE_PTRS;
  27. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  28. /*! @brief Pointers to DAC clocks for each instance. */
  29. static const clock_ip_name_t s_dacClocks[] = DAC_CLOCKS;
  30. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  31. /*******************************************************************************
  32. * Codes
  33. ******************************************************************************/
  34. static uint32_t DAC_GetInstance(DAC_Type *base)
  35. {
  36. uint32_t instance;
  37. /* Find the instance index from base address mappings. */
  38. for (instance = 0; instance < ARRAY_SIZE(s_dacBases); instance++)
  39. {
  40. if (s_dacBases[instance] == base)
  41. {
  42. break;
  43. }
  44. }
  45. assert(instance < ARRAY_SIZE(s_dacBases));
  46. return instance;
  47. }
  48. /*!
  49. * brief Initializes the DAC module.
  50. *
  51. * This function initializes the DAC module including the following operations.
  52. * - Enabling the clock for DAC module.
  53. * - Configuring the DAC converter with a user configuration.
  54. * - Enabling the DAC module.
  55. *
  56. * param base DAC peripheral base address.
  57. * param config Pointer to the configuration structure. See "dac_config_t".
  58. */
  59. void DAC_Init(DAC_Type *base, const dac_config_t *config)
  60. {
  61. assert(NULL != config);
  62. uint8_t tmp8;
  63. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  64. /* Enable the clock. */
  65. CLOCK_EnableClock(s_dacClocks[DAC_GetInstance(base)]);
  66. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  67. /* Configure. */
  68. /* DACx_C0. */
  69. tmp8 = base->C0 & (uint8_t)(~(DAC_C0_DACRFS_MASK | DAC_C0_LPEN_MASK));
  70. if (kDAC_ReferenceVoltageSourceVref2 == config->referenceVoltageSource)
  71. {
  72. tmp8 |= DAC_C0_DACRFS_MASK;
  73. }
  74. if (config->enableLowPowerMode)
  75. {
  76. tmp8 |= DAC_C0_LPEN_MASK;
  77. }
  78. base->C0 = tmp8;
  79. /* DAC_Enable(base, true); */
  80. /* Tip: The DAC output can be enabled till then after user sets their own available data in application. */
  81. }
  82. /*!
  83. * brief De-initializes the DAC module.
  84. *
  85. * This function de-initializes the DAC module including the following operations.
  86. * - Disabling the DAC module.
  87. * - Disabling the clock for the DAC module.
  88. *
  89. * param base DAC peripheral base address.
  90. */
  91. void DAC_Deinit(DAC_Type *base)
  92. {
  93. DAC_Enable(base, false);
  94. #if !(defined(FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL) && FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL)
  95. /* Disable the clock. */
  96. CLOCK_DisableClock(s_dacClocks[DAC_GetInstance(base)]);
  97. #endif /* FSL_SDK_DISABLE_DRIVER_CLOCK_CONTROL */
  98. }
  99. /*!
  100. * brief Initializes the DAC user configuration structure.
  101. *
  102. * This function initializes the user configuration structure to a default value. The default values are as follows.
  103. * code
  104. * config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
  105. * config->enableLowPowerMode = false;
  106. * endcode
  107. * param config Pointer to the configuration structure. See "dac_config_t".
  108. */
  109. void DAC_GetDefaultConfig(dac_config_t *config)
  110. {
  111. assert(NULL != config);
  112. /* Initializes the configure structure to zero. */
  113. (void)memset(config, 0, sizeof(*config));
  114. config->referenceVoltageSource = kDAC_ReferenceVoltageSourceVref2;
  115. config->enableLowPowerMode = false;
  116. }
  117. /*!
  118. * brief Configures the CMP buffer.
  119. *
  120. * param base DAC peripheral base address.
  121. * param config Pointer to the configuration structure. See "dac_buffer_config_t".
  122. */
  123. void DAC_SetBufferConfig(DAC_Type *base, const dac_buffer_config_t *config)
  124. {
  125. assert(NULL != config);
  126. uint8_t tmp8;
  127. /* DACx_C0. */
  128. tmp8 = base->C0 & (uint8_t)(~DAC_C0_DACTRGSEL_MASK);
  129. if (kDAC_BufferTriggerBySoftwareMode == config->triggerMode)
  130. {
  131. tmp8 |= DAC_C0_DACTRGSEL_MASK;
  132. }
  133. base->C0 = tmp8;
  134. /* DACx_C1. */
  135. tmp8 = base->C1 & (uint8_t)(~(
  136. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  137. DAC_C1_DACBFWM_MASK |
  138. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  139. DAC_C1_DACBFMD_MASK));
  140. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  141. tmp8 |= DAC_C1_DACBFWM(config->watermark);
  142. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  143. tmp8 |= DAC_C1_DACBFMD(config->workMode);
  144. base->C1 = tmp8;
  145. /* DACx_C2. */
  146. tmp8 = base->C2 & (uint8_t)(~DAC_C2_DACBFUP_MASK);
  147. tmp8 |= DAC_C2_DACBFUP(config->upperLimit);
  148. base->C2 = tmp8;
  149. }
  150. /*!
  151. * brief Initializes the DAC buffer configuration structure.
  152. *
  153. * This function initializes the DAC buffer configuration structure to default values. The default values are as
  154. * follows.
  155. * code
  156. * config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
  157. * config->watermark = kDAC_BufferWatermark1Word;
  158. * config->workMode = kDAC_BufferWorkAsNormalMode;
  159. * config->upperLimit = DAC_DATL_COUNT - 1U;
  160. * endcode
  161. * param config Pointer to the configuration structure. See "dac_buffer_config_t".
  162. */
  163. void DAC_GetDefaultBufferConfig(dac_buffer_config_t *config)
  164. {
  165. assert(NULL != config);
  166. /* Initializes the configure structure to zero. */
  167. (void)memset(config, 0, sizeof(*config));
  168. config->triggerMode = kDAC_BufferTriggerBySoftwareMode;
  169. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION
  170. config->watermark = kDAC_BufferWatermark1Word;
  171. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_SELECTION */
  172. config->workMode = kDAC_BufferWorkAsNormalMode;
  173. config->upperLimit = DAC_DATL_COUNT - 1U;
  174. }
  175. /*!
  176. * brief Sets the value for items in the buffer.
  177. *
  178. * param base DAC peripheral base address.
  179. * param index Setting the index for items in the buffer. The available index should not exceed the size of the DAC
  180. * buffer.
  181. * param value Setting the value for items in the buffer. 12-bits are available.
  182. */
  183. void DAC_SetBufferValue(DAC_Type *base, uint8_t index, uint16_t value)
  184. {
  185. assert(index < DAC_DATL_COUNT);
  186. base->DAT[index].DATL = (uint8_t)(0xFFU & value); /* Low 8-bit. */
  187. base->DAT[index].DATH = (uint8_t)((0xF00U & value) >> 8); /* High 4-bit. */
  188. }
  189. /*!
  190. * brief Sets the current read pointer of the DAC buffer.
  191. *
  192. * This function sets the current read pointer of the DAC buffer.
  193. * The current output value depends on the item indexed by the read pointer. It is updated either by a
  194. * software trigger or a hardware trigger. After the read pointer changes, the DAC output value also changes.
  195. *
  196. * param base DAC peripheral base address.
  197. * param index Setting an index value for the pointer.
  198. */
  199. void DAC_SetBufferReadPointer(DAC_Type *base, uint8_t index)
  200. {
  201. assert(index < DAC_DATL_COUNT);
  202. uint8_t tmp8 = base->C2 & (uint8_t)(~DAC_C2_DACBFRP_MASK);
  203. tmp8 |= DAC_C2_DACBFRP(index);
  204. base->C2 = tmp8;
  205. }
  206. /*!
  207. * brief Enables interrupts for the DAC buffer.
  208. *
  209. * param base DAC peripheral base address.
  210. * param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
  211. */
  212. void DAC_EnableBufferInterrupts(DAC_Type *base, uint32_t mask)
  213. {
  214. mask &= (
  215. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  216. DAC_C0_DACBWIEN_MASK |
  217. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  218. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  219. base->C0 |= ((uint8_t)mask); /* Write 1 to enable. */
  220. }
  221. /*!
  222. * brief Disables interrupts for the DAC buffer.
  223. *
  224. * param base DAC peripheral base address.
  225. * param mask Mask value for interrupts. See "_dac_buffer_interrupt_enable".
  226. */
  227. void DAC_DisableBufferInterrupts(DAC_Type *base, uint32_t mask)
  228. {
  229. mask &= (
  230. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  231. DAC_C0_DACBWIEN_MASK |
  232. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  233. DAC_C0_DACBTIEN_MASK | DAC_C0_DACBBIEN_MASK);
  234. base->C0 &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to disable. */
  235. }
  236. /*!
  237. * brief Gets the flags of events for the DAC buffer.
  238. *
  239. * param base DAC peripheral base address.
  240. *
  241. * return Mask value for the asserted flags. See "_dac_buffer_status_flags".
  242. */
  243. uint8_t DAC_GetBufferStatusFlags(DAC_Type *base)
  244. {
  245. return base->SR & (
  246. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  247. DAC_SR_DACBFWMF_MASK |
  248. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  249. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK);
  250. }
  251. /*!
  252. * brief Clears the flags of events for the DAC buffer.
  253. *
  254. * param base DAC peripheral base address.
  255. * param mask Mask value for flags. See "_dac_buffer_status_flags_t".
  256. */
  257. void DAC_ClearBufferStatusFlags(DAC_Type *base, uint32_t mask)
  258. {
  259. mask &= (
  260. #if defined(FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION) && FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION
  261. DAC_SR_DACBFWMF_MASK |
  262. #endif /* FSL_FEATURE_DAC_HAS_WATERMARK_DETECTION */
  263. DAC_SR_DACBFRPTF_MASK | DAC_SR_DACBFRPBF_MASK);
  264. base->SR &= (uint8_t)(~((uint8_t)mask)); /* Write 0 to clear flags. */
  265. }