Browse Source

🧑‍💻 Misc. servo code cleanup

Scott Lahteine 3 years ago
parent
commit
0ca33429b6

+ 1
- 1
Marlin/src/HAL/AVR/Servo.cpp View File

66
 
66
 
67
 /************ static functions common to all instances ***********************/
67
 /************ static functions common to all instances ***********************/
68
 
68
 
69
-static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t* TCNTn, volatile uint16_t* OCRnA) {
69
+static inline void handle_interrupts(const timer16_Sequence_t timer, volatile uint16_t* TCNTn, volatile uint16_t* OCRnA) {
70
   if (Channel[timer] < 0)
70
   if (Channel[timer] < 0)
71
     *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
71
     *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
72
   else {
72
   else {

+ 8
- 13
Marlin/src/HAL/DUE/Servo.cpp View File

52
 // ------------------------
52
 // ------------------------
53
 /// Interrupt handler for the TC0 channel 1.
53
 /// Interrupt handler for the TC0 channel 1.
54
 // ------------------------
54
 // ------------------------
55
-void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel);
55
+void Servo_Handler(const timer16_Sequence_t, Tc*, const uint8_t);
56
 
56
 
57
 #ifdef _useTimer1
57
 #ifdef _useTimer1
58
   void HANDLER_FOR_TIMER1() { Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); }
58
   void HANDLER_FOR_TIMER1() { Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); }
70
   void HANDLER_FOR_TIMER5() { Servo_Handler(_timer5, TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); }
70
   void HANDLER_FOR_TIMER5() { Servo_Handler(_timer5, TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); }
71
 #endif
71
 #endif
72
 
72
 
73
-void Servo_Handler(timer16_Sequence_t timer, Tc *tc, uint8_t channel) {
73
+void Servo_Handler(const timer16_Sequence_t timer, Tc *tc, const uint8_t channel) {
74
   // clear interrupt
74
   // clear interrupt
75
   tc->TC_CHANNEL[channel].TC_SR;
75
   tc->TC_CHANNEL[channel].TC_SR;
76
   if (Channel[timer] < 0)
76
   if (Channel[timer] < 0)
113
   TC_Start(tc, channel);
113
   TC_Start(tc, channel);
114
 }
114
 }
115
 
115
 
116
-void initISR(timer16_Sequence_t timer) {
116
+void initISR(const timer16_Sequence_t timer) {
117
   #ifdef _useTimer1
117
   #ifdef _useTimer1
118
-    if (timer == _timer1)
119
-      _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1);
118
+    if (timer == _timer1) _initISR(TC_FOR_TIMER1, CHANNEL_FOR_TIMER1, ID_TC_FOR_TIMER1, IRQn_FOR_TIMER1);
120
   #endif
119
   #endif
121
   #ifdef _useTimer2
120
   #ifdef _useTimer2
122
-    if (timer == _timer2)
123
-      _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2);
121
+    if (timer == _timer2) _initISR(TC_FOR_TIMER2, CHANNEL_FOR_TIMER2, ID_TC_FOR_TIMER2, IRQn_FOR_TIMER2);
124
   #endif
122
   #endif
125
   #ifdef _useTimer3
123
   #ifdef _useTimer3
126
-    if (timer == _timer3)
127
-      _initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3);
124
+    if (timer == _timer3) _initISR(TC_FOR_TIMER3, CHANNEL_FOR_TIMER3, ID_TC_FOR_TIMER3, IRQn_FOR_TIMER3);
128
   #endif
125
   #endif
129
   #ifdef _useTimer4
126
   #ifdef _useTimer4
130
-    if (timer == _timer4)
131
-      _initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4);
127
+    if (timer == _timer4) _initISR(TC_FOR_TIMER4, CHANNEL_FOR_TIMER4, ID_TC_FOR_TIMER4, IRQn_FOR_TIMER4);
132
   #endif
128
   #endif
133
   #ifdef _useTimer5
129
   #ifdef _useTimer5
134
-    if (timer == _timer5)
135
-      _initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5);
130
+    if (timer == _timer5) _initISR(TC_FOR_TIMER5, CHANNEL_FOR_TIMER5, ID_TC_FOR_TIMER5, IRQn_FOR_TIMER5);
136
   #endif
131
   #endif
137
 }
132
 }
138
 
133
 

