1 /*********************COPYRIGHT(C)  2019 WCH. A11 rights reserved***********************
2 * File Name          : ch32f10x_dac.c
3 * Author             : WCH
4 * Version            : V1.0.0
5 * Date               : 2019/10/15
6 * Description        : This file provides all the DAC firmware functions.
7 ****************************************************************************************/
8 #include "ch32f10x_dac.h"
9 #include "ch32f10x_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 /******************************************************************************************
140 * Function Name  : DAC_WaveGenerationCmd
141 * Description    : Enables or disables the selected DAC channel wave generation.
142 * Input          : DAC_Channel: the selected DAC channel.
143 *                    DAC_Channel_1: DAC Channel1 selected
144 *                    DAC_Channel_2: DAC Channel2 selected
145 *                  DAC_Wave: Specifies the wave type to enable or disable.
146 *                    DAC_Wave_Noise: noise wave generation
147 *                    DAC_Wave_Triangle: triangle wave generation
148 *                  NewState:new state of the DAC channel(ENABLE or DISABLE).
149 * Return         : None
150 *******************************************************************************************/
DAC_WaveGenerationCmd(uint32_t DAC_Channel,uint32_t DAC_Wave,FunctionalState NewState)151 void DAC_WaveGenerationCmd(uint32_t DAC_Channel, uint32_t DAC_Wave, FunctionalState NewState)
152 {
153   if (NewState != DISABLE)
154   {
155     DAC->CTLR |= DAC_Wave << DAC_Channel;
156   }
157   else
158   {
159     DAC->CTLR &= ~(DAC_Wave << DAC_Channel);
160   }
161 }
162 
163 /******************************************************************************************
164 * Function Name  : DAC_SetChannel1Data
165 * Description    : Set the specified data holding register value for DAC channel1.
166 * Input          : DAC_Align: Specifies the data alignment for DAC channel1.
167 *                    DAC_Align_8b_R: 8bit right data alignment selected
168 *                    DAC_Align_12b_L: 12bit left data alignment selected
169 *                    DAC_Align_12b_R: 12bit right data alignment selected
170 *                  Data : Data to be loaded in the selected data holding register.
171 * Return         : None
172 *******************************************************************************************/
DAC_SetChannel1Data(uint32_t DAC_Align,uint16_t Data)173 void DAC_SetChannel1Data(uint32_t DAC_Align, uint16_t Data)
174 {
175   __IO uint32_t tmp = 0;
176 
177   tmp = (uint32_t)DAC_BASE;
178   tmp += DHR12R1_OFFSET + DAC_Align;
179 
180   *(__IO uint32_t *) tmp = Data;
181 }
182 
183 /******************************************************************************************
184 * Function Name  : DAC_SetChannel2Data
185 * Description    : Set the specified data holding register value for DAC channel2.
186 * Input          : DAC_Align: Specifies the data alignment for DAC channel1.
187 *                    DAC_Align_8b_R: 8bit right data alignment selected
188 *                    DAC_Align_12b_L: 12bit left data alignment selected
189 *                    DAC_Align_12b_R: 12bit right data alignment selected
190 *                  Data : Data to be loaded in the selected data holding register.
191 * Return         : None
192 *******************************************************************************************/
DAC_SetChannel2Data(uint32_t DAC_Align,uint16_t Data)193 void DAC_SetChannel2Data(uint32_t DAC_Align, uint16_t Data)
194 {
195   __IO uint32_t tmp = 0;
196 
197   tmp = (uint32_t)DAC_BASE;
198   tmp += DHR12R2_OFFSET + DAC_Align;
199 
200   *(__IO uint32_t *)tmp = Data;
201 }
202 
203 /******************************************************************************************
204 * Function Name  : DAC_GetDataOutputValue
205 * Description    : Returns the last data output value of the selected DAC channel.
206 * Input          : DAC_Channel: the selected DAC channel.
207 *                    DAC_Channel_1: DAC Channel1 selected
208 *                    DAC_Channel_2: DAC Channel2 selected
209 * Return         : The selected DAC channel data output value.
210 *******************************************************************************************/
DAC_GetDataOutputValue(uint32_t DAC_Channel)211 uint16_t DAC_GetDataOutputValue(uint32_t DAC_Channel)
212 {
213   __IO uint32_t tmp = 0;
214 
215   tmp = (uint32_t) DAC_BASE ;
216   tmp += DOR_OFFSET + ((uint32_t)DAC_Channel >> 2);
217 
218   return (uint16_t) (*(__IO uint32_t*) tmp);
219 }
220 
221 
222 
223 
224