Subversion Repositories DashDisplay

Rev

Go to most recent revision | Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
56 mjames 1
/**
2
  ******************************************************************************
3
  * @file    stm32l1xx_hal_dac.c
4
  * @author  MCD Application Team
5
  * @brief   DAC HAL module driver.
6
  *         This file provides firmware functions to manage the following
7
  *         functionalities of the Digital to Analog Converter (DAC) peripheral:
8
  *           + Initialization and de-initialization functions
9
  *           + IO operation functions
10
  *           + Peripheral Control functions
11
  *           + Peripheral State and Errors functions
12
  *
13
  *
14
  @verbatim
15
  ==============================================================================
16
                      ##### DAC Peripheral features #####
17
  ==============================================================================
18
    [..]
19
      *** DAC Channels ***
20
      ====================
21
    [..]
22
    STM32L1 devices integrate two 12-bit Digital Analog Converters
23
 
24
    The 2 converters (i.e. channel1 & channel2)
25
    can be used independently or simultaneously (dual mode):
26
      (#) DAC channel1 with DAC_OUT1 (PA4) as output or connected to on-chip
27
          peripherals (ex. timers).
28
      (#) DAC channel2 with DAC_OUT2 (PA5) as output or connected to on-chip
29
          peripherals (ex. timers).
30
 
31
      *** DAC Triggers ***
32
      ====================
33
    [..]
34
    Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
35
    and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register.
36
    [..]
37
    Digital to Analog conversion can be triggered by:
38
      (#) External event: EXTI Line 9 (any GPIOx_PIN_9) using DAC_TRIGGER_EXT_IT9.
39
          The used pin (GPIOx_PIN_9) must be configured in input mode.
40
 
41
      (#) Timers TRGO: TIM2, TIM4, TIM6, TIM7, TIM9
42
          (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
43
 
44
      (#) Software using DAC_TRIGGER_SOFTWARE
45
 
46
      *** DAC Buffer mode feature ***
47
      ===============================
48
      [..]
49
      Each DAC channel integrates an output buffer that can be used to
50
      reduce the output impedance, and to drive external loads directly
51
      without having to add an external operational amplifier.
52
      To enable, the output buffer use
53
      sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
54
      [..]
55
      (@) Refer to the device datasheet for more details about output
56
          impedance value with and without output buffer.
57
 
58
      *** DAC connect feature ***
59
      ===============================
60
      [..]
61
      Each DAC channel can be connected internally.
62
      To connect, use
63
      sConfig.DAC_ConnectOnChipPeripheral = DAC_CHIPCONNECT_ENABLE;
64
 
65
      *** GPIO configurations guidelines ***
66
      =====================
67
      [..]
68
      When a DAC channel is used (ex channel1 on PA4) and the other is not
69
      (ex channel2 on PA5 is configured in Analog and disabled).
70
      Channel1 may disturb channel2 as coupling effect.
71
      Note that there is no coupling on channel2 as soon as channel2 is turned on.
72
      Coupling on adjacent channel could be avoided as follows:
73
      when unused PA5 is configured as INPUT PULL-UP or DOWN.
74
      PA5 is configured in ANALOG just before it is turned on.
75
 
76
       *** DAC wave generation feature ***
77
       ===================================
78
       [..]
79
       Both DAC channels can be used to generate
80
         (#) Noise wave
81
         (#) Triangle wave
82
 
83
       *** DAC data format ***
84
       =======================
85
       [..]
86
       The DAC data format can be:
87
         (#) 8-bit right alignment using DAC_ALIGN_8B_R
88
         (#) 12-bit left alignment using DAC_ALIGN_12B_L
89
         (#) 12-bit right alignment using DAC_ALIGN_12B_R
90
 
91
       *** DAC data value to voltage correspondence ***
92
       ================================================
93
       [..]
94
       The analog output voltage on each DAC channel pin is determined
95
       by the following equation:
96
       [..]
97
       DAC_OUTx = VREF+ * DOR / 4095
98
       (+) with  DOR is the Data Output Register
99
       [..]
100
          VEF+ is the input voltage reference (refer to the device datasheet)
101
       [..]
102
        e.g. To set DAC_OUT1 to 0.7V, use
103
       (+) Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
104
 
105
       *** DMA requests ***
106
       =====================
107
       [..]
108
       A DMA1 request can be generated when an external trigger (but not a software trigger)
109
       occurs if DMA1 requests are enabled using HAL_DAC_Start_DMA().
110
       DMA1 requests are mapped as following:
111
      (#) DAC channel1 mapped on DMA1 channel2
112
      (#) DAC channel2 mapped on DMA1 channel3
113
 
114
     [..]
115
    (@) For Dual mode and specific signal (Triangle and noise) generation please
116
        refer to Extended Features Driver description
117
 
118
                      ##### How to use this driver #####
119
  ==============================================================================
120
    [..]
121
      (+) DAC APB clock must be enabled to get write access to DAC
122
          registers using HAL_DAC_Init()
123
      (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
124
      (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
125
      (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA() functions.
126
 
127
 
128
     *** Polling mode IO operation ***
129
     =================================
130
     [..]
131
       (+) Start the DAC peripheral using HAL_DAC_Start()
132
       (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
133
       (+) Stop the DAC peripheral using HAL_DAC_Stop()
134
 
135
     *** DMA mode IO operation ***
136
     ==============================
137
     [..]
138
       (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length
139
           of data to be transferred at each end of conversion
140
           First issued trigger will start the conversion of the value previously set by HAL_DAC_SetValue().
141
       (+) At the middle of data transfer HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
142
           function is executed and user can add his own code by customization of function pointer
143
           HAL_DAC_ConvHalfCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
144
       (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
145
           function is executed and user can add his own code by customization of function pointer
146
           HAL_DAC_ConvCpltCallbackCh1() or HAL_DACEx_ConvHalfCpltCallbackCh2()
147
       (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can
148
            add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
149
       (+) In case of DMA underrun, DAC interruption triggers and execute internal function HAL_DAC_IRQHandler.
150
           HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2()
151
           function is executed and user can add his own code by customization of function pointer
152
           HAL_DAC_DMAUnderrunCallbackCh1() or HAL_DACEx_DMAUnderrunCallbackCh2() and
153
           add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1()
154
       (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
155
 
156
    *** Callback registration ***
157
    =============================================
158
    [..]
159
      The compilation define  USE_HAL_DAC_REGISTER_CALLBACKS when set to 1
160
      allows the user to configure dynamically the driver callbacks.
161
 
162
    Use Functions @ref HAL_DAC_RegisterCallback() to register a user callback,
163
      it allows to register following callbacks:
164
      (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
165
      (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
166
      (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
167
      (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
168
      (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
169
      (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
170
      (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
171
      (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
172
      (+) MspInitCallback         : DAC MspInit.
173
      (+) MspDeInitCallback       : DAC MspdeInit.
174
      This function takes as parameters the HAL peripheral handle, the Callback ID
175
      and a pointer to the user callback function.
176
 
177
    Use function @ref HAL_DAC_UnRegisterCallback() to reset a callback to the default
178
      weak (surcharged) function. It allows to reset following callbacks:
179
      (+) ConvCpltCallbackCh1     : callback when a half transfer is completed on Ch1.
180
      (+) ConvHalfCpltCallbackCh1 : callback when a transfer is completed on Ch1.
181
      (+) ErrorCallbackCh1        : callback when an error occurs on Ch1.
182
      (+) DMAUnderrunCallbackCh1  : callback when an underrun error occurs on Ch1.
183
      (+) ConvCpltCallbackCh2     : callback when a half transfer is completed on Ch2.
184
      (+) ConvHalfCpltCallbackCh2 : callback when a transfer is completed on Ch2.
185
      (+) ErrorCallbackCh2        : callback when an error occurs on Ch2.
186
      (+) DMAUnderrunCallbackCh2  : callback when an underrun error occurs on Ch2.
187
      (+) MspInitCallback         : DAC MspInit.
188
      (+) MspDeInitCallback       : DAC MspdeInit.
189
      (+) All Callbacks
190
      This function) takes as parameters the HAL peripheral handle and the Callback ID.
191
 
192
      By default, after the @ref HAL_DAC_Init and if the state is HAL_DAC_STATE_RESET
193
      all callbacks are reset to the corresponding legacy weak (surcharged) functions.
194
      Exception done for MspInit and MspDeInit callbacks that are respectively
195
      reset to the legacy weak (surcharged) functions in the @ref HAL_DAC_Init
196
      and @ref  HAL_DAC_DeInit only when these callbacks are null (not registered beforehand).
197
      If not, MspInit or MspDeInit are not null, the @ref HAL_DAC_Init and @ref HAL_DAC_DeInit
198
      keep and use the user MspInit/MspDeInit callbacks (registered beforehand)
199
 
200
      Callbacks can be registered/unregistered in READY state only.
201
      Exception done for MspInit/MspDeInit callbacks that can be registered/unregistered
202
      in READY or RESET state, thus registered (user) MspInit/DeInit callbacks can be used
203
      during the Init/DeInit.
204
      In that case first register the MspInit/MspDeInit user callbacks
205
      using @ref HAL_DAC_RegisterCallback before calling @ref HAL_DAC_DeInit
206
      or @ref HAL_DAC_Init function.
207
 
208
      When The compilation define USE_HAL_DAC_REGISTER_CALLBACKS is set to 0 or
209
      not defined, the callback registering feature is not available
210
      and weak (surcharged) callbacks are used.
211
 
212
 
213
 
214
     *** DAC HAL driver macros list ***
215
     =============================================
216
     [..]
217
       Below the list of most used macros in DAC HAL driver.
218
 
219
      (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
220
      (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
221
      (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
222
      (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
223
 
224
     [..]
225
      (@) You can refer to the DAC HAL driver header file for more useful macros
226
 
227
 @endverbatim
228
  ******************************************************************************
229
  * @attention
230
  *
231
  * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
232
  * All rights reserved.</center></h2>
233
  *
234
  * This software component is licensed by ST under BSD 3-Clause license,
235
  * the "License"; You may not use this file except in compliance with the
236
  * License. You may obtain a copy of the License at:
237
  *                        opensource.org/licenses/BSD-3-Clause
238
  *
239
  ******************************************************************************
240
  */
