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