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