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