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