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