Skip to content

Commit 1d185d0

Browse files
committed
Merge pull request #1 from Makuna/Servo
Servo
2 parents 19554e5 + 3c3bc0f commit 1d185d0

File tree

9 files changed

+727
-7
lines changed

9 files changed

+727
-7
lines changed

Diff for: cores/esp8266/Arduino.h

+17-1
Original file line numberDiff line numberDiff line change
@@ -101,13 +101,29 @@ void yield(void);
101101
#define timer1_enabled() ((T1C & (1 << TCTE)) != 0)
102102
#define timer1_interrupted() ((T1C & (1 << TCIS)) != 0)
103103

104+
typedef void(*timercallback)(void);
105+
104106
void timer1_isr_init(void);
105107
void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload);
106108
void timer1_disable(void);
107-
void timer1_attachInterrupt(void (*userFunc)(void));
109+
void timer1_attachInterrupt(timercallback userFunc);
108110
void timer1_detachInterrupt(void);
109111
void timer1_write(uint32_t ticks); //maximum ticks 8388607
110112

113+
// timer0 is a special CPU timer that has very high resolution but with
114+
// limited control.
115+
// it uses CCOUNT (ESP.GetCycleCount()) as the non-resetable timer counter
116+
// it does not support divide, type, or reload flags
117+
// it is auto-disabled when the compare value matches CCOUNT
118+
// it is auto-enabled when the compare value changes
119+
#define timer0_interrupted() (ETS_INTR_PENDING() & (_BV(ETS_COMPARE0_INUM)))
120+
#define timer0_read() ((__extension__({uint32_t count;__asm__ __volatile__("esync; rsr %0,ccompare0":"=a" (count));count;})))
121+
#define timer0_write(count) __asm__ __volatile__("wsr %0,ccompare0; esync"::"a" (count) : "memory")
122+
123+
void timer0_isr_init(void);
124+
void timer0_attachInterrupt(timercallback userFunc);
125+
void timer0_detachInterrupt(void);
126+
111127
// undefine stdlib's abs if encountered
112128
#ifdef abs
113129
#undef abs

Diff for: cores/esp8266/core_esp8266_timer.c

+45-6
Original file line numberDiff line numberDiff line change
@@ -20,22 +20,33 @@
2020
*/
2121
#include "wiring_private.h"
2222
#include "pins_arduino.h"
23+
#ifdef __cplusplus
24+
extern "C" {
25+
#endif
26+
2327
#include "c_types.h"
2428
#include "ets_sys.h"
2529

26-
void (*timer1_user_cb)(void);
30+
#ifdef __cplusplus
31+
}
32+
#endif
33+
34+
/ ------------------------------------------------------------------ -
35+
// timer 1
36+
37+
static volatile timercallback timer1_user_cb = NULL;
2738

