]> git.donarmstrong.com Git - qmk_firmware.git/blob - tool/mbed/mbed-sdk/libraries/mbed/targets/cmsis/TARGET_STM/TARGET_STM32F3/stm32f3xx_hal_dac_ex.c
Squashed 'tmk_core/' changes from 7967731..b9e0ea0
[qmk_firmware.git] / tool / mbed / mbed-sdk / libraries / mbed / targets / cmsis / TARGET_STM / TARGET_STM32F3 / stm32f3xx_hal_dac_ex.c
1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_hal_dac_ex.c
4   * @author  MCD Application Team
5   * @version V1.1.0
6   * @date    12-Sept-2014
7   * @brief   Extended DAC HAL module driver.
8   *
9   *          This file provides firmware functions to manage the following
10   *          functionalities of the Power Controller (DAC) peripheral:
11   *           + Initialization and de-initialization functions
12   *           + Peripheral Control functions
13   @verbatim   
14
15  @endverbatim    
16   ******************************************************************************
17   * @attention
18   *
19   * <h2><center>&copy; COPYRIGHT(c) 2014 STMicroelectronics</center></h2>
20   *
21   * Redistribution and use in source and binary forms, with or without modification,
22   * are permitted provided that the following conditions are met:
23   *   1. Redistributions of source code must retain the above copyright notice,
24   *      this list of conditions and the following disclaimer.
25   *   2. Redistributions in binary form must reproduce the above copyright notice,
26   *      this list of conditions and the following disclaimer in the documentation
27   *      and/or other materials provided with the distribution.
28   *   3. Neither the name of STMicroelectronics nor the names of its contributors
29   *      may be used to endorse or promote products derived from this software
30   *      without specific prior written permission.
31   *
32   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
33   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
34   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
35   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
36   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
37   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
38   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
39   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
40   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
41   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
42   *
43   ******************************************************************************  
44   */
45
46 /* Includes ------------------------------------------------------------------*/
47 #include "stm32f3xx_hal.h"
48
49 /** @addtogroup STM32F3xx_HAL_Driver
50   * @{
51   */
52
53 #ifdef HAL_DAC_MODULE_ENABLED
54
55 /** @defgroup DACEx DAC Extended HAL module driver
56   * @brief DAC HAL module driver
57   * @{
58   */
59
60 /* Private typedef -----------------------------------------------------------*/
61 /* Private define ------------------------------------------------------------*/
62 /* Private macro -------------------------------------------------------------*/
63 /* Private variables ---------------------------------------------------------*/
64 /* Private function prototypes -----------------------------------------------*/
65 /** @defgroup DACEx_Private_Functions DAC Extended Private Functions
66   * @{
67   */
68 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
69 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
70 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma); 
71
72 #if defined(STM32F303xE) || defined(STM32F398xx)                         || \
73     defined(STM32F303xC) || defined(STM32F358xx)                         || \
74     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
75     defined(STM32F373xC) || defined(STM32F378xx) 
76 /* DAC channel 2 is available on top of DAC channel 1 */
77 static void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma);
78 static void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma);
79 static void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma); 
80 #endif /* STM32F303xE || STM32F398xx                || */
81        /* STM32F303xC || STM32F358xx                || */
82        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
83        /* STM32F373xC || STM32F378xx                   */
84 /**
85   * @}
86   */
87
88 /**
89   * @}
90   */
91
92 /** @addtogroup DAC DAC HAL module driver
93   * @brief DAC HAL module driver
94   * @{
95   */ 
96
97 /* Exported functions ---------------------------------------------------------*/
98 /** @addtogroup DAC_Exported_Functions DAC Exported Functions
99   * @{
100   */
101
102 /** @addtogroup DAC_Exported_Functions_Group3 Peripheral Control functions
103  *  @brief      Peripheral Control functions 
104  *
105 @verbatim   
106   ==============================================================================
107              ##### Peripheral Control functions #####
108   ==============================================================================  
109     [..]  This section provides functions allowing to:
110       (+) Set the specified data holding register value for DAC channel.
111       
112 @endverbatim
113   * @{
114   */
115
116 /**
117   * @brief  Set the specified data holding register value for DAC channel.
118   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
119   *         the configuration information for the specified DAC.
120   * @param  channel: The selected DAC channel. 
121   * @param  alignment: Specifies the data alignment for DAC channel.
122   *          This parameter can be one of the following values:
123   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
124   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
125   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
126   * @param  data: Data to be loaded in the selected data holding register.
127   * @retval HAL status
128   */
129 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t alignment, uint32_t data)
130 {  
131   __IO uint32_t tmp = 0;
132   
133   /* Check the parameters */
134   assert_param(IS_DAC_CHANNEL(channel));
135   assert_param(IS_DAC_ALIGN(alignment));
136   assert_param(IS_DAC_DATA(data));
137    
138   tmp = (uint32_t) (hdac->Instance);
139
140 /* DAC 1 has 1 or 2 channels - no DAC2 */
141 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
142
143   if(channel == DAC_CHANNEL_1)
144   {
145     tmp += __HAL_DHR12R1_ALIGNEMENT(alignment);
146   }
147 #if defined(STM32F303xE) || defined(STM32F398xx)                         || \
148     defined(STM32F303xC) || defined(STM32F358xx)                         || \
149     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
150     defined(STM32F373xC) || defined(STM32F378xx)
151   else /* channel = DAC_CHANNEL_2  */
152   {
153     tmp += __HAL_DHR12R2_ALIGNEMENT(alignment);
154   }
155 #endif /* STM32F303xE || STM32F398xx                || */
156        /* STM32F303xC || STM32F358xx                || */
157        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
158        /* STM32F373xC || STM32F378xx                   */
159
160   /* Set the DAC channel1 selected data holding register */
161   *(__IO uint32_t *) tmp = data;
162   
163   /* Return function status */
164   return HAL_OK;
165 }
166
167 /**
168   * @}
169   */
170
171 /** @addtogroup DAC_Exported_Functions_Group2 Input and Output operation functions
172  *  @brief    IO operation functions 
173  *
174 @verbatim   
175   ==============================================================================
176              ##### IO operation functions #####
177   ==============================================================================  
178     [..]  This section provides functions allowing to:
179       (+) Start conversion.
180       (+) Stop conversion.
181       (+) Start conversion and enable DMA transfer.
182       (+) Stop conversion and disable DMA transfer.
183       (+) Get result of conversion.
184                      
185 @endverbatim
186   * @{
187   */
188
189 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
190 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
191      defined(STM32F303xC) || defined(STM32F358xx)                         || \
192      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
193      defined(STM32F373xC) || defined(STM32F378xx)
194 /* DAC 1 has 2 channels 1 & 2 */
195
196 /**
197   * @brief  Enables DAC and starts conversion of channel.
198   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
199   *         the configuration information for the specified DAC.
200   * @param  channel: The selected DAC channel. 
201   *          This parameter can be one of the following values:
202   *            @arg DAC_CHANNEL_1: DAC1 Channel1 or DAC2 Channel1 selected
203   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
204   * @retval HAL status
205   */
206
207 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t channel)
208 {
209   /* Check the parameters */
210   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
211   
212   /* Process locked */
213   __HAL_LOCK(hdac);
214   
215   /* Change DAC state */
216   hdac->State = HAL_DAC_STATE_BUSY;
217   
218   /* Enable the Peripharal */
219   __HAL_DAC_ENABLE(hdac, channel);
220   
221   if(channel == DAC_CHANNEL_1)
222   {
223     /* Check if software trigger enabled */
224     if(((hdac->Instance->CR & DAC_CR_TEN1) ==  DAC_CR_TEN1) && ((hdac->Instance->CR & DAC_CR_TSEL1) ==  DAC_CR_TSEL1))
225     {
226       /* Enable the selected DAC software conversion */
227       hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
228     }
229   }
230   else
231   {
232     /* Check if software trigger enabled */
233     if(((hdac->Instance->CR & DAC_CR_TEN2) == DAC_CR_TEN2) && ((hdac->Instance->CR & DAC_CR_TSEL2) == DAC_CR_TSEL2))
234     {
235       /* Enable the selected DAC software conversion*/
236       hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
237     }
238   }
239   
240   /* Change DAC state */
241   hdac->State = HAL_DAC_STATE_READY;
242   
243   /* Process unlocked */
244   __HAL_UNLOCK(hdac);
245     
246   /* Return function status */
247   return HAL_OK;
248 }
249 #endif /* STM32F303xE || STM32F398xx                || */
250        /* STM32F303xC || STM32F358xx                || */
251        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
252        /* STM32F373xC || STM32F378xx                   */
253
254 #if defined(STM32F302xE) || \
255     defined(STM32F302xC) || \
256     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
257 /* DAC 1 has 1 channels 1 */
258
259 /**
260   * @brief  Enables DAC and starts conversion of channel.
261   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
262   *         the configuration information for the specified DAC.
263   * @param  channel: The selected DAC channel. 
264   *          This parameter can be one of the following values:
265   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
266   * @retval HAL status
267   */
268
269 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t channel)
270 {
271   /* Check the parameters */
272   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
273   
274   /* Process locked */
275   __HAL_LOCK(hdac);
276   
277   /* Change DAC state */
278   hdac->State = HAL_DAC_STATE_BUSY;
279   
280   /* Enable the Peripharal */
281   __HAL_DAC_ENABLE(hdac, channel);
282   
283   /* Check if software trigger enabled */
284   if(((hdac->Instance->CR & DAC_CR_TEN1) ==  DAC_CR_TEN1) && ((hdac->Instance->CR & DAC_CR_TSEL1) ==  DAC_CR_TSEL1))
285   {
286     /* Enable the selected DAC software conversion */
287     hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
288   }
289   
290   /* Change DAC state */
291   hdac->State = HAL_DAC_STATE_READY;
292   
293   /* Process unlocked */
294   __HAL_UNLOCK(hdac);
295     
296   /* Return function status */
297   return HAL_OK;
298 }
299 #endif /* STM32F302xE                               || */
300        /* STM32F302xC                               || */
301        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
302
303 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
304 #if defined(STM32F303xE) || defined(STM32F398xx)                         || \
305     defined(STM32F303xC) || defined(STM32F358xx)                         || \
306     defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
307     defined(STM32F373xC) || defined(STM32F378xx)
308     
309 /* DAC 1 has 2 channels 1 & 2 */
310
311 /**
312   * @brief  Enables DAC and starts conversion of channel.
313   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
314   *         the configuration information for the specified DAC.
315   * @param  channel: The selected DAC channel. 
316   *          This parameter can be one of the following values:
317   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
318   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
319   * @param  pData: The destination peripheral Buffer address.
320   * @param  Length: The length of data to be transferred from memory to DAC peripheral
321   * @param  alignment: Specifies the data alignment for DAC channel.
322   *          This parameter can be one of the following values:
323   *            @arg DAC_Align_8b_R: 8bit right data alignment selected
324   *            @arg DAC_Align_12b_L: 12bit left data alignment selected
325   *            @arg DAC_Align_12b_R: 12bit right data alignment selected
326   * @retval HAL status
327   */
328
329 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t* pData, uint32_t Length, uint32_t alignment)
330 {
331   uint32_t tmpreg = 0;
332     
333   /* Check the parameters */
334   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
335   assert_param(IS_DAC_ALIGN(alignment));
336   
337   /* Process locked */
338   __HAL_LOCK(hdac);
339   
340   /* Change DAC state */
341   hdac->State = HAL_DAC_STATE_BUSY;
342
343   if(channel == DAC_CHANNEL_1)
344   {
345     /* Set the DMA transfer complete callback for channel1 */
346     hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
347     
348     /* Set the DMA half transfer complete callback for channel1 */
349     hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
350       
351     /* Set the DMA error callback for channel1 */
352     hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
353   }
354   else
355   {
356     /* Set the DMA transfer complete callback for channel2 */
357     hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
358     
359     /* Set the DMA half transfer complete callback for channel2 */
360     hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
361        
362     /* Set the DMA error callback for channel2 */
363     hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
364   }
365   
366  if(channel == DAC_CHANNEL_1)
367   {
368     /* Enable the selected DAC channel1 DMA request */
369     hdac->Instance->CR |= DAC_CR_DMAEN1;
370     
371     /* Case of use of channel 1 */
372     switch(alignment)
373     {
374       case DAC_ALIGN_12B_R:
375         /* Get DHR12R1 address */
376         tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
377         break;
378       case DAC_ALIGN_12B_L:
379         /* Get DHR12L1 address */
380         tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
381         break;
382       case DAC_ALIGN_8B_R:
383         /* Get DHR8R1 address */
384         tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
385         break;
386       default:
387         break;
388     }
389   }
390   else
391   {
392     /* Enable the selected DAC channel2 DMA request */
393     hdac->Instance->CR |= DAC_CR_DMAEN2;
394     
395     /* Case of use of channel 2 */
396     switch(alignment)
397     {
398       case DAC_ALIGN_12B_R:
399         /* Get DHR12R2 address */
400         tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
401         break;
402       case DAC_ALIGN_12B_L:
403         /* Get DHR12L2 address */
404         tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
405         break;
406       case DAC_ALIGN_8B_R:
407         /* Get DHR8R2 address */
408         tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
409         break;
410       default:
411         break;
412     }
413   }
414   
415   /* Enable the DMA Channel */
416   if(channel == DAC_CHANNEL_1)
417   {
418     /* Enable the DAC DMA underrun interrupt */
419     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
420
421     /* Enable the DMA Channel */
422     HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
423   } 
424   else
425   {
426     /* Enable the DAC DMA underrun interrupt */
427     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
428
429     /* Enable the DMA Channel */
430     HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
431   }
432  
433   /* Enable the Peripheral */
434   __HAL_DAC_ENABLE(hdac, channel);
435   
436   /* Process Unlocked */
437   __HAL_UNLOCK(hdac);
438   
439   /* Return function status */
440   return HAL_OK;
441 }
442 #endif /* STM32F303xE || STM32F398xx                || */
443        /* STM32F303xC || STM32F358xx                || */
444        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
445        /* STM32F373xC || STM32F378xx                   */
446
447 #if defined(STM32F302xE) || \
448     defined(STM32F302xC) || \
449     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
450 /* DAC 1 has 1 channels 1 */
451
452 /**
453   * @brief  Enables DAC and starts conversion of channel.
454   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
455   *         the configuration information for the specified DAC.
456   * @param  channel: The selected DAC channel. 
457   *          This parameter can be one of the following values:
458   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
459   * @param  pData: The destination peripheral Buffer address.
460   * @param  Length: The length of data to be transferred from memory to DAC peripheral
461   * @param  alignment: Specifies the data alignment for DAC channel.
462   *          This parameter can be one of the following values:
463   *            @arg DAC_Align_8b_R: 8bit right data alignment selected
464   *            @arg DAC_Align_12b_L: 12bit left data alignment selected
465   *            @arg DAC_Align_12b_R: 12bit right data alignment selected
466   * @retval HAL status
467   */
468
469 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t* pData, uint32_t Length, uint32_t alignment)
470 {
471   uint32_t tmpreg = 0;
472     
473   /* Check the parameters */
474   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
475   assert_param(IS_DAC_ALIGN(alignment));
476   
477   /* Process locked */
478   __HAL_LOCK(hdac);
479   
480   /* Change DAC state */
481   hdac->State = HAL_DAC_STATE_BUSY;
482     
483   /* Set the DMA transfer complete callback for channel1 */
484   hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
485   
486   /* Set the DMA half transfer complete callback for channel1 */
487   hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
488     
489   /* Set the DMA error callback for channel1 */
490   hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
491
492   /* Enable the selected DAC channel1 DMA request */
493   hdac->Instance->CR |= DAC_CR_DMAEN1;
494     
495   /* Case of use of channel 1 */
496   switch(alignment)
497   {
498     case DAC_ALIGN_12B_R:
499       /* Get DHR12R1 address */
500       tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
501       break;
502     case DAC_ALIGN_12B_L:
503       /* Get DHR12L1 address */
504       tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
505       break;
506     case DAC_ALIGN_8B_R:
507       /* Get DHR8R1 address */
508       tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
509       break;
510     default:
511       break;
512   }
513   
514   /* Enable the DMA Channel */
515   /* Enable the DAC DMA underrun interrupt */
516   __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
517
518   /* Enable the DMA Channel */
519   HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
520  
521   /* Enable the Peripheral */
522   __HAL_DAC_ENABLE(hdac, channel);
523   
524   /* Process Unlocked */
525   __HAL_UNLOCK(hdac);
526   
527   /* Return function status */
528   return HAL_OK;
529 }
530 #endif /* STM32F302xE                               || */
531        /* STM32F302xC                               || */
532        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
533
534 /* DAC 1 has 2 channels 1 & 2 - DAC 2 has one channel 1 */
535 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
536      defined(STM32F303xC) || defined(STM32F358xx)                         || \
537      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
538      defined(STM32F373xC) || defined(STM32F378xx)
539      
540 /* DAC 1 has 2 channels 1 & 2 */
541
542 /**
543   * @brief  Returns the last data output value of the selected DAC channel.
544   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
545   *         the configuration information for the specified DAC.
546   * @param  channel: The selected DAC channel. 
547   *          This parameter can be one of the following values:
548   *            @arg DAC_CHANNEL_1: DAC1 Channel1 or DAC2 Channel1 selected
549   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
550   * @retval The selected DAC channel data output value.
551   */
552
553
554 /**
555   * @brief  Returns the last data output value of the selected DAC channel.
556   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
557   *         the configuration information for the specified DAC.
558   * @param  channel: The selected DAC channel. 
559   *          This parameter can be one of the following values:
560   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
561   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
562   * @retval The selected DAC channel data output value.
563   */
564 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t channel)
565 {
566   /* Check the parameters */
567   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
568   
569   /* Returns the DAC channel data output register value */
570   if(channel == DAC_CHANNEL_1) 
571   {
572     return hdac->Instance->DOR1;
573   }
574   else /* channel = DAC_CHANNEL_2  */
575   {
576     return hdac->Instance->DOR2;
577   }
578 }
579
580 #endif /* STM32F303xE || STM32F398xx                || */
581        /* STM32F303xC || STM32F358xx                || */
582        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
583        /* STM32F373xC || STM32F378xx                   */
584
585 #if defined(STM32F302xE) || \
586     defined(STM32F302xC) || \
587     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
588 /* DAC 1 has 1 channel (channel 1)  */
589 /**
590   * @brief  Returns the last data output value of the selected DAC channel.
591   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
592   *         the configuration information for the specified DAC.
593   * @param  channel: The selected DAC channel. 
594   *          This parameter can be one of the following values:
595   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
596   * @retval The selected DAC channel data output value.
597   */
598 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t channel)
599 {
600   /* Check the parameters */
601   assert_param(IS_DAC_CHANNEL_INSTANCE(hdac->Instance, channel));
602   
603   /* Returns the DAC channel data output register value */
604   return hdac->Instance->DOR1;
605 }
606 #endif /* STM32F302xE                               || */
607        /* STM32F302xC                               || */
608        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
609
610 #if defined(STM32F302xE) || \
611     defined(STM32F302xC) || \
612     defined(STM32F301x8) || defined(STM32F302x8) || defined(STM32F318xx)
613 /* DAC channel 2 is NOT available. Only DAC channel 1 is available */
614
615 /**
616   * @brief  Handles DAC interrupt request  
617   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
618   *         the configuration information for the specified DAC.
619   * @retval None
620   */
621
622 void HAL_DAC_IRQHandler(struct __DAC_HandleTypeDef* hdac)
623 {
624   /* Check Overrun flag */
625   if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
626   {
627     /* Change DAC state to error state */
628     hdac->State = HAL_DAC_STATE_ERROR;
629     
630     /* Set DAC error code to chanel1 DMA underrun error */
631     hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
632     
633     /* Clear the underrun flag */
634     __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
635     
636     /* Disable the selected DAC channel1 DMA request */
637     hdac->Instance->CR &= ~DAC_CR_DMAEN1;
638     
639     /* Error callback */ 
640     HAL_DAC_DMAUnderrunCallbackCh1(hdac);
641   }
642 }
643 #endif /* STM32F302xE                               || */
644        /* STM32F302xC                               || */
645        /* STM32F301x8 || STM32F302x8 || STM32F318xx    */
646
647 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
648      defined(STM32F303xC) || defined(STM32F358xx)                         || \
649      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
650      defined(STM32F373xC) || defined(STM32F378xx)
651 /* DAC channel 2 is available on top of DAC channel 1 */
652
653 /**
654   * @brief  Handles DAC interrupt request  
655   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
656   *         the configuration information for the specified DAC.
657   * @retval None
658   */
659 void HAL_DAC_IRQHandler(struct __DAC_HandleTypeDef* hdac)
660 {
661   /* Check Overrun flag */
662   if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
663   {
664     /* Change DAC state to error state */
665     hdac->State = HAL_DAC_STATE_ERROR;
666     
667     /* Set DAC error code to chanel1 DMA underrun error */
668     hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
669     
670     /* Clear the underrun flag */
671     __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
672     
673     /* Disable the selected DAC channel1 DMA request */
674     hdac->Instance->CR &= ~DAC_CR_DMAEN1;
675     
676     /* Error callback */ 
677     HAL_DAC_DMAUnderrunCallbackCh1(hdac);
678   }
679   else
680   {
681     if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
682     {
683       /* Change DAC state to error state */
684       hdac->State = HAL_DAC_STATE_ERROR;
685     
686       /* Set DAC error code to channel2 DMA underrun error */
687       hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
688     
689       /* Clear the underrun flag */
690       __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
691     
692       /* Disable the selected DAC channel1 DMA request */
693       hdac->Instance->CR &= ~DAC_CR_DMAEN2;
694     
695       /* Error callback */ 
696       HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
697     }
698   }
699 }
700 #endif /* STM32F303xE || STM32F398xx                || */
701        /* STM32F303xC || STM32F358xx                || */
702        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
703        /* STM32F373xC || STM32F378xx                   */
704
705 /**
706   * @}
707   */
708
709 /**
710   * @}
711   */
712
713 /**
714   * @}
715   */
716
717 /** @addtogroup DACEx
718   * @brief DACEx Extended HAL module driver
719   * @{
720   */
721 /* Exported functions ---------------------------------------------------------*/
722
723 /** @addtogroup DACEx_Exported_Functions DAC Extended Exported Functions
724   * @{
725   */
726
727 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
728      defined(STM32F303xC) || defined(STM32F358xx)                         || \
729      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
730      defined(STM32F373xC) || defined(STM32F378xx)
731 /* DAC channel 2 is present in DAC 1 */
732 /**
733   * @brief  Set the specified data holding register value for dual DAC channel.
734   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
735   *         the configuration information for the specified DAC.
736   * @param  alignment: Specifies the data alignment for dual channel DAC.
737   *          This parameter can be one of the following values:
738   *            @arg DAC_Align_8b_R: 8bit right data alignment selected
739   *            @arg DAC_Align_12b_L: 12bit left data alignment selected
740   *            @arg DAC_Align_12b_R: 12bit right data alignment selected
741   * @param  data2: Data for DAC Channel2 to be loaded in the selected data holding register.
742   * @param  data1: Data for DAC Channel1 to be loaded in the selected data  holding register.
743   * @note   In dual mode, a unique register access is required to write in both
744   *          DAC channels at the same time.
745   * @retval HAL status
746   */
747 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t alignment, uint32_t data1, uint32_t data2)
748 {
749   uint32_t data = 0, tmp = 0;
750
751   /* Check the parameters */
752   assert_param(IS_DAC_ALIGN(alignment));
753   assert_param(IS_DAC_DATA(data1));
754   assert_param(IS_DAC_DATA(data2));
755
756   /* Calculate and set dual DAC data holding register value */
757   if (alignment == DAC_ALIGN_8B_R)
758   {
759     data = ((uint32_t)data2 << 8) | data1;
760   }
761   else
762   {
763     data = ((uint32_t)data2 << 16) | data1;
764   }
765
766     tmp = (uint32_t) (hdac->Instance);
767     tmp += __HAL_DHR12RD_ALIGNEMENT(alignment);
768
769   /* Set the dual DAC selected data holding register */
770   *(__IO uint32_t *)tmp = data;
771
772   /* Return function status */
773   return HAL_OK;
774 }
775 #endif /* STM32F303xE || STM32F398xx                || */
776        /* STM32F303xC || STM32F358xx                || */
777        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
778        /* STM32F373xC || STM32F378xx                   */
779
780 /**
781   * @brief  Returns the last data output value of the selected DAC channel.
782   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
783   *         the configuration information for the specified DAC.
784   * @retval The selected DAC channel data output value.
785   */
786 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac)
787 {
788   uint32_t tmp = 0;
789
790   tmp |= hdac->Instance->DOR1;
791
792 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
793      defined(STM32F303xC) || defined(STM32F358xx)                         || \
794      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
795      defined(STM32F373xC) || defined(STM32F378xx)
796 /* DAC channel 2 is present in DAC 1 */
797   tmp |= hdac->Instance->DOR2 << 16;
798 #endif /* STM32F303xE || STM32F398xx                || */
799        /* STM32F303xC || STM32F358xx                || */
800        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
801        /* STM32F373xC || STM32F378xx                   */
802
803   /* Returns the DAC channel data output register value */
804   return tmp;
805 }
806
807 /**
808   * @brief  Enables or disables the selected DAC channel wave generation.
809   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
810   *         the configuration information for the specified DAC.
811   * @param  channel: The selected DAC channel.
812   *          This parameter can be one of the following values:
813   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
814   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
815   * @param  Amplitude: Select max triangle amplitude.
816   *          This parameter can be one of the following values:
817   *            @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
818   *            @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
819   *            @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
820   *            @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
821   *            @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
822   *            @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
823   *            @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
824   *            @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
825   *            @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
826   *            @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
827   *            @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
828   *            @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
829   * @note   Wave generation is not available in DAC2.
830   * @retval HAL status
831   */
832 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t Amplitude)
833 {
834   /* Check the parameters */
835   assert_param(IS_DAC_CHANNEL(channel));
836   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
837
838   /* Process locked */
839   __HAL_LOCK(hdac);
840
841   /* Change DAC state */
842   hdac->State = HAL_DAC_STATE_BUSY;
843
844   /* Enable the selected wave generation for the selected DAC channel */
845   hdac->Instance->CR |= (DAC_WAVE_TRIANGLE | Amplitude) << channel;
846
847   /* Change DAC state */
848   hdac->State = HAL_DAC_STATE_READY;
849
850   /* Process unlocked */
851   __HAL_UNLOCK(hdac);
852
853   /* Return function status */
854   return HAL_OK;
855 }
856
857 /**
858   * @brief  Enables or disables the selected DAC channel wave generation.
859   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
860   *         the configuration information for the specified DAC.
861   * @param  channel: The selected DAC channel.
862   *          This parameter can be one of the following values:
863   *            @arg DAC_CHANNEL_1: DAC1 Channel1 selected
864   *            @arg DAC_CHANNEL_2: DAC1 Channel2 selected
865   * @param  Amplitude: Unmask DAC channel LFSR for noise wave generation.
866   *          This parameter can be one of the following values:
867   *            @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
868   *            @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
869   *            @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
870   *            @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
871   *            @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
872   *            @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
873   *            @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
874   *            @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
875   *            @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
876   *            @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
877   *            @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
878   *            @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
879   * @retval HAL status
880   */
881 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t channel, uint32_t Amplitude)
882 {
883   /* Check the parameters */
884   assert_param(IS_DAC_CHANNEL(channel));
885   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
886
887   /* Process locked */
888   __HAL_LOCK(hdac);
889
890   /* Change DAC state */
891   hdac->State = HAL_DAC_STATE_BUSY;
892
893   /* Enable the selected wave generation for the selected DAC channel */
894   hdac->Instance->CR |= (DAC_WAVE_NOISE | Amplitude) << channel;
895
896   /* Change DAC state */
897   hdac->State = HAL_DAC_STATE_READY;
898
899   /* Process unlocked */
900   __HAL_UNLOCK(hdac);
901
902   /* Return function status */
903   return HAL_OK;
904 }
905
906 /**
907   * @brief  Conversion complete callback in non blocking mode for Channel2
908   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
909   *         the configuration information for the specified DAC.
910   * @retval None
911   */
912 __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac)
913 {
914   /* NOTE : This function Should not be modified, when the callback is needed,
915             the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
916    */
917 }
918
919 /**
920   * @brief  Conversion half DMA transfer callback in non blocking mode for Channel2
921   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
922   *         the configuration information for the specified DAC.
923   * @retval None
924   */
925 __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac)
926 {
927   /* NOTE : This function Should not be modified, when the callback is needed,
928             the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
929    */
930 }
931
932 /**
933   * @brief  Error DAC callback for Channel2.
934   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
935   *         the configuration information for the specified DAC.
936   * @retval None
937   */
938 __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
939 {
940   /* NOTE : This function Should not be modified, when the callback is needed,
941             the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
942    */
943 }
944
945 /**
946   * @brief  DMA underrun DAC callback for channel2.
947   * @param  hdac: pointer to a DAC_HandleTypeDef structure that contains
948   *         the configuration information for the specified DAC.
949   * @retval None
950   */
951 __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
952 {
953   /* NOTE : This function Should not be modified, when the callback is needed,
954             the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
955    */
956 }
957
958 /**
959   * @brief  DMA conversion complete callback. 
960   * @param  hdma: pointer to DMA handle.
961   * @retval None
962   */
963 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)   
964 {
965   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
966   
967   HAL_DAC_ConvCpltCallbackCh1(hdac); 
968   
969   hdac->State= HAL_DAC_STATE_READY;
970 }
971
972 /**
973   * @brief  DMA half transfer complete callback. 
974   * @param  hdma: pointer to DMA handle.
975   * @retval None
976   */
977 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)   
978 {
979     DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
980     /* Conversion complete callback */
981     HAL_DAC_ConvHalfCpltCallbackCh1(hdac); 
982 }
983
984 /**
985   * @brief  DMA error callback 
986   * @param  hdma: pointer to DMA handle.
987   * @retval None
988   */
989 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)   
990 {
991   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
992     
993   /* Set DAC error code to DMA error */
994   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
995     
996   HAL_DAC_ErrorCallbackCh1(hdac); 
997     
998   hdac->State= HAL_DAC_STATE_READY;
999 }
1000
1001 #if  defined(STM32F303xE) || defined(STM32F398xx)                         || \
1002      defined(STM32F303xC) || defined(STM32F358xx)                         || \
1003      defined(STM32F303x8) || defined(STM32F334x8) || defined(STM32F328xx) || \
1004      defined(STM32F373xC) || defined(STM32F378xx)
1005 /* DAC channel 2 is available on top of DAC channel 1 */
1006
1007 /**
1008   * @brief  DMA conversion complete callback. 
1009   * @param  hdma: pointer to DMA handle.
1010   * @retval None
1011   */
1012 static void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)   
1013 {
1014   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1015   
1016   HAL_DACEx_ConvCpltCallbackCh2(hdac);
1017   
1018   hdac->State= HAL_DAC_STATE_READY;
1019 }
1020
1021 /**
1022   * @brief  DMA half transfer complete callback. 
1023   * @param  hdma: pointer to DMA handle.
1024   * @retval None
1025   */
1026 static void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)   
1027 {
1028     DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1029     /* Conversion complete callback */
1030     HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
1031 }
1032
1033 /**
1034   * @brief  DMA error callback 
1035   * @param  hdma: pointer to DMA handle.
1036   * @retval None
1037   */
1038 static void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)   
1039 {
1040   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
1041     
1042   /* Set DAC error code to DMA error */
1043   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1044     
1045   HAL_DACEx_ErrorCallbackCh2(hdac);
1046     
1047   hdac->State= HAL_DAC_STATE_READY;
1048 }
1049 #endif /* STM32F303xE || STM32F398xx                || */
1050        /* STM32F303xC || STM32F358xx                || */
1051        /* STM32F303x8 || STM32F334x8 || STM32F328xx || */
1052        /* STM32F373xC || STM32F378xx                   */
1053
1054 /**
1055   * @}
1056   */
1057
1058 /**
1059   * @}
1060   */
1061
1062 #endif /* HAL_DAC_MODULE_ENABLED */
1063 /**
1064   * @}
1065   */
1066
1067 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/