Skip to content

Commit 0b1eae7

Browse files
committed
fix failure to update servo
1 parent 8ea0c71 commit 0b1eae7

6 files changed

Lines changed: 38 additions & 46 deletions

File tree

megaavr/libraries/Servo/library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ name=Servo
22
version=1.2.3
33
author=Spence Konde based on work by Michael Margolis, Arduino
44
maintainer=Spence Konde <spencekonde@gmail.com>
5-
sentence=Allows post-2016 AVR parts to control a variety of servo motors. If IDE uses version from libraries, include this version, Servo_DxCore.h instead of Servo.h, keeping all other code identical.
5+
sentence=Allows post-2016 AVR parts to control a variety of servo motors. If IDE uses version from libraries, include Servo_megaTinyCore.h instead, keeping all other code identical.
66
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer, and does not depend on TCA0 prescaler.<br />1.2.3 - adapt to changed spelling of constants in v 2.x.x of atpacks. Fix version of library included by examples.<br />1.2.2 Fix compile error, make area around it more graceful by pushing stack of ifdef's out into ServoTimers which is meant to sequester that crap away from code that does stuff.<br />1.2.1 - Add support for tinyAVR 2-series and AVR Dx-series parts, improve comments and formatting, add compile error for DxCore. 1.2.0 actually works, unlike 1.1.7, and a great deal of changes were made under the hood.<br />
77
category=Device Control
88
url=https://www.arduino.cc/reference/en/libraries/servo/

megaavr/libraries/Servo_megaTinyCore/examples/Knob/Knob.ino

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -11,11 +11,11 @@
1111

1212
Servo myservo; // create servo object to control a servo
1313

14-
int potpin = 0; // analog pin used to connect the potentiometer
14+
int potpin = PIN_PA7; // analog pin used to connect the potentiometer - analog channel 7 exists everywhere!
1515
int val; // variable to read the value from the analog pin
1616

1717
void setup() {
18-
myservo.attach(9); // attaches the servo on pin 9 to the servo object
18+
myservo.attach(PIN_PA1); // attaches the servo on PA1, this pin exists on all tinyAVRs.
1919
}
2020

