1 /********************************** (C) COPYRIGHT  *******************************
2 * File Name          : ch32f20x_dac.c
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2021/08/08
6 * Description        : This file provides all the DAC firmware functions.
7 ****************************************************************************************/
8 #include "ch32f20x_dac.h"
9 #include "ch32f20x_rcc.h"
10 
11 /* CTLR register Mask */
12 #define CTLR_CLEAR_MASK            ((uint32_t)0x00000FFE)
13 
14 /* DAC Dual Channels SWTR masks */
15 #define DUAL_SWTR_SET              ((uint32_t)0x00000003)
16 #define DUAL_SWTR_RESET            ((uint32_t)0xFFFFFFFC)
17 
18 /* DHR registers offsets */
19 #define DHR12R1_OFFSET             ((uint32_t)0x00000008)
20 #define DHR12R2_OFFSET             ((uint32_t)0x00000014)
21 #define DHR12RD_OFFSET             ((uint32_t)0x00000020)
22 
23 /* DOR register offset */
24 #define DOR_OFFSET                 ((uint32_t)0x0000002C)
25 
26 
27 /******************************************************************************************
28 * Function Name  : DAC_DeInit
29 * Description    : Deinitializes the DAC peripheral registers to their default reset values.
30 * Input          : None
31 * Return         : None
32 *******************************************************************************************/
DAC_DeInit(void)33 void DAC_DeInit(void)
34 {
35   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, ENABLE);
36   RCC_APB1PeriphResetCmd(RCC_APB1Periph_DAC, DISABLE);
37 }
38 
39 /******************************************************************************************
40 * Function Name  : DAC_Init
41 * Description    : Initializes the DAC peripheral according to the specified parameters in
42 *                  the DAC_InitStruct.
43 * Input          : DAC_Channel:the selected DAC channel.
44 *                     DAC_Channel_1: DAC Channel1 selected
45 *                     DAC_Channel_2: DAC Channel2 selected
46 *                  DAC_InitStruct:pointer to a DAC_InitTypeDef structure.
47 * Return         : None
48 *******************************************************************************************/
DAC_Init(uint32_t DAC_Channel,DAC_InitTypeDef * DAC_InitStruct)49 void DAC_Init(uint32_t DAC_Channel, DAC_InitTypeDef* DAC_InitStruct)
50 {
51   uint32_t tmpreg1 = 0, tmpreg2 = 0;
52 
53   tmpreg1 = DAC->CTLR;
54   tmpreg1 &= ~(CTLR_CLEAR_MASK << DAC_Channel);
55   tmpreg2 = (DAC_InitStruct->DAC_Trigger | DAC_InitStruct->DAC_WaveGeneration |
56              DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude | DAC_InitStruct->DAC_OutputBuffer);
57   tmpreg1 |= tmpreg2 << DAC_Channel;
58   DAC->CTLR = tmpreg1;
59 }
60 
61 /******************************************************************************************
62 * Function Name  : DAC_StructInit
63 * Description    : Fills each DAC_InitStruct member with its default value.
64 * Input          : DAC_InitStruct:pointer to a DAC_InitTypeDef structure which will be initialized.
65 * Return         : None
66 *******************************************************************************************/
DAC_StructInit(DAC_InitTypeDef * DAC_InitStruct)67 void DAC_StructInit(DAC_InitTypeDef* DAC_InitStruct)
68 {
69   DAC_InitStruct->DAC_Trigger = DAC_Trigger_None;
70   DAC_InitStruct->DAC_WaveGeneration = DAC_WaveGeneration_None;
71   DAC_InitStruct->DAC_LFSRUnmask_TriangleAmplitude = DAC_LFSRUnmask_Bit0;
72   DAC_InitStruct->DAC_OutputBuffer = DAC_OutputBuffer_Enable;
73 }
74 
75 /******************************************************************************************
76 * Function Name  : DAC_Cmd
77 * Description    : Enables or disables the specified DAC channel.
78 * Input          : DAC_Channel: the selected DAC channel.
79 *                    DAC_Channel_1: DAC Channel1 selected
80 *                    DAC_Channel_2: DAC Channel2 selected
81 *                  NewState:new state of the DAC channel(ENABLE or DISABLE).
82 * Return         : None
83 *******************************************************************************************/
DAC_Cmd(uint32_t DAC_Channel,FunctionalState NewState)84 void DAC_Cmd(uint32_t DAC_Channel, FunctionalState NewState)
85 {
86   if (NewState != DISABLE)
87   {
88     DAC->CTLR |= (DAC_EN1 << DAC_Channel);
89   }
90   else
91   {
92     DAC->CTLR &= ~(DAC_EN1 << DAC_Channel);
93   }
94 }
95 
96 /******************************************************************************************
97 * Function Name  : DAC_DMACmd
98 * Description    : Enables or disables the specified DAC channel DMA request.
99 * Input          : DAC_Channel: the selected DAC channel.
100 *                    DAC_Channel_1: DAC Channel1 selected
101 *                    DAC_Channel_2: DAC Channel2 selected
102 *                  NewState:new state of the DAC channel(ENABLE or DISABLE).
103 * Return         : None
104 *******************************************************************************************/
DAC_DMACmd(uint32_t DAC_Channel,FunctionalState NewState)105 void DAC_DMACmd(uint32_t DAC_Channel, FunctionalState NewState)
106 {
107   if (NewState != DISABLE)
108   {
109     DAC->CTLR |= (DAC_DMAEN1 << DAC_Channel);
110   }
111   else
112   {
113     DAC->CTLR &= ~(DAC_DMAEN1 << DAC_Channel);
114   }
115 }
116 
117 /******************************************************************************************
118 * Function Name  : DAC_SoftwareTriggerCmd
119 * Description    : Enables or disables the selected DAC channel software trigger.
120 * Input          : DAC_Channel: the selected DAC channel.
121 *                    DAC_Channel_1: DAC Channel1 selected
122 *                    DAC_Channel_2: DAC Channel2 selected
123 *                  NewState:new state of the DAC channel(ENABLE or DISABLE).
124 * Return         : None
125 *******************************************************************************************/
DAC_SoftwareTriggerCmd(uint32_t DAC_Channel,FunctionalState NewState)126 void DAC_SoftwareTriggerCmd(uint32_t DAC_Channel, FunctionalState NewState)
127 {
128   if (NewState != DISABLE)
129   {
130     DAC->SWTR |= (uint32_t)DAC_SWTRIG1 << (DAC_Channel >> 4);
131   }
132   else
133   {
134     DAC->SWTR &= ~((uint32_t)DAC_SWTRIG1 << (DAC_Channel >> 4));
135   }
136 }
137 
138 /******************************************************************************************
139 * Function Name  : DAC_DualSoftwareTriggerCmd
140 * Description    : Enables or disables the two DAC channel software trigger.
141 * Input          : NewState:new state of the DAC channel(ENABLE or DISABLE).
142 * Return         : None
143 *******************************************************************************************/
DAC_DualSoftwareTriggerCmd(FunctionalState NewState)144 void DAC_DualSoftwareTriggerCmd(FunctionalState NewState)
145 {
146   if (NewState != DISABLE)
147   {
148     DAC->SWTR |= DUAL_SWTR_SET ;
149   }
150   else
151   {
152     DAC->SWTR &= DUAL_SWTR_RESET;
153   }
154 }
155 
156 /******************************************************************************************
157 * Function Name  : DAC_WaveGenerationCmd
158 * Description    : Enables or disables the selected DAC channel wave generation.
159 * Input          : DAC_Channel: the selected DAC channel.
160 *                    DAC_Channel_1: DAC Channel1 selected
161 *                    DAC_Channel_2: DAC Channel2 selected
162 *                  DAC_Wave: Specifies the wave type to enable or disable.
163 *                    DAC_Wave_Noise: noise wave generation
164 *                    DAC_Wave_Triangle: triangle wave generation
165 *                  NewState:new state of the DAC channel(ENABLE or DISABLE).
166 * Return         : None
167 *******************************************************************************************/
DAC_WaveGenerationCmd(uint32_t DAC_Channel,uint32_t DAC_Wave,FunctionalState NewState)168 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
169 {
170   if (NewState != DISABLE)
171   {
172     DAC->CTLR |= DAC_Wave << DAC_Channel;
173   }
174   else
175   {
176     DAC->CTLR &= ~(DAC_Wave << DAC_Channel);
177   }
178 }
179 
180 /******************************************************************************************
181 * Function Name  : DAC_SetChannel1Data
182 * Description    : Set the specified data holding register value for DAC channel1.
183 * Input          : DAC_Align: Specifies the data alignment for DAC channel1.
184 *                    DAC_Align_8b_R: 8bit right data alignment selected
185 *                    DAC_Align_12b_L: 12bit left data alignment selected
186 *                    DAC_Align_12b_R: 12bit right data alignment selected
187 *                  Data : Data to be loaded in the selected data holding register.
188 * Return         : None
189 *******************************************************************************************/
DAC_SetChannel1Data(uint32_t DAC_Align,uint16_t Data)190 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
191 {
192   __IO uint32_t tmp = 0;
193 
194   tmp = (uint32_t)DAC_BASE;
195   tmp += DHR12R1_OFFSET + DAC_Align;
196 
197   *(__IO uint32_t *) tmp = Data;
198 }
199 
200 /******************************************************************************************
201 * Function Name  : DAC_SetChannel2Data
202 * Description    : Set the specified data holding register value for DAC channel2.
203 * Input          : DAC_Align: Specifies the data alignment for DAC channel1.
204 *                    DAC_Align_8b_R: 8bit right data alignment selected
205 *                    DAC_Align_12b_L: 12bit left data alignment selected
206 *                    DAC_Align_12b_R: 12bit right data alignment selected
207 *                  Data : Data to be loaded in the selected data holding register.
208 * Return         : None
209 *******************************************************************************************/
DAC_SetChannel2Data(uint32_t DAC_Align,uint16_t Data)210 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
211 {
212   __IO uint32_t tmp = 0;
213 
214   tmp = (uint32_t)DAC_BASE;
215   tmp += DHR12R2_OFFSET + DAC_Align;
216 
217   *(__IO uint32_t *)tmp = Data;
218 }
219 
220 /******************************************************************************************
221 * Function Name  : DAC_SetDualChannelData
222 * Description    : Set the specified data holding register value for two DAC.
223 * Input          : DAC_Align: Specifies the data alignment for DAC channel1.
224 *                    DAC_Align_8b_R: 8bit right data alignment selected
225 *                    DAC_Align_12b_L: 12bit left data alignment selected
226 *                    DAC_Align_12b_R: 12bit right data alignment selected
227 *                  Data1 : Data for DAC Channel1.
228 *                  Data2 : Data for DAC Channel2
229 * Return         : None
230 *******************************************************************************************/
DAC_SetDualChannelData(uint32_t DAC_Align,uint16_t Data2,uint16_t Data1)231 void DAC_SetDualChannelData(uint32_t DAC_Align, uint16_t Data2, uint16_t Data1)
232 {
233   uint32_t data = 0, tmp = 0;
234 
235   if (DAC_Align == DAC_Align_8b_R)
236   {
237     data = ((uint32_t)Data2 << 8) | Data1;
238   }
239   else
240   {
241     data = ((uint32_t)Data2 << 16) | Data1;
242   }
243 
244   tmp = (uint32_t)DAC_BASE;
245   tmp += DHR12RD_OFFSET + DAC_Align;
246 
247   *(__IO uint32_t *)tmp = data;
248 }
249 
250 /******************************************************************************************
251 * Function Name  : DAC_GetDataOutputValue
252 * Description    : Returns the last data output value of the selected DAC channel.
253 * Input          : DAC_Channel: the selected DAC channel.
254 *                    DAC_Channel_1: DAC Channel1 selected
255 *                    DAC_Channel_2: DAC Channel2 selected
256 * Return         : None
257 *******************************************************************************************/
DAC_GetDataOutputValue(uint32_t DAC_Channel)258 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
259 {
260   __IO uint32_t tmp = 0;
261 
262   tmp = (uint32_t) DAC_BASE ;
263   tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
264 
265   return (uint16_t) (*(__IO uint32_t*) tmp);
266 }
267 
268 
269 
270