241
 
242
/* Includes ------------------------------------------------------------------*/
243
#include "stm32l1xx_hal.h"
244
 
245
/** @addtogroup STM32L1xx_HAL_Driver
246
  * @{
247
  */
248
 
249
#ifdef HAL_DAC_MODULE_ENABLED
250
#if defined(DAC1)
251
 
252
/** @defgroup DAC DAC
253
  * @brief DAC driver modules
254
  * @{
255
  */
256
 
257
/* Private typedef -----------------------------------------------------------*/
258
/* Private define ------------------------------------------------------------*/
259
/* Private constants ---------------------------------------------------------*/
260
/* Private macro -------------------------------------------------------------*/
261
/* Private variables ---------------------------------------------------------*/
262
/* Private function prototypes -----------------------------------------------*/
263
/* Exported functions -------------------------------------------------------*/
264
 
265
/** @defgroup DAC_Exported_Functions DAC Exported Functions
266
  * @{
267
  */
268
 
269
/** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions
270
  *  @brief    Initialization and Configuration functions
271
  *
272
@verbatim
273
  ==============================================================================
274
              ##### Initialization and de-initialization functions #####
275
  ==============================================================================
276
    [..]  This section provides functions allowing to:
277
      (+) Initialize and configure the DAC.
278
      (+) De-initialize the DAC.
279
 
280
@endverbatim
281
  * @{
282
  */
283
 
284
/**
285
  * @brief  Initialize the DAC peripheral according to the specified parameters
286
  *         in the DAC_InitStruct and initialize the associated handle.
287
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
288
  *         the configuration information for the specified DAC.
289
  * @retval HAL status
290
  */