2121
void loop() {
Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
name=Servo_megaTinyCore
2-
version=1.2.1
2+
version=1.2.3
33
author=Spence Konde based on work by Michael Margolis, Arduino
44
maintainer=Spence Konde <spencekonde@gmail.com>
55
sentence=Allows post-2016 AVR parts to control a variety of servo motors. If IDE uses version from libraries, include Servo_megaTinyCore.h instead, keeping all other code identical.
6-
paragraph=Alternate name version - if you have Servo.h installed through library manager, you may need to use this version. Only the name of the include changes.<br/>This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer, and does not depend on TCA0 prescaler.<br /> 1.2.0 actually works, unlike 1.1.7, and a great deal of changes were made under the hood.<br /> 1.2.1 - Add support for tinyAVR 2-series and AVR Dx-series parts, improve comments and formatting.<br />
6+
paragraph=This library can control a great number of servos.<br />It makes careful use of timers: the library can control 12 servos using only 1 timer, and does not depend on TCA0 prescaler.<br />1.2.3 - adapt to changed spelling of constants in v 2.x.x of atpacks. Fix version of library included by examples.<br />1.2.2 Fix compile error, make area around it more graceful by pushing stack of ifdef's out into ServoTimers which is meant to sequester that crap away from code that does stuff.<br />1.2.1 - Add support for tinyAVR 2-series and AVR Dx-series parts, improve comments and formatting, add compile error for DxCore. 1.2.0 actually works, unlike 1.1.7, and a great deal of changes were made under the hood.<br />
77
category=Device Control
88
url=https://www.arduino.cc/reference/en/libraries/servo/
99
architectures=megaavr

megaavr/libraries/Servo_megaTinyCore/src/Servo_megaTinyCore.h

Lines changed: 18 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -9,8 +9,8 @@
99
This library is included with megaTinyCore and DxCore.
1010
1111
This version was designed for and will be included with:
12-
megaTinyCore 2.3.0+
13-
DxCore 1.3.2+
12+
megaTinyCore 2.6.0+
13+
DxCore 1.5.0+
1414
*/
1515

1616
/* OBLIGATORY LEGAL BOILERPLATE
@@ -56,22 +56,20 @@
5656
5757
5858
This library supports 12 servos controlled by one timer.
59-
It does not recruit additional timers. Who the hell runs over a dozen
60-
servos from one board? Complain in the github issues for the core if
61-
this is actually a problem - I'm inclined to think it's not.
62-
63-
This version is the alternate-name one for megaTinyCore
64-
These are used to load this library even if the original one has been
65-
installed - would take preference over a core-supplied library named
66-
Servo - even though the core-supplied one worked, and the one in library
67-
folder would just #error about unsupported part.
68-
*/
59+
It does not recruit additional timers.
60+
61+
Who the hell runs over a dozen servos from one board? Complain in the github
62+
issues for the core this was packaged with if this is actually a problem for you...
63+
I don't expect to hear from anyone. -Spence, Jan 2021
64+
*/
6965

7066
#ifndef Servo_h
7167
#define Servo_h
72-
7368
#include <inttypes.h>
74-
#if (!defined(TCB_CLKSEL2_bm))
69+
#include <core_devices.h> // They renamed about a thousand _bm and _bp defines in latest atpacks, this file (included automatically via Arduino.h for sketches)
70+
// ensures compatibility with both toolchain versions without the header having to include the whole Arduino.h). About 4000 lines of fixes, generatede with
71+
// find-all, a manual read-through, and then regex replacement to add the workaround). That is, I did what they should have done.
72+
#if (!defined(TCB_CLKSEL_2_bm))
7573
// This means it's a tinyAVR 0/1-series, or a megaAVR 0-series.
7674
// Their TCB_CLKSEL enums use different names for the clock settings, for reasons unclear.
7775
// To align with the future, we use the Dx-series names for these.
@@ -80,7 +78,7 @@
8078
#endif
8179
#if defined(ARDUINO_ARCH_MEGAAVR)
8280
#include "megaavr/ServoTimers.h"
83-
#if (F_CPU==1000000)
81+
#if (F_CPU == 1000000)
8482
#pragma message("Running at 1MHz results in unstable servo signal.")
8583
#endif
8684
#else
@@ -92,17 +90,15 @@
9290
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
9391
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
9492
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
95-
#define REFRESH_INTERVAL 20000UL // minumim time to refresh servos in microseconds
96-
// UL is super-important, otherwise it will work at low system clock
97-
// but overflow at higher ones!
93+
#define REFRESH_INTERVAL 20000UL // minumim time to refresh servos in microseconds - UL is super-important!
9894
#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
9995
#define INVALID_SERVO 255 // flag indicating an invalid servo index
100-
101-
#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
10296
// *INDENT-ON*
97+
#define MAX_SERVOS (_Nbr_16timers * SERVOS_PER_TIMER)
98+
10399

104-
typedef struct {
105-
uint8_t isActive ; // true if this channel is enabled, pin not pulsed if false
100+
typedef struct { // port & bitmask used instead of pin number to realize dramatic performance boost
101+
uint8_t isActive; // true if this channel is enabled, pin not pulsed if false
106102
uint8_t port; // port number (A=0, B=1, and so on)
107103
uint8_t bitmask; // port & bitmask used instead of pin number to realize dramatic performance boost
108104
} ServoPin_t ;

megaavr/libraries/Servo_megaTinyCore/src/megaavr/ServoTimers.h

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -17,8 +17,8 @@
1717
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
1818
1919
This version was designed for and will be included with:
20-
megaTinyCore 2.3.0+
21-
DxCore 1.3.2+
20+
megaTinyCore 2.6.0+
21+
DxCore 1.5.0+
2222
*/
2323

2424
#ifndef __SERVO_TIMERS_H__
@@ -44,6 +44,7 @@
4444
#else
4545
#define USE_TIMERB3
4646
#endif
47+
// 14 and 20-pin AVR DD-series parts do not have a third type B timer!
4748
#elif defined(SERVO_USE_TIMERB2)
4849
#if !defined(TCB2)
4950
#error "SERVO_USE_TIMERB2 is defined, but there is no TCB2 on selected part."
@@ -52,7 +53,7 @@
5253
#else
5354
#define USE_TIMERB2
5455
#endif
55-
// ah, finally cases that might be relevant to tinyAVR
56+
5657
#elif (defined(TCB1) && defined(SERVO_USE_TIMERB1))
5758
#if defined(MILLIS_USE_TIMERB1)
5859
#error "SERVO_USE_TIMERB1 is defined, but so is MILLIS_USE_TIMERB1 - TCB1 can only be used for one of these."
@@ -65,7 +66,7 @@
6566
#else
6667
#define USE_TIMERB0
6768
#endif
68-
// No defines try to force a specific timer, use TCB1 if it exists unless it's being used for millis.
69+
// No defines try to force using a specific timer, use TCB1 if it exists unless it's being used for millis.
6970
#elif (defined(TCB1) && !defined(MILLIS_USE_TIMERB1))
7071
#define USE_TIMERB1
7172
#elif !defined(MILLIS_USE_TIMERB0)
@@ -84,14 +85,19 @@
8485
static volatile __attribute__((used)) TCB_t *_timer =
8586
#if defined(USE_TIMERB0)
8687
&TCB0;
88+
#define SERVO_INT_vect TCB0_INT_vect
8789
#elif defined(USE_TIMERB1)
8890
&TCB1;
91+
#define SERVO_INT_vect TCB1_INT_vect
8992
#elif defined(USE_TIMERB2)
9093
&TCB2;
94+
#define SERVO_INT_vect TCB2_INT_vect
9195
#elif defined(USE_TIMERB3)
9296
&TCB3;
97+
#define SERVO_INT_vect TCB3_INT_vect
9398
#elif defined(USE_TIMERB4)
9499
&TCB4;
100+
#define SERVO_INT_vect TCB4_INT_vect
95101
#endif
96102

97103
typedef enum {

megaavr/libraries/Servo_megaTinyCore/src/megaavr/Servo_megaTinyCore.cpp

Lines changed: 5 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -4,12 +4,12 @@
44
#include <Servo_megaTinyCore.h>
55

66
#if (F_CPU > 10000000)
7-
#define usToTicks(_us) ((( (_us) / 2) * clockCyclesPerMicrosecond())) // converts microseconds to tick
7+
#define usToTicks(_us) ((((_us) / 2) * clockCyclesPerMicrosecond())) // converts microseconds to tick
88
#define ticksToUs(_ticks) (((unsigned) (_ticks) * 2) / clockCyclesPerMicrosecond()) // converts from ticks back to microseconds
99
#define TRIM_DURATION 51 // compensation ticks to trim adjust delays in the ISR
1010
#else
11-
#define usToTicks(_us) ((( _us) * clockCyclesPerMicrosecond())) // converts microseconds to tick
12-
#define ticksToUs(_ticks) (((unsigned) _ticks) / clockCyclesPerMicrosecond()) // converts from ticks back to microseconds
11+
#define usToTicks(_us) ((( _us) * clockCyclesPerMicrosecond())) // converts microseconds to tick
12+
#define ticksToUs(_ticks) (((unsigned) _ticks) / clockCyclesPerMicrosecond()) // converts from ticks back to microseconds
1313
#define TRIM_DURATION 102 // compensation ticks to trim adjust delays in the ISR
1414
#endif
1515

@@ -101,18 +101,8 @@ void ServoHandler(int timer) {
101101
_timer->INTFLAGS = TCB_CAPT_bm;
102102
}
103103

104-
#if defined USE_TIMERB0
105-
ISR(TCB0_INT_vect)
106-
#elif defined USE_TIMERB1
107-
ISR(TCB1_INT_vect)
108-
#elif defined USE_TIMERB2
109-
ISR(TCB2_INT_vect)
110-
#elif defined USE_TIMERB3
111-
ISR(TCB3_INT_vect)
112-
#elif defined USE_TIMERB4
113-
ISR(TCB4_INT_vect)
114-
#endif
115-
{
104+
/* Defined in ServoTimers.h */
105+
ISR(SERVO_INT_vect) {
116106
ServoHandler(0);
117107
}
118108

0 commit comments

Comments
 (0)