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