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.
 
 

386 lines
15 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @brief DAC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of DAC extension peripheral:
  8. * + Extended features functions
  9. *
  10. *
  11. @verbatim
  12. ==============================================================================
  13. ##### How to use this driver #####
  14. ==============================================================================
  15. [..]
  16. (+) When Dual mode is enabled (i.e DAC Channel1 and Channel2 are used simultaneously) :
  17. Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
  18. HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in Channel 1 and Channel 2.
  19. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  20. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  21. @endverbatim
  22. ******************************************************************************
  23. * @attention
  24. *
  25. * <h2><center>&copy; Copyright (c) 2017 STMicroelectronics.
  26. * All rights reserved.</center></h2>
  27. *
  28. * This software component is licensed by ST under BSD 3-Clause license,
  29. * the "License"; You may not use this file except in compliance with the
  30. * License. You may obtain a copy of the License at:
  31. * opensource.org/licenses/BSD-3-Clause
  32. *
  33. ******************************************************************************
  34. */
  35. /* Includes ------------------------------------------------------------------*/
  36. #include "stm32f4xx_hal.h"
  37. /** @addtogroup STM32F4xx_HAL_Driver
  38. * @{
  39. */
  40. /** @defgroup DACEx DACEx
  41. * @brief DAC driver modules
  42. * @{
  43. */
  44. #ifdef HAL_DAC_MODULE_ENABLED
  45. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  46. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  47. defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\
  48. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
  49. /* Private typedef -----------------------------------------------------------*/
  50. /* Private define ------------------------------------------------------------*/
  51. /* Private macro -------------------------------------------------------------*/
  52. /* Private variables ---------------------------------------------------------*/
  53. /* Private function prototypes -----------------------------------------------*/
  54. /* Private functions ---------------------------------------------------------*/
  55. /* Exported functions --------------------------------------------------------*/
  56. /** @defgroup DACEx_Exported_Functions DAC Exported Functions
  57. * @{
  58. */
  59. /** @defgroup DACEx_Exported_Functions_Group1 Extended features functions
  60. * @brief Extended features functions
  61. *
  62. @verbatim
  63. ==============================================================================
  64. ##### Extended features functions #####
  65. ==============================================================================
  66. [..] This section provides functions allowing to:
  67. (+) Start conversion.
  68. (+) Stop conversion.
  69. (+) Start conversion and enable DMA transfer.
  70. (+) Stop conversion and disable DMA transfer.
  71. (+) Get result of conversion.
  72. (+) Get result of dual mode conversion.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Returns the last data output value of the selected DAC channel.
  78. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  79. * the configuration information for the specified DAC.
  80. * @retval The selected DAC channel data output value.
  81. */
  82. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
  83. {
  84. uint32_t tmp = 0U;
  85. tmp |= hdac->Instance->DOR1;
  86. tmp |= hdac->Instance->DOR2 << 16U;
  87. /* Returns the DAC channel data output register value */
  88. return tmp;
  89. }
  90. /**
  91. * @brief Enables or disables the selected DAC channel wave generation.
  92. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  93. * the configuration information for the specified DAC.
  94. * @param Channel The selected DAC channel.
  95. * This parameter can be one of the following values:
  96. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  97. * @param Amplitude Select max triangle amplitude.
  98. * This parameter can be one of the following values:
  99. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  100. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  101. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  102. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  103. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  104. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  105. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  106. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  107. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  108. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  109. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  110. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  111. * @retval HAL status
  112. */
  113. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  114. {
  115. /* Check the parameters */
  116. assert_param(IS_DAC_CHANNEL(Channel));
  117. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  118. /* Process locked */
  119. __HAL_LOCK(hdac);
  120. /* Change DAC state */
  121. hdac->State = HAL_DAC_STATE_BUSY;
  122. /* Enable the selected wave generation for the selected DAC channel */
  123. MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel);
  124. /* Change DAC state */
  125. hdac->State = HAL_DAC_STATE_READY;
  126. /* Process unlocked */
  127. __HAL_UNLOCK(hdac);
  128. /* Return function status */
  129. return HAL_OK;
  130. }
  131. /**
  132. * @brief Enables or disables the selected DAC channel wave generation.
  133. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  134. * the configuration information for the specified DAC.
  135. * @param Channel The selected DAC channel.
  136. * This parameter can be one of the following values:
  137. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  138. * @param Amplitude Unmask DAC channel LFSR for noise wave generation.
  139. * This parameter can be one of the following values:
  140. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  141. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  142. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  143. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  144. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  145. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  146. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  147. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  148. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  149. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  150. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  151. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  152. * @retval HAL status
  153. */
  154. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  155. {
  156. /* Check the parameters */
  157. assert_param(IS_DAC_CHANNEL(Channel));
  158. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  159. /* Process locked */
  160. __HAL_LOCK(hdac);
  161. /* Change DAC state */
  162. hdac->State = HAL_DAC_STATE_BUSY;
  163. /* Enable the selected wave generation for the selected DAC channel */
  164. MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_0 | Amplitude) << Channel);
  165. /* Change DAC state */
  166. hdac->State = HAL_DAC_STATE_READY;
  167. /* Process unlocked */
  168. __HAL_UNLOCK(hdac);
  169. /* Return function status */
  170. return HAL_OK;
  171. }
  172. /**
  173. * @brief Set the specified data holding register value for dual DAC channel.
  174. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  175. * the configuration information for the specified DAC.
  176. * @param Alignment Specifies the data alignment for dual channel DAC.
  177. * This parameter can be one of the following values:
  178. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  179. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  180. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  181. * @param Data1 Data for DAC Channel2 to be loaded in the selected data holding register.
  182. * @param Data2 Data for DAC Channel1 to be loaded in the selected data holding register.
  183. * @note In dual mode, a unique register access is required to write in both
  184. * DAC channels at the same time.
  185. * @retval HAL status
  186. */
  187. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  188. {
  189. uint32_t data = 0U, tmp = 0U;
  190. /* Check the parameters */
  191. assert_param(IS_DAC_ALIGN(Alignment));
  192. assert_param(IS_DAC_DATA(Data1));
  193. assert_param(IS_DAC_DATA(Data2));
  194. /* Calculate and set dual DAC data holding register value */
  195. if (Alignment == DAC_ALIGN_8B_R)
  196. {
  197. data = ((uint32_t)Data2 << 8U) | Data1;
  198. }
  199. else
  200. {
  201. data = ((uint32_t)Data2 << 16U) | Data1;
  202. }
  203. tmp = (uint32_t)hdac->Instance;
  204. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  205. /* Set the dual DAC selected data holding register */
  206. *(__IO uint32_t *)tmp = data;
  207. /* Return function status */
  208. return HAL_OK;
  209. }
  210. /**
  211. * @}
  212. */
  213. /**
  214. * @brief Conversion complete callback in non blocking mode for Channel2
  215. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  216. * the configuration information for the specified DAC.
  217. * @retval None
  218. */
  219. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  220. {
  221. /* Prevent unused argument(s) compilation warning */
  222. UNUSED(hdac);
  223. /* NOTE : This function Should not be modified, when the callback is needed,
  224. the HAL_DAC_ConvCpltCallback could be implemented in the user file
  225. */
  226. }
  227. /**
  228. * @brief Conversion half DMA transfer callback in non blocking mode for Channel2
  229. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  230. * the configuration information for the specified DAC.
  231. * @retval None
  232. */
  233. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  234. {
  235. /* Prevent unused argument(s) compilation warning */
  236. UNUSED(hdac);
  237. /* NOTE : This function Should not be modified, when the callback is needed,
  238. the HAL_DAC_ConvHalfCpltCallbackCh2 could be implemented in the user file
  239. */
  240. }
  241. /**
  242. * @brief Error DAC callback for Channel2.
  243. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  244. * the configuration information for the specified DAC.
  245. * @retval None
  246. */
  247. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  248. {
  249. /* Prevent unused argument(s) compilation warning */
  250. UNUSED(hdac);
  251. /* NOTE : This function Should not be modified, when the callback is needed,
  252. the HAL_DAC_ErrorCallback could be implemented in the user file
  253. */
  254. }
  255. /**
  256. * @brief DMA underrun DAC callback for channel2.
  257. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  258. * the configuration information for the specified DAC.
  259. * @retval None
  260. */
  261. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  262. {
  263. /* Prevent unused argument(s) compilation warning */
  264. UNUSED(hdac);
  265. /* NOTE : This function Should not be modified, when the callback is needed,
  266. the HAL_DAC_DMAUnderrunCallbackCh2 could be implemented in the user file
  267. */
  268. }
  269. /**
  270. * @brief DMA conversion complete callback.
  271. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  272. * the configuration information for the specified DMA module.
  273. * @retval None
  274. */
  275. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  276. {
  277. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  278. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  279. hdac->ConvCpltCallbackCh2(hdac);
  280. #else
  281. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  282. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  283. hdac->State= HAL_DAC_STATE_READY;
  284. }
  285. /**
  286. * @brief DMA half transfer complete callback.
  287. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  288. * the configuration information for the specified DMA module.
  289. * @retval None
  290. */
  291. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  292. {
  293. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  294. /* Conversion complete callback */
  295. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  296. hdac->ConvHalfCpltCallbackCh2(hdac);
  297. #else
  298. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  299. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  300. }
  301. /**
  302. * @brief DMA error callback
  303. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  304. * the configuration information for the specified DMA module.
  305. * @retval None
  306. */
  307. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  308. {
  309. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  310. /* Set DAC error code to DMA error */
  311. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  312. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  313. hdac->ErrorCallbackCh2(hdac);
  314. #else
  315. HAL_DACEx_ErrorCallbackCh2(hdac);
  316. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  317. hdac->State= HAL_DAC_STATE_READY;
  318. }
  319. /**
  320. * @}
  321. */
  322. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  323. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  324. STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx ||\
  325. STM32F413xx || STM32F423xx */
  326. #endif /* HAL_DAC_MODULE_ENABLED */
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/