1 /**
2   ******************************************************************************
3   * @file    stm32l0xx_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 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32l0xx_ll_dac.h"
23 #include "stm32l0xx_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 STM32L0xx_LL_Driver
32   * @{
33   */
34 
35 #if defined (DAC1)
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 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
64   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
65    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
66    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_TRGO)                      \
67    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM3_CH3)                       \
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_TIM21_TRGO)                     \
71    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
72   )
73 
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 
107 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__)                             \
108   (   ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE)                     \
109    || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE)                    \
110   )
111 
112 /**
113   * @}
114   */
115 
116 
117 /* Private function prototypes -----------------------------------------------*/
118 
119 /* Exported functions --------------------------------------------------------*/
120 /** @addtogroup DAC_LL_Exported_Functions
121   * @{
122   */
123 
124 /** @addtogroup DAC_LL_EF_Init
125   * @{
126   */
127 
128 /**
129   * @brief  De-initialize registers of the selected DAC instance
130   *         to their default reset values.
131   * @param  DACx DAC instance
132   * @retval An ErrorStatus enumeration value:
133   *          - SUCCESS: DAC registers are de-initialized
134   *          - ERROR: not applicable
135   */
LL_DAC_DeInit(DAC_TypeDef * DACx)136 ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
137 {
138   /* Check the parameters */
139   assert_param(IS_DAC_ALL_INSTANCE(DACx));
140 
141   /* Force reset of DAC clock */
142   LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
143 
144   /* Release reset of DAC clock */
145   LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
146 
147   return SUCCESS;
148 }
149 
150 /**
151   * @brief  Initialize some features of DAC instance.
152   * @note   The setting of these parameters by function @ref LL_DAC_Init()
153   *         is conditioned to DAC state:
154   *         DAC instance must be disabled.
155   * @param  DACx DAC instance
156   * @param  DAC_Channel This parameter can be one of the following values:
157   *         @arg @ref LL_DAC_CHANNEL_1
158   *         @arg @ref LL_DAC_CHANNEL_2 (1)
159   *
160   *         (1) On this STM32 serie, parameter not available on all devices.
161   *             Refer to device datasheet for channels availability.
162   * @param  DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
163   * @retval An ErrorStatus enumeration value:
164   *          - SUCCESS: DAC registers are initialized
165   *          - ERROR: DAC registers are not initialized
166   */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)167 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
168 {
169   ErrorStatus status = SUCCESS;
170 
171   /* Check the parameters */
172   assert_param(IS_DAC_ALL_INSTANCE(DACx));
173   assert_param(IS_LL_DAC_CHANNEL(DACx, DAC_Channel));
174   assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
175   assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
176   assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
177   if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
178   {
179     assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGenerationConfig));
180   }
181 
182   /* Note: Hardware constraint (refer to description of this function)        */
183   /*       DAC instance must be disabled.                                     */
184   if(LL_DAC_IsEnabled(DACx, DAC_Channel) == 0U)
185   {
186     /* Configuration of DAC channel:                                          */
187     /*  - TriggerSource                                                       */
188     /*  - WaveAutoGeneration                                                  */
189     /*  - OutputBuffer                                                        */
190     if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
191     {
192       MODIFY_REG(DACx->CR,
193                  (  DAC_CR_TSEL1
194                   | DAC_CR_WAVE1
195                   | DAC_CR_MAMP1
196                   | DAC_CR_BOFF1
197                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
198                 ,
199                  (  DAC_InitStruct->TriggerSource
200                   | DAC_InitStruct->WaveAutoGeneration
201                   | DAC_InitStruct->WaveAutoGenerationConfig
202                   | DAC_InitStruct->OutputBuffer
203                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
204                 );
205     }
206     else
207     {
208       MODIFY_REG(DACx->CR,
209                  (  DAC_CR_TSEL1
210                   | DAC_CR_WAVE1
211                   | DAC_CR_BOFF1
212                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
213                 ,
214                  (  DAC_InitStruct->TriggerSource
215                   | LL_DAC_WAVE_AUTO_GENERATION_NONE
216                   | DAC_InitStruct->OutputBuffer
217                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
218                 );
219     }
220   }
221   else
222   {
223     /* Initialization error: DAC instance is not disabled.                    */
224     status = ERROR;
225   }
226   return status;
227 }
228 
229 /**
230   * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
231   * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
232   *                       whose fields will be set to default values.
233   * @retval None
234   */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)235 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
236 {
237   /* Set DAC_InitStruct fields to default values */
238   DAC_InitStruct->TriggerSource            = LL_DAC_TRIG_SOFTWARE;
239   DAC_InitStruct->WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NONE;
240   /* Note: Parameter discarded if wave auto generation is disabled,           */
241   /*       set anyway to its default value.                                   */
242   DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
243   DAC_InitStruct->OutputBuffer             = LL_DAC_OUTPUT_BUFFER_ENABLE;
244 }
245 
246 /**
247   * @}
248   */
249 
250 /**
251   * @}
252   */
253 
254 /**
255   * @}
256   */
257 
258 #endif /* DAC1 */
259 
260 /**
261   * @}
262   */
263 
264 #endif /* USE_FULL_LL_DRIVER */
265 
266 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
267