2839
void timer1_isr_handler(void *para){
29-
if((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable
40+
if ((T1C & ((1 << TCAR) | (1 << TCIT))) == 0) TEIE &= ~TEIE1;//edge int disable
3041
T1I = 0;
31-
if(timer1_user_cb) timer1_user_cb();
42+
if (timer1_user_cb) timer1_user_cb();
3243
}
3344

3445
void timer1_isr_init(){
3546
ETS_FRC_TIMER1_INTR_ATTACH(timer1_isr_handler, NULL);
3647
}
3748

38-
void timer1_attachInterrupt(void (*userFunc)(void)) {
49+
void timer1_attachInterrupt(timercallback userFunc) {
3950
timer1_user_cb = userFunc;
4051
ETS_FRC1_INTR_ENABLE();
4152
}
@@ -52,11 +63,39 @@ void timer1_enable(uint8_t divider, uint8_t int_type, uint8_t reload){
5263
}
5364

5465
void timer1_write(uint32_t ticks){
55-
T1L = ((ticks) & 0x7FFFFF);
56-
if((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable
66+
T1L = ((ticks)& 0x7FFFFF);
67+
if ((T1C & (1 << TCIT)) == 0) TEIE |= TEIE1;//edge int enable
5768
}
5869

5970
void timer1_disable(){
6071
T1C = 0;
6172
T1I = 0;
6273
}
74+
75+
//-------------------------------------------------------------------
76+
// timer 0
77+
78+
static volatile timercallback timer0_user_cb = NULL;
79+
80+
void timer0_isr_handler(void* para){
81+
if (timer0_user_cb) {
82+
timer0_user_cb();
83+
}
84+
}
85+
86+
void timer0_isr_init(){
87+
ETS_CCOMPARE0_INTR_ATTACH(timer0_isr_handler, NULL);
88+
}
89+
90+
void timer0_attachInterrupt(timercallback userFunc) {
91+
timer0_user_cb = userFunc;
92+
ETS_CCOMPARE0_ENABLE();
93+
}
94+
95+
void timer0_detachInterrupt() {
96+
timer0_user_cb = NULL;
97+
ETS_CCOMPARE0_DISABLE();
98+
}
99+
100+
101+

Diff for: libraries/Servo/examples/Sweep/Sweep.ino

+39
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/* Sweep
2+
by BARRAGAN <http://barraganstudio.com>
3+
This example code is in the public domain.
4+
5+
modified 28 May 2015
6+
by Michael C. Miller
7+
modified 8 Nov 2013
8+
by Scott Fitzgerald
9+
10+
http://arduino.cc/en/Tutorial/Sweep
11+
*/
12+
13+
#include <Servo.h>
14+
15+
Servo myservo; // create servo object to control a servo
16+
// twelve servo objects can be created on most boards
17+
18+
19+
void setup()
20+
{
21+
myservo.attach(2); // attaches the servo on GIO2 to the servo object
22+
}
23+
24+
void loop()
25+
{
26+
int pos;
27+
28+
for(pos = 0; pos <= 180; pos += 1) // goes from 0 degrees to 180 degrees
29+
{ // in steps of 1 degree
30+
myservo.write(pos); // tell servo to go to position in variable 'pos'
31+
delay(15); // waits 15ms for the servo to reach the position
32+
}
33+
for(pos = 180; pos>=0; pos-=1) // goes from 180 degrees to 0 degrees
34+
{
35+
myservo.write(pos); // tell servo to go to position in variable 'pos'
36+
delay(15); // waits 15ms for the servo to reach the position
37+
}
38+
}
39+

Diff for: libraries/Servo/keywords.txt

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#######################################
2+
# Syntax Coloring Map Servo
3+
#######################################
4+
5+
#######################################
6+
# Datatypes (KEYWORD1)
7+
#######################################
8+
9+
Servo KEYWORD1 Servo
10+
11+
#######################################
12+
# Methods and Functions (KEYWORD2)
13+
#######################################
14+
attach KEYWORD2
15+
detach KEYWORD2
16+
write KEYWORD2
17+
read KEYWORD2
18+
attached KEYWORD2
19+
writeMicroseconds KEYWORD2
20+
readMicroseconds KEYWORD2
21+
22+
#######################################
23+
# Constants (LITERAL1)
24+
#######################################

Diff for: libraries/Servo/library.properties

+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
name=Servo
2+
version=1.0.2
3+
author=Michael C. Miller
4+
maintainer=GitHub/esp8266/arduino
5+
sentence=Allows Esp8266 boards to control a variety of servo motors.
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.<br />
7+
category=Device Control
8+
url=http://arduino.cc/en/Reference/Servo
9+
architectures=esp8266

Diff for: libraries/Servo/src/Servo.h

+93
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,93 @@
1+
/*
2+
Servo.h - Interrupt driven Servo library for Esp8266 using timers
3+
Copyright (c) 2015 Michael C. Miller. All right reserved.
4+
5+
This library is free software; you can redistribute it and/or
6+
modify it under the terms of the GNU Lesser General Public
7+
License as published by the Free Software Foundation; either
8+
version 2.1 of the License, or (at your option) any later version.
9+
10+
This library is distributed in the hope that it will be useful,
11+
but WITHOUT ANY WARRANTY; without even the implied warranty of
12+
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13+
Lesser General Public License for more details.
14+
15+
You should have received a copy of the GNU Lesser General Public
16+
License along with this library; if not, write to the Free Software
17+
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18+
*/
19+
20+
21+
// A servo is activated by creating an instance of the Servo class passing
22+
// the desired pin to the attach() method.
23+
// The servos are pulsed in the background using the value most recently
24+
// written using the write() method.
25+
//
26+
// This library uses time0 and timer1.
27+
// Note that timer0 may be repurposed when the first servo is attached.
28+
//
29+
// Timers are seized as needed in groups of 12 servos - 24 servos use two
30+
// timers, there are only two timers for the esp8266 so the support stops here
31+
// The sequence used to sieze timers is defined in timers.h
32+
//
33+
// The methods are:
34+
//
35+
// Servo - Class for manipulating servo motors connected to Arduino pins.
36+
//
37+
// attach(pin ) - Attaches a servo motor to an i/o pin.
38+
// attach(pin, min, max ) - Attaches to a pin setting min and max values in microseconds
39+
// default min is 544, max is 2400
40+
//
41+
// write() - Sets the servo angle in degrees. (invalid angle that is valid as pulse in microseconds is treated as microseconds)
42+
// writeMicroseconds() - Sets the servo pulse width in microseconds
43+
// read() - Gets the last written servo pulse width as an angle between 0 and 180.
44+
// readMicroseconds() - Gets the last written servo pulse width in microseconds. (was read_us() in first release)
45+
// attached() - Returns true if there is a servo attached.
46+
// detach() - Stops an attached servos from pulsing its i/o pin.
47+
48+
49+
#ifndef Servo_h
50+
#define Servo_h
51+
52+
#include <Arduino.h>
53+
54+
// the following are in us (microseconds)
55+
//
56+
#define MIN_PULSE_WIDTH 544 // the shortest pulse sent to a servo
57+
#define MAX_PULSE_WIDTH 2400 // the longest pulse sent to a servo
58+
#define DEFAULT_PULSE_WIDTH 1500 // default pulse width when servo is attached
59+
#define REFRESH_INTERVAL 20000 // minumim time to refresh servos in microseconds
60+
61+
// NOTE: to maintain a strict refresh interval the user needs to not exceede 8 servos
62+
#define SERVOS_PER_TIMER 12 // the maximum number of servos controlled by one timer
63+
#define MAX_SERVOS (ServoTimerSequence_COUNT * SERVOS_PER_TIMER)
64+
65+
#if defined(ESP8266)
66+
67+
#include "esp8266/ServoTimers.h"
68+
69+
#else
70+
71+
#error "This library only supports esp8266 boards."
72+
73+
#endif
74+
75+
class Servo
76+
{
77+
public:
78+
Servo();
79+
uint8_t attach(int pin); // attach the given pin to the next free channel, sets pinMode, returns channel number or 0 if failure
80+
uint8_t attach(int pin, int min, int max); // as above but also sets min and max values for writes.
81+
void detach();
82+
void write(int value); // if value is < 200 its treated as an angle, otherwise as pulse width in microseconds
83+
void writeMicroseconds(int value); // Write pulse width in microseconds
84+
int read(); // returns current pulse width as an angle between 0 and 180 degrees
85+
int readMicroseconds(); // returns current pulse width in microseconds for this servo (was read_us() in first release)
86+
bool attached(); // return true if this servo is attached, otherwise false
87+
private:
88+
uint8_t _servoIndex; // index into the channel data for this servo
89+
uint16_t _minUs;
90+
uint16_t _maxUs;
91+
};
92+
93+
#endif

0 commit comments

Comments
 (0)