stm32f7xx_hal_dac_ex.c 15 KB

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