STM32F103xB HAL User Manual
stm32f1xx_hal_dac_ex.c
Go to the documentation of this file.
00001 /**
00002   ******************************************************************************
00003   * @file    stm32f1xx_hal_dac_ex.c
00004   * @author  MCD Application Team
00005   * @brief   Extended DAC HAL module driver.
00006   *          This file provides firmware functions to manage the extended
00007   *          functionalities of the DAC peripheral.
00008   *
00009   *
00010   @verbatim
00011   ==============================================================================
00012                       ##### How to use this driver #####
00013   ==============================================================================
00014     [..]
00015 
00016      *** Dual mode IO operation ***
00017      ==============================
00018      [..]
00019       (+) When Dual mode is enabled (i.e. DAC Channel1 and Channel2 are used simultaneously) :
00020           Use HAL_DACEx_DualGetValue() to get digital data to be converted and use
00021           HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in
00022           Channel 1 and Channel 2.
00023 
00024      *** Signal generation operation ***
00025      ===================================
00026      [..]
00027       (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal.
00028       (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal.
00029 
00030  @endverbatim
00031   ******************************************************************************
00032   * @attention
00033   *
00034   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
00035   * All rights reserved.</center></h2>
00036   *
00037   * This software component is licensed by ST under BSD 3-Clause license,
00038   * the "License"; You may not use this file except in compliance with the
00039   * License. You may obtain a copy of the License at:
00040   *                        opensource.org/licenses/BSD-3-Clause
00041   *
00042   ******************************************************************************
00043   */
00044 
00045 
00046 /* Includes ------------------------------------------------------------------*/
00047 #include "stm32f1xx_hal.h"
00048 
00049 /** @addtogroup STM32F1xx_HAL_Driver
00050   * @{
00051   */
00052 
00053 #ifdef HAL_DAC_MODULE_ENABLED
00054 
00055 #if defined(DAC)
00056 
00057 /** @defgroup DACEx DACEx
00058   * @brief DAC Extended HAL module driver
00059   * @{
00060   */
00061 
00062 /* Private typedef -----------------------------------------------------------*/
00063 /* Private define ------------------------------------------------------------*/
00064 /* Private macro -------------------------------------------------------------*/
00065 /* Private variables ---------------------------------------------------------*/
00066 /* Private function prototypes -----------------------------------------------*/
00067 /* Exported functions --------------------------------------------------------*/
00068 
00069 /** @defgroup DACEx_Exported_Functions DACEx Exported Functions
00070   * @{
00071   */
00072 
00073 /** @defgroup DACEx_Exported_Functions_Group2 IO operation functions
00074   *  @brief    Extended IO operation functions
00075   *
00076 @verbatim
00077   ==============================================================================
00078                  ##### Extended features functions #####
00079   ==============================================================================
00080     [..]  This section provides functions allowing to:
00081       (+) Start conversion.
00082       (+) Stop conversion.
00083       (+) Start conversion and enable DMA transfer.
00084       (+) Stop conversion and disable DMA transfer.
00085       (+) Get result of conversion.
00086       (+) Get result of dual mode conversion.
00087 
00088 @endverbatim
00089   * @{
00090   */
00091 
00092 
00093 /**
00094   * @brief  Enables DAC and starts conversion of both channels.
00095   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00096   *         the configuration information for the specified DAC.
00097   * @retval HAL status
00098   */
00099 HAL_StatusTypeDef HAL_DACEx_DualStart(DAC_HandleTypeDef *hdac)
00100 {
00101   uint32_t tmp_swtrig = 0UL;
00102 
00103 
00104   /* Process locked */
00105   __HAL_LOCK(hdac);
00106 
00107   /* Change DAC state */
00108   hdac->State = HAL_DAC_STATE_BUSY;
00109 
00110   /* Enable the Peripheral */
00111   __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_1);
00112   __HAL_DAC_ENABLE(hdac, DAC_CHANNEL_2);
00113 
00114   /* Check if software trigger enabled */
00115   if ((hdac->Instance->CR & (DAC_CR_TEN1 | DAC_CR_TSEL1)) == DAC_TRIGGER_SOFTWARE)
00116   {
00117     tmp_swtrig |= DAC_SWTRIGR_SWTRIG1;
00118   }
00119   if ((hdac->Instance->CR & (DAC_CR_TEN2 | DAC_CR_TSEL2)) == (DAC_TRIGGER_SOFTWARE << (DAC_CHANNEL_2 & 0x10UL)))
00120   {
00121     tmp_swtrig |= DAC_SWTRIGR_SWTRIG2;
00122   }
00123   /* Enable the selected DAC software conversion*/
00124   SET_BIT(hdac->Instance->SWTRIGR, tmp_swtrig);
00125 
00126   /* Change DAC state */
00127   hdac->State = HAL_DAC_STATE_READY;
00128 
00129   /* Process unlocked */
00130   __HAL_UNLOCK(hdac);
00131 
00132   /* Return function status */
00133   return HAL_OK;
00134 }
00135 
00136 /**
00137   * @brief  Disables DAC and stop conversion of both channels.
00138   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00139   *         the configuration information for the specified DAC.
00140   * @retval HAL status
00141   */
00142 HAL_StatusTypeDef HAL_DACEx_DualStop(DAC_HandleTypeDef *hdac)
00143 {
00144 
00145   /* Disable the Peripheral */
00146   __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_1);
00147   __HAL_DAC_DISABLE(hdac, DAC_CHANNEL_2);
00148 
00149   /* Change DAC state */
00150   hdac->State = HAL_DAC_STATE_READY;
00151 
00152   /* Return function status */
00153   return HAL_OK;
00154 }
00155 
00156 
00157 /**
00158   * @brief  Enable or disable the selected DAC channel wave generation.
00159   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00160   *         the configuration information for the specified DAC.
00161   * @param  Channel The selected DAC channel.
00162   *          This parameter can be one of the following values:
00163   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00164   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00165   * @param  Amplitude Select max triangle amplitude.
00166   *          This parameter can be one of the following values:
00167   *            @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1
00168   *            @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3
00169   *            @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7
00170   *            @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15
00171   *            @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31
00172   *            @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63
00173   *            @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127
00174   *            @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255
00175   *            @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511
00176   *            @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023
00177   *            @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047
00178   *            @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095
00179   * @retval HAL status
00180   */
00181 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
00182 {
00183   /* Check the parameters */
00184   assert_param(IS_DAC_CHANNEL(Channel));
00185   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
00186 
00187   /* Process locked */
00188   __HAL_LOCK(hdac);
00189 
00190   /* Change DAC state */
00191   hdac->State = HAL_DAC_STATE_BUSY;
00192 
00193   /* Enable the triangle wave generation for the selected DAC channel */
00194   MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
00195              (DAC_CR_WAVE1_1 | Amplitude) << (Channel & 0x10UL));
00196 
00197   /* Change DAC state */
00198   hdac->State = HAL_DAC_STATE_READY;
00199 
00200   /* Process unlocked */
00201   __HAL_UNLOCK(hdac);
00202 
00203   /* Return function status */
00204   return HAL_OK;
00205 }
00206 
00207 /**
00208   * @brief  Enable or disable the selected DAC channel wave generation.
00209   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00210   *         the configuration information for the specified DAC.
00211   * @param  Channel The selected DAC channel.
00212   *          This parameter can be one of the following values:
00213   *            @arg DAC_CHANNEL_1: DAC Channel1 selected
00214   *            @arg DAC_CHANNEL_2: DAC Channel2 selected
00215   * @param  Amplitude Unmask DAC channel LFSR for noise wave generation.
00216   *          This parameter can be one of the following values:
00217   *            @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation
00218   *            @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation
00219   *            @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation
00220   *            @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation
00221   *            @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation
00222   *            @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation
00223   *            @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation
00224   *            @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation
00225   *            @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation
00226   *            @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation
00227   *            @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation
00228   *            @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation
00229   * @retval HAL status
00230   */
00231 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude)
00232 {
00233   /* Check the parameters */
00234   assert_param(IS_DAC_CHANNEL(Channel));
00235   assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude));
00236 
00237   /* Process locked */
00238   __HAL_LOCK(hdac);
00239 
00240   /* Change DAC state */
00241   hdac->State = HAL_DAC_STATE_BUSY;
00242 
00243   /* Enable the noise wave generation for the selected DAC channel */
00244   MODIFY_REG(hdac->Instance->CR, ((DAC_CR_WAVE1) | (DAC_CR_MAMP1)) << (Channel & 0x10UL),
00245              (DAC_CR_WAVE1_0 | Amplitude) << (Channel & 0x10UL));
00246 
00247   /* Change DAC state */
00248   hdac->State = HAL_DAC_STATE_READY;
00249 
00250   /* Process unlocked */
00251   __HAL_UNLOCK(hdac);
00252 
00253   /* Return function status */
00254   return HAL_OK;
00255 }
00256 
00257 
00258 /**
00259   * @brief  Set the specified data holding register value for dual DAC channel.
00260   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00261   *               the configuration information for the specified DAC.
00262   * @param  Alignment Specifies the data alignment for dual channel DAC.
00263   *          This parameter can be one of the following values:
00264   *            DAC_ALIGN_8B_R: 8bit right data alignment selected
00265   *            DAC_ALIGN_12B_L: 12bit left data alignment selected
00266   *            DAC_ALIGN_12B_R: 12bit right data alignment selected
00267   * @param  Data1 Data for DAC Channel1 to be loaded in the selected data holding register.
00268   * @param  Data2 Data for DAC Channel2 to be loaded in the selected data  holding register.
00269   * @note   In dual mode, a unique register access is required to write in both
00270   *          DAC channels at the same time.
00271   * @retval HAL status
00272   */
00273 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2)
00274 {
00275   uint32_t data;
00276   uint32_t tmp;
00277 
00278   /* Check the parameters */
00279   assert_param(IS_DAC_ALIGN(Alignment));
00280   assert_param(IS_DAC_DATA(Data1));
00281   assert_param(IS_DAC_DATA(Data2));
00282 
00283   /* Calculate and set dual DAC data holding register value */
00284   if (Alignment == DAC_ALIGN_8B_R)
00285   {
00286     data = ((uint32_t)Data2 << 8U) | Data1;
00287   }
00288   else
00289   {
00290     data = ((uint32_t)Data2 << 16U) | Data1;
00291   }
00292 
00293   tmp = (uint32_t)hdac->Instance;
00294   tmp += DAC_DHR12RD_ALIGNMENT(Alignment);
00295 
00296   /* Set the dual DAC selected data holding register */
00297   *(__IO uint32_t *)tmp = data;
00298 
00299   /* Return function status */
00300   return HAL_OK;
00301 }
00302 
00303 /**
00304   * @brief  Conversion complete callback in non-blocking mode for Channel2.
00305   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00306   *         the configuration information for the specified DAC.
00307   * @retval None
00308   */
00309 __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac)
00310 {
00311   /* Prevent unused argument(s) compilation warning */
00312   UNUSED(hdac);
00313 
00314   /* NOTE : This function should not be modified, when the callback is needed,
00315             the HAL_DACEx_ConvCpltCallbackCh2 could be implemented in the user file
00316    */
00317 }
00318 
00319 /**
00320   * @brief  Conversion half DMA transfer callback in non-blocking mode for Channel2.
00321   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00322   *         the configuration information for the specified DAC.
00323   * @retval None
00324   */
00325 __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac)
00326 {
00327   /* Prevent unused argument(s) compilation warning */
00328   UNUSED(hdac);
00329 
00330   /* NOTE : This function should not be modified, when the callback is needed,
00331             the HAL_DACEx_ConvHalfCpltCallbackCh2 could be implemented in the user file
00332    */
00333 }
00334 
00335 /**
00336   * @brief  Error DAC callback for Channel2.
00337   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00338   *         the configuration information for the specified DAC.
00339   * @retval None
00340   */
00341 __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac)
00342 {
00343   /* Prevent unused argument(s) compilation warning */
00344   UNUSED(hdac);
00345 
00346   /* NOTE : This function should not be modified, when the callback is needed,
00347             the HAL_DACEx_ErrorCallbackCh2 could be implemented in the user file
00348    */
00349 }
00350 
00351 /**
00352   * @brief  DMA underrun DAC callback for Channel2.
00353   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00354   *         the configuration information for the specified DAC.
00355   * @retval None
00356   */
00357 __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac)
00358 {
00359   /* Prevent unused argument(s) compilation warning */
00360   UNUSED(hdac);
00361 
00362   /* NOTE : This function should not be modified, when the callback is needed,
00363             the HAL_DACEx_DMAUnderrunCallbackCh2 could be implemented in the user file
00364    */
00365 }
00366 
00367 
00368 
00369 /**
00370   * @}
00371   */
00372 
00373 /** @defgroup DACEx_Exported_Functions_Group3 Peripheral Control functions
00374   *  @brief    Extended Peripheral Control functions
00375   *
00376 @verbatim
00377   ==============================================================================
00378              ##### Peripheral Control functions #####
00379   ==============================================================================
00380     [..]  This section provides functions allowing to:
00381       (+) Set the specified data holding register value for DAC channel.
00382 
00383 @endverbatim
00384   * @{
00385   */
00386 
00387 
00388 /**
00389   * @brief  Return the last data output value of the selected DAC channel.
00390   * @param  hdac pointer to a DAC_HandleTypeDef structure that contains
00391   *         the configuration information for the specified DAC.
00392   * @retval The selected DAC channel data output value.
00393   */
00394 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac)
00395 {
00396   uint32_t tmp = 0UL;
00397 
00398   tmp |= hdac->Instance->DOR1;
00399 
00400   tmp |= hdac->Instance->DOR2 << 16UL;
00401 
00402   /* Returns the DAC channel data output register value */
00403   return tmp;
00404 }
00405 
00406 
00407 /**
00408   * @}
00409   */
00410 /**
00411   * @}
00412   */
00413 
00414 /* Private functions ---------------------------------------------------------*/
00415 /** @defgroup DACEx_Private_Functions DACEx private functions
00416   *  @brief    Extended private functions
00417   * @{
00418   */
00419 
00420 
00421 /**
00422   * @brief  DMA conversion complete callback.
00423   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00424   *                the configuration information for the specified DMA module.
00425   * @retval None
00426   */
00427 void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma)
00428 {
00429   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00430 
00431 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
00432   hdac->ConvCpltCallbackCh2(hdac);
00433 #else
00434   HAL_DACEx_ConvCpltCallbackCh2(hdac);
00435 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
00436 
00437   hdac->State = HAL_DAC_STATE_READY;
00438 }
00439 
00440 /**
00441   * @brief  DMA half transfer complete callback.
00442   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00443   *                the configuration information for the specified DMA module.
00444   * @retval None
00445   */
00446 void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma)
00447 {
00448   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00449   /* Conversion complete callback */
00450 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
00451   hdac->ConvHalfCpltCallbackCh2(hdac);
00452 #else
00453   HAL_DACEx_ConvHalfCpltCallbackCh2(hdac);
00454 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
00455 }
00456 
00457 /**
00458   * @brief  DMA error callback.
00459   * @param  hdma pointer to a DMA_HandleTypeDef structure that contains
00460   *                the configuration information for the specified DMA module.
00461   * @retval None
00462   */
00463 void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma)
00464 {
00465   DAC_HandleTypeDef *hdac = (DAC_HandleTypeDef *)((DMA_HandleTypeDef *)hdma)->Parent;
00466 
00467   /* Set DAC error code to DMA error */
00468   hdac->ErrorCode |= HAL_DAC_ERROR_DMA;
00469 
00470 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1)
00471   hdac->ErrorCallbackCh2(hdac);
00472 #else
00473   HAL_DACEx_ErrorCallbackCh2(hdac);
00474 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */
00475 
00476   hdac->State = HAL_DAC_STATE_READY;
00477 }
00478 
00479 
00480 /**
00481   * @}
00482   */
00483 
00484 /**
00485   * @}
00486   */
00487 
00488 #endif /* DAC */
00489 
00490 #endif /* HAL_DAC_MODULE_ENABLED */
00491 
00492 /**
00493   * @}
00494   */
00495 
00496 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/