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.
 
 
 

389 lines
15 KiB

  1. /**
  2. ******************************************************************************
  3. * @file stm32f7xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @version V1.1.2
  6. * @date 23-September-2016
  7. * @brief Extended 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) 2016 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 "stm32f7xx_hal.h"
  55. /** @addtogroup STM32F7xx_HAL_Driver
  56. * @{
  57. */
  58. /** @defgroup DACEx DACEx
  59. * @brief DAC driver modules
  60. * @{
  61. */
  62. #ifdef HAL_DAC_MODULE_ENABLED
  63. /* Private typedef -----------------------------------------------------------*/
  64. /* Private define ------------------------------------------------------------*/
  65. /* Private macro -------------------------------------------------------------*/
  66. /* Private variables ---------------------------------------------------------*/
  67. /* Private function prototypes -----------------------------------------------*/
  68. /* Private functions ---------------------------------------------------------*/
  69. /* Exported functions --------------------------------------------------------*/
  70. /** @defgroup DACEx_Exported_Functions DAC Exported Functions
  71. * @{
  72. */
  73. /** @defgroup DACEx_Exported_Functions_Group1 Extended features functions
  74. * @brief Extended features functions
  75. *
  76. @verbatim
  77. ==============================================================================
  78. ##### Extended features functions #####
  79. ==============================================================================
  80. [..] This section provides functions allowing to:
  81. (+) Start conversion.
  82. (+) Stop conversion.
  83. (+) Start conversion and enable DMA transfer.
  84. (+) Stop conversion and disable DMA transfer.
  85. (+) Get result of conversion.
  86. (+) Get result of dual mode conversion.
  87. @endverbatim
  88. * @{
  89. */
  90. /**
  91. * @brief Returns the last data output value of the selected DAC channel.
  92. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  93. * the configuration information for the specified DAC.
  94. * @retval The selected DAC channel data output value.
  95. */
  96. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
  97. {
  98. uint32_t tmp = 0;
  99. tmp |= hdac->Instance->DOR1;
  100. tmp |= hdac->Instance->DOR2 << 16;
  101. /* Returns the DAC channel data output register value */
  102. return tmp;
  103. }
  104. /**
  105. * @brief Enables or disables the selected DAC channel wave generation.
  106. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  107. * the configuration information for the specified DAC.
  108. * @param Channel: The selected DAC channel.
  109. * This parameter can be one of the following values:
  110. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  111. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  112. * @param Amplitude: Select max triangle amplitude.
  113. * This parameter can be one of the following values:
  114. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  115. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  116. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  117. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  118. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  119. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  120. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  121. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  122. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  123. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  124. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  125. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  126. * @retval HAL status
  127. */
  128. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  129. {
  130. /* Check the parameters */
  131. assert_param(IS_DAC_CHANNEL(Channel));
  132. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  133. /* Process locked */
  134. __HAL_LOCK(hdac);
  135. /* Change DAC state */
  136. hdac->State = HAL_DAC_STATE_BUSY;
  137. /* Enable the selected wave generation for the selected DAC channel */
  138. MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel);
  139. /* Change DAC state */
  140. hdac->State = HAL_DAC_STATE_READY;
  141. /* Process unlocked */
  142. __HAL_UNLOCK(hdac);
  143. /* Return function status */
  144. return HAL_OK;
  145. }
  146. /**
  147. * @brief Enables or disables the selected DAC channel wave generation.
  148. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  149. * the configuration information for the specified DAC.
  150. * @param Channel: The selected DAC channel.
  151. * This parameter can be one of the following values:
  152. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  153. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  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 = 0, tmp = 0;
  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 << 8) | Data1;
  214. }
  215. else
  216. {
  217. data = ((uint32_t)Data2 << 16) | 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_ConvCpltCallbackCh2 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_DACEx_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_DACEx_ErrorCallbackCh2 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_DACEx_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 /* HAL_DAC_MODULE_ENABLED */
  327. /**
  328. * @}
  329. */
  330. /**
  331. * @}
  332. */
  333. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/