1 /*!
2     \file    gd32f4xx_timer.h
3     \brief   definitions for the TIMER
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_TIMER_H
39 #define GD32F4XX_TIMER_H
40 
41 #include "gd32f4xx.h"
42 
43 /* TIMERx(x=0..13) definitions */
44 #define TIMER0                           (TIMER_BASE + 0x00010000U)
45 #define TIMER1                           (TIMER_BASE + 0x00000000U)
46 #define TIMER2                           (TIMER_BASE + 0x00000400U)
47 #define TIMER3                           (TIMER_BASE + 0x00000800U)
48 #define TIMER4                           (TIMER_BASE + 0x00000C00U)
49 #define TIMER5                           (TIMER_BASE + 0x00001000U)
50 #define TIMER6                           (TIMER_BASE + 0x00001400U)
51 #define TIMER7                           (TIMER_BASE + 0x00010400U)
52 #define TIMER8                           (TIMER_BASE + 0x00014000U)
53 #define TIMER9                           (TIMER_BASE + 0x00014400U)
54 #define TIMER10                          (TIMER_BASE + 0x00014800U)
55 #define TIMER11                          (TIMER_BASE + 0x00001800U)
56 #define TIMER12                          (TIMER_BASE + 0x00001C00U)
57 #define TIMER13                          (TIMER_BASE + 0x00002000U)
58 
59 /* registers definitions */
60 #define TIMER_CTL0(timerx)               REG32((timerx) + 0x00U)           /*!< TIMER control register 0 */
61 #define TIMER_CTL1(timerx)               REG32((timerx) + 0x04U)           /*!< TIMER control register 1 */
62 #define TIMER_SMCFG(timerx)              REG32((timerx) + 0x08U)           /*!< TIMER slave mode configuration register */
63 #define TIMER_DMAINTEN(timerx)           REG32((timerx) + 0x0CU)           /*!< TIMER DMA and interrupt enable register */
64 #define TIMER_INTF(timerx)               REG32((timerx) + 0x10U)           /*!< TIMER interrupt flag register */
65 #define TIMER_SWEVG(timerx)              REG32((timerx) + 0x14U)           /*!< TIMER software event generation register */
66 #define TIMER_CHCTL0(timerx)             REG32((timerx) + 0x18U)           /*!< TIMER channel control register 0 */
67 #define TIMER_CHCTL1(timerx)             REG32((timerx) + 0x1CU)           /*!< TIMER channel control register 1 */
68 #define TIMER_CHCTL2(timerx)             REG32((timerx) + 0x20U)           /*!< TIMER channel control register 2 */
69 #define TIMER_CNT(timerx)                REG32((timerx) + 0x24U)           /*!< TIMER counter register */
70 #define TIMER_PSC(timerx)                REG32((timerx) + 0x28U)           /*!< TIMER prescaler register */
71 #define TIMER_CAR(timerx)                REG32((timerx) + 0x2CU)           /*!< TIMER counter auto reload register */
72 #define TIMER_CREP(timerx)               REG32((timerx) + 0x30U)           /*!< TIMER counter repetition register */
73 #define TIMER_CH0CV(timerx)              REG32((timerx) + 0x34U)           /*!< TIMER channel 0 capture/compare value register */
74 #define TIMER_CH1CV(timerx)              REG32((timerx) + 0x38U)           /*!< TIMER channel 1 capture/compare value register */
75 #define TIMER_CH2CV(timerx)              REG32((timerx) + 0x3CU)           /*!< TIMER channel 2 capture/compare value register */
76 #define TIMER_CH3CV(timerx)              REG32((timerx) + 0x40U)           /*!< TIMER channel 3 capture/compare value register */
77 #define TIMER_CCHP(timerx)               REG32((timerx) + 0x44U)           /*!< TIMER complementary channel protection register */
78 #define TIMER_DMACFG(timerx)             REG32((timerx) + 0x48U)           /*!< TIMER DMA configuration register */
79 #define TIMER_DMATB(timerx)              REG32((timerx) + 0x4CU)           /*!< TIMER DMA transfer buffer register */
80 #define TIMER_IRMP(timerx)               REG32((timerx) + 0x50U)           /*!< TIMER channel input remap register */
81 #define TIMER_CFG(timerx)                REG32((timerx) + 0xFCU)           /*!< TIMER configuration register */
82 
83 /* bits definitions */
84 /* TIMER_CTL0 */
85 #define TIMER_CTL0_CEN                   BIT(0)              /*!< TIMER counter enable */
86 #define TIMER_CTL0_UPDIS                 BIT(1)              /*!< update disable */
87 #define TIMER_CTL0_UPS                   BIT(2)              /*!< update source */
88 #define TIMER_CTL0_SPM                   BIT(3)              /*!< single pulse mode */
89 #define TIMER_CTL0_DIR                   BIT(4)              /*!< timer counter direction */
90 #define TIMER_CTL0_CAM                   BITS(5,6)           /*!< center-aligned mode selection */
91 #define TIMER_CTL0_ARSE                  BIT(7)              /*!< auto-reload shadow enable */
92 #define TIMER_CTL0_CKDIV                 BITS(8,9)           /*!< clock division */
93 
94 /* TIMER_CTL1 */
95 #define TIMER_CTL1_CCSE                  BIT(0)              /*!< commutation control shadow enable */
96 #define TIMER_CTL1_CCUC                  BIT(2)              /*!< commutation control shadow register update control */
97 #define TIMER_CTL1_DMAS                  BIT(3)              /*!< DMA request source selection */
98 #define TIMER_CTL1_MMC                   BITS(4,6)           /*!< master mode control */
99 #define TIMER_CTL1_TI0S                  BIT(7)              /*!< channel 0 trigger input selection(hall mode selection) */
100 #define TIMER_CTL1_ISO0                  BIT(8)              /*!< idle state of channel 0 output */
101 #define TIMER_CTL1_ISO0N                 BIT(9)              /*!< idle state of channel 0 complementary output */
102 #define TIMER_CTL1_ISO1                  BIT(10)             /*!< idle state of channel 1 output */
103 #define TIMER_CTL1_ISO1N                 BIT(11)             /*!< idle state of channel 1 complementary output */
104 #define TIMER_CTL1_ISO2                  BIT(12)             /*!< idle state of channel 2 output */
105 #define TIMER_CTL1_ISO2N                 BIT(13)             /*!< idle state of channel 2 complementary output */
106 #define TIMER_CTL1_ISO3                  BIT(14)             /*!< idle state of channel 3 output */
107 
108 /* TIMER_SMCFG */
109 #define TIMER_SMCFG_SMC                  BITS(0,2)           /*!< slave mode control */
110 #define TIMER_SMCFG_TRGS                 BITS(4,6)           /*!< trigger selection */
111 #define TIMER_SMCFG_MSM                  BIT(7)              /*!< master-slave mode */
112 #define TIMER_SMCFG_ETFC                 BITS(8,11)          /*!< external trigger filter control */
113 #define TIMER_SMCFG_ETPSC                BITS(12,13)         /*!< external trigger prescaler */
114 #define TIMER_SMCFG_SMC1                 BIT(14)             /*!< part of SMC for enable external clock mode 1 */
115 #define TIMER_SMCFG_ETP                  BIT(15)             /*!< external trigger polarity */
116 
117 /* TIMER_DMAINTEN */
118 #define TIMER_DMAINTEN_UPIE              BIT(0)              /*!< update interrupt enable */
119 #define TIMER_DMAINTEN_CH0IE             BIT(1)              /*!< channel 0 capture/compare interrupt enable */
120 #define TIMER_DMAINTEN_CH1IE             BIT(2)              /*!< channel 1 capture/compare interrupt enable */
121 #define TIMER_DMAINTEN_CH2IE             BIT(3)              /*!< channel 2 capture/compare interrupt enable */
122 #define TIMER_DMAINTEN_CH3IE             BIT(4)              /*!< channel 3 capture/compare interrupt enable */
123 #define TIMER_DMAINTEN_CMTIE             BIT(5)              /*!< commutation interrupt request enable */
124 #define TIMER_DMAINTEN_TRGIE             BIT(6)              /*!< trigger interrupt enable */
125 #define TIMER_DMAINTEN_BRKIE             BIT(7)              /*!< break interrupt enable */
126 #define TIMER_DMAINTEN_UPDEN             BIT(8)              /*!< update DMA request enable */
127 #define TIMER_DMAINTEN_CH0DEN            BIT(9)              /*!< channel 0 DMA request enable */
128 #define TIMER_DMAINTEN_CH1DEN            BIT(10)             /*!< channel 1 DMA request enable */
129 #define TIMER_DMAINTEN_CH2DEN            BIT(11)             /*!< channel 2 DMA request enable */
130 #define TIMER_DMAINTEN_CH3DEN            BIT(12)             /*!< channel 3 DMA request enable */
131 #define TIMER_DMAINTEN_CMTDEN            BIT(13)             /*!< commutation DMA request enable */
132 #define TIMER_DMAINTEN_TRGDEN            BIT(14)             /*!< trigger DMA request enable */
133 
134 /* TIMER_INTF */
135 #define TIMER_INTF_UPIF                  BIT(0)              /*!< update interrupt flag */
136 #define TIMER_INTF_CH0IF                 BIT(1)              /*!< channel 0 capture/compare interrupt flag */
137 #define TIMER_INTF_CH1IF                 BIT(2)              /*!< channel 1 capture/compare interrupt flag */
138 #define TIMER_INTF_CH2IF                 BIT(3)              /*!< channel 2 capture/compare interrupt flag */
139 #define TIMER_INTF_CH3IF                 BIT(4)              /*!< channel 3 capture/compare interrupt flag */
140 #define TIMER_INTF_CMTIF                 BIT(5)              /*!< channel commutation interrupt flag */
141 #define TIMER_INTF_TRGIF                 BIT(6)              /*!< trigger interrupt flag */
142 #define TIMER_INTF_BRKIF                 BIT(7)              /*!< break interrupt flag */
143 #define TIMER_INTF_CH0OF                 BIT(9)              /*!< channel 0 overcapture flag */
144 #define TIMER_INTF_CH1OF                 BIT(10)             /*!< channel 1 overcapture flag */
145 #define TIMER_INTF_CH2OF                 BIT(11)             /*!< channel 2 overcapture flag */
146 #define TIMER_INTF_CH3OF                 BIT(12)             /*!< channel 3 overcapture flag */
147 
148 /* TIMER_SWEVG */
149 #define TIMER_SWEVG_UPG                  BIT(0)              /*!< update event generate */
150 #define TIMER_SWEVG_CH0G                 BIT(1)              /*!< channel 0 capture or compare event generation */
151 #define TIMER_SWEVG_CH1G                 BIT(2)              /*!< channel 1 capture or compare event generation */
152 #define TIMER_SWEVG_CH2G                 BIT(3)              /*!< channel 2 capture or compare event generation */
153 #define TIMER_SWEVG_CH3G                 BIT(4)              /*!< channel 3 capture or compare event generation */
154 #define TIMER_SWEVG_CMTG                 BIT(5)              /*!< channel commutation event generation */
155 #define TIMER_SWEVG_TRGG                 BIT(6)              /*!< trigger event generation */
156 #define TIMER_SWEVG_BRKG                 BIT(7)              /*!< break event generation */
157 
158 /* TIMER_CHCTL0 */
159 /* output compare mode */
160 #define TIMER_CHCTL0_CH0MS               BITS(0,1)           /*!< channel 0 mode selection */
161 #define TIMER_CHCTL0_CH0COMFEN           BIT(2)              /*!< channel 0 output compare fast enable */
162 #define TIMER_CHCTL0_CH0COMSEN           BIT(3)              /*!< channel 0 output compare shadow enable */
163 #define TIMER_CHCTL0_CH0COMCTL           BITS(4,6)           /*!< channel 0 output compare mode */
164 #define TIMER_CHCTL0_CH0COMCEN           BIT(7)              /*!< channel 0 output compare clear enable */
165 #define TIMER_CHCTL0_CH1MS               BITS(8,9)           /*!< channel 1 mode selection */
166 #define TIMER_CHCTL0_CH1COMFEN           BIT(10)             /*!< channel 1 output compare fast enable */
167 #define TIMER_CHCTL0_CH1COMSEN           BIT(11)             /*!< channel 1 output compare shadow enable */
168 #define TIMER_CHCTL0_CH1COMCTL           BITS(12,14)         /*!< channel 1 output compare mode */
169 #define TIMER_CHCTL0_CH1COMCEN           BIT(15)             /*!< channel 1 output compare clear enable */
170 /* input capture mode */
171 #define TIMER_CHCTL0_CH0CAPPSC           BITS(2,3)           /*!< channel 0 input capture prescaler */
172 #define TIMER_CHCTL0_CH0CAPFLT           BITS(4,7)           /*!< channel 0 input capture filter control */
173 #define TIMER_CHCTL0_CH1CAPPSC           BITS(10,11)         /*!< channel 1 input capture prescaler */
174 #define TIMER_CHCTL0_CH1CAPFLT           BITS(12,15)         /*!< channel 1 input capture filter control */
175 
176 /* TIMER_CHCTL1 */
177 /* output compare mode */
178 #define TIMER_CHCTL1_CH2MS               BITS(0,1)           /*!< channel 2 mode selection */
179 #define TIMER_CHCTL1_CH2COMFEN           BIT(2)              /*!< channel 2 output compare fast enable */
180 #define TIMER_CHCTL1_CH2COMSEN           BIT(3)              /*!< channel 2 output compare shadow enable */
181 #define TIMER_CHCTL1_CH2COMCTL           BITS(4,6)           /*!< channel 2 output compare mode */
182 #define TIMER_CHCTL1_CH2COMCEN           BIT(7)              /*!< channel 2 output compare clear enable */
183 #define TIMER_CHCTL1_CH3MS               BITS(8,9)           /*!< channel 3 mode selection */
184 #define TIMER_CHCTL1_CH3COMFEN           BIT(10)             /*!< channel 3 output compare fast enable */
185 #define TIMER_CHCTL1_CH3COMSEN           BIT(11)             /*!< channel 3 output compare shadow enable */
186 #define TIMER_CHCTL1_CH3COMCTL           BITS(12,14)         /*!< channel 3 output compare mode */
187 #define TIMER_CHCTL1_CH3COMCEN           BIT(15)             /*!< channel 3 output compare clear enable */
188 /* input capture mode */
189 #define TIMER_CHCTL1_CH2CAPPSC           BITS(2,3)           /*!< channel 2 input capture prescaler */
190 #define TIMER_CHCTL1_CH2CAPFLT           BITS(4,7)           /*!< channel 2 input capture filter control */
191 #define TIMER_CHCTL1_CH3CAPPSC           BITS(10,11)         /*!< channel 3 input capture prescaler */
192 #define TIMER_CHCTL1_CH3CAPFLT           BITS(12,15)         /*!< channel 3 input capture filter control */
193 
194 /* TIMER_CHCTL2 */
195 #define TIMER_CHCTL2_CH0EN               BIT(0)              /*!< channel 0 capture/compare function enable */
196 #define TIMER_CHCTL2_CH0P                BIT(1)              /*!< channel 0 capture/compare function polarity */
197 #define TIMER_CHCTL2_CH0NEN              BIT(2)              /*!< channel 0 complementary output enable */
198 #define TIMER_CHCTL2_CH0NP               BIT(3)              /*!< channel 0 complementary output polarity */
199 #define TIMER_CHCTL2_CH1EN               BIT(4)              /*!< channel 1 capture/compare function enable  */
200 #define TIMER_CHCTL2_CH1P                BIT(5)              /*!< channel 1 capture/compare function polarity */
201 #define TIMER_CHCTL2_CH1NEN              BIT(6)              /*!< channel 1 complementary output enable */
202 #define TIMER_CHCTL2_CH1NP               BIT(7)              /*!< channel 1 complementary output polarity */
203 #define TIMER_CHCTL2_CH2EN               BIT(8)              /*!< channel 2 capture/compare function enable  */
204 #define TIMER_CHCTL2_CH2P                BIT(9)              /*!< channel 2 capture/compare function polarity */
205 #define TIMER_CHCTL2_CH2NEN              BIT(10)             /*!< channel 2 complementary output enable */
206 #define TIMER_CHCTL2_CH2NP               BIT(11)             /*!< channel 2 complementary output polarity */
207 #define TIMER_CHCTL2_CH3EN               BIT(12)             /*!< channel 3 capture/compare function enable  */
208 #define TIMER_CHCTL2_CH3P                BIT(13)             /*!< channel 3 capture/compare function polarity */
209 
210 /* TIMER_CNT */
211 #define TIMER_CNT_CNT16                  BITS(0,15)          /*!< 16 bit timer counter */
212 #define TIMER_CNT_CNT32                  BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) timer counter */
213 
214 /* TIMER_PSC */
215 #define TIMER_PSC_PSC                    BITS(0,15)          /*!< prescaler value of the counter clock */
216 
217 /* TIMER_CAR */
218 #define TIMER_CAR_CARL16                 BITS(0,15)          /*!< 16 bit counter auto reload value */
219 #define TIMER_CAR_CARL32                 BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) counter auto reload value */
220 
221 /* TIMER_CREP */
222 #define TIMER_CREP_CREP                  BITS(0,7)           /*!< counter repetition value */
223 
224 /* TIMER_CH0CV */
225 #define TIMER_CH0CV_CH0VAL16             BITS(0,15)          /*!< 16 bit capture/compare value of channel 0 */
226 #define TIMER_CH0CV_CH0VAL32             BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) capture/compare value of channel 0 */
227 
228 /* TIMER_CH1CV */
229 #define TIMER_CH1CV_CH1VAL16             BITS(0,15)          /*!< 16 bit capture/compare value of channel 1 */
230 #define TIMER_CH1CV_CH1VAL32             BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) capture/compare value of channel 1 */
231 
232 /* TIMER_CH2CV */
233 #define TIMER_CH2CV_CH2VAL16             BITS(0,15)          /*!< 16 bit capture/compare value of channel 2 */
234 #define TIMER_CH2CV_CH2VAL32             BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) capture/compare value of channel 2 */
235 
236 /* TIMER_CH3CV */
237 #define TIMER_CH3CV_CH3VAL16             BITS(0,15)          /*!< 16 bit capture/compare value of channel 3 */
238 #define TIMER_CH3CV_CH3VAL32             BITS(0,31)          /*!< 32 bit(TIMER1,TIMER4) capture/compare value of channel 3 */
239 
240 /* TIMER_CCHP */
241 #define TIMER_CCHP_DTCFG                 BITS(0,7)           /*!< dead time configure */
242 #define TIMER_CCHP_PROT                  BITS(8,9)           /*!< complementary register protect control */
243 #define TIMER_CCHP_IOS                   BIT(10)             /*!< idle mode off-state configure */
244 #define TIMER_CCHP_ROS                   BIT(11)             /*!< run mode off-state configure */
245 #define TIMER_CCHP_BRKEN                 BIT(12)             /*!< break enable */
246 #define TIMER_CCHP_BRKP                  BIT(13)             /*!< break polarity */
247 #define TIMER_CCHP_OAEN                  BIT(14)             /*!< output automatic enable */
248 #define TIMER_CCHP_POEN                  BIT(15)             /*!< primary output enable */
249 
250 /* TIMER_DMACFG */
251 #define TIMER_DMACFG_DMATA               BITS(0,4)           /*!< DMA transfer access start address */
252 #define TIMER_DMACFG_DMATC               BITS(8,12)          /*!< DMA transfer count */
253 
254 /* TIMER_DMATB */
255 #define TIMER_DMATB_DMATB                BITS(0,15)          /*!< DMA transfer buffer address */
256 
257 /* TIMER_IRMP */
258 #define TIMER1_IRMP_ITI1_RMP             BITS(10,11)         /*!< TIMER1 internal trigger input 1 remap */
259 #define TIMER4_IRMP_CI3_RMP              BITS(6,7)           /*!< TIMER4 channel 3 input remap */
260 #define TIMER10_IRMP_ITI1_RMP            BITS(0,1)           /*!< TIMER10 internal trigger input 1 remap */
261 
262 /* TIMER_CFG */
263 #define TIMER_CFG_OUTSEL                 BIT(0)              /*!< the output value selection */
264 #define TIMER_CFG_CHVSEL                 BIT(1)              /*!< write CHxVAL register selection */
265 
266 /* constants definitions */
267 /* TIMER init parameter struct definitions*/
268 typedef struct
269 {
270     uint16_t prescaler;                         /*!< prescaler value */
271     uint16_t alignedmode;                       /*!< aligned mode */
272     uint16_t counterdirection;                  /*!< counter direction */
273     uint16_t clockdivision;                     /*!< clock division value */
274     uint32_t period;                            /*!< period value */
275     uint8_t  repetitioncounter;                 /*!< the counter repetition value */
276 }timer_parameter_struct;
277 
278 /* break parameter struct definitions*/
279 typedef struct
280 {
281     uint16_t runoffstate;                       /*!< run mode off-state */
282     uint16_t ideloffstate;                      /*!< idle mode off-state */
283     uint16_t deadtime;                          /*!< dead time */
284     uint16_t breakpolarity;                     /*!< break polarity */
285     uint16_t outputautostate;                   /*!< output automatic enable */
286     uint16_t protectmode;                       /*!< complementary register protect control */
287     uint16_t breakstate;                        /*!< break enable */
288 }timer_break_parameter_struct;
289 
290 /* channel output parameter struct definitions */
291 typedef struct
292 {
293     uint16_t outputstate;                       /*!< channel output state */
294     uint16_t outputnstate;                      /*!< channel complementary output state */
295     uint16_t ocpolarity;                        /*!< channel output polarity */
296     uint16_t ocnpolarity;                       /*!< channel complementary output polarity */
297     uint16_t ocidlestate;                       /*!< idle state of channel output */
298     uint16_t ocnidlestate;                      /*!< idle state of channel complementary output */
299 }timer_oc_parameter_struct;
300 
301 /* channel input parameter struct definitions */
302 typedef struct
303 {
304     uint16_t icpolarity;                        /*!< channel input polarity */
305     uint16_t icselection;                       /*!< channel input mode selection */
306     uint16_t icprescaler;                       /*!< channel input capture prescaler */
307     uint16_t icfilter;                          /*!< channel input capture filter control */
308 }timer_ic_parameter_struct;
309 
310 /* TIMER interrupt enable or disable */
311 #define TIMER_INT_UP                        TIMER_DMAINTEN_UPIE                     /*!< update interrupt */
312 #define TIMER_INT_CH0                       TIMER_DMAINTEN_CH0IE                    /*!< channel 0 interrupt */
313 #define TIMER_INT_CH1                       TIMER_DMAINTEN_CH1IE                    /*!< channel 1 interrupt */
314 #define TIMER_INT_CH2                       TIMER_DMAINTEN_CH2IE                    /*!< channel 2 interrupt */
315 #define TIMER_INT_CH3                       TIMER_DMAINTEN_CH3IE                    /*!< channel 3 interrupt */
316 #define TIMER_INT_CMT                       TIMER_DMAINTEN_CMTIE                    /*!< channel commutation interrupt flag */
317 #define TIMER_INT_TRG                       TIMER_DMAINTEN_TRGIE                    /*!< trigger interrupt */
318 #define TIMER_INT_BRK                       TIMER_DMAINTEN_BRKIE                    /*!< break interrupt */
319 
320 /* TIMER flag */
321 #define TIMER_FLAG_UP                       TIMER_INTF_UPIF                         /*!< update flag */
322 #define TIMER_FLAG_CH0                      TIMER_INTF_CH0IF                        /*!< channel 0 flag */
323 #define TIMER_FLAG_CH1                      TIMER_INTF_CH1IF                        /*!< channel 1 flag */
324 #define TIMER_FLAG_CH2                      TIMER_INTF_CH2IF                        /*!< channel 2 flag */
325 #define TIMER_FLAG_CH3                      TIMER_INTF_CH3IF                        /*!< channel 3 flag */
326 #define TIMER_FLAG_CMT                      TIMER_INTF_CMTIF                        /*!< channel commutation flag */
327 #define TIMER_FLAG_TRG                      TIMER_INTF_TRGIF                        /*!< trigger flag */
328 #define TIMER_FLAG_BRK                      TIMER_INTF_BRKIF                        /*!< break flag */
329 #define TIMER_FLAG_CH0O                     TIMER_INTF_CH0OF                        /*!< channel 0 overcapture flag */
330 #define TIMER_FLAG_CH1O                     TIMER_INTF_CH1OF                        /*!< channel 1 overcapture flag */
331 #define TIMER_FLAG_CH2O                     TIMER_INTF_CH2OF                        /*!< channel 2 overcapture flag */
332 #define TIMER_FLAG_CH3O                     TIMER_INTF_CH3OF                        /*!< channel 3 overcapture flag */
333 
334 /* TIMER interrupt flag */
335 #define TIMER_INT_FLAG_UP                   TIMER_INTF_UPIF                         /*!< update interrupt flag */
336 #define TIMER_INT_FLAG_CH0                  TIMER_INTF_CH0IF                        /*!< channel 0 interrupt flag */
337 #define TIMER_INT_FLAG_CH1                  TIMER_INTF_CH1IF                        /*!< channel 1 interrupt flag */
338 #define TIMER_INT_FLAG_CH2                  TIMER_INTF_CH2IF                        /*!< channel 2 interrupt flag */
339 #define TIMER_INT_FLAG_CH3                  TIMER_INTF_CH3IF                        /*!< channel 3 interrupt flag */
340 #define TIMER_INT_FLAG_CMT                  TIMER_INTF_CMTIF                        /*!< channel commutation interrupt flag */
341 #define TIMER_INT_FLAG_TRG                  TIMER_INTF_TRGIF                        /*!< trigger interrupt flag */
342 #define TIMER_INT_FLAG_BRK                  TIMER_INTF_BRKIF
343 
344 /* TIMER DMA source enable */
345 #define TIMER_DMA_UPD                       ((uint16_t)TIMER_DMAINTEN_UPDEN)        /*!< update DMA enable */
346 #define TIMER_DMA_CH0D                      ((uint16_t)TIMER_DMAINTEN_CH0DEN)       /*!< channel 0 DMA enable */
347 #define TIMER_DMA_CH1D                      ((uint16_t)TIMER_DMAINTEN_CH1DEN)       /*!< channel 1 DMA enable */
348 #define TIMER_DMA_CH2D                      ((uint16_t)TIMER_DMAINTEN_CH2DEN)       /*!< channel 2 DMA enable */
349 #define TIMER_DMA_CH3D                      ((uint16_t)TIMER_DMAINTEN_CH3DEN)       /*!< channel 3 DMA enable */
350 #define TIMER_DMA_CMTD                      ((uint16_t)TIMER_DMAINTEN_CMTDEN)       /*!< commutation DMA request enable */
351 #define TIMER_DMA_TRGD                      ((uint16_t)TIMER_DMAINTEN_TRGDEN)       /*!< trigger DMA enable */
352 
353 /* channel DMA request source selection */
354 #define TIMER_DMAREQUEST_UPDATEEVENT        ((uint8_t)0x00U)                        /*!< DMA request of channel y is sent when update event occurs */
355 #define TIMER_DMAREQUEST_CHANNELEVENT       ((uint8_t)0x01U)                        /*!< DMA request of channel y is sent when channel y event occurs */
356 
357 /* DMA access base address */
358 #define DMACFG_DMATA(regval)                (BITS(0, 4) & ((uint32_t)(regval) << 0U))
359 #define TIMER_DMACFG_DMATA_CTL0             DMACFG_DMATA(0)                         /*!< DMA transfer address is TIMER_CTL0 */
360 #define TIMER_DMACFG_DMATA_CTL1             DMACFG_DMATA(1)                         /*!< DMA transfer address is TIMER_CTL1 */
361 #define TIMER_DMACFG_DMATA_SMCFG            DMACFG_DMATA(2)                         /*!< DMA transfer address is TIMER_SMCFG */
362 #define TIMER_DMACFG_DMATA_DMAINTEN         DMACFG_DMATA(3)                         /*!< DMA transfer address is TIMER_DMAINTEN */
363 #define TIMER_DMACFG_DMATA_INTF             DMACFG_DMATA(4)                         /*!< DMA transfer address is TIMER_INTF */
364 #define TIMER_DMACFG_DMATA_SWEVG            DMACFG_DMATA(5)                         /*!< DMA transfer address is TIMER_SWEVG */
365 #define TIMER_DMACFG_DMATA_CHCTL0           DMACFG_DMATA(6)                         /*!< DMA transfer address is TIMER_CHCTL0 */
366 #define TIMER_DMACFG_DMATA_CHCTL1           DMACFG_DMATA(7)                         /*!< DMA transfer address is TIMER_CHCTL1 */
367 #define TIMER_DMACFG_DMATA_CHCTL2           DMACFG_DMATA(8)                         /*!< DMA transfer address is TIMER_CHCTL2 */
368 #define TIMER_DMACFG_DMATA_CNT              DMACFG_DMATA(9)                         /*!< DMA transfer address is TIMER_CNT */
369 #define TIMER_DMACFG_DMATA_PSC              DMACFG_DMATA(10)                        /*!< DMA transfer address is TIMER_PSC */
370 #define TIMER_DMACFG_DMATA_CAR              DMACFG_DMATA(11)                        /*!< DMA transfer address is TIMER_CAR */
371 #define TIMER_DMACFG_DMATA_CREP             DMACFG_DMATA(12)                        /*!< DMA transfer address is TIMER_CREP */
372 #define TIMER_DMACFG_DMATA_CH0CV            DMACFG_DMATA(13)                        /*!< DMA transfer address is TIMER_CH0CV */
373 #define TIMER_DMACFG_DMATA_CH1CV            DMACFG_DMATA(14)                        /*!< DMA transfer address is TIMER_CH1CV */
374 #define TIMER_DMACFG_DMATA_CH2CV            DMACFG_DMATA(15)                        /*!< DMA transfer address is TIMER_CH2CV */
375 #define TIMER_DMACFG_DMATA_CH3CV            DMACFG_DMATA(16)                        /*!< DMA transfer address is TIMER_CH3CV */
376 #define TIMER_DMACFG_DMATA_CCHP             DMACFG_DMATA(17)                        /*!< DMA transfer address is TIMER_CCHP */
377 #define TIMER_DMACFG_DMATA_DMACFG           DMACFG_DMATA(18)                        /*!< DMA transfer address is TIMER_DMACFG */
378 #define TIMER_DMACFG_DMATA_DMATB            DMACFG_DMATA(19)                        /*!< DMA transfer address is TIMER_DMATB */
379 
380 /* DMA access burst length */
381 #define DMACFG_DMATC(regval)                (BITS(8, 12) & ((uint32_t)(regval) << 8U))
382 #define TIMER_DMACFG_DMATC_1TRANSFER        DMACFG_DMATC(0)                         /*!< DMA transfer 1 time */
383 #define TIMER_DMACFG_DMATC_2TRANSFER        DMACFG_DMATC(1)                         /*!< DMA transfer 2 times */
384 #define TIMER_DMACFG_DMATC_3TRANSFER        DMACFG_DMATC(2)                         /*!< DMA transfer 3 times */
385 #define TIMER_DMACFG_DMATC_4TRANSFER        DMACFG_DMATC(3)                         /*!< DMA transfer 4 times */
386 #define TIMER_DMACFG_DMATC_5TRANSFER        DMACFG_DMATC(4)                         /*!< DMA transfer 5 times */
387 #define TIMER_DMACFG_DMATC_6TRANSFER        DMACFG_DMATC(5)                         /*!< DMA transfer 6 times */
388 #define TIMER_DMACFG_DMATC_7TRANSFER        DMACFG_DMATC(6)                         /*!< DMA transfer 7 times */
389 #define TIMER_DMACFG_DMATC_8TRANSFER        DMACFG_DMATC(7)                         /*!< DMA transfer 8 times */
390 #define TIMER_DMACFG_DMATC_9TRANSFER        DMACFG_DMATC(8)                         /*!< DMA transfer 9 times */
391 #define TIMER_DMACFG_DMATC_10TRANSFER       DMACFG_DMATC(9)                         /*!< DMA transfer 10 times */
392 #define TIMER_DMACFG_DMATC_11TRANSFER       DMACFG_DMATC(10)                        /*!< DMA transfer 11 times */
393 #define TIMER_DMACFG_DMATC_12TRANSFER       DMACFG_DMATC(11)                        /*!< DMA transfer 12 times */
394 #define TIMER_DMACFG_DMATC_13TRANSFER       DMACFG_DMATC(12)                        /*!< DMA transfer 13 times */
395 #define TIMER_DMACFG_DMATC_14TRANSFER       DMACFG_DMATC(13)                        /*!< DMA transfer 14 times */
396 #define TIMER_DMACFG_DMATC_15TRANSFER       DMACFG_DMATC(14)                        /*!< DMA transfer 15 times */
397 #define TIMER_DMACFG_DMATC_16TRANSFER       DMACFG_DMATC(15)                        /*!< DMA transfer 16 times */
398 #define TIMER_DMACFG_DMATC_17TRANSFER       DMACFG_DMATC(16)                        /*!< DMA transfer 17 times */
399 #define TIMER_DMACFG_DMATC_18TRANSFER       DMACFG_DMATC(17)                        /*!< DMA transfer 18 times */
400 
401 /* TIMER software event generation source */
402 #define TIMER_EVENT_SRC_UPG                 ((uint16_t)0x0001U)                     /*!< update event generation */
403 #define TIMER_EVENT_SRC_CH0G                ((uint16_t)0x0002U)                     /*!< channel 0 capture or compare event generation */
404 #define TIMER_EVENT_SRC_CH1G                ((uint16_t)0x0004U)                     /*!< channel 1 capture or compare event generation */
405 #define TIMER_EVENT_SRC_CH2G                ((uint16_t)0x0008U)                     /*!< channel 2 capture or compare event generation */
406 #define TIMER_EVENT_SRC_CH3G                ((uint16_t)0x0010U)                     /*!< channel 3 capture or compare event generation */
407 #define TIMER_EVENT_SRC_CMTG                ((uint16_t)0x0020U)                     /*!< channel commutation event generation */
408 #define TIMER_EVENT_SRC_TRGG                ((uint16_t)0x0040U)                     /*!< trigger event generation */
409 #define TIMER_EVENT_SRC_BRKG                ((uint16_t)0x0080U)                     /*!< break event generation */
410 
411 /* center-aligned mode selection */
412 #define CTL0_CAM(regval)                    ((uint16_t)(BITS(5, 6) & ((uint32_t)(regval) << 5U)))
413 #define TIMER_COUNTER_EDGE                  CTL0_CAM(0)                             /*!< edge-aligned mode */
414 #define TIMER_COUNTER_CENTER_DOWN           CTL0_CAM(1)                             /*!< center-aligned and counting down assert mode */
415 #define TIMER_COUNTER_CENTER_UP             CTL0_CAM(2)                             /*!< center-aligned and counting up assert mode */
416 #define TIMER_COUNTER_CENTER_BOTH           CTL0_CAM(3)                             /*!< center-aligned and counting up/down assert mode */
417 
418 /* TIMER prescaler reload mode */
419 #define TIMER_PSC_RELOAD_NOW                ((uint32_t)0x00000000U)                 /*!< the prescaler is loaded right now */
420 #define TIMER_PSC_RELOAD_UPDATE             ((uint32_t)0x00000001U)                 /*!< the prescaler is loaded at the next update event */
421 
422 /* count direction */
423 #define TIMER_COUNTER_UP                    ((uint16_t)0x0000U)                     /*!< counter up direction */
424 #define TIMER_COUNTER_DOWN                  ((uint16_t)TIMER_CTL0_DIR)              /*!< counter down direction */
425 
426 /* specify division ratio between TIMER clock and dead-time and sampling clock */
427 #define CTL0_CKDIV(regval)                  ((uint16_t)(BITS(8, 9) & ((uint32_t)(regval) << 8U)))
428 #define TIMER_CKDIV_DIV1                    CTL0_CKDIV(0)                           /*!< clock division value is 1,fDTS=fTIMER_CK */
429 #define TIMER_CKDIV_DIV2                    CTL0_CKDIV(1)                           /*!< clock division value is 2,fDTS= fTIMER_CK/2 */
430 #define TIMER_CKDIV_DIV4                    CTL0_CKDIV(2)                           /*!< clock division value is 4, fDTS= fTIMER_CK/4 */
431 
432 /* single pulse mode */
433 #define TIMER_SP_MODE_SINGLE                ((uint32_t)0x00000000U)                 /*!< single pulse mode */
434 #define TIMER_SP_MODE_REPETITIVE            ((uint32_t)0x00000001U)                 /*!< repetitive pulse mode */
435 
436 /* update source */
437 #define TIMER_UPDATE_SRC_REGULAR            ((uint32_t)0x00000000U)                 /*!< update generate only by counter overflow/underflow */
438 #define TIMER_UPDATE_SRC_GLOBAL             ((uint32_t)0x00000001U)                 /*!< update generate by setting of UPG bit or the counter overflow/underflow,or the slave mode controller trigger */
439 
440 /* run mode off-state configure */
441 #define TIMER_ROS_STATE_ENABLE              ((uint16_t)TIMER_CCHP_ROS)              /*!< when POEN bit is set, the channel output signals (CHx_O/CHx_ON) are enabled, with relationship to CHxEN/CHxNEN bits */
442 #define TIMER_ROS_STATE_DISABLE             ((uint16_t)0x0000U)                     /*!< when POEN bit is set, the channel output signals (CHx_O/CHx_ON) are disabled */
443 
444 /* idle mode off-state configure */
445 #define TIMER_IOS_STATE_ENABLE              ((uint16_t)TIMER_CCHP_IOS)              /*!< when POEN bit is reset, the channel output signals (CHx_O/CHx_ON) are enabled, with relationship to CHxEN/CHxNEN bits */
446 #define TIMER_IOS_STATE_DISABLE             ((uint16_t)0x0000U)                     /*!< when POEN bit is reset, the channel output signals (CHx_O/CHx_ON) are disabled */
447 
448 /* break input polarity */
449 #define TIMER_BREAK_POLARITY_LOW            ((uint16_t)0x0000U)                     /*!< break input polarity is low */
450 #define TIMER_BREAK_POLARITY_HIGH           ((uint16_t)TIMER_CCHP_BRKP)             /*!< break input polarity is high */
451 
452 /* output automatic enable */
453 #define TIMER_OUTAUTO_ENABLE                ((uint16_t)TIMER_CCHP_OAEN)             /*!< output automatic enable */
454 #define TIMER_OUTAUTO_DISABLE               ((uint16_t)0x0000U)                     /*!< output automatic disable */
455 
456 /* complementary register protect control */
457 #define CCHP_PROT(regval)                   ((uint16_t)(BITS(8, 9) & ((uint32_t)(regval) << 8U)))
458 #define TIMER_CCHP_PROT_OFF                 CCHP_PROT(0)                            /*!< protect disable */
459 #define TIMER_CCHP_PROT_0                   CCHP_PROT(1)                            /*!< PROT mode 0 */
460 #define TIMER_CCHP_PROT_1                   CCHP_PROT(2)                            /*!< PROT mode 1 */
461 #define TIMER_CCHP_PROT_2                   CCHP_PROT(3)                            /*!< PROT mode 2 */
462 
463 /* break input enable */
464 #define TIMER_BREAK_ENABLE                  ((uint16_t)TIMER_CCHP_BRKEN)            /*!< break input enable */
465 #define TIMER_BREAK_DISABLE                 ((uint16_t)0x0000U)                     /*!< break input disable */
466 
467 /* TIMER channel n(n=0,1,2,3) */
468 #define TIMER_CH_0                          ((uint16_t)0x0000U)                     /*!< TIMER channel 0(TIMERx(x=0..4,7..13)) */
469 #define TIMER_CH_1                          ((uint16_t)0x0001U)                     /*!< TIMER channel 1(TIMERx(x=0..4,7,8,11)) */
470 #define TIMER_CH_2                          ((uint16_t)0x0002U)                     /*!< TIMER channel 2(TIMERx(x=0..4,7)) */
471 #define TIMER_CH_3                          ((uint16_t)0x0003U)                     /*!< TIMER channel 3(TIMERx(x=0..4,7)) */
472 
473 /* channel enable state*/
474 #define TIMER_CCX_ENABLE                    ((uint32_t)0x00000001U)                 /*!< channel enable */
475 #define TIMER_CCX_DISABLE                   ((uint32_t)0x00000000U)                 /*!< channel disable */
476 
477 /* channel complementary output enable state*/
478 #define TIMER_CCXN_ENABLE                   ((uint16_t)0x0004U)                     /*!< channel complementary enable */
479 #define TIMER_CCXN_DISABLE                  ((uint16_t)0x0000U)                     /*!< channel complementary disable */
480 
481 /* channel output polarity */
482 #define TIMER_OC_POLARITY_HIGH              ((uint16_t)0x0000U)                     /*!< channel output polarity is high */
483 #define TIMER_OC_POLARITY_LOW               ((uint16_t)0x0002U)                     /*!< channel output polarity is low */
484 
485 /* channel complementary output polarity */
486 #define TIMER_OCN_POLARITY_HIGH             ((uint16_t)0x0000U)                     /*!< channel complementary output polarity is high */
487 #define TIMER_OCN_POLARITY_LOW              ((uint16_t)0x0008U)                     /*!< channel complementary output polarity is low */
488 
489 /* idle state of channel output */
490 #define TIMER_OC_IDLE_STATE_HIGH            ((uint16_t)0x0100)                      /*!< idle state of channel output is high */
491 #define TIMER_OC_IDLE_STATE_LOW             ((uint16_t)0x0000)                      /*!< idle state of channel output is low */
492 
493 /* idle state of channel complementary output */
494 #define TIMER_OCN_IDLE_STATE_HIGH           ((uint16_t)0x0200U)                     /*!< idle state of channel complementary output is high */
495 #define TIMER_OCN_IDLE_STATE_LOW            ((uint16_t)0x0000U)                     /*!< idle state of channel complementary output is low */
496 
497 /* channel output compare mode */
498 #define TIMER_OC_MODE_TIMING                ((uint16_t)0x0000U)                     /*!< timing mode */
499 #define TIMER_OC_MODE_ACTIVE                ((uint16_t)0x0010U)                     /*!< active mode */
500 #define TIMER_OC_MODE_INACTIVE              ((uint16_t)0x0020U)                     /*!< inactive mode */
501 #define TIMER_OC_MODE_TOGGLE                ((uint16_t)0x0030U)                     /*!< toggle mode */
502 #define TIMER_OC_MODE_LOW                   ((uint16_t)0x0040U)                     /*!< force low mode */
503 #define TIMER_OC_MODE_HIGH                  ((uint16_t)0x0050U)                     /*!< force high mode */
504 #define TIMER_OC_MODE_PWM0                  ((uint16_t)0x0060U)                     /*!< PWM0 mode */
505 #define TIMER_OC_MODE_PWM1                  ((uint16_t)0x0070U)                     /*!< PWM1 mode*/
506 
507 /* channel output compare shadow enable */
508 #define TIMER_OC_SHADOW_ENABLE              ((uint16_t)0x0008U)                     /*!< channel output shadow state enable */
509 #define TIMER_OC_SHADOW_DISABLE             ((uint16_t)0x0000U)                     /*!< channel output shadow state disable */
510 
511 /* channel output compare fast enable */
512 #define TIMER_OC_FAST_ENABLE                ((uint16_t)0x0004)                      /*!< channel output fast function enable */
513 #define TIMER_OC_FAST_DISABLE               ((uint16_t)0x0000)                      /*!< channel output fast function disable */
514 
515 /* channel output compare clear enable */
516 #define TIMER_OC_CLEAR_ENABLE               ((uint16_t)0x0080U)                     /*!< channel output clear function enable */
517 #define TIMER_OC_CLEAR_DISABLE              ((uint16_t)0x0000U)                     /*!< channel output clear function disable */
518 
519 /* channel control shadow register update control */
520 #define TIMER_UPDATECTL_CCU                 ((uint32_t)0x00000000U)                 /*!< the shadow registers are updated when CMTG bit is set */
521 #define TIMER_UPDATECTL_CCUTRI              ((uint32_t)0x00000001U)                        /*!< the shadow registers update by when CMTG bit is set or an rising edge of TRGI occurs */
522 
523 /* channel input capture polarity */
524 #define TIMER_IC_POLARITY_RISING            ((uint16_t)0x0000U)                     /*!< input capture rising edge */
525 #define TIMER_IC_POLARITY_FALLING           ((uint16_t)0x0002U)                     /*!< input capture falling edge */
526 #define TIMER_IC_POLARITY_BOTH_EDGE         ((uint16_t)0x000AU)                     /*!< input capture both edge */
527 
528 /* TIMER input capture selection */
529 #define TIMER_IC_SELECTION_DIRECTTI         ((uint16_t)0x0001U)                     /*!< channel y is configured as input and icy is mapped on CIy */
530 #define TIMER_IC_SELECTION_INDIRECTTI       ((uint16_t)0x0002U)                     /*!< channel y is configured as input and icy is mapped on opposite input */
531 #define TIMER_IC_SELECTION_ITS              ((uint16_t)0x0003U)                     /*!< channel y is configured as input and icy is mapped on ITS */
532 
533 /* channel input capture prescaler */
534 #define TIMER_IC_PSC_DIV1                   ((uint16_t)0x0000U)                     /*!< no prescaler */
535 #define TIMER_IC_PSC_DIV2                   ((uint16_t)0x0004U)                     /*!< divided by 2 */
536 #define TIMER_IC_PSC_DIV4                   ((uint16_t)0x0008U)                     /*!< divided by 4*/
537 #define TIMER_IC_PSC_DIV8                   ((uint16_t)0x000CU)                     /*!< divided by 8 */
538 
539 /* trigger selection */
540 #define SMCFG_TRGSEL(regval)                 (BITS(4, 6) & ((uint32_t)(regval) << 4U))
541 #define TIMER_SMCFG_TRGSEL_ITI0              SMCFG_TRGSEL(0)                        /*!< internal trigger 0 */
542 #define TIMER_SMCFG_TRGSEL_ITI1              SMCFG_TRGSEL(1)                        /*!< internal trigger 1 */
543 #define TIMER_SMCFG_TRGSEL_ITI2              SMCFG_TRGSEL(2)                        /*!< internal trigger 2 */
544 #define TIMER_SMCFG_TRGSEL_ITI3              SMCFG_TRGSEL(3)                        /*!< internal trigger 3 */
545 #define TIMER_SMCFG_TRGSEL_CI0F_ED           SMCFG_TRGSEL(4)                        /*!< TI0 Edge Detector */
546 #define TIMER_SMCFG_TRGSEL_CI0FE0            SMCFG_TRGSEL(5)                        /*!< filtered TIMER input 0 */
547 #define TIMER_SMCFG_TRGSEL_CI1FE1            SMCFG_TRGSEL(6)                        /*!< filtered TIMER input 1 */
548 #define TIMER_SMCFG_TRGSEL_ETIFP             SMCFG_TRGSEL(7)                        /*!< external trigger */
549 
550 /* master mode control */
551 #define CTL1_MMC(regval)                    (BITS(4, 6) & ((uint32_t)(regval) << 4U))
552 #define TIMER_TRI_OUT_SRC_RESET             CTL1_MMC(0)                             /*!< the UPG bit as trigger output */
553 #define TIMER_TRI_OUT_SRC_ENABLE            CTL1_MMC(1)                             /*!< the counter enable signal TIMER_CTL0_CEN as trigger output */
554 #define TIMER_TRI_OUT_SRC_UPDATE            CTL1_MMC(2)                             /*!< update event as trigger output */
555 #define TIMER_TRI_OUT_SRC_CH0               CTL1_MMC(3)                             /*!< a capture or a compare match occurred in channal0 as trigger output TRGO */
556 #define TIMER_TRI_OUT_SRC_O0CPRE            CTL1_MMC(4)                             /*!< O0CPRE as trigger output */
557 #define TIMER_TRI_OUT_SRC_O1CPRE            CTL1_MMC(5)                             /*!< O1CPRE as trigger output */
558 #define TIMER_TRI_OUT_SRC_O2CPRE            CTL1_MMC(6)                             /*!< O2CPRE as trigger output */
559 #define TIMER_TRI_OUT_SRC_O3CPRE            CTL1_MMC(7)                             /*!< O3CPRE as trigger output */
560 
561 /* slave mode control */
562 #define SMCFG_SMC(regval)                   (BITS(0, 2) & ((uint32_t)(regval) << 0U))
563 #define TIMER_SLAVE_MODE_DISABLE            SMCFG_SMC(0)                            /*!< slave mode disable */
564 #define TIMER_ENCODER_MODE0                 SMCFG_SMC(1)                            /*!< encoder mode 0 */
565 #define TIMER_ENCODER_MODE1                 SMCFG_SMC(2)                            /*!< encoder mode 1 */
566 #define TIMER_ENCODER_MODE2                 SMCFG_SMC(3)                            /*!< encoder mode 2 */
567 #define TIMER_SLAVE_MODE_RESTART            SMCFG_SMC(4)                            /*!< restart mode */
568 #define TIMER_SLAVE_MODE_PAUSE              SMCFG_SMC(5)                            /*!< pause mode */
569 #define TIMER_SLAVE_MODE_EVENT              SMCFG_SMC(6)                            /*!< event mode */
570 #define TIMER_SLAVE_MODE_EXTERNAL0          SMCFG_SMC(7)                            /*!< external clock mode 0 */
571 
572 /* master slave mode selection */
573 #define TIMER_MASTER_SLAVE_MODE_ENABLE      ((uint32_t)0x00000000U)                 /*!< master slave mode enable */
574 #define TIMER_MASTER_SLAVE_MODE_DISABLE     ((uint32_t)0x00000001U)                 /*!< master slave mode disable */
575 
576 /* external trigger prescaler */
577 #define SMCFG_ETPSC(regval)                 (BITS(12, 13) & ((uint32_t)(regval) << 12U))
578 #define TIMER_EXT_TRI_PSC_OFF               SMCFG_ETPSC(0)                          /*!< no divided */
579 #define TIMER_EXT_TRI_PSC_DIV2              SMCFG_ETPSC(1)                          /*!< divided by 2 */
580 #define TIMER_EXT_TRI_PSC_DIV4              SMCFG_ETPSC(2)                          /*!< divided by 4 */
581 #define TIMER_EXT_TRI_PSC_DIV8              SMCFG_ETPSC(3)                          /*!< divided by 8 */
582 
583 /* external trigger polarity */
584 #define TIMER_ETP_FALLING                   TIMER_SMCFG_ETP                         /*!< active low or falling edge active */
585 #define TIMER_ETP_RISING                    ((uint32_t)0x00000000U)                 /*!< active high or rising edge active */
586 
587 /* channel 0 trigger input selection */
588 #define TIMER_HALLINTERFACE_ENABLE          ((uint32_t)0x00000000U)                 /*!< TIMER hall sensor mode enable */
589 #define TIMER_HALLINTERFACE_DISABLE         ((uint32_t)0x00000001U)                 /*!< TIMER hall sensor mode disable */
590 
591 /* timer1 internal trigger input1 remap */
592 #define TIMER1_IRMP(regval)                 (BITS(10, 11) & ((uint32_t)(regval) << 10U))
593 #define TIMER1_ITI1_RMP_TIMER7_TRGO         TIMER1_IRMP(0)                          /*!< timer1 internal trigger input 1 remap to TIMER7_TRGO */
594 #define TIMER1_ITI1_RMP_ETHERNET_PTP        TIMER1_IRMP(1)                          /*!< timer1 internal trigger input 1 remap to ethernet PTP */
595 #define TIMER1_ITI1_RMP_USB_FS_SOF          TIMER1_IRMP(2)                          /*!< timer1 internal trigger input 1 remap to USB FS SOF */
596 #define TIMER1_ITI1_RMP_USB_HS_SOF          TIMER1_IRMP(3)                          /*!< timer1 internal trigger input 1 remap to USB HS SOF */
597 
598 /* timer4 channel 3 input remap */
599 #define TIMER4_IRMP(regval)                 (BITS(6, 7) & ((uint32_t)(regval) << 6U))
600 #define TIMER4_CI3_RMP_GPIO                 TIMER4_IRMP(0)                          /*!< timer4 channel 3 input remap to GPIO pin */
601 #define TIMER4_CI3_RMP_IRC32K               TIMER4_IRMP(1)                          /*!< timer4 channel 3 input remap to IRC32K */
602 #define TIMER4_CI3_RMP_LXTAL                TIMER4_IRMP(2)                          /*!< timer4 channel 3 input remap to  LXTAL */
603 #define TIMER4_CI3_RMP_RTC_WAKEUP_INT       TIMER4_IRMP(3)                          /*!< timer4 channel 3 input remap to RTC wakeup interrupt */
604 
605 /* timer10 internal trigger input1 remap */
606 #define TIMER10_IRMP(regval)                (BITS(0, 1) & ((uint32_t)(regval) << 0U))
607 #define TIMER10_ITI1_RMP_GPIO               TIMER10_IRMP(0)                         /*!< timer10 internal trigger input1 remap based on GPIO setting */
608 #define TIMER10_ITI1_RMP_RTC_HXTAL_DIV      TIMER10_IRMP(2)                         /*!< timer10 internal trigger input1 remap  HXTAL _DIV(clock used for RTC which is HXTAL clock divided by RTCDIV bits in RCU_CFG0 register) */
609 
610 /* timerx(x=0,1,2,13,14,15,16) write cc register selection */
611 #define TIMER_CHVSEL_ENABLE                  ((uint16_t)0x0002U)                    /*!< write CHxVAL register selection enable  */
612 #define TIMER_CHVSEL_DISABLE                 ((uint16_t)0x0000U)                    /*!< write CHxVAL register selection disable */
613 
614 /* the output value selection */
615 #define TIMER_OUTSEL_ENABLE                 ((uint16_t)0x0001U)                     /*!< output value selection enable */
616 #define TIMER_OUTSEL_DISABLE                ((uint16_t)0x0000U)                     /*!< output value selection disable */
617 
618 /* function declarations */
619 /* TIMER timebase*/
620 /* deinit a TIMER */
621 void timer_deinit(uint32_t timer_periph);
622 /* initialize TIMER init parameter struct */
623 void timer_struct_para_init(timer_parameter_struct* initpara);
624 /* initialize TIMER counter */
625 void gd32_timer_init(uint32_t timer_periph, timer_parameter_struct* initpara);
626 /* enable a TIMER */
627 void timer_enable(uint32_t timer_periph);
628 /* disable a TIMER */
629 void timer_disable(uint32_t timer_periph);
630 /* enable the auto reload shadow function */
631 void timer_auto_reload_shadow_enable(uint32_t timer_periph);
632 /* disable the auto reload shadow function */
633 void timer_auto_reload_shadow_disable(uint32_t timer_periph);
634 /* enable the update event */
635 void timer_update_event_enable(uint32_t timer_periph);
636 /* disable the update event */
637 void timer_update_event_disable(uint32_t timer_periph);
638 /* set TIMER counter alignment mode */
639 void timer_counter_alignment(uint32_t timer_periph, uint16_t aligned);
640 /* set TIMER counter up direction */
641 void timer_counter_up_direction(uint32_t timer_periph);
642 /* set TIMER counter down direction */
643 void timer_counter_down_direction(uint32_t timer_periph);
644 /* configure TIMER prescaler */
645 void timer_prescaler_config(uint32_t timer_periph, uint16_t prescaler, uint8_t pscreload);
646 /* configure TIMER repetition register value */
647 void timer_repetition_value_config(uint32_t timer_periph, uint16_t repetition);
648 /* configure TIMER autoreload register value */
649 void timer_autoreload_value_config(uint32_t timer_periph,uint32_t autoreload);
650 /* configure TIMER counter register value */
651 void timer_counter_value_config(uint32_t timer_periph , uint32_t counter);
652 /* read TIMER counter value */
653 uint32_t timer_counter_read(uint32_t timer_periph);
654 /* read TIMER prescaler value */
655 uint16_t timer_prescaler_read(uint32_t timer_periph);
656 /* configure TIMER single pulse mode */
657 void timer_single_pulse_mode_config(uint32_t timer_periph, uint32_t spmode);
658 /* configure TIMER update source */
659 void timer_update_source_config(uint32_t timer_periph, uint32_t update);
660 
661 /* timer DMA and event*/
662 /* enable the TIMER DMA */
663 void timer_dma_enable(uint32_t timer_periph, uint16_t dma);
664 /* disable the TIMER DMA */
665 void timer_dma_disable(uint32_t timer_periph, uint16_t dma);
666 /* channel DMA request source selection */
667 void timer_channel_dma_request_source_select(uint32_t timer_periph, uint8_t dma_request);
668 /* configure the TIMER DMA transfer */
669 void timer_dma_transfer_config(uint32_t timer_periph,uint32_t dma_baseaddr, uint32_t dma_lenth);
670 /* software generate events */
671 void timer_event_software_generate(uint32_t timer_periph, uint16_t event);
672 
673 /* TIMER channel complementary protection */
674 /* initialize TIMER break parameter struct */
675 void timer_break_struct_para_init(timer_break_parameter_struct* breakpara);
676 /* configure TIMER break function */
677 void timer_break_config(uint32_t timer_periph, timer_break_parameter_struct* breakpara);
678 /* enable TIMER break function */
679 void timer_break_enable(uint32_t timer_periph);
680 /* disable TIMER break function */
681 void timer_break_disable(uint32_t timer_periph);
682 /* enable TIMER output automatic function */
683 void timer_automatic_output_enable(uint32_t timer_periph);
684 /* disable TIMER output automatic function */
685 void timer_automatic_output_disable(uint32_t timer_periph);
686 /* enable or disable TIMER primary output function */
687 void timer_primary_output_config(uint32_t timer_periph, ControlStatus newvalue);
688 /* enable or disable channel capture/compare control shadow register */
689 void timer_channel_control_shadow_config(uint32_t timer_periph, ControlStatus newvalue);
690 /* configure TIMER channel control shadow register update control */
691 void timer_channel_control_shadow_update_config(uint32_t timer_periph, uint8_t ccuctl);
692 
693 /* TIMER channel output */
694 /* initialize TIMER channel output parameter struct */
695 void timer_channel_output_struct_para_init(timer_oc_parameter_struct* ocpara);
696 /* configure TIMER channel output function */
697 void timer_channel_output_config(uint32_t timer_periph,uint16_t channel, timer_oc_parameter_struct* ocpara);
698 /* configure TIMER channel output compare mode */
699 void timer_channel_output_mode_config(uint32_t timer_periph, uint16_t channel,uint16_t ocmode);
700 /* configure TIMER channel output pulse value */
701 void timer_channel_output_pulse_value_config(uint32_t timer_periph, uint16_t channel, uint32_t pulse);
702 /* configure TIMER channel output shadow function */
703 void timer_channel_output_shadow_config(uint32_t timer_periph, uint16_t channel, uint16_t ocshadow);
704 /* configure TIMER channel output fast function */
705 void timer_channel_output_fast_config(uint32_t timer_periph, uint16_t channel, uint16_t ocfast);
706 /* configure TIMER channel output clear function */
707 void timer_channel_output_clear_config(uint32_t timer_periph,uint16_t channel,uint16_t occlear);
708 /* configure TIMER channel output polarity */
709 void timer_channel_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocpolarity);
710 /* configure TIMER channel complementary output polarity */
711 void timer_channel_complementary_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnpolarity);
712 /* configure TIMER channel enable state */
713 void timer_channel_output_state_config(uint32_t timer_periph, uint16_t channel, uint32_t state);
714 /* configure TIMER channel complementary output enable state */
715 void timer_channel_complementary_output_state_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnstate);
716 
717 /* TIMER channel input */
718 /* initialize TIMER channel input parameter struct */
719 void timer_channel_input_struct_para_init(timer_ic_parameter_struct* icpara);
720 /* configure TIMER input capture parameter */
721 void timer_input_capture_config(uint32_t timer_periph, uint16_t channel, timer_ic_parameter_struct* icpara);
722 /* configure TIMER channel input capture prescaler value */
723 void timer_channel_input_capture_prescaler_config(uint32_t timer_periph, uint16_t channel, uint16_t prescaler);
724 /* read TIMER channel capture compare register value */
725 uint32_t timer_channel_capture_value_register_read(uint32_t timer_periph, uint16_t channel);
726 /* configure TIMER input pwm capture function */
727 void timer_input_pwm_capture_config(uint32_t timer_periph, uint16_t channel, timer_ic_parameter_struct* icpwm);
728 /* configure TIMER hall sensor mode */
729 void timer_hall_mode_config(uint32_t timer_periph, uint32_t hallmode);
730 
731 /* TIMER master and slave */
732 /* select TIMER input trigger source */
733 void timer_input_trigger_source_select(uint32_t timer_periph, uint32_t intrigger);
734 /* select TIMER master mode output trigger source */
735 void timer_master_output_trigger_source_select(uint32_t timer_periph, uint32_t outrigger);
736 /* select TIMER slave mode */
737 void timer_slave_mode_select(uint32_t timer_periph,uint32_t slavemode);
738 /* configure TIMER master slave mode */
739 void timer_master_slave_mode_config(uint32_t timer_periph, uint32_t masterslave);
740 /* configure TIMER external trigger input */
741 void timer_external_trigger_config(uint32_t timer_periph, uint32_t extprescaler, uint32_t extpolarity, uint32_t extfilter);
742 /* configure TIMER quadrature decoder mode */
743 void timer_quadrature_decoder_mode_config(uint32_t timer_periph, uint32_t decomode, uint16_t ic0polarity, uint16_t ic1polarity);
744 /* configure TIMER internal clock mode */
745 void timer_internal_clock_config(uint32_t timer_periph);
746 /* configure TIMER the internal trigger as external clock input */
747 void timer_internal_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t intrigger);
748 /* configure TIMER the external trigger as external clock input */
749 void timer_external_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t extrigger, uint16_t extpolarity,uint32_t extfilter);
750 /* configure TIMER the external clock mode 0 */
751 void timer_external_clock_mode0_config(uint32_t timer_periph, uint32_t extprescaler, uint32_t extpolarity, uint32_t extfilter);
752 /* configure TIMER the external clock mode 1 */
753 void timer_external_clock_mode1_config(uint32_t timer_periph, uint32_t extprescaler, uint32_t extpolarity, uint32_t extfilter);
754 /* disable TIMER the external clock mode 1 */
755 void timer_external_clock_mode1_disable(uint32_t timer_periph);
756 /* configure TIMER channel remap function */
757 void timer_channel_remap_config(uint32_t timer_periph,uint32_t remap);
758 
759 /* TIMER configure */
760 /* configure TIMER write CHxVAL register selection */
761 void timer_write_chxval_register_config(uint32_t timer_periph, uint16_t ccsel);
762 /* configure TIMER output value selection */
763 void timer_output_value_selection_config(uint32_t timer_periph, uint16_t outsel);
764 
765 /* TIMER interrupt and flag*/
766 /* get TIMER flags */
767 FlagStatus timer_flag_get(uint32_t timer_periph, uint32_t flag);
768 /* clear TIMER flags */
769 void timer_flag_clear(uint32_t timer_periph, uint32_t flag);
770 /* enable the TIMER interrupt */
771 void timer_interrupt_enable(uint32_t timer_periph, uint32_t interrupt);
772 /* disable the TIMER interrupt */
773 void timer_interrupt_disable(uint32_t timer_periph, uint32_t interrupt);
774 /* get timer interrupt flag */
775 FlagStatus timer_interrupt_flag_get(uint32_t timer_periph, uint32_t interrupt);
776 /* clear TIMER interrupt flag */
777 void timer_interrupt_flag_clear(uint32_t timer_periph, uint32_t interrupt);
778 
779 #endif /* GD32F4XX_TIMER_H */
780