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   (                                                                            \
51       ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)                                  \
52    || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2)                                  \
53   )
54 #else
55 #define IS_LL_DAC_CHANNEL(__DACX__, __DAC_CHANNEL__)                           \
56   (                                                                            \
57    ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)                                     \
58   )
59 #endif /* DAC_CHANNEL2_SUPPORT */
60 
61 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
62   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
63    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
64    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
65    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO)                      \
66    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
67    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
68    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO)                      \
69    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
70   )
71 
72 #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__)              \
73   (   ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE)        \
74       || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)    \
75       || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
76   )
77 
78 #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_MODE__, __WAVE_AUTO_GENERATION_CONFIG__)  \
79   ( (((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)                               \
80      && (  ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0)                           \
81            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0)                     \
82            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0)                     \
83            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0)                     \
84            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0)                     \
85            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0)                     \
86            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0)                     \
87            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0)                     \
88            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0)                     \
89            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0)                     \
90            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0)                    \
91            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0))                   \
92     )                                                                                                     \
93     ||(((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE)                          \
94        && (  ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1)                           \
95              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3)                        \
96              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7)                        \
97              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15)                       \
98              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31)                       \
99              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63)                       \
100              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127)                      \
101              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255)                      \
102              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511)                      \
103              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023)                     \
104              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047)                     \
105              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095))                    \
106       )                                                                                                   \
107   )
108 
109 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__)                             \
110   (   ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE)                     \
111       || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE)                 \
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_DAC1);
145 
146   /* Release reset of DAC clock */
147   LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
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   *         @arg @ref LL_DAC_CHANNEL_2 (1)
172   *
173   *         (1) On this STM32 serie, parameter not available on all devices.
174   *             Refer to device datasheet for channels availability.
175   * @param  DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
176   * @retval An ErrorStatus enumeration value:
177   *          - SUCCESS: DAC registers are initialized
178   *          - ERROR: DAC registers are not initialized
179   */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)180 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
181 {
182   ErrorStatus status = SUCCESS;
183 
184   /* Check the parameters */
185   assert_param(IS_DAC_ALL_INSTANCE(DACx));
186   assert_param(IS_LL_DAC_CHANNEL(DACx, DAC_Channel));
187   assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
188   assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
189   assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
190   if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
191   {
192     assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGeneration,
193                                                   DAC_InitStruct->WaveAutoGenerationConfig));
194   }
195 
196   /* Note: Hardware constraint (refer to description of this function)        */
197   /*       DAC instance must be disabled.                                     */
198   if (LL_DAC_IsEnabled(DACx, DAC_Channel) == 0UL)
199   {
200     /* Configuration of DAC channel:                                          */
201     /*  - TriggerSource                                                       */
202     /*  - WaveAutoGeneration                                                  */
203     /*  - OutputBuffer                                                        */
204     /*  - OutputMode                                                          */
205     if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
206     {
207       MODIFY_REG(DACx->CR,
208                  (DAC_CR_TSEL1
209                   | DAC_CR_WAVE1
210                   | DAC_CR_MAMP1
211                   | DAC_CR_BOFF1
212                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
213                  ,
214                  (DAC_InitStruct->TriggerSource
215                   | DAC_InitStruct->WaveAutoGeneration
216                   | DAC_InitStruct->WaveAutoGenerationConfig
217                   | DAC_InitStruct->OutputBuffer
218                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
219                 );
220     }
221     else
222     {
223       MODIFY_REG(DACx->CR,
224                  (DAC_CR_TSEL1
225                   | DAC_CR_WAVE1
226                   | DAC_CR_BOFF1
227                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
228                  ,
229                  (DAC_InitStruct->TriggerSource
230                   | LL_DAC_WAVE_AUTO_GENERATION_NONE
231                   | DAC_InitStruct->OutputBuffer
232                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
233                 );
234     }
235   }
236   else
237   {
238     /* Initialization error: DAC instance is not disabled.                    */
239     status = ERROR;
240   }
241   return status;
242 }
243 
244 /**
245   * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
246   * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
247   *                       whose fields will be set to default values.
248   * @retval None
249   */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)250 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
251 {
252   /* Set DAC_InitStruct fields to default values */
253   DAC_InitStruct->TriggerSource            = LL_DAC_TRIG_SOFTWARE;
254   DAC_InitStruct->WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NONE;
255   /* Note: Parameter discarded if wave auto generation is disabled,           */
256   /*       set anyway to its default value.                                   */
257   DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
258   DAC_InitStruct->OutputBuffer             = LL_DAC_OUTPUT_BUFFER_ENABLE;
259 }
260 
261 /**
262   * @}
263   */
264 
265 /**
266   * @}
267   */
268 
269 /**
270   * @}
271   */
272 
273 #endif /* DAC */
274 
275 /**
276   * @}
277   */
278 
279 #endif /* USE_FULL_LL_DRIVER */
280 
281