+ 2
- 2
Marlin/src/HAL/SAMD51/Servo.cpp View File

124
   }
124
   }
125
 }
125
 }
126
 
126
 
127
-void initISR(timer16_Sequence_t timer) {
127
+void initISR(const timer16_Sequence_t timer) {
128
   Tc * const tc = timer_config[SERVO_TC].pTc;
128
   Tc * const tc = timer_config[SERVO_TC].pTc;
129
   const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
129
   const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
130
 
130
 
201
   }
201
   }
202
 }
202
 }
203
 
203
 
204
-void finISR(timer16_Sequence_t timer) {
204
+void finISR(const timer16_Sequence_t timer) {
205
   Tc * const tc = timer_config[SERVO_TC].pTc;
205
   Tc * const tc = timer_config[SERVO_TC].pTc;
206
   const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
206
   const uint8_t tcChannel = TIMER_TCCHANNEL(timer);
207
 
207
 

+ 8
- 8
Marlin/src/HAL/STM32F1/Servo.cpp View File

147
     uint16_t SR = timer_get_status(tdev);
147
     uint16_t SR = timer_get_status(tdev);
148
     if (SR & TIMER_SR_CC1IF) { // channel 1 off
148
     if (SR & TIMER_SR_CC1IF) { // channel 1 off
149
       #ifdef SERVO0_PWM_OD
149
       #ifdef SERVO0_PWM_OD
150
-        OUT_WRITE_OD(SERVO0_PIN, 1); // off
150
+        OUT_WRITE_OD(SERVO0_PIN, HIGH); // off
151
       #else
151
       #else
152
-        OUT_WRITE(SERVO0_PIN, 0);
152
+        OUT_WRITE(SERVO0_PIN, LOW);
153
       #endif
153
       #endif
154
       timer_reset_status_bit(tdev, TIMER_SR_CC1IF_BIT);
154
       timer_reset_status_bit(tdev, TIMER_SR_CC1IF_BIT);
155
     }
155
     }
156
     if (SR & TIMER_SR_CC2IF) { // channel 2 resume
156
     if (SR & TIMER_SR_CC2IF) { // channel 2 resume
157
       #ifdef SERVO0_PWM_OD
157
       #ifdef SERVO0_PWM_OD
158
-        OUT_WRITE_OD(SERVO0_PIN, 0); // on
158
+        OUT_WRITE_OD(SERVO0_PIN, LOW); // on
159
       #else
159
       #else
160
-        OUT_WRITE(SERVO0_PIN, 1);
160
+        OUT_WRITE(SERVO0_PIN, HIGH);
161
       #endif
161
       #endif
162
       timer_reset_status_bit(tdev, TIMER_SR_CC2IF_BIT);
162
       timer_reset_status_bit(tdev, TIMER_SR_CC2IF_BIT);
163
     }
163
     }
167
     timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0);
167
     timer_dev *tdev = HAL_get_timer_dev(MF_TIMER_SERVO0);
168
     if (!tdev) return false;
168
     if (!tdev) return false;
169
     #ifdef SERVO0_PWM_OD
169
     #ifdef SERVO0_PWM_OD
170
-      OUT_WRITE_OD(inPin, 1);
170
+      OUT_WRITE_OD(inPin, HIGH);
171
     #else
171
     #else
172
-      OUT_WRITE(inPin, 0);
172
+      OUT_WRITE(inPin, LOW);
173
     #endif
173
     #endif
174
 
174
 
175
     timer_pause(tdev);
175
     timer_pause(tdev);
200
       timer_disable_irq(tdev, 1);
200
       timer_disable_irq(tdev, 1);
201
       timer_disable_irq(tdev, 2);
201
       timer_disable_irq(tdev, 2);
202
       #ifdef SERVO0_PWM_OD
202
       #ifdef SERVO0_PWM_OD
203
-        OUT_WRITE_OD(pin, 1); // off
203
+        OUT_WRITE_OD(pin, HIGH); // off
204
       #else
204
       #else
205
-        OUT_WRITE(pin, 0);
205
+        OUT_WRITE(pin, LOW);
206
       #endif
206
       #endif
207
     }
207
     }
208
   }
208
   }

+ 7
- 6
Marlin/src/HAL/shared/servo.cpp View File

65
 
65
 
66
 /************ static functions common to all instances ***********************/
