Vous ne pouvez pas sélectionner plus de 25 sujets Les noms de sujets doivent commencer par une lettre ou un nombre, peuvent contenir des tirets ('-') et peuvent comporter jusqu'à 35 caractères.

stm32f1xx_hal_dac_ex.c 17KB

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