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