291
HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef *hdac)
292
{
293
  /* Check DAC handle */
294
  if (hdac == NULL)
295
  {
296
    return HAL_ERROR;
297
  }
298
  /* Check the parameters */
299
  assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
300
 
301
  if (hdac->State == HAL_DAC_STATE_RESET)
302
  {
303
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
304
    /* Init the DAC Callback settings */
305
    hdac->ConvCpltCallbackCh1           = HAL_DAC_ConvCpltCallbackCh1;
306
    hdac->ConvHalfCpltCallbackCh1       = HAL_DAC_ConvHalfCpltCallbackCh1;
307
    hdac->ErrorCallbackCh1              = HAL_DAC_ErrorCallbackCh1;
308
    hdac->DMAUnderrunCallbackCh1        = HAL_DAC_DMAUnderrunCallbackCh1;
309
 
310
    hdac->ConvCpltCallbackCh2           = HAL_DACEx_ConvCpltCallbackCh2;
311
    hdac->ConvHalfCpltCallbackCh2       = HAL_DACEx_ConvHalfCpltCallbackCh2;
312
    hdac->ErrorCallbackCh2              = HAL_DACEx_ErrorCallbackCh2;
313
    hdac->DMAUnderrunCallbackCh2        = HAL_DACEx_DMAUnderrunCallbackCh2;
314
 
315
    if (hdac->MspInitCallback == NULL)
316
    {
317
      hdac->MspInitCallback             = HAL_DAC_MspInit;
318
    }
319
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
320
 
321
    /* Allocate lock resource and initialize it */
322
    hdac->Lock = HAL_UNLOCKED;
323
 
324
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
325
    /* Init the low level hardware */
326
    hdac->MspInitCallback(hdac);
327
#else
328
    /* Init the low level hardware */
329
    HAL_DAC_MspInit(hdac);
330
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
331
  }
332
 
333
  /* Initialize the DAC state*/
334
  hdac->State = HAL_DAC_STATE_BUSY;
335
 
336
  /* Set DAC error code to none */
337
  hdac->ErrorCode = HAL_DAC_ERROR_NONE;
338
 
339
  /* Initialize the DAC state*/
340
  hdac->State = HAL_DAC_STATE_READY;
341
 
342
  /* Return function status */
343
  return HAL_OK;
344
}
345
 
346
/**
347
  * @brief  Deinitialize the DAC peripheral registers to their default reset values.
348
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
349
  *         the configuration information for the specified DAC.
350
  * @retval HAL status
351
  */
352
HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef *hdac)
353
{
354
  /* Check DAC handle */
355
  if (hdac == NULL)
356
  {
357
    return HAL_ERROR;
358
  }
359
 
360
  /* Check the parameters */
361
  assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
362
 
363
  /* Change DAC state */
364
  hdac->State = HAL_DAC_STATE_BUSY;
365
 
366
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
367
  if (hdac->MspDeInitCallback == NULL)
368
  {
369
    hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
370
  }
371
  /* DeInit the low level hardware */
372
  hdac->MspDeInitCallback(hdac);
373
#else
374
  /* DeInit the low level hardware */
375
  HAL_DAC_MspDeInit(hdac);
376
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
377
 
378
  /* Set DAC error code to none */
379
  hdac->ErrorCode = HAL_DAC_ERROR_NONE;
380
 
381
  /* Change DAC state */
382
  hdac->State = HAL_DAC_STATE_RESET;
383
 
384
  /* Release Lock */
385
  __HAL_UNLOCK(hdac);
386
 
387
  /* Return function status */
388
  return HAL_OK;
389
}
390
 
391
/**
392
  * @brief  Initialize the DAC MSP.
393
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
394
  *         the configuration information for the specified DAC.
395
  * @retval None
396
  */
397
__weak void HAL_DAC_MspInit(DAC_HandleTypeDef *hdac)
398
{
399
  /* Prevent unused argument(s) compilation warning */
400
  UNUSED(hdac);
401
 
402
  /* NOTE : This function should not be modified, when the callback is needed,
403
            the HAL_DAC_MspInit could be implemented in the user file
404
   */
405
}
406
 
407
/**
408
  * @brief  DeInitialize the DAC MSP.
409
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
410
  *         the configuration information for the specified DAC.
411
  * @retval None
412
  */
413
__weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef *hdac)
414
{
415
  /* Prevent unused argument(s) compilation warning */
416
  UNUSED(hdac);
417
 
418
  /* NOTE : This function should not be modified, when the callback is needed,
419
            the HAL_DAC_MspDeInit could be implemented in the user file
420
   */
421
}
422
 
423
/**
424
  * @}
425
  */
426
 
427
/** @defgroup DAC_Exported_Functions_Group2 IO operation functions
428
  *  @brief    IO operation functions
429
  *
430
@verbatim
431
  ==============================================================================
432
             ##### IO operation functions #####
433
  ==============================================================================
434
    [..]  This section provides functions allowing to:
435
      (+) Start conversion.
436
      (+) Stop conversion.
437
      (+) Start conversion and enable DMA transfer.
438
      (+) Stop conversion and disable DMA transfer.
439
      (+) Get result of conversion.
440
 
441
@endverbatim
442
  * @{
443
  */
444
 
445
/**
446
  * @brief  Enables DAC and starts conversion of channel.
447
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
448
  *         the configuration information for the specified DAC.
449
  * @param  Channel The selected DAC channel.
450
  *          This parameter can be one of the following values:
451
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
452
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
453
  * @retval HAL status
454
  */
