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.
 
 
 

392 lines
15 KiB

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