miniFOC 1.0.3
This open-source project aims to accomplish a FOC(Field Oriented Control) scheme that is operatable with minimum costs in China.
timer.c
Go to the documentation of this file.
1/**************************************************************************/
10#include "timer.h"
11#include "gd32f1x0.h"
12#include "config.h"
13
20void update_pwm_dutycycle(float ch0, float ch1, float ch2) {
21 /* update the comparison register of timer1 */
22 TIMER_CH0CV(TIMER1) = (uint32_t) ((float) PWM_PERIOD * ch0);
23 TIMER_CH1CV(TIMER1) = (uint32_t) ((float) PWM_PERIOD * ch1);
24 TIMER_CH2CV(TIMER1) = (uint32_t) ((float) PWM_PERIOD * ch2);
25}
26
30void timer13_disable(void) {
31 /* stop TIMER13 and deinit */
32 timer_disable(TIMER13);
33 timer_deinit(TIMER13);
34
35 /* disable TIMER13 update interrupt and clock*/
36 timer_interrupt_disable(TIMER13, TIMER_INT_UP);
37 nvic_irq_disable(TIMER13_IRQn);
38 rcu_periph_clock_disable(RCU_TIMER13);
39}
40
44void timer2_disable(void) {
45 /* stop TIMER2 and deinit */
46 timer_disable(TIMER2);
47 timer_deinit(TIMER2);
48
49 /* disable TIMER2 update interrupt and clock */
50 timer_interrupt_disable(TIMER2, TIMER_INT_UP);
51 nvic_irq_disable(TIMER2_IRQn);
52 rcu_periph_clock_disable(RCU_TIMER2);
53
54 /* zero the torque in all directions to release the motor */
55 update_pwm_dutycycle(0.5f, 0.5f, 0.5f);
56}
57
61void timer2_config(void) {
62 timer_parameter_struct timer_initpara;
63
64 /* enable TIMER2 clock*/
65 rcu_periph_clock_enable(RCU_TIMER2);
66 timer_deinit(TIMER2);
67
68 /* TIMER1CLK = SystemCoreClock / 72 = 1MHz */
69 timer_initpara.prescaler = 71;
70
71 /* timer edge alignment up count mode */
72 timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
73 timer_initpara.counterdirection = TIMER_COUNTER_UP;
74
75 /* Period = 1000 / TIM2_FREQUENCY (kHz) */
76 timer_initpara.period = (1000 / TIM2_FREQUENCY) - 1;
77 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
78 timer_init(TIMER2, &timer_initpara);
79
80 /* enable TIMER2 update interrupt */
81 timer_interrupt_enable(TIMER2, TIMER_INT_UP);
82 timer_enable(TIMER2);
83 nvic_irq_enable(TIMER2_IRQn, TIM2_PRIORITY, 0);
84}
85
89void timer13_config(void) {
90 timer_parameter_struct timer_initpara;
91
92 /* enable TIMER13 clock*/
93 rcu_periph_clock_enable(RCU_TIMER13);
94 timer_deinit(TIMER13);
95
96 /* TIMER1CLK = SystemCoreClock / 72 = 1MHz */
97 timer_initpara.prescaler = 71;
98
99 /* timer edge alignment up count mode */
100 timer_initpara.alignedmode = TIMER_COUNTER_EDGE;
101 timer_initpara.counterdirection = TIMER_COUNTER_UP;
102
103 /* Period = 1000000 / TIM13_FREQUENCY (kHz) */
104 timer_initpara.period = (1000000 / TIM13_FREQUENCY) - 1;
105 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
106 timer_init(TIMER13, &timer_initpara);
107
108 /* enable TIMER13 update interrupt */
109 timer_interrupt_enable(TIMER13, TIMER_INT_UP);
110 timer_enable(TIMER13);
111 nvic_irq_enable(TIMER13_IRQn, TIM13_PRIORITY, 0);
112}
113
117void pwm_config(void) {
118 timer_oc_parameter_struct timer_ocintpara;
119 timer_parameter_struct timer_initpara;
120
121 /* enable GPIO clock and TIMER1 clock*/
122 rcu_periph_clock_enable(RCU_GPIOA);
123 rcu_periph_clock_enable(RCU_TIMER1);
124 timer_deinit(TIMER1);
125
126 /* configure TIMER1_CH0 as alternate function push-pull */
127 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_0);
128 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_0);
129
130 /* configure TIMER1_CH1 as alternate function push-pull */
131 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_1);
132 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_1);
133
134 /* configure TIMER1_CH2 as alternate function push-pull */
135 gpio_mode_set(GPIOA, GPIO_MODE_AF, GPIO_PUPD_NONE, GPIO_PIN_2);
136 gpio_output_options_set(GPIOA, GPIO_OTYPE_PP, GPIO_OSPEED_50MHZ, GPIO_PIN_2);
137
138 /* connect port to TIMER1_CH0 1 2 */
139 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_0);
140 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_1);
141 gpio_af_set(GPIOA, GPIO_AF_2, GPIO_PIN_2);
142
143 /* TIMER1CLK = SystemCoreClock / 2 = 18MHz */
144 timer_initpara.prescaler = 1;
145
146 /* timer center alignment up count mode */
147 timer_initpara.alignedmode = TIMER_COUNTER_CENTER_UP;
148 timer_initpara.counterdirection = TIMER_COUNTER_UP;
149
150 /* Period = 36000 / PWM_FREQUENCY */
151 timer_initpara.period = PWM_PERIOD;
152 timer_initpara.clockdivision = TIMER_CKDIV_DIV1;
153 timer_initpara.repetitioncounter = 0;
154 timer_init(TIMER1, &timer_initpara);
155
156 /* CH1, CH2 and CH3 configuration in PWM0 mode */
157 timer_ocintpara.ocpolarity = TIMER_OC_POLARITY_HIGH;
158 timer_ocintpara.outputstate = TIMER_CCX_ENABLE;
159 timer_channel_output_config(TIMER1, TIMER_CH_0, &timer_ocintpara);
160 timer_channel_output_config(TIMER1, TIMER_CH_1, &timer_ocintpara);
161 timer_channel_output_config(TIMER1, TIMER_CH_2, &timer_ocintpara);
162
163 /* CH0 configuration in PWM mode1,duty cycle 50% */
164 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_0, PWM_PERIOD / 2);
165 timer_channel_output_mode_config(TIMER1, TIMER_CH_0, TIMER_OC_MODE_PWM0);
166 timer_channel_output_shadow_config(TIMER1, TIMER_CH_0, TIMER_OC_SHADOW_DISABLE);
167
168 /* CH1 configuration in PWM mode1,duty cycle 25% */
169 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_1, PWM_PERIOD / 2);
170 timer_channel_output_mode_config(TIMER1, TIMER_CH_1, TIMER_OC_MODE_PWM0);
171 timer_channel_output_shadow_config(TIMER1, TIMER_CH_1, TIMER_OC_SHADOW_DISABLE);
172
173 /* CH2 configuration in PWM mode1,duty cycle 12.5% */
174 timer_channel_output_pulse_value_config(TIMER1, TIMER_CH_2, PWM_PERIOD / 2);
175 timer_channel_output_mode_config(TIMER1, TIMER_CH_2, TIMER_OC_MODE_PWM0);
176 timer_channel_output_shadow_config(TIMER1, TIMER_CH_2, TIMER_OC_SHADOW_DISABLE);
177
178 /* auto-reload preload and timer enable */
179 timer_auto_reload_shadow_enable(TIMER1);
180 timer_enable(TIMER1);
181}
used to place important parameter configurations for users
#define TIM13_PRIORITY
TIMER13 preemption priority set to 3.
Definition: config.h:41
#define TIM2_FREQUENCY
set FOC loop frequency to 8kHz
Definition: config.h:28
#define TIM13_FREQUENCY
set speed/angle PID loop frequency to 100Hz
Definition: config.h:30
#define TIM2_PRIORITY
TIMER2 preemption priority set to 2.
Definition: config.h:39
void timer2_disable(void)
disable timer2 periph and timer2 interrupt
Definition: timer.c:44
void pwm_config(void)
configure timer1 periph and its gpios
Definition: timer.c:117
void timer13_disable(void)
disable timer13 periph and timer2 interrupt
Definition: timer.c:30
void timer13_config(void)
configure timer13 periph for timing interrupt
Definition: timer.c:89
void update_pwm_dutycycle(float ch0, float ch1, float ch2)
update timer1 ch0 1 2 duty-cycle
Definition: timer.c:20
void timer2_config(void)
configure timer2 periph for timing interrupt
Definition: timer.c:61
this is the header file of timer.c.
#define PWM_PERIOD
PWM period calculate from PWM_FREQUENCY.
Definition: timer.h:15