1 /*!
2     \file    gd32vf103_dma.h
3     \brief   definitions for the DMA
4 
5     \version 2019-06-05, V1.0.0, firmware for GD32VF103
6     \version 2019-10-30, V1.0.1, firmware for GD32VF103
7     \version 2020-08-04, V1.1.0, firmware for GD32VF103
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 #ifndef GD32VF103_DMA_H
38 #define GD32VF103_DMA_H
39 
40 #include "gd32vf103.h"
41 
42 /* DMA definitions */
43 #define DMA0                            (DMA_BASE)               /*!< DMA0 base address */
44 #define DMA1                            (DMA_BASE + 0x0400U)     /*!< DMA1 base address */
45 
46 /* registers definitions */
47 #define DMA_INTF(dmax)                  REG32((dmax) + 0x00U)    /*!< DMA interrupt flag register */
48 #define DMA_INTC(dmax)                  REG32((dmax) + 0x04U)    /*!< DMA interrupt flag clear register */
49 
50 #define DMA_CH0CTL(dmax)                REG32((dmax) + 0x08U)    /*!< DMA channel 0 control register */
51 #define DMA_CH0CNT(dmax)                REG32((dmax) + 0x0CU)    /*!< DMA channel 0 counter register */
52 #define DMA_CH0PADDR(dmax)              REG32((dmax) + 0x10U)    /*!< DMA channel 0 peripheral base address register */
53 #define DMA_CH0MADDR(dmax)              REG32((dmax) + 0x14U)    /*!< DMA channel 0 memory base address register */
54 
55 #define DMA_CH1CTL(dmax)                REG32((dmax) + 0x1CU)    /*!< DMA channel 1 control register */
56 #define DMA_CH1CNT(dmax)                REG32((dmax) + 0x20U)    /*!< DMA channel 1 counter register */
57 #define DMA_CH1PADDR(dmax)              REG32((dmax) + 0x24U)    /*!< DMA channel 1 peripheral base address register */
58 #define DMA_CH1MADDR(dmax)              REG32((dmax) + 0x28U)    /*!< DMA channel 1 memory base address register */
59 
60 #define DMA_CH2CTL(dmax)                REG32((dmax) + 0x30U)    /*!< DMA channel 2 control register */
61 #define DMA_CH2CNT(dmax)                REG32((dmax) + 0x34U)    /*!< DMA channel 2 counter register */
62 #define DMA_CH2PADDR(dmax)              REG32((dmax) + 0x38U)    /*!< DMA channel 2 peripheral base address register */
63 #define DMA_CH2MADDR(dmax)              REG32((dmax) + 0x3CU)    /*!< DMA channel 2 memory base address register */
64 
65 #define DMA_CH3CTL(dmax)                REG32((dmax) + 0x44U)    /*!< DMA channel 3 control register */
66 #define DMA_CH3CNT(dmax)                REG32((dmax) + 0x48U)    /*!< DMA channel 3 counter register */
67 #define DMA_CH3PADDR(dmax)              REG32((dmax) + 0x4CU)    /*!< DMA channel 3 peripheral base address register */
68 #define DMA_CH3MADDR(dmax)              REG32((dmax) + 0x50U)    /*!< DMA channel 3 memory base address register */
69 
70 #define DMA_CH4CTL(dmax)                REG32((dmax) + 0x58U)    /*!< DMA channel 4 control register */
71 #define DMA_CH4CNT(dmax)                REG32((dmax) + 0x5CU)    /*!< DMA channel 4 counter register */
72 #define DMA_CH4PADDR(dmax)              REG32((dmax) + 0x60U)    /*!< DMA channel 4 peripheral base address register */
73 #define DMA_CH4MADDR(dmax)              REG32((dmax) + 0x64U)    /*!< DMA channel 4 memory base address register */
74 
75 #define DMA_CH5CTL(dmax)                REG32((dmax) + 0x6CU)    /*!< DMA channel 5 control register */
76 #define DMA_CH5CNT(dmax)                REG32((dmax) + 0x70U)    /*!< DMA channel 5 counter register */
77 #define DMA_CH5PADDR(dmax)              REG32((dmax) + 0x74U)    /*!< DMA channel 5 peripheral base address register */
78 #define DMA_CH5MADDR(dmax)              REG32((dmax) + 0x78U)    /*!< DMA channel 5 memory base address register */
79 
80 #define DMA_CH6CTL(dmax)                REG32((dmax) + 0x80U)    /*!< DMA channel 6 control register */
81 #define DMA_CH6CNT(dmax)                REG32((dmax) + 0x84U)    /*!< DMA channel 6 counter register */
82 #define DMA_CH6PADDR(dmax)              REG32((dmax) + 0x88U)    /*!< DMA channel 6 peripheral base address register */
83 #define DMA_CH6MADDR(dmax)              REG32((dmax) + 0x8CU)    /*!< DMA channel 6 memory base address register */
84 
85 /* bits definitions */
86 /* DMA_INTF */
87 #define DMA_INTF_GIF                    BIT(0)                  /*!< global interrupt flag of channel */
88 #define DMA_INTF_FTFIF                  BIT(1)                  /*!< full transfer finish flag of channel */
89 #define DMA_INTF_HTFIF                  BIT(2)                  /*!< half transfer finish flag of channel */
90 #define DMA_INTF_ERRIF                  BIT(3)                  /*!< error flag of channel */
91 
92 /* DMA_INTC */
93 #define DMA_INTC_GIFC                   BIT(0)                  /*!< clear global interrupt flag of channel */
94 #define DMA_INTC_FTFIFC                 BIT(1)                  /*!< clear transfer finish flag of channel */
95 #define DMA_INTC_HTFIFC                 BIT(2)                  /*!< clear half transfer finish flag of channel */
96 #define DMA_INTC_ERRIFC                 BIT(3)                  /*!< clear error flag of channel */
97 
98 /* DMA_CHxCTL, x=0..6 */
99 #define DMA_CHXCTL_CHEN                 BIT(0)                  /*!< channel enable */
100 #define DMA_CHXCTL_FTFIE                BIT(1)                  /*!< enable bit for channel full transfer finish interrupt */
101 #define DMA_CHXCTL_HTFIE                BIT(2)                  /*!< enable bit for channel half transfer finish interrupt */
102 #define DMA_CHXCTL_ERRIE                BIT(3)                  /*!< enable bit for channel error interrupt */
103 #define DMA_CHXCTL_DIR                  BIT(4)                  /*!< transfer direction */
104 #define DMA_CHXCTL_CMEN                 BIT(5)                  /*!< circular mode enable */
105 #define DMA_CHXCTL_PNAGA                BIT(6)                  /*!< next address generation algorithm of peripheral */
106 #define DMA_CHXCTL_MNAGA                BIT(7)                  /*!< next address generation algorithm of memory */
107 #define DMA_CHXCTL_PWIDTH               BITS(8,9)               /*!< transfer data width of peripheral */
108 #define DMA_CHXCTL_MWIDTH               BITS(10,11)             /*!< transfer data width of memory */
109 #define DMA_CHXCTL_PRIO                 BITS(12,13)             /*!< priority level */
110 #define DMA_CHXCTL_M2M                  BIT(14)                 /*!< memory to memory mode */
111 
112 /* DMA_CHxCNT, x=0..6 */
113 #define DMA_CHXCNT_CNT                  BITS(0,15)              /*!< transfer counter */
114 
115 /* DMA_CHxPADDR, x=0..6 */
116 #define DMA_CHXPADDR_PADDR              BITS(0,31)              /*!< peripheral base address */
117 
118 /* DMA_CHxMADDR, x=0..6 */
119 #define DMA_CHXMADDR_MADDR              BITS(0,31)              /*!< memory base address */
120 
121 /* constants definitions */
122 /* DMA channel select */
123 typedef enum
124 {
125     DMA_CH0 = 0,                /*!< DMA Channel0 */
126     DMA_CH1,                    /*!< DMA Channel1 */
127     DMA_CH2,                    /*!< DMA Channel2 */
128     DMA_CH3,                    /*!< DMA Channel3 */
129     DMA_CH4,                    /*!< DMA Channel4 */
130     DMA_CH5,                    /*!< DMA Channel5 */
131     DMA_CH6                     /*!< DMA Channel6 */
132 } dma_channel_enum;
133 
134 /* DMA initialize struct */
135 typedef struct
136 {
137     uint32_t periph_addr;       /*!< peripheral base address */
138     uint32_t periph_width;      /*!< transfer data size of peripheral */
139     uint32_t memory_addr;       /*!< memory base address */
140     uint32_t memory_width;      /*!< transfer data size of memory */
141     uint32_t number;            /*!< channel transfer number */
142     uint32_t priority;          /*!< channel priority level */
143     uint8_t periph_inc;         /*!< peripheral increasing mode */
144     uint8_t memory_inc;         /*!< memory increasing mode */
145     uint8_t direction;          /*!< channel data transfer direction */
146 
147 } dma_parameter_struct;
148 
149 #define DMA_FLAG_ADD(flag, shift)           ((flag) << ((shift) * 4U))                      /*!< DMA channel flag shift */
150 
151 /* DMA_register address */
152 #define DMA_CHCTL(dma, channel)             REG32(((dma) + 0x08U) + 0x14U * (uint32_t)(channel))      /*!< the address of DMA channel CHXCTL register */
153 #define DMA_CHCNT(dma, channel)             REG32(((dma) + 0x0CU) + 0x14U * (uint32_t)(channel))      /*!< the address of DMA channel CHXCNT register */
154 #define DMA_CHPADDR(dma, channel)           REG32(((dma) + 0x10U) + 0x14U * (uint32_t)(channel))      /*!< the address of DMA channel CHXPADDR register */
155 #define DMA_CHMADDR(dma, channel)           REG32(((dma) + 0x14U) + 0x14U * (uint32_t)(channel))      /*!< the address of DMA channel CHXMADDR register */
156 
157 /* DMA reset value */
158 #define DMA_CHCTL_RESET_VALUE               ((uint32_t)0x00000000U)                         /*!< the reset value of DMA channel CHXCTL register */
159 #define DMA_CHCNT_RESET_VALUE               ((uint32_t)0x00000000U)                         /*!< the reset value of DMA channel CHXCNT register */
160 #define DMA_CHPADDR_RESET_VALUE             ((uint32_t)0x00000000U)                         /*!< the reset value of DMA channel CHXPADDR register */
161 #define DMA_CHMADDR_RESET_VALUE             ((uint32_t)0x00000000U)                         /*!< the reset value of DMA channel CHXMADDR register */
162 #define DMA_CHINTF_RESET_VALUE              (DMA_INTF_GIF | DMA_INTF_FTFIF | \
163                                              DMA_INTF_HTFIF | DMA_INTF_ERRIF)               /*!< clear DMA channel DMA_INTF register */
164 
165 /* DMA_INTF register */
166 /* interrupt flag bits */
167 #define DMA_INT_FLAG_G                      DMA_INTF_GIF                                    /*!< global interrupt flag of channel */
168 #define DMA_INT_FLAG_FTF                    DMA_INTF_FTFIF                                  /*!< full transfer finish interrupt flag of channel */
169 #define DMA_INT_FLAG_HTF                    DMA_INTF_HTFIF                                  /*!< half transfer finish interrupt flag of channel */
170 #define DMA_INT_FLAG_ERR                    DMA_INTF_ERRIF                                  /*!< error interrupt flag of channel */
171 
172 /* flag bits */
173 #define DMA_FLAG_G                          DMA_INTF_GIF                                    /*!< global interrupt flag of channel */
174 #define DMA_FLAG_FTF                        DMA_INTF_FTFIF                                  /*!< full transfer finish flag of channel */
175 #define DMA_FLAG_HTF                        DMA_INTF_HTFIF                                  /*!< half transfer finish flag of channel */
176 #define DMA_FLAG_ERR                        DMA_INTF_ERRIF                                  /*!< error flag of channel */
177 
178 /* DMA_CHxCTL register */
179 /* interrupt enable bits */
180 #define DMA_INT_FTF                         DMA_CHXCTL_FTFIE                                /*!< enable bit for channel full transfer finish interrupt */
181 #define DMA_INT_HTF                         DMA_CHXCTL_HTFIE                                /*!< enable bit for channel half transfer finish interrupt */
182 #define DMA_INT_ERR                         DMA_CHXCTL_ERRIE                                /*!< enable bit for channel error interrupt */
183 
184 /* transfer direction */
185 #define DMA_PERIPHERAL_TO_MEMORY            ((uint8_t)0x00U)                         		/*!< read from peripheral and write to memory */
186 #define DMA_MEMORY_TO_PERIPHERAL            ((uint8_t)0x01U)                         		/*!< read from memory and write to peripheral */
187 
188 /* peripheral increasing mode */
189 #define DMA_PERIPH_INCREASE_DISABLE         ((uint8_t)0x00U)                                /*!< next address of peripheral is fixed address mode */
190 #define DMA_PERIPH_INCREASE_ENABLE          ((uint8_t)0x01U)                                /*!< next address of peripheral is increasing address mode */
191 
192 /* memory increasing mode */
193 #define DMA_MEMORY_INCREASE_DISABLE         ((uint8_t)0x00U)                                /*!< next address of memory is fixed address mode */
194 #define DMA_MEMORY_INCREASE_ENABLE          ((uint8_t)0x01U)                                /*!< next address of memory is increasing address mode */
195 
196 /* transfer data size of peripheral */
197 #define CHCTL_PWIDTH(regval)                (BITS(8,9) & ((uint32_t)(regval) << 8))          /*!< transfer data size of peripheral */
198 #define DMA_PERIPHERAL_WIDTH_8BIT           CHCTL_PWIDTH(0U)                                 /*!< transfer data size of peripheral is 8-bit */
199 #define DMA_PERIPHERAL_WIDTH_16BIT          CHCTL_PWIDTH(1U)                                 /*!< transfer data size of peripheral is 16-bit */
200 #define DMA_PERIPHERAL_WIDTH_32BIT          CHCTL_PWIDTH(2U)                                 /*!< transfer data size of peripheral is 32-bit */
201 
202 /* transfer data size of memory */
203 #define CHCTL_MWIDTH(regval)                (BITS(10,11) & ((uint32_t)(regval) << 10))       /*!< transfer data size of memory */
204 #define DMA_MEMORY_WIDTH_8BIT               CHCTL_MWIDTH(0U)                                 /*!< transfer data size of memory is 8-bit */
205 #define DMA_MEMORY_WIDTH_16BIT              CHCTL_MWIDTH(1U)                                 /*!< transfer data size of memory is 16-bit */
206 #define DMA_MEMORY_WIDTH_32BIT              CHCTL_MWIDTH(2U)                                 /*!< transfer data size of memory is 32-bit */
207 
208 /* channel priority level */
209 #define CHCTL_PRIO(regval)                  (BITS(12,13) & ((uint32_t)(regval) << 12))       /*!< DMA channel priority level */
210 #define DMA_PRIORITY_LOW                    CHCTL_PRIO(0U)                                   /*!< low priority */
211 #define DMA_PRIORITY_MEDIUM                 CHCTL_PRIO(1U)                                   /*!< medium priority */
212 #define DMA_PRIORITY_HIGH                   CHCTL_PRIO(2U)                                   /*!< high priority */
213 #define DMA_PRIORITY_ULTRA_HIGH             CHCTL_PRIO(3U)                                   /*!< ultra high priority */
214 
215 /* memory to memory mode */
216 #define DMA_MEMORY_TO_MEMORY_DISABLE        ((uint32_t)0x00000000U)                         /*!< disable memory to memory mode */
217 #define DMA_MEMORY_TO_MEMORY_ENABLE         ((uint32_t)0x00000001U)                         /*!< enable memory to memory mode */
218 
219 /* DMA_CHxCNT register */
220 /* transfer counter */
221 #define DMA_CHANNEL_CNT_MASK                DMA_CHXCNT_CNT                                  /*!< transfer counter mask */
222 
223 /* function declarations */
224 /* DMA deinitialization and initialization functions */
225 /* deinitialize DMA a channel registers */
226 void dma_deinit(uint32_t dma_periph, dma_channel_enum channelx);
227 /* initialize the parameters of DMA struct with the default values */
228 void dma_struct_para_init(dma_parameter_struct* init_struct);
229 /* initialize DMA channel */
230 void dma_init(uint32_t dma_periph, dma_channel_enum channelx, dma_parameter_struct *init_struct);
231 /* enable DMA circulation mode */
232 void dma_circulation_enable(uint32_t dma_periph, dma_channel_enum channelx);
233 /* disable DMA circulation mode */
234 void dma_circulation_disable(uint32_t dma_periph, dma_channel_enum channelx);
235 /* enable memory to memory mode */
236 void dma_memory_to_memory_enable(uint32_t dma_periph, dma_channel_enum channelx);
237 /* disable memory to memory mode */
238 void dma_memory_to_memory_disable(uint32_t dma_periph, dma_channel_enum channelx);
239 /* enable DMA channel */
240 void dma_channel_enable(uint32_t dma_periph, dma_channel_enum channelx);
241 /* disable DMA channel */
242 void dma_channel_disable(uint32_t dma_periph, dma_channel_enum channelx);
243 
244 /* DMA configuration functions */
245 /* set DMA peripheral base address */
246 void dma_periph_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address);
247 /* set DMA memory base address */
248 void dma_memory_address_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t address);
249 /* set the number of remaining data to be transferred by the DMA */
250 void dma_transfer_number_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t number);
251 /* get the number of remaining data to be transferred by the DMA */
252 uint32_t dma_transfer_number_get(uint32_t dma_periph, dma_channel_enum channelx);
253 /* configure priority level of DMA channel */
254 void dma_priority_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t priority);
255 /* configure transfer data size of memory */
256 void dma_memory_width_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t mwidth);
257 /* configure transfer data size of peripheral */
258 void dma_periph_width_config(uint32_t dma_periph, dma_channel_enum channelx, uint32_t pwidth);
259 /* enable next address increasement algorithm of memory */
260 void dma_memory_increase_enable(uint32_t dma_periph, dma_channel_enum channelx);
261 /* disable next address increasement algorithm of memory */
262 void dma_memory_increase_disable(uint32_t dma_periph, dma_channel_enum channelx);
263 /* enable next address increasement algorithm of peripheral */
264 void dma_periph_increase_enable(uint32_t dma_periph, dma_channel_enum channelx);
265 /* disable next address increasement algorithm of peripheral */
266 void dma_periph_increase_disable(uint32_t dma_periph, dma_channel_enum channelx);
267 /* configure the direction of data transfer on the channel */
268 void dma_transfer_direction_config(uint32_t dma_periph, dma_channel_enum channelx, uint8_t direction);
269 
270 /* flag and interrupt functions */
271 /* check DMA flag is set or not */
272 FlagStatus dma_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
273 /* clear the flag of a DMA channel */
274 void dma_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
275 /* check DMA flag and interrupt enable bit is set or not */
276 FlagStatus dma_interrupt_flag_get(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
277 /* clear the interrupt flag of a DMA channel */
278 void dma_interrupt_flag_clear(uint32_t dma_periph, dma_channel_enum channelx, uint32_t flag);
279 /* enable DMA interrupt */
280 void dma_interrupt_enable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source);
281 /* disable DMA interrupt */
282 void dma_interrupt_disable(uint32_t dma_periph, dma_channel_enum channelx, uint32_t source);
283 
284 #endif /* GD32VF103_DMA_H */
285