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