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