1 /** 2 ****************************************************************************** 3 * @file stm32l0xx_hal_dac.h 4 * @author MCD Application Team 5 * @brief Header file of DAC HAL module. 6 ****************************************************************************** 7 * @attention 8 * 9 * Copyright (c) 2016 STMicroelectronics. 10 * All rights reserved. 11 * 12 * This software is licensed under terms that can be found in the LICENSE file 13 * in the root directory of this software component. 14 * If no LICENSE file comes with this software, it is provided AS-IS. 15 * 16 ****************************************************************************** 17 */ 18 19 /* Define to prevent recursive inclusion -------------------------------------*/ 20 #ifndef __STM32L0xx_HAL_DAC_H 21 #define __STM32L0xx_HAL_DAC_H 22 23 #ifdef __cplusplus 24 extern "C" { 25 #endif 26 27 #if !defined (STM32L010xB) && !defined (STM32L010x8) && !defined (STM32L010x6) && !defined (STM32L010x4) && !defined (STM32L011xx) && !defined (STM32L021xx) && !defined (STM32L031xx) && !defined (STM32L041xx) && !defined (STM32L051xx) && !defined (STM32L071xx) && !defined (STM32L081xx) 28 29 /* Includes ------------------------------------------------------------------*/ 30 #include "stm32l0xx_hal_def.h" 31 32 /** @addtogroup STM32L0xx_HAL_Driver 33 * @{ 34 */ 35 36 /** @defgroup DAC DAC 37 * @{ 38 */ 39 40 /* Exported types ------------------------------------------------------------*/ 41 42 /** @defgroup DAC_Exported_Types DAC Exported Types 43 * @{ 44 */ 45 46 /** 47 * @brief HAL State structures definition 48 */ 49 typedef enum 50 { 51 HAL_DAC_STATE_RESET = 0x00U, /*!< DAC not yet initialized or disabled */ 52 HAL_DAC_STATE_READY = 0x01U, /*!< DAC initialized and ready for use */ 53 HAL_DAC_STATE_BUSY = 0x02U, /*!< DAC internal processing is ongoing */ 54 HAL_DAC_STATE_TIMEOUT = 0x03U, /*!< DAC timeout state */ 55 HAL_DAC_STATE_ERROR = 0x04U /*!< DAC error state */ 56 57 }HAL_DAC_StateTypeDef; 58 59 /** 60 * @brief DAC handle Structure definition 61 */ 62 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 63 typedef struct __DAC_HandleTypeDef 64 #else 65 typedef struct 66 #endif 67 { 68 DAC_TypeDef *Instance; /*!< Register base address */ 69 70 __IO HAL_DAC_StateTypeDef State; /*!< DAC communication state */ 71 72 HAL_LockTypeDef Lock; /*!< DAC locking object */ 73 74 DMA_HandleTypeDef *DMA_Handle1; /*!< Pointer DMA handler for channel 1 */ 75 76 #if defined (DAC_CHANNEL2_SUPPORT) 77 DMA_HandleTypeDef *DMA_Handle2; /*!< Pointer DMA handler for channel 2 */ 78 #endif 79 80 __IO uint32_t ErrorCode; /*!< DAC Error code */ 81 82 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 83 void (* ConvCpltCallbackCh1) (struct __DAC_HandleTypeDef *hdac); 84 void (* ConvHalfCpltCallbackCh1) (struct __DAC_HandleTypeDef *hdac); 85 void (* ErrorCallbackCh1) (struct __DAC_HandleTypeDef *hdac); 86 void (* DMAUnderrunCallbackCh1) (struct __DAC_HandleTypeDef *hdac); 87 void (* ConvCpltCallbackCh2) (struct __DAC_HandleTypeDef* hdac); 88 void (* ConvHalfCpltCallbackCh2) (struct __DAC_HandleTypeDef* hdac); 89 void (* ErrorCallbackCh2) (struct __DAC_HandleTypeDef* hdac); 90 void (* DMAUnderrunCallbackCh2) (struct __DAC_HandleTypeDef* hdac); 91 92 void (* MspInitCallback) (struct __DAC_HandleTypeDef *hdac); 93 void (* MspDeInitCallback ) (struct __DAC_HandleTypeDef *hdac); 94 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ 95 96 }DAC_HandleTypeDef; 97 98 /** 99 * @brief DAC Configuration regular Channel structure definition 100 */ 101 typedef struct 102 { 103 uint32_t DAC_Trigger; /*!< Specifies the external trigger for the selected DAC channel. 104 This parameter can be a value of @ref DAC_trigger_selection */ 105 106 uint32_t DAC_OutputBuffer; /*!< Specifies whether the DAC channel output buffer is enabled or disabled. 107 This parameter can be a value of @ref DAC_output_buffer */ 108 109 }DAC_ChannelConfTypeDef; 110 111 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 112 /** 113 * @brief HAL DAC Callback ID enumeration definition 114 */ 115 typedef enum 116 { 117 HAL_DAC_CH1_COMPLETE_CB_ID = 0x00U, /*!< DAC CH1 Complete Callback ID */ 118 HAL_DAC_CH1_HALF_COMPLETE_CB_ID = 0x01U, /*!< DAC CH1 half Complete Callback ID */ 119 HAL_DAC_CH1_ERROR_ID = 0x02U, /*!< DAC CH1 error Callback ID */ 120 HAL_DAC_CH1_UNDERRUN_CB_ID = 0x03U, /*!< DAC CH1 underrun Callback ID */ 121 HAL_DAC_CH2_COMPLETE_CB_ID = 0x04U, /*!< DAC CH2 Complete Callback ID */ 122 HAL_DAC_CH2_HALF_COMPLETE_CB_ID = 0x05U, /*!< DAC CH2 half Complete Callback ID */ 123 HAL_DAC_CH2_ERROR_ID = 0x06U, /*!< DAC CH2 error Callback ID */ 124 HAL_DAC_CH2_UNDERRUN_CB_ID = 0x07U, /*!< DAC CH2 underrun Callback ID */ 125 HAL_DAC_MSP_INIT_CB_ID = 0x08U, /*!< DAC MspInit Callback ID */ 126 HAL_DAC_MSP_DEINIT_CB_ID = 0x09U, /*!< DAC MspDeInit Callback ID */ 127 HAL_DAC_ALL_CB_ID = 0x0AU /*!< DAC All ID */ 128 }HAL_DAC_CallbackIDTypeDef; 129 130 /** 131 * @brief HAL DAC Callback pointer definition 132 */ 133 typedef void (*pDAC_CallbackTypeDef)(DAC_HandleTypeDef *hdac); 134 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ 135 136 /** 137 * @} 138 */ 139 140 /* Exported constants --------------------------------------------------------*/ 141 142 /** @defgroup DAC_Exported_Constants DAC Exported Constants 143 * @{ 144 */ 145 146 /** @defgroup DAC_Error_Code DAC Error Code 147 * @{ 148 */ 149 #define HAL_DAC_ERROR_NONE 0x00U /*!< No error */ 150 #define HAL_DAC_ERROR_DMAUNDERRUNCH1 0x01U /*!< DAC channel1 DMA underrun error */ 151 #if defined (DAC_CHANNEL2_SUPPORT) 152 #define HAL_DAC_ERROR_DMAUNDERRUNCH2 0x02U /*!< DAC channel2 DMA underrun error */ 153 #endif 154 #define HAL_DAC_ERROR_DMA 0x04U /*!< DMA error */ 155 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 156 #define HAL_DAC_ERROR_INVALID_CALLBACK 0x10U /*!< Invalid callback error */ 157 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ 158 159 /** 160 * @} 161 */ 162 163 /** @defgroup DAC_trigger_selection DAC trigger selection 164 * @{ 165 */ 166 #define DAC_TRIGGER_NONE (0x00000000U) /*!< Conversion is automatic once the DAC1_DHRxxxx register has been loaded, and not by external trigger */ 167 #define DAC_TRIGGER_T6_TRGO ( DAC_CR_TEN1) /*!< TIM6 TRGO selected as external conversion trigger for DAC channel */ 168 #define DAC_TRIGGER_T21_TRGO ((uint32_t)( DAC_CR_TSEL1_1 | DAC_CR_TSEL1_0 | DAC_CR_TEN1)) /*!< TIM21 TRGO selected as external conversion trigger for DAC channel */ 169 #define DAC_TRIGGER_T2_TRGO ((uint32_t)(DAC_CR_TSEL1_2 | DAC_CR_TEN1)) /*!< TIM2 TRGO selected as external conversion trigger for DAC channel */ 170 #define DAC_TRIGGER_EXT_IT9 ((uint32_t)(DAC_CR_TSEL1_2 | DAC_CR_TSEL1_1 | DAC_CR_TEN1)) /*!< EXTI Line9 event selected as external conversion trigger for DAC channel */ 171 #define DAC_TRIGGER_SOFTWARE ((uint32_t)(DAC_CR_TSEL1_2 | DAC_CR_TSEL1_1 | DAC_CR_TSEL1_0 | DAC_CR_TEN1)) /*!< Conversion started by software trigger for DAC channel */ 172 173 #if defined (DAC_CHANNEL2_SUPPORT) 174 #define DAC_TRIGGER_T3_TRGO ((uint32_t)( DAC_CR_TSEL1_0 | DAC_CR_TEN1)) /*!< TIM3 TRGO selected as external conversion trigger for DAC channel */ 175 #define DAC_TRIGGER_T3_CH3 ((uint32_t)( DAC_CR_TSEL1_1 | DAC_CR_TEN1)) /*!< TIM3 CH3 selected as external conversion trigger for DAC channel */ 176 #define DAC_TRIGGER_T7_TRGO ((uint32_t)(DAC_CR_TSEL1_2 | DAC_CR_TSEL1_0 | DAC_CR_TEN1)) /*!< TIM7 TRGO selected as external conversion trigger for DAC channel */ 177 #endif 178 179 #if defined (DAC_CHANNEL2_SUPPORT) 180 #define IS_DAC_TRIGGER(TRIGGER) (((TRIGGER) == DAC_TRIGGER_NONE) || \ 181 ((TRIGGER) == DAC_TRIGGER_T6_TRGO) || \ 182 ((TRIGGER) == DAC_TRIGGER_T3_TRGO) || \ 183 ((TRIGGER) == DAC_TRIGGER_T3_CH3) || \ 184 ((TRIGGER) == DAC_TRIGGER_T7_TRGO) || \ 185 ((TRIGGER) == DAC_TRIGGER_T21_TRGO) || \ 186 ((TRIGGER) == DAC_TRIGGER_T2_TRGO) || \ 187 ((TRIGGER) == DAC_TRIGGER_EXT_IT9) || \ 188 ((TRIGGER) == DAC_TRIGGER_SOFTWARE)) 189 #else 190 #define IS_DAC_TRIGGER(TRIGGER) (((TRIGGER) == DAC_TRIGGER_NONE) || \ 191 ((TRIGGER) == DAC_TRIGGER_T6_TRGO) || \ 192 ((TRIGGER) == DAC_TRIGGER_T21_TRGO) || \ 193 ((TRIGGER) == DAC_TRIGGER_T2_TRGO) || \ 194 ((TRIGGER) == DAC_TRIGGER_EXT_IT9) || \ 195 ((TRIGGER) == DAC_TRIGGER_SOFTWARE)) 196 #endif /* DAC_CHANNEL2_SUPPORT */ 197 /** 198 * @} 199 */ 200 201 /** @defgroup DAC_output_buffer DAC output buffer 202 * @{ 203 */ 204 #define DAC_OUTPUTBUFFER_ENABLE (0x00000000U) 205 #define DAC_OUTPUTBUFFER_DISABLE (DAC_CR_BOFF1) 206 207 #define IS_DAC_OUTPUT_BUFFER_STATE(STATE) (((STATE) == DAC_OUTPUTBUFFER_ENABLE) || \ 208 ((STATE) == DAC_OUTPUTBUFFER_DISABLE)) 209 /** 210 * @} 211 */ 212 213 /** @defgroup DAC_Channel_selection DAC Channel selection 214 * @{ 215 */ 216 #define DAC_CHANNEL_1 (0x00000000U) 217 #if defined (DAC_CHANNEL2_SUPPORT) 218 #define DAC_CHANNEL_2 (0x00000010U) 219 #endif 220 221 #if defined (DAC_CHANNEL2_SUPPORT) 222 #define IS_DAC_CHANNEL(CHANNEL) (((CHANNEL) == DAC_CHANNEL_1) || \ 223 ((CHANNEL) == DAC_CHANNEL_2)) 224 #else 225 #define IS_DAC_CHANNEL(CHANNEL) ((CHANNEL) == DAC_CHANNEL_1) 226 #endif 227 /** 228 * @} 229 */ 230 231 /** @defgroup DAC_data_alignement DAC data alignment 232 * @{ 233 */ 234 #define DAC_ALIGN_12B_R (0x00000000U) 235 #define DAC_ALIGN_12B_L (0x00000004U) 236 #define DAC_ALIGN_8B_R (0x00000008U) 237 238 #define IS_DAC_ALIGN(ALIGN) (((ALIGN) == DAC_ALIGN_12B_R) || \ 239 ((ALIGN) == DAC_ALIGN_12B_L) || \ 240 ((ALIGN) == DAC_ALIGN_8B_R)) 241 /** 242 * @} 243 */ 244 245 /** @defgroup DAC_data DAC data 246 * @{ 247 */ 248 #define IS_DAC_DATA(DATA) ((DATA) <= 0xFFF0U) 249 /** 250 * @} 251 */ 252 253 /** @defgroup DAC_flags_definition DAC flags definition 254 * @{ 255 */ 256 #define DAC_FLAG_DMAUDR1 (DAC_SR_DMAUDR1) 257 #if defined (DAC_CHANNEL2_SUPPORT) 258 #define DAC_FLAG_DMAUDR2 (DAC_SR_DMAUDR2) 259 #endif 260 261 /** 262 * @} 263 */ 264 265 /** @defgroup DAC_IT_definition DAC IT definition 266 * @{ 267 */ 268 #define DAC_IT_DMAUDR1 (DAC_SR_DMAUDR1) 269 #if defined (DAC_CHANNEL2_SUPPORT) 270 #define DAC_IT_DMAUDR2 (DAC_SR_DMAUDR2) 271 #endif 272 273 /** 274 * @} 275 */ 276 277 /** 278 * @} 279 */ 280 281 /* Exported macro ------------------------------------------------------------*/ 282 283 /** @defgroup DAC_Exported_Macros DAC Exported Macros 284 * @{ 285 */ 286 287 /** @brief Reset DAC handle state 288 * @param __HANDLE__ specifies the DAC handle. 289 * @retval None 290 */ 291 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 292 #define __HAL_DAC_RESET_HANDLE_STATE(__HANDLE__) do { \ 293 (__HANDLE__)->State = HAL_DAC_STATE_RESET; \ 294 (__HANDLE__)->MspInitCallback = NULL; \ 295 (__HANDLE__)->MspDeInitCallback = NULL; \ 296 } while(0) 297 #else 298 #define __HAL_DAC_RESET_HANDLE_STATE(__HANDLE__) ((__HANDLE__)->State = HAL_DAC_STATE_RESET) 299 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ 300 301 /** @brief Enable the DAC channel 302 * @param __HANDLE__ specifies the DAC handle. 303 * @param __DAC_CHANNEL__ specifies the DAC channel 304 * @retval None 305 */ 306 #define __HAL_DAC_ENABLE(__HANDLE__, __DAC_CHANNEL__) \ 307 SET_BIT((__HANDLE__)->Instance->CR, (DAC_CR_EN1 << (__DAC_CHANNEL__))) 308 309 /** @brief Disable the DAC channel 310 * @param __HANDLE__ specifies the DAC handle 311 * @param __DAC_CHANNEL__ specifies the DAC channel. 312 * @retval None 313 */ 314 #define __HAL_DAC_DISABLE(__HANDLE__, __DAC_CHANNEL__) \ 315 CLEAR_BIT((__HANDLE__)->Instance->CR, (DAC_CR_EN1 << (__DAC_CHANNEL__))) 316 317 318 #define __HAL_DAC_ENABLE_IT(__HANDLE__, __INTERRUPT__) \ 319 SET_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__)) 320 321 322 /** @brief Disable the DAC interrupt 323 * @param __HANDLE__ specifies the DAC handle 324 * @param __INTERRUPT__ specifies the DAC interrupt. 325 * @retval None 326 */ 327 #define __HAL_DAC_DISABLE_IT(__HANDLE__, __INTERRUPT__) \ 328 CLEAR_BIT((__HANDLE__)->Instance->CR, (__INTERRUPT__)) 329 330 /** @brief Check whether the specified DAC interrupt source is enabled or not. 331 * @param __HANDLE__ DAC handle 332 * @param __INTERRUPT__ DAC interrupt source to check 333 * This parameter can be any combination of the following values: 334 * @arg DAC_IT_DMAUDR1: DAC channel 1 DMA underrun interrupt 335 * @arg DAC_IT_DMAUDR2: DAC channel 2 DMA underrun interrupt (STM32L072xx STM32L073xx STM32L082xx STM32L083xx only) 336 * @retval State of interruption (SET or RESET) 337 */ 338 #define __HAL_DAC_GET_IT_SOURCE(__HANDLE__, __INTERRUPT__) \ 339 (((__HANDLE__)->Instance->CR & (__INTERRUPT__)) == (__INTERRUPT__)) 340 341 /** @brief Get the selected DAC's flag status. 342 * @param __HANDLE__ specifies the DAC handle. 343 * @param __FLAG__ specifies the FLAG. 344 * @retval None 345 */ 346 #define __HAL_DAC_GET_FLAG(__HANDLE__, __FLAG__) \ 347 ((((__HANDLE__)->Instance->SR) & (__FLAG__)) == (__FLAG__)) 348 349 /** @brief Clear the DAC's flag. 350 * @param __HANDLE__ specifies the DAC handle. 351 * @param __FLAG__ specifies the FLAG. 352 * @retval None 353 */ 354 #define __HAL_DAC_CLEAR_FLAG(__HANDLE__, __FLAG__) \ 355 (((__HANDLE__)->Instance->SR) = (__FLAG__)) 356 357 /** 358 * @} 359 */ 360 361 /* Private macro ------------------------------------------------------------*/ 362 363 /** @defgroup DAC_Private_Macros DAC Private Macros 364 * @{ 365 */ 366 367 /** @brief Set DHR12R1 alignment 368 * @param __ALIGNMENT__ specifies the DAC alignment 369 * @retval None 370 */ 371 #define DAC_DHR12R1_ALIGNMENT(__ALIGNMENT__) ((0x00000008U) + (__ALIGNMENT__)) 372 373 /** @brief Set DHR12R2 alignment 374 * @param __ALIGNMENT__ specifies the DAC alignment 375 * @retval None 376 */ 377 #define DAC_DHR12R2_ALIGNMENT(__ALIGNMENT__) ((0x00000014U) + (__ALIGNMENT__)) 378 379 /** @brief Set DHR12RD alignment 380 * @param __ALIGNMENT__ specifies the DAC alignment 381 * @retval None 382 */ 383 #define DAC_DHR12RD_ALIGNMENT(__ALIGNMENT__) ((0x00000020U) + (__ALIGNMENT__)) 384 385 /** @brief Enable the DAC interrupt 386 * @param __HANDLE__ specifies the DAC handle 387 * @param __INTERRUPT__ specifies the DAC interrupt. 388 * @retval None 389 */ 390 391 /** 392 * @} 393 */ 394 395 396 /* Include DAC HAL Extension module */ 397 #include "stm32l0xx_hal_dac_ex.h" 398 399 /* Exported functions --------------------------------------------------------*/ 400 401 /** @defgroup DAC_Exported_Functions DAC Exported Functions 402 * @{ 403 */ 404 405 /** @defgroup DAC_Exported_Functions_Group1 Initialization and de-initialization functions 406 * @{ 407 */ 408 /* Initialization and de-initialization functions *****************************/ 409 HAL_StatusTypeDef HAL_DAC_Init(DAC_HandleTypeDef* hdac); 410 HAL_StatusTypeDef HAL_DAC_DeInit(DAC_HandleTypeDef* hdac); 411 void HAL_DAC_MspInit(DAC_HandleTypeDef* hdac); 412 void HAL_DAC_MspDeInit(DAC_HandleTypeDef* hdac); 413 414 /** 415 * @} 416 */ 417 418 /** @defgroup DAC_Exported_Functions_Group2 IO operation functions 419 * @{ 420 */ 421 /* IO operation functions *****************************************************/ 422 HAL_StatusTypeDef HAL_DAC_Start(DAC_HandleTypeDef* hdac, uint32_t Channel); 423 HAL_StatusTypeDef HAL_DAC_Stop(DAC_HandleTypeDef* hdac, uint32_t Channel); 424 HAL_StatusTypeDef HAL_DAC_Start_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t* pData, uint32_t Length, uint32_t Alignment); 425 HAL_StatusTypeDef HAL_DAC_Stop_DMA(DAC_HandleTypeDef* hdac, uint32_t Channel); 426 HAL_StatusTypeDef HAL_DAC_SetValue(DAC_HandleTypeDef* hdac, uint32_t Channel, uint32_t Alignment, uint32_t Data); 427 uint32_t HAL_DAC_GetValue(DAC_HandleTypeDef* hdac, uint32_t Channel); 428 void HAL_DAC_IRQHandler(DAC_HandleTypeDef* hdac); 429 void HAL_DAC_ConvCpltCallbackCh1(DAC_HandleTypeDef* hdac); 430 void HAL_DAC_ConvHalfCpltCallbackCh1(DAC_HandleTypeDef* hdac); 431 void HAL_DAC_ErrorCallbackCh1(DAC_HandleTypeDef *hdac); 432 void HAL_DAC_DMAUnderrunCallbackCh1(DAC_HandleTypeDef *hdac); 433 434 #if (USE_HAL_DAC_REGISTER_CALLBACKS == 1) 435 /* DAC callback registering/unregistering */ 436 HAL_StatusTypeDef HAL_DAC_RegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackId, pDAC_CallbackTypeDef pCallback); 437 HAL_StatusTypeDef HAL_DAC_UnRegisterCallback (DAC_HandleTypeDef *hdac, HAL_DAC_CallbackIDTypeDef CallbackId); 438 #endif /* USE_HAL_DAC_REGISTER_CALLBACKS */ 439 440 /** 441 * @} 442 */ 443 444 /** @defgroup DAC_Exported_Functions_Group3 Peripheral Control functions 445 * @{ 446 */ 447 /* Peripheral Control functions ***********************************************/ 448 HAL_StatusTypeDef HAL_DAC_ConfigChannel(DAC_HandleTypeDef* hdac, DAC_ChannelConfTypeDef* sConfig, uint32_t Channel); 449 450 /** 451 * @} 452 */ 453 454 /** @defgroup DAC_Exported_Functions_Group4 Peripheral State and Errors functions 455 * @{ 456 */ 457 /* Peripheral State functions ***************************************************/ 458 HAL_DAC_StateTypeDef HAL_DAC_GetState(DAC_HandleTypeDef* hdac); 459 uint32_t HAL_DAC_GetError(DAC_HandleTypeDef *hdac); 460 461 /** 462 * @} 463 */ 464 465 /** 466 * @} 467 */ 468 469 /** 470 * @} 471 */ 472 473 /** 474 * @} 475 */ 476 477 #endif /* !STM32L010xB && !STM32L010x8 && !STM32L010x6 && !STM32L010x4 && !STM32L011xx && !STM32L021xx && !STM32L031xx && !STM32L041xx && !STM32L071xx && !STM32L081xx*/ 478 479 #ifdef __cplusplus 480 } 481 #endif 482 483 #endif /*__STM32L0xx_HAL_DAC_H */ 484 485