stm32f4xx_hal_dac_ex.c 16 KB

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