1 /*!
2     \file    gd32f3x0_dac.h
3     \brief   definitions for the DAC
4 
5     \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6     \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7     \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9 
10 /*
11     Copyright (c) 2020, GigaDevice Semiconductor Inc.
12 
13     Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15 
16     1. Redistributions of source code must retain the above copyright notice, this
17        list of conditions and the following disclaimer.
18     2. Redistributions in binary form must reproduce the above copyright notice,
19        this list of conditions and the following disclaimer in the documentation
20        and/or other materials provided with the distribution.
21     3. Neither the name of the copyright holder nor the names of its contributors
22        may be used to endorse or promote products derived from this software without
23        specific prior written permission.
24 
25     THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36 
37 #ifdef GD32F350
38 #ifndef GD32F3X0_DAC_H
39 #define GD32F3X0_DAC_H
40 
41 #include "gd32f3x0.h"
42 
43 /* DAC definitions */
44 #define DAC                     DAC_BASE
45 
46 /* registers definitions */
47 #define DAC_CTL                 REG32(DAC + (0x00000000U))  /*!< DAC control register */
48 #define DAC_SWT                 REG32(DAC + (0x00000004U))  /*!< DAC software trigger register */
49 #define DAC_R12DH               REG32(DAC + (0x00000008U))  /*!< DAC 12-bit right-aligned data holding register */
50 #define DAC_L12DH               REG32(DAC + (0x0000000CU))  /*!< DAC 12-bit left-aligned data holding register */
51 #define DAC_R8DH                REG32(DAC + (0x00000010U))  /*!< DAC 8-bit right-aligned data holding register */
52 #define DAC_DO                  REG32(DAC + (0x0000002CU))  /*!< DAC output data register */
53 #define DAC_STAT                REG32(DAC + (0x00000034U))  /*!< DAC status register */
54 
55 /* bits definitions */
56 /* DAC_CTL */
57 #define DAC_CTL_DEN             BIT(0)                      /*!< DAC enable/disable bit */
58 #define DAC_CTL_DBOFF           BIT(1)                      /*!< DAC output buffer turn on/turn off bit */
59 #define DAC_CTL_DTEN            BIT(2)                      /*!< DAC trigger enable/disable bit */
60 #define DAC_CTL_DTSEL           BITS(3,5)                   /*!< DAC trigger source selection enable/disable bits */
61 #define DAC_CTL_DWM             BITS(6,7)                   /*!< DAC noise wave mode */
62 #define DAC_CTL_DWBW            BITS(8,11)                  /*!< DAC noise wave bit width */
63 #define DAC_CTL_DDMAEN          BIT(12)                     /*!< DAC DMA enable/disable bit */
64 #define DAC_CTL_DDUDRIE         BIT(13)                     /*!< DAC DMA underrun interrupt enable/disable bit */
65 
66 /* DAC_SWT */
67 #define DAC_SWT_SWTR            BIT(0)                      /*!< DAC software trigger bit,cleared by hardware */
68 
69 /* DAC_R12DH */
70 #define DAC_R12DH_DAC_DH        BITS(0,11)                  /*!< DAC 12-bit right-aligned data bits */
71 
72 /* DAC_L12DH */
73 #define DAC_L12DH_DAC_DH        BITS(4,15)                  /*!< DAC 12-bit left-aligned data bits */
74 
75 /* DAC_R8DH */
76 #define DAC_R8DH_DAC_DH         BITS(0,7)                   /*!< DAC 8-bit right-aligned data bits */
77 
78 /* DAC_DO */
79 #define DAC_DO_DAC_DO           BITS(0,11)                  /*!< DAC 12-bit output data bits */
80 
81 /* DAC_STAT */
82 #define DAC_STAT_DDUDR          BIT(13)                     /*!< DAC DMA underrun flag */
83 
84 /* constants definitions */
85 /* DAC trigger source */
86 #define CTL_DTSEL(regval)       (BITS(3,5) & ((uint32_t)(regval) << 3))
87 #define DAC_TRIGGER_T5_TRGO     CTL_DTSEL(0)                /*!< TIMER5 TRGO */
88 #define DAC_TRIGGER_T2_TRGO     CTL_DTSEL(1)                /*!< TIMER2 TRGO */
89 #define DAC_TRIGGER_T14_TRGO    CTL_DTSEL(3)                /*!< TIMER14 TRGO */
90 #define DAC_TRIGGER_T1_TRGO     CTL_DTSEL(4)                /*!< TIMER1 TRGO */
91 #define DAC_TRIGGER_EXTI_9      CTL_DTSEL(6)                /*!< EXTI interrupt line9 event */
92 #define DAC_TRIGGER_SOFTWARE    CTL_DTSEL(7)                /*!< software trigger */
93 
94 /* DAC noise wave mode */
95 #define CTL_DWM(regval)         (BITS(6,7) & ((uint32_t)(regval) << 6))
96 #define DAC_WAVE_DISABLE        CTL_DWM(0)                  /*!< wave disable */
97 #define DAC_WAVE_MODE_LFSR      CTL_DWM(1)                  /*!< LFSR noise mode */
98 #define DAC_WAVE_MODE_TRIANGLE  CTL_DWM(2)                  /*!< triangle noise mode */
99 
100 /* DAC noise wave bit width */
101 #define DWBW(regval)            (BITS(8,11) & ((uint32_t)(regval) << 8))
102 #define DAC_WAVE_BIT_WIDTH_1    DWBW(0)                     /*!< bit width of the wave signal is 1 */
103 #define DAC_WAVE_BIT_WIDTH_2    DWBW(1)                     /*!< bit width of the wave signal is 2 */
104 #define DAC_WAVE_BIT_WIDTH_3    DWBW(2)                     /*!< bit width of the wave signal is 3 */
105 #define DAC_WAVE_BIT_WIDTH_4    DWBW(3)                     /*!< bit width of the wave signal is 4 */
106 #define DAC_WAVE_BIT_WIDTH_5    DWBW(4)                     /*!< bit width of the wave signal is 5 */
107 #define DAC_WAVE_BIT_WIDTH_6    DWBW(5)                     /*!< bit width of the wave signal is 6 */
108 #define DAC_WAVE_BIT_WIDTH_7    DWBW(6)                     /*!< bit width of the wave signal is 7 */
109 #define DAC_WAVE_BIT_WIDTH_8    DWBW(7)                     /*!< bit width of the wave signal is 8 */
110 #define DAC_WAVE_BIT_WIDTH_9    DWBW(8)                     /*!< bit width of the wave signal is 9 */
111 #define DAC_WAVE_BIT_WIDTH_10   DWBW(9)                     /*!< bit width of the wave signal is 10 */
112 #define DAC_WAVE_BIT_WIDTH_11   DWBW(10)                    /*!< bit width of the wave signal is 11 */
113 #define DAC_WAVE_BIT_WIDTH_12   DWBW(11)                    /*!< bit width of the wave signal is 12 */
114 
115 /* unmask LFSR bits in DAC LFSR noise mode */
116 #define DAC_LFSR_BIT0           DAC_WAVE_BIT_WIDTH_1        /*!< unmask the LFSR bit0 */
117 #define DAC_LFSR_BITS1_0        DAC_WAVE_BIT_WIDTH_2        /*!< unmask the LFSR bits[1:0] */
118 #define DAC_LFSR_BITS2_0        DAC_WAVE_BIT_WIDTH_3        /*!< unmask the LFSR bits[2:0] */
119 #define DAC_LFSR_BITS3_0        DAC_WAVE_BIT_WIDTH_4        /*!< unmask the LFSR bits[3:0] */
120 #define DAC_LFSR_BITS4_0        DAC_WAVE_BIT_WIDTH_5        /*!< unmask the LFSR bits[4:0] */
121 #define DAC_LFSR_BITS5_0        DAC_WAVE_BIT_WIDTH_6        /*!< unmask the LFSR bits[5:0] */
122 #define DAC_LFSR_BITS6_0        DAC_WAVE_BIT_WIDTH_7        /*!< unmask the LFSR bits[6:0] */
123 #define DAC_LFSR_BITS7_0        DAC_WAVE_BIT_WIDTH_8        /*!< unmask the LFSR bits[7:0] */
124 #define DAC_LFSR_BITS8_0        DAC_WAVE_BIT_WIDTH_9        /*!< unmask the LFSR bits[8:0] */
125 #define DAC_LFSR_BITS9_0        DAC_WAVE_BIT_WIDTH_10       /*!< unmask the LFSR bits[9:0] */
126 #define DAC_LFSR_BITS10_0       DAC_WAVE_BIT_WIDTH_11       /*!< unmask the LFSR bits[10:0] */
127 #define DAC_LFSR_BITS11_0       DAC_WAVE_BIT_WIDTH_12       /*!< unmask the LFSR bits[11:0] */
128 
129 /* triangle amplitude in DAC triangle noise mode */
130 #define DAC_TRIANGLE_AMPLITUDE_1    DAC_WAVE_BIT_WIDTH_1    /*!< triangle amplitude is 1 */
131 #define DAC_TRIANGLE_AMPLITUDE_3    DAC_WAVE_BIT_WIDTH_2    /*!< triangle amplitude is 3 */
132 #define DAC_TRIANGLE_AMPLITUDE_7    DAC_WAVE_BIT_WIDTH_3    /*!< triangle amplitude is 7 */
133 #define DAC_TRIANGLE_AMPLITUDE_15   DAC_WAVE_BIT_WIDTH_4    /*!< triangle amplitude is 15 */
134 #define DAC_TRIANGLE_AMPLITUDE_31   DAC_WAVE_BIT_WIDTH_5    /*!< triangle amplitude is 31 */
135 #define DAC_TRIANGLE_AMPLITUDE_63   DAC_WAVE_BIT_WIDTH_6    /*!< triangle amplitude is 63 */
136 #define DAC_TRIANGLE_AMPLITUDE_127  DAC_WAVE_BIT_WIDTH_7    /*!< triangle amplitude is 127 */
137 #define DAC_TRIANGLE_AMPLITUDE_255  DAC_WAVE_BIT_WIDTH_8    /*!< triangle amplitude is 255 */
138 #define DAC_TRIANGLE_AMPLITUDE_511  DAC_WAVE_BIT_WIDTH_9    /*!< triangle amplitude is 511 */
139 #define DAC_TRIANGLE_AMPLITUDE_1023 DAC_WAVE_BIT_WIDTH_10   /*!< triangle amplitude is 1023 */
140 #define DAC_TRIANGLE_AMPLITUDE_2047 DAC_WAVE_BIT_WIDTH_11   /*!< triangle amplitude is 2047 */
141 #define DAC_TRIANGLE_AMPLITUDE_4095 DAC_WAVE_BIT_WIDTH_12   /*!< triangle amplitude is 4095 */
142 
143 /* DAC data alignment */
144 #define DATA_ALIGN(regval)      (BITS(0,1) & ((uint32_t)(regval) << 0))
145 #define DAC_ALIGN_12B_R         DATA_ALIGN(0)               /*!< data right 12b alignment */
146 #define DAC_ALIGN_12B_L         DATA_ALIGN(1)               /*!< data left 12b alignment */
147 #define DAC_ALIGN_8B_R          DATA_ALIGN(2)               /*!< data right 8b alignment */
148 
149 /* function declarations */
150 /* deinitialize DAC */
151 void dac_deinit(void);
152 
153 /* enable DAC */
154 void dac_enable(void);
155 /* disable DAC */
156 void dac_disable(void);
157 /* enable DAC DMA */
158 void dac_dma_enable(void);
159 /* disable DAC DMA */
160 void dac_dma_disable(void);
161 /* enable DAC output buffer */
162 void dac_output_buffer_enable(void);
163 /* disable DAC output buffer */
164 void dac_output_buffer_disable(void);
165 /* enable DAC trigger */
166 void dac_trigger_enable(void);
167 /* disable DAC trigger */
168 void dac_trigger_disable(void);
169 /* enable DAC software trigger */
170 void dac_software_trigger_enable(void);
171 /* disable DAC software trigger */
172 void dac_software_trigger_disable(void);
173 /* enable DAC interrupt(DAC DMA underrun interrupt) */
174 void dac_interrupt_enable(void);
175 /* disable DAC interrupt(DAC DMA underrun interrupt) */
176 void dac_interrupt_disable(void);
177 
178 /* configure DAC trigger source */
179 void dac_trigger_source_config(uint32_t triggersource);
180 /* configure DAC wave mode */
181 void dac_wave_mode_config(uint32_t wave_mode);
182 /* configure DAC wave bit width */
183 void dac_wave_bit_width_config(uint32_t bit_width);
184 /* configure DAC LFSR noise mode */
185 void dac_lfsr_noise_config(uint32_t unmask_bits);
186 /* configure DAC triangle noise mode */
187 void dac_triangle_noise_config(uint32_t amplitude);
188 /* get the last data output value */
189 uint16_t dac_output_value_get(void);
190 
191 /* get the specified DAC flag(DAC DMA underrun flag) */
192 FlagStatus dac_flag_get(void);
193 /* clear the specified DAC flag(DAC DMA underrun flag) */
194 void dac_flag_clear(void);
195 /* get the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
196 FlagStatus dac_interrupt_flag_get(void);
197 /* clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */
198 void dac_interrupt_flag_clear(void);
199 
200 /* set DAC data holding register value */
201 void dac_data_set(uint32_t dac_align, uint16_t data);
202 
203 #endif /* GD32F3X0_DAC_H */
204 
205 #endif /* GD32F350 */
206