455
HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef *hdac, uint32_t Channel)
456
{
457
  /* Check the parameters */
458
  assert_param(IS_DAC_CHANNEL(Channel));
459
 
460
  /* Process locked */
461
  __HAL_LOCK(hdac);
462
 
463
  /* Change DAC state */
464
  hdac->State = HAL_DAC_STATE_BUSY;
465
 
466
  /* Enable the Peripheral */
467
  __HAL_DAC_ENABLE(hdac, Channel);
468
 
469
  if (Channel == DAC_CHANNEL_1)
470
  {
471
    /* Check if software trigger enabled */
472
    if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
473
    {
474
      /* Enable the selected DAC software conversion */
475
      SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG1);
476
    }
477
  }
478
  else
479
  {
480
    /* Check if software trigger enabled */
481
    if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << Channel))
482
    {
483
      /* Enable the selected DAC software conversion*/
484
      SET_BIT(hdac->Instance->SWTRIGR, DAC_SWTRIGR_SWTRIG2);
485
    }
486
  }
487
 
488
  /* Change DAC state */
489
  hdac->State = HAL_DAC_STATE_READY;
490
 
491
  /* Process unlocked */
492
  __HAL_UNLOCK(hdac);
493
 
494
  /* Return function status */
495
  return HAL_OK;
496
}
497
 
498
/**
499
  * @brief  Disables DAC and stop conversion of channel.
500
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
501
  *         the configuration information for the specified DAC.
502
  * @param  Channel The selected DAC channel.
503
  *          This parameter can be one of the following values:
504
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
505
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
506
  * @retval HAL status
507
  */
508
HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef *hdac, uint32_t Channel)
509
{
510
  /* Check the parameters */
511
  assert_param(IS_DAC_CHANNEL(Channel));
512
 
513
  /* Disable the Peripheral */
514
  __HAL_DAC_DISABLE(hdac, Channel);
515
 
516
  /* Change DAC state */
517
  hdac->State = HAL_DAC_STATE_READY;
518
 
519
  /* Return function status */
520
  return HAL_OK;
521
}
522
 
523
/**
524
  * @brief  Enables DAC and starts conversion of channel.
525
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
526
  *         the configuration information for the specified DAC.
527
  * @param  Channel The selected DAC channel.
528
  *          This parameter can be one of the following values:
529
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
530
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
531
  * @param  pData The destination peripheral Buffer address.
532
  * @param  Length The length of data to be transferred from memory to DAC peripheral
533
  * @param  Alignment Specifies the data alignment for DAC channel.
534
  *          This parameter can be one of the following values:
535
  *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
536
  *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
537
  *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
538
  * @retval HAL status
539
  */
540
HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length,
541
                                    uint32_t Alignment)
542
{
543
  HAL_StatusTypeDef status;
544
  uint32_t tmpreg = 0U;
545
 
546
  /* Check the parameters */
547
  assert_param(IS_DAC_CHANNEL(Channel));
548
  assert_param(IS_DAC_ALIGN(Alignment));
549
 
550
  /* Process locked */
551
  __HAL_LOCK(hdac);
552
 
553
  /* Change DAC state */
554
  hdac->State = HAL_DAC_STATE_BUSY;
555
 
556
  if (Channel == DAC_CHANNEL_1)
557
  {
558
    /* Set the DMA transfer complete callback for channel1 */
559
    hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
560
 
561
    /* Set the DMA half transfer complete callback for channel1 */
562
    hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
563
 
564
    /* Set the DMA error callback for channel1 */
565
    hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
566
 
567
    /* Enable the selected DAC channel1 DMA request */
568
    SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
569
 
570
    /* Case of use of channel 1 */
571
    switch (Alignment)
572
    {
573
      case DAC_ALIGN_12B_R:
574
        /* Get DHR12R1 address */
575
        tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
576
        break;
577
      case DAC_ALIGN_12B_L:
578
        /* Get DHR12L1 address */
579
        tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
580
        break;
581
      case DAC_ALIGN_8B_R:
582
        /* Get DHR8R1 address */
583
        tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
584
        break;
585
      default:
586
        break;
587
    }
588
  }
589
  else
590
  {
591
    /* Set the DMA transfer complete callback for channel2 */
592
    hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
593
 
594
    /* Set the DMA half transfer complete callback for channel2 */
595
    hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
596
 
597
    /* Set the DMA error callback for channel2 */
598
    hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
599
 
600
    /* Enable the selected DAC channel2 DMA request */
601
    SET_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
602
 
603
    /* Case of use of channel 2 */
604
    switch (Alignment)
605
    {
606
      case DAC_ALIGN_12B_R:
607
        /* Get DHR12R2 address */
608
        tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
609
        break;
610
      case DAC_ALIGN_12B_L:
611
        /* Get DHR12L2 address */
612
        tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
613
        break;
614
      case DAC_ALIGN_8B_R:
615
        /* Get DHR8R2 address */
616
        tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
617
        break;
618
      default:
619
        break;
620
    }
621
  }
622
 
623
  /* Enable the DMA channel */
624
  if (Channel == DAC_CHANNEL_1)
625
  {
626
    /* Enable the DAC DMA underrun interrupt */
627
    __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
628
 
629
    /* Enable the DMA channel */
630
    status = HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
631
  }
632
  else
633
  {
634
    /* Enable the DAC DMA underrun interrupt */
635
    __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
636
 
637
    /* Enable the DMA channel */
638
    status = HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
639
  }
640
 
641
  /* Process Unlocked */
642
  __HAL_UNLOCK(hdac);
643
 
644
  if (status == HAL_OK)
645
  {
646
    /* Enable the Peripheral */
647
    __HAL_DAC_ENABLE(hdac, Channel);
648
  }
649
  else
650
  {
651
    hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
652
  }
653
 
654
  /* Return function status */
655
  return status;
656
}
657
 
658
/**
659
  * @brief  Disables DAC and stop conversion of channel.
660
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
661
  *         the configuration information for the specified DAC.
662
  * @param  Channel The selected DAC channel.
663
  *          This parameter can be one of the following values:
664
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
665
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
666
  * @retval HAL status
667
  */
