STM32F439xx HAL User Manual
|
00001 /** 00002 ****************************************************************************** 00003 * @file stm32f4xx_hal_dac_ex.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 DAC extension peripheral: 00008 * + Extended features functions 00009 * 00010 * 00011 @verbatim 00012 ============================================================================== 00013 ##### How to use this driver ##### 00014 ============================================================================== 00015 [..] 00016 (+) When Dual mode is enabled (i.e DAC Channel1 and Channel2 are used simultaneously) : 00017 Use HAL_DACEx_DualGetValue() to get digital data to be converted and use 00018 HAL_DACEx_DualSetValue() to set digital value to converted simultaneously in Channel 1 and Channel 2. 00019 (+) Use HAL_DACEx_TriangleWaveGenerate() to generate Triangle signal. 00020 (+) Use HAL_DACEx_NoiseWaveGenerate() to generate Noise signal. 00021 00022 @endverbatim 00023 ****************************************************************************** 00024 * @attention 00025 * 00026 * <h2><center>© COPYRIGHT(c) 2017 STMicroelectronics</center></h2> 00027 * 00028 * Redistribution and use in source and binary forms, with or without modification, 00029 * are permitted provided that the following conditions are met: 00030 * 1. Redistributions of source code must retain the above copyright notice, 00031 * this list of conditions and the following disclaimer. 00032 * 2. Redistributions in binary form must reproduce the above copyright notice, 00033 * this list of conditions and the following disclaimer in the documentation 00034 * and/or other materials provided with the distribution. 00035 * 3. Neither the name of STMicroelectronics nor the names of its contributors 00036 * may be used to endorse or promote products derived from this software 00037 * without specific prior written permission. 00038 * 00039 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 00040 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE 00041 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE 00042 * DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE 00043 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL 00044 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR 00045 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER 00046 * CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 00047 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 00048 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 00049 * 00050 ****************************************************************************** 00051 */ 00052 00053 00054 /* Includes ------------------------------------------------------------------*/ 00055 #include "stm32f4xx_hal.h" 00056 00057 /** @addtogroup STM32F4xx_HAL_Driver 00058 * @{ 00059 */ 00060 00061 /** @defgroup DACEx DACEx 00062 * @brief DAC driver modules 00063 * @{ 00064 */ 00065 00066 #ifdef HAL_DAC_MODULE_ENABLED 00067 00068 #if defined(STM32F405xx) || defined(STM32F415xx) || defined(STM32F407xx) || defined(STM32F417xx) ||\ 00069 defined(STM32F427xx) || defined(STM32F437xx) || defined(STM32F429xx) || defined(STM32F439xx) ||\ 00070 defined(STM32F410Tx) || defined(STM32F410Cx) || defined(STM32F410Rx) || defined(STM32F446xx) ||\ 00071 defined(STM32F469xx) || defined(STM32F479xx) || defined(STM32F413xx) || defined(STM32F423xx) 00072 /* Private typedef -----------------------------------------------------------*/ 00073 /* Private define ------------------------------------------------------------*/ 00074 /* Private macro -------------------------------------------------------------*/ 00075 /* Private variables ---------------------------------------------------------*/ 00076 /* Private function prototypes -----------------------------------------------*/ 00077 /* Private functions ---------------------------------------------------------*/ 00078 /* Exported functions --------------------------------------------------------*/ 00079 /** @defgroup DACEx_Exported_Functions DAC Exported Functions 00080 * @{ 00081 */ 00082 00083 /** @defgroup DACEx_Exported_Functions_Group1 Extended features functions 00084 * @brief Extended features functions 00085 * 00086 @verbatim 00087 ============================================================================== 00088 ##### Extended features functions ##### 00089 ============================================================================== 00090 [..] This section provides functions allowing to: 00091 (+) Start conversion. 00092 (+) Stop conversion. 00093 (+) Start conversion and enable DMA transfer. 00094 (+) Stop conversion and disable DMA transfer. 00095 (+) Get result of conversion. 00096 (+) Get result of dual mode conversion. 00097 00098 @endverbatim 00099 * @{ 00100 */ 00101 00102 /** 00103 * @brief Returns the last data output value of the selected DAC channel. 00104 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00105 * the configuration information for the specified DAC. 00106 * @retval The selected DAC channel data output value. 00107 */ 00108 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef* hdac) 00109 { 00110 uint32_t tmp = 0U; 00111 00112 tmp |= hdac->Instance->DOR1; 00113 00114 tmp |= hdac->Instance->DOR2 << 16U; 00115 00116 /* Returns the DAC channel data output register value */ 00117 return tmp; 00118 } 00119 00120 /** 00121 * @brief Enables or disables the selected DAC channel wave generation. 00122 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00123 * the configuration information for the specified DAC. 00124 * @param Channel The selected DAC channel. 00125 * This parameter can be one of the following values: 00126 * DAC_CHANNEL_1 / DAC_CHANNEL_2 00127 * @param Amplitude Select max triangle amplitude. 00128 * This parameter can be one of the following values: 00129 * @arg DAC_TRIANGLEAMPLITUDE_1: Select max triangle amplitude of 1 00130 * @arg DAC_TRIANGLEAMPLITUDE_3: Select max triangle amplitude of 3 00131 * @arg DAC_TRIANGLEAMPLITUDE_7: Select max triangle amplitude of 7 00132 * @arg DAC_TRIANGLEAMPLITUDE_15: Select max triangle amplitude of 15 00133 * @arg DAC_TRIANGLEAMPLITUDE_31: Select max triangle amplitude of 31 00134 * @arg DAC_TRIANGLEAMPLITUDE_63: Select max triangle amplitude of 63 00135 * @arg DAC_TRIANGLEAMPLITUDE_127: Select max triangle amplitude of 127 00136 * @arg DAC_TRIANGLEAMPLITUDE_255: Select max triangle amplitude of 255 00137 * @arg DAC_TRIANGLEAMPLITUDE_511: Select max triangle amplitude of 511 00138 * @arg DAC_TRIANGLEAMPLITUDE_1023: Select max triangle amplitude of 1023 00139 * @arg DAC_TRIANGLEAMPLITUDE_2047: Select max triangle amplitude of 2047 00140 * @arg DAC_TRIANGLEAMPLITUDE_4095: Select max triangle amplitude of 4095 00141 * @retval HAL status 00142 */ 00143 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude) 00144 { 00145 /* Check the parameters */ 00146 assert_param(IS_DAC_CHANNEL(Channel)); 00147 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); 00148 00149 /* Process locked */ 00150 __HAL_LOCK(hdac); 00151 00152 /* Change DAC state */ 00153 hdac->State = HAL_DAC_STATE_BUSY; 00154 00155 /* Enable the selected wave generation for the selected DAC channel */ 00156 MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_1 | Amplitude) << Channel); 00157 00158 /* Change DAC state */ 00159 hdac->State = HAL_DAC_STATE_READY; 00160 00161 /* Process unlocked */ 00162 __HAL_UNLOCK(hdac); 00163 00164 /* Return function status */ 00165 return HAL_OK; 00166 } 00167 00168 /** 00169 * @brief Enables or disables the selected DAC channel wave generation. 00170 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00171 * the configuration information for the specified DAC. 00172 * @param Channel The selected DAC channel. 00173 * This parameter can be one of the following values: 00174 * DAC_CHANNEL_1 / DAC_CHANNEL_2 00175 * @param Amplitude Unmask DAC channel LFSR for noise wave generation. 00176 * This parameter can be one of the following values: 00177 * @arg DAC_LFSRUNMASK_BIT0: Unmask DAC channel LFSR bit0 for noise wave generation 00178 * @arg DAC_LFSRUNMASK_BITS1_0: Unmask DAC channel LFSR bit[1:0] for noise wave generation 00179 * @arg DAC_LFSRUNMASK_BITS2_0: Unmask DAC channel LFSR bit[2:0] for noise wave generation 00180 * @arg DAC_LFSRUNMASK_BITS3_0: Unmask DAC channel LFSR bit[3:0] for noise wave generation 00181 * @arg DAC_LFSRUNMASK_BITS4_0: Unmask DAC channel LFSR bit[4:0] for noise wave generation 00182 * @arg DAC_LFSRUNMASK_BITS5_0: Unmask DAC channel LFSR bit[5:0] for noise wave generation 00183 * @arg DAC_LFSRUNMASK_BITS6_0: Unmask DAC channel LFSR bit[6:0] for noise wave generation 00184 * @arg DAC_LFSRUNMASK_BITS7_0: Unmask DAC channel LFSR bit[7:0] for noise wave generation 00185 * @arg DAC_LFSRUNMASK_BITS8_0: Unmask DAC channel LFSR bit[8:0] for noise wave generation 00186 * @arg DAC_LFSRUNMASK_BITS9_0: Unmask DAC channel LFSR bit[9:0] for noise wave generation 00187 * @arg DAC_LFSRUNMASK_BITS10_0: Unmask DAC channel LFSR bit[10:0] for noise wave generation 00188 * @arg DAC_LFSRUNMASK_BITS11_0: Unmask DAC channel LFSR bit[11:0] for noise wave generation 00189 * @retval HAL status 00190 */ 00191 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Amplitude) 00192 { 00193 /* Check the parameters */ 00194 assert_param(IS_DAC_CHANNEL(Channel)); 00195 assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(Amplitude)); 00196 00197 /* Process locked */ 00198 __HAL_LOCK(hdac); 00199 00200 /* Change DAC state */ 00201 hdac->State = HAL_DAC_STATE_BUSY; 00202 00203 /* Enable the selected wave generation for the selected DAC channel */ 00204 MODIFY_REG(hdac->Instance->CR, (DAC_CR_WAVE1 | DAC_CR_MAMP1) << Channel, (DAC_CR_WAVE1_0 | Amplitude) << Channel); 00205 00206 /* Change DAC state */ 00207 hdac->State = HAL_DAC_STATE_READY; 00208 00209 /* Process unlocked */ 00210 __HAL_UNLOCK(hdac); 00211 00212 /* Return function status */ 00213 return HAL_OK; 00214 } 00215 00216 /** 00217 * @brief Set the specified data holding register value for dual DAC channel. 00218 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00219 * the configuration information for the specified DAC. 00220 * @param Alignment Specifies the data alignment for dual channel DAC. 00221 * This parameter can be one of the following values: 00222 * DAC_ALIGN_8B_R: 8bit right data alignment selected 00223 * DAC_ALIGN_12B_L: 12bit left data alignment selected 00224 * DAC_ALIGN_12B_R: 12bit right data alignment selected 00225 * @param Data1 Data for DAC Channel2 to be loaded in the selected data holding register. 00226 * @param Data2 Data for DAC Channel1 to be loaded in the selected data holding register. 00227 * @note In dual mode, a unique register access is required to write in both 00228 * DAC channels at the same time. 00229 * @retval HAL status 00230 */ 00231 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef* hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2) 00232 { 00233 uint32_t data = 0U, tmp = 0U; 00234 00235 /* Check the parameters */ 00236 assert_param(IS_DAC_ALIGN(Alignment)); 00237 assert_param(IS_DAC_DATA(Data1)); 00238 assert_param(IS_DAC_DATA(Data2)); 00239 00240 /* Calculate and set dual DAC data holding register value */ 00241 if (Alignment == DAC_ALIGN_8B_R) 00242 { 00243 data = ((uint32_t)Data2 << 8U) | Data1; 00244 } 00245 else 00246 { 00247 data = ((uint32_t)Data2 << 16U) | Data1; 00248 } 00249 00250 tmp = (uint32_t)hdac->Instance; 00251 tmp += DAC_DHR12RD_ALIGNMENT(Alignment); 00252 00253 /* Set the dual DAC selected data holding register */ 00254 *(__IO uint32_t *)tmp = data; 00255 00256 /* Return function status */ 00257 return HAL_OK; 00258 } 00259 00260 /** 00261 * @} 00262 */ 00263 00264 /** 00265 * @brief Conversion complete callback in non blocking mode for Channel2 00266 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00267 * the configuration information for the specified DAC. 00268 * @retval None 00269 */ 00270 __weak void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef* hdac) 00271 { 00272 /* Prevent unused argument(s) compilation warning */ 00273 UNUSED(hdac); 00274 /* NOTE : This function Should not be modified, when the callback is needed, 00275 the HAL_DAC_ConvCpltCallback could be implemented in the user file 00276 */ 00277 } 00278 00279 /** 00280 * @brief Conversion half DMA transfer callback in non blocking mode for Channel2 00281 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00282 * the configuration information for the specified DAC. 00283 * @retval None 00284 */ 00285 __weak void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef* hdac) 00286 { 00287 /* Prevent unused argument(s) compilation warning */ 00288 UNUSED(hdac); 00289 /* NOTE : This function Should not be modified, when the callback is needed, 00290 the HAL_DAC_ConvHalfCpltCallbackCh2 could be implemented in the user file 00291 */ 00292 } 00293 00294 /** 00295 * @brief Error DAC callback for Channel2. 00296 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00297 * the configuration information for the specified DAC. 00298 * @retval None 00299 */ 00300 __weak void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac) 00301 { 00302 /* Prevent unused argument(s) compilation warning */ 00303 UNUSED(hdac); 00304 /* NOTE : This function Should not be modified, when the callback is needed, 00305 the HAL_DAC_ErrorCallback could be implemented in the user file 00306 */ 00307 } 00308 00309 /** 00310 * @brief DMA underrun DAC callback for channel2. 00311 * @param hdac pointer to a DAC_HandleTypeDef structure that contains 00312 * the configuration information for the specified DAC. 00313 * @retval None 00314 */ 00315 __weak void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac) 00316 { 00317 /* Prevent unused argument(s) compilation warning */ 00318 UNUSED(hdac); 00319 /* NOTE : This function Should not be modified, when the callback is needed, 00320 the HAL_DAC_DMAUnderrunCallbackCh2 could be implemented in the user file 00321 */ 00322 } 00323 00324 /** 00325 * @brief DMA conversion complete callback. 00326 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 00327 * the configuration information for the specified DMA module. 00328 * @retval None 00329 */ 00330 void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma) 00331 { 00332 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 00333 00334 HAL_DACEx_ConvCpltCallbackCh2(hdac); 00335 00336 hdac->State= HAL_DAC_STATE_READY; 00337 } 00338 00339 /** 00340 * @brief DMA half transfer complete callback. 00341 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 00342 * the configuration information for the specified DMA module. 00343 * @retval None 00344 */ 00345 void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma) 00346 { 00347 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 00348 /* Conversion complete callback */ 00349 HAL_DACEx_ConvHalfCpltCallbackCh2(hdac); 00350 } 00351 00352 /** 00353 * @brief DMA error callback 00354 * @param hdma pointer to a DMA_HandleTypeDef structure that contains 00355 * the configuration information for the specified DMA module. 00356 * @retval None 00357 */ 00358 void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma) 00359 { 00360 DAC_HandleTypeDef* hdac = ( DAC_HandleTypeDef* )((DMA_HandleTypeDef* )hdma)->Parent; 00361 00362 /* Set DAC error code to DMA error */ 00363 hdac->ErrorCode |= HAL_DAC_ERROR_DMA; 00364 00365 HAL_DACEx_ErrorCallbackCh2(hdac); 00366 00367 hdac->State= HAL_DAC_STATE_READY; 00368 } 00369 00370 /** 00371 * @} 00372 */ 00373 00374 #endif /* STM32F405xx || STM32F415xx || STM32F407xx || STM32F417xx ||\ 00375 STM32F427xx || STM32F437xx || STM32F429xx || STM32F439xx ||\ 00376 STM32F410xx || STM32F446xx || STM32F469xx || STM32F479xx ||\ 00377 STM32F413xx || STM32F423xx */ 00378 00379 #endif /* HAL_DAC_MODULE_ENABLED */ 00380 00381 /** 00382 * @} 00383 */ 00384 00385 /** 00386 * @} 00387 */ 00388 00389 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/