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.

stm32l1xx_hal_dac_ex.c 15KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420
  1. /**
  2. ******************************************************************************
  3. * @file stm32l1xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @brief DAC HAL module driver.
  6. * This file provides firmware functions to manage the extended
  7. * functionalities of the DAC peripheral.
  8. *
  9. *
  10. @verbatim
  11. ==============================================================================
  12. ##### How to use this driver #####
  13. ==============================================================================
  14. [..]
  15. *** Dual mode IO operation ***
  16. ==============================
  17. (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) :
  18. Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
  19. HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in
  20. Channel 1 and Channel 2.
  21. *** Signal generation operation ***
  22. ===================================
  23. (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
  24. (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
  25. @endverbatim
  26. ******************************************************************************
  27. * @attention
  28. *
  29. * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
  30. * All rights reserved.</center></h2>
  31. *
  32. * This software component is licensed by ST under BSD 3-Clause license,
  33. * the "License"; You may not use this file except in compliance with the
  34. * License. You may obtain a copy of the License at:
  35. * opensource.org/licenses/BSD-3-Clause
  36. *
  37. ******************************************************************************
  38. */
  39. /* Includes ------------------------------------------------------------------*/
  40. #include "stm32l1xx_hal.h"
  41. /** @addtogroup STM32L1xx_HAL_Driver
  42. * @{
  43. */
  44. #ifdef HAL_DAC_MODULE_ENABLED
  45. #if defined(DAC1)
  46. /** @defgroup DACEx DACEx
  47. * @brief DAC Extended HAL module driver
  48. * @{
  49. */
  50. /* Private typedef -----------------------------------------------------------*/
  51. /* Private define ------------------------------------------------------------*/
  52. /* Private macro -------------------------------------------------------------*/
  53. /* Private variables ---------------------------------------------------------*/
  54. /* Private function prototypes -----------------------------------------------*/
  55. /* Exported functions --------------------------------------------------------*/
  56. /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
  57. * @{
  58. */
  59. /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
  60. * @brief Extended IO operation functions
  61. *
  62. @verbatim
  63. ==============================================================================
  64. ##### Extended features functions #####
  65. ==============================================================================
  66. [..] This section provides functions allowing to:
  67. (+) Start conversion.
  68. (+) Stop conversion.
  69. (+) Start conversion and enable DMA transfer.
  70. (+) Stop conversion and disable DMA transfer.
  71. (+) Get result of conversion.
  72. (+) Get result of dual mode conversion.
  73. @endverbatim
  74. * @{
  75. */
  76. /**
  77. * @brief Enable or disable the selected DAC channel wave generation.
  78. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  79. * the configuration information for the specified DAC.
  80. * @param Channel The selected DAC channel.
  81. * This parameter can be one of the following values:
  82. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  83. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  84. * @param Amplitude Select max triangle amplitude.
  85. * This parameter can be one of the following values:
  86. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  87. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  88. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  89. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  90. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  91. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  92. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  93. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  94. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  95. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  96. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  97. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  98. * @retval HAL status
  99. */
  100. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  101. {
  102. /* Check the parameters */
  103. assert_param(IS_DAC_CHANNEL(Channel));
  104. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  105. /* Process locked */
  106. __HAL_LOCK(hdac);
  107. /* Change DAC state */
  108. hdac->State = HAL_DAC_STATE_BUSY;
  109. /* Enable the triangle wave generation for the selected DAC channel */
  110. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
  111. /* Change DAC state */
  112. hdac->State = HAL_DAC_STATE_READY;
  113. /* Process unlocked */
  114. __HAL_UNLOCK(hdac);
  115. /* Return function status */
  116. return HAL_OK;
  117. }
  118. /**
  119. * @brief Enable or disable the selected DAC channel wave generation.
  120. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  121. * the configuration information for the specified DAC.
  122. * @param Channel The selected DAC channel.
  123. * This parameter can be one of the following values:
  124. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  125. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  126. * @param Amplitude Unmask DAC channel LFSR for noise wave generation.
  127. * This parameter can be one of the following values:
  128. * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
  129. * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
  130. * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
  131. * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
  132. * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
  133. * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
  134. * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
  135. * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
  136. * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
  137. * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
  138. * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
  139. * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
  140. * @retval HAL status
  141. */
  142. HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
  143. {
  144. /* Check the parameters */
  145. assert_param(IS_DAC_CHANNEL(Channel));
  146. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  147. /* Process locked */
  148. __HAL_LOCK(hdac);
  149. /* Change DAC state */
  150. hdac->State = HAL_DAC_STATE_BUSY;
  151. /* Enable the noise wave generation for the selected DAC channel */
  152. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL), (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
  153. /* Change DAC state */
  154. hdac->State = HAL_DAC_STATE_READY;
  155. /* Process unlocked */
  156. __HAL_UNLOCK(hdac);
  157. /* Return function status */
  158. return HAL_OK;
  159. }
  160. /**
  161. * @brief Set the specified data holding register value for dual DAC channel.
  162. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  163. * the configuration information for the specified DAC.
  164. * @param Alignment Specifies the data alignment for dual channel DAC.
  165. * This parameter can be one of the following values:
  166. * DAC_ALIGN_8B_R: 8bit right data alignment selected
  167. * DAC_ALIGN_12B_L: 12bit left data alignment selected
  168. * DAC_ALIGN_12B_R: 12bit right data alignment selected
  169. * @param Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
  170. * @param Data2 Data for DAC Channel2 to be loaded in the selected data holding register.
  171. * @note In dual mode, a unique register access is required to write in both
  172. * DAC channels at the same time.
  173. * @retval HAL status
  174. */
  175. HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
  176. {
  177. uint32_t data;
  178. uint32_t tmp;
  179. /* Check the parameters */
  180. assert_param(IS_DAC_ALIGN(Alignment));
  181. assert_param(IS_DAC_DATA(Data1));
  182. assert_param(IS_DAC_DATA(Data2));
  183. /* Calculate and set dual DAC data holding register value */
  184. if (Alignment == DAC_ALIGN_8B_R)
  185. {
  186. data = ((uint32_t)Data2 << 8U) | Data1;
  187. }
  188. else
  189. {
  190. data = ((uint32_t)Data2 << 16U) | Data1;
  191. }
  192. tmp = (uint32_t)hdac->Instance;
  193. tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
  194. /* Set the dual DAC selected data holding register */
  195. *(__IO uint32_t *)tmp = data;
  196. /* Return function status */
  197. return HAL_OK;
  198. }
  199. /**
  200. * @brief Conversion complete callback in non-blocking mode for Channel2.
  201. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  202. * the configuration information for the specified DAC.
  203. * @retval None
  204. */
  205. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  206. {
  207. /* Prevent unused argument(s) compilation warning */
  208. UNUSED(hdac);
  209. /* NOTE : This function should not be modified, when the callback is needed,
  210. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  211. */
  212. }
  213. /**
  214. * @brief Conversion half DMA transfer callback in non-blocking mode for Channel2.
  215. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  216. * the configuration information for the specified DAC.
  217. * @retval None
  218. */
  219. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
  220. {
  221. /* Prevent unused argument(s) compilation warning */
  222. UNUSED(hdac);
  223. /* NOTE : This function should not be modified, when the callback is needed,
  224. the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  225. */
  226. }
  227. /**
  228. * @brief Error DAC callback 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_ErrorCallbackCh2(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_DACEx_ErrorCallbackCh2 could be implemented in the user file
  239. */
  240. }
  241. /**
  242. * @brief DMA underrun DAC callback 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_DMAUnderrunCallbackCh2(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_DMAUnderrunCallbackCh2 could be implemented in the user file
  253. */
  254. }
  255. /**
  256. * @}
  257. */
  258. /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
  259. * @brief Extended Peripheral Control functions
  260. *
  261. @verbatim
  262. ==============================================================================
  263. ##### Peripheral Control functions #####
  264. ==============================================================================
  265. [..] This section provides functions allowing to:
  266. (+) Set the specified data holding register value for DAC channel.
  267. @endverbatim
  268. * @{
  269. */
  270. /**
  271. * @brief Return the last data output value of the selected DAC channel.
  272. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  273. * the configuration information for the specified DAC.
  274. * @retval The selected DAC channel data output value.
  275. */
  276. uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac)
  277. {
  278. uint32_t tmp = 0U;
  279. tmp |= hdac->Instance->DOR1;
  280. tmp |= hdac->Instance->DOR2 << 16U;
  281. /* Returns the DAC channel data output register value */
  282. return tmp;
  283. }
  284. /**
  285. * @}
  286. */
  287. /**
  288. * @}
  289. */
  290. /* Private functions ---------------------------------------------------------*/
  291. /** @defgroup DACEx_Private_Functions DACEx private functions
  292. * @brief Extended private functions
  293. * @{
  294. */
  295. /**
  296. * @brief DMA conversion 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_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  302. {
  303. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  304. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  305. hdac->ConvCpltCallbackCh2(hdac);
  306. #else
  307. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  308. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  309. hdac->State = HAL_DAC_STATE_READY;
  310. }
  311. /**
  312. * @brief DMA half transfer complete 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_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  318. {
  319. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  320. /* Conversion complete callback */
  321. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  322. hdac->ConvHalfCpltCallbackCh2(hdac);
  323. #else
  324. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  325. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  326. }
  327. /**
  328. * @brief DMA error callback.
  329. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  330. * the configuration information for the specified DMA module.
  331. * @retval None
  332. */
  333. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  334. {
  335. DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
  336. /* Set DAC error code to DMA error */
  337. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  338. #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
  339. hdac->ErrorCallbackCh2(hdac);
  340. #else
  341. HAL_DACEx_ErrorCallbackCh2(hdac);
  342. #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
  343. hdac->State = HAL_DAC_STATE_READY;
  344. }
  345. /**
  346. * @}
  347. */
  348. /**
  349. * @}
  350. */
  351. #endif /* DAC1 */
  352. #endif /* HAL_DAC_MODULE_ENABLED */
  353. /**
  354. * @}
  355. */
  356. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/