1 /*!
2  * @file        apm32e10x_dac.c
3  *
4  * @brief       This file provides all the DAC firmware functions
5  *
6  * @version     V1.0.2
7  *
8  * @date        2022-12-31
9  *
10  * @attention
11  *
12  *  Copyright (C) 2021-2023 Geehy Semiconductor
13  *
14  *  You may not use this file except in compliance with the
15  *  GEEHY COPYRIGHT NOTICE (GEEHY SOFTWARE PACKAGE LICENSE).
16  *
17  *  The program is only for reference, which is distributed in the hope
18  *  that it will be useful and instructional for customers to develop
19  *  their software. Unless required by applicable law or agreed to in
20  *  writing, the program is distributed on an "AS IS" BASIS, WITHOUT
21  *  ANY WARRANTY OR CONDITIONS OF ANY KIND, either express or implied.
22  *  See the GEEHY SOFTWARE PACKAGE LICENSE for the governing permissions
23  *  and limitations under the License.
24  */
25 
26 #include "apm32e10x_dac.h"
27 #include "apm32e10x_rcm.h"
28 
29 /** @addtogroup APM32E10x_StdPeriphDriver
30   @{
31 */
32 
33 
34 /** @addtogroup DAC_Driver
35   * @brief DAC driver modules
36   @{
37 */
38 
39 /** @defgroup DAC_Functions Functions
40   @{
41 */
42 
43 /*!
44  * @brief        Reset dac peripheral registers to their default reset values.
45  *
46  * @param        None
47  *
48  * @retval       None
49  */
DAC_Reset(void)50 void DAC_Reset(void)
51 {
52     RCM_EnableAPB1PeriphReset(RCM_APB1_PERIPH_DAC);
53     RCM_DisableAPB1PeriphReset(RCM_APB1_PERIPH_DAC);
54 }
55 
56 /*!
57  * @brief        Config the DAC peripheral according to the specified parameters in the configStruct
58  *
59  * @param        channel: Select the DAC channel.
60  *                        This parameter can be one of the following values:
61  *                        @arg DAC_CHANNEL_1 : DAC channel 1
62  *                        @arg DAC_CHANNEL_2 : DAC channel 2
63  *
64  * @param        dacConfig: pointer to a DAC_Config_T structure
65  *
66  * @retval       None
67  */
DAC_Config(uint32_t channel,DAC_Config_T * dacConfig)68 void DAC_Config(uint32_t channel, DAC_Config_T* dacConfig)
69 {
70     uint32_t tmp1 = 0, tmp2 = 0;
71 
72     tmp1 = DAC->CTRL;
73 
74     tmp1 &= ~(((uint32_t)0x00000FFE) << channel);
75 
76     tmp2 = ((uint32_t)dacConfig->trigger | \
77             (uint32_t)dacConfig->waveGeneration | \
78             (uint32_t)dacConfig->maskAmplitudeSelect | \
79             (uint32_t)dacConfig->outputBuffer);
80     tmp1 |= tmp2 << channel;
81 
82     DAC->CTRL = tmp1;
83 }
84 
85 /*!
86  * @brief        Fills each DAC_Config_T member with its default value
87  *
88  * @param        dacConfig: pointer to a DAC_Config_T structure which will be initialized
89  *
90  * @retval       None
91  */
DAC_ConfigStructInit(DAC_Config_T * dacConfig)92 void DAC_ConfigStructInit(DAC_Config_T* dacConfig)
93 {
94     /* Initialize the DAC_Trigger member */
95     dacConfig->trigger = DAC_TRIGGER_NONE;
96     /* Initialize the DAC_WaveGeneration member */
97     dacConfig->waveGeneration = DAC_WAVE_GENERATION_NONE;
98     /* Initialize the DAC_LFSRUnmask_TriangleAmplitude member */
99     dacConfig->maskAmplitudeSelect = DAC_LFSR_MASK_BIT11_1;
100     /* Initialize the DAC_OutputBuffer member */
101     dacConfig->outputBuffer = DAC_OUTPUT_BUFFER_ENBALE;
102 }
103 
104 /*!
105  * @brief        Enables the specified DAC peripheral
106  *
107  * @param        channel: Select the DAC channel.
108  *                        This parameter can be one of the following values:
109  *                        @arg DAC_CHANNEL_1 : DAC channel 1
110  *                        @arg DAC_CHANNEL_2 : DAC channel 2
111  *
112  * @retval       None
113  */
DAC_Enable(DAC_CHANNEL_T channel)114 void DAC_Enable(DAC_CHANNEL_T channel)
115 {
116     if (channel == DAC_CHANNEL_1)
117     {
118         DAC->CTRL_B.ENCH1 = BIT_SET;
119     }
120     else if (channel == DAC_CHANNEL_2)
121     {
122         DAC->CTRL_B.ENCH2 = BIT_SET;
123     }
124 }
125 
126 /*!
127  * @brief        Disables the specified DAC peripheral
128  *
129  * @param        channel: Select the DAC channel.
130  *                        This parameter can be one of the following values:
131  *                        @arg DAC_CHANNEL_1 : DAC channel 1
132  *                        @arg DAC_CHANNEL_2 : DAC channel 2
133  *
134  * @retval       None
135  */
DAC_Disable(DAC_CHANNEL_T channel)136 void DAC_Disable(DAC_CHANNEL_T channel)
137 {
138     if (channel == DAC_CHANNEL_1)
139     {
140         DAC->CTRL_B.ENCH1 = BIT_RESET;
141     }
142     else if (channel == DAC_CHANNEL_2)
143     {
144         DAC->CTRL_B.ENCH2 = BIT_RESET;
145     }
146 }
147 
148 
149 /*!
150  * @brief        Enables the specified DAC channel DMA request
151  *
152  * @param        channel: Select the DAC channel.
153  *                        This parameter can be one of the following values:
154  *                        @arg DAC_CHANNEL_1 : DAC channel 1
155  *                        @arg DAC_CHANNEL_2 : DAC channel 2
156  *
157  * @retval       None
158  */
DAC_DMA_Enable(DAC_CHANNEL_T channel)159 void DAC_DMA_Enable(DAC_CHANNEL_T channel)
160 {
161     if (channel == DAC_CHANNEL_1)
162     {
163         DAC->CTRL_B.DMAENCH1 = BIT_SET;
164     }
165     else if (channel == DAC_CHANNEL_2)
166     {
167         DAC->CTRL_B.DMAENCH2 = BIT_SET;
168     }
169 }
170 
171 /*!
172  * @brief        Disables the specified DAC channel DMA request
173  *
174  * @param        channel: Select the DAC channel.
175  *                        This parameter can be one of the following values:
176  *                        @arg DAC_CHANNEL_1 : DAC channel 1
177  *                        @arg DAC_CHANNEL_2 : DAC channel 2
178  *
179  * @retval       None
180  */
DAC_DMA_Disable(DAC_CHANNEL_T channel)181 void DAC_DMA_Disable(DAC_CHANNEL_T channel)
182 {
183     if (channel == DAC_CHANNEL_1)
184     {
185         DAC->CTRL_B.DMAENCH1 = BIT_RESET;
186     }
187     else if (channel == DAC_CHANNEL_2)
188     {
189         DAC->CTRL_B.DMAENCH2 = BIT_RESET;
190     }
191 }
192 
193 /*!
194  * @brief        Enables the selected DAC channel software trigger
195  *
196  * @param        channel: Select the DAC channel.
197  *                        This parameter can be one of the following values:
198  *                        @arg DAC_CHANNEL_1 : DAC channel 1
199  *                        @arg DAC_CHANNEL_2 : DAC channel 2
200  *
201  * @retval       None
202  */
DAC_EnableSoftwareTrigger(DAC_CHANNEL_T channel)203 void DAC_EnableSoftwareTrigger(DAC_CHANNEL_T channel)
204 {
205     if (channel == DAC_CHANNEL_1)
206     {
207         DAC->SWTRG_B.SWTRG1 = BIT_SET;
208     }
209     else if (channel == DAC_CHANNEL_2)
210     {
211         DAC->SWTRG_B.SWTRG2 = BIT_SET;
212     }
213 }
214 
215 /*!
216  * @brief        Disable the selected DAC channel software trigger
217  *
218  * @param        channel: Select the DAC channel.
219  *                        This parameter can be one of the following values:
220  *                        @arg DAC_CHANNEL_1 : DAC channel 1
221  *                        @arg DAC_CHANNEL_2 : DAC channel 2
222  *
223  * @retval       None
224  */
DAC_DisableSoftwareTrigger(DAC_CHANNEL_T channel)225 void DAC_DisableSoftwareTrigger(DAC_CHANNEL_T channel)
226 {
227     if (channel == DAC_CHANNEL_1)
228     {
229         DAC->SWTRG_B.SWTRG1 = BIT_RESET;
230     }
231     else if (channel == DAC_CHANNEL_2)
232     {
233         DAC->SWTRG_B.SWTRG2 = BIT_RESET;
234     }
235 }
236 /*!
237  * @brief        Enables simultaneously the two DAC channels software
238  *
239  * @param        None
240  *
241  * @retval       None
242  */
DAC_EnableDualSoftwareTrigger(void)243 void DAC_EnableDualSoftwareTrigger(void)
244 {
245     DAC->SWTRG_B.SWTRG1 = BIT_SET;
246     DAC->SWTRG_B.SWTRG2 = BIT_SET;
247 }
248 
249 /*!
250  * @brief        Disables simultaneously the two DAC channels software
251  *
252  * @param        None
253  *
254  * @retval       None
255  */
DAC_DisableDualSoftwareTrigger(void)256 void DAC_DisableDualSoftwareTrigger(void)
257 {
258     DAC->SWTRG_B.SWTRG1 = BIT_RESET;
259     DAC->SWTRG_B.SWTRG2 = BIT_RESET;
260 }
261 
262 /*!
263  * @brief        Enables the selected DAC channel wave generation
264  *
265  * @param        channel: Select the DAC channel.
266  *                        This parameter can be one of the following values:
267  *                        @arg DAC_CHANNEL_1 : DAC channel 1
268  *                        @arg DAC_CHANNEL_2 : DAC channel 2
269  *
270  * @param        wave: Select the wave
271  *                     This parameter can be one of the following values:
272  *                     @arg DAC_WAVE_GENERATION_NONE     : no wave generation
273  *                     @arg DAC_WAVE_GENERATION_NOISE    : Noise wave generation
274  *                     @arg DAC_WAVE_GENERATION_TRIANGLE : Triangle wave generation
275  *
276  * @retval       None
277  */
DAC_EnableWaveGeneration(DAC_CHANNEL_T channel,DAC_WAVE_GENERATION_T wave)278 void DAC_EnableWaveGeneration(DAC_CHANNEL_T channel, DAC_WAVE_GENERATION_T wave)
279 {
280     DAC->CTRL &= 0xFF3FFF3F;
281     DAC->CTRL |= wave << channel;
282 }
283 
284 /*!
285  * @brief        Disables the selected DAC channel wave generation
286  *
287  * @param        channel: Select the DAC channel.
288  *                        This parameter can be one of the following values:
289  *                        @arg DAC_CHANNEL_1 : DAC channel 1
290  *                        @arg DAC_CHANNEL_2 : DAC channel 2
291  *
292  * @param        wave: Select the wave
293  *                     This parameter can be one of the following values:
294  *                     @arg DAC_WAVE_GENERATION_NONE     : no wave generation
295  *                     @arg DAC_WAVE_GENERATION_NOISE    : Noise wave generation
296  *                     @arg DAC_WAVE_GENERATION_TRIANGLE : Triangle wave generation
297  *
298  * @retval       None
299  */
DAC_DisableWaveGeneration(DAC_CHANNEL_T channel,DAC_WAVE_GENERATION_T wave)300 void DAC_DisableWaveGeneration(DAC_CHANNEL_T channel, DAC_WAVE_GENERATION_T wave)
301 {
302     DAC->CTRL &= ~(wave << channel);
303 }
304 
305 /*!
306  * @brief        Set the specified data holding register value for DAC channel 1
307  *
308  * @param        align: DAC channel 1 data alignment
309  *                      This parameter can be one of the following values:
310  *                      @arg DAC_ALIGN_12BIT_R : 12-bit right-aligned data
311  *                      @arg DAC_ALIGN_12BIT_L : 12-bit left-aligned data
312  *                      @arg DAC_ALIGN_8BIT_R  : 8-bit right-aligned data
313  *
314  * @param        data: The data to be loaded in the selected data register.
315  *
316  * @retval       None
317  */
DAC_ConfigChannel1Data(DAC_ALIGN_T align,uint16_t data)318 void DAC_ConfigChannel1Data(DAC_ALIGN_T align, uint16_t data)
319 {
320     __IO uint32_t tmp = 0;
321 
322     tmp = (uint32_t)DAC_BASE;
323     tmp += 0x00000008 + align;
324 
325     /* Set the DAC channel1 selected data holding register */
326     *(__IO uint32_t *) tmp = data;
327 }
328 
329 /*!
330  * @brief        Set the specified data holding register value for DAC channel 2
331  *
332  * @param        align: DAC channel 2 data alignment
333  *                      This parameter can be one of the following values:
334  *                      @arg DAC_ALIGN_12BIT_R : 12-bit right-aligned data
335  *                      @arg DAC_ALIGN_12BIT_L : 12-bit left-aligned data
336  *                      @arg DAC_ALIGN_8BIT_R  : 8-bit right-aligned data
337  *
338  * @param        data: The data to be loaded in the selected data register.
339  *
340  * @retval       None
341  */
DAC_ConfigChannel2Data(DAC_ALIGN_T align,uint16_t data)342 void DAC_ConfigChannel2Data(DAC_ALIGN_T align, uint16_t data)
343 {
344     __IO uint32_t tmp = 0;
345 
346     tmp = (uint32_t)DAC_BASE;
347     tmp += 0x00000014 + align;
348 
349     /* Set the DAC channel1 selected data holding register */
350     *(__IO uint32_t *) tmp = data;
351 }
352 
353 /*!
354  * @brief        Set the specified data holding register value for dual DAC channel
355  *
356  * @param        align: Dual DAC channel data alignment
357  *                      This parameter can be one of the following values:
358  *                      @arg DAC_ALIGN_12BIT_R : 12-bit right-aligned data
359  *                      @arg DAC_ALIGN_12BIT_L : 12-bit left-aligned data
360  *                      @arg DAC_ALIGN_8BIT_R  : 8-bit right-aligned data
361  *
362  * @param        data2: Data for channel2 to be loaded in the selected data register.
363  *
364  * @param        data1: Data for channel1 to be loaded in the selected data register
365  *
366  * @retval       None
367  */
DAC_ConfigDualChannelData(DAC_ALIGN_T align,uint16_t data2,uint16_t data1)368 void DAC_ConfigDualChannelData(DAC_ALIGN_T align, uint16_t data2, uint16_t data1)
369 {
370     uint32_t data = 0, tmp = 0;
371 
372     /* Calculate and set dual DAC data holding register value */
373     if (align == DAC_ALIGN_8BIT_R)
374     {
375         data = ((uint32_t)data2 << 8) | data1;
376     }
377     else
378     {
379         data = ((uint32_t)data2 << 16) | data1;
380     }
381 
382     tmp = (uint32_t)DAC_BASE;
383     tmp += 0x00000020 + align;
384 
385     /* Set the dual DAC selected data holding register */
386     *(__IO uint32_t *)tmp = data;
387 }
388 
389 /*!
390  * @brief     Reads the specified DAC channel data output value.
391  *
392  * @param     channel: Select the DAC channel.
393  *                     This parameter can be one of the following values:
394  *                     @arg DAC_CHANNEL_1 : DAC channel 1
395  *                     @arg DAC_CHANNEL_2 : DAC channel 2
396  *
397  * @retval    The data output value of the specified DAC channel.
398  */
DAC_ReadDataOutputValue(DAC_CHANNEL_T channel)399 uint16_t DAC_ReadDataOutputValue(DAC_CHANNEL_T channel)
400 {
401     __IO uint32_t tmp = 0;
402 
403     tmp = (uint32_t) DAC_BASE ;
404     tmp += 0x0000002C + ((uint32_t)channel >> 2);
405 
406     /* Returns the DAC channel data output register value */
407     return (uint16_t) (*(__IO uint32_t*) tmp);
408 }
409 
410 /**@} end of group DAC_Functions */
411 /**@} end of group DAC_Driver */
412 /**@} end of group APM32E10x_StdPeriphDriver */
413