stm32f4xx_hal_dac_ex.c 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @version V1.3.0
  6. * @date 09-March-2015
  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) 2015 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(STM32F446xx)
  66. /* Private typedef -----------------------------------------------------------*/
  67. /* Private define ------------------------------------------------------------*/
  68. /* Private macro -------------------------------------------------------------*/
  69. /* Private variables ---------------------------------------------------------*/
  70. /* Private function prototypes -----------------------------------------------*/
  71. /* Private functions ---------------------------------------------------------*/
  72. /* Exported functions --------------------------------------------------------*/
  73. /** @defgroup DACEx_Exported_Functions DAC Exported Functions
  74. * @{
  75. */
  76. /** @defgroup DACEx_Exported_Functions_Group1 Extended features functions
  77. * @brief Extended features functions
  78. *
  79. @verbatim
  80. ==============================================================================
  81. ##### Extended features functions #####
  82. ==============================================================================
  83. [..] This section provides functions allowing to:
  84. (+) Start conversion.
  85. (+) Stop conversion.
  86. (+) Start conversion and enable DMA transfer.
  87. (+) Stop conversion and disable DMA transfer.
  88. (+) Get result of conversion.
  89. (+) Get result of dual mode conversion.
  90. @endverbatim
  91. * @{
  92. */
  93. /**
  94. * @brief Returns the last data output value of the selected DAC channel.
  95. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  96. * the configuration information for the specified DAC.
  97. * @retval The selected DAC channel data output value.
  98. */
  99. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
  100. {
  101. uint32_t tmp = 0;
  102. tmp |= hdac->Instance->DOR1;
  103. tmp |= hdac->Instance->DOR2 << 16;
  104. /* Returns the DAC channel data output register value */
  105. return tmp;
  106. }
  107. /**
  108. * @brief Enables or disables the selected DAC channel wave generation.
  109. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  110. * the configuration information for the specified DAC.
  111. * @param Channel: The selected DAC channel.
  112. * This parameter can be one of the following values:
  113. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  114. * @param Amplitude: Select max triangle amplitude.
  115. * This parameter can be one of the following values:
  116. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  117. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  118. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  119. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  120. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  121. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  122. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  123. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  124. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  125. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  126. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  127. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  128. * @retval HAL status
  129. */
  130. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  131. {
  132. /* Check the parameters */
  133. assert_param(IS_DAC_CHANNEL(Channel));
  134. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  135. /* Process locked */
  136. __HAL_LOCK(hdac);
  137. /* Change DAC state */
  138. hdac->State = HAL_DAC_STATE_BUSY;
  139. /* Enable the selected wave generation for the selected DAC channel */
  140. MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel);
  141. /* Change DAC state */
  142. hdac->State = HAL_DAC_STATE_READY;
  143. /* Process unlocked */
  144. __HAL_UNLOCK(hdac);
  145. /* Return function status */
  146. return HAL_OK;
  147. }
  148. /**
  149. * @brief Enables or disables the selected DAC channel wave generation.
  150. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  151. * the configuration information for the specified DAC.
  152. * @param Channel: The selected DAC channel.
  153. * This parameter can be one of the following values:
  154. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  155. * @param Amplitude: Unmask DAC channel LFSR for noise wave generation.
  156. * This parameter can be one of the following values:
  157. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  158. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  159. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  160. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  161. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  162. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  163. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  164. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  165. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  166. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  167. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  168. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  169. * @retval HAL status
  170. */
  171. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  172. {
  173. /* Check the parameters */
  174. assert_param(IS_DAC_CHANNEL(Channel));
  175. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  176. /* Process locked */
  177. __HAL_LOCK(hdac);
  178. /* Change DAC state */
  179. hdac->State = HAL_DAC_STATE_BUSY;
  180. /* Enable the selected wave generation for the selected DAC channel */
  181. MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_0 | Amplitude) << Channel);
  182. /* Change DAC state */
  183. hdac->State = HAL_DAC_STATE_READY;
  184. /* Process unlocked */
  185. __HAL_UNLOCK(hdac);
  186. /* Return function status */
  187. return HAL_OK;
  188. }
  189. /**
  190. * @brief Set the specified data holding register value for dual DAC channel.
  191. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  192. * the configuration information for the specified DAC.
  193. * @param Alignment: Specifies the data alignment for dual channel DAC.
  194. * This parameter can be one of the following values:
  195. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  196. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  197. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  198. * @param Data1: Data for DAC Channel2 to be loaded in the selected data holding register.
  199. * @param Data2: Data for DAC Channel1 to be loaded in the selected data holding register.
  200. * @note In dual mode, a unique register access is required to write in both
  201. * DAC channels at the same time.
  202. * @retval HAL status
  203. */
  204. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  205. {
  206. uint32_t data = 0, tmp = 0;
  207. /* Check the parameters */
  208. assert_param(IS_DAC_ALIGN(Alignment));
  209. assert_param(IS_DAC_DATA(Data1));
  210. assert_param(IS_DAC_DATA(Data2));
  211. /* Calculate and set dual DAC data holding register value */
  212. if (Alignment == DAC_ALIGN_8B_R)
  213. {
  214. data = ((uint32_t)Data2 << 8) | Data1;
  215. }
  216. else
  217. {
  218. data = ((uint32_t)Data2 << 16) | Data1;
  219. }
  220. tmp = (uint32_t)hdac->Instance;
  221. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  222. /* Set the dual DAC selected data holding register */
  223. *(__IO uint32_t *)tmp = data;
  224. /* Return function status */
  225. return HAL_OK;
  226. }
  227. /**
  228. * @}
  229. */
  230. /**
  231. * @brief Conversion complete callback in non blocking mode for Channel2
  232. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  233. * the configuration information for the specified DAC.
  234. * @retval None
  235. */
  236. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  237. {
  238. /* NOTE : This function Should not be modified, when the callback is needed,
  239. the HAL_DAC_ConvCpltCallback could be implemented in the user file
  240. */
  241. }
  242. /**
  243. * @brief Conversion half DMA transfer callback in non blocking mode for Channel2
  244. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  245. * the configuration information for the specified DAC.
  246. * @retval None
  247. */
  248. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  249. {
  250. /* NOTE : This function Should not be modified, when the callback is needed,
  251. the HAL_DAC_ConvHalfCpltCallbackCh2 could be implemented in the user file
  252. */
  253. }
  254. /**
  255. * @brief Error DAC callback for Channel2.
  256. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  257. * the configuration information for the specified DAC.
  258. * @retval None
  259. */
  260. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  261. {
  262. /* NOTE : This function Should not be modified, when the callback is needed,
  263. the HAL_DAC_ErrorCallback could be implemented in the user file
  264. */
  265. }
  266. /**
  267. * @brief DMA underrun DAC callback for channel2.
  268. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  269. * the configuration information for the specified DAC.
  270. * @retval None
  271. */
  272. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  273. {
  274. /* NOTE : This function Should not be modified, when the callback is needed,
  275. the HAL_DAC_DMAUnderrunCallbackCh2 could be implemented in the user file
  276. */
  277. }
  278. /**
  279. * @brief DMA conversion complete callback.
  280. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  281. * the configuration information for the specified DMA module.
  282. * @retval None
  283. */
  284. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  285. {
  286. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  287. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  288. hdac->State= HAL_DAC_STATE_READY;
  289. }
  290. /**
  291. * @brief DMA half transfer complete callback.
  292. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  293. * the configuration information for the specified DMA module.
  294. * @retval None
  295. */
  296. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  297. {
  298. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  299. /* Conversion complete callback */
  300. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  301. }
  302. /**
  303. * @brief DMA error callback
  304. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  305. * the configuration information for the specified DMA module.
  306. * @retval None
  307. */
  308. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  309. {
  310. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  311. /* Set DAC error code to DMA error */
  312. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  313. HAL_DACEx_ErrorCallbackCh2(hdac);
  314. hdac->State= HAL_DAC_STATE_READY;
  315. }
  316. /**
  317. * @}
  318. */
  319. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  320. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  321. STM32F446xx
  322. */
  323. #endif /* HAL_DAC_MODULE_ENABLED */
  324. /**
  325. * @}
  326. */
  327. /**
  328. * @}
  329. */
  330. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/