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