668
HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel)
669
{
670
  HAL_StatusTypeDef status;
671
 
672
  /* Check the parameters */
673
  assert_param(IS_DAC_CHANNEL(Channel));
674
 
675
  /* Disable the selected DAC channel DMA request */
676
  hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << (Channel & 0x10UL));
677
 
678
  /* Disable the Peripheral */
679
  __HAL_DAC_DISABLE(hdac, Channel);
680
 
681
  /* Disable the DMA channel */
682
 
683
  /* Channel1 is used */
684
  if (Channel == DAC_CHANNEL_1)
685
  {
686
    /* Disable the DMA channel */
687
    status = HAL_DMA_Abort(hdac->DMA_Handle1);
688
 
689
    /* Disable the DAC DMA underrun interrupt */
690
    __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR1);
691
  }
692
  else /* Channel2 is used for */
693
  {
694
    /* Disable the DMA channel */
695
    status = HAL_DMA_Abort(hdac->DMA_Handle2);
696
 
697
    /* Disable the DAC DMA underrun interrupt */
698
    __HAL_DAC_DISABLE_IT(hdac, DAC_IT_DMAUDR2);
699
  }
700
 
701
  /* Check if DMA Channel effectively disabled */
702
  if (status != HAL_OK)
703
  {
704
    /* Update DAC state machine to error */
705
    hdac->State = HAL_DAC_STATE_ERROR;
706
  }
707
  else
708
  {
709
    /* Change DAC state */
710
    hdac->State = HAL_DAC_STATE_READY;
711
  }
712
 
713
  /* Return function status */
714
  return status;
715
}
716
 
717
/**
718
  * @brief  Handles DAC interrupt request
719
  *         This function uses the interruption of DMA
720
  *         underrun.
721
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
722
  *         the configuration information for the specified DAC.
723
  * @retval None
724
  */
725
void HAL_DAC_IRQHandler(DAC_HandleTypeDef *hdac)
726
{
727
  if (__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR1))
728
  {
729
    /* Check underrun flag of DAC channel 1 */
730
    if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
731
    {
732
      /* Change DAC state to error state */
733
      hdac->State = HAL_DAC_STATE_ERROR;
734
 
735
      /* Set DAC error code to chanel1 DMA underrun error */
736
      SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH1);
737
 
738
      /* Clear the underrun flag */
739
      __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR1);
740
 
741
      /* Disable the selected DAC channel1 DMA request */
742
      CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN1);
743
 
744
      /* Error callback */
745
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
746
      hdac->DMAUnderrunCallbackCh1(hdac);
747
#else
748
      HAL_DAC_DMAUnderrunCallbackCh1(hdac);
749
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
750
    }
751
  }
752
 
753
  if (__HAL_DAC_GET_IT_SOURCE(hdac, DAC_IT_DMAUDR2))
754
  {
755
    /* Check underrun flag of DAC channel 2 */
756
    if (__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
757
    {
758
      /* Change DAC state to error state */
759
      hdac->State = HAL_DAC_STATE_ERROR;
760
 
761
      /* Set DAC error code to channel2 DMA underrun error */
762
      SET_BIT(hdac->ErrorCode, HAL_DAC_ERROR_DMAUNDERRUNCH2);
763
 
764
      /* Clear the underrun flag */
765
      __HAL_DAC_CLEAR_FLAG(hdac, DAC_FLAG_DMAUDR2);
766
 
767
      /* Disable the selected DAC channel2 DMA request */
768
      CLEAR_BIT(hdac->Instance->CR, DAC_CR_DMAEN2);
769
 
770
      /* Error callback */
771
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
772
      hdac->DMAUnderrunCallbackCh2(hdac);
773
#else
774
      HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
775
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
776
    }
777
  }
778
}
779
 
780
/**
781
  * @brief  Set the specified data holding register value for DAC channel.
782
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
783
  *         the configuration information for the specified DAC.
784
  * @param  Channel The selected DAC channel.
785
  *          This parameter can be one of the following values:
786
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
787
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
788
  * @param  Alignment Specifies the data alignment.
789
  *          This parameter can be one of the following values:
790
  *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
791
  *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
792
  *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
793
  * @param  Data Data to be loaded in the selected data holding register.
794
  * @retval HAL status
795
  */
796
HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
797
{
798
  __IO uint32_t tmp = 0;
799
 
800
  /* Check the parameters */
801
  assert_param(IS_DAC_CHANNEL(Channel));
802
  assert_param(IS_DAC_ALIGN(Alignment));
803
  assert_param(IS_DAC_DATA(Data));
804
 
805
  tmp = (uint32_t)hdac->Instance;
806
  if (Channel == DAC_CHANNEL_1)
807
  {
808
    tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
809
  }
810
  else
811
  {
812
    tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
813
  }
814
 
815
  /* Set the DAC channel selected data holding register */
816
  *(__IO uint32_t *) tmp = Data;
817
 
818
  /* Return function status */
819
  return HAL_OK;
820
}
821
 
822
/**
823
  * @brief  Conversion complete callback in non-blocking mode for Channel1
824
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
825
  *         the configuration information for the specified DAC.
826
  * @retval None
827
  */
828
__weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef *hdac)
829
{
830
  /* Prevent unused argument(s) compilation warning */
831
  UNUSED(hdac);
832
 
833
  /* NOTE : This function should not be modified, when the callback is needed,
834
            the HAL_DAC_ConvCpltCallbackCh1 could be implemented in the user file
835
   */
836
}
837
 
838
/**
839
  * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel1
840
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
841
  *         the configuration information for the specified DAC.
842
  * @retval None
843
  */
844
__weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef *hdac)
845
{
846
  /* Prevent unused argument(s) compilation warning */
847
  UNUSED(hdac);
848
 
849
  /* NOTE : This function should not be modified, when the callback is needed,
850
            the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
851
   */
852
}
853
 
854
/**
855
  * @brief  Error DAC callback for Channel1.
856
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
857
  *         the configuration information for the specified DAC.
858
  * @retval None
859
  */
