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