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