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