|
@@ -46,7 +46,7 @@
|
46
|
46
|
typedef uint16_t hal_timer_t;
|
47
|
47
|
#define HAL_TIMER_TYPE_MAX 0xFFFF
|
48
|
48
|
|
49
|
|
-#ifdef MCU_STM32F103CB || defined(MCU_STM32F103C8)
|
|
49
|
+#if defined MCU_STM32F103CB || defined(MCU_STM32F103C8)
|
50
|
50
|
#define STEP_TIMER_NUM 4 // For C8/CB boards, use timer 4
|
51
|
51
|
#else
|
52
|
52
|
#define STEP_TIMER_NUM 5 // for other boards, five is fine.
|
|
@@ -56,8 +56,18 @@ typedef uint16_t hal_timer_t;
|
56
|
56
|
#define TEMP_TIMER_NUM 2 // index of timer to use for temperature
|
57
|
57
|
#define TEMP_TIMER_CHAN 1 // Channel of the timer to use for compare and interrupts
|
58
|
58
|
|
|
59
|
+#define CAT(a, ...) a ## __VA_ARGS__
|
|
60
|
+#define TIMER_DEV(num) CAT (&timer, num)
|
|
61
|
+
|
|
62
|
+#define STEP_TIMER_DEV TIMER_DEV(STEP_TIMER_NUM)
|
|
63
|
+#define TEMP_TIMER_DEV TIMER_DEV(TEMP_TIMER_NUM)
|
|
64
|
+
|
|
65
|
+
|
|
66
|
+
|
|
67
|
+//STM32_HAVE_TIMER(n);
|
|
68
|
+
|
59
|
69
|
#define HAL_TIMER_RATE (F_CPU) // frequency of timers peripherals
|
60
|
|
-#define STEPPER_TIMER_PRESCALE 36 // prescaler for setting stepper timer, 2Mhz
|
|
70
|
+#define STEPPER_TIMER_PRESCALE 18 // prescaler for setting stepper timer, 4Mhz
|
61
|
71
|
#define HAL_STEPPER_TIMER_RATE (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE) // frequency of stepper timer (HAL_TIMER_RATE / STEPPER_TIMER_PRESCALE)
|
62
|
72
|
#define HAL_TICKS_PER_US ((HAL_STEPPER_TIMER_RATE) / 1000000) // stepper timer ticks per us
|
63
|
73
|
|
|
@@ -65,13 +75,17 @@ typedef uint16_t hal_timer_t;
|
65
|
75
|
#define PULSE_TIMER_PRESCALE STEPPER_TIMER_PRESCALE
|
66
|
76
|
|
67
|
77
|
#define TEMP_TIMER_PRESCALE 1000 // prescaler for setting Temp timer, 72Khz
|
68
|
|
-#define TEMP_TIMER_FREQUENCY 1000 // temperature interrupt frequency
|
|
78
|
+#define TEMP_TIMER_FREQUENCY 100 // temperature interrupt frequency
|
69
|
79
|
|
70
|
|
-#define ENABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_enable_interrupt(STEP_TIMER_NUM)
|
71
|
|
-#define DISABLE_STEPPER_DRIVER_INTERRUPT() HAL_timer_disable_interrupt(STEP_TIMER_NUM)
|
|
80
|
+#define ENABLE_STEPPER_DRIVER_INTERRUPT() timer_enable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
|
|
81
|
+#define DISABLE_STEPPER_DRIVER_INTERRUPT() timer_disable_irq(STEP_TIMER_DEV, STEP_TIMER_CHAN)
|
|
82
|
+
|
|
83
|
+#define ENABLE_TEMPERATURE_INTERRUPT() timer_enable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
|
|
84
|
+#define DISABLE_TEMPERATURE_INTERRUPT() timer_disable_irq(TEMP_TIMER_DEV, TEMP_TIMER_CHAN)
|
|
85
|
+
|
|
86
|
+#define HAL_timer_get_current_count(timer_num) timer_get_count(TIMER_DEV(timer_num))
|
|
87
|
+#define HAL_timer_set_current_count(timer_num, count) timer_set_count(TIMER_DEV(timer_num, (uint16)count))
|
72
|
88
|
|
73
|
|
-#define ENABLE_TEMPERATURE_INTERRUPT() HAL_timer_enable_interrupt(TEMP_TIMER_NUM)
|
74
|
|
-#define DISABLE_TEMPERATURE_INTERRUPT() HAL_timer_disable_interrupt(TEMP_TIMER_NUM)
|
75
|
89
|
|
76
|
90
|
#define HAL_ENABLE_ISRs() do { if (thermalManager.in_temp_isr)DISABLE_TEMPERATURE_INTERRUPT(); else ENABLE_TEMPERATURE_INTERRUPT(); ENABLE_STEPPER_DRIVER_INTERRUPT(); } while(0)
|
77
|
91
|
// TODO change this
|
|
@@ -115,81 +129,42 @@ void HAL_timer_disable_interrupt(uint8_t timer_num);
|
115
|
129
|
*/
|
116
|
130
|
|
117
|
131
|
FORCE_INLINE static void HAL_timer_set_count(const uint8_t timer_num, const hal_timer_t count) {
|
|
132
|
+ //count = min(count, HAL_TIMER_TYPE_MAX);
|
118
|
133
|
switch (timer_num) {
|
119
|
134
|
case STEP_TIMER_NUM:
|
120
|
|
- StepperTimer.pause();
|
121
|
|
- StepperTimer.setCompare(STEP_TIMER_CHAN, count);
|
122
|
|
- StepperTimer.refresh();
|
123
|
|
- StepperTimer.resume();
|
124
|
|
- break;
|
|
135
|
+ timer_set_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN, count);
|
|
136
|
+ return;
|
125
|
137
|
case TEMP_TIMER_NUM:
|
126
|
|
- TempTimer.pause();
|
127
|
|
- TempTimer.setCompare(TEMP_TIMER_CHAN, count);
|
128
|
|
- TempTimer.refresh();
|
129
|
|
- TempTimer.resume();
|
130
|
|
- break;
|
|
138
|
+ timer_set_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN, count);
|
|
139
|
+ return;
|
131
|
140
|
default:
|
132
|
|
- break;
|
|
141
|
+ return;
|
133
|
142
|
}
|
134
|
143
|
}
|
135
|
144
|
|
136
|
145
|
FORCE_INLINE static hal_timer_t HAL_timer_get_count(const uint8_t timer_num) {
|
137
|
|
- hal_timer_t temp;
|
138
|
146
|
switch (timer_num) {
|
139
|
147
|
case STEP_TIMER_NUM:
|
140
|
|
- temp = StepperTimer.getCompare(STEP_TIMER_CHAN);
|
141
|
|
- break;
|
|
148
|
+ return timer_get_compare(STEP_TIMER_DEV, STEP_TIMER_CHAN);
|
142
|
149
|
case TEMP_TIMER_NUM:
|
143
|
|
- temp = TempTimer.getCompare(TEMP_TIMER_CHAN);
|
144
|
|
- break;
|
|
150
|
+ return timer_get_compare(TEMP_TIMER_DEV, TEMP_TIMER_CHAN);
|
145
|
151
|
default:
|
146
|
|
- temp = 0;
|
147
|
|
- break;
|
|
152
|
+ return 0;
|
148
|
153
|
}
|
149
|
|
- return temp;
|
150
|
154
|
}
|
151
|
155
|
|
152
|
|
-FORCE_INLINE static void HAL_timer_set_current_count(const uint8_t timer_num, const hal_timer_t count) {
|
153
|
|
- switch (timer_num) {
|
154
|
|
- case STEP_TIMER_NUM: StepperTimer.setCount(count); break;
|
155
|
|
- case TEMP_TIMER_NUM: TempTimer.setCount(count); break;
|
156
|
|
- }
|
157
|
|
-}
|
158
|
|
-
|
159
|
|
-FORCE_INLINE static hal_timer_t HAL_timer_get_current_count(const uint8_t timer_num) {
|
160
|
|
- hal_timer_t temp;
|
161
|
|
- switch (timer_num) {
|
162
|
|
- case STEP_TIMER_NUM:
|
163
|
|
- temp = StepperTimer.getCount();
|
164
|
|
- break;
|
165
|
|
- case TEMP_TIMER_NUM:
|
166
|
|
- temp = TempTimer.getCount();
|
167
|
|
- break;
|
168
|
|
- default:
|
169
|
|
- temp = 0;
|
170
|
|
- break;
|
171
|
|
- }
|
172
|
|
- return temp;
|
173
|
|
-}
|
174
|
|
-
|
175
|
|
-//void HAL_timer_isr_prologue (const uint8_t timer_num);
|
176
|
|
-
|
177
|
156
|
FORCE_INLINE static void HAL_timer_isr_prologue(const uint8_t timer_num) {
|
178
|
157
|
switch (timer_num) {
|
179
|
158
|
case STEP_TIMER_NUM:
|
180
|
|
- StepperTimer.pause();
|
181
|
|
- StepperTimer.setCount(0);
|
182
|
|
- StepperTimer.refresh();
|
183
|
|
- StepperTimer.resume();
|
184
|
|
- break;
|
|
159
|
+ timer_set_count(STEP_TIMER_DEV, 0);
|
|
160
|
+ timer_generate_update(STEP_TIMER_DEV);
|
|
161
|
+ return;
|
185
|
162
|
case TEMP_TIMER_NUM:
|
186
|
|
- TempTimer.pause();
|
187
|
|
- TempTimer.setCount(0);
|
188
|
|
- TempTimer.refresh();
|
189
|
|
- TempTimer.resume();
|
190
|
|
- break;
|
|
163
|
+ timer_set_count(TEMP_TIMER_DEV, 0);
|
|
164
|
+ timer_generate_update(TEMP_TIMER_DEV);
|
|
165
|
+ return;
|
191
|
166
|
default:
|
192
|
|
- break;
|
|
167
|
+ return;
|
193
|
168
|
}
|
194
|
169
|
}
|
195
|
170
|
|