1 /**
2   ******************************************************************************
3   * @file    stm32f3xx_ll_dac.c
4   * @author  MCD Application Team
5   * @brief   DAC LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 
20 #if defined(USE_FULL_LL_DRIVER)
21 
22 /* Includes ------------------------------------------------------------------*/
23 #include "stm32f3xx_ll_dac.h"
24 #include "stm32f3xx_ll_bus.h"
25 
26 #ifdef USE_FULL_ASSERT
27   #include "stm32_assert.h"
28 #else
29   #define assert_param(expr) ((void)0U)
30 #endif
31 
32 /** @addtogroup STM32F3xx_LL_Driver
33   * @{
34   */
35 
36 #if defined (DAC1) || defined (DAC2)
37 
38 /** @addtogroup DAC_LL DAC
39   * @{
40   */
41 
42 /* Private types -------------------------------------------------------------*/
43 /* Private variables ---------------------------------------------------------*/
44 /* Private constants ---------------------------------------------------------*/
45 /* Private macros ------------------------------------------------------------*/
46 
47 /** @addtogroup DAC_LL_Private_Macros
48   * @{
49   */
50 
51 #if defined(DAC_CHANNEL2_SUPPORT)
52 #define IS_LL_DAC_CHANNEL(__DACX__, __DAC_CHANNEL__)                           \
53   (                                                                            \
54       ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)                                  \
55    || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2)                                  \
56   )
57 #else
58 #define IS_LL_DAC_CHANNEL(__DACX__, __DAC_CHANNEL__)                           \
59   (                                                                            \
60    ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)                                     \
61   )
62 #endif /* DAC_CHANNEL2_SUPPORT */
63 
64 #if defined(STM32F303xE) || defined(STM32F398xx) || defined(STM32F303xC) || defined(STM32F358xx)
65 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
66   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
67    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
68    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
69    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
70    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
71    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
72    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
73    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
74   )
75 
76 #elif defined(STM32F303x8) || defined(STM32F328xx)
77 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
78   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
79    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
80    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
81    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
82    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
83    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
84    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
85   )
86 
87 #elif defined(STM32F302xE) || defined(STM32F302xC) || defined(STM32F302x8)
88 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
89   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
90    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
91    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
92    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
93    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
94    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
95    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
96   )
97 
98 #elif defined(STM32F301x8) || defined(STM32F318xx)
99 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
100   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
101    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
102    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
103    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
104    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
105   )
106 
107 #elif defined(STM32F373xC) || defined(STM32F378xx)
108 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
109   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
110    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
111    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
112    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
113    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO)                      \
114    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
115    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
116    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
117   )
118 
119 #elif defined(STM32F334x8)
120 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
121   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
122    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
123    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
124    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
125    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
126    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
127    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIGGER_HRTIM1_DACTRG2)                  \
128    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIGGER_HRTIM1_DACTRG3)                  \
129    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
130   )
131 #endif
132 
133 #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__)           \
134   (   ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE)     \
135    || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)    \
136    || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
137   )
138 
139 #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_CONFIG__)      \
140   (   ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0)     \
141    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0)  \
142    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0)  \
143    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0)  \
144    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0)  \
145    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0)  \
146    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0)  \
147    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0)  \
148    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0)  \
149    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0)  \
150    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0) \
151    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0) \
152    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1)       \
153    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3)       \
154    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7)       \
155    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15)      \
156    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31)      \
157    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63)      \
158    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127)     \
159    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255)     \
160    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511)     \
161    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023)    \
162    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047)    \
163    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095)    \
164   )
165 
166 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__)                             \
167   (   ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE)                     \
168    || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE)                    \
169   )
170 
171 /**
172   * @}
173   */
174 
175 
176 /* Private function prototypes -----------------------------------------------*/
177 
178 /* Exported functions --------------------------------------------------------*/
179 /** @addtogroup DAC_LL_Exported_Functions
180   * @{
181   */
182 
183 /** @addtogroup DAC_LL_EF_Init
184   * @{
185   */
186 
187 /**
188   * @brief  De-initialize registers of the selected DAC instance
189   *         to their default reset values.
190   * @param  DACx DAC instance
191   * @retval An ErrorStatus enumeration value:
192   *          - SUCCESS: DAC registers are de-initialized
193   *          - ERROR: not applicable
194   */
LL_DAC_DeInit(DAC_TypeDef * DACx)195 ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
196 {
197   /* Check the parameters */
198   assert_param(IS_DAC_ALL_INSTANCE(DACx));
199 
200   if(DACx == DAC1)
201   {
202     /* Force reset of DAC clock */
203     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
204 
205     /* Release reset of DAC clock */
206     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
207   }
208 #if defined(DAC2)
209   else
210   {
211     /* Force reset of DAC clock */
212     LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC2);
213 
214     /* Release reset of DAC clock */
215     LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC2);
216   }
217 #endif
218   return SUCCESS;
219 }
220 
221 /**
222   * @brief  Initialize some features of DAC instance.
223   * @note   The setting of these parameters by function @ref LL_DAC_Init()
224   *         is conditioned to DAC state:
225   *         DAC instance must be disabled.
226   * @param  DACx DAC instance
227   * @param  DAC_Channel This parameter can be one of the following values:
228   *         @arg @ref LL_DAC_CHANNEL_1
229   *         @arg @ref LL_DAC_CHANNEL_2 (1)
230   *
231   *         (1) On this STM32 serie, parameter not available on all devices.
232   *             Refer to device datasheet for channels availability.
233   * @param  DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
234   * @retval An ErrorStatus enumeration value:
235   *          - SUCCESS: DAC registers are initialized
236   *          - ERROR: DAC registers are not initialized
237   */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)238 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
239 {
240   ErrorStatus status = SUCCESS;
241 
242   /* Check the parameters */
243   assert_param(IS_DAC_ALL_INSTANCE(DACx));
244   assert_param(IS_LL_DAC_CHANNEL(DACx, DAC_Channel));
245   assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
246   assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
247   assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
248   if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
249   {
250     assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGenerationConfig));
251   }
252 
253   /* Note: Hardware constraint (refer to description of this function)        */
254   /*       DAC instance must be disabled.                                     */
255   if(LL_DAC_IsEnabled(DACx, DAC_Channel) == 0U)
256   {
257     /* Configuration of DAC channel:                                          */
258     /*  - TriggerSource                                                       */
259     /*  - WaveAutoGeneration                                                  */
260     /*  - OutputBuffer                                                        */
261     if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
262     {
263       MODIFY_REG(DACx->CR,
264                  (  DAC_CR_TSEL1
265                   | DAC_CR_WAVE1
266                   | DAC_CR_MAMP1
267                   | DAC_CR_BOFF1
268                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
269                 ,
270                  (  DAC_InitStruct->TriggerSource
271                   | DAC_InitStruct->WaveAutoGeneration
272                   | DAC_InitStruct->WaveAutoGenerationConfig
273                   | DAC_InitStruct->OutputBuffer
274                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
275                 );
276     }
277     else
278     {
279       MODIFY_REG(DACx->CR,
280                  (  DAC_CR_TSEL1
281                   | DAC_CR_WAVE1
282                   | DAC_CR_BOFF1
283                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
284                 ,
285                  (  DAC_InitStruct->TriggerSource
286                   | LL_DAC_WAVE_AUTO_GENERATION_NONE
287                   | DAC_InitStruct->OutputBuffer
288                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
289                 );
290     }
291   }
292   else
293   {
294     /* Initialization error: DAC instance is not disabled.                    */
295     status = ERROR;
296   }
297   return status;
298 }
299 
300 /**
301   * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
302   * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
303   *                       whose fields will be set to default values.
304   * @retval None
305   */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)306 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
307 {
308   /* Set DAC_InitStruct fields to default values */
309   DAC_InitStruct->TriggerSource            = LL_DAC_TRIG_SOFTWARE;
310   DAC_InitStruct->WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NONE;
311   /* Note: Parameter discarded if wave auto generation is disabled,           */
312   /*       set anyway to its default value.                                   */
313   DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
314   DAC_InitStruct->OutputBuffer             = LL_DAC_OUTPUT_BUFFER_ENABLE;
315 }
316 
317 /**
318   * @}
319   */
320 
321 /**
322   * @}
323   */
324 
325 /**
326   * @}
327   */
328 
329 #endif /* DAC1 || DAC2 */
330 
331 /**
332   * @}
333   */
334 
335 #endif /* USE_FULL_LL_DRIVER */
336 
337 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
338