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