1 /*****************************************************************************
2  * Copyright (c) 2022, Nations Technologies Inc.
3  *
4  * All rights reserved.
5  * ****************************************************************************
6  *
7  * Redistribution and use in source and binary forms, with or without
8  * modification, are permitted provided that the following conditions are met:
9  *
10  * - Redistributions of source code must retain the above copyright notice,
11  * this list of conditions and the disclaimer below.
12  *
13  * Nations' name may not be used to endorse or promote products derived from
14  * this software without specific prior written permission.
15  *
16  * DISCLAIMER: THIS SOFTWARE IS PROVIDED BY NATIONS "AS IS" AND ANY EXPRESS OR
17  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT ARE
19  * DISCLAIMED. IN NO EVENT SHALL NATIONS BE LIABLE FOR ANY DIRECT, INDIRECT,
20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
21  * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA,
22  * OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
23  * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
24  * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE,
25  * EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
26  * ****************************************************************************/
27 
28 /**
29  * @file n32l40x_dac.c
30  * @author Nations
31  * @version v1.2.0
32  *
33  * @copyright Copyright (c) 2022, Nations Technologies Inc. All rights reserved.
34  */
35 #include "n32l40x_dac.h"
36 #include "n32l40x_rcc.h"
37 
38 /** @addtogroup n32l40x_StdPeriph_Driver
39  * @{
40  */
41 
42 /** @addtogroup DAC
43  * @brief DAC driver modules
44  * @{
45  */
46 
47 /** @addtogroup DAC_Private_TypesDefinitions
48  * @{
49  */
50 
51 /**
52  * @}
53  */
54 
55 /** @addtogroup DAC_Private_Defines
56  * @{
57  */
58 
59 /* CTRL register Mask */
60 #define CTRL_CLEAR_MASK ((uint32_t)0x00000FFE)
61 
62 /* DAC Dual Channels SWTRIG masks */
63 #define DUAL_SWTRIG_SET   ((uint32_t)0x00000001)
64 #define DUAL_SWTRIG_RESET ((uint32_t)0xFFFFFFFE)
65 
66 /* DCH registers offsets */
67 #define DR12CH_OFFSET ((uint32_t)0x00000008)
68 
69 /* DATO register offset */
70 #define DATO_OFFSET ((uint32_t)0x0000002C)
71 /**
72  * @}
73  */
74 
75 /** @addtogroup DAC_Private_Macros
76  * @{
77  */
78 
79 /**
80  * @}
81  */
82 
83 /** @addtogroup DAC_Private_Variables
84  * @{
85  */
86 
87 /**
88  * @}
89  */
90 
91 /** @addtogroup DAC_Private_FunctionPrototypes
92  * @{
93  */
94 
95 /**
96  * @}
97  */
98 
99 /** @addtogroup DAC_Private_Functions
100  * @{
101  */
102 
103 /**
104  * @brief  Deinitializes the DAC peripheral registers to their default reset values.
105  */
DAC_DeInit(void)106 void DAC_DeInit(void)
107 {
108     /* Enable DAC reset state */
109     RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_DAC, ENABLE);
110     /* Release DAC from reset state */
111     RCC_EnableAPB1PeriphReset(RCC_APB1_PERIPH_DAC, DISABLE);
112 }
113 
114 /**
115  * @brief  Initializes the DAC peripheral according to the specified
116  *         parameters in the DAC_InitStruct.
117  * @param DAC_Channel the selected DAC channel.
118  *   This parameter can be one of the following values:
119  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
120  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
121  * @param DAC_InitStruct pointer to a DAC_InitType structure that
122  *        contains the configuration information for the specified DAC channel.
123  */
DAC_Init(DAC_InitType * DAC_InitStruct)124 void DAC_Init(DAC_InitType* DAC_InitStruct)
125 {
126     uint32_t tmpreg1 = 0, tmpreg2 = 0;
127     /* Check the DAC parameters */
128     assert_param(IS_DAC_TRIGGER(DAC_InitStruct->Trigger));
129     assert_param(IS_DAC_GENERATE_WAVE(DAC_InitStruct->WaveGen));
130     assert_param(IS_DAC_LFSR_UNMASK_TRIANGLE_AMPLITUDE(DAC_InitStruct->LfsrUnMaskTriAmp));
131     assert_param(IS_DAC_OUTPUT_BUFFER_STATE(DAC_InitStruct->BufferOutput));
132     /*---------------------------- DAC CTRL Configuration --------------------------*/
133     /* Get the DAC CTRL value */
134     tmpreg1 = DAC->CTRL;
135     /* Clear BOFFx, TENx, TSELx, WAVEx and MAMPx bits */
136     tmpreg1 &= ~(CTRL_CLEAR_MASK );
137     /* Configure for the selected DAC channel: buffer output, trigger, wave generation,
138        mask/amplitude for wave generation */
139     /* Set TSELx and TENx bits according to Trigger value */
140     /* Set WAVEx bits according to WaveGen value */
141     /* Set MAMPx bits according to LfsrUnMaskTriAmp value */
142     /* Set BOFFx bit according to BufferOutput value */
143     tmpreg2 = (DAC_InitStruct->Trigger | DAC_InitStruct->WaveGen | DAC_InitStruct->LfsrUnMaskTriAmp
144                | DAC_InitStruct->BufferOutput);
145     /* Calculate CTRL register value depending on DAC_Channel */
146     tmpreg1 |= tmpreg2 ;
147     /* Write to DAC CTRL */
148     DAC->CTRL = tmpreg1;
149 }
150 
151 /**
152  * @brief  Fills each DAC_InitStruct member with its default value.
153  * @param DAC_InitStruct pointer to a DAC_InitType structure which will
154  *         be initialized.
155  */
DAC_ClearStruct(DAC_InitType * DAC_InitStruct)156 void DAC_ClearStruct(DAC_InitType* DAC_InitStruct)
157 {
158     /*--------------- Reset DAC init structure parameters values -----------------*/
159     /* Initialize the Trigger member */
160     DAC_InitStruct->Trigger = DAC_TRG_NONE;
161     /* Initialize the WaveGen member */
162     DAC_InitStruct->WaveGen = DAC_WAVEGEN_NONE;
163     /* Initialize the LfsrUnMaskTriAmp member */
164     DAC_InitStruct->LfsrUnMaskTriAmp = DAC_UNMASK_LFSRBIT0;
165     /* Initialize the BufferOutput member */
166     DAC_InitStruct->BufferOutput = DAC_BUFFOUTPUT_ENABLE;
167 }
168 
169 /**
170  * @brief  Enables or disables the specified DAC channel.
171  * @param DAC_Channel the selected DAC channel.
172  *   This parameter can be one of the following values:
173  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
174  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
175  * @param Cmd new state of the DAC channel.
176  *   This parameter can be: ENABLE or DISABLE.
177  */
DAC_Enable(FunctionalState Cmd)178 void DAC_Enable(FunctionalState Cmd)
179 {
180     /* Check the parameters */
181     assert_param(IS_FUNCTIONAL_STATE(Cmd));
182     if (Cmd != DISABLE)
183     {
184         /* Enable the selected DAC channel */
185         DAC->CTRL |= DAC_CTRL_CHEN ;
186     }
187     else
188     {
189         /* Disable the selected DAC channel */
190         DAC->CTRL &= ~DAC_CTRL_CHEN ;
191     }
192 }
193 
194 /**
195  * @brief  Enables or disables the specified DAC channel DMA request.
196  * @param DAC_Channel the selected DAC channel.
197  *   This parameter can be one of the following values:
198  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
199  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
200  * @param Cmd new state of the selected DAC channel DMA request.
201  *   This parameter can be: ENABLE or DISABLE.
202  */
DAC_DmaEnable(FunctionalState Cmd)203 void DAC_DmaEnable(FunctionalState Cmd)
204 {
205     /* Check the parameters */
206     assert_param(IS_FUNCTIONAL_STATE(Cmd));
207     if (Cmd != DISABLE)
208     {
209         /* Enable the selected DAC channel DMA request */
210         DAC->CTRL |= DAC_CTRL_DMAEN;
211     }
212     else
213     {
214         /* Disable the selected DAC channel DMA request */
215         DAC->CTRL &= ~DAC_CTRL_DMAEN;
216     }
217 }
218 
219 /**
220  * @brief  Enables or disables the selected DAC channel software trigger.
221  * @param DAC_Channel the selected DAC channel.
222  *   This parameter can be one of the following values:
223  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
224  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
225  * @param Cmd new state of the selected DAC channel software trigger.
226  *   This parameter can be: ENABLE or DISABLE.
227  */
DAC_SoftTrgEnable(FunctionalState Cmd)228 void DAC_SoftTrgEnable(FunctionalState Cmd)
229 {
230     /* Check the parameters */
231     assert_param(IS_FUNCTIONAL_STATE(Cmd));
232     if (Cmd != DISABLE)
233     {
234         /* Enable software trigger for the selected DAC channel */
235         DAC->SOTTR |= DAC_SOTTR_TREN ;
236     }
237     else
238     {
239         /* Disable software trigger for the selected DAC channel */
240         DAC->SOTTR &= ~(DAC_SOTTR_TREN);
241     }
242 }
243 
244 /**
245  * @brief  Enables or disables simultaneously the two DAC channels software
246  *   triggers.
247  * @param Cmd new state of the DAC channels software triggers.
248  *   This parameter can be: ENABLE or DISABLE.
249  */
DAC_SoftwareTrgEnable(FunctionalState Cmd)250 void DAC_SoftwareTrgEnable(FunctionalState Cmd)
251 {
252     /* Check the parameters */
253     assert_param(IS_FUNCTIONAL_STATE(Cmd));
254     if (Cmd != DISABLE)
255     {
256         /* Enable software trigger for both DAC channels */
257         DAC->SOTTR |= DUAL_SWTRIG_SET;
258     }
259     else
260     {
261         /* Disable software trigger for both DAC channels */
262         DAC->SOTTR &= DUAL_SWTRIG_RESET;
263     }
264 }
265 
266 /**
267  * @brief  Enables or disables the selected DAC channel wave generation.
268  * @param DAC_Channel the selected DAC channel.
269  *   This parameter can be one of the following values:
270  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
271  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
272  * @param DAC_Wave Specifies the wave type to enable or disable.
273  *   This parameter can be one of the following values:
274  *     @arg DAC_WAVE_NOISE noise wave generation
275  *     @arg DAC_WAVE_TRIANGLE triangle wave generation
276  * @param Cmd new state of the selected DAC channel wave generation.
277  *   This parameter can be: ENABLE or DISABLE.
278  */
DAC_WaveGenerationEnable(uint32_t DAC_Wave,FunctionalState Cmd)279 void DAC_WaveGenerationEnable(uint32_t DAC_Wave, FunctionalState Cmd)
280 {
281       __IO uint32_t tmp = 0;
282     /* Check the parameters */
283     assert_param(IS_DAC_WAVE(DAC_Wave));
284     assert_param(IS_FUNCTIONAL_STATE(Cmd));
285       tmp=DAC->CTRL;
286       tmp&=~(3<<6);
287     if (Cmd != DISABLE)
288     {
289         /* Enable the selected wave generation for the selected DAC channel */
290         tmp |= DAC_Wave;
291     }
292     else
293     {
294         /* Disable the selected wave generation for the selected DAC channel */
295         tmp&=~(3<<6);
296     }
297         DAC->CTRL =tmp;
298 }
299 
300 /**
301  * @brief  Set the specified data holding register value for DAC channel1.
302  * @param DAC_Align Specifies the data alignment for DAC channel1.
303  *   This parameter can be one of the following values:
304  *     @arg DAC_ALIGN_R_8BIT 8bit right data alignment selected
305  *     @arg DAC_ALIGN_L_12BIT 12bit left data alignment selected
306  *     @arg DAC_ALIGN_R_12BIT 12bit right data alignment selected
307  * @param Data Data to be loaded in the selected data holding register.
308  */
DAC_SetChData(uint32_t DAC_Align,uint16_t Data)309 void DAC_SetChData(uint32_t DAC_Align, uint16_t Data)
310 {
311     __IO uint32_t tmp = 0;
312 
313     /* Check the parameters */
314     assert_param(IS_DAC_ALIGN(DAC_Align));
315     assert_param(IS_DAC_DATA(Data));
316 
317     tmp = (uint32_t)DAC_BASE;
318     tmp += DR12CH_OFFSET + DAC_Align;
319 
320     /* Set the DAC channel1 selected data holding register */
321     *(__IO uint32_t*)tmp = Data;
322 }
323 
324 
325 
326 
327 
328 /**
329  * @brief  Returns the last data output value of the selected DAC channel.
330  * @param DAC_Channel the selected DAC channel.
331  *   This parameter can be one of the following values:
332  *     @arg DAC_CHANNEL_1 DAC Channel1 selected
333  *     @arg DAC_CHANNEL_2 DAC Channel2 selected
334  * @return The selected DAC channel data output value.
335  */
DAC_GetOutputDataVal(void)336 uint16_t DAC_GetOutputDataVal(void)
337 {
338     __IO uint32_t tmp = 0;
339 
340     tmp = (uint32_t)DAC_BASE;
341     tmp += DATO_OFFSET;
342 
343     /* Returns the DAC channel data output register value */
344     return (uint16_t)(*(__IO uint32_t*)tmp);
345 }
346 
347 /**
348  * @}
349  */
350 
351 /**
352  * @}
353  */
354 
355 /**
356  * @}
357  */
358