860
__weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
861
{
862
  /* Prevent unused argument(s) compilation warning */
863
  UNUSED(hdac);
864
 
865
  /* NOTE : This function should not be modified, when the callback is needed,
866
            the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
867
   */
868
}
869
 
870
/**
871
  * @brief  DMA underrun DAC callback for channel1.
872
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
873
  *         the configuration information for the specified DAC.
874
  * @retval None
875
  */
876
__weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
877
{
878
  /* Prevent unused argument(s) compilation warning */
879
  UNUSED(hdac);
880
 
881
  /* NOTE : This function should not be modified, when the callback is needed,
882
            the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
883
   */
884
}
885
 
886
/**
887
  * @}
888
  */
889
 
890
/** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
891
  *  @brief    Peripheral Control functions
892
  *
893
@verbatim
894
  ==============================================================================
895
             ##### Peripheral Control functions #####
896
  ==============================================================================
897
    [..]  This section provides functions allowing to:
898
      (+) Configure channels.
899
      (+) Set the specified data holding register value for DAC channel.
900
 
901
@endverbatim
902
  * @{
903
  */
904
 
905
/**
906
  * @brief  Returns the last data output value of the selected DAC channel.
907
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
908
  *         the configuration information for the specified DAC.
909
  * @param  Channel The selected DAC channel.
910
  *          This parameter can be one of the following values:
911
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
912
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
913
  * @retval The selected DAC channel data output value.
914
  */
915
uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef *hdac, uint32_t Channel)
916
{
917
  /* Check the parameters */
918
  assert_param(IS_DAC_CHANNEL(Channel));
919
 
920
  /* Returns the DAC channel data output register value */
921
  if (Channel == DAC_CHANNEL_1)
922
  {
923
    return hdac->Instance->DOR1;
924
  }
925
  else
926
  {
927
    return hdac->Instance->DOR2;
928
  }
929
}
930
 
931
/**
932
  * @brief  Configures the selected DAC channel.
933
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
934
  *         the configuration information for the specified DAC.
935
  * @param  sConfig DAC configuration structure.
936
  * @param  Channel The selected DAC channel.
937
  *          This parameter can be one of the following values:
938
  *            @arg DAC_CHANNEL_1: DAC Channel1 selected
939
  *            @arg DAC_CHANNEL_2: DAC Channel2 selected
940
  * @retval HAL status
941
  */
942
HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel)
943
{
944
  uint32_t tmpreg1;
945
  uint32_t tmpreg2;
946
 
947
  /* Check the DAC parameters */
948
  assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
949
  assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
950
  assert_param(IS_DAC_CHANNEL(Channel));
951
 
952
  /* Process locked */
953
  __HAL_LOCK(hdac);
954
 
955
  /* Change DAC state */
956
  hdac->State = HAL_DAC_STATE_BUSY;
957
 
958
  /* Get the DAC CR value */
959
  tmpreg1 = hdac->Instance->CR;
960
  /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
961
  tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
962
  /* Configure for the selected DAC channel: buffer output, trigger */
963
  /* Set TSELx and TENx bits according to DAC_Trigger value */
964
  /* Set BOFFx bit according to DAC_OutputBuffer value */  
965
  tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
966
  /* Calculate CR register value depending on DAC_Channel */
967
  tmpreg1 |= tmpreg2 << Channel;
968
  /* Write to DAC CR */
969
  hdac->Instance->CR = tmpreg1;
970
  /* Disable wave generation */
971
  CLEAR_BIT(hdac->Instance->CR, (DAC_CR_WAVE1 << Channel));
972
  /* Disable wave generation */
973
  hdac->Instance->CR &= ~(DAC_CR_WAVE1 << (Channel & 0x10UL));
974
 
975
  /* Change DAC state */
976
  hdac->State = HAL_DAC_STATE_READY;
977
 
978
  /* Process unlocked */
979
  __HAL_UNLOCK(hdac);
980
 
981
  /* Return function status */
982
  return HAL_OK;
983
}
984
 
985
/**
986
  * @}
987
  */
988
 
989
/** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
990
  *  @brief   Peripheral State and Errors functions
991
  *
992
@verbatim
993
  ==============================================================================
994
            ##### Peripheral State and Errors functions #####
995
  ==============================================================================
996
    [..]
997
    This subsection provides functions allowing to
998
      (+) Check the DAC state.
999
      (+) Check the DAC Errors.
1000
 
1001
@endverbatim
1002
  * @{
1003
  */
1004
 
1005
/**
1006
  * @brief  return the DAC handle state
1007
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1008
  *         the configuration information for the specified DAC.
1009
  * @retval HAL state
1010
  */
1011
HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef *hdac)
1012
{
1013
  /* Return DAC handle state */
1014
  return hdac->State;
1015
}
1016
 
1017
 
1018
/**
1019
  * @brief  Return the DAC error code
1020
  * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
1021
  *         the configuration information for the specified DAC.
1022
  * @retval DAC Error Code
1023
  */
1024
uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
1025
{
1026
  return hdac->ErrorCode;
1027
}
1028
 
1029
/**
1030
  * @}
1031
  */
1032
 
1033
/**
1034
  * @}
1035
  */
1036
 
1037
/** @addtogroup DAC_Exported_Functions
1038
  * @{
1039
  */
1040
 
1041
/** @addtogroup DAC_Exported_Functions_Group1
1042
  * @{
1043
  */
