1 /**
2   ******************************************************************************
3   * @file    stm32g4xx_hal_dac_ex.h
4   * @author  MCD Application Team
5   * @brief   Header file of DAC HAL Extended module.
6   ******************************************************************************
7   * @attention
8   *
9   * Copyright (c) 2019 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 STM32G4xx_HAL_DAC_EX_H
21 #define STM32G4xx_HAL_DAC_EX_H
22 
23 #ifdef __cplusplus
24 extern "C" {
25 #endif
26 
27 /** @addtogroup STM32G4xx_HAL_Driver
28   * @{
29   */
30 
31 /* Includes ------------------------------------------------------------------*/
32 #include "stm32g4xx_hal_def.h"
33 
34 #if defined(DAC1) || defined(DAC2) || defined(DAC3) ||defined (DAC4)
35 
36 /** @addtogroup DACEx
37   * @{
38   */
39 
40 /* Exported types ------------------------------------------------------------*/
41 
42 /**
43   * @brief  HAL State structures definition
44   */
45 
46 /* Exported constants --------------------------------------------------------*/
47 
48 /** @defgroup DACEx_Exported_Constants DACEx Exported Constants
49   * @{
50   */
51 
52 /** @defgroup DACEx_lfsrunmask_triangleamplitude DACEx lfsrunmask triangle amplitude
53   * @{
54   */
55 #define DAC_LFSRUNMASK_BIT0                0x00000000UL                                                        /*!< Unmask DAC channel LFSR bit0 for noise wave generation */
56 #define DAC_LFSRUNMASK_BITS1_0             (                                                   DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[1:0] for noise wave generation */
57 #define DAC_LFSRUNMASK_BITS2_0             (                                  DAC_CR_MAMP1_1                 ) /*!< Unmask DAC channel LFSR bit[2:0] for noise wave generation */
58 #define DAC_LFSRUNMASK_BITS3_0             (                                  DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[3:0] for noise wave generation */
59 #define DAC_LFSRUNMASK_BITS4_0             (                 DAC_CR_MAMP1_2                                  ) /*!< Unmask DAC channel LFSR bit[4:0] for noise wave generation */
60 #define DAC_LFSRUNMASK_BITS5_0             (                 DAC_CR_MAMP1_2                  | DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[5:0] for noise wave generation */
61 #define DAC_LFSRUNMASK_BITS6_0             (                 DAC_CR_MAMP1_2 | DAC_CR_MAMP1_1                 ) /*!< Unmask DAC channel LFSR bit[6:0] for noise wave generation */
62 #define DAC_LFSRUNMASK_BITS7_0             (                 DAC_CR_MAMP1_2 | DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[7:0] for noise wave generation */
63 #define DAC_LFSRUNMASK_BITS8_0             (DAC_CR_MAMP1_3                                                   ) /*!< Unmask DAC channel LFSR bit[8:0] for noise wave generation */
64 #define DAC_LFSRUNMASK_BITS9_0             (DAC_CR_MAMP1_3                                   | DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[9:0] for noise wave generation */
65 #define DAC_LFSRUNMASK_BITS10_0            (DAC_CR_MAMP1_3                  | DAC_CR_MAMP1_1                 ) /*!< Unmask DAC channel LFSR bit[10:0] for noise wave generation */
66 #define DAC_LFSRUNMASK_BITS11_0            (DAC_CR_MAMP1_3                  | DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Unmask DAC channel LFSR bit[11:0] for noise wave generation */
67 #define DAC_TRIANGLEAMPLITUDE_1            0x00000000UL                                                        /*!< Select max triangle amplitude of 1 */
68 #define DAC_TRIANGLEAMPLITUDE_3            (                                                   DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 3 */
69 #define DAC_TRIANGLEAMPLITUDE_7            (                                  DAC_CR_MAMP1_1                 ) /*!< Select max triangle amplitude of 7 */
70 #define DAC_TRIANGLEAMPLITUDE_15           (                                  DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 15 */
71 #define DAC_TRIANGLEAMPLITUDE_31           (                 DAC_CR_MAMP1_2                                  ) /*!< Select max triangle amplitude of 31 */
72 #define DAC_TRIANGLEAMPLITUDE_63           (                 DAC_CR_MAMP1_2                  | DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 63 */
73 #define DAC_TRIANGLEAMPLITUDE_127          (                 DAC_CR_MAMP1_2 | DAC_CR_MAMP1_1                 ) /*!< Select max triangle amplitude of 127 */
74 #define DAC_TRIANGLEAMPLITUDE_255          (                 DAC_CR_MAMP1_2 | DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 255 */
75 #define DAC_TRIANGLEAMPLITUDE_511          (DAC_CR_MAMP1_3                                                   ) /*!< Select max triangle amplitude of 511 */
76 #define DAC_TRIANGLEAMPLITUDE_1023         (DAC_CR_MAMP1_3                                   | DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 1023 */
77 #define DAC_TRIANGLEAMPLITUDE_2047         (DAC_CR_MAMP1_3                  | DAC_CR_MAMP1_1                 ) /*!< Select max triangle amplitude of 2047 */
78 #define DAC_TRIANGLEAMPLITUDE_4095         (DAC_CR_MAMP1_3                  | DAC_CR_MAMP1_1 | DAC_CR_MAMP1_0) /*!< Select max triangle amplitude of 4095 */
79 
80 /**
81   * @}
82   */
83 
84 /** @defgroup DACEx_SawtoothPolarityMode DAC Sawtooth polarity mode
85   * @{
86   */
87 #define DAC_SAWTOOTH_POLARITY_DECREMENT        0x00000000UL            /*!< Sawtooth wave generation, polarity is decrement */
88 #define DAC_SAWTOOTH_POLARITY_INCREMENT        (DAC_STR1_STDIR1)       /*!< Sawtooth wave generation, polarity is increment */
89 
90 /**
91   * @}
92   */
93 /**
94   * @}
95   */
96 
97 /* Exported macro ------------------------------------------------------------*/
98 
99 
100 /* Private macro -------------------------------------------------------------*/
101 
102 /** @defgroup DACEx_Private_Macros DACEx Private Macros
103   * @{
104   */
105 #if defined(STM32G474xx) || defined(STM32G484xx)
106 #define IS_DAC_TRIGGER(DACX, TRIGGER) \
107   (((TRIGGER) == DAC_TRIGGER_NONE)           || \
108    ((TRIGGER) == DAC_TRIGGER_SOFTWARE)       || \
109    ((TRIGGER) == DAC_TRIGGER_T7_TRGO)        || \
110    ((TRIGGER) == DAC_TRIGGER_T15_TRGO)       || \
111    ((TRIGGER) == DAC_TRIGGER_T2_TRGO)        || \
112    ((TRIGGER) == DAC_TRIGGER_T4_TRGO)        || \
113    ((TRIGGER) == DAC_TRIGGER_EXT_IT9)        || \
114    ((TRIGGER) == DAC_TRIGGER_T6_TRGO)        || \
115    ((TRIGGER) == DAC_TRIGGER_T3_TRGO)        || \
116    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG1) || \
117    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG2) || \
118    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG3) || \
119    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG4) || \
120    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG5) || \
121    ((TRIGGER) == DAC_TRIGGER_HRTIM_RST_TRG6) || \
122    (((DACX) == DAC1) &&                         \
123     (((TRIGGER) == DAC_TRIGGER_T8_TRGO)      || \
124      ((TRIGGER) == DAC_TRIGGER_HRTIM_TRG01))     \
125    )                                         || \
126    (((DACX) == DAC2) &&                         \
127     (((TRIGGER) == DAC_TRIGGER_T8_TRGO)      || \
128      ((TRIGGER) == DAC_TRIGGER_HRTIM_TRG02))     \
129    )                                         || \
130    (((DACX) == DAC3) &&                         \
131     (((TRIGGER) == DAC_TRIGGER_T1_TRGO)      || \
132      ((TRIGGER) == DAC_TRIGGER_HRTIM_TRG03))     \
133    )                                         || \
134    (((DACX) == DAC4) &&                         \
135     (((TRIGGER) == DAC_TRIGGER_T8_TRGO)      || \
136      ((TRIGGER) == DAC_TRIGGER_HRTIM_TRG01))     \
137    )                                            \
138   )
139 #else
140 #define IS_DAC_TRIGGER(DACX, TRIGGER) \
141   (((TRIGGER) == DAC_TRIGGER_NONE)           || \
142    ((TRIGGER) == DAC_TRIGGER_SOFTWARE)       || \
143    ((TRIGGER) == DAC_TRIGGER_T7_TRGO)        || \
144    ((TRIGGER) == DAC_TRIGGER_T15_TRGO)       || \
145    ((TRIGGER) == DAC_TRIGGER_T2_TRGO)        || \
146    ((TRIGGER) == DAC_TRIGGER_T4_TRGO)        || \
147    ((TRIGGER) == DAC_TRIGGER_EXT_IT9)        || \
148    ((TRIGGER) == DAC_TRIGGER_T6_TRGO)        || \
149    ((TRIGGER) == DAC_TRIGGER_T3_TRGO)        || \
150    (((DACX) == DAC3) ?                          \
151     ((TRIGGER) == DAC_TRIGGER_T1_TRGO)          \
152     : ((TRIGGER) == DAC_TRIGGER_T8_TRGO)        \
153    )                                            \
154   )
155 #endif
156 
157 #if defined(STM32G474xx) || defined(STM32G484xx)
158 #define IS_DAC_TRIGGER2(DACX, TRIGGER) \
159   (((TRIGGER) == DAC_TRIGGER_NONE)            || \
160    ((TRIGGER) == DAC_TRIGGER_SOFTWARE)        || \
161    ((TRIGGER) == DAC_TRIGGER_T7_TRGO)         || \
162    ((TRIGGER) == DAC_TRIGGER_T15_TRGO)        || \
163    ((TRIGGER) == DAC_TRIGGER_T2_TRGO)         || \
164    ((TRIGGER) == DAC_TRIGGER_T4_TRGO)         || \
165    ((TRIGGER) == DAC_TRIGGER_EXT_IT10)        || \
166    ((TRIGGER) == DAC_TRIGGER_T6_TRGO)         || \
167    ((TRIGGER) == DAC_TRIGGER_T3_TRGO)         || \
168    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG1) || \
169    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG2) || \
170    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG3) || \
171    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG4) || \
172    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG5) || \
173    ((TRIGGER) == DAC_TRIGGER_HRTIM_STEP_TRG6) || \
174    (((DACX) == DAC1) &&                          \
175     ((TRIGGER) == DAC_TRIGGER_T8_TRGO)           \
176    ) ||                                          \
177    (((DACX) == DAC2) &&                          \
178     ((TRIGGER) == DAC_TRIGGER_T8_TRGO)           \
179    ) ||                                          \
180    (((DACX) == DAC3) &&                          \
181     ((TRIGGER) == DAC_TRIGGER_T1_TRGO)           \
182    ) ||                                          \
183    (((DACX) == DAC4) &&                          \
184     ((TRIGGER) == DAC_TRIGGER_T8_TRGO)           \
185    )                                             \
186   )
187 #else
188 #define IS_DAC_TRIGGER2(DACX, TRIGGER) \
189   (((TRIGGER) == DAC_TRIGGER_NONE)            || \
190    ((TRIGGER) == DAC_TRIGGER_SOFTWARE)        || \
191    ((TRIGGER) == DAC_TRIGGER_T7_TRGO)         || \
192    ((TRIGGER) == DAC_TRIGGER_T15_TRGO)        || \
193    ((TRIGGER) == DAC_TRIGGER_T2_TRGO)         || \
194    ((TRIGGER) == DAC_TRIGGER_T4_TRGO)         || \
195    ((TRIGGER) == DAC_TRIGGER_EXT_IT10)        || \
196    ((TRIGGER) == DAC_TRIGGER_T6_TRGO)         || \
197    ((TRIGGER) == DAC_TRIGGER_T3_TRGO)         || \
198    (((DACX) == DAC3) ?                           \
199     ((TRIGGER) == DAC_TRIGGER_T1_TRGO)           \
200     :((TRIGGER) == DAC_TRIGGER_T8_TRGO)          \
201    )                                             \
202   )
203 #endif
204 #define  IS_DAC_HIGH_FREQUENCY_MODE(MODE) (((MODE) == DAC_HIGH_FREQUENCY_INTERFACE_MODE_DISABLE)         || \
205                                            ((MODE) == DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_80MHZ)     || \
206                                            ((MODE) == DAC_HIGH_FREQUENCY_INTERFACE_MODE_ABOVE_160MHZ)    || \
207                                            ((MODE) == DAC_HIGH_FREQUENCY_INTERFACE_MODE_AUTOMATIC))
208 
209 #define IS_DAC_SAMPLETIME(TIME) ((TIME) <= 0x000003FFU)
210 
211 #define IS_DAC_HOLDTIME(TIME)   ((TIME) <= 0x000003FFU)
212 
213 #define IS_DAC_SAMPLEANDHOLD(MODE) (((MODE) == DAC_SAMPLEANDHOLD_DISABLE) || \
214                                     ((MODE) == DAC_SAMPLEANDHOLD_ENABLE))
215 
216 #define IS_DAC_TRIMMINGVALUE(TRIMMINGVALUE) ((TRIMMINGVALUE) <= 0x1FU)
217 
218 #define IS_DAC_NEWTRIMMINGVALUE(TRIMMINGVALUE) ((TRIMMINGVALUE) <= 0x1FU)
219 
220 #define IS_DAC_CHIP_CONNECTION(CONNECT) (((CONNECT) == DAC_CHIPCONNECT_EXTERNAL) || \
221                                          ((CONNECT) == DAC_CHIPCONNECT_INTERNAL) || \
222                                          ((CONNECT) == DAC_CHIPCONNECT_BOTH))
223 
224 #define IS_DAC_TRIMMING(TRIMMING) (((TRIMMING) == DAC_TRIMMING_FACTORY) || \
225                                    ((TRIMMING) == DAC_TRIMMING_USER))
226 
227 #define IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(VALUE) (((VALUE) == DAC_LFSRUNMASK_BIT0) || \
228                                                       ((VALUE) == DAC_LFSRUNMASK_BITS1_0) || \
229                                                       ((VALUE) == DAC_LFSRUNMASK_BITS2_0) || \
230                                                       ((VALUE) == DAC_LFSRUNMASK_BITS3_0) || \
231                                                       ((VALUE) == DAC_LFSRUNMASK_BITS4_0) || \
232                                                       ((VALUE) == DAC_LFSRUNMASK_BITS5_0) || \
233                                                       ((VALUE) == DAC_LFSRUNMASK_BITS6_0) || \
234                                                       ((VALUE) == DAC_LFSRUNMASK_BITS7_0) || \
235                                                       ((VALUE) == DAC_LFSRUNMASK_BITS8_0) || \
236                                                       ((VALUE) == DAC_LFSRUNMASK_BITS9_0) || \
237                                                       ((VALUE) == DAC_LFSRUNMASK_BITS10_0) || \
238                                                       ((VALUE) == DAC_LFSRUNMASK_BITS11_0) || \
239                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_1) || \
240                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_3) || \
241                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_7) || \
242                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_15) || \
243                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_31) || \
244                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_63) || \
245                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_127) || \
246                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_255) || \
247                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_511) || \
248                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_1023) || \
249                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_2047) || \
250                                                       ((VALUE) == DAC_TRIANGLEAMPLITUDE_4095))
251 
252 #define IS_DAC_SAWTOOTH_POLARITY(POLARITY) (((POLARITY) == DAC_SAWTOOTH_POLARITY_DECREMENT) || \
253                                             ((POLARITY) == DAC_SAWTOOTH_POLARITY_INCREMENT))
254 
255 #define IS_DAC_RESET_DATA(DATA) ((DATA) <= 0x00000FFFUL)
256 #define IS_DAC_STEP_DATA(DATA)  ((DATA) <= 0x0000FFFFUL)
257 /**
258   * @}
259   */
260 
261 /* Exported functions --------------------------------------------------------*/
262 /* Extended features functions ***********************************************/
263 
264 /** @addtogroup DACEx_Exported_Functions
265   * @{
266   */
267 
268 /** @addtogroup DACEx_Exported_Functions_Group2
269   * @{
270   */
271 /* IO operation functions *****************************************************/
272 
273 HAL_StatusTypeDef HAL_DACEx_TriangleWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude);
274 HAL_StatusTypeDef HAL_DACEx_NoiseWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Amplitude);
275 HAL_StatusTypeDef HAL_DACEx_SawtoothWaveGenerate(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t Polarity,
276                                                  uint32_t ResetData, uint32_t StepData);
277 HAL_StatusTypeDef HAL_DACEx_SawtoothWaveDataReset(DAC_HandleTypeDef *hdac, uint32_t Channel);
278 HAL_StatusTypeDef HAL_DACEx_SawtoothWaveDataStep(DAC_HandleTypeDef *hdac, uint32_t Channel);
279 
280 HAL_StatusTypeDef HAL_DACEx_DualStart(DAC_HandleTypeDef *hdac);
281 HAL_StatusTypeDef HAL_DACEx_DualStop(DAC_HandleTypeDef *hdac);
282 HAL_StatusTypeDef HAL_DACEx_DualStart_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel, uint32_t *pData, uint32_t Length,
283                                           uint32_t Alignment);
284 HAL_StatusTypeDef HAL_DACEx_DualStop_DMA(DAC_HandleTypeDef *hdac, uint32_t Channel);
285 HAL_StatusTypeDef HAL_DACEx_DualSetValue(DAC_HandleTypeDef *hdac, uint32_t Alignment, uint32_t Data1, uint32_t Data2);
286 uint32_t HAL_DACEx_DualGetValue(DAC_HandleTypeDef *hdac);
287 
288 void HAL_DACEx_ConvCpltCallbackCh2(DAC_HandleTypeDef *hdac);
289 void HAL_DACEx_ConvHalfCpltCallbackCh2(DAC_HandleTypeDef *hdac);
290 void HAL_DACEx_ErrorCallbackCh2(DAC_HandleTypeDef *hdac);
291 void HAL_DACEx_DMAUnderrunCallbackCh2(DAC_HandleTypeDef *hdac);
292 
293 
294 /**
295   * @}
296   */
297 
298 /** @addtogroup DACEx_Exported_Functions_Group3
299   * @{
300   */
301 /* Peripheral Control functions ***********************************************/
302 
303 HAL_StatusTypeDef HAL_DACEx_SelfCalibrate(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel);
304 HAL_StatusTypeDef HAL_DACEx_SetUserTrimming(DAC_HandleTypeDef *hdac, DAC_ChannelConfTypeDef *sConfig, uint32_t Channel,
305                                             uint32_t NewTrimmingValue);
306 uint32_t HAL_DACEx_GetTrimOffset(DAC_HandleTypeDef *hdac, uint32_t Channel);
307 
308 /**
309   * @}
310   */
311 
312 /**
313   * @}
314   */
315 
316 /** @addtogroup DACEx_Private_Functions
317   * @{
318   */
319 
320 /* DAC_DMAConvCpltCh2 / DAC_DMAErrorCh2 / DAC_DMAHalfConvCpltCh2 */
321 /* are called by HAL_DAC_Start_DMA */
322 void DAC_DMAConvCpltCh2(DMA_HandleTypeDef *hdma);
323 void DAC_DMAErrorCh2(DMA_HandleTypeDef *hdma);
324 void DAC_DMAHalfConvCpltCh2(DMA_HandleTypeDef *hdma);
325 
326 /**
327   * @}
328   */
329 
330 /**
331   * @}
332   */
333 
334 #endif /* DAC1 || DAC2 || DAC3 || DAC4 */
335 
336 /**
337   * @}
338   */
339 
340 #ifdef __cplusplus
341 }
342 #endif
343 
344 #endif /* STM32G4xx_HAL_DAC_EX_H */
345 
346