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