1 /*!
2 \file gd32f3x0_timer.c
3 \brief TIMER driver
4
5 \version 2017-06-06, V1.0.0, firmware for GD32F3x0
6 \version 2019-06-01, V2.0.0, firmware for GD32F3x0
7 \version 2020-09-30, V2.1.0, firmware for GD32F3x0
8 */
9
10 /*
11 Copyright (c) 2020, GigaDevice Semiconductor Inc.
12
13 Redistribution and use in source and binary forms, with or without modification,
14 are permitted provided that the following conditions are met:
15
16 1. Redistributions of source code must retain the above copyright notice, this
17 list of conditions and the following disclaimer.
18 2. Redistributions in binary form must reproduce the above copyright notice,
19 this list of conditions and the following disclaimer in the documentation
20 and/or other materials provided with the distribution.
21 3. Neither the name of the copyright holder nor the names of its contributors
22 may be used to endorse or promote products derived from this software without
23 specific prior written permission.
24
25 THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED
27 WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
28 IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,
29 INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
30 NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
31 PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
32 WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY
34 OF SUCH DAMAGE.
35 */
36
37
38 #include "gd32f3x0_timer.h"
39
40 /*!
41 \brief deinit a TIMER
42 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
43 \param[out] none
44 \retval none
45 */
timer_deinit(uint32_t timer_periph)46 void timer_deinit(uint32_t timer_periph)
47 {
48 switch(timer_periph){
49 case TIMER0:
50 /* reset TIMER0 */
51 rcu_periph_reset_enable(RCU_TIMER0RST);
52 rcu_periph_reset_disable(RCU_TIMER0RST);
53 break;
54 case TIMER1:
55 /* reset TIMER1 */
56 rcu_periph_reset_enable(RCU_TIMER1RST);
57 rcu_periph_reset_disable(RCU_TIMER1RST);
58 break;
59 case TIMER2:
60 /* reset TIMER2 */
61 rcu_periph_reset_enable(RCU_TIMER2RST);
62 rcu_periph_reset_disable(RCU_TIMER2RST);
63 break;
64 #ifdef GD32F350
65 case TIMER5:
66 /* reset TIMER5 */
67 rcu_periph_reset_enable(RCU_TIMER5RST);
68 rcu_periph_reset_disable(RCU_TIMER5RST);
69 break;
70 #endif
71 case TIMER13:
72 /* reset TIMER13 */
73 rcu_periph_reset_enable(RCU_TIMER13RST);
74 rcu_periph_reset_disable(RCU_TIMER13RST);
75 break;
76 case TIMER14:
77 /* reset TIMER14 */
78 rcu_periph_reset_enable(RCU_TIMER14RST);
79 rcu_periph_reset_disable(RCU_TIMER14RST);
80 break;
81 case TIMER15:
82 /* reset TIMER15 */
83 rcu_periph_reset_enable(RCU_TIMER15RST);
84 rcu_periph_reset_disable(RCU_TIMER15RST);
85 break;
86 case TIMER16:
87 /* reset TIMER16 */
88 rcu_periph_reset_enable(RCU_TIMER16RST);
89 rcu_periph_reset_disable(RCU_TIMER16RST);
90 break;
91 default:
92 break;
93 }
94 }
95
96 /*!
97 \brief initialize TIMER init parameter struct with a default value
98 \param[in] initpara: init parameter struct
99 \param[out] none
100 \retval none
101 */
timer_struct_para_init(timer_parameter_struct * initpara)102 void timer_struct_para_init(timer_parameter_struct* initpara)
103 {
104 /* initialize the init parameter struct member with the default value */
105 initpara->prescaler = 0U;
106 initpara->alignedmode = TIMER_COUNTER_EDGE;
107 initpara->counterdirection = TIMER_COUNTER_UP;
108 initpara->period = 65535U;
109 initpara->clockdivision = TIMER_CKDIV_DIV1;
110 initpara->repetitioncounter = 0U;
111 }
112
113 /*!
114 \brief initialize TIMER counter
115 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
116 \param[in] timer_initpara: init parameter struct
117 prescaler: prescaler value of the counter clock,0~65535
118 alignedmode: TIMER_COUNTER_EDGE,TIMER_COUNTER_CENTER_DOWN,TIMER_COUNTER_CENTER_UP,TIMER_COUNTER_CENTER_BOTH
119 counterdirection: TIMER_COUNTER_UP,TIMER_COUNTER_DOWN
120 period: counter auto reload value,(TIMER1 32 bit)
121 clockdivision: TIMER_CKDIV_DIV1,TIMER_CKDIV_DIV2,TIMER_CKDIV_DIV4
122 repetitioncounter: counter repetition value,0~255
123 \param[out] none
124 \retval none
125 */
gd32_timer_init(uint32_t timer_periph,timer_parameter_struct * initpara)126 void gd32_timer_init(uint32_t timer_periph, timer_parameter_struct* initpara)
127 {
128 /* configure the counter prescaler value */
129 TIMER_PSC(timer_periph) = (uint16_t)initpara->prescaler;
130
131 /* configure the counter direction and aligned mode */
132 if((TIMER0 == timer_periph) || (TIMER1 == timer_periph) || (TIMER2 == timer_periph)){
133 TIMER_CTL0(timer_periph) &= ~(uint32_t)(TIMER_CTL0_DIR|TIMER_CTL0_CAM);
134 TIMER_CTL0(timer_periph) |= (uint32_t)initpara->alignedmode;
135 TIMER_CTL0(timer_periph) |= (uint32_t)initpara->counterdirection;
136 }
137
138 /* configure the autoreload value */
139 TIMER_CAR(timer_periph) = (uint32_t)initpara->period;
140
141 if((TIMER0 == timer_periph) || (TIMER1 == timer_periph) || (TIMER2 == timer_periph) || (TIMER13 == timer_periph)
142 || (TIMER14 == timer_periph) || (TIMER15 == timer_periph) || (TIMER16 == timer_periph)){
143 /* reset the CKDIV bit */
144 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CKDIV;
145 TIMER_CTL0(timer_periph) |= (uint32_t)initpara->clockdivision;
146 }
147
148 if((TIMER0 == timer_periph) || (TIMER14 == timer_periph) || (TIMER15 == timer_periph) || (TIMER16 == timer_periph)){
149 /* configure the repetition counter value */
150 TIMER_CREP(timer_periph) = (uint32_t)initpara->repetitioncounter;
151 }
152
153 /* generate an update event */
154 TIMER_SWEVG(timer_periph) |= (uint32_t)TIMER_SWEVG_UPG;
155 }
156
157 /*!
158 \brief enable a TIMER
159 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
160 \param[out] none
161 \retval none
162 */
timer_enable(uint32_t timer_periph)163 void timer_enable(uint32_t timer_periph)
164 {
165 TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_CEN;
166 }
167
168 /*!
169 \brief disable a TIMER
170 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
171 \param[out] none
172 \retval none
173 */
timer_disable(uint32_t timer_periph)174 void timer_disable(uint32_t timer_periph)
175 {
176 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CEN;
177 }
178
179 /*!
180 \brief enable the auto reload shadow function
181 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
182 \param[out] none
183 \retval none
184 */
timer_auto_reload_shadow_enable(uint32_t timer_periph)185 void timer_auto_reload_shadow_enable(uint32_t timer_periph)
186 {
187 TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_ARSE;
188 }
189
190 /*!
191 \brief disable the auto reload shadow function
192 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
193 \param[out] none
194 \retval none
195 */
timer_auto_reload_shadow_disable(uint32_t timer_periph)196 void timer_auto_reload_shadow_disable(uint32_t timer_periph)
197 {
198 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_ARSE;
199 }
200
201 /*!
202 \brief enable the update event
203 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
204 \param[out] none
205 \retval none
206 */
timer_update_event_enable(uint32_t timer_periph)207 void timer_update_event_enable(uint32_t timer_periph)
208 {
209 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_UPDIS;
210 }
211
212 /*!
213 \brief disable the update event
214 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
215 \param[out] none
216 \retval none
217 */
timer_update_event_disable(uint32_t timer_periph)218 void timer_update_event_disable(uint32_t timer_periph)
219 {
220 TIMER_CTL0(timer_periph) |= (uint32_t) TIMER_CTL0_UPDIS;
221 }
222
223 /*!
224 \brief set TIMER counter alignment mode
225 \param[in] timer_periph: TIMERx(x=0..2)
226 \param[in] aligned:
227 only one parameter can be selected which is shown as below:
228 \arg TIMER_COUNTER_EDGE: edge-aligned mode
229 \arg TIMER_COUNTER_CENTER_DOWN: center-aligned and counting down assert mode
230 \arg TIMER_COUNTER_CENTER_UP: center-aligned and counting up assert mode
231 \arg TIMER_COUNTER_CENTER_BOTH: center-aligned and counting up/down assert mode
232 \param[out] none
233 \retval none
234 */
timer_counter_alignment(uint32_t timer_periph,uint16_t aligned)235 void timer_counter_alignment(uint32_t timer_periph, uint16_t aligned)
236 {
237 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_CAM;
238 TIMER_CTL0(timer_periph) |= (uint32_t)aligned;
239 }
240
241 /*!
242 \brief set TIMER counter up direction
243 \param[in] timer_periph: TIMERx(x=0..2)
244 \param[out] none
245 \retval none
246 */
timer_counter_up_direction(uint32_t timer_periph)247 void timer_counter_up_direction(uint32_t timer_periph)
248 {
249 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_DIR;
250 }
251
252 /*!
253 \brief set TIMER counter down direction
254 \param[in] timer_periph: TIMERx(x=0..2)
255 \param[out] none
256 \retval none
257 */
timer_counter_down_direction(uint32_t timer_periph)258 void timer_counter_down_direction(uint32_t timer_periph)
259 {
260 TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_DIR;
261 }
262
263 /*!
264 \brief configure TIMER prescaler
265 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
266 \param[in] prescaler: prescaler value
267 \param[in] pscreload: prescaler reload mode
268 only one parameter can be selected which is shown as below:
269 \arg TIMER_PSC_RELOAD_NOW: the prescaler is loaded right now
270 \arg TIMER_PSC_RELOAD_UPDATE: the prescaler is loaded at the next update event
271 \param[out] none
272 \retval none
273 */
timer_prescaler_config(uint32_t timer_periph,uint16_t prescaler,uint8_t pscreload)274 void timer_prescaler_config(uint32_t timer_periph, uint16_t prescaler, uint8_t pscreload)
275 {
276 TIMER_PSC(timer_periph) = (uint32_t)prescaler;
277
278 if(TIMER_PSC_RELOAD_NOW == pscreload){
279 TIMER_SWEVG(timer_periph) |= (uint32_t)TIMER_SWEVG_UPG;
280 }
281 }
282
283 /*!
284 \brief configure TIMER repetition register value
285 \param[in] timer_periph: TIMERx(x=0,15,16)
286 \param[in] repetition: the counter repetition value,0~255
287 \param[out] none
288 \retval none
289 */
timer_repetition_value_config(uint32_t timer_periph,uint16_t repetition)290 void timer_repetition_value_config(uint32_t timer_periph, uint16_t repetition)
291 {
292 TIMER_CREP(timer_periph) = (uint32_t)repetition;
293 }
294
295 /*!
296 \brief configure TIMER autoreload register value
297 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
298 \param[in] autoreload: the counter auto-reload value
299 \param[out] none
300 \retval none
301 */
timer_autoreload_value_config(uint32_t timer_periph,uint32_t autoreload)302 void timer_autoreload_value_config(uint32_t timer_periph, uint32_t autoreload)
303 {
304 TIMER_CAR(timer_periph) = (uint32_t)autoreload;
305 }
306
307 /*!
308 \brief configure TIMER counter register value
309 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
310 \param[in] counter: the counter value
311 \param[out] none
312 \retval none
313 */
timer_counter_value_config(uint32_t timer_periph,uint32_t counter)314 void timer_counter_value_config(uint32_t timer_periph, uint32_t counter)
315 {
316 TIMER_CNT(timer_periph) = (uint32_t)counter;
317 }
318
319 /*!
320 \brief read TIMER counter value
321 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
322 \param[out] none
323 \retval counter value
324 */
timer_counter_read(uint32_t timer_periph)325 uint32_t timer_counter_read(uint32_t timer_periph)
326 {
327 uint32_t count_value = 0U;
328 count_value = TIMER_CNT(timer_periph);
329 return (count_value);
330 }
331
332 /*!
333 \brief read TIMER prescaler value
334 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
335 \param[out] none
336 \retval prescaler register value
337 */
timer_prescaler_read(uint32_t timer_periph)338 uint16_t timer_prescaler_read(uint32_t timer_periph)
339 {
340 uint16_t prescaler_value = 0U;
341 prescaler_value = (uint16_t)(TIMER_PSC(timer_periph));
342 return (prescaler_value);
343 }
344
345 /*!
346 \brief configure TIMER single pulse mode
347 \param[in] timer_periph: TIMERx(x=0..2,14..16),TIMER5 just for GD32F350
348 \param[in] spmode:
349 only one parameter can be selected which is shown as below:
350 \arg TIMER_SP_MODE_SINGLE: single pulse mode
351 \arg TIMER_SP_MODE_REPETITIVE: repetitive pulse mode
352 \param[out] none
353 \retval none
354 */
timer_single_pulse_mode_config(uint32_t timer_periph,uint8_t spmode)355 void timer_single_pulse_mode_config(uint32_t timer_periph, uint8_t spmode)
356 {
357 if(TIMER_SP_MODE_SINGLE == spmode){
358 TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_SPM;
359 }else if(TIMER_SP_MODE_REPETITIVE == spmode){
360 TIMER_CTL0(timer_periph) &= ~((uint32_t)TIMER_CTL0_SPM);
361 }else{
362 /* illegal parameters */
363 }
364 }
365
366 /*!
367 \brief configure TIMER update source
368 \param[in] timer_periph: TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
369 \param[in] update:
370 only one parameter can be selected which is shown as below:
371 \arg TIMER_UPDATE_SRC_GLOBAL: update generate by setting of UPG bit or the counter overflow/underflow,or the slave mode controller trigger
372 \arg TIMER_UPDATE_SRC_REGULAR: update generate only by counter overflow/underflow
373 \param[out] none
374 \retval none
375 */
timer_update_source_config(uint32_t timer_periph,uint8_t update)376 void timer_update_source_config(uint32_t timer_periph, uint8_t update)
377 {
378 if(TIMER_UPDATE_SRC_REGULAR == update){
379 TIMER_CTL0(timer_periph) |= (uint32_t)TIMER_CTL0_UPS;
380 }else if(TIMER_UPDATE_SRC_GLOBAL == update){
381 TIMER_CTL0(timer_periph) &= ~(uint32_t)TIMER_CTL0_UPS;
382 }else{
383 /* illegal parameters */
384 }
385 }
386
387 /*!
388 \brief configure TIMER OCPRE clear source selection
389 \param[in] timer_periph: TIMERx(x=0..2)
390 \param[in] ocpreclear:
391 only one parameter can be selected which is shown as below:
392 \arg TIMER_OCPRE_CLEAR_SOURCE_CLR: OCPRE_CLR_INT is connected to the OCPRE_CLR input
393 \arg TIMER_OCPRE_CLEAR_SOURCE_ETIF: OCPRE_CLR_INT is connected to ETIF
394 \param[out] none
395 \retval none
396 */
timer_ocpre_clear_source_config(uint32_t timer_periph,uint8_t ocpreclear)397 void timer_ocpre_clear_source_config(uint32_t timer_periph, uint8_t ocpreclear)
398 {
399 if(TIMER_OCPRE_CLEAR_SOURCE_ETIF == ocpreclear){
400 TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SMCFG_OCRC;
401 }else if(TIMER_OCPRE_CLEAR_SOURCE_CLR == ocpreclear){
402 TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_OCRC;
403 }else{
404 /* illegal parameters */
405 }
406 }
407
408 /*!
409 \brief enable the TIMER interrupt
410 \param[in] timer_periph: please refer to the following parameters
411 \param[in] interrupt: timer interrupt enable source
412 only one parameter can be selected which is shown as below:
413 \arg TIMER_INT_UP: update interrupt enable, TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
414 \arg TIMER_INT_CH0: channel 0 interrupt enable, TIMERx(x=0..2,13..16)
415 \arg TIMER_INT_CH1: channel 1 interrupt enable, TIMERx(x=0..2,14)
416 \arg TIMER_INT_CH2: channel 2 interrupt enable, TIMERx(x=0..2)
417 \arg TIMER_INT_CH3: channel 3 interrupt enable , TIMERx(x=0..2)
418 \arg TIMER_INT_CMT: commutation interrupt enable, TIMERx(x=0,14..16)
419 \arg TIMER_INT_TRG: trigger interrupt enable, TIMERx(x=0..2,14)
420 \arg TIMER_INT_BRK: break interrupt enable, TIMERx(x=0,14..16)
421 \param[out] none
422 \retval none
423 */
timer_interrupt_enable(uint32_t timer_periph,uint32_t interrupt)424 void timer_interrupt_enable(uint32_t timer_periph, uint32_t interrupt)
425 {
426 TIMER_DMAINTEN(timer_periph) |= (uint32_t) interrupt;
427 }
428
429 /*!
430 \brief disable the TIMER interrupt
431 \param[in] timer_periph: please refer to the following parameters
432 \param[in] interrupt: timer interrupt source disable
433 only one parameter can be selected which is shown as below:
434 \arg TIMER_INT_UP: update interrupt disable, TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
435 \arg TIMER_INT_CH0: channel 0 interrupt disable, TIMERx(x=0..2,13..16)
436 \arg TIMER_INT_CH1: channel 1 interrupt disable, TIMERx(x=0..2,14)
437 \arg TIMER_INT_CH2: channel 2 interrupt disable, TIMERx(x=0..2)
438 \arg TIMER_INT_CH3: channel 3 interrupt disable , TIMERx(x=0..2)
439 \arg TIMER_INT_CMT: commutation interrupt disable, TIMERx(x=0,14..16)
440 \arg TIMER_INT_TRG: trigger interrupt disable, TIMERx(x=0..2,14)
441 \arg TIMER_INT_BRK: break interrupt disable, TIMERx(x=0,14..16)
442 \param[out] none
443 \retval none
444 */
timer_interrupt_disable(uint32_t timer_periph,uint32_t interrupt)445 void timer_interrupt_disable(uint32_t timer_periph, uint32_t interrupt)
446 {
447 TIMER_DMAINTEN(timer_periph) &= (~(uint32_t)interrupt);
448 }
449
450 /*!
451 \brief get timer interrupt flag
452 \param[in] timer_periph: please refer to the following parameters
453 \param[in] interrupt: the timer interrupt bits
454 only one parameter can be selected which is shown as below:
455 \arg TIMER_INT_FLAG_UP: update interrupt flag,TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
456 \arg TIMER_INT_FLAG_CH0: channel 0 interrupt flag,TIMERx(x=0..2,13..16)
457 \arg TIMER_INT_FLAG_CH1: channel 1 interrupt flag,TIMERx(x=0..2,14)
458 \arg TIMER_INT_FLAG_CH2: channel 2 interrupt flag,TIMERx(x=0..2)
459 \arg TIMER_INT_FLAG_CH3: channel 3 interrupt flag,TIMERx(x=0..2)
460 \arg TIMER_INT_FLAG_CMT: channel commutation interrupt flag,TIMERx(x=0,14..16)
461 \arg TIMER_INT_FLAG_TRG: trigger interrupt flag,TIMERx(x=0..2,14)
462 \arg TIMER_INT_FLAG_BRK: break interrupt flag,TIMERx(x=0,14..16)
463 \param[out] none
464 \retval FlagStatus: SET or RESET
465 */
timer_interrupt_flag_get(uint32_t timer_periph,uint32_t interrupt)466 FlagStatus timer_interrupt_flag_get(uint32_t timer_periph, uint32_t interrupt)
467 {
468 uint32_t val;
469 val = (TIMER_DMAINTEN(timer_periph) & interrupt);
470 if((RESET != (TIMER_INTF(timer_periph) & interrupt) ) && (RESET != val)){
471 return SET;
472 }else{
473 return RESET;
474 }
475 }
476
477 /*!
478 \brief clear TIMER interrupt flag
479 \param[in] timer_periph: please refer to the following parameters
480 \param[in] interrupt: the timer interrupt bits
481 only one parameter can be selected which is shown as below:
482 \arg TIMER_INT_FLAG_UP: update interrupt flag, TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
483 \arg TIMER_INT_FLAG_CH0: channel 0 interrupt flag, TIMERx(x=0..2,13..16)
484 \arg TIMER_INT_FLAG_CH1: channel 1 interrupt flag, TIMERx(x=0..2,14)
485 \arg TIMER_INT_FLAG_CH2: channel 2 interrupt flag, TIMERx(x=0..2)
486 \arg TIMER_INT_FLAG_CH3: channel 3 interrupt flag, TIMERx(x=0..2)
487 \arg TIMER_INT_FLAG_CMT: channel commutation interrupt flag, TIMERx(x=0,14..16)
488 \arg TIMER_INT_FLAG_TRG: trigger interrupt flag, TIMERx(x=0..2,14)
489 \arg TIMER_INT_FLAG_BRK: break interrupt flag, TIMERx(x=0,14..16)
490 \param[out] none
491 \retval none
492 */
timer_interrupt_flag_clear(uint32_t timer_periph,uint32_t interrupt)493 void timer_interrupt_flag_clear(uint32_t timer_periph, uint32_t interrupt)
494 {
495 TIMER_INTF(timer_periph) = (~(uint32_t)interrupt);
496 }
497
498 /*!
499 \brief get TIMER flags
500 \param[in] timer_periph: please refer to the following parameters
501 \param[in] flag: the timer interrupt flags
502 only one parameter can be selected which is shown as below:
503 \arg TIMER_FLAG_UP: update flag, TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
504 \arg TIMER_FLAG_CH0: channel 0 flag, TIMERx(x=0..2,13..16)
505 \arg TIMER_FLAG_CH1: channel 1 flag, TIMERx(x=0..2,14)
506 \arg TIMER_FLAG_CH2: channel 2 flag, TIMERx(x=0..2)
507 \arg TIMER_FLAG_CH3: channel 3 flag, TIMERx(x=0..2)
508 \arg TIMER_FLAG_CMT: channel control update flag, TIMERx(x=0,14..16)
509 \arg TIMER_FLAG_TRG: trigger flag, TIMERx(x=0..2,14)
510 \arg TIMER_FLAG_BRK: break flag,TIMERx(x=0,14..16)
511 \arg TIMER_FLAG_CH0O: channel 0 overcapture flag, TIMERx(x=0..2,13..16)
512 \arg TIMER_FLAG_CH1O: channel 1 overcapture flag, TIMERx(x=0..2,14)
513 \arg TIMER_FLAG_CH2O: channel 2 overcapture flag, TIMERx(x=0..2)
514 \arg TIMER_FLAG_CH3O: channel 3 overcapture flag, TIMERx(x=0..2)
515 \param[out] none
516 \retval FlagStatus: SET or RESET
517 */
timer_flag_get(uint32_t timer_periph,uint32_t flag)518 FlagStatus timer_flag_get(uint32_t timer_periph, uint32_t flag)
519 {
520 if(RESET != (TIMER_INTF(timer_periph) & flag)){
521 return SET;
522 }else{
523 return RESET;
524 }
525 }
526
527 /*!
528 \brief clear TIMER flags
529 \param[in] timer_periph: please refer to the following parameters
530 \param[in] flag: the timer interrupt flags
531 only one parameter can be selected which is shown as below:
532 \arg TIMER_FLAG_UP: update flag, TIMERx(x=0..2,13..16),TIMER5 just for GD32F350
533 \arg TIMER_FLAG_CH0: channel 0 flag, TIMERx(x=0..2,13..16)
534 \arg TIMER_FLAG_CH1: channel 1 flag, TIMERx(x=0..2,14)
535 \arg TIMER_FLAG_CH2: channel 2 flag, TIMERx(x=0..2)
536 \arg TIMER_FLAG_CH3: channel 3 flag, TIMERx(x=0..2)
537 \arg TIMER_FLAG_CMT: channel control update flag, TIMERx(x=0,14..16)
538 \arg TIMER_FLAG_TRG: trigger flag, TIMERx(x=0..2,14)
539 \arg TIMER_FLAG_BRK: break flag,TIMERx(x=0,14..16)
540 \arg TIMER_FLAG_CH0O: channel 0 overcapture flag, TIMERx(x=0..2,13..16)
541 \arg TIMER_FLAG_CH1O: channel 1 overcapture flag, TIMERx(x=0..2,14)
542 \arg TIMER_FLAG_CH2O: channel 2 overcapture flag, TIMERx(x=0..2)
543 \arg TIMER_FLAG_CH3O: channel 3 overcapture flag, TIMERx(x=0..2)
544 \param[out] none
545 \retval none
546 */
timer_flag_clear(uint32_t timer_periph,uint32_t flag)547 void timer_flag_clear(uint32_t timer_periph, uint32_t flag)
548 {
549 TIMER_INTF(timer_periph) = (~(uint32_t)flag);
550 }
551
552 /*!
553 \brief enable the TIMER DMA
554 \param[in] timer_periph: please refer to the following parameters
555 \param[in] dma: specify which DMA to enable
556 one or more parameters can be selected which is shown as below:
557 \arg TIMER_DMA_UPD: update DMA, TIMERx(x=0..2,14..16),TIMER5 just for GD32F350
558 \arg TIMER_DMA_CH0D: channel 0 DMA request, TIMERx(x=0..2,14..16)
559 \arg TIMER_DMA_CH1D: channel 1 DMA request, TIMERx(x=0..2,14)
560 \arg TIMER_DMA_CH2D: channel 2 DMA request, TIMERx(x=0..2)
561 \arg TIMER_DMA_CH3D: channel 3 DMA request, TIMERx(x=0..2)
562 \arg TIMER_DMA_CMTD: commutation DMA request, TIMERx(x=0,14)
563 \arg TIMER_DMA_TRGD: trigger DMA request, TIMERx(x=0..2,14)
564 \param[out] none
565 \retval none
566 */
timer_dma_enable(uint32_t timer_periph,uint16_t dma)567 void timer_dma_enable(uint32_t timer_periph, uint16_t dma)
568 {
569 TIMER_DMAINTEN(timer_periph) |= (uint32_t) dma;
570 }
571
572 /*!
573 \brief disable the TIMER DMA
574 \param[in] timer_periph: please refer to the following parameters
575 \param[in] dma: specify which DMA to disable
576 one or more parameters can be selected which are shown as below:
577 \arg TIMER_DMA_UPD: update DMA, TIMERx(x=0..2,14..16),TIMER5 just for GD32F350
578 \arg TIMER_DMA_CH0D: channel 0 DMA request, TIMERx(x=0..2,14..16)
579 \arg TIMER_DMA_CH1D: channel 1 DMA request, TIMERx(x=0..2,14)
580 \arg TIMER_DMA_CH2D: channel 2 DMA request, TIMERx(x=0..2)
581 \arg TIMER_DMA_CH3D: channel 3 DMA request, TIMERx(x=0..2)
582 \arg TIMER_DMA_CMTD: commutation DMA request , TIMERx(x=0,14)
583 \arg TIMER_DMA_TRGD: trigger DMA request, TIMERx(x=0..2,14)
584 \param[out] none
585 \retval none
586 */
timer_dma_disable(uint32_t timer_periph,uint16_t dma)587 void timer_dma_disable(uint32_t timer_periph, uint16_t dma)
588 {
589 TIMER_DMAINTEN(timer_periph) &= (~(uint32_t)(dma));
590 }
591
592 /*!
593 \brief channel DMA request source selection
594 \param[in] timer_periph: TIMERx(x=0..2,14..16)
595 \param[in] dma_request: channel DMA request source selection
596 only one parameter can be selected which is shown as below:
597 \arg TIMER_DMAREQUEST_CHANNELEVENT: DMA request of channel y is sent when channel y event occurs
598 \arg TIMER_DMAREQUEST_UPDATEEVENT: DMA request of channel y is sent when update event occurs
599 \param[out] none
600 \retval none
601 */
timer_channel_dma_request_source_select(uint32_t timer_periph,uint8_t dma_request)602 void timer_channel_dma_request_source_select(uint32_t timer_periph, uint8_t dma_request)
603 {
604 if(TIMER_DMAREQUEST_UPDATEEVENT == dma_request){
605 TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_DMAS;
606 }else if(TIMER_DMAREQUEST_CHANNELEVENT == dma_request){
607 TIMER_CTL1(timer_periph) &= ~(uint32_t)TIMER_CTL1_DMAS;
608 }else{
609 /* illegal parameters */
610 }
611 }
612
613 /*!
614 \brief configure the TIMER DMA transfer
615 \param[in] timer_periph: TIMERx(x=0..2,14..16)
616 \param[in] dma_baseaddr:
617 only one parameter can be selected which is shown as below:
618 \arg TIMER_DMACFG_DMATA_CTL0: DMA transfer address is TIMER_CTL0, TIMERx(x=0..2,14..16)
619 \arg TIMER_DMACFG_DMATA_CTL1: DMA transfer address is TIMER_CTL1, TIMERx(x=0..2,14..16)
620 \arg TIMER_DMACFG_DMATA_SMCFG: DMA transfer address is TIMER_SMCFG, TIMERx(x=0..2,14)
621 \arg TIMER_DMACFG_DMATA_DMAINTEN: DMA transfer address is TIMER_DMAINTEN, TIMERx(x=0..2,14..16)
622 \arg TIMER_DMACFG_DMATA_INTF: DMA transfer address is TIMER_INTF, TIMERx(x=0..2,14..16)
623 \arg TIMER_DMACFG_DMATA_SWEVG: DMA transfer address is TIMER_SWEVG, TIMERx(x=0..2,14..16)
624 \arg TIMER_DMACFG_DMATA_CHCTL0: DMA transfer address is TIMER_CHCTL0, TIMERx(x=0..2,14..16)
625 \arg TIMER_DMACFG_DMATA_CHCTL1: DMA transfer address is TIMER_CHCTL1, TIMERx(x=0..2)
626 \arg TIMER_DMACFG_DMATA_CHCTL2: DMA transfer address is TIMER_CHCTL2, TIMERx(x=0..2,14..16)
627 \arg TIMER_DMACFG_DMATA_CNT: DMA transfer address is TIMER_CNT, TIMERx(x=0..2,14..16)
628 \arg TIMER_DMACFG_DMATA_PSC: DMA transfer address is TIMER_PSC, TIMERx(x=0..2,14..16)
629 \arg TIMER_DMACFG_DMATA_CAR: DMA transfer address is TIMER_CAR, TIMERx(x=0..2,14..16)
630 \arg TIMER_DMACFG_DMATA_CREP: DMA transfer address is TIMER_CREP, TIMERx(x=0,14..16)
631 \arg TIMER_DMACFG_DMATA_CH0CV: DMA transfer address is TIMER_CH0CV, TIMERx(x=0..2,14..16)
632 \arg TIMER_DMACFG_DMATA_CH1CV: DMA transfer address is TIMER_CH1CV, TIMERx(x=0..2,14)
633 \arg TIMER_DMACFG_DMATA_CH2CV: DMA transfer address is TIMER_CH2CV, TIMERx(x=0..2)
634 \arg TIMER_DMACFG_DMATA_CH3CV: DMA transfer address is TIMER_CH3CV, TIMERx(x=0..2)
635 \arg TIMER_DMACFG_DMATA_CCHP: DMA transfer address is TIMER_CCHP, TIMERx(x=0,14..16)
636 \arg TIMER_DMACFG_DMATA_DMACFG: DMA transfer address is TIMER_DMACFG, TIMERx(x=0..2,14..16)
637 \arg TIMER_DMACFG_DMATA_DMATB: DMA transfer address is TIMER_DMATB, TIMERx(x=0..2,14..16)
638 \param[in] dma_lenth:
639 only one parameter can be selected which is shown as below:
640 \arg TIMER_DMACFG_DMATC_xTRANSFER(x=1..18): DMA transfer x time
641 \param[out] none
642 \retval none
643 */
timer_dma_transfer_config(uint32_t timer_periph,uint32_t dma_baseaddr,uint32_t dma_lenth)644 void timer_dma_transfer_config(uint32_t timer_periph, uint32_t dma_baseaddr, uint32_t dma_lenth)
645 {
646 TIMER_DMACFG(timer_periph) &= (~(uint32_t)(TIMER_DMACFG_DMATA | TIMER_DMACFG_DMATC));
647 TIMER_DMACFG(timer_periph) |= (uint32_t)(dma_baseaddr | dma_lenth);
648 }
649
650 /*!
651 \brief software generate events
652 \param[in] timer_periph: please refer to the following parameters
653 \param[in] event: the timer software event generation sources
654 one or more parameters can be selected which are shown as below:
655 \arg TIMER_EVENT_SRC_UPG: update event,TIMERx(x=0..2,13..16), TIMER5 just for GD32F350
656 \arg TIMER_EVENT_SRC_CH0G: channel 0 capture or compare event generation, TIMERx(x=0..2,13..16)
657 \arg TIMER_EVENT_SRC_CH1G: channel 1 capture or compare event generation, TIMERx(x=0..2,14)
658 \arg TIMER_EVENT_SRC_CH2G: channel 2 capture or compare event generation, TIMERx(x=0..2)
659 \arg TIMER_EVENT_SRC_CH3G: channel 3 capture or compare event generation, TIMERx(x=0..2)
660 \arg TIMER_EVENT_SRC_CMTG: channel commutation event generation, TIMERx(x=0,14..16)
661 \arg TIMER_EVENT_SRC_TRGG: trigger event generation, TIMERx(x=0..2,14)
662 \arg TIMER_EVENT_SRC_BRKG: break event generation, TIMERx(x=0,14..16)
663 \param[out] none
664 \retval none
665 */
timer_event_software_generate(uint32_t timer_periph,uint16_t event)666 void timer_event_software_generate(uint32_t timer_periph, uint16_t event)
667 {
668 TIMER_SWEVG(timer_periph) |= (uint32_t)event;
669 }
670
671 /*!
672 \brief initialize TIMER break parameter struct with a default value
673 \param[in] breakpara: TIMER break parameter struct
674 \param[out] none
675 \retval none
676 */
timer_break_struct_para_init(timer_break_parameter_struct * breakpara)677 void timer_break_struct_para_init(timer_break_parameter_struct* breakpara)
678 {
679 /* initialize the break parameter struct member with the default value */
680 breakpara->runoffstate = TIMER_ROS_STATE_DISABLE;
681 breakpara->ideloffstate = TIMER_IOS_STATE_DISABLE;
682 breakpara->deadtime = 0U;
683 breakpara->breakpolarity = TIMER_BREAK_POLARITY_LOW;
684 breakpara->outputautostate = TIMER_OUTAUTO_DISABLE;
685 breakpara->protectmode = TIMER_CCHP_PROT_OFF;
686 breakpara->breakstate = TIMER_BREAK_DISABLE;
687 }
688
689 /*!
690 \brief configure TIMER break function
691 \param[in] timer_periph: TIMERx(x=0,14..16)
692 \param[in] breakpara: TIMER break parameter struct
693 runoffstate: TIMER_ROS_STATE_ENABLE,TIMER_ROS_STATE_DISABLE
694 ideloffstate: TIMER_IOS_STATE_ENABLE,TIMER_IOS_STATE_DISABLE
695 deadtime: 0~255
696 breakpolarity: TIMER_BREAK_POLARITY_LOW,TIMER_BREAK_POLARITY_HIGH
697 outputautostate: TIMER_OUTAUTO_ENABLE,TIMER_OUTAUTO_DISABLE
698 protectmode: TIMER_CCHP_PROT_OFF,TIMER_CCHP_PROT_0,TIMER_CCHP_PROT_1,TIMER_CCHP_PROT_2
699 breakstate: TIMER_BREAK_ENABLE,TIMER_BREAK_DISABLE
700 \param[out] none
701 \retval none
702 */
timer_break_config(uint32_t timer_periph,timer_break_parameter_struct * breakpara)703 void timer_break_config(uint32_t timer_periph, timer_break_parameter_struct* breakpara)
704 {
705 TIMER_CCHP(timer_periph) = (uint32_t)(((uint32_t)(breakpara->runoffstate))|
706 ((uint32_t)(breakpara->ideloffstate))|
707 ((uint32_t)(breakpara->deadtime))|
708 ((uint32_t)(breakpara->breakpolarity))|
709 ((uint32_t)(breakpara->outputautostate)) |
710 ((uint32_t)(breakpara->protectmode))|
711 ((uint32_t)(breakpara->breakstate))) ;
712 }
713
714 /*!
715 \brief enable TIMER break function
716 \param[in] timer_periph: TIMERx(x=0,14..16)
717 \param[out] none
718 \retval none
719 */
timer_break_enable(uint32_t timer_periph)720 void timer_break_enable(uint32_t timer_periph)
721 {
722 TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_BRKEN;
723 }
724
725 /*!
726 \brief disable TIMER break function
727 \param[in] timer_periph: TIMERx(x=0,14..16)
728 \param[out] none
729 \retval none
730 */
timer_break_disable(uint32_t timer_periph)731 void timer_break_disable(uint32_t timer_periph)
732 {
733 TIMER_CCHP(timer_periph) &= ~(uint32_t)TIMER_CCHP_BRKEN;
734 }
735
736 /*!
737 \brief enable TIMER output automatic function
738 \param[in] timer_periph: TIMERx(x=0,14..16)
739 \param[out] none
740 \retval none
741 */
timer_automatic_output_enable(uint32_t timer_periph)742 void timer_automatic_output_enable(uint32_t timer_periph)
743 {
744 TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_OAEN;
745 }
746
747 /*!
748 \brief disable TIMER output automatic function
749 \param[in] timer_periph: TIMERx(x=0,14..16)
750 \param[out] none
751 \retval none
752 */
timer_automatic_output_disable(uint32_t timer_periph)753 void timer_automatic_output_disable(uint32_t timer_periph)
754 {
755 TIMER_CCHP(timer_periph) &= ~(uint32_t)TIMER_CCHP_OAEN;
756 }
757
758 /*!
759 \brief configure TIMER primary output function
760 \param[in] timer_periph: TIMERx(x=0,14..16)
761 \param[in] newvalue: ENABLE or DISABLE
762 \param[out] none
763 \retval none
764 */
timer_primary_output_config(uint32_t timer_periph,ControlStatus newvalue)765 void timer_primary_output_config(uint32_t timer_periph, ControlStatus newvalue)
766 {
767 if(ENABLE == newvalue){
768 TIMER_CCHP(timer_periph) |= (uint32_t)TIMER_CCHP_POEN;
769 }else{
770 TIMER_CCHP(timer_periph) &= (~(uint32_t)TIMER_CCHP_POEN);
771 }
772 }
773
774 /*!
775 \brief enable or disable channel capture/compare control shadow register
776 \param[in] timer_periph: TIMERx(x=0,14..16)
777 \param[in] newvalue: ENABLE or DISABLE
778 \param[out] none
779 \retval none
780 */
timer_channel_control_shadow_config(uint32_t timer_periph,ControlStatus newvalue)781 void timer_channel_control_shadow_config(uint32_t timer_periph, ControlStatus newvalue)
782 {
783 if(ENABLE == newvalue){
784 TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_CCSE;
785 }else{
786 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_CCSE);
787 }
788 }
789
790 /*!
791 \brief configure TIMER channel control shadow register update control
792 \param[in] timer_periph: TIMERx(x=0,14..16)
793 \param[in] ccuctl: channel control shadow register update control
794 only one parameter can be selected which is shown as below:
795 \arg TIMER_UPDATECTL_CCU: the shadow registers update by when CMTG bit is set
796 \arg TIMER_UPDATECTL_CCUTRI: the shadow registers update by when CMTG bit is set or an rising edge of TRGI occurs
797 \param[out] none
798 \retval none
799 */
timer_channel_control_shadow_update_config(uint32_t timer_periph,uint8_t ccuctl)800 void timer_channel_control_shadow_update_config(uint32_t timer_periph, uint8_t ccuctl)
801 {
802 if(TIMER_UPDATECTL_CCU == ccuctl){
803 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_CCUC);
804 }else if(TIMER_UPDATECTL_CCUTRI == ccuctl){
805 TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_CCUC;
806 }else{
807 /* illegal parameters */
808 }
809 }
810
811 /*!
812 \brief initialize TIMER channel output parameter struct with a default value
813 \param[in] ocpara: TIMER channel n output parameter struct
814 \param[out] none
815 \retval none
816 */
timer_channel_output_struct_para_init(timer_oc_parameter_struct * ocpara)817 void timer_channel_output_struct_para_init(timer_oc_parameter_struct* ocpara)
818 {
819 /* initialize the channel output parameter struct member with the default value */
820 ocpara->outputstate = (uint16_t)TIMER_CCX_DISABLE;
821 ocpara->outputnstate = TIMER_CCXN_DISABLE;
822 ocpara->ocpolarity = TIMER_OC_POLARITY_HIGH;
823 ocpara->ocnpolarity = TIMER_OCN_POLARITY_HIGH;
824 ocpara->ocidlestate = TIMER_OC_IDLE_STATE_LOW;
825 ocpara->ocnidlestate = TIMER_OCN_IDLE_STATE_LOW;
826 }
827
828 /*!
829 \brief configure TIMER channel output function
830 \param[in] timer_periph: please refer to the following parameters
831 \param[in] channel:
832 only one parameter can be selected which is shown as below:
833 \arg TIMER_CH_0: TIMER channel 0(TIMERx(x=0..2,13..16))
834 \arg TIMER_CH_1: TIMER channel 1(TIMERx(x=0..2,14))
835 \arg TIMER_CH_2: TIMER channel 2(TIMERx(x=0..2))
836 \arg TIMER_CH_3: TIMER channel 3(TIMERx(x=0..2))
837 \param[in] ocpara: TIMER channeln output parameter struct
838 outputstate: TIMER_CCX_ENABLE,TIMER_CCX_DISABLE
839 outputnstate: TIMER_CCXN_ENABLE,TIMER_CCXN_DISABLE
840 ocpolarity: TIMER_OC_POLARITY_HIGH,TIMER_OC_POLARITY_LOW
841 ocnpolarity: TIMER_OCN_POLARITY_HIGH,TIMER_OCN_POLARITY_LOW
842 ocidlestate: TIMER_OC_IDLE_STATE_LOW,TIMER_OC_IDLE_STATE_HIGH
843 ocnidlestate: TIMER_OCN_IDLE_STATE_LOW,TIMER_OCN_IDLE_STATE_HIGH
844 \param[out] none
845 \retval none
846 */
timer_channel_output_config(uint32_t timer_periph,uint16_t channel,timer_oc_parameter_struct * ocpara)847 void timer_channel_output_config(uint32_t timer_periph, uint16_t channel, timer_oc_parameter_struct* ocpara)
848 {
849 switch(channel){
850 /* configure TIMER_CH_0 */
851 case TIMER_CH_0:
852 /* reset the CH0EN bit */
853 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
854 TIMER_CHCTL0(timer_periph) &= ~(uint32_t)TIMER_CHCTL0_CH0MS;
855 /* set the CH0EN bit */
856 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->outputstate;
857 /* reset the CH0P bit */
858 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0P);
859 /* set the CH0P bit */
860 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->ocpolarity;
861
862 if((TIMER0 == timer_periph) || (TIMER14 == timer_periph) || (TIMER15 == timer_periph) || (TIMER16 == timer_periph)){
863 /* reset the CH0NEN bit */
864 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NEN);
865 /* set the CH0NEN bit */
866 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->outputnstate;
867 /* reset the CH0NP bit */
868 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NP);
869 /* set the CH0NP bit */
870 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpara->ocnpolarity;
871 /* reset the ISO0 bit */
872 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO0);
873 /* set the ISO0 bit */
874 TIMER_CTL1(timer_periph) |= (uint32_t)ocpara->ocidlestate;
875 /* reset the ISO0N bit */
876 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO0N);
877 /* set the ISO0N bit */
878 TIMER_CTL1(timer_periph) |= (uint32_t)ocpara->ocnidlestate;
879 }
880 break;
881 /* configure TIMER_CH_1 */
882 case TIMER_CH_1:
883 /* reset the CH1EN bit */
884 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
885 TIMER_CHCTL0(timer_periph) &= ~(uint32_t)TIMER_CHCTL0_CH1MS;
886 /* set the CH1EN bit */
887 TIMER_CHCTL2(timer_periph) |= (uint32_t)(ocpara->outputstate << 4U);
888 /* reset the CH1P bit */
889 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1P);
890 /* set the CH1P bit */
891 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 4U);
892
893 if(TIMER0 == timer_periph){
894 /* reset the CH1NEN bit */
895 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NEN);
896 /* set the CH1NEN bit */
897 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->outputnstate) << 4U);
898 /* reset the CH1NP bit */
899 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NP);
900 /* set the CH1NP bit */
901 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnpolarity) << 4U);
902 /* reset the ISO1 bit */
903 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO1);
904 /* set the ISO1 bit */
905 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 2U);
906 /* reset the ISO1N bit */
907 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO1N);
908 /* set the ISO1N bit */
909 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnidlestate) << 2U);
910 }
911
912 if(TIMER14 == timer_periph){
913 /* reset the ISO1 bit */
914 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO1);
915 /* set the ISO1 bit */
916 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 2U);
917 }
918
919 break;
920 /* configure TIMER_CH_2 */
921 case TIMER_CH_2:
922 /* reset the CH2EN bit */
923 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
924 TIMER_CHCTL1(timer_periph) &= ~(uint32_t)TIMER_CHCTL1_CH2MS;
925 /* set the CH2EN bit */
926 TIMER_CHCTL2(timer_periph) |= (uint32_t)(ocpara->outputstate << 8U);
927 /* reset the CH2P bit */
928 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2P);
929 /* set the CH2P bit */
930 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 8U);
931
932 if(TIMER0 == timer_periph){
933 /* reset the CH2NEN bit */
934 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NEN);
935 /* set the CH2NEN bit */
936 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->outputnstate) << 8U);
937 /* reset the CH2NP bit */
938 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NP);
939 /* set the CH2NP bit */
940 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnpolarity) << 8U);
941 /* reset the ISO2 bit */
942 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO2);
943 /* set the ISO2 bit */
944 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 4U);
945 /* reset the ISO2N bit */
946 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO2N);
947 /* set the ISO2N bit */
948 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocnidlestate) << 4U);
949 }
950 break;
951 /* configure TIMER_CH_3 */
952 case TIMER_CH_3:
953 /* reset the CH3EN bit */
954 TIMER_CHCTL2(timer_periph) &=(~(uint32_t)TIMER_CHCTL2_CH3EN);
955 TIMER_CHCTL1(timer_periph) &= ~(uint32_t)TIMER_CHCTL1_CH3MS;
956 /* set the CH3EN bit */
957 TIMER_CHCTL2(timer_periph) |= (uint32_t)(ocpara->outputstate << 12U);
958 /* reset the CH3P bit */
959 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3P);
960 /* set the CH3P bit */
961 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocpolarity) << 12U);
962
963 if(TIMER0 == timer_periph){
964 /* reset the ISO3 bit */
965 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_ISO3);
966 /* set the ISO3 bit */
967 TIMER_CTL1(timer_periph) |= (uint32_t)((uint32_t)(ocpara->ocidlestate) << 6U);
968 }
969 break;
970 default:
971 break;
972 }
973 }
974
975 /*!
976 \brief configure TIMER channel output compare mode
977 \param[in] timer_periph: please refer to the following parameters
978 \param[in] channel:
979 only one parameter can be selected which is shown as below:
980 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
981 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
982 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
983 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
984 \param[in] ocmode: channel output compare mode
985 only one parameter can be selected which is shown as below:
986 \arg TIMER_OC_MODE_TIMING: timing mode
987 \arg TIMER_OC_MODE_ACTIVE: active mode
988 \arg TIMER_OC_MODE_INACTIVE: inactive mode
989 \arg TIMER_OC_MODE_TOGGLE: toggle mode
990 \arg TIMER_OC_MODE_LOW: force low mode
991 \arg TIMER_OC_MODE_HIGH: force high mode
992 \arg TIMER_OC_MODE_PWM0: PWM0 mode
993 \arg TIMER_OC_MODE_PWM1: PWM1 mode
994 \param[out] none
995 \retval none
996 */
timer_channel_output_mode_config(uint32_t timer_periph,uint16_t channel,uint16_t ocmode)997 void timer_channel_output_mode_config(uint32_t timer_periph, uint16_t channel, uint16_t ocmode)
998 {
999 switch(channel){
1000 /* configure TIMER_CH_0 */
1001 case TIMER_CH_0:
1002 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMCTL);
1003 TIMER_CHCTL0(timer_periph) |= (uint32_t)ocmode;
1004 break;
1005 /* configure TIMER_CH_1 */
1006 case TIMER_CH_1:
1007 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMCTL);
1008 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(ocmode) << 8U);
1009 break;
1010 /* configure TIMER_CH_2 */
1011 case TIMER_CH_2:
1012 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMCTL);
1013 TIMER_CHCTL1(timer_periph) |= (uint32_t)ocmode;
1014 break;
1015 /* configure TIMER_CH_3 */
1016 case TIMER_CH_3:
1017 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMCTL);
1018 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(ocmode) << 8U);
1019 break;
1020 default:
1021 break;
1022 }
1023 }
1024
1025 /*!
1026 \brief configure TIMER channel output pulse value
1027 \param[in] timer_periph: please refer to the following parameters
1028 \param[in] channel:
1029 only one parameter can be selected which is shown as below:
1030 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1031 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1032 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1033 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1034 \param[in] pulse: channel output pulse value,0~65535
1035 \param[out] none
1036 \retval none
1037 */
timer_channel_output_pulse_value_config(uint32_t timer_periph,uint16_t channel,uint32_t pulse)1038 void timer_channel_output_pulse_value_config(uint32_t timer_periph, uint16_t channel, uint32_t pulse)
1039 {
1040 switch(channel){
1041 /* configure TIMER_CH_0 */
1042 case TIMER_CH_0:
1043 TIMER_CH0CV(timer_periph) = (uint32_t)pulse;
1044 break;
1045 /* configure TIMER_CH_1 */
1046 case TIMER_CH_1:
1047 TIMER_CH1CV(timer_periph) = (uint32_t)pulse;
1048 break;
1049 /* configure TIMER_CH_2 */
1050 case TIMER_CH_2:
1051 TIMER_CH2CV(timer_periph) = (uint32_t)pulse;
1052 break;
1053 /* configure TIMER_CH_3 */
1054 case TIMER_CH_3:
1055 TIMER_CH3CV(timer_periph) = (uint32_t)pulse;
1056 break;
1057 default:
1058 break;
1059 }
1060 }
1061
1062 /*!
1063 \brief configure TIMER channel output shadow function
1064 \param[in] timer_periph: please refer to the following parameters
1065 \param[in] channel:
1066 only one parameter can be selected which is shown as below:
1067 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1068 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1069 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1070 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1071 \param[in] ocshadow: channel output shadow state
1072 only one parameter can be selected which is shown as below:
1073 \arg TIMER_OC_SHADOW_ENABLE: channel output shadow state enable
1074 \arg TIMER_OC_SHADOW_DISABLE: channel output shadow state disable
1075 \param[out] none
1076 \retval none
1077 */
timer_channel_output_shadow_config(uint32_t timer_periph,uint16_t channel,uint16_t ocshadow)1078 void timer_channel_output_shadow_config(uint32_t timer_periph, uint16_t channel, uint16_t ocshadow)
1079 {
1080 switch(channel){
1081 /* configure TIMER_CH_0 */
1082 case TIMER_CH_0:
1083 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMSEN);
1084 TIMER_CHCTL0(timer_periph) |= (uint32_t)ocshadow;
1085 break;
1086 /* configure TIMER_CH_1 */
1087 case TIMER_CH_1:
1088 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMSEN);
1089 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(ocshadow) << 8U);
1090 break;
1091 /* configure TIMER_CH_2 */
1092 case TIMER_CH_2:
1093 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMSEN);
1094 TIMER_CHCTL1(timer_periph) |= (uint32_t)ocshadow;
1095 break;
1096 /* configure TIMER_CH_3 */
1097 case TIMER_CH_3:
1098 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMSEN);
1099 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(ocshadow) << 8U);
1100 break;
1101 default:
1102 break;
1103 }
1104 }
1105
1106 /*!
1107 \brief configure TIMER channel output fast function
1108 \param[in] timer_periph: please refer to the following parameters
1109 \param[in] channel:
1110 only one parameter can be selected which is shown as below:
1111 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1112 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1113 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1114 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1115 \param[in] ocfast: channel output fast function
1116 only one parameter can be selected which is shown as below:
1117 \arg TIMER_OC_FAST_ENABLE: channel output fast function enable
1118 \arg TIMER_OC_FAST_DISABLE: channel output fast function disable
1119 \param[out] none
1120 \retval none
1121 */
timer_channel_output_fast_config(uint32_t timer_periph,uint16_t channel,uint16_t ocfast)1122 void timer_channel_output_fast_config(uint32_t timer_periph, uint16_t channel, uint16_t ocfast)
1123 {
1124 switch(channel){
1125 /* configure TIMER_CH_0 */
1126 case TIMER_CH_0:
1127 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMFEN);
1128 TIMER_CHCTL0(timer_periph) |= (uint32_t)ocfast;
1129 break;
1130 /* configure TIMER_CH_1 */
1131 case TIMER_CH_1:
1132 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMFEN);
1133 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)ocfast << 8U);
1134 break;
1135 /* configure TIMER_CH_2 */
1136 case TIMER_CH_2:
1137 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMFEN);
1138 TIMER_CHCTL1(timer_periph) |= (uint32_t)ocfast;
1139 break;
1140 /* configure TIMER_CH_3 */
1141 case TIMER_CH_3:
1142 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMFEN);
1143 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)ocfast << 8U);
1144 break;
1145 default:
1146 break;
1147 }
1148 }
1149
1150 /*!
1151 \brief configure TIMER channel output clear function
1152 \param[in] timer_periph: please refer to the following parameters
1153 \param[in] channel:
1154 only one parameter can be selected which is shown as below:
1155 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2))
1156 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2))
1157 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1158 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1159 \param[in] occlear: channel output clear function
1160 only one parameter can be selected which is shown as below:
1161 \arg TIMER_OC_CLEAR_ENABLE: channel output clear function enable
1162 \arg TIMER_OC_CLEAR_DISABLE: channel output clear function disable
1163 \param[out] none
1164 \retval none
1165 */
timer_channel_output_clear_config(uint32_t timer_periph,uint16_t channel,uint16_t occlear)1166 void timer_channel_output_clear_config(uint32_t timer_periph, uint16_t channel, uint16_t occlear)
1167 {
1168 switch(channel){
1169 /* configure TIMER_CH_0 */
1170 case TIMER_CH_0:
1171 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0COMCEN);
1172 TIMER_CHCTL0(timer_periph) |= (uint32_t)occlear;
1173 break;
1174 /* configure TIMER_CH_1 */
1175 case TIMER_CH_1:
1176 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1COMCEN);
1177 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)occlear << 8U);
1178 break;
1179 /* configure TIMER_CH_2 */
1180 case TIMER_CH_2:
1181 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2COMCEN);
1182 TIMER_CHCTL1(timer_periph) |= (uint32_t)occlear;
1183 break;
1184 /* configure TIMER_CH_3 */
1185 case TIMER_CH_3:
1186 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3COMCEN);
1187 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)occlear << 8U);
1188 break;
1189 default:
1190 break;
1191 }
1192 }
1193
1194 /*!
1195 \brief configure TIMER channel output polarity
1196 \param[in] timer_periph: please refer to the following parameters
1197 \param[in] channel:
1198 only one parameter can be selected which is shown as below:
1199 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1200 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1201 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1202 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1203 \param[in] ocpolarity: channel output polarity
1204 only one parameter can be selected which is shown as below:
1205 \arg TIMER_OC_POLARITY_HIGH: channel output polarity is high
1206 \arg TIMER_OC_POLARITY_LOW: channel output polarity is low
1207 \param[out] none
1208 \retval none
1209 */
timer_channel_output_polarity_config(uint32_t timer_periph,uint16_t channel,uint16_t ocpolarity)1210 void timer_channel_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocpolarity)
1211 {
1212 switch(channel){
1213 /* configure TIMER_CH_0 */
1214 case TIMER_CH_0:
1215 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0P);
1216 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocpolarity;
1217 break;
1218 /* configure TIMER_CH_1 */
1219 case TIMER_CH_1:
1220 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1P);
1221 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 4U);
1222 break;
1223 /* configure TIMER_CH_2 */
1224 case TIMER_CH_2:
1225 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2P);
1226 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 8U);
1227 break;
1228 /* configure TIMER_CH_3 */
1229 case TIMER_CH_3:
1230 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3P);
1231 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocpolarity << 12U);
1232 break;
1233 default:
1234 break;
1235 }
1236 }
1237
1238 /*!
1239 \brief configure TIMER channel complementary output polarity
1240 \param[in] timer_periph: TIMERx(x=0..2,14)
1241 \param[in] channel:
1242 only one parameter can be selected which is shown as below:
1243 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1244 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1245 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1246 \arg TIMER_CH_3: TIMER channel2(TIMERx(x=1,2))
1247 \param[in] ocnpolarity: channel complementary output polarity
1248 only one parameter can be selected which is shown as below:
1249 \arg TIMER_OCN_POLARITY_HIGH: channel complementary output polarity is high
1250 \arg TIMER_OCN_POLARITY_LOW: channel complementary output polarity is low
1251 \param[out] none
1252 \retval none
1253 */
timer_channel_complementary_output_polarity_config(uint32_t timer_periph,uint16_t channel,uint16_t ocnpolarity)1254 void timer_channel_complementary_output_polarity_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnpolarity)
1255 {
1256 switch(channel){
1257 /* configure TIMER_CH_0 */
1258 case TIMER_CH_0:
1259 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NP);
1260 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocnpolarity;
1261 break;
1262 /* configure TIMER_CH_1 */
1263 case TIMER_CH_1:
1264 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NP);
1265 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnpolarity << 4U);
1266 break;
1267 /* configure TIMER_CH_2 */
1268 case TIMER_CH_2:
1269 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NP);
1270 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnpolarity << 8U);
1271 break;
1272 /* configure TIMER_CH_3 */
1273 case TIMER_CH_3:
1274 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3NP);
1275 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnpolarity << 12U);
1276 break;
1277 default:
1278 break;
1279 }
1280 }
1281
1282 /*!
1283 \brief configure TIMER channel enable state
1284 \param[in] timer_periph: please refer to the following parameters
1285 \param[in] channel:
1286 only one parameter can be selected which is shown as below:
1287 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1288 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1289 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1290 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1291 \param[in] state: TIMER channel enable state
1292 only one parameter can be selected which is shown as below:
1293 \arg TIMER_CCX_ENABLE: channel enable
1294 \arg TIMER_CCX_DISABLE: channel disable
1295 \param[out] none
1296 \retval none
1297 */
timer_channel_output_state_config(uint32_t timer_periph,uint16_t channel,uint32_t state)1298 void timer_channel_output_state_config(uint32_t timer_periph, uint16_t channel, uint32_t state)
1299 {
1300 switch(channel){
1301 /* configure TIMER_CH_0 */
1302 case TIMER_CH_0:
1303 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
1304 TIMER_CHCTL2(timer_periph) |= (uint32_t)state;
1305 break;
1306 /* configure TIMER_CH_1 */
1307 case TIMER_CH_1:
1308 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
1309 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 4U);
1310 break;
1311 /* configure TIMER_CH_2 */
1312 case TIMER_CH_2:
1313 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
1314 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 8U);
1315 break;
1316 /* configure TIMER_CH_3 */
1317 case TIMER_CH_3:
1318 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3EN);
1319 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)state << 12U);
1320 break;
1321 default:
1322 break;
1323 }
1324 }
1325
1326 /*!
1327 \brief configure TIMER channel complementary output enable state
1328 \param[in] timer_periph: TIMERx(x=0,14..16)
1329 \param[in] channel:
1330 only one parameter can be selected which is shown as below:
1331 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0,14..16))
1332 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0))
1333 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0))
1334 \param[in] ocnstate: TIMER channel complementary output enable state
1335 only one parameter can be selected which is shown as below:
1336 \arg TIMER_CCXN_ENABLE: channel complementary enable
1337 \arg TIMER_CCXN_DISABLE: channel complementary disable
1338 \param[out] none
1339 \retval none
1340 */
timer_channel_complementary_output_state_config(uint32_t timer_periph,uint16_t channel,uint16_t ocnstate)1341 void timer_channel_complementary_output_state_config(uint32_t timer_periph, uint16_t channel, uint16_t ocnstate)
1342 {
1343 switch(channel){
1344 /* configure TIMER_CH_0 */
1345 case TIMER_CH_0:
1346 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0NEN);
1347 TIMER_CHCTL2(timer_periph) |= (uint32_t)ocnstate;
1348 break;
1349 /* configure TIMER_CH_1 */
1350 case TIMER_CH_1:
1351 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1NEN);
1352 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnstate << 4U);
1353 break;
1354 /* configure TIMER_CH_2 */
1355 case TIMER_CH_2:
1356 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2NEN);
1357 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)ocnstate << 8U);
1358 break;
1359 default:
1360 break;
1361 }
1362 }
1363
1364 /*!
1365 \brief initialize TIMER channel input parameter struct with a default value
1366 \param[in] icpara: TIMER channel intput parameter struct
1367 \param[out] none
1368 \retval none
1369 */
timer_channel_input_struct_para_init(timer_ic_parameter_struct * icpara)1370 void timer_channel_input_struct_para_init(timer_ic_parameter_struct* icpara)
1371 {
1372 /* initialize the channel input parameter struct member with the default value */
1373 icpara->icpolarity = TIMER_IC_POLARITY_RISING;
1374 icpara->icselection = TIMER_IC_SELECTION_DIRECTTI;
1375 icpara->icprescaler = TIMER_IC_PSC_DIV1;
1376 icpara->icfilter = 0U;
1377 }
1378
1379 /*!
1380 \brief configure TIMER input capture parameter
1381 \param[in] timer_periph: please refer to the following parameters
1382 \param[in] channel:
1383 only one parameter can be selected which is shown as below:
1384 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1385 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1386 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1387 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1388 \param[in] icpara: TIMER channel intput parameter struct
1389 icpolarity: TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_FALLING,TIMER_IC_POLARITY_BOTH_EDGE
1390 icselection: TIMER_IC_SELECTION_DIRECTTI,TIMER_IC_SELECTION_INDIRECTTI,TIMER_IC_SELECTION_ITS
1391 icprescaler: TIMER_IC_PSC_DIV1,TIMER_IC_PSC_DIV2,TIMER_IC_PSC_DIV4,TIMER_IC_PSC_DIV8
1392 icfilter: 0~15
1393 \param[out] none
1394 \retval none
1395 */
timer_input_capture_config(uint32_t timer_periph,uint16_t channel,timer_ic_parameter_struct * icpara)1396 void timer_input_capture_config(uint32_t timer_periph,uint16_t channel, timer_ic_parameter_struct* icpara)
1397 {
1398 switch(channel){
1399 /* configure TIMER_CH_0 */
1400 case TIMER_CH_0:
1401 /* reset the CH0EN bit */
1402 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
1403
1404 /* reset the CH0P and CH0NP bits */
1405 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P | TIMER_CHCTL2_CH0NP));
1406 TIMER_CHCTL2(timer_periph) |= (uint32_t)(icpara->icpolarity);
1407 /* reset the CH0MS bit */
1408 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
1409 TIMER_CHCTL0(timer_periph) |= (uint32_t)(icpara->icselection);
1410 /* reset the CH0CAPFLT bit */
1411 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
1412 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 4U);
1413
1414 /* set the CH0EN bit */
1415 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
1416 break;
1417
1418 /* configure TIMER_CH_1 */
1419 case TIMER_CH_1:
1420 /* reset the CH1EN bit */
1421 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
1422
1423 /* reset the CH1P and CH1NP bits */
1424 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P | TIMER_CHCTL2_CH1NP));
1425 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 4U);
1426 /* reset the CH1MS bit */
1427 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
1428 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection) << 8U);
1429 /* reset the CH1CAPFLT bit */
1430 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
1431 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 12U);
1432
1433 /* set the CH1EN bit */
1434 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
1435 break;
1436 /* configure TIMER_CH_2 */
1437 case TIMER_CH_2:
1438 /* reset the CH2EN bit */
1439 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH2EN);
1440
1441 /* reset the CH2P and CH2NP bits */
1442 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH2P|TIMER_CHCTL2_CH2NP));
1443 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 8U);
1444
1445 /* reset the CH2MS bit */
1446 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2MS);
1447 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection));
1448
1449 /* reset the CH2CAPFLT bit */
1450 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2CAPFLT);
1451 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 4U);
1452
1453 /* set the CH2EN bit */
1454 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH2EN;
1455 break;
1456 /* configure TIMER_CH_3 */
1457 case TIMER_CH_3:
1458 /* reset the CH3EN bit */
1459 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH3EN);
1460
1461 /* reset the CH3P and CH3NP bits */
1462 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH3P|TIMER_CHCTL2_CH3NP));
1463 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpara->icpolarity) << 12U);
1464
1465 /* reset the CH3MS bit */
1466 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3MS);
1467 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icselection) << 8U);
1468
1469 /* reset the CH3CAPFLT bit */
1470 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3CAPFLT);
1471 TIMER_CHCTL1(timer_periph) |= (uint32_t)((uint32_t)(icpara->icfilter) << 12U);
1472
1473 /* set the CH3EN bit */
1474 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH3EN;
1475 break;
1476 default:
1477 break;
1478 }
1479 /* configure TIMER channel input capture prescaler value */
1480 timer_channel_input_capture_prescaler_config(timer_periph, channel, (uint16_t)(icpara->icprescaler));
1481 }
1482
1483 /*!
1484 \brief configure TIMER channel input capture prescaler value
1485 \param[in] timer_periph: please refer to the following parameters
1486 \param[in] channel:
1487 only one parameter can be selected which is shown as below:
1488 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1489 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1490 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1491 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1492 \param[in] prescaler: channel input capture prescaler value
1493 only one parameter can be selected which is shown as below:
1494 \arg TIMER_IC_PSC_DIV1: no prescaler
1495 \arg TIMER_IC_PSC_DIV2: divided by 2
1496 \arg TIMER_IC_PSC_DIV4: divided by 4
1497 \arg TIMER_IC_PSC_DIV8: divided by 8
1498 \param[out] none
1499 \retval none
1500 */
timer_channel_input_capture_prescaler_config(uint32_t timer_periph,uint16_t channel,uint16_t prescaler)1501 void timer_channel_input_capture_prescaler_config(uint32_t timer_periph, uint16_t channel, uint16_t prescaler)
1502 {
1503 switch(channel){
1504 /* configure TIMER_CH_0 */
1505 case TIMER_CH_0:
1506 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPPSC);
1507 TIMER_CHCTL0(timer_periph) |= (uint32_t)prescaler;
1508 break;
1509 /* configure TIMER_CH_1 */
1510 case TIMER_CH_1:
1511 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPPSC);
1512 TIMER_CHCTL0(timer_periph) |= ((uint32_t)prescaler << 8U);
1513 break;
1514 /* configure TIMER_CH_2 */
1515 case TIMER_CH_2:
1516 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH2CAPPSC);
1517 TIMER_CHCTL1(timer_periph) |= (uint32_t)prescaler;
1518 break;
1519 /* configure TIMER_CH_3 */
1520 case TIMER_CH_3:
1521 TIMER_CHCTL1(timer_periph) &= (~(uint32_t)TIMER_CHCTL1_CH3CAPPSC);
1522 TIMER_CHCTL1(timer_periph) |= ((uint32_t)prescaler << 8U);
1523 break;
1524 default:
1525 break;
1526 }
1527 }
1528
1529 /*!
1530 \brief read TIMER channel capture compare register value
1531 \param[in] timer_periph: please refer to the following parameters
1532 \param[in] channel:
1533 only one parameter can be selected which is shown as below:
1534 \arg TIMER_CH_0: TIMER channel0(TIMERx(x=0..2,13..16))
1535 \arg TIMER_CH_1: TIMER channel1(TIMERx(x=0..2,14))
1536 \arg TIMER_CH_2: TIMER channel2(TIMERx(x=0..2))
1537 \arg TIMER_CH_3: TIMER channel3(TIMERx(x=0..2))
1538 \param[out] none
1539 \retval channel capture compare register value
1540 */
timer_channel_capture_value_register_read(uint32_t timer_periph,uint16_t channel)1541 uint32_t timer_channel_capture_value_register_read(uint32_t timer_periph, uint16_t channel)
1542 {
1543 uint32_t count_value = 0U;
1544
1545 switch(channel){
1546 /* read TIMER channel 0 capture compare register value */
1547 case TIMER_CH_0:
1548 count_value = TIMER_CH0CV(timer_periph);
1549 break;
1550 /* read TIMER channel 1 capture compare register value */
1551 case TIMER_CH_1:
1552 count_value = TIMER_CH1CV(timer_periph);
1553 break;
1554 /* read TIMER channel 2 capture compare register value */
1555 case TIMER_CH_2:
1556 count_value = TIMER_CH2CV(timer_periph);
1557 break;
1558 /* read TIMER channel 3 capture compare register value */
1559 case TIMER_CH_3:
1560 count_value = TIMER_CH3CV(timer_periph);
1561 break;
1562 default:
1563 break;
1564 }
1565 return (count_value);
1566 }
1567
1568 /*!
1569 \brief configure TIMER input pwm capture function
1570 \param[in] timer_periph: TIMERx(x=0..2,14)
1571 \param[in] channel:
1572 only one parameter can be selected which is shown as below:
1573 \arg TIMER_CH_0: TIMER channel0
1574 \arg TIMER_CH_1: TIMER channel1
1575 \param[in] icpwm:TIMER channel intput pwm parameter struct
1576 icpolarity: TIMER_IC_POLARITY_RISING,TIMER_IC_POLARITY_FALLING
1577 icselection: TIMER_IC_SELECTION_DIRECTTI,TIMER_IC_SELECTION_INDIRECTTI
1578 icprescaler: TIMER_IC_PSC_DIV1,TIMER_IC_PSC_DIV2,TIMER_IC_PSC_DIV4,TIMER_IC_PSC_DIV8
1579 icfilter: 0~15
1580 \param[out] none
1581 \retval none
1582 */
timer_input_pwm_capture_config(uint32_t timer_periph,uint16_t channel,timer_ic_parameter_struct * icpwm)1583 void timer_input_pwm_capture_config(uint32_t timer_periph, uint16_t channel, timer_ic_parameter_struct* icpwm)
1584 {
1585 uint16_t icpolarity = 0x0U;
1586 uint16_t icselection = 0x0U;
1587
1588 /* Set channel input polarity */
1589 if(TIMER_IC_POLARITY_RISING == icpwm->icpolarity){
1590 icpolarity = TIMER_IC_POLARITY_FALLING;
1591 }else{
1592 icpolarity = TIMER_IC_POLARITY_RISING;
1593 }
1594
1595 /* Set channel input mode selection */
1596 if(TIMER_IC_SELECTION_DIRECTTI == icpwm->icselection){
1597 icselection = TIMER_IC_SELECTION_INDIRECTTI;
1598 }else{
1599 icselection = TIMER_IC_SELECTION_DIRECTTI;
1600 }
1601
1602 if(TIMER_CH_0 == channel){
1603 /* reset the CH0EN bit */
1604 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
1605 /* reset the CH0P and CH0NP bits */
1606 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P|TIMER_CHCTL2_CH0NP));
1607 /* set the CH0P and CH0NP bits */
1608 TIMER_CHCTL2(timer_periph) |= (uint32_t)(icpwm->icpolarity);
1609 /* reset the CH0MS bit */
1610 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
1611 /* set the CH0MS bit */
1612 TIMER_CHCTL0(timer_periph) |= (uint32_t)(icpwm->icselection);
1613 /* reset the CH0CAPFLT bit */
1614 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
1615 /* set the CH0CAPFLT bit */
1616 TIMER_CHCTL0(timer_periph) |= ((uint32_t)(icpwm->icfilter) << 4U);
1617 /* set the CH0EN bit */
1618 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
1619 /* configure TIMER channel input capture prescaler value */
1620 timer_channel_input_capture_prescaler_config(timer_periph,TIMER_CH_0,(uint16_t)(icpwm->icprescaler));
1621
1622 /* reset the CH1EN bit */
1623 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
1624 /* reset the CH1P and CH1NP bits */
1625 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P|TIMER_CHCTL2_CH1NP));
1626 /* set the CH1P and CH1NP bits */
1627 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)icpolarity << 4U);
1628 /* reset the CH1MS bit */
1629 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
1630 /* set the CH1MS bit */
1631 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)icselection << 8U);
1632 /* reset the CH1CAPFLT bit */
1633 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
1634 /* set the CH1CAPFLT bit */
1635 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icfilter) << 12U);
1636 /* set the CH1EN bit */
1637 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
1638 /* configure TIMER channel input capture prescaler value */
1639 timer_channel_input_capture_prescaler_config(timer_periph,TIMER_CH_1,(uint16_t)(icpwm->icprescaler));
1640 }else{
1641 /* reset the CH1EN bit */
1642 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
1643 /* reset the CH1P and CH1NP bits */
1644 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P|TIMER_CHCTL2_CH1NP));
1645 /* set the CH1P and CH1NP bits */
1646 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icpolarity) << 4U);
1647 /* reset the CH1MS bit */
1648 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
1649 /* set the CH1MS bit */
1650 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icselection) << 8U);
1651 /* reset the CH1CAPFLT bit */
1652 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
1653 /* set the CH1CAPFLT bit */
1654 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)(icpwm->icfilter) << 12U);
1655 /* set the CH1EN bit */
1656 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
1657 /* configure TIMER channel input capture prescaler value */
1658 timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_1, (uint16_t)(icpwm->icprescaler));
1659
1660 /* reset the CH0EN bit */
1661 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
1662 /* reset the CH0P and CH0NP bits */
1663 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P|TIMER_CHCTL2_CH0NP));
1664 /* set the CH0P and CH0NP bits */
1665 TIMER_CHCTL2(timer_periph) |= (uint32_t)icpolarity;
1666 /* reset the CH0MS bit */
1667 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
1668 /* set the CH0MS bit */
1669 TIMER_CHCTL0(timer_periph) |= (uint32_t)icselection;
1670 /* reset the CH0CAPFLT bit */
1671 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
1672 /* set the CH0CAPFLT bit */
1673 TIMER_CHCTL0(timer_periph) |= ((uint32_t)(icpwm->icfilter) << 4U);
1674 /* set the CH0EN bit */
1675 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
1676 /* configure TIMER channel input capture prescaler value */
1677 timer_channel_input_capture_prescaler_config(timer_periph, TIMER_CH_0, (uint16_t)(icpwm->icprescaler));
1678 }
1679 }
1680
1681 /*!
1682 \brief configure TIMER hall sensor mode
1683 \param[in] timer_periph: TIMERx(x=0..2)
1684 \param[in] hallmode:
1685 only one parameter can be selected which is shown as below:
1686 \arg TIMER_HALLINTERFACE_ENABLE: TIMER hall sensor mode enable
1687 \arg TIMER_HALLINTERFACE_DISABLE: TIMER hall sensor mode disable
1688 \param[out] none
1689 \retval none
1690 */
timer_hall_mode_config(uint32_t timer_periph,uint8_t hallmode)1691 void timer_hall_mode_config(uint32_t timer_periph, uint8_t hallmode)
1692 {
1693 if(TIMER_HALLINTERFACE_ENABLE == hallmode){
1694 TIMER_CTL1(timer_periph) |= (uint32_t)TIMER_CTL1_TI0S;
1695 }else if(TIMER_HALLINTERFACE_DISABLE == hallmode){
1696 TIMER_CTL1(timer_periph) &= ~(uint32_t)TIMER_CTL1_TI0S;
1697 }else{
1698 /* illegal parameters */
1699 }
1700 }
1701
1702 /*!
1703 \brief select TIMER input trigger source
1704 \param[in] timer_periph: TIMERx(x=0..2,14)
1705 \param[in] intrigger:
1706 only one parameter can be selected which is shown as below:
1707 \arg TIMER_SMCFG_TRGSEL_ITI0: internal trigger 0(TIMERx(x=0..2,14))
1708 \arg TIMER_SMCFG_TRGSEL_ITI1: internal trigger 1(TIMERx(x=0..2,14))
1709 \arg TIMER_SMCFG_TRGSEL_ITI2: internal trigger 2(TIMERx(x=0..2))
1710 \arg TIMER_SMCFG_TRGSEL_CI0F_ED: TI0 edge detector(TIMERx(x=0..2,14))
1711 \arg TIMER_SMCFG_TRGSEL_CI0FE0: filtered TIMER input 0(TIMERx(x=0..2,14))
1712 \arg TIMER_SMCFG_TRGSEL_CI1FE1: filtered TIMER input 1(TIMERx(x=0..2,14))
1713 \arg TIMER_SMCFG_TRGSEL_ETIFP: external trigger(TIMERx(x=0..2))
1714 \param[out] none
1715 \retval none
1716 */
timer_input_trigger_source_select(uint32_t timer_periph,uint32_t intrigger)1717 void timer_input_trigger_source_select(uint32_t timer_periph, uint32_t intrigger)
1718 {
1719 TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_TRGS);
1720 TIMER_SMCFG(timer_periph) |= (uint32_t)intrigger;
1721 }
1722
1723 /*!
1724 \brief select TIMER master mode output trigger source
1725 \param[in] timer_periph: TIMERx(x=0..2,14),TIMER5 just for GD32F350
1726 \param[in] outrigger:
1727 only one parameter can be selected which is shown as below:
1728 \arg TIMER_TRI_OUT_SRC_RESET: the UPG bit as trigger output(TIMERx(x=0..2,14),TIMER5 just for GD32F350)
1729 \arg TIMER_TRI_OUT_SRC_ENABLE: the counter enable signal TIMER_CTL0_CEN as trigger output(TIMERx(x=0..2,14),TIMER5 just for GD32F350)
1730 \arg TIMER_TRI_OUT_SRC_UPDATE: update event as trigger output(TIMERx(x=0..2,14),TIMER5 just for GD32F350)
1731 \arg TIMER_TRI_OUT_SRC_CH0: a capture or a compare match occurred in channal0 as trigger output TRGO
1732 \arg TIMER_TRI_OUT_SRC_O0CPRE: O0CPRE as trigger output(TIMERx(x=0..2,14))
1733 \arg TIMER_TRI_OUT_SRC_O1CPRE: O1CPRE as trigger output(TIMERx(x=0..2,14))
1734 \arg TIMER_TRI_OUT_SRC_O2CPRE: O2CPRE as trigger output(TIMERx(x=0..2,14))
1735 \arg TIMER_TRI_OUT_SRC_O3CPRE: O3CPRE as trigger output(TIMERx(x=0..2,14))
1736 \param[out] none
1737 \retval none
1738 */
timer_master_output_trigger_source_select(uint32_t timer_periph,uint32_t outrigger)1739 void timer_master_output_trigger_source_select(uint32_t timer_periph, uint32_t outrigger)
1740 {
1741 TIMER_CTL1(timer_periph) &= (~(uint32_t)TIMER_CTL1_MMC);
1742 TIMER_CTL1(timer_periph) |= (uint32_t)outrigger;
1743 }
1744
1745 /*!
1746 \brief select TIMER slave mode
1747 \param[in] timer_periph: TIMERx(x=0..2,14)
1748 \param[in] slavemode:
1749 only one parameter can be selected which is shown as below:
1750 \arg TIMER_SLAVE_MODE_DISABLE: slave mode disable(TIMERx(x=0..2,14))
1751 \arg TIMER_ENCODER_MODE0: encoder mode 0(TIMERx(x=0..2))
1752 \arg TIMER_ENCODER_MODE1: encoder mode 1(TIMERx(x=0..2))
1753 \arg TIMER_ENCODER_MODE2: encoder mode 2(TIMERx(x=0..2))
1754 \arg TIMER_SLAVE_MODE_RESTART: restart mode(TIMERx(x=0..2,14))
1755 \arg TIMER_SLAVE_MODE_PAUSE: pause mode(TIMERx(x=0..2,14))
1756 \arg TIMER_SLAVE_MODE_EVENT: event mode(TIMERx(x=0..2,14))
1757 \arg TIMER_SLAVE_MODE_EXTERNAL0: external clock mode 0(TIMERx(x=0..2,14))
1758 \param[out] none
1759 \retval none
1760 */
1761
timer_slave_mode_select(uint32_t timer_periph,uint32_t slavemode)1762 void timer_slave_mode_select(uint32_t timer_periph, uint32_t slavemode)
1763 {
1764 TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
1765
1766 TIMER_SMCFG(timer_periph) |= (uint32_t)slavemode;
1767 }
1768
1769 /*!
1770 \brief configure TIMER master slave mode
1771 \param[in] timer_periph: TIMERx(x=0..2,14)
1772 \param[in] masterslave:
1773 only one parameter can be selected which is shown as below:
1774 \arg TIMER_MASTER_SLAVE_MODE_ENABLE: master slave mode enable
1775 \arg TIMER_MASTER_SLAVE_MODE_DISABLE: master slave mode disable
1776 \param[out] none
1777 \retval none
1778 */
timer_master_slave_mode_config(uint32_t timer_periph,uint8_t masterslave)1779 void timer_master_slave_mode_config(uint32_t timer_periph, uint8_t masterslave)
1780 {
1781 if(TIMER_MASTER_SLAVE_MODE_ENABLE == masterslave){
1782 TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SMCFG_MSM;
1783 }else if(TIMER_MASTER_SLAVE_MODE_DISABLE == masterslave){
1784 TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_MSM;
1785 }else{
1786 /* illegal parameters */
1787 }
1788 }
1789
1790 /*!
1791 \brief configure TIMER external trigger input
1792 \param[in] timer_periph: TIMERx(x=0..2)
1793 \param[in] extprescaler:
1794 only one parameter can be selected which is shown as below:
1795 \arg TIMER_EXT_TRI_PSC_OFF: no divided
1796 \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
1797 \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
1798 \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
1799 \param[in] extpolarity:
1800 only one parameter can be selected which is shown as below:
1801 \arg TIMER_ETP_FALLING: active low or falling edge active
1802 \arg TIMER_ETP_RISING: active high or rising edge active
1803 \param[in] extfilter: a value between 0 and 15
1804 \param[out] none
1805 \retval none
1806 */
timer_external_trigger_config(uint32_t timer_periph,uint32_t extprescaler,uint32_t extpolarity,uint32_t extfilter)1807 void timer_external_trigger_config(uint32_t timer_periph, uint32_t extprescaler,
1808 uint32_t extpolarity, uint32_t extfilter)
1809 {
1810 TIMER_SMCFG(timer_periph) &= (~(uint32_t)(TIMER_SMCFG_ETP | TIMER_SMCFG_ETPSC | TIMER_SMCFG_ETFC));
1811 TIMER_SMCFG(timer_periph) |= (uint32_t)(extprescaler | extpolarity);
1812 TIMER_SMCFG(timer_periph) |= (uint32_t)(extfilter << 8U);
1813 }
1814
1815 /*!
1816 \brief configure TIMER quadrature decoder mode
1817 \param[in] timer_periph: TIMERx(x=0..2)
1818 \param[in] decomode:
1819 only one parameter can be selected which is shown as below:
1820 \arg TIMER_ENCODER_MODE0: counter counts on CI0FE0 edge depending on CI1FE1 level
1821 \arg TIMER_ENCODER_MODE1: counter counts on CI1FE1 edge depending on CI0FE0 level
1822 \arg TIMER_ENCODER_MODE2: counter counts on both CI0FE0 and CI1FE1 edges depending on the level of the other input
1823 \param[in] ic0polarity:
1824 only one parameter can be selected which is shown as below:
1825 \arg TIMER_IC_POLARITY_RISING: capture rising edge
1826 \arg TIMER_IC_POLARITY_FALLING: capture falling edge
1827 \param[in] ic1polarity:
1828 only one parameter can be selected which is shown as below:
1829 \arg TIMER_IC_POLARITY_RISING: capture rising edge
1830 \arg TIMER_IC_POLARITY_FALLING: capture falling edge
1831 \param[out] none
1832 \retval none
1833 */
timer_quadrature_decoder_mode_config(uint32_t timer_periph,uint32_t decomode,uint16_t ic0polarity,uint16_t ic1polarity)1834 void timer_quadrature_decoder_mode_config(uint32_t timer_periph, uint32_t decomode,
1835 uint16_t ic0polarity, uint16_t ic1polarity)
1836 {
1837 TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
1838 TIMER_SMCFG(timer_periph) |= (uint32_t)decomode;
1839
1840 TIMER_CHCTL0(timer_periph) &= (uint32_t)(((~(uint32_t)TIMER_CHCTL0_CH0MS))&((~(uint32_t)TIMER_CHCTL0_CH1MS)));
1841 TIMER_CHCTL0(timer_periph) |= (uint32_t)(TIMER_IC_SELECTION_DIRECTTI|((uint32_t)TIMER_IC_SELECTION_DIRECTTI << 8U));
1842
1843 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P|TIMER_CHCTL2_CH0NP));
1844 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P|TIMER_CHCTL2_CH1NP));
1845 TIMER_CHCTL2(timer_periph) |= ((uint32_t)ic0polarity|((uint32_t)ic1polarity << 4U));
1846 }
1847
1848 /*!
1849 \brief configure TIMER internal clock mode
1850 \param[in] timer_periph: TIMERx(x=0..2,14)
1851 \param[out] none
1852 \retval none
1853 */
timer_internal_clock_config(uint32_t timer_periph)1854 void timer_internal_clock_config(uint32_t timer_periph)
1855 {
1856 TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC;
1857 }
1858
1859 /*!
1860 \brief configure TIMER the internal trigger as external clock input
1861 \param[in] timer_periph: TIMERx(x=0..2,14)
1862 \param[in] intrigger:
1863 only one parameter can be selected which is shown as below:
1864 \arg TIMER_SMCFG_TRGSEL_ITI0: internal trigger 0(TIMERx(x=0..2,14))
1865 \arg TIMER_SMCFG_TRGSEL_ITI1: internal trigger 1(TIMERx(x=0..2,14))
1866 \arg TIMER_SMCFG_TRGSEL_ITI2: internal trigger 2(TIMERx(x=0..2))
1867 \param[out] none
1868 \retval none
1869 */
timer_internal_trigger_as_external_clock_config(uint32_t timer_periph,uint32_t intrigger)1870 void timer_internal_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t intrigger)
1871 {
1872 timer_input_trigger_source_select(timer_periph, intrigger);
1873 TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC;
1874 TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SLAVE_MODE_EXTERNAL0;
1875 }
1876
1877 /*!
1878 \brief configure TIMER the external trigger as external clock input
1879 \param[in] timer_periph: TIMERx(x=0..2,14)
1880 \param[in] extrigger:
1881 only one parameter can be selected which is shown as below:
1882 \arg TIMER_SMCFG_TRGSEL_CI0F_ED: TI0 edge detector
1883 \arg TIMER_SMCFG_TRGSEL_CI0FE0: filtered TIMER input 0
1884 \arg TIMER_SMCFG_TRGSEL_CI1FE1: filtered TIMER input 1
1885 \param[in] extpolarity:
1886 only one parameter can be selected which is shown as below:
1887 \arg TIMER_IC_POLARITY_RISING: active high or rising edge active
1888 \arg TIMER_IC_POLARITY_FALLING: active low or falling edge active
1889 \arg TIMER_IC_POLARITY_BOTH_EDGE: active both edge
1890 \param[in] extfilter: a value between 0 and 15
1891 \param[out] none
1892 \retval none
1893 */
timer_external_trigger_as_external_clock_config(uint32_t timer_periph,uint32_t extrigger,uint16_t extpolarity,uint32_t extfilter)1894 void timer_external_trigger_as_external_clock_config(uint32_t timer_periph, uint32_t extrigger,
1895 uint16_t extpolarity, uint32_t extfilter)
1896 {
1897 if(TIMER_SMCFG_TRGSEL_CI1FE1 == extrigger){
1898 /* reset the CH1EN bit */
1899 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH1EN);
1900 /* reset the CH1NP bit */
1901 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH1P|TIMER_CHCTL2_CH1NP));
1902 /* set the CH1NP bit */
1903 TIMER_CHCTL2(timer_periph) |= (uint32_t)((uint32_t)extpolarity << 4U);
1904 /* reset the CH1MS bit */
1905 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1MS);
1906 /* set the CH1MS bit */
1907 TIMER_CHCTL0(timer_periph) |= (uint32_t)((uint32_t)TIMER_IC_SELECTION_DIRECTTI << 8U);
1908 /* reset the CH1CAPFLT bit */
1909 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH1CAPFLT);
1910 /* set the CH1CAPFLT bit */
1911 TIMER_CHCTL0(timer_periph) |= (uint32_t)(extfilter << 12U);
1912 /* set the CH1EN bit */
1913 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH1EN;
1914 }else{
1915 /* reset the CH0EN bit */
1916 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)TIMER_CHCTL2_CH0EN);
1917 /* reset the CH0P and CH0NP bits */
1918 TIMER_CHCTL2(timer_periph) &= (~(uint32_t)(TIMER_CHCTL2_CH0P|TIMER_CHCTL2_CH0NP));
1919 /* set the CH0P and CH0NP bits */
1920 TIMER_CHCTL2(timer_periph) |= (uint32_t)extpolarity;
1921 /* reset the CH0MS bit */
1922 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0MS);
1923 /* set the CH0MS bit */
1924 TIMER_CHCTL0(timer_periph) |= (uint32_t)TIMER_IC_SELECTION_DIRECTTI;
1925 /* reset the CH0CAPFLT bit */
1926 TIMER_CHCTL0(timer_periph) &= (~(uint32_t)TIMER_CHCTL0_CH0CAPFLT);
1927 /* reset the CH0CAPFLT bit */
1928 TIMER_CHCTL0(timer_periph) |= (uint32_t)(extfilter << 4U);
1929 /* set the CH0EN bit */
1930 TIMER_CHCTL2(timer_periph) |= (uint32_t)TIMER_CHCTL2_CH0EN;
1931 }
1932 /* select TIMER input trigger source */
1933 timer_input_trigger_source_select(timer_periph,extrigger);
1934 /* reset the SMC bit */
1935 TIMER_SMCFG(timer_periph) &= (~(uint32_t)TIMER_SMCFG_SMC);
1936 /* set the SMC bit */
1937 TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SLAVE_MODE_EXTERNAL0;
1938 }
1939
1940 /*!
1941 \brief configure TIMER the external clock mode0
1942 \param[in] timer_periph: TIMERx(x=0..2)
1943 \param[in] extprescaler:
1944 only one parameter can be selected which is shown as below:
1945 \arg TIMER_EXT_TRI_PSC_OFF: no divided
1946 \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
1947 \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
1948 \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
1949 \param[in] extpolarity:
1950 only one parameter can be selected which is shown as below:
1951 \arg TIMER_ETP_FALLING: active low or falling edge active
1952 \arg TIMER_ETP_RISING: active high or rising edge active
1953 \param[in] extfilter: a value between 0 and 15
1954 \param[out] none
1955 \retval none
1956 */
timer_external_clock_mode0_config(uint32_t timer_periph,uint32_t extprescaler,uint32_t extpolarity,uint32_t extfilter)1957 void timer_external_clock_mode0_config(uint32_t timer_periph, uint32_t extprescaler,
1958 uint32_t extpolarity, uint32_t extfilter)
1959 {
1960 /* configure TIMER external trigger input */
1961 timer_external_trigger_config(timer_periph, extprescaler, extpolarity, extfilter);
1962
1963 /* reset the SMC bit,TRGS bit */
1964 TIMER_SMCFG(timer_periph) &= (~(uint32_t)(TIMER_SMCFG_SMC | TIMER_SMCFG_TRGS));
1965 /* set the SMC bit,TRGS bit */
1966 TIMER_SMCFG(timer_periph) |= (uint32_t)(TIMER_SLAVE_MODE_EXTERNAL0 | TIMER_SMCFG_TRGSEL_ETIFP);
1967 }
1968
1969 /*!
1970 \brief configure TIMER the external clock mode1
1971 \param[in] timer_periph: TIMERx(x=0..2)
1972 \param[in] extprescaler:
1973 only one parameter can be selected which is shown as below:
1974 \arg TIMER_EXT_TRI_PSC_OFF: no divided
1975 \arg TIMER_EXT_TRI_PSC_DIV2: divided by 2
1976 \arg TIMER_EXT_TRI_PSC_DIV4: divided by 4
1977 \arg TIMER_EXT_TRI_PSC_DIV8: divided by 8
1978 \param[in] extpolarity:
1979 only one parameter can be selected which is shown as below:
1980 \arg TIMER_ETP_FALLING: active low or falling edge active
1981 \arg TIMER_ETP_RISING: active high or rising edge active
1982 \param[in] extfilter: a value between 0 and 15
1983 \param[out] none
1984 \retval none
1985 */
timer_external_clock_mode1_config(uint32_t timer_periph,uint32_t extprescaler,uint32_t extpolarity,uint32_t extfilter)1986 void timer_external_clock_mode1_config(uint32_t timer_periph, uint32_t extprescaler,
1987 uint32_t extpolarity, uint32_t extfilter)
1988 {
1989 /* configure TIMER external trigger input */
1990 timer_external_trigger_config(timer_periph, extprescaler, extpolarity, extfilter);
1991
1992 TIMER_SMCFG(timer_periph) |= (uint32_t)TIMER_SMCFG_SMC1;
1993 }
1994
1995 /*!
1996 \brief disable TIMER the external clock mode1
1997 \param[in] timer_periph: TIMERx(x=0..2)
1998 \param[out] none
1999 \retval none
2000 */
timer_external_clock_mode1_disable(uint32_t timer_periph)2001 void timer_external_clock_mode1_disable(uint32_t timer_periph)
2002 {
2003 TIMER_SMCFG(timer_periph) &= ~(uint32_t)TIMER_SMCFG_SMC1;
2004 }
2005
2006 /*!
2007 \brief configure TIMER channel remap function
2008 \param[in] timer_periph: TIMERx(x=13)
2009 \param[in] remap:
2010 only one parameter can be selected which is shown as below:
2011 \arg TIMER13_CI0_RMP_GPIO: timer13 channel 0 input is connected to GPIO(TIMER13_CH0)
2012 \arg TIMER13_CI0_RMP_RTCCLK: timer13 channel 0 input is connected to the RTCCLK
2013 \arg TIMER13_CI0_RMP_HXTAL_DIV32: timer13 channel 0 input is connected to HXTAL/32 clock
2014 \arg TIMER13_CI0_RMP_CKOUTSEL: timer13 channel 0 input is connected to CKOUTSEL
2015 \param[out] none
2016 \retval none
2017 */
timer_channel_remap_config(uint32_t timer_periph,uint32_t remap)2018 void timer_channel_remap_config(uint32_t timer_periph, uint32_t remap)
2019 {
2020 TIMER_IRMP(timer_periph) = (uint32_t)remap;
2021 }
2022
2023 /*!
2024 \brief configure TIMER write CHxVAL register selection
2025 \param[in] timer_periph: TIMERx(x=0..2,13..16)
2026 \param[in] ccsel:
2027 only one parameter can be selected which is shown as below:
2028 \arg TIMER_CHVSEL_DISABLE: no effect
2029 \arg TIMER_CHVSEL_ENABLE: when write the CHxVAL register, if the write value is same as the CHxVAL value, the write access is ignored
2030 \param[out] none
2031 \retval none
2032 */
timer_write_chxval_register_config(uint32_t timer_periph,uint16_t ccsel)2033 void timer_write_chxval_register_config(uint32_t timer_periph, uint16_t ccsel)
2034 {
2035 if(TIMER_CHVSEL_ENABLE == ccsel){
2036 TIMER_CFG(timer_periph) |= (uint32_t)TIMER_CFG_CHVSEL;
2037 }else if(TIMER_CHVSEL_DISABLE == ccsel){
2038 TIMER_CFG(timer_periph) &= ~(uint32_t)TIMER_CFG_CHVSEL;
2039 }else{
2040 /* illegal parameters */
2041 }
2042 }
2043
2044 /*!
2045 \brief configure TIMER output value selection
2046 \param[in] timer_periph: TIMERx(x=0,14..16)
2047 \param[in] outsel:
2048 only one parameter can be selected which is shown as below:
2049 \arg TIMER_OUTSEL_DISABLE: no effect
2050 \arg TIMER_OUTSEL_ENABLE: if POEN and IOS is 0, the output disabled
2051 \param[out] none
2052 \retval none
2053 */
timer_output_value_selection_config(uint32_t timer_periph,uint16_t outsel)2054 void timer_output_value_selection_config(uint32_t timer_periph, uint16_t outsel)
2055 {
2056 if(TIMER_OUTSEL_ENABLE == outsel){
2057 TIMER_CFG(timer_periph) |= (uint32_t)TIMER_CFG_OUTSEL;
2058 }else if(TIMER_OUTSEL_DISABLE == outsel){
2059 TIMER_CFG(timer_periph) &= ~(uint32_t)TIMER_CFG_OUTSEL;
2060 }else{
2061 /* illegal parameters */
2062 }
2063 }
2064