stm32f4xx_hal_dac.c 33 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964
  1. /**
  2. ******************************************************************************
  3. * @file stm32f4xx_hal_dac.c
  4. * @author MCD Application Team
  5. * @brief DAC HAL module driver.
  6. * This file provides firmware functions to manage the following
  7. * functionalities of the Digital to Analog Converter (DAC) peripheral:
  8. * + Initialization and de-initialization functions
  9. * + IO operation functions
  10. * + Peripheral Control functions
  11. * + Peripheral State and Errors functions
  12. *
  13. *
  14. @verbatim
  15. ==============================================================================
  16. ##### DAC Peripheral features #####
  17. ==============================================================================
  18. [..]
  19. *** DAC Channels ***
  20. ====================
  21. [..]
  22. The device integrates two 12-bit Digital Analog Converters that can
  23. be used independently or simultaneously (dual mode):
  24. (#) DAC channel1 with DAC_OUT1 (PA4) as output
  25. (#) DAC channel2 with DAC_OUT2 (PA5) as output
  26. *** DAC Triggers ***
  27. ====================
  28. [..]
  29. Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
  30. and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
  31. [..]
  32. Digital to Analog conversion can be triggered by:
  33. (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_TRIGGER_EXT_IT9.
  34. The used pin (GPIOx_Pin9) must be configured in input mode.
  35. (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8
  36. (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
  37. (#) Software using DAC_TRIGGER_SOFTWARE
  38. *** DAC Buffer mode feature ***
  39. ===============================
  40. [..]
  41. Each DAC channel integrates an output buffer that can be used to
  42. reduce the output impedance, and to drive external loads directly
  43. without having to add an external operational amplifier.
  44. To enable, the output buffer use
  45. sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
  46. [..]
  47. (@) Refer to the device datasheet for more details about output
  48. impedance value with and without output buffer.
  49. *** DAC wave generation feature ***
  50. ===================================
  51. [..]
  52. Both DAC channels can be used to generate
  53. (#) Noise wave
  54. (#) Triangle wave
  55. *** DAC data format ***
  56. =======================
  57. [..]
  58. The DAC data format can be:
  59. (#) 8-bit right alignment using DAC_ALIGN_8B_R
  60. (#) 12-bit left alignment using DAC_ALIGN_12B_L
  61. (#) 12-bit right alignment using DAC_ALIGN_12B_R
  62. *** DAC data value to voltage correspondence ***
  63. ================================================
  64. [..]
  65. The analog output voltage on each DAC channel pin is determined
  66. by the following equation:
  67. DAC_OUTx = VREF+ * DOR / 4095
  68. with DOR is the Data Output Register
  69. VEF+ is the input voltage reference (refer to the device datasheet)
  70. e.g. To set DAC_OUT1 to 0.7V, use
  71. Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
  72. *** DMA requests ***
  73. =====================
  74. [..]
  75. A DMA1 request can be generated when an external trigger (but not
  76. a software trigger) occurs if DMA1 requests are enabled using
  77. HAL_DAC_Start_DMA()
  78. [..]
  79. DMA1 requests are mapped as following:
  80. (#) DAC channel1 : mapped on DMA1 Stream5 channel7 which must be
  81. already configured
  82. (#) DAC channel2 : mapped on DMA1 Stream6 channel7 which must be
  83. already configured
  84. -@- For Dual mode and specific signal (Triangle and noise) generation please
  85. refer to Extension Features Driver description
  86. ##### How to use this driver #####
  87. ==============================================================================
  88. [..]
  89. (+) DAC APB clock must be enabled to get write access to DAC
  90. registers using HAL_DAC_Init()
  91. (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
  92. (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
  93. (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
  94. *** Polling mode IO operation ***
  95. =================================
  96. [..]
  97. (+) Start the DAC peripheral using HAL_DAC_Start()
  98. (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
  99. (+) Stop the DAC peripheral using HAL_DAC_Stop()
  100. *** DMA mode IO operation ***
  101. ==============================
  102. [..]
  103. (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
  104. of data to be transferred at each end of conversion
  105. (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()
  106. function is executed and user can add his own code by customization of function pointer
  107. HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
  108. (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
  109. add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
  110. (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
  111. *** DAC HAL driver macros list ***
  112. =============================================
  113. [..]
  114. Below the list of most used macros in DAC HAL driver.
  115. (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
  116. (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
  117. (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
  118. (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
  119. [..]
  120. (@) You can refer to the DAC HAL driver header file for more useful macros
  121. @endverbatim
  122. ******************************************************************************
  123. * @attention
  124. *
  125. * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
  126. *
  127. * Redistribution and use in source and binary forms, with or without modification,
  128. * are permitted provided that the following conditions are met:
  129. * 1. Redistributions of source code must retain the above copyright notice,
  130. * this list of conditions and the following disclaimer.
  131. * 2. Redistributions in binary form must reproduce the above copyright notice,
  132. * this list of conditions and the following disclaimer in the documentation
  133. * and/or other materials provided with the distribution.
  134. * 3. Neither the name of STMicroelectronics nor the names of its contributors
  135. * may be used to endorse or promote products derived from this software
  136. * without specific prior written permission.
  137. *
  138. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  139. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  140. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
  141. * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
  142. * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
  143. * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
  144. * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
  145. * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
  146. * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
  147. * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  148. *
  149. ******************************************************************************
  150. */
  151. /* Includes ------------------------------------------------------------------*/
  152. #include "stm32f4xx_hal.h"
  153. /** @addtogroup STM32F4xx_HAL_Driver
  154. * @{
  155. */
  156. /** @defgroup DAC DAC
  157. * @brief DAC driver modules
  158. * @{
  159. */
  160. #ifdef HAL_DAC_MODULE_ENABLED
  161. #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
  162. defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
  163. defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\
  164. defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
  165. /* Private typedef -----------------------------------------------------------*/
  166. /* Private define ------------------------------------------------------------*/
  167. /* Private macro -------------------------------------------------------------*/
  168. /* Private variables ---------------------------------------------------------*/
  169. /** @addtogroup DAC_Private_Functions
  170. * @{
  171. */
  172. /* Private function prototypes -----------------------------------------------*/
  173. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
  174. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
  175. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma);
  176. /**
  177. * @}
  178. */
  179. /* Exported functions --------------------------------------------------------*/
  180. /** @defgroup DAC_Exported_Functions DAC Exported Functions
  181. * @{
  182. */
  183. /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
  184. * @brief Initialization and Configuration functions
  185. *
  186. @verbatim
  187. ==============================================================================
  188. ##### Initialization and de-initialization functions #####
  189. ==============================================================================
  190. [..] This section provides functions allowing to:
  191. (+) Initialize and configure the DAC.
  192. (+) De-initialize the DAC.
  193. @endverbatim
  194. * @{
  195. */
  196. /**
  197. * @brief Initializes the DAC peripheral according to the specified parameters
  198. * in the DAC_InitStruct.
  199. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  200. * the configuration information for the specified DAC.
  201. * @retval HAL status
  202. */
  203. HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
  204. {
  205. /* Check DAC handle */
  206. if(hdac == NULL)
  207. {
  208. return HAL_ERROR;
  209. }
  210. /* Check the parameters */
  211. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  212. if(hdac->State == HAL_DAC_STATE_RESET)
  213. {
  214. /* Allocate lock resource and initialize it */
  215. hdac->Lock = HAL_UNLOCKED;
  216. /* Init the low level hardware */
  217. HAL_DAC_MspInit(hdac);
  218. }
  219. /* Initialize the DAC state*/
  220. hdac->State = HAL_DAC_STATE_BUSY;
  221. /* Set DAC error code to none */
  222. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  223. /* Initialize the DAC state*/
  224. hdac->State = HAL_DAC_STATE_READY;
  225. /* Return function status */
  226. return HAL_OK;
  227. }
  228. /**
  229. * @brief Deinitializes the DAC peripheral registers to their default reset values.
  230. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  231. * the configuration information for the specified DAC.
  232. * @retval HAL status
  233. */
  234. HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
  235. {
  236. /* Check DAC handle */
  237. if(hdac == NULL)
  238. {
  239. return HAL_ERROR;
  240. }
  241. /* Check the parameters */
  242. assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
  243. /* Change DAC state */
  244. hdac->State = HAL_DAC_STATE_BUSY;
  245. /* DeInit the low level hardware */
  246. HAL_DAC_MspDeInit(hdac);
  247. /* Set DAC error code to none */
  248. hdac->ErrorCode = HAL_DAC_ERROR_NONE;
  249. /* Change DAC state */
  250. hdac->State = HAL_DAC_STATE_RESET;
  251. /* Release Lock */
  252. __HAL_UNLOCK(hdac);
  253. /* Return function status */
  254. return HAL_OK;
  255. }
  256. /**
  257. * @brief Initializes the DAC MSP.
  258. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  259. * the configuration information for the specified DAC.
  260. * @retval None
  261. */
  262. __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
  263. {
  264. /* Prevent unused argument(s) compilation warning */
  265. UNUSED(hdac);
  266. /* NOTE : This function Should not be modified, when the callback is needed,
  267. the HAL_DAC_MspInit could be implemented in the user file
  268. */
  269. }
  270. /**
  271. * @brief DeInitializes the DAC MSP.
  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_MspDeInit(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_MspDeInit could be implemented in the user file
  282. */
  283. }
  284. /**
  285. * @}
  286. */
  287. /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
  288. * @brief IO operation functions
  289. *
  290. @verbatim
  291. ==============================================================================
  292. ##### IO operation functions #####
  293. ==============================================================================
  294. [..] This section provides functions allowing to:
  295. (+) Start conversion.
  296. (+) Stop conversion.
  297. (+) Start conversion and enable DMA transfer.
  298. (+) Stop conversion and disable DMA transfer.
  299. (+) Get result of conversion.
  300. @endverbatim
  301. * @{
  302. */
  303. /**
  304. * @brief Enables DAC and starts conversion of channel.
  305. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  306. * the configuration information for the specified DAC.
  307. * @param Channel The selected DAC channel.
  308. * This parameter can be one of the following values:
  309. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  310. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  311. * @retval HAL status
  312. */
  313. HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
  314. {
  315. uint32_t tmp1 = 0U, tmp2 = 0U;
  316. /* Check the parameters */
  317. assert_param(IS_DAC_CHANNEL(Channel));
  318. /* Process locked */
  319. __HAL_LOCK(hdac);
  320. /* Change DAC state */
  321. hdac->State = HAL_DAC_STATE_BUSY;
  322. /* Enable the Peripheral */
  323. __HAL_DAC_ENABLE(hdac, Channel);
  324. if(Channel == DAC_CHANNEL_1)
  325. {
  326. tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
  327. tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
  328. /* Check if software trigger enabled */
  329. if((tmp1 == DAC_CR_TEN1) && (tmp2 == DAC_CR_TSEL1))
  330. {
  331. /* Enable the selected DAC software conversion */
  332. hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
  333. }
  334. }
  335. else
  336. {
  337. tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
  338. tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;
  339. /* Check if software trigger enabled */
  340. if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
  341. {
  342. /* Enable the selected DAC software conversion*/
  343. hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
  344. }
  345. }
  346. /* Change DAC state */
  347. hdac->State = HAL_DAC_STATE_READY;
  348. /* Process unlocked */
  349. __HAL_UNLOCK(hdac);
  350. /* Return function status */
  351. return HAL_OK;
  352. }
  353. /**
  354. * @brief Disables DAC and stop conversion of channel.
  355. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  356. * the configuration information for the specified DAC.
  357. * @param Channel The selected DAC channel.
  358. * This parameter can be one of the following values:
  359. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  360. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  361. * @retval HAL status
  362. */
  363. HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
  364. {
  365. /* Check the parameters */
  366. assert_param(IS_DAC_CHANNEL(Channel));
  367. /* Disable the Peripheral */
  368. __HAL_DAC_DISABLE(hdac, Channel);
  369. /* Change DAC state */
  370. hdac->State = HAL_DAC_STATE_READY;
  371. /* Return function status */
  372. return HAL_OK;
  373. }
  374. /**
  375. * @brief Enables DAC and starts conversion of channel.
  376. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  377. * the configuration information for the specified DAC.
  378. * @param Channel The selected DAC channel.
  379. * This parameter can be one of the following values:
  380. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  381. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  382. * @param pData The destination peripheral Buffer address.
  383. * @param Length The length of data to be transferred from memory to DAC peripheral
  384. * @param Alignment Specifies the data alignment for DAC channel.
  385. * This parameter can be one of the following values:
  386. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  387. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  388. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  389. * @retval HAL status
  390. */
  391. HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
  392. {
  393. uint32_t tmpreg = 0U;
  394. /* Check the parameters */
  395. assert_param(IS_DAC_CHANNEL(Channel));
  396. assert_param(IS_DAC_ALIGN(Alignment));
  397. /* Process locked */
  398. __HAL_LOCK(hdac);
  399. /* Change DAC state */
  400. hdac->State = HAL_DAC_STATE_BUSY;
  401. if(Channel == DAC_CHANNEL_1)
  402. {
  403. /* Set the DMA transfer complete callback for channel1 */
  404. hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
  405. /* Set the DMA half transfer complete callback for channel1 */
  406. hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
  407. /* Set the DMA error callback for channel1 */
  408. hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
  409. /* Enable the selected DAC channel1 DMA request */
  410. hdac->Instance->CR |= DAC_CR_DMAEN1;
  411. /* Case of use of channel 1 */
  412. switch(Alignment)
  413. {
  414. case DAC_ALIGN_12B_R:
  415. /* Get DHR12R1 address */
  416. tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
  417. break;
  418. case DAC_ALIGN_12B_L:
  419. /* Get DHR12L1 address */
  420. tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
  421. break;
  422. case DAC_ALIGN_8B_R:
  423. /* Get DHR8R1 address */
  424. tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
  425. break;
  426. default:
  427. break;
  428. }
  429. }
  430. else
  431. {
  432. /* Set the DMA transfer complete callback for channel2 */
  433. hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
  434. /* Set the DMA half transfer complete callback for channel2 */
  435. hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
  436. /* Set the DMA error callback for channel2 */
  437. hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
  438. /* Enable the selected DAC channel2 DMA request */
  439. hdac->Instance->CR |= DAC_CR_DMAEN2;
  440. /* Case of use of channel 2 */
  441. switch(Alignment)
  442. {
  443. case DAC_ALIGN_12B_R:
  444. /* Get DHR12R2 address */
  445. tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
  446. break;
  447. case DAC_ALIGN_12B_L:
  448. /* Get DHR12L2 address */
  449. tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
  450. break;
  451. case DAC_ALIGN_8B_R:
  452. /* Get DHR8R2 address */
  453. tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
  454. break;
  455. default:
  456. break;
  457. }
  458. }
  459. /* Enable the DMA Stream */
  460. if(Channel == DAC_CHANNEL_1)
  461. {
  462. /* Enable the DAC DMA underrun interrupt */
  463. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
  464. /* Enable the DMA Stream */
  465. HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
  466. }
  467. else
  468. {
  469. /* Enable the DAC DMA underrun interrupt */
  470. __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
  471. /* Enable the DMA Stream */
  472. HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
  473. }
  474. /* Enable the Peripheral */
  475. __HAL_DAC_ENABLE(hdac, Channel);
  476. /* Process Unlocked */
  477. __HAL_UNLOCK(hdac);
  478. /* Return function status */
  479. return HAL_OK;
  480. }
  481. /**
  482. * @brief Disables DAC and stop conversion of channel.
  483. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  484. * the configuration information for the specified DAC.
  485. * @param Channel The selected DAC channel.
  486. * This parameter can be one of the following values:
  487. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  488. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  489. * @retval HAL status
  490. */
  491. HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
  492. {
  493. HAL_StatusTypeDef status = HAL_OK;
  494. /* Check the parameters */
  495. assert_param(IS_DAC_CHANNEL(Channel));
  496. /* Disable the selected DAC channel DMA request */
  497. hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
  498. /* Disable the Peripheral */
  499. __HAL_DAC_DISABLE(hdac, Channel);
  500. /* Disable the DMA Channel */
  501. /* Channel1 is used */
  502. if(Channel == DAC_CHANNEL_1)
  503. {
  504. status = HAL_DMA_Abort(hdac->DMA_Handle1);
  505. }
  506. else /* Channel2 is used for */
  507. {
  508. status = HAL_DMA_Abort(hdac->DMA_Handle2);
  509. }
  510. /* Check if DMA Channel effectively disabled */
  511. if(status != HAL_OK)
  512. {
  513. /* Update DAC state machine to error */
  514. hdac->State = HAL_DAC_STATE_ERROR;
  515. }
  516. else
  517. {
  518. /* Change DAC state */
  519. hdac->State = HAL_DAC_STATE_READY;
  520. }
  521. /* Return function status */
  522. return status;
  523. }
  524. /**
  525. * @brief Returns the last data output value of the selected DAC channel.
  526. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  527. * the configuration information for the specified DAC.
  528. * @param Channel The selected DAC channel.
  529. * This parameter can be one of the following values:
  530. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  531. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  532. * @retval The selected DAC channel data output value.
  533. */
  534. uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
  535. {
  536. /* Check the parameters */
  537. assert_param(IS_DAC_CHANNEL(Channel));
  538. /* Returns the DAC channel data output register value */
  539. if(Channel == DAC_CHANNEL_1)
  540. {
  541. return hdac->Instance->DOR1;
  542. }
  543. else
  544. {
  545. return hdac->Instance->DOR2;
  546. }
  547. }
  548. /**
  549. * @brief Handles DAC interrupt request
  550. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  551. * the configuration information for the specified DAC.
  552. * @retval None
  553. */
  554. void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
  555. {
  556. /* Check underrun channel 1 flag */
  557. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
  558. {
  559. /* Change DAC state to error state */
  560. hdac->State = HAL_DAC_STATE_ERROR;
  561. /* Set DAC error code to channel1 DMA underrun error */
  562. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
  563. /* Clear the underrun flag */
  564. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
  565. /* Disable the selected DAC channel1 DMA request */
  566. hdac->Instance->CR &= ~DAC_CR_DMAEN1;
  567. /* Error callback */
  568. HAL_DAC_DMAUnderrunCallbackCh1(hdac);
  569. }
  570. /* Check underrun channel 2 flag */
  571. if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
  572. {
  573. /* Change DAC state to error state */
  574. hdac->State = HAL_DAC_STATE_ERROR;
  575. /* Set DAC error code to channel2 DMA underrun error */
  576. hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
  577. /* Clear the underrun flag */
  578. __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
  579. /* Disable the selected DAC channel1 DMA request */
  580. hdac->Instance->CR &= ~DAC_CR_DMAEN2;
  581. /* Error callback */
  582. HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
  583. }
  584. }
  585. /**
  586. * @brief Conversion complete callback in non blocking mode for Channel1
  587. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  588. * the configuration information for the specified DAC.
  589. * @retval None
  590. */
  591. __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  592. {
  593. /* Prevent unused argument(s) compilation warning */
  594. UNUSED(hdac);
  595. /* NOTE : This function Should not be modified, when the callback is needed,
  596. the HAL_DAC_ConvCpltCallback could be implemented in the user file
  597. */
  598. }
  599. /**
  600. * @brief Conversion half DMA transfer callback in non blocking mode for Channel1
  601. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  602. * the configuration information for the specified DAC.
  603. * @retval None
  604. */
  605. __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
  606. {
  607. /* Prevent unused argument(s) compilation warning */
  608. UNUSED(hdac);
  609. /* NOTE : This function Should not be modified, when the callback is needed,
  610. the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
  611. */
  612. }
  613. /**
  614. * @brief Error DAC callback for Channel1.
  615. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  616. * the configuration information for the specified DAC.
  617. * @retval None
  618. */
  619. __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
  620. {
  621. /* Prevent unused argument(s) compilation warning */
  622. UNUSED(hdac);
  623. /* NOTE : This function Should not be modified, when the callback is needed,
  624. the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
  625. */
  626. }
  627. /**
  628. * @brief DMA underrun DAC callback for channel1.
  629. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  630. * the configuration information for the specified DAC.
  631. * @retval None
  632. */
  633. __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
  634. {
  635. /* Prevent unused argument(s) compilation warning */
  636. UNUSED(hdac);
  637. /* NOTE : This function Should not be modified, when the callback is needed,
  638. the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
  639. */
  640. }
  641. /**
  642. * @}
  643. */
  644. /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
  645. * @brief Peripheral Control functions
  646. *
  647. @verbatim
  648. ==============================================================================
  649. ##### Peripheral Control functions #####
  650. ==============================================================================
  651. [..] This section provides functions allowing to:
  652. (+) Configure channels.
  653. (+) Set the specified data holding register value for DAC channel.
  654. @endverbatim
  655. * @{
  656. */
  657. /**
  658. * @brief Configures the selected DAC channel.
  659. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  660. * the configuration information for the specified DAC.
  661. * @param sConfig DAC configuration structure.
  662. * @param Channel The selected DAC channel.
  663. * This parameter can be one of the following values:
  664. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  665. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  666. * @retval HAL status
  667. */
  668. HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
  669. {
  670. uint32_t tmpreg1 = 0U, tmpreg2 = 0U;
  671. /* Check the DAC parameters */
  672. assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
  673. assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
  674. assert_param(IS_DAC_CHANNEL(Channel));
  675. /* Process locked */
  676. __HAL_LOCK(hdac);
  677. /* Change DAC state */
  678. hdac->State = HAL_DAC_STATE_BUSY;
  679. /* Get the DAC CR value */
  680. tmpreg1 = hdac->Instance->CR;
  681. /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
  682. tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
  683. /* Configure for the selected DAC channel: buffer output, trigger */
  684. /* Set TSELx and TENx bits according to DAC_Trigger value */
  685. /* Set BOFFx bit according to DAC_OutputBuffer value */
  686. tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
  687. /* Calculate CR register value depending on DAC_Channel */
  688. tmpreg1 |= tmpreg2 << Channel;
  689. /* Write to DAC CR */
  690. hdac->Instance->CR = tmpreg1;
  691. /* Disable wave generation */
  692. hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
  693. /* Change DAC state */
  694. hdac->State = HAL_DAC_STATE_READY;
  695. /* Process unlocked */
  696. __HAL_UNLOCK(hdac);
  697. /* Return function status */
  698. return HAL_OK;
  699. }
  700. /**
  701. * @brief Set the specified data holding register value for DAC channel.
  702. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  703. * the configuration information for the specified DAC.
  704. * @param Channel The selected DAC channel.
  705. * This parameter can be one of the following values:
  706. * @arg DAC_CHANNEL_1: DAC Channel1 selected
  707. * @arg DAC_CHANNEL_2: DAC Channel2 selected
  708. * @param Alignment Specifies the data alignment.
  709. * This parameter can be one of the following values:
  710. * @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
  711. * @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
  712. * @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
  713. * @param Data Data to be loaded in the selected data holding register.
  714. * @retval HAL status
  715. */
  716. HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
  717. {
  718. __IO uint32_t tmp = 0U;
  719. /* Check the parameters */
  720. assert_param(IS_DAC_CHANNEL(Channel));
  721. assert_param(IS_DAC_ALIGN(Alignment));
  722. assert_param(IS_DAC_DATA(Data));
  723. tmp = (uint32_t)hdac->Instance;
  724. if(Channel == DAC_CHANNEL_1)
  725. {
  726. tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
  727. }
  728. else
  729. {
  730. tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
  731. }
  732. /* Set the DAC channel1 selected data holding register */
  733. *(__IO uint32_t *) tmp = Data;
  734. /* Return function status */
  735. return HAL_OK;
  736. }
  737. /**
  738. * @}
  739. */
  740. /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
  741. * @brief Peripheral State and Errors functions
  742. *
  743. @verbatim
  744. ==============================================================================
  745. ##### Peripheral State and Errors functions #####
  746. ==============================================================================
  747. [..]
  748. This subsection provides functions allowing to
  749. (+) Check the DAC state.
  750. (+) Check the DAC Errors.
  751. @endverbatim
  752. * @{
  753. */
  754. /**
  755. * @brief return the DAC state
  756. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  757. * the configuration information for the specified DAC.
  758. * @retval HAL state
  759. */
  760. HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
  761. {
  762. /* Return DAC state */
  763. return hdac->State;
  764. }
  765. /**
  766. * @brief Return the DAC error code
  767. * @param hdac pointer to a DAC_HandleTypeDef structure that contains
  768. * the configuration information for the specified DAC.
  769. * @retval DAC Error Code
  770. */
  771. uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
  772. {
  773. return hdac->ErrorCode;
  774. }
  775. /**
  776. * @}
  777. */
  778. /**
  779. * @brief DMA conversion complete callback.
  780. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  781. * the configuration information for the specified DMA module.
  782. * @retval None
  783. */
  784. static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
  785. {
  786. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  787. HAL_DAC_ConvCpltCallbackCh1(hdac);
  788. hdac->State= HAL_DAC_STATE_READY;
  789. }
  790. /**
  791. * @brief DMA half transfer complete callback.
  792. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  793. * the configuration information for the specified DMA module.
  794. * @retval None
  795. */
  796. static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
  797. {
  798. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  799. /* Conversion complete callback */
  800. HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
  801. }
  802. /**
  803. * @brief DMA error callback
  804. * @param hdma pointer to a DMA_HandleTypeDef structure that contains
  805. * the configuration information for the specified DMA module.
  806. * @retval None
  807. */
  808. static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
  809. {
  810. DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
  811. /* Set DAC error code to DMA error */
  812. hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
  813. HAL_DAC_ErrorCallbackCh1(hdac);
  814. hdac->State= HAL_DAC_STATE_READY;
  815. }
  816. /**
  817. * @}
  818. */
  819. #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
  820. STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
  821. STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx ||\
  822. STM32F413xx || STM32F423xx */
  823. #endif /* HAL_DAC_MODULE_ENABLED */
  824. /**
  825. * @}
  826. */
  827. /**
  828. * @}
  829. */
  830. /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/