1 /**
2   ******************************************************************************
3   * @file    stm32f0xx_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 #if defined(USE_FULL_LL_DRIVER)
19 
20 /* Includes ------------------------------------------------------------------*/
21 #include "stm32f0xx_ll_dac.h"
22 #include "stm32f0xx_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 STM32F0xx_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(__DACX__, __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(__DACX__, __DAC_CHANNEL__)                           \
57   (                                                                            \
58    ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)                                     \
59   )
60 #endif /* DAC_CHANNEL2_SUPPORT */
61 
62 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
63   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
64    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
65    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
66    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
67    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
68    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
69    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM15_TRGO)                     \
70    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
71   )
72 
73 #if defined(DAC_CR_WAVE1)
74 #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__)           \
75   (   ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE)     \
76    || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)    \
77    || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
78   )
79 
80 #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_CONFIG__)      \
81   (   ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0)     \
82    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0)  \
83    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0)  \
84    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0)  \
85    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0)  \
86    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0)  \
87    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0)  \
88    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0)  \
89    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0)  \
90    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0)  \
91    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0) \
92    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0) \
93    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1)       \
94    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3)       \
95    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7)       \
96    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15)      \
97    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31)      \
98    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63)      \
99    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127)     \
100    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255)     \
101    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511)     \
102    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023)    \
103    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047)    \
104    || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095)    \
105   )
106 #endif
107 
108 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__)                             \
109   (   ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE)                     \
110    || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE)                    \
111   )
112 
113 /**
114   * @}
115   */
116 
117 
118 /* Private function prototypes -----------------------------------------------*/
119 
120 /* Exported functions --------------------------------------------------------*/
121 /** @addtogroup DAC_LL_Exported_Functions
122   * @{
123   */
124 
125 /** @addtogroup DAC_LL_EF_Init
126   * @{
127   */
128 
129 /**
130   * @brief  De-initialize registers of the selected DAC instance
131   *         to their default reset values.
132   * @param  DACx DAC instance
133   * @retval An ErrorStatus enumeration value:
134   *          - SUCCESS: DAC registers are de-initialized
135   *          - ERROR: not applicable
136   */
LL_DAC_DeInit(DAC_TypeDef * DACx)137 ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
138 {
139   /* Check the parameters */
140   assert_param(IS_DAC_ALL_INSTANCE(DACx));
141 
142   /* Force reset of DAC clock */
143   LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
144 
145   /* Release reset of DAC clock */
146   LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
147 
148   return SUCCESS;
149 }
150 
151 /**
152   * @brief  Initialize some features of DAC instance.
153   * @note   The setting of these parameters by function @ref LL_DAC_Init()
154   *         is conditioned to DAC state:
155   *         DAC instance must be disabled.
156   * @param  DACx DAC instance
157   * @param  DAC_Channel This parameter can be one of the following values:
158   *         @arg @ref LL_DAC_CHANNEL_1
159   *         @arg @ref LL_DAC_CHANNEL_2 (1)
160   *
161   *         (1) On this STM32 series, parameter not available on all devices.
162   *             Refer to device datasheet for channels availability.
163   * @param  DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
164   * @retval An ErrorStatus enumeration value:
165   *          - SUCCESS: DAC registers are initialized
166   *          - ERROR: DAC registers are not initialized
167   */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)168 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
169 {
170   ErrorStatus status = SUCCESS;
171 
172   /* Check the parameters */
173   assert_param(IS_DAC_ALL_INSTANCE(DACx));
174   assert_param(IS_LL_DAC_CHANNEL(DACx, DAC_Channel));
175   assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
176   assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
177 #if defined(DAC_CR_WAVE1)
178   assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
179   if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
180   {
181     assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGenerationConfig));
182   }
183 #endif
184 
185   /* Note: Hardware constraint (refer to description of this function)        */
186   /*       DAC instance must be disabled.                                     */
187   if(LL_DAC_IsEnabled(DACx, DAC_Channel) == 0U)
188   {
189     /* Configuration of DAC channel:                                          */
190     /*  - TriggerSource                                                       */
191 #if defined(DAC_CR_WAVE1)
192     /*  - WaveAutoGeneration                                                  */
193 #endif
194     /*  - OutputBuffer                                                        */
195 #if defined(DAC_CR_WAVE1)
196     if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
197     {
198       MODIFY_REG(DACx->CR,
199                  (  DAC_CR_TSEL1
200                   | DAC_CR_WAVE1
201                   | DAC_CR_MAMP1
202                   | DAC_CR_BOFF1
203                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
204                 ,
205                  (  DAC_InitStruct->TriggerSource
206                   | DAC_InitStruct->WaveAutoGeneration
207                   | DAC_InitStruct->WaveAutoGenerationConfig
208                   | DAC_InitStruct->OutputBuffer
209                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
210                 );
211     }
212     else
213     {
214       MODIFY_REG(DACx->CR,
215                  (  DAC_CR_TSEL1
216                   | DAC_CR_WAVE1
217                   | DAC_CR_BOFF1
218                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
219                 ,
220                  (  DAC_InitStruct->TriggerSource
221                   | LL_DAC_WAVE_AUTO_GENERATION_NONE
222                   | DAC_InitStruct->OutputBuffer
223                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
224                 );
225     }
226 #endif
227   }
228   else
229   {
230     /* Initialization error: DAC instance is not disabled.                    */
231     status = ERROR;
232   }
233   return status;
234 }
235 
236 /**
237   * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
238   * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
239   *                       whose fields will be set to default values.
240   * @retval None
241   */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)242 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
243 {
244   /* Set DAC_InitStruct fields to default values */
245   DAC_InitStruct->TriggerSource            = LL_DAC_TRIG_SOFTWARE;
246 #if defined(DAC_CR_WAVE1)
247   DAC_InitStruct->WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NONE;
248   /* Note: Parameter discarded if wave auto generation is disabled,           */
249   /*       set anyway to its default value.                                   */
250   DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
251 #endif
252   DAC_InitStruct->OutputBuffer             = LL_DAC_OUTPUT_BUFFER_ENABLE;
253 }
254 
255 /**
256   * @}
257   */
258 
259 /**
260   * @}
261   */
262 
263 /**
264   * @}
265   */
266 
267 #endif /* DAC1 */
268 
269 /**
270   * @}
271   */
272 
273 #endif /* USE_FULL_LL_DRIVER */
274 
275