Skip to content

Commit 6566031

Browse files
committed
keep check for good SERVO_INDEX
1 parent e7a6c77 commit 6566031

File tree

2 files changed

+28
-29
lines changed

2 files changed

+28
-29
lines changed

Marlin/src/HAL/AVR/Servo.cpp

Lines changed: 14 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -67,26 +67,26 @@ static volatile int8_t Channel[_Nbr_16timers]; // counter for the s
6767
/************ static functions common to all instances ***********************/
6868

6969
static inline void handle_interrupts(timer16_Sequence_t timer, volatile uint16_t* TCNTn, volatile uint16_t* OCRnA) {
70+
const bool good_servo = SERVO_INDEX(timer, Channel[timer]) < ServoCount;
7071
if (Channel[timer] < 0)
71-
*TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
72-
else {
73-
if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && SERVO(timer, Channel[timer]).Pin.isActive)
74-
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated
75-
}
72+
*TCNTn = 0; // channel set to -1 indicated that refresh interval completed so reset the timer
73+
else if (good_servo && SERVO(timer, Channel[timer]).Pin.isActive)
74+
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated
7675

77-
Channel[timer]++; // increment to the next channel
78-
if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) {
76+
Channel[timer]++; // increment to the next channel
77+
if (good_servo && Channel[timer] < SERVOS_PER_TIMER) {
7978
*OCRnA = *TCNTn + SERVO(timer, Channel[timer]).ticks;
80-
if (SERVO(timer, Channel[timer]).Pin.isActive) // check if activated
81-
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, HIGH); // it's an active channel so pulse it high
79+
if (SERVO(timer, Channel[timer]).Pin.isActive) // check if activated
80+
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, HIGH); // it's an active channel so pulse it high
8281
}
8382
else {
84-
// finished all channels so wait for the refresh period to expire before starting over
85-
if (((unsigned)*TCNTn) + 4 < usToTicks(REFRESH_INTERVAL)) // allow a few ticks to ensure the next OCR1A not missed
86-
*OCRnA = (unsigned int)usToTicks(REFRESH_INTERVAL);
83+
// Finished all channels so wait for the refresh period to expire before starting over
84+
const unsigned int iticks = (unsigned int)usToTicks(REFRESH_INTERVAL);
85+
if (((unsigned)*TCNTn) + 4 < iticks) // allow a few ticks to ensure the next OCR1A not missed
86+
*OCRnA = iticks;
8787
else
88-
*OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed
89-
Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
88+
*OCRnA = *TCNTn + 4; // at least REFRESH_INTERVAL has elapsed
89+
Channel[timer] = -1; // gets incremented at the end of the refresh period to start again at the first channel
9090
}
9191
}
9292

Marlin/src/HAL/DUE/Servo.cpp

Lines changed: 14 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -71,29 +71,28 @@ void Servo_Handler(timer16_Sequence_t timer, Tc *pTc, uint8_t channel);
7171
#endif
7272

7373
void Servo_Handler(timer16_Sequence_t timer, Tc *tc, uint8_t channel) {
74+
const bool good_servo = SERVO_INDEX(timer, Channel[timer]) < ServoCount;
7475
if (Channel[timer] < 0)
75-
tc->TC_CHANNEL[channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer
76-
else {
77-
//else if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && SERVO(timer, Channel[timer]).Pin.isActive)
78-
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated
79-
}
76+
tc->TC_CHANNEL[channel].TC_CCR |= TC_CCR_SWTRG; // channel set to -1 indicated that refresh interval completed so reset the timer
77+
else if (good_servo)
78+
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, LOW); // pulse this channel low if activated
8079

81-
// clear interrupt
82-
tc->TC_CHANNEL[channel].TC_SR;
80+
tc->TC_CHANNEL[channel].TC_SR; // clear interrupt
8381

84-
Channel[timer]++; // increment to the next channel
85-
if (SERVO_INDEX(timer, Channel[timer]) < ServoCount && Channel[timer] < SERVOS_PER_TIMER) {
82+
Channel[timer]++; // increment to the next channel
83+
if (good_servo && Channel[timer] < SERVOS_PER_TIMER) {
8684
tc->TC_CHANNEL[channel].TC_RA = tc->TC_CHANNEL[channel].TC_CV + SERVO(timer,Channel[timer]).ticks;
87-
if (SERVO(timer,Channel[timer]).Pin.isActive) // check if activated
88-
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, HIGH); // its an active channel so pulse it high
85+
if (SERVO(timer,Channel[timer]).Pin.isActive) // check if activated
86+
extDigitalWrite(SERVO(timer, Channel[timer]).Pin.nbr, HIGH); // it's an active channel so pulse it high
8987
}
9088
else {
9189
// finished all channels so wait for the refresh period to expire before starting over
90+
const unsigned int iticks = (unsigned int)usToTicks(REFRESH_INTERVAL);
9291
tc->TC_CHANNEL[channel].TC_RA =
93-
tc->TC_CHANNEL[channel].TC_CV < usToTicks(REFRESH_INTERVAL) - 4
94-
? (unsigned int)usToTicks(REFRESH_INTERVAL) // allow a few ticks to ensure the next OCR1A not missed
95-
: tc->TC_CHANNEL[channel].TC_CV + 4; // at least REFRESH_INTERVAL has elapsed
96-
Channel[timer] = -1; // this will get incremented at the end of the refresh period to start again at the first channel
92+
tc->TC_CHANNEL[channel].TC_CV + 4 < iticks
93+
? iticks // allow a few ticks to ensure the next OCR1A not missed
94+
: tc->TC_CHANNEL[channel].TC_CV + 4; // at least REFRESH_INTERVAL has elapsed
95+
Channel[timer] = -1; // gets incremented at the end of the refresh period to start again at the first channel
9796
}
9897

9998
}

0 commit comments

Comments
 (0)