1044
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1045
/**
1046
  * @brief  Register a User DAC Callback
1047
  *         To be used instead of the weak (surcharged) predefined callback
1048
  * @param  hdac DAC handle
1049
  * @param  CallbackID ID of the callback to be registered
1050
  *         This parameter can be one of the following values:
1051
  *          @arg @ref HAL_DAC_ERROR_INVALID_CALLBACK   DAC Error Callback ID
1052
  *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID       DAC CH1 Complete Callback ID
1053
  *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID  DAC CH1 Half Complete Callback ID
1054
  *          @arg @ref HAL_DAC_CH1_ERROR_ID             DAC CH1 Error Callback ID
1055
  *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID       DAC CH1 UnderRun Callback ID
1056
  *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID       DAC CH2 Complete Callback ID
1057
  *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID  DAC CH2 Half Complete Callback ID
1058
  *          @arg @ref HAL_DAC_CH2_ERROR_ID             DAC CH2 Error Callback ID
1059
  *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID       DAC CH2 UnderRun Callback ID
1060
  *          @arg @ref HAL_DAC_MSPINIT_CB_ID            DAC MSP Init Callback ID
1061
  *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID          DAC MSP DeInit Callback ID
1062
  *
1063
  * @param  pCallback pointer to the Callback function
1064
  * @retval status
1065
  */
1066
HAL_StatusTypeDef HAL_DAC_RegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID,
1067
                                           pDAC_CallbackTypeDef pCallback)
1068
{
1069
  HAL_StatusTypeDef status = HAL_OK;
1070
 
1071
  if (pCallback == NULL)
1072
  {
1073
    /* Update the error code */
1074
    hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1075
    return HAL_ERROR;
1076
  }
1077
 
1078
  /* Process locked */
1079
  __HAL_LOCK(hdac);
1080
 
1081
  if (hdac->State == HAL_DAC_STATE_READY)
1082
  {
1083
    switch (CallbackID)
1084
    {
1085
      case HAL_DAC_CH1_COMPLETE_CB_ID :
1086
        hdac->ConvCpltCallbackCh1 = pCallback;
1087
        break;
1088
      case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1089
        hdac->ConvHalfCpltCallbackCh1 = pCallback;
1090
        break;
1091
      case HAL_DAC_CH1_ERROR_ID :
1092
        hdac->ErrorCallbackCh1 = pCallback;
1093
        break;
1094
      case HAL_DAC_CH1_UNDERRUN_CB_ID :
1095
        hdac->DMAUnderrunCallbackCh1 = pCallback;
1096
        break;
1097
      case HAL_DAC_CH2_COMPLETE_CB_ID :
1098
        hdac->ConvCpltCallbackCh2 = pCallback;
1099
        break;
1100
      case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1101
        hdac->ConvHalfCpltCallbackCh2 = pCallback;
1102
        break;
1103
      case HAL_DAC_CH2_ERROR_ID :
1104
        hdac->ErrorCallbackCh2 = pCallback;
1105
        break;
1106
      case HAL_DAC_CH2_UNDERRUN_CB_ID :
1107
        hdac->DMAUnderrunCallbackCh2 = pCallback;
1108
        break;
1109
      case HAL_DAC_MSPINIT_CB_ID :
1110
        hdac->MspInitCallback = pCallback;
1111
        break;
1112
      case HAL_DAC_MSPDEINIT_CB_ID :
1113
        hdac->MspDeInitCallback = pCallback;
1114
        break;
1115
      default :
1116
        /* Update the error code */
1117
        hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1118
        /* update return status */
1119
        status =  HAL_ERROR;
1120
        break;
1121
    }
1122
  }
1123
  else if (hdac->State == HAL_DAC_STATE_RESET)
1124
  {
1125
    switch (CallbackID)
1126
    {
1127
      case HAL_DAC_MSPINIT_CB_ID :
1128
        hdac->MspInitCallback = pCallback;
1129
        break;
1130
      case HAL_DAC_MSPDEINIT_CB_ID :
1131
        hdac->MspDeInitCallback = pCallback;
1132
        break;
1133
      default :
1134
        /* Update the error code */
1135
        hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1136
        /* update return status */
1137
        status =  HAL_ERROR;
1138
        break;
1139
    }
1140
  }
1141
  else
1142
  {
1143
    /* Update the error code */
1144
    hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1145
    /* update return status */
1146
    status =  HAL_ERROR;
1147
  }
1148
 
1149
  /* Release Lock */
1150
  __HAL_UNLOCK(hdac);
1151
  return status;
1152
}
1153
 
1154
/**
1155
  * @brief  Unregister a User DAC Callback
1156
  *         DAC Callback is redirected to the weak (surcharged) predefined callback
1157
  * @param  hdac DAC handle
1158
  * @param  CallbackID ID of the callback to be unregistered
1159
  *         This parameter can be one of the following values:
1160
  *          @arg @ref HAL_DAC_CH1_COMPLETE_CB_ID          DAC CH1 tranfer Complete Callback ID
1161
  *          @arg @ref HAL_DAC_CH1_HALF_COMPLETE_CB_ID     DAC CH1 Half Complete Callback ID
1162
  *          @arg @ref HAL_DAC_CH1_ERROR_ID                DAC CH1 Error Callback ID
1163
  *          @arg @ref HAL_DAC_CH1_UNDERRUN_CB_ID          DAC CH1 UnderRun Callback ID
1164
  *          @arg @ref HAL_DAC_CH2_COMPLETE_CB_ID          DAC CH2 Complete Callback ID
1165
  *          @arg @ref HAL_DAC_CH2_HALF_COMPLETE_CB_ID     DAC CH2 Half Complete Callback ID
1166
  *          @arg @ref HAL_DAC_CH2_ERROR_ID                DAC CH2 Error Callback ID
1167
  *          @arg @ref HAL_DAC_CH2_UNDERRUN_CB_ID          DAC CH2 UnderRun Callback ID
1168
  *          @arg @ref HAL_DAC_MSPINIT_CB_ID               DAC MSP Init Callback ID
1169
  *          @arg @ref HAL_DAC_MSPDEINIT_CB_ID             DAC MSP DeInit Callback ID
1170
  *          @arg @ref HAL_DAC_ALL_CB_ID                   DAC All callbacks
1171
  * @retval status
1172
  */
