1 /*! 2 \file gd32f4xx_dac.h 3 \brief definitions for the DAC 4 5 \version 2016-08-15, V1.0.0, firmware for GD32F4xx 6 \version 2018-12-12, V2.0.0, firmware for GD32F4xx 7 \version 2020-09-30, V2.1.0, firmware for GD32F4xx 8 \version 2022-03-09, V3.0.0, firmware for GD32F4xx 9 */ 10 11 /* 12 Copyright (c) 2022, GigaDevice Semiconductor Inc. 13 14 Redistribution and use in source and binary forms, with or without modification, 15 are permitted provided that the following conditions are met: 16 17 1. Redistributions of source code must retain the above copyright notice, this 18 list of conditions and the following disclaimer. 19 2. Redistributions in binary form must reproduce the above copyright notice, 20 this list of conditions and the following disclaimer in the documentation 21 and/or other materials provided with the distribution. 22 3. Neither the name of the copyright holder nor the names of its contributors 23 may be used to endorse or promote products derived from this software without 24 specific prior written permission. 25 26 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" 27 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED 28 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. 29 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, 30 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT 31 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR 32 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 33 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) 34 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY 35 OF SUCH DAMAGE. 36 */ 37 38 #ifndef GD32F4XX_DAC_H 39 #define GD32F4XX_DAC_H 40 41 #include "gd32f4xx.h" 42 43 /* DACx(x=0,1) definitions */ 44 #define DAC DAC_BASE 45 #define DAC0 0U 46 #define DAC1 1U 47 48 /* registers definitions */ 49 #define DAC_CTL REG32(DAC + 0x00U) /*!< DAC control register */ 50 #define DAC_SWT REG32(DAC + 0x04U) /*!< DAC software trigger register */ 51 #define DAC0_R12DH REG32(DAC + 0x08U) /*!< DAC0 12-bit right-aligned data holding register */ 52 #define DAC0_L12DH REG32(DAC + 0x0CU) /*!< DAC0 12-bit left-aligned data holding register */ 53 #define DAC0_R8DH REG32(DAC + 0x10U) /*!< DAC0 8-bit right-aligned data holding register */ 54 #define DAC1_R12DH REG32(DAC + 0x14U) /*!< DAC1 12-bit right-aligned data holding register */ 55 #define DAC1_L12DH REG32(DAC + 0x18U) /*!< DAC1 12-bit left-aligned data holding register */ 56 #define DAC1_R8DH REG32(DAC + 0x1CU) /*!< DAC1 8-bit right-aligned data holding register */ 57 #define DACC_R12DH REG32(DAC + 0x20U) /*!< DAC concurrent mode 12-bit right-aligned data holding register */ 58 #define DACC_L12DH REG32(DAC + 0x24U) /*!< DAC concurrent mode 12-bit left-aligned data holding register */ 59 #define DACC_R8DH REG32(DAC + 0x28U) /*!< DAC concurrent mode 8-bit right-aligned data holding register */ 60 #define DAC0_DO REG32(DAC + 0x2CU) /*!< DAC0 data output register */ 61 #define DAC1_DO REG32(DAC + 0x30U) /*!< DAC1 data output register */ 62 #define DAC_STAT REG32(DAC + 0x34U) /*!< DAC status register */ 63 64 /* bits definitions */ 65 /* DAC_CTL */ 66 #define DAC_CTL_DEN0 BIT(0) /*!< DAC0 enable/disable bit */ 67 #define DAC_CTL_DBOFF0 BIT(1) /*!< DAC0 output buffer turn on/turn off bit */ 68 #define DAC_CTL_DTEN0 BIT(2) /*!< DAC0 trigger enable/disable bit */ 69 #define DAC_CTL_DTSEL0 BITS(3,5) /*!< DAC0 trigger source selection enable/disable bits */ 70 #define DAC_CTL_DWM0 BITS(6,7) /*!< DAC0 noise wave mode */ 71 #define DAC_CTL_DWBW0 BITS(8,11) /*!< DAC0 noise wave bit width */ 72 #define DAC_CTL_DDMAEN0 BIT(12) /*!< DAC0 DMA enable/disable bit */ 73 #define DAC_CTL_DDUDRIE0 BIT(13) /*!< DAC0 DMA underrun interrupt enable/disable bit */ 74 #define DAC_CTL_DEN1 BIT(16) /*!< DAC1 enable/disable bit */ 75 #define DAC_CTL_DBOFF1 BIT(17) /*!< DAC1 output buffer turn on/turn off bit */ 76 #define DAC_CTL_DTEN1 BIT(18) /*!< DAC1 trigger enable/disable bit */ 77 #define DAC_CTL_DTSEL1 BITS(19,21) /*!< DAC1 trigger source selection enable/disable bits */ 78 #define DAC_CTL_DWM1 BITS(22,23) /*!< DAC1 noise wave mode */ 79 #define DAC_CTL_DWBW1 BITS(24,27) /*!< DAC1 noise wave bit width */ 80 #define DAC_CTL_DDMAEN1 BIT(28) /*!< DAC1 DMA enable/disable bit */ 81 #define DAC_CTL_DDUDRIE1 BIT(29) /*!< DAC1 DMA underrun interrupt enable/disable bit */ 82 83 /* DAC_SWT */ 84 #define DAC_SWT_SWTR0 BIT(0) /*!< DAC0 software trigger bit, cleared by hardware */ 85 #define DAC_SWT_SWTR1 BIT(1) /*!< DAC1 software trigger bit, cleared by hardware */ 86 87 /* DAC0_R12DH */ 88 #define DAC0_R12DH_DAC0_DH BITS(0,11) /*!< DAC0 12-bit right-aligned data bits */ 89 90 /* DAC0_L12DH */ 91 #define DAC0_L12DH_DAC0_DH BITS(4,15) /*!< DAC0 12-bit left-aligned data bits */ 92 93 /* DAC0_R8DH */ 94 #define DAC0_R8DH_DAC0_DH BITS(0,7) /*!< DAC0 8-bit right-aligned data bits */ 95 96 /* DAC1_R12DH */ 97 #define DAC1_R12DH_DAC1_DH BITS(0,11) /*!< DAC1 12-bit right-aligned data bits */ 98 99 /* DAC1_L12DH */ 100 #define DAC1_L12DH_DAC1_DH BITS(4,15) /*!< DAC1 12-bit left-aligned data bits */ 101 102 /* DAC1_R8DH */ 103 #define DAC1_R8DH_DAC1_DH BITS(0,7) /*!< DAC1 8-bit right-aligned data bits */ 104 105 /* DACC_R12DH */ 106 #define DACC_R12DH_DAC0_DH BITS(0,11) /*!< DAC concurrent mode DAC0 12-bit right-aligned data bits */ 107 #define DACC_R12DH_DAC1_DH BITS(16,27) /*!< DAC concurrent mode DAC1 12-bit right-aligned data bits */ 108 109 /* DACC_L12DH */ 110 #define DACC_L12DH_DAC0_DH BITS(4,15) /*!< DAC concurrent mode DAC0 12-bit left-aligned data bits */ 111 #define DACC_L12DH_DAC1_DH BITS(20,31) /*!< DAC concurrent mode DAC1 12-bit left-aligned data bits */ 112 113 /* DACC_R8DH */ 114 #define DACC_R8DH_DAC0_DH BITS(0,7) /*!< DAC concurrent mode DAC0 8-bit right-aligned data bits */ 115 #define DACC_R8DH_DAC1_DH BITS(8,15) /*!< DAC concurrent mode DAC1 8-bit right-aligned data bits */ 116 117 /* DAC0_DO */ 118 #define DAC0_DO_DAC0_DO BITS(0,11) /*!< DAC0 12-bit output data bits */ 119 120 /* DAC1_DO */ 121 #define DAC1_DO_DAC1_DO BITS(0,11) /*!< DAC1 12-bit output data bits */ 122 123 /* DAC_STAT */ 124 #define DAC_STAT_DDUDR0 BIT(13) /*!< DAC0 DMA underrun flag */ 125 #define DAC_STAT_DDUDR1 BIT(29) /*!< DAC1 DMA underrun flag */ 126 127 /* constants definitions */ 128 /* DAC trigger source */ 129 #define CTL_DTSEL(regval) (BITS(3,5) & ((uint32_t)(regval) << 3)) 130 #define DAC_TRIGGER_T5_TRGO CTL_DTSEL(0) /*!< TIMER5 TRGO */ 131 #define DAC_TRIGGER_T7_TRGO CTL_DTSEL(1) /*!< TIMER7 TRGO */ 132 #define DAC_TRIGGER_T6_TRGO CTL_DTSEL(2) /*!< TIMER6 TRGO */ 133 #define DAC_TRIGGER_T4_TRGO CTL_DTSEL(3) /*!< TIMER4 TRGO */ 134 #define DAC_TRIGGER_T1_TRGO CTL_DTSEL(4) /*!< TIMER1 TRGO */ 135 #define DAC_TRIGGER_T3_TRGO CTL_DTSEL(5) /*!< TIMER3 TRGO */ 136 #define DAC_TRIGGER_EXTI_9 CTL_DTSEL(6) /*!< EXTI interrupt line9 event */ 137 #define DAC_TRIGGER_SOFTWARE CTL_DTSEL(7) /*!< software trigger */ 138 139 /* DAC noise wave mode */ 140 #define CTL_DWM(regval) (BITS(6,7) & ((uint32_t)(regval) << 6)) 141 #define DAC_WAVE_DISABLE CTL_DWM(0) /*!< wave disable */ 142 #define DAC_WAVE_MODE_LFSR CTL_DWM(1) /*!< LFSR noise mode */ 143 #define DAC_WAVE_MODE_TRIANGLE CTL_DWM(2) /*!< triangle noise mode */ 144 145 /* DAC noise wave bit width */ 146 #define DWBW(regval) (BITS(8,11) & ((uint32_t)(regval) << 8)) 147 #define DAC_WAVE_BIT_WIDTH_1 DWBW(0) /*!< bit width of the wave signal is 1 */ 148 #define DAC_WAVE_BIT_WIDTH_2 DWBW(1) /*!< bit width of the wave signal is 2 */ 149 #define DAC_WAVE_BIT_WIDTH_3 DWBW(2) /*!< bit width of the wave signal is 3 */ 150 #define DAC_WAVE_BIT_WIDTH_4 DWBW(3) /*!< bit width of the wave signal is 4 */ 151 #define DAC_WAVE_BIT_WIDTH_5 DWBW(4) /*!< bit width of the wave signal is 5 */ 152 #define DAC_WAVE_BIT_WIDTH_6 DWBW(5) /*!< bit width of the wave signal is 6 */ 153 #define DAC_WAVE_BIT_WIDTH_7 DWBW(6) /*!< bit width of the wave signal is 7 */ 154 #define DAC_WAVE_BIT_WIDTH_8 DWBW(7) /*!< bit width of the wave signal is 8 */ 155 #define DAC_WAVE_BIT_WIDTH_9 DWBW(8) /*!< bit width of the wave signal is 9 */ 156 #define DAC_WAVE_BIT_WIDTH_10 DWBW(9) /*!< bit width of the wave signal is 10 */ 157 #define DAC_WAVE_BIT_WIDTH_11 DWBW(10) /*!< bit width of the wave signal is 11 */ 158 #define DAC_WAVE_BIT_WIDTH_12 DWBW(11) /*!< bit width of the wave signal is 12 */ 159 160 /* unmask LFSR bits in DAC LFSR noise mode */ 161 #define DAC_LFSR_BIT0 DAC_WAVE_BIT_WIDTH_1 /*!< unmask the LFSR bit0 */ 162 #define DAC_LFSR_BITS1_0 DAC_WAVE_BIT_WIDTH_2 /*!< unmask the LFSR bits[1:0] */ 163 #define DAC_LFSR_BITS2_0 DAC_WAVE_BIT_WIDTH_3 /*!< unmask the LFSR bits[2:0] */ 164 #define DAC_LFSR_BITS3_0 DAC_WAVE_BIT_WIDTH_4 /*!< unmask the LFSR bits[3:0] */ 165 #define DAC_LFSR_BITS4_0 DAC_WAVE_BIT_WIDTH_5 /*!< unmask the LFSR bits[4:0] */ 166 #define DAC_LFSR_BITS5_0 DAC_WAVE_BIT_WIDTH_6 /*!< unmask the LFSR bits[5:0] */ 167 #define DAC_LFSR_BITS6_0 DAC_WAVE_BIT_WIDTH_7 /*!< unmask the LFSR bits[6:0] */ 168 #define DAC_LFSR_BITS7_0 DAC_WAVE_BIT_WIDTH_8 /*!< unmask the LFSR bits[7:0] */ 169 #define DAC_LFSR_BITS8_0 DAC_WAVE_BIT_WIDTH_9 /*!< unmask the LFSR bits[8:0] */ 170 #define DAC_LFSR_BITS9_0 DAC_WAVE_BIT_WIDTH_10 /*!< unmask the LFSR bits[9:0] */ 171 #define DAC_LFSR_BITS10_0 DAC_WAVE_BIT_WIDTH_11 /*!< unmask the LFSR bits[10:0] */ 172 #define DAC_LFSR_BITS11_0 DAC_WAVE_BIT_WIDTH_12 /*!< unmask the LFSR bits[11:0] */ 173 174 /* DAC data alignment */ 175 #define DATA_ALIGN(regval) (BITS(0,1) & ((uint32_t)(regval) << 0)) 176 #define DAC_ALIGN_12B_R DATA_ALIGN(0) /*!< data right 12 bit alignment */ 177 #define DAC_ALIGN_12B_L DATA_ALIGN(1) /*!< data left 12 bit alignment */ 178 #define DAC_ALIGN_8B_R DATA_ALIGN(2) /*!< data right 8 bit alignment */ 179 180 /* triangle amplitude in DAC triangle noise mode */ 181 #define DAC_TRIANGLE_AMPLITUDE_1 DAC_WAVE_BIT_WIDTH_1 /*!< triangle amplitude is 1 */ 182 #define DAC_TRIANGLE_AMPLITUDE_3 DAC_WAVE_BIT_WIDTH_2 /*!< triangle amplitude is 3 */ 183 #define DAC_TRIANGLE_AMPLITUDE_7 DAC_WAVE_BIT_WIDTH_3 /*!< triangle amplitude is 7 */ 184 #define DAC_TRIANGLE_AMPLITUDE_15 DAC_WAVE_BIT_WIDTH_4 /*!< triangle amplitude is 15 */ 185 #define DAC_TRIANGLE_AMPLITUDE_31 DAC_WAVE_BIT_WIDTH_5 /*!< triangle amplitude is 31 */ 186 #define DAC_TRIANGLE_AMPLITUDE_63 DAC_WAVE_BIT_WIDTH_6 /*!< triangle amplitude is 63 */ 187 #define DAC_TRIANGLE_AMPLITUDE_127 DAC_WAVE_BIT_WIDTH_7 /*!< triangle amplitude is 127 */ 188 #define DAC_TRIANGLE_AMPLITUDE_255 DAC_WAVE_BIT_WIDTH_8 /*!< triangle amplitude is 255 */ 189 #define DAC_TRIANGLE_AMPLITUDE_511 DAC_WAVE_BIT_WIDTH_9 /*!< triangle amplitude is 511 */ 190 #define DAC_TRIANGLE_AMPLITUDE_1023 DAC_WAVE_BIT_WIDTH_10 /*!< triangle amplitude is 1023 */ 191 #define DAC_TRIANGLE_AMPLITUDE_2047 DAC_WAVE_BIT_WIDTH_11 /*!< triangle amplitude is 2047 */ 192 #define DAC_TRIANGLE_AMPLITUDE_4095 DAC_WAVE_BIT_WIDTH_12 /*!< triangle amplitude is 4095 */ 193 194 /* function declarations */ 195 /* initialization functions */ 196 /* deinitialize DAC */ 197 void dac_deinit(void); 198 /* enable DAC */ 199 void dac_enable(uint32_t dac_periph); 200 /* disable DAC */ 201 void dac_disable(uint32_t dac_periph); 202 /* enable DAC DMA */ 203 void dac_dma_enable(uint32_t dac_periph); 204 /* disable DAC DMA */ 205 void dac_dma_disable(uint32_t dac_periph); 206 /* enable DAC output buffer */ 207 void dac_output_buffer_enable(uint32_t dac_periph); 208 /* disable DAC output buffer */ 209 void dac_output_buffer_disable(uint32_t dac_periph); 210 /* get the last data output value */ 211 uint16_t dac_output_value_get(uint32_t dac_periph); 212 /* set DAC data holding register value */ 213 void dac_data_set(uint32_t dac_periph, uint32_t dac_align, uint16_t data); 214 215 /* DAC trigger configuration */ 216 /* enable DAC trigger */ 217 void dac_trigger_enable(uint32_t dac_periph); 218 /* disable DAC trigger */ 219 void dac_trigger_disable(uint32_t dac_periph); 220 /* configure DAC trigger source */ 221 void dac_trigger_source_config(uint32_t dac_periph, uint32_t triggersource); 222 /* enable DAC software trigger */ 223 void dac_software_trigger_enable(uint32_t dac_periph); 224 /* disable DAC software trigger */ 225 void dac_software_trigger_disable(uint32_t dac_periph); 226 227 /* DAC wave mode configuration */ 228 /* configure DAC wave mode */ 229 void dac_wave_mode_config(uint32_t dac_periph, uint32_t wave_mode); 230 /* configure DAC wave bit width */ 231 void dac_wave_bit_width_config(uint32_t dac_periph, uint32_t bit_width); 232 /* configure DAC LFSR noise mode */ 233 void dac_lfsr_noise_config(uint32_t dac_periph, uint32_t unmask_bits); 234 /* configure DAC triangle noise mode */ 235 void dac_triangle_noise_config(uint32_t dac_periph, uint32_t amplitude); 236 237 /* DAC concurrent mode configuration */ 238 /* enable DAC concurrent mode */ 239 void dac_concurrent_enable(void); 240 /* disable DAC concurrent mode */ 241 void dac_concurrent_disable(void); 242 /* enable DAC concurrent software trigger */ 243 void dac_concurrent_software_trigger_enable(void); 244 /* disable DAC concurrent software trigger */ 245 void dac_concurrent_software_trigger_disable(void); 246 /* enable DAC concurrent buffer function */ 247 void dac_concurrent_output_buffer_enable(void); 248 /* disable DAC concurrent buffer function */ 249 void dac_concurrent_output_buffer_disable(void); 250 /* set DAC concurrent mode data holding register value */ 251 void dac_concurrent_data_set(uint32_t dac_align, uint16_t data0, uint16_t data1); 252 /* enable DAC concurrent interrupt */ 253 void dac_concurrent_interrupt_enable(void); 254 /* disable DAC concurrent interrupt */ 255 void dac_concurrent_interrupt_disable(void); 256 257 /* DAC interrupt configuration */ 258 /* get the specified DAC flag(DAC DMA underrun flag) */ 259 FlagStatus dac_flag_get(uint32_t dac_periph); 260 /* clear the specified DAC flag(DAC DMA underrun flag) */ 261 void dac_flag_clear(uint32_t dac_periph); 262 /* enable DAC interrupt(DAC DMA underrun interrupt) */ 263 void dac_interrupt_enable(uint32_t dac_periph); 264 /* disable DAC interrupt(DAC DMA underrun interrupt) */ 265 void dac_interrupt_disable(uint32_t dac_periph); 266 /* get the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */ 267 FlagStatus dac_interrupt_flag_get(uint32_t dac_periph); 268 /* clear the specified DAC interrupt flag(DAC DMA underrun interrupt flag) */ 269 void dac_interrupt_flag_clear(uint32_t dac_periph); 270 271 #endif /* GD32F4XX_DAC_H */ 272