STM32F439xx HAL User Manual
stm32f4xx_hal_dac.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f4xx_hal_dac.c
00004   * @author  MCD Application Team
00005   * @brief   DAC HAL module driver.
00006   *         This file provides firmware functions to manage the following 
00007   *         functionalities of the Digital to Analog Converter (DAC) peripheral:
00008   *           + Initialization and de-initialization functions
00009   *           + IO operation functions
00010   *           + Peripheral Control functions
00011   *           + Peripheral State and Errors functions      
00012   *     
00013   *
00014   @verbatim      
00015   ==============================================================================
00016                       ##### DAC Peripheral features #####
00017   ==============================================================================
00018     [..]        
00019       *** DAC Channels ***
00020       ====================  
00021     [..]
00022     The device integrates two 12-bit Digital Analog Converters that can 
00023     be used independently or simultaneously (dual mode):
00024       (#) DAC channel1 with DAC_OUT1 (PA4) as output
00025       (#) DAC channel2 with DAC_OUT2 (PA5) as output
00026       
00027       *** DAC Triggers ***
00028       ====================
00029     [..]
00030     Digital to Analog conversion can be non-triggered using DAC_TRIGGER_NONE
00031     and DAC_OUT1/DAC_OUT2 is available once writing to DHRx register. 
00032     [..] 
00033     Digital to Analog conversion can be triggered by:
00034       (#) External event: EXTI Line 9 (any GPIOx_Pin9) using DAC_TRIGGER_EXT_IT9.
00035           The used pin (GPIOx_Pin9) must be configured in input mode.
00036   
00037       (#) Timers TRGO: TIM2, TIM4, TIM5, TIM6, TIM7 and TIM8 
00038           (DAC_TRIGGER_T2_TRGO, DAC_TRIGGER_T4_TRGO...)
00039   
00040       (#) Software using DAC_TRIGGER_SOFTWARE
00041   
00042       *** DAC Buffer mode feature ***
00043       =============================== 
00044       [..] 
00045       Each DAC channel integrates an output buffer that can be used to 
00046       reduce the output impedance, and to drive external loads directly
00047       without having to add an external operational amplifier.
00048       To enable, the output buffer use  
00049       sConfig.DAC_OutputBuffer = DAC_OUTPUTBUFFER_ENABLE;
00050       [..]           
00051       (@) Refer to the device datasheet for more details about output 
00052           impedance value with and without output buffer.
00053             
00054        *** DAC wave generation feature ***
00055        =================================== 
00056        [..]     
00057        Both DAC channels can be used to generate
00058          (#) Noise wave 
00059          (#) Triangle wave
00060             
00061        *** DAC data format ***
00062        =======================
00063        [..]   
00064        The DAC data format can be:
00065          (#) 8-bit right alignment using DAC_ALIGN_8B_R
00066          (#) 12-bit left alignment using DAC_ALIGN_12B_L
00067          (#) 12-bit right alignment using DAC_ALIGN_12B_R
00068   
00069        *** DAC data value to voltage correspondence ***  
00070        ================================================ 
00071        [..] 
00072        The analog output voltage on each DAC channel pin is determined
00073        by the following equation: 
00074        DAC_OUTx = VREF+ * DOR / 4095
00075        with  DOR is the Data Output Register
00076           VEF+ is the input voltage reference (refer to the device datasheet)
00077         e.g. To set DAC_OUT1 to 0.7V, use
00078           Assuming that VREF+ = 3.3V, DAC_OUT1 = (3.3 * 868) / 4095 = 0.7V
00079   
00080        *** DMA requests  ***
00081        =====================
00082        [..]    
00083        A DMA1 request can be generated when an external trigger (but not
00084        a software trigger) occurs if DMA1 requests are enabled using
00085        HAL_DAC_Start_DMA()
00086        [..]
00087        DMA1 requests are mapped as following:
00088          (#) DAC channel1 : mapped on DMA1 Stream5 channel7 which must be 
00089              already configured
00090          (#) DAC channel2 : mapped on DMA1 Stream6 channel7 which must be 
00091              already configured
00092        
00093     -@- For Dual mode and specific signal (Triangle and noise) generation please 
00094         refer to Extension Features Driver description        
00095   
00096       
00097                       ##### How to use this driver #####
00098   ==============================================================================
00099     [..]          
00100       (+) DAC APB clock must be enabled to get write access to DAC
00101           registers using HAL_DAC_Init()
00102       (+) Configure DAC_OUTx (DAC_OUT1: PA4, DAC_OUT2: PA5) in analog mode.
00103       (+) Configure the DAC channel using HAL_DAC_ConfigChannel() function.
00104       (+) Enable the DAC channel using HAL_DAC_Start() or HAL_DAC_Start_DMA functions
00105 
00106      *** Polling mode IO operation ***
00107      =================================
00108      [..]    
00109        (+) Start the DAC peripheral using HAL_DAC_Start() 
00110        (+) To read the DAC last data output value, use the HAL_DAC_GetValue() function.
00111        (+) Stop the DAC peripheral using HAL_DAC_Stop()
00112        
00113      *** DMA mode IO operation ***    
00114      ==============================
00115      [..]    
00116        (+) Start the DAC peripheral using HAL_DAC_Start_DMA(), at this stage the user specify the length 
00117            of data to be transferred at each end of conversion 
00118        (+) At The end of data transfer HAL_DAC_ConvCpltCallbackCh1()or HAL_DAC_ConvCpltCallbackCh2()  
00119            function is executed and user can add his own code by customization of function pointer 
00120            HAL_DAC_ConvCpltCallbackCh1 or HAL_DAC_ConvCpltCallbackCh2
00121        (+) In case of transfer Error, HAL_DAC_ErrorCallbackCh1() function is executed and user can 
00122             add his own code by customization of function pointer HAL_DAC_ErrorCallbackCh1
00123        (+) Stop the DAC peripheral using HAL_DAC_Stop_DMA()
00124                     
00125      *** DAC HAL driver macros list ***
00126      ============================================= 
00127      [..]
00128        Below the list of most used macros in DAC HAL driver.
00129        
00130       (+) __HAL_DAC_ENABLE : Enable the DAC peripheral
00131       (+) __HAL_DAC_DISABLE : Disable the DAC peripheral
00132       (+) __HAL_DAC_CLEAR_FLAG: Clear the DAC's pending flags
00133       (+) __HAL_DAC_GET_FLAG: Get the selected DAC's flag status
00134       
00135      [..]
00136       (@) You can refer to the DAC HAL driver header file for more useful macros  
00137    
00138  @endverbatim    
00139   ******************************************************************************
00140   * @attention
00141   *
00142   * <h2><center>&copy; COPYRIGHT(c) 2017 STMicroelectronics</center></h2>
00143   *
00144   * Redistribution and use in source and binary forms, with or without modification,
00145   * are permitted provided that the following conditions are met:
00146   *   1. Redistributions of source code must retain the above copyright notice,
00147   *      this list of conditions and the following disclaimer.
00148   *   2. Redistributions in binary form must reproduce the above copyright notice,
00149   *      this list of conditions and the following disclaimer in the documentation
00150   *      and/or other materials provided with the distribution.
00151   *   3. Neither the name of STMicroelectronics nor the names of its contributors
00152   *      may be used to endorse or promote products derived from this software
00153   *      without specific prior written permission.
00154   *
00155   * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00156   * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00157   * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
00158   * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
00159   * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
00160   * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
00161   * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
00162   * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
00163   * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
00164   * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00165   *
00166   ******************************************************************************
00167   */ 
00168 
00169 
00170 /* Includes ------------------------------------------------------------------*/
00171 #include "stm32f4xx_hal.h"
00172 
00173 /** @addtogroup STM32F4xx_HAL_Driver
00174   * @{
00175   */
00176 
00177 /** @defgroup DAC DAC
00178   * @brief DAC driver modules
00179   * @{
00180   */ 
00181 
00182 #ifdef HAL_DAC_MODULE_ENABLED
00183 
00184 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\
00185     defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\
00186     defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\
00187     defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx)
00188 /* Private typedef -----------------------------------------------------------*/
00189 /* Private define ------------------------------------------------------------*/
00190 /* Private macro -------------------------------------------------------------*/
00191 /* Private variables ---------------------------------------------------------*/
00192 /** @addtogroup DAC_Private_Functions
00193   * @{
00194   */
00195 /* Private function prototypes -----------------------------------------------*/
00196 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma);
00197 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma);
00198 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma); 
00199 /**
00200   * @}
00201   */
00202 
00203 /* Exported functions --------------------------------------------------------*/
00204 /** @defgroup DAC_Exported_Functions DAC Exported Functions
00205   * @{
00206   */
00207 
00208 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions 
00209  *  @brief    Initialization and Configuration functions 
00210  *
00211 @verbatim    
00212   ==============================================================================
00213               ##### Initialization and de-initialization functions #####
00214   ==============================================================================
00215     [..]  This section provides functions allowing to:
00216       (+) Initialize and configure the DAC. 
00217       (+) De-initialize the DAC. 
00218          
00219 @endverbatim
00220   * @{
00221   */
00222 
00223 /**
00224   * @brief  Initializes the DAC peripheral according to the specified parameters
00225   *         in the DAC_InitStruct.
00226   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00227   *         the configuration information for the specified DAC.
00228   * @retval HAL status
00229   */
00230 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac)
00231 { 
00232   /* Check DAC handle */
00233   if(hdac == NULL)
00234   {
00235      return HAL_ERROR;
00236   }
00237   /* Check the parameters */
00238   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
00239   
00240   if(hdac->State == HAL_DAC_STATE_RESET)
00241   {  
00242     /* Allocate lock resource and initialize it */
00243     hdac->Lock = HAL_UNLOCKED;
00244     /* Init the low level hardware */
00245     HAL_DAC_MspInit(hdac);
00246   }
00247   
00248   /* Initialize the DAC state*/
00249   hdac->State = HAL_DAC_STATE_BUSY;
00250   
00251   /* Set DAC error code to none */
00252   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
00253   
00254   /* Initialize the DAC state*/
00255   hdac->State = HAL_DAC_STATE_READY;
00256   
00257   /* Return function status */
00258   return HAL_OK;
00259 }
00260 
00261 /**
00262   * @brief  Deinitializes the DAC peripheral registers to their default reset values.
00263   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00264   *         the configuration information for the specified DAC.
00265   * @retval HAL status
00266   */
00267 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac)
00268 {
00269   /* Check DAC handle */
00270   if(hdac == NULL)
00271   {
00272      return HAL_ERROR;
00273   }
00274 
00275   /* Check the parameters */
00276   assert_param(IS_DAC_ALL_INSTANCE(hdac->Instance));
00277 
00278   /* Change DAC state */
00279   hdac->State = HAL_DAC_STATE_BUSY;
00280 
00281   /* DeInit the low level hardware */
00282   HAL_DAC_MspDeInit(hdac);
00283 
00284   /* Set DAC error code to none */
00285   hdac->ErrorCode = HAL_DAC_ERROR_NONE;
00286 
00287   /* Change DAC state */
00288   hdac->State = HAL_DAC_STATE_RESET;
00289 
00290   /* Release Lock */
00291   __HAL_UNLOCK(hdac);
00292 
00293   /* Return function status */
00294   return HAL_OK;
00295 }
00296 
00297 /**
00298   * @brief  Initializes the DAC MSP.
00299   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00300   *         the configuration information for the specified DAC.
00301   * @retval None
00302   */
00303 __weak void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac)
00304 {
00305   /* Prevent unused argument(s) compilation warning */
00306   UNUSED(hdac);
00307   /* NOTE : This function Should not be modified, when the callback is needed,
00308             the HAL_DAC_MspInit could be implemented in the user file
00309    */ 
00310 }
00311 
00312 /**
00313   * @brief  DeInitializes the DAC MSP.
00314   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00315   *         the configuration information for the specified DAC.  
00316   * @retval None
00317   */
00318 __weak void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac)
00319 {
00320   /* Prevent unused argument(s) compilation warning */
00321   UNUSED(hdac);
00322   /* NOTE : This function Should not be modified, when the callback is needed,
00323             the HAL_DAC_MspDeInit could be implemented in the user file
00324    */ 
00325 }
00326 
00327 /**
00328   * @}
00329   */
00330 
00331 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions
00332  *  @brief    IO operation functions 
00333  *
00334 @verbatim   
00335   ==============================================================================
00336              ##### IO operation functions #####
00337   ==============================================================================  
00338     [..]  This section provides functions allowing to:
00339       (+) Start conversion.
00340       (+) Stop conversion.
00341       (+) Start conversion and enable DMA transfer.
00342       (+) Stop conversion and disable DMA transfer.
00343       (+) Get result of conversion.
00344                      
00345 @endverbatim
00346   * @{
00347   */
00348 
00349 /**
00350   * @brief  Enables DAC and starts conversion of channel.
00351   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00352   *         the configuration information for the specified DAC.
00353   * @param  Channel The selected DAC channel. 
00354   *          This parameter can be one of the following values:
00355   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00356   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00357   * @retval HAL status
00358   */
00359 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel)
00360 {
00361   uint32_t tmp1 = 0U, tmp2 = 0U;
00362   
00363   /* Check the parameters */
00364   assert_param(IS_DAC_CHANNEL(Channel));
00365   
00366   /* Process locked */
00367   __HAL_LOCK(hdac);
00368   
00369   /* Change DAC state */
00370   hdac->State = HAL_DAC_STATE_BUSY;
00371   
00372   /* Enable the Peripheral */
00373   __HAL_DAC_ENABLE(hdac, Channel);
00374   
00375   if(Channel == DAC_CHANNEL_1)
00376   {
00377     tmp1 = hdac->Instance->CR & DAC_CR_TEN1;
00378     tmp2 = hdac->Instance->CR & DAC_CR_TSEL1;
00379     /* Check if software trigger enabled */
00380     if((tmp1 ==  DAC_CR_TEN1) && (tmp2 ==  DAC_CR_TSEL1))
00381     {
00382       /* Enable the selected DAC software conversion */
00383       hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG1;
00384     }
00385   }
00386   else
00387   {
00388     tmp1 = hdac->Instance->CR & DAC_CR_TEN2;
00389     tmp2 = hdac->Instance->CR & DAC_CR_TSEL2;    
00390     /* Check if software trigger enabled */
00391     if((tmp1 == DAC_CR_TEN2) && (tmp2 == DAC_CR_TSEL2))
00392     {
00393       /* Enable the selected DAC software conversion*/
00394       hdac->Instance->SWTRIGR |= (uint32_t)DAC_SWTRIGR_SWTRIG2;
00395     }
00396   }
00397   
00398   /* Change DAC state */
00399   hdac->State = HAL_DAC_STATE_READY;
00400   
00401   /* Process unlocked */
00402   __HAL_UNLOCK(hdac);
00403     
00404   /* Return function status */
00405   return HAL_OK;
00406 }
00407 
00408 /**
00409   * @brief  Disables DAC and stop conversion of channel.
00410   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00411   *         the configuration information for the specified DAC.
00412   * @param  Channel The selected DAC channel. 
00413   *          This parameter can be one of the following values:
00414   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00415   *            @arg DAC_CHANNEL_2: DAC Channel2 selected  
00416   * @retval HAL status
00417   */
00418 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel)
00419 {
00420   /* Check the parameters */
00421   assert_param(IS_DAC_CHANNEL(Channel));
00422   
00423   /* Disable the Peripheral */
00424   __HAL_DAC_DISABLE(hdac, Channel);
00425  
00426   /* Change DAC state */
00427   hdac->State = HAL_DAC_STATE_READY;
00428   
00429   /* Return function status */
00430   return HAL_OK;
00431 }
00432 
00433 /**
00434   * @brief  Enables DAC and starts conversion of channel.
00435   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00436   *         the configuration information for the specified DAC.
00437   * @param  Channel The selected DAC channel. 
00438   *          This parameter can be one of the following values:
00439   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00440   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00441   * @param  pData The destination peripheral Buffer address.
00442   * @param  Length The length of data to be transferred from memory to DAC peripheral
00443   * @param  Alignment Specifies the data alignment for DAC channel.
00444   *          This parameter can be one of the following values:
00445   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
00446   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
00447   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
00448   * @retval HAL status
00449   */
00450 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment)
00451 {
00452   uint32_t tmpreg = 0U;
00453     
00454   /* Check the parameters */
00455   assert_param(IS_DAC_CHANNEL(Channel));
00456   assert_param(IS_DAC_ALIGN(Alignment));
00457   
00458   /* Process locked */
00459   __HAL_LOCK(hdac);
00460   
00461   /* Change DAC state */
00462   hdac->State = HAL_DAC_STATE_BUSY;
00463 
00464   if(Channel == DAC_CHANNEL_1)
00465   {
00466     /* Set the DMA transfer complete callback for channel1 */
00467     hdac->DMA_Handle1->XferCpltCallback = DAC_DMAConvCpltCh1;
00468 
00469     /* Set the DMA half transfer complete callback for channel1 */
00470     hdac->DMA_Handle1->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh1;
00471 
00472     /* Set the DMA error callback for channel1 */
00473     hdac->DMA_Handle1->XferErrorCallback = DAC_DMAErrorCh1;
00474 
00475     /* Enable the selected DAC channel1 DMA request */
00476     hdac->Instance->CR |= DAC_CR_DMAEN1;
00477     
00478     /* Case of use of channel 1 */
00479     switch(Alignment)
00480     {
00481       case DAC_ALIGN_12B_R:
00482         /* Get DHR12R1 address */
00483         tmpreg = (uint32_t)&hdac->Instance->DHR12R1;
00484         break;
00485       case DAC_ALIGN_12B_L:
00486         /* Get DHR12L1 address */
00487         tmpreg = (uint32_t)&hdac->Instance->DHR12L1;
00488         break;
00489       case DAC_ALIGN_8B_R:
00490         /* Get DHR8R1 address */
00491         tmpreg = (uint32_t)&hdac->Instance->DHR8R1;
00492         break;
00493       default:
00494         break;
00495     }
00496   }
00497   else
00498   {
00499     /* Set the DMA transfer complete callback for channel2 */
00500     hdac->DMA_Handle2->XferCpltCallback = DAC_DMAConvCpltCh2;
00501 
00502     /* Set the DMA half transfer complete callback for channel2 */
00503     hdac->DMA_Handle2->XferHalfCpltCallback = DAC_DMAHalfConvCpltCh2;
00504 
00505     /* Set the DMA error callback for channel2 */
00506     hdac->DMA_Handle2->XferErrorCallback = DAC_DMAErrorCh2;
00507 
00508     /* Enable the selected DAC channel2 DMA request */
00509     hdac->Instance->CR |= DAC_CR_DMAEN2;
00510 
00511     /* Case of use of channel 2 */
00512     switch(Alignment)
00513     {
00514       case DAC_ALIGN_12B_R:
00515         /* Get DHR12R2 address */
00516         tmpreg = (uint32_t)&hdac->Instance->DHR12R2;
00517         break;
00518       case DAC_ALIGN_12B_L:
00519         /* Get DHR12L2 address */
00520         tmpreg = (uint32_t)&hdac->Instance->DHR12L2;
00521         break;
00522       case DAC_ALIGN_8B_R:
00523         /* Get DHR8R2 address */
00524         tmpreg = (uint32_t)&hdac->Instance->DHR8R2;
00525         break;
00526       default:
00527         break;
00528     }
00529   }
00530   
00531   /* Enable the DMA Stream */
00532   if(Channel == DAC_CHANNEL_1)
00533   {
00534     /* Enable the DAC DMA underrun interrupt */
00535     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR1);
00536     
00537     /* Enable the DMA Stream */
00538     HAL_DMA_Start_IT(hdac->DMA_Handle1, (uint32_t)pData, tmpreg, Length);
00539   } 
00540   else
00541   {
00542     /* Enable the DAC DMA underrun interrupt */
00543     __HAL_DAC_ENABLE_IT(hdac, DAC_IT_DMAUDR2);
00544     
00545     /* Enable the DMA Stream */
00546     HAL_DMA_Start_IT(hdac->DMA_Handle2, (uint32_t)pData, tmpreg, Length);
00547   }
00548   
00549   /* Enable the Peripheral */
00550   __HAL_DAC_ENABLE(hdac, Channel);
00551   
00552   /* Process Unlocked */
00553   __HAL_UNLOCK(hdac);
00554   
00555   /* Return function status */
00556   return HAL_OK;
00557 }
00558 
00559 /**
00560   * @brief  Disables DAC and stop conversion of channel.
00561   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00562   *         the configuration information for the specified DAC.
00563   * @param  Channel The selected DAC channel. 
00564   *          This parameter can be one of the following values:
00565   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00566   *            @arg DAC_CHANNEL_2: DAC Channel2 selected   
00567   * @retval HAL status
00568   */
00569 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel)
00570 {
00571   HAL_StatusTypeDef status = HAL_OK;
00572 
00573   /* Check the parameters */
00574   assert_param(IS_DAC_CHANNEL(Channel));
00575   
00576   /* Disable the selected DAC channel DMA request */
00577    hdac->Instance->CR &= ~(DAC_CR_DMAEN1 << Channel);
00578     
00579   /* Disable the Peripheral */
00580   __HAL_DAC_DISABLE(hdac, Channel);
00581   
00582   /* Disable the DMA Channel */
00583   /* Channel1 is used */
00584   if(Channel == DAC_CHANNEL_1)
00585   { 
00586     status = HAL_DMA_Abort(hdac->DMA_Handle1);
00587   }
00588   else /* Channel2 is used for */
00589   { 
00590     status = HAL_DMA_Abort(hdac->DMA_Handle2); 
00591   }
00592 
00593   /* Check if DMA Channel effectively disabled */
00594   if(status != HAL_OK)
00595   {
00596     /* Update DAC state machine to error */
00597     hdac->State = HAL_DAC_STATE_ERROR;      
00598   }
00599   else
00600   {
00601     /* Change DAC state */
00602     hdac->State = HAL_DAC_STATE_READY;
00603   }
00604 
00605   /* Return function status */
00606   return status;
00607 }
00608 
00609 /**
00610   * @brief  Returns the last data output value of the selected DAC channel.
00611   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00612   *         the configuration information for the specified DAC.
00613   * @param  Channel The selected DAC channel. 
00614   *          This parameter can be one of the following values:
00615   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00616   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00617   * @retval The selected DAC channel data output value.
00618   */
00619 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel)
00620 {
00621   /* Check the parameters */
00622   assert_param(IS_DAC_CHANNEL(Channel));
00623   
00624   /* Returns the DAC channel data output register value */
00625   if(Channel == DAC_CHANNEL_1)
00626   {
00627     return hdac->Instance->DOR1;
00628   }
00629   else
00630   {
00631     return hdac->Instance->DOR2;
00632   }
00633 }
00634 
00635 /**
00636   * @brief  Handles DAC interrupt request  
00637   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00638   *         the configuration information for the specified DAC.
00639   * @retval None
00640   */
00641 void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac)
00642 {
00643   /* Check underrun channel 1 flag */
00644   if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR1))
00645   {
00646     /* Change DAC state to error state */
00647     hdac->State = HAL_DAC_STATE_ERROR;
00648     
00649     /* Set DAC error code to channel1 DMA underrun error */
00650     hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH1;
00651     
00652     /* Clear the underrun flag */
00653     __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR1);
00654     
00655     /* Disable the selected DAC channel1 DMA request */
00656     hdac->Instance->CR &= ~DAC_CR_DMAEN1;
00657     
00658     /* Error callback */ 
00659     HAL_DAC_DMAUnderrunCallbackCh1(hdac);
00660   }
00661   /* Check underrun channel 2 flag */
00662   if(__HAL_DAC_GET_FLAG(hdac, DAC_FLAG_DMAUDR2))
00663   {
00664     /* Change DAC state to error state */
00665     hdac->State = HAL_DAC_STATE_ERROR;
00666     
00667     /* Set DAC error code to channel2 DMA underrun error */
00668     hdac->ErrorCode |= HAL_DAC_ERROR_DMAUNDERRUNCH2;
00669     
00670     /* Clear the underrun flag */
00671     __HAL_DAC_CLEAR_FLAG(hdac,DAC_FLAG_DMAUDR2);
00672     
00673     /* Disable the selected DAC channel1 DMA request */
00674     hdac->Instance->CR &= ~DAC_CR_DMAEN2;
00675     
00676     /* Error callback */ 
00677     HAL_DACEx_DMAUnderrunCallbackCh2(hdac);
00678   }
00679 }
00680 
00681 /**
00682   * @brief  Conversion complete callback in non blocking mode for Channel1 
00683   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00684   *         the configuration information for the specified DAC.
00685   * @retval None
00686   */
00687 __weak void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac)
00688 {
00689   /* Prevent unused argument(s) compilation warning */
00690   UNUSED(hdac);
00691   /* NOTE : This function Should not be modified, when the callback is needed,
00692             the HAL_DAC_ConvCpltCallback could be implemented in the user file
00693    */
00694 }
00695 
00696 /**
00697   * @brief  Conversion half DMA transfer callback in non blocking mode for Channel1 
00698   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00699   *         the configuration information for the specified DAC.
00700   * @retval None
00701   */
00702 __weak void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac)
00703 {
00704   /* Prevent unused argument(s) compilation warning */
00705   UNUSED(hdac);
00706   /* NOTE : This function Should not be modified, when the callback is needed,
00707             the HAL_DAC_ConvHalfCpltCallbackCh1 could be implemented in the user file
00708    */
00709 }
00710 
00711 /**
00712   * @brief  Error DAC callback for Channel1.
00713   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00714   *         the configuration information for the specified DAC.
00715   * @retval None
00716   */
00717 __weak void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac)
00718 {
00719   /* Prevent unused argument(s) compilation warning */
00720   UNUSED(hdac);
00721   /* NOTE : This function Should not be modified, when the callback is needed,
00722             the HAL_DAC_ErrorCallbackCh1 could be implemented in the user file
00723    */
00724 }
00725 
00726 /**
00727   * @brief  DMA underrun DAC callback for channel1.
00728   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00729   *         the configuration information for the specified DAC.
00730   * @retval None
00731   */
00732 __weak void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac)
00733 {
00734   /* Prevent unused argument(s) compilation warning */
00735   UNUSED(hdac);
00736   /* NOTE : This function Should not be modified, when the callback is needed,
00737             the HAL_DAC_DMAUnderrunCallbackCh1 could be implemented in the user file
00738    */
00739 }
00740 
00741 /**
00742   * @}
00743   */
00744   
00745 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions
00746  *  @brief      Peripheral Control functions 
00747  *
00748 @verbatim   
00749   ==============================================================================
00750              ##### Peripheral Control functions #####
00751   ==============================================================================  
00752     [..]  This section provides functions allowing to:
00753       (+) Configure channels. 
00754       (+) Set the specified data holding register value for DAC channel.
00755       
00756 @endverbatim
00757   * @{
00758   */
00759 
00760 /**
00761   * @brief  Configures the selected DAC channel.
00762   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00763   *         the configuration information for the specified DAC.
00764   * @param  sConfig DAC configuration structure.
00765   * @param  Channel The selected DAC channel. 
00766   *          This parameter can be one of the following values:
00767   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00768   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00769   * @retval HAL status
00770   */
00771 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel)
00772 {
00773   uint32_t tmpreg1 = 0U, tmpreg2 = 0U;
00774 
00775   /* Check the DAC parameters */
00776   assert_param(IS_DAC_TRIGGER(sConfig->DAC_Trigger));
00777   assert_param(IS_DAC_OUTPUT_BUFFER_STATE(sConfig->DAC_OutputBuffer));
00778   assert_param(IS_DAC_CHANNEL(Channel));
00779   
00780   /* Process locked */
00781   __HAL_LOCK(hdac);
00782   
00783   /* Change DAC state */
00784   hdac->State = HAL_DAC_STATE_BUSY;
00785   
00786   /* Get the DAC CR value */
00787   tmpreg1 = hdac->Instance->CR;
00788   /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
00789   tmpreg1 &= ~(((uint32_t)(DAC_CR_MAMP1 | DAC_CR_WAVE1 | DAC_CR_TSEL1 | DAC_CR_TEN1 | DAC_CR_BOFF1)) << Channel);
00790   /* Configure for the selected DAC channel: buffer output, trigger */
00791   /* Set TSELx and TENx bits according to DAC_Trigger value */
00792   /* Set BOFFx bit according to DAC_OutputBuffer value */   
00793   tmpreg2 = (sConfig->DAC_Trigger | sConfig->DAC_OutputBuffer);
00794   /* Calculate CR register value depending on DAC_Channel */
00795   tmpreg1 |= tmpreg2 << Channel;
00796   /* Write to DAC CR */
00797   hdac->Instance->CR = tmpreg1;
00798   /* Disable wave generation */
00799   hdac->Instance->CR &= ~(DAC_CR_WAVE1 << Channel);
00800   
00801   /* Change DAC state */
00802   hdac->State = HAL_DAC_STATE_READY;
00803   
00804   /* Process unlocked */
00805   __HAL_UNLOCK(hdac);
00806   
00807   /* Return function status */
00808   return HAL_OK;
00809 }
00810 
00811 /**
00812   * @brief  Set the specified data holding register value for DAC channel.
00813   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00814   *         the configuration information for the specified DAC.
00815   * @param  Channel The selected DAC channel. 
00816   *          This parameter can be one of the following values:
00817   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00818   *            @arg DAC_CHANNEL_2: DAC Channel2 selected  
00819   * @param  Alignment Specifies the data alignment.
00820   *          This parameter can be one of the following values:
00821   *            @arg DAC_ALIGN_8B_R: 8bit right data alignment selected
00822   *            @arg DAC_ALIGN_12B_L: 12bit left data alignment selected
00823   *            @arg DAC_ALIGN_12B_R: 12bit right data alignment selected
00824   * @param  Data Data to be loaded in the selected data holding register.
00825   * @retval HAL status
00826   */
00827 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data)
00828 {  
00829   __IO uint32_t tmp = 0U;
00830   
00831   /* Check the parameters */
00832   assert_param(IS_DAC_CHANNEL(Channel));
00833   assert_param(IS_DAC_ALIGN(Alignment));
00834   assert_param(IS_DAC_DATA(Data));
00835   
00836   tmp = (uint32_t)hdac->Instance; 
00837   if(Channel == DAC_CHANNEL_1)
00838   {
00839     tmp += DAC_DHR12R1_ALIGNMENT(Alignment);
00840   }
00841   else
00842   {
00843     tmp += DAC_DHR12R2_ALIGNMENT(Alignment);
00844   }
00845 
00846   /* Set the DAC channel1 selected data holding register */
00847   *(__IO uint32_t *) tmp = Data;
00848   
00849   /* Return function status */
00850   return HAL_OK;
00851 }
00852 
00853 /**
00854   * @}
00855   */
00856 
00857 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions
00858  *  @brief   Peripheral State and Errors functions 
00859  *
00860 @verbatim   
00861   ==============================================================================
00862             ##### Peripheral State and Errors functions #####
00863   ==============================================================================  
00864     [..]
00865     This subsection provides functions allowing to
00866       (+) Check the DAC state.
00867       (+) Check the DAC Errors.
00868         
00869 @endverbatim
00870   * @{
00871   */
00872 
00873 /**
00874   * @brief  return the DAC state
00875   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00876   *         the configuration information for the specified DAC.
00877   * @retval HAL state
00878   */
00879 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac)
00880 {
00881   /* Return DAC state */
00882   return hdac->State;
00883 }
00884 
00885 
00886 /**
00887   * @brief  Return the DAC error code
00888   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00889   *         the configuration information for the specified DAC.
00890   * @retval DAC Error Code
00891   */
00892 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac)
00893 {
00894   return hdac->ErrorCode;
00895 }
00896 
00897 /**
00898   * @}
00899   */
00900 
00901 /**
00902   * @brief  DMA conversion complete callback. 
00903   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00904   *                the configuration information for the specified DMA module.
00905   * @retval None
00906   */
00907 static void DAC_DMAConvCpltCh1(DMA_HandleTypeDef *hdma)   
00908 {
00909   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
00910   
00911   HAL_DAC_ConvCpltCallbackCh1(hdac); 
00912   
00913   hdac->State= HAL_DAC_STATE_READY;
00914 }
00915 
00916 /**
00917   * @brief  DMA half transfer complete callback. 
00918   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00919   *                the configuration information for the specified DMA module.
00920   * @retval None
00921   */
00922 static void DAC_DMAHalfConvCpltCh1(DMA_HandleTypeDef *hdma)   
00923 {
00924     DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
00925     /* Conversion complete callback */
00926     HAL_DAC_ConvHalfCpltCallbackCh1(hdac); 
00927 }
00928 
00929 /**
00930   * @brief  DMA error callback 
00931   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00932   *                the configuration information for the specified DMA module.
00933   * @retval None
00934   */
00935 static void DAC_DMAErrorCh1(DMA_HandleTypeDef *hdma)   
00936 {
00937   DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent;
00938     
00939   /* Set DAC error code to DMA error */
00940   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
00941     
00942   HAL_DAC_ErrorCallbackCh1(hdac); 
00943     
00944   hdac->State= HAL_DAC_STATE_READY;
00945 }
00946 
00947 /**
00948   * @}
00949   */
00950 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\
00951           STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\
00952           STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx ||\
00953                   STM32F413xx || STM32F423xx */
00954 #endif /* HAL_DAC_MODULE_ENABLED */
00955 
00956 /**
00957   * @}
00958   */
00959 
00960 /**
00961   * @}
00962   */
00963 
00964 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/