stm32f1xx_hal_dac_ex.c 24 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669
  1. /**
  2. ******************************************************************************
  3. * @file stm32f1xx_hal_dac_ex.c
  4. * @author MCD Application Team
  5. * @version V1.0.4
  6. * @date 29-April-2016
  7. * @brief 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 "stm32f1xx_hal.h"
  55. /** @addtogroup STM32F1xx_HAL_Driver
  56. * @{
  57. */
  58. /** @defgroup DACEx DACEx
  59. * @brief DACEx driver module
  60. * @{
  61. */
  62. #ifdef HAL_DAC_MODULE_ENABLED
  63. #if defined (STM32F100xB) || defined (STM32F100xE) || defined (STM32F101xE) || defined (STM32F101xG) || defined (STM32F103xE) || defined (STM32F103xG) || defined (STM32F105xC) || defined (STM32F107xC)
  64. /* Private typedef -----------------------------------------------------------*/
  65. /* Private define ------------------------------------------------------------*/
  66. /* Private macro -------------------------------------------------------------*/
  67. /* Private variables ---------------------------------------------------------*/
  68. /* Private function prototypes -----------------------------------------------*/
  69. /* Exported functions --------------------------------------------------------*/
  70. /** @defgroup DACEx_Exported_Functions DACEx 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. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  111. * @param Amplitude: Select max triangle amplitude.
  112. * This parameter can be one of the following values:
  113. * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
  114. * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
  115. * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
  116. * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
  117. * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
  118. * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
  119. * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
  120. * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
  121. * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
  122. * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
  123. * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
  124. * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
  125. * @retval HAL status
  126. */
  127. HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude)
  128. {
  129. /* Check the parameters */
  130. assert_param(IS_DAC_CHANNEL(Channel));
  131. assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
  132. /* Process locked */
  133. __HAL_LOCK(hdac);
  134. /* Change DAC state */
  135. hdac->State = HAL_DAC_STATE_BUSY;
  136. /* Enable the selected wave generation for the selected DAC channel */
  137. MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1)|(DAC_CR_MAMP1))<<Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel);
  138. /* Change DAC state */
  139. hdac->State = HAL_DAC_STATE_READY;
  140. /* Process unlocked */
  141. __HAL_UNLOCK(hdac);
  142. /* Return function status */
  143. return HAL_OK;
  144. }
  145. /**
  146. * @brief Enables or disables the selected DAC channel wave generation.
  147. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  148. * the configuration information for the specified DAC.
  149. * @param Channel: The selected DAC channel.
  150. * This parameter can be one of the following values:
  151. * DAC_CHANNEL_1 / DAC_CHANNEL_2
  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. * @brief Conversion complete callback in non blocking mode for Channel2
  226. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  227. * the configuration information for the specified DAC.
  228. * @retval None
  229. */
  230. __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  231. {
  232. /* Prevent unused argument(s) compilation warning */
  233. UNUSED(hdac);
  234. /* NOTE : This function Should not be modified, when the callback is needed,
  235. the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
  236. */
  237. }
  238. /**
  239. * @brief Conversion half DMA transfer callback in non blocking mode for Channel2
  240. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  241. * the configuration information for the specified DAC.
  242. * @retval None
  243. */
  244. __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
  245. {
  246. /* Prevent unused argument(s) compilation warning */
  247. UNUSED(hdac);
  248. /* NOTE : This function Should not be modified, when the callback is needed,
  249. the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
  250. */
  251. }
  252. /**
  253. * @brief Error DAC callback for Channel2.
  254. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  255. * the configuration information for the specified DAC.
  256. * @retval None
  257. */
  258. __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
  259. {
  260. /* Prevent unused argument(s) compilation warning */
  261. UNUSED(hdac);
  262. /* NOTE : This function Should not be modified, when the callback is needed,
  263. the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
  264. */
  265. }
  266. #if defined (STM32F100xB) || defined (STM32F100xE)
  267. /**
  268. * @brief DMA underrun DAC callback for channel1.
  269. * Note: For STM32F100x devices with specific feature: DMA underrun.
  270. * On these devices, this function uses the interruption of DMA
  271. * underrun.
  272. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  273. * the configuration information for the specified DAC.
  274. * @retval None
  275. */
  276. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  277. {
  278. /* Prevent unused argument(s) compilation warning */
  279. UNUSED(hdac);
  280. /* NOTE : This function Should not be modified, when the callback is needed,
  281. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  282. */
  283. }
  284. /**
  285. * @brief DMA underrun DAC callback for channel2.
  286. * Note: For STM32F100x devices with specific feature: DMA underrun.
  287. * On these devices, this function uses the interruption of DMA
  288. * underrun.
  289. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  290. * the configuration information for the specified DAC.
  291. * @retval None
  292. */
  293. __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
  294. {
  295. /* Prevent unused argument(s) compilation warning */
  296. UNUSED(hdac);
  297. /* NOTE : This function Should not be modified, when the callback is needed,
  298. the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
  299. */
  300. }
  301. #endif /* STM32F100xB) || defined (STM32F100xE) */
  302. /**
  303. * @}
  304. */
  305. #if defined (STM32F100xB) || defined (STM32F100xE)
  306. /**
  307. * @brief Enables DAC and starts conversion of channel.
  308. * Note: For STM32F100x devices with specific feature: DMA underrun.
  309. * On these devices, this function enables the interruption of DMA
  310. * underrun.
  311. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  312. * the configuration information for the specified DAC.
  313. * @param Channel: The selected DAC channel.
  314. * This parameter can be one of the following values:
  315. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  316. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  317. * @param pData: The destination peripheral Buffer address.
  318. * @param Length: The length of data to be transferred from memory to DAC peripheral
  319. * @param Alignment: Specifies the data alignment for DAC channel.
  320. * This parameter can be one of the following values:
  321. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  322. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  323. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  324. * @retval HAL status
  325. */
  326. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  327. {
  328. uint32_t tmpreg = 0;
  329. /* Check the parameters */
  330. assert_param(IS_DAC_CHANNEL(Channel));
  331. assert_param(IS_DAC_ALIGN(Alignment));
  332. /* Process locked */
  333. __HAL_LOCK(hdac);
  334. /* Change DAC state */
  335. hdac->State = HAL_DAC_STATE_BUSY;
  336. if(Channel == DAC_CHANNEL_1)
  337. {
  338. /* Set the DMA transfer complete callback for channel1 */
  339. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  340. /* Set the DMA half transfer complete callback for channel1 */
  341. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  342. /* Set the DMA error callback for channel1 */
  343. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  344. /* Enable the selected DAC channel1 DMA request */
  345. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  346. /* Case of use of channel 1 */
  347. switch(Alignment)
  348. {
  349. case DAC_ALIGN_12B_R:
  350. /* Get DHR12R1 address */
  351. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  352. break;
  353. case DAC_ALIGN_12B_L:
  354. /* Get DHR12L1 address */
  355. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  356. break;
  357. case DAC_ALIGN_8B_R:
  358. /* Get DHR8R1 address */
  359. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  360. break;
  361. default:
  362. break;
  363. }
  364. }
  365. else
  366. {
  367. /* Set the DMA transfer complete callback for channel2 */
  368. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  369. /* Set the DMA half transfer complete callback for channel2 */
  370. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  371. /* Set the DMA error callback for channel2 */
  372. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  373. /* Enable the selected DAC channel2 DMA request */
  374. SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  375. /* Case of use of channel 2 */
  376. switch(Alignment)
  377. {
  378. case DAC_ALIGN_12B_R:
  379. /* Get DHR12R2 address */
  380. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  381. break;
  382. case DAC_ALIGN_12B_L:
  383. /* Get DHR12L2 address */
  384. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  385. break;
  386. case DAC_ALIGN_8B_R:
  387. /* Get DHR8R2 address */
  388. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  389. break;
  390. default:
  391. break;
  392. }
  393. }
  394. /* Enable the DMA channel */
  395. if(Channel == DAC_CHANNEL_1)
  396. {
  397. /* Enable the DAC DMA underrun interrupt */
  398. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  399. /* Enable the DMA channel */
  400. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  401. }
  402. else
  403. {
  404. /* Enable the DAC DMA underrun interrupt */
  405. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  406. /* Enable the DMA channel */
  407. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  408. }
  409. /* Enable the Peripharal */
  410. __HAL_DAC_ENABLE(hdac, Channel);
  411. /* Process Unlocked */
  412. __HAL_UNLOCK(hdac);
  413. /* Return function status */
  414. return HAL_OK;
  415. }
  416. #endif /* STM32F100xB) || defined (STM32F100xE) */
  417. #if defined (STM32F100xB) || defined (STM32F100xE)
  418. /**
  419. * @brief Disables DAC and stop conversion of channel.
  420. * Note: For STM32F100x devices with specific feature: DMA underrun.
  421. * On these devices, this function disables the interruption of DMA
  422. * underrun.
  423. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  424. * the configuration information for the specified DAC.
  425. * @param Channel: The selected DAC channel.
  426. * This parameter can be one of the following values:
  427. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  428. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  429. * @retval HAL status
  430. */
  431. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  432. {
  433. HAL_StatusTypeDef status = HAL_OK;
  434. /* Check the parameters */
  435. assert_param(IS_DAC_CHANNEL(Channel));
  436. /* Disable the selected DAC channel DMA request */
  437. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
  438. /* Disable the Peripharal */
  439. __HAL_DAC_DISABLE(hdac, Channel);
  440. /* Disable the DMA Channel */
  441. /* Channel1 is used */
  442. if(Channel == DAC_CHANNEL_1)
  443. {
  444. /* Disable the DMA channel */
  445. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  446. /* Disable the DAC DMA underrun interrupt */
  447. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
  448. }
  449. else /* Channel2 is used for */
  450. {
  451. /* Disable the DMA channel */
  452. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  453. /* Disable the DAC DMA underrun interrupt */
  454. __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
  455. }
  456. /* Check if DMA Channel effectively disabled */
  457. if(status != HAL_OK)
  458. {
  459. /* Update ADC state machine to error */
  460. hdac->State = HAL_DAC_STATE_ERROR;
  461. }
  462. else
  463. {
  464. /* Change DAC state */
  465. hdac->State = HAL_DAC_STATE_READY;
  466. }
  467. /* Return function status */
  468. return status;
  469. }
  470. #endif /* STM32F100xB) || defined (STM32F100xE) */
  471. #if defined (STM32F100xB) || defined (STM32F100xE)
  472. /**
  473. * @brief Handles DAC interrupt request
  474. * Note: For STM32F100x devices with specific feature: DMA underrun.
  475. * On these devices, this function uses the interruption of DMA
  476. * underrun.
  477. * @param hdac: pointer to a DAC_HandleTypeDef structure that contains
  478. * the configuration information for the specified DAC.
  479. * @retval None
  480. */
  481. void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  482. {
  483. if(__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR1))
  484. {
  485. /* Check underrun flag of DAC channel 1 */
  486. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
  487. {
  488. /* Change DAC state to error state */
  489. hdac->State = HAL_DAC_STATE_ERROR;
  490. /* Set DAC error code to chanel1 DMA underrun error */
  491. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
  492. /* Clear the underrun flag */
  493. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
  494. /* Disable the selected DAC channel1 DMA request */
  495. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
  496. /* Error callback */
  497. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  498. }
  499. }
  500. if(__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR2))
  501. {
  502. /* Check underrun flag of DAC channel 2 */
  503. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
  504. {
  505. /* Change DAC state to error state */
  506. hdac->State = HAL_DAC_STATE_ERROR;
  507. /* Set DAC error code to channel2 DMA underrun error */
  508. SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
  509. /* Clear the underrun flag */
  510. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
  511. /* Disable the selected DAC channel1 DMA request */
  512. CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
  513. /* Error callback */
  514. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  515. }
  516. }
  517. }
  518. #endif /* STM32F100xB || STM32F100xE */
  519. /**
  520. * @}
  521. */
  522. /** @defgroup DACEx_Private_Functions DACEx Private Functions
  523. * @{
  524. */
  525. /**
  526. * @brief DMA conversion complete callback.
  527. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  528. * the configuration information for the specified DMA module.
  529. * @retval None
  530. */
  531. void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
  532. {
  533. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  534. HAL_DACEx_ConvCpltCallbackCh2(hdac);
  535. hdac->State= HAL_DAC_STATE_READY;
  536. }
  537. /**
  538. * @brief DMA half transfer complete callback.
  539. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  540. * the configuration information for the specified DMA module.
  541. * @retval None
  542. */
  543. void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
  544. {
  545. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  546. /* Conversion complete callback */
  547. HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
  548. }
  549. /**
  550. * @brief DMA error callback
  551. * @param hdma: pointer to a DMA_HandleTypeDef structure that contains
  552. * the configuration information for the specified DMA module.
  553. * @retval None
  554. */
  555. void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
  556. {
  557. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  558. /* Set DAC error code to DMA error */
  559. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  560. HAL_DACEx_ErrorCallbackCh2(hdac);
  561. hdac->State= HAL_DAC_STATE_READY;
  562. }
  563. /**
  564. * @}
  565. */
  566. #endif /* STM32F100xB || STM32F100xE || STM32F101xE || STM32F101xG || STM32F103xE || STM32F103xG || STM32F105xC || STM32F107xC */
  567. #endif /* HAL_DAC_MODULE_ENABLED */
  568. /**
  569. * @}
  570. */
  571. /**
  572. * @}
  573. */
  574. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/