1 /**
2   ******************************************************************************
3   * @file    stm32f2xx_ll_dac.c
4   * @author  MCD Application Team
5   * @brief   DAC LL module driver
6   ******************************************************************************
7   * @attention
8   *
9   * <h2><center>&copy; Copyright (c) 2016 STMicroelectronics.
10   * All rights reserved.</center></h2>
11   *
12   * This software component is licensed by ST under BSD 3-Clause license,
13   * the "License"; You may not use this file except in compliance with the
14   * License. You may obtain a copy of the License at:
15   *                        opensource.org/licenses/BSD-3-Clause
16   *
17   ******************************************************************************
18   */
19 #if defined(USE_FULL_LL_DRIVER)
20 
21 /* Includes ------------------------------------------------------------------*/
22 #include "stm32f2xx_ll_dac.h"
23 #include "stm32f2xx_ll_bus.h"
24 
25 #ifdef USE_FULL_ASSERT
26 #include "stm32_assert.h"
27 #else
28 #define assert_param(expr) ((void)0U)
29 #endif /* USE_FULL_ASSERT */
30 
31 /** @addtogroup STM32F2xx_LL_Driver
32   * @{
33   */
34 
35 #if defined(DAC)
36 
37 /** @addtogroup DAC_LL DAC
38   * @{
39   */
40 
41 /* Private types -------------------------------------------------------------*/
42 /* Private variables ---------------------------------------------------------*/
43 /* Private constants ---------------------------------------------------------*/
44 /* Private macros ------------------------------------------------------------*/
45 
46 /** @addtogroup DAC_LL_Private_Macros
47   * @{
48   */
49 #define IS_LL_DAC_CHANNEL(__DAC_CHANNEL__)        \
50   (  ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_1)      \
51      || ((__DAC_CHANNEL__) == LL_DAC_CHANNEL_2)   \
52   )
53 
54 #define IS_LL_DAC_TRIGGER_SOURCE(__TRIGGER_SOURCE__)                           \
55   (   ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_SOFTWARE)                           \
56    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM2_TRGO)                      \
57    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM4_TRGO)                      \
58    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM5_TRGO)                      \
59    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM6_TRGO)                      \
60    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM7_TRGO)                      \
61    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_TIM8_TRGO)                      \
62    || ((__TRIGGER_SOURCE__) == LL_DAC_TRIG_EXT_EXTI_LINE9)                     \
63   )
64 
65 #define IS_LL_DAC_WAVE_AUTO_GENER_MODE(__WAVE_AUTO_GENERATION_MODE__)              \
66   (   ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NONE)        \
67       || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)    \
68       || ((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE) \
69   )
70 
71 #define IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(__WAVE_AUTO_GENERATION_MODE__, __WAVE_AUTO_GENERATION_CONFIG__)  \
72   ( (((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_NOISE)                               \
73      && (  ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BIT0)                           \
74            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS1_0)                     \
75            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS2_0)                     \
76            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS3_0)                     \
77            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS4_0)                     \
78            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS5_0)                     \
79            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS6_0)                     \
80            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS7_0)                     \
81            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS8_0)                     \
82            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS9_0)                     \
83            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS10_0)                    \
84            || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_NOISE_LFSR_UNMASK_BITS11_0))                   \
85     )                                                                                                     \
86     ||(((__WAVE_AUTO_GENERATION_MODE__) == LL_DAC_WAVE_AUTO_GENERATION_TRIANGLE)                          \
87        && (  ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1)                           \
88              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_3)                        \
89              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_7)                        \
90              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_15)                       \
91              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_31)                       \
92              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_63)                       \
93              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_127)                      \
94              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_255)                      \
95              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_511)                      \
96              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_1023)                     \
97              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_2047)                     \
98              || ((__WAVE_AUTO_GENERATION_CONFIG__) == LL_DAC_TRIANGLE_AMPLITUDE_4095))                    \
99       )                                                                                                   \
100   )
101 
102 #define IS_LL_DAC_OUTPUT_BUFFER(__OUTPUT_BUFFER__)                             \
103   (   ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_ENABLE)                     \
104       || ((__OUTPUT_BUFFER__) == LL_DAC_OUTPUT_BUFFER_DISABLE)                 \
105   )
106 
107 /**
108   * @}
109   */
110 
111 
112 /* Private function prototypes -----------------------------------------------*/
113 
114 /* Exported functions --------------------------------------------------------*/
115 /** @addtogroup DAC_LL_Exported_Functions
116   * @{
117   */
118 
119 /** @addtogroup DAC_LL_EF_Init
120   * @{
121   */
122 
123 /**
124   * @brief  De-initialize registers of the selected DAC instance
125   *         to their default reset values.
126   * @param  DACx DAC instance
127   * @retval An ErrorStatus enumeration value:
128   *          - SUCCESS: DAC registers are de-initialized
129   *          - ERROR: not applicable
130   */
LL_DAC_DeInit(DAC_TypeDef * DACx)131 ErrorStatus LL_DAC_DeInit(DAC_TypeDef *DACx)
132 {
133   /* Check the parameters */
134   assert_param(IS_DAC_ALL_INSTANCE(DACx));
135 
136   /* Force reset of DAC clock */
137   LL_APB1_GRP1_ForceReset(LL_APB1_GRP1_PERIPH_DAC1);
138 
139   /* Release reset of DAC clock */
140   LL_APB1_GRP1_ReleaseReset(LL_APB1_GRP1_PERIPH_DAC1);
141 
142   return SUCCESS;
143 }
144 
145 /**
146   * @brief  Initialize some features of DAC channel.
147   * @note   @ref LL_DAC_Init() aims to ease basic configuration of a DAC channel.
148   *         Leaving it ready to be enabled and output:
149   *         a level by calling one of
150   *           @ref LL_DAC_ConvertData12RightAligned
151   *           @ref LL_DAC_ConvertData12LeftAligned
152   *           @ref LL_DAC_ConvertData8RightAligned
153   *         or one of the supported autogenerated wave.
154   * @note   This function allows configuration of:
155   *          - Output mode
156   *          - Trigger
157   *          - Wave generation
158   * @note   The setting of these parameters by function @ref LL_DAC_Init()
159   *         is conditioned to DAC state:
160   *         DAC channel must be disabled.
161   * @param  DACx DAC instance
162   * @param  DAC_Channel This parameter can be one of the following values:
163   *         @arg @ref LL_DAC_CHANNEL_1
164   *         @arg @ref LL_DAC_CHANNEL_2
165   * @param  DAC_InitStruct Pointer to a @ref LL_DAC_InitTypeDef structure
166   * @retval An ErrorStatus enumeration value:
167   *          - SUCCESS: DAC registers are initialized
168   *          - ERROR: DAC registers are not initialized
169   */
LL_DAC_Init(DAC_TypeDef * DACx,uint32_t DAC_Channel,LL_DAC_InitTypeDef * DAC_InitStruct)170 ErrorStatus LL_DAC_Init(DAC_TypeDef *DACx, uint32_t DAC_Channel, LL_DAC_InitTypeDef *DAC_InitStruct)
171 {
172   ErrorStatus status = SUCCESS;
173 
174   /* Check the parameters */
175   assert_param(IS_DAC_ALL_INSTANCE(DACx));
176   assert_param(IS_LL_DAC_CHANNEL(DAC_Channel));
177   assert_param(IS_LL_DAC_TRIGGER_SOURCE(DAC_InitStruct->TriggerSource));
178   assert_param(IS_LL_DAC_OUTPUT_BUFFER(DAC_InitStruct->OutputBuffer));
179   assert_param(IS_LL_DAC_WAVE_AUTO_GENER_MODE(DAC_InitStruct->WaveAutoGeneration));
180   if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
181   {
182     assert_param(IS_LL_DAC_WAVE_AUTO_GENER_CONFIG(DAC_InitStruct->WaveAutoGeneration,
183                                                   DAC_InitStruct->WaveAutoGenerationConfig));
184   }
185 
186   /* Note: Hardware constraint (refer to description of this function)        */
187   /*       DAC instance must be disabled.                                     */
188   if (LL_DAC_IsEnabled(DACx, DAC_Channel) == 0UL)
189   {
190     /* Configuration of DAC channel:                                          */
191     /*  - TriggerSource                                                       */
192     /*  - WaveAutoGeneration                                                  */
193     /*  - OutputBuffer                                                        */
194     /*  - OutputMode                                                          */
195     if (DAC_InitStruct->WaveAutoGeneration != LL_DAC_WAVE_AUTO_GENERATION_NONE)
196     {
197       MODIFY_REG(DACx->CR,
198                  (DAC_CR_TSEL1
199                   | DAC_CR_WAVE1
200                   | DAC_CR_MAMP1
201                   | DAC_CR_BOFF1
202                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
203                  ,
204                  (DAC_InitStruct->TriggerSource
205                   | DAC_InitStruct->WaveAutoGeneration
206                   | DAC_InitStruct->WaveAutoGenerationConfig
207                   | DAC_InitStruct->OutputBuffer
208                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
209                 );
210     }
211     else
212     {
213       MODIFY_REG(DACx->CR,
214                  (DAC_CR_TSEL1
215                   | DAC_CR_WAVE1
216                   | DAC_CR_BOFF1
217                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
218                  ,
219                  (DAC_InitStruct->TriggerSource
220                   | LL_DAC_WAVE_AUTO_GENERATION_NONE
221                   | DAC_InitStruct->OutputBuffer
222                  ) << (DAC_Channel & DAC_CR_CHX_BITOFFSET_MASK)
223                 );
224     }
225   }
226   else
227   {
228     /* Initialization error: DAC instance is not disabled.                    */
229     status = ERROR;
230   }
231   return status;
232 }
233 
234 /**
235   * @brief Set each @ref LL_DAC_InitTypeDef field to default value.
236   * @param DAC_InitStruct pointer to a @ref LL_DAC_InitTypeDef structure
237   *                       whose fields will be set to default values.
238   * @retval None
239   */
LL_DAC_StructInit(LL_DAC_InitTypeDef * DAC_InitStruct)240 void LL_DAC_StructInit(LL_DAC_InitTypeDef *DAC_InitStruct)
241 {
242   /* Set DAC_InitStruct fields to default values */
243   DAC_InitStruct->TriggerSource            = LL_DAC_TRIG_SOFTWARE;
244   DAC_InitStruct->WaveAutoGeneration       = LL_DAC_WAVE_AUTO_GENERATION_NONE;
245   /* Note: Parameter discarded if wave auto generation is disabled,           */
246   /*       set anyway to its default value.                                   */
247   DAC_InitStruct->WaveAutoGenerationConfig = LL_DAC_NOISE_LFSR_UNMASK_BIT0;
248   DAC_InitStruct->OutputBuffer             = LL_DAC_OUTPUT_BUFFER_ENABLE;
249 }
250 
251 /**
252   * @}
253   */
254 
255 /**
256   * @}
257   */
258 
259 /**
260   * @}
261   */
262 
263 #endif /* DAC */
264 
265 /**
266   * @}
267   */
268 
269 #endif /* USE_FULL_LL_DRIVER */
270 
271 /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
272