Browse Source

🧑‍💻 Misc. servo code cleanup

Scott Lahteine 2 years ago
parent
commit
0ca33429b6

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

@@ -66,7 +66,7 @@ static volatile int8_t Channel[_Nbr_16timers];              // counter for the s
66 66
 
67 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 70
   if (Channel[timer] < 0)
71 71
     *TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
72 72
   else {

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

@@ -52,7 +52,7 @@ static volatile int8_t Channel[_Nbr_16timers];              // counter for the s
52 52
 // ------------------------
53 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 57
 #ifdef _useTimer1
58 58
   void HANDLER_FOR_TIMER1() { Servo_Handler(_timer1, TC_FOR_TIMER1, CHANNEL_FOR_TIMER1); }
@@ -70,7 +70,7 @@ void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel);
70 70
   void HANDLER_FOR_TIMER5() { Servo_Handler(_timer5, TC_FOR_TIMER5, CHANNEL_FOR_TIMER5); }
71 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 74
   // clear interrupt
75 75
   tc->TC_CHANNEL[channel].TC_SR;
76 76
   if (Channel[timer] < 0)
@@ -113,26 +113,21 @@ static void _initISR(Tc *tc, uint32_t channel, uint32_t id, IRQn_Type irqn) {
113 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 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 119
   #endif
121 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 122
   #endif
125 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 125
   #endif
129 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 128
   #endif
133 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 131
   #endif
137 132
 }
138 133
 

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

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

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

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

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

@@ -65,7 +65,7 @@ uint8_t ServoCount = 0;                         // the total number of attached
65 65
 
66 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 69
   // returns true if any servo is active on this timer
70 70
   LOOP_L_N(channel, SERVOS_PER_TIMER) {
71 71
     if (SERVO(timer, channel).Pin.isActive)
@@ -101,17 +101,18 @@ int8_t Servo::attach(const int inPin, const int inMin, const int inMax) {
101 101
   max = (MAX_PULSE_WIDTH - inMax) / 4;
102 102
 
103 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 108
   return servoIndex;
109 109
 }
110 110
 
111 111
 void Servo::detach() {
112 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 118
 void Servo::write(int value) {

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

@@ -70,10 +70,10 @@
70 70
 #define ticksToUs(_ticks) (unsigned(_ticks) * (SERVO_TIMER_PRESCALER) / clockCyclesPerMicrosecond())
71 71
 
72 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 78
 // Types
79 79
 
@@ -94,5 +94,5 @@ extern ServoInfo_t servo_info[MAX_SERVOS];
94 94
 
95 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