66
 /************ static functions common to all instances ***********************/
67
 
67
 
68
-static boolean isTimerActive(timer16_Sequence_t timer) {
68
+static bool anyTimerChannelActive(const timer16_Sequence_t timer) {
69
   // returns true if any servo is active on this timer
69
   // returns true if any servo is active on this timer
70
   LOOP_L_N(channel, SERVOS_PER_TIMER) {
70
   LOOP_L_N(channel, SERVOS_PER_TIMER) {
71
     if (SERVO(timer, channel).Pin.isActive)
71
     if (SERVO(timer, channel).Pin.isActive)
101
   max = (MAX_PULSE_WIDTH - inMax) / 4;
101
   max = (MAX_PULSE_WIDTH - inMax) / 4;
102
 
102
 
103
   // initialize the timer if it has not already been initialized
103
   // initialize the timer if it has not already been initialized
104
-  timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
105
-  if (!isTimerActive(timer)) initISR(timer);
106
-  servo_info[servoIndex].Pin.isActive = true;  // this must be set after the check for isTimerActive
104
+  const timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
105
+  if (!anyTimerChannelActive(timer)) initISR(timer);
106
+  servo_info[servoIndex].Pin.isActive = true;  // this must be set after the check for anyTimerChannelActive
107
 
107
 
108
   return servoIndex;
108
   return servoIndex;
109
 }
109
 }
110
 
110
 
111
 void Servo::detach() {
111
 void Servo::detach() {
112
   servo_info[servoIndex].Pin.isActive = false;
112
   servo_info[servoIndex].Pin.isActive = false;
113
-  timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
114
-  if (!isTimerActive(timer)) finISR(timer);
113
+  const timer16_Sequence_t timer = SERVO_INDEX_TO_TIMER(servoIndex);
114
+  if (!anyTimerChannelActive(timer)) finISR(timer);
115
+  //pinMode(servo_info[servoIndex].Pin.nbr, INPUT); // set servo pin to input
115
 }
116
 }
116
 
117
 
117
 void Servo::write(int value) {
118
 void Servo::write(int value) {

+ 6
- 6
Marlin/src/HAL/shared/servo_private.h View File

70
 #define ticksToUs(_ticks) (unsigned(_ticks) * (SERVO_TIMER_PRESCALER) / clockCyclesPerMicrosecond())
70
 #define ticksToUs(_ticks) (unsigned(_ticks) * (SERVO_TIMER_PRESCALER) / clockCyclesPerMicrosecond())
71
 
71
 
72
 // convenience macros
72
 // convenience macros
73
-#define SERVO_INDEX_TO_TIMER(_servo_nbr) ((timer16_Sequence_t)(_servo_nbr / (SERVOS_PER_TIMER))) // returns the timer controlling this servo
74
-#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % (SERVOS_PER_TIMER))       // returns the index of the servo on this timer
75
-#define SERVO_INDEX(_timer,_channel)  ((_timer*(SERVOS_PER_TIMER)) + _channel)     // macro to access servo index by timer and channel
76
-#define SERVO(_timer,_channel)  (servo_info[SERVO_INDEX(_timer,_channel)])       // macro to access servo class by timer and channel
73
+#define SERVO_INDEX_TO_TIMER(_servo_nbr) timer16_Sequence_t(_servo_nbr / (SERVOS_PER_TIMER)) // the timer controlling this servo
74
+#define SERVO_INDEX_TO_CHANNEL(_servo_nbr) (_servo_nbr % (SERVOS_PER_TIMER))      // the index of the servo on this timer
75
+#define SERVO_INDEX(_timer,_channel)  ((_timer*(SERVOS_PER_TIMER)) + _channel)    // servo index by timer and channel
76
+#define SERVO(_timer,_channel)  servo_info[SERVO_INDEX(_timer,_channel)]          // servo class by timer and channel
77
 
77
 
78
 // Types
78
 // Types
79
 
79
 
94
 
94
 
95
 // Public functions
95
 // Public functions
96
 
96
 
97
-extern void initISR(timer16_Sequence_t timer);
98
-extern void finISR(timer16_Sequence_t timer);
97
+extern void initISR(const timer16_Sequence_t timer);
98
+extern void finISR(const timer16_Sequence_t timer);

Loading…
Cancel
Save