1 /**
2 ******************************************************************************
3 * @file stm32h7xx_ll_dac.c
4 * @author MCD Application Team
5 * @brief DAC LL module driver
6 ******************************************************************************
7 * @attention
8 *
9 * Copyright (c) 2017 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 #if defined(USE_FULL_LL_DRIVER)
19
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32h7xx_ll_dac.h"
22 #include "stm32h7xx_ll_bus.h"
23
24 #ifdef USE_FULL_ASSERT
25 #include "stm32_assert.h"
26 #else
27 #define assert_param(expr) ((void)0U)
28 #endif /* USE_FULL_ASSERT */
29
30 /** @addtogroup STM32H7xx_LL_Driver
31 * @{
32 */
33
34 #if defined(DAC1) || defined(DAC2)
35
36 /** @addtogroup DAC_LL DAC
37 * @{
38 */
39
40 /* Private types -------------------------------------------------------------*/
41 /* Private variables ---------------------------------------------------------*/
42 /* Private constants ---------------------------------------------------------*/
43 /* Private macros ------------------------------------------------------------*/
44
45 /** @addtogroup DAC_LL_Private_Macros
46 * @{
47 */
48 #define IS_LL_DAC_CHANNEL(__DAC_CHANNEL__) \
49 ( ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1) \
50 || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2) \
51 )
52
53 #if defined (HRTIM1)
54 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
55 ( ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
56 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM1_TRGO) \
57 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
58 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
59 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
60 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
61 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
62 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
63 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO) \
64 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_HRTIM_TRGO1) \
65 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_HRTIM_TRGO2) \
66 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM1_OUT) \
67 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM2_OUT) \
68 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
69 )
70 #elif defined (DAC2)
71 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
72 ( ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
73 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM1_TRGO) \
74 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
75 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
76 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
77 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
78 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
79 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
80 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO) \
81 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM1_OUT) \
82 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM2_OUT) \
83 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
84 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM3_OUT) \
85 )
86 #else
87 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__) \
88 ( ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE) \
89 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM1_TRGO) \
90 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO) \
91 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO) \
92 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO) \
93 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO) \
94 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO) \
95 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO) \
96 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO) \
97 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM1_OUT) \
98 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_LPTIM2_OUT) \
99 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9) \
100 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM23_TRGO) \
101 || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM24_TRGO) \
102 )
103 #endif
104
105 #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__) \
106 ( ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE) \
107 || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE) \
108 || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
109 )
110
111 #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_MODE__, __WAVE_AUTO_GENERATION_CONFIG__) \
112 ( (((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE) \
113 && ( ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0) \
114 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0) \
115 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0) \
116 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0) \
117 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0) \
118 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0) \
119 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0) \
120 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0) \
121 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0) \
122 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0) \
123 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0) \
124 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0)) \
125 ) \
126 ||(((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
127 && ( ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1) \
128 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3) \
129 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7) \
130 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15) \
131 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31) \
132 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63) \
133 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127) \
134 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255) \
135 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511) \
136 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023) \
137 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047) \
138 || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095)) \
139 ) \
140 )
141
142 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__) \
143 ( ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE) \
144 || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE) \
145 )
146
147 #define IS_LL_DAC_OUTPUT_CONNECTION(__OUTPUT_CONNECTION__) \
148 ( ((__OUTPUT_CONNECTION__) == LL_DAC_OUTPUT_CONNECT_GPIO) \
149 || ((__OUTPUT_CONNECTION__) == LL_DAC_OUTPUT_CONNECT_INTERNAL) \
150 )
151
152 #define IS_LL_DAC_OUTPUT_MODE(__OUTPUT_MODE__) \
153 ( ((__OUTPUT_MODE__) == LL_DAC_OUTPUT_MODE_NORMAL) \
154 || ((__OUTPUT_MODE__) == LL_DAC_OUTPUT_MODE_SAMPLE_AND_HOLD) \
155 )
156
157 /**
158 * @}
159 */
160
161
162 /* Private function prototypes -----------------------------------------------*/
163
164 /* Exported functions --------------------------------------------------------*/
165 /** @addtogroup DAC_LL_Exported_Functions
166 * @{
167 */
168
169 /** @addtogroup DAC_LL_EF_Init
170 * @{
171 */
172
173 /**
174 * @brief De-initialize registers of the selected DAC instance
175 * to their default reset values.
176 * @param DACx DAC instance
177 * @retval An ErrorStatus enumeration value:
178 * - SUCCESS: DAC registers are de-initialized
179 * - ERROR: not applicable
180 */
LL_DAC_DeInit(DAC_TypeDef * DACx)181 ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
182 {
183 /* Check the parameters */
184 assert_param(IS_DAC_ALL_INSTANCE(DACx));
185
186 if(DACx == DAC1)
187 {
188 /* Force reset of DAC clock */
189 LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC12);
190
191 /* Release reset of DAC clock */
192 LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC12);
193 }
194 #if defined (DAC2)
195 else
196 {
197 /* Force reset of DAC clock */
198 LL_APB4_GRP1_ForceReset(LL_APB4_GRP1_PERIPH_DAC2);
199
200 /* Release reset of DAC clock */
201 LL_APB4_GRP1_ReleaseReset(LL_APB4_GRP1_PERIPH_DAC2);
202 }
203 #endif
204
205
206 return SUCCESS;
207 }
208
209 /**
210 * @brief Initialize some features of DAC channel.
211 * @note @ref LL_DAC_Init() aims to ease basic configuration of a DAC channel.
212 * Leaving it ready to be enabled and output:
213 * a level by calling one of
214 * @ref LL_DAC_ConvertData12RightAligned
215 * @ref LL_DAC_ConvertData12LeftAligned
216 * @ref LL_DAC_ConvertData8RightAligned
217 * or one of the supported autogenerated wave.
218 * @note This function allows configuration of:
219 * - Output mode
220 * - Trigger
221 * - Wave generation
222 * @note The setting of these parameters by function @ref LL_DAC_Init()
223 * is conditioned to DAC state:
224 * DAC channel must be disabled.
225 * @param DACx DAC instance
226 * @param DAC_Channel This parameter can be one of the following values:
227 * @arg @ref LL_DAC_CHANNEL_1
228 * @arg @ref LL_DAC_CHANNEL_2
229 * @param DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
230 * @retval An ErrorStatus enumeration value:
231 * - SUCCESS: DAC registers are initialized
232 * - ERROR: DAC registers are not initialized
233 */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)234 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
235 {
236 ErrorStatus status = SUCCESS;
237
238 /* Check the parameters */
239 assert_param(IS_DAC_ALL_INSTANCE(DACx));
240 assert_param(IS_LL_DAC_CHANNEL(DAC_Channel));
241 assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
242 assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
243 assert_param(IS_LL_DAC_OUTPUT_CONNECTION(DAC_InitStruct->OutputConnection));
244 assert_param(IS_LL_DAC_OUTPUT_MODE(DAC_InitStruct->OutputMode));
245 assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
246 if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
247 {
248 assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGeneration,
249 DAC_InitStruct->WaveAutoGenerationConfig));
250 }
251
252 /* Note: Hardware constraint (refer to description of this function) */
253 /* DAC instance must be disabled. */
254 if (LL_DAC_IsEnabled(DACx, DAC_Channel) == 0UL)
255 {
256 /* Configuration of DAC channel: */
257 /* - TriggerSource */
258 /* - WaveAutoGeneration */
259 /* - OutputBuffer */
260 /* - OutputConnection */
261 /* - OutputMode */
262 if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
263 {
264 MODIFY_REG(DACx->CR,
265 (DAC_CR_TSEL1
266 | DAC_CR_WAVE1
267 | DAC_CR_MAMP1
268 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
269 ,
270 (DAC_InitStruct->TriggerSource
271 | DAC_InitStruct->WaveAutoGeneration
272 | DAC_InitStruct->WaveAutoGenerationConfig
273 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
274 );
275 }
276 else
277 {
278 MODIFY_REG(DACx->CR,
279 (DAC_CR_TSEL1
280 | DAC_CR_WAVE1
281 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
282 ,
283 (DAC_InitStruct->TriggerSource
284 | LL_DAC_WAVE_AUTO_GENERATION_NONE
285 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
286 );
287 }
288 MODIFY_REG(DACx->MCR,
289 (DAC_MCR_MODE1_1
290 | DAC_MCR_MODE1_0
291 | DAC_MCR_MODE1_2
292 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
293 ,
294 (DAC_InitStruct->OutputBuffer
295 | DAC_InitStruct->OutputConnection
296 | DAC_InitStruct->OutputMode
297 ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
298 );
299 }
300 else
301 {
302 /* Initialization error: DAC instance is not disabled. */
303 status = ERROR;
304 }
305 return status;
306 }
307
308 /**
309 * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
310 * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
311 * whose fields will be set to default values.
312 * @retval None
313 */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)314 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
315 {
316 /* Set DAC_InitStruct fields to default values */
317 DAC_InitStruct->TriggerSource = LL_DAC_TRIG_SOFTWARE;
318 DAC_InitStruct->WaveAutoGeneration = LL_DAC_WAVE_AUTO_GENERATION_NONE;
319 /* Note: Parameter discarded if wave auto generation is disabled, */
320 /* set anyway to its default value. */
321 DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
322 DAC_InitStruct->OutputBuffer = LL_DAC_OUTPUT_BUFFER_ENABLE;
323 DAC_InitStruct->OutputConnection = LL_DAC_OUTPUT_CONNECT_GPIO;
324 DAC_InitStruct->OutputMode = LL_DAC_OUTPUT_MODE_NORMAL;
325 }
326
327 /**
328 * @}
329 */
330
331 /**
332 * @}
333 */
334
335 /**
336 * @}
337 */
338
339 #endif /* DAC1 || DAC2 */
340
341 /**
342 * @}
343 */
344
345 #endif /* USE_FULL_LL_DRIVER */
346
347