stm32f1xx_hal_dac_ex.c 16 KB

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