1173
HAL_StatusTypeDef HAL_DAC_UnRegisterCallback(DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackID)
1174
{
1175
  HAL_StatusTypeDef status = HAL_OK;
1176
 
1177
  /* Process locked */
1178
  __HAL_LOCK(hdac);
1179
 
1180
  if (hdac->State == HAL_DAC_STATE_READY)
1181
  {
1182
    switch (CallbackID)
1183
    {
1184
      case HAL_DAC_CH1_COMPLETE_CB_ID :
1185
        hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1186
        break;
1187
      case HAL_DAC_CH1_HALF_COMPLETE_CB_ID :
1188
        hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1189
        break;
1190
      case HAL_DAC_CH1_ERROR_ID :
1191
        hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1192
        break;
1193
      case HAL_DAC_CH1_UNDERRUN_CB_ID :
1194
        hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1195
        break;
1196
      case HAL_DAC_CH2_COMPLETE_CB_ID :
1197
        hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1198
        break;
1199
      case HAL_DAC_CH2_HALF_COMPLETE_CB_ID :
1200
        hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1201
        break;
1202
      case HAL_DAC_CH2_ERROR_ID :
1203
        hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1204
        break;
1205
      case HAL_DAC_CH2_UNDERRUN_CB_ID :
1206
        hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1207
        break;
1208
      case HAL_DAC_MSPINIT_CB_ID :
1209
        hdac->MspInitCallback = HAL_DAC_MspInit;
1210
        break;
1211
      case HAL_DAC_MSPDEINIT_CB_ID :
1212
        hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1213
        break;
1214
      case HAL_DAC_ALL_CB_ID :
1215
        hdac->ConvCpltCallbackCh1 = HAL_DAC_ConvCpltCallbackCh1;
1216
        hdac->ConvHalfCpltCallbackCh1 = HAL_DAC_ConvHalfCpltCallbackCh1;
1217
        hdac->ErrorCallbackCh1 = HAL_DAC_ErrorCallbackCh1;
1218
        hdac->DMAUnderrunCallbackCh1 = HAL_DAC_DMAUnderrunCallbackCh1;
1219
        hdac->ConvCpltCallbackCh2 = HAL_DACEx_ConvCpltCallbackCh2;
1220
        hdac->ConvHalfCpltCallbackCh2 = HAL_DACEx_ConvHalfCpltCallbackCh2;
1221
        hdac->ErrorCallbackCh2 = HAL_DACEx_ErrorCallbackCh2;
1222
        hdac->DMAUnderrunCallbackCh2 = HAL_DACEx_DMAUnderrunCallbackCh2;
1223
        hdac->MspInitCallback = HAL_DAC_MspInit;
1224
        hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1225
        break;
1226
      default :
1227
        /* Update the error code */
1228
        hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1229
        /* update return status */
1230
        status =  HAL_ERROR;
1231
        break;
1232
    }
1233
  }
1234
  else if (hdac->State == HAL_DAC_STATE_RESET)
1235
  {
1236
    switch (CallbackID)
1237
    {
1238
      case HAL_DAC_MSPINIT_CB_ID :
1239
        hdac->MspInitCallback = HAL_DAC_MspInit;
1240
        break;
1241
      case HAL_DAC_MSPDEINIT_CB_ID :
1242
        hdac->MspDeInitCallback = HAL_DAC_MspDeInit;
1243
        break;
1244
      default :
1245
        /* Update the error code */
1246
        hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1247
        /* update return status */
1248
        status =  HAL_ERROR;
1249
        break;
1250
    }
1251
  }
1252
  else
1253
  {
1254
    /* Update the error code */
1255
    hdac->ErrorCode |= HAL_DAC_ERROR_INVALID_CALLBACK;
1256
    /* update return status */
1257
    status =  HAL_ERROR;
1258
  }
1259
 
1260
  /* Release Lock */
1261
  __HAL_UNLOCK(hdac);
1262
  return status;
1263
}
1264
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1265
 
1266
/**
1267
  * @}
1268
  */
1269
 
1270
/**
1271
  * @}
1272
  */
1273
 
1274
/** @addtogroup DAC_Private_Functions
1275
  * @{
1276
  */
1277
 
1278
/**
1279
  * @brief  DMA conversion complete callback.
1280
  * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1281
  *                the configuration information for the specified DMA module.
1282
  * @retval None
1283
  */
1284
void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)
1285
{
1286
  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1287
 
1288
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1289
  hdac->ConvCpltCallbackCh1(hdac);
1290
#else
1291
  HAL_DAC_ConvCpltCallbackCh1(hdac);
1292
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1293
 
1294
  hdac->State = HAL_DAC_STATE_READY;
1295
}
1296
 
1297
/**
1298
  * @brief  DMA half transfer complete callback.
1299
  * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1300
  *                the configuration information for the specified DMA module.
1301
  * @retval None
1302
  */
1303
void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)
1304
{
1305
  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1306
  /* Conversion complete callback */
1307
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1308
  hdac->ConvHalfCpltCallbackCh1(hdac);
1309
#else
1310
  HAL_DAC_ConvHalfCpltCallbackCh1(hdac);
1311
#endif  /* USE_HAL_DAC_REGISTER_CALLBACKS */
1312
}
1313
 
1314
/**
1315
  * @brief  DMA error callback
1316
  * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
1317
  *                the configuration information for the specified DMA module.
1318
  * @retval None
1319
  */
1320
void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)
1321
{
1322
  DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
1323
 
1324
  /* Set DAC error code to DMA error */
1325
  hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
1326
 
1327
#if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
1328
  hdac->ErrorCallbackCh1(hdac);
1329
#else
1330
  HAL_DAC_ErrorCallbackCh1(hdac);
1331
#endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
1332
 
1333
  hdac->State = HAL_DAC_STATE_READY;
1334
}
1335
 
1336
/**
1337
  * @}
1338
  */
1339
 
1340
/**
1341
  * @}
1342
  */
1343
 
1344
#endif /* DAC1 */
1345
 
1346
#endif /* HAL_DAC_MODULE_ENABLED */
1347
 
1348
/**
1349
  * @}
1350
  */
1351
 
1352
/************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/