Skip to content

Commit d48b3ba

Browse files
Move to shared TIMER1 infrastructure, add Steppers
Remove and rewrite all the parts of the core/libraries using TIMER1 and consolidate into a single, shared waveform generation interrupt structure. Tone, analogWrite(), Servo all now just call into this shared resource to perform their tasks. This setup enables multiple tones, analogWrites, servos, and stepper motors to be controlled with reasonable accuracy. It uses both TIMER1 and the internal ESP cycle counter to handle timing of waveform edges. TIMER1 is used in non-reload mode and only edges cause interrupts. The interrupt is started and stopped as required, minimizing overhead when these features are not being used. Adds in control of DRV8825 or A4988 interfaces stepper motors to the timer infrastructure. The interrupt handles acceleration and velocity of stepper pulses including jerk (dA/dt) calculations, but it is up to the user application to do path planning and generate S-curve accelerations. All stepper DIR pins are connected together, and then the STEP from each stepper interface board is routed to an individual pin. For motions which involve multiple steppers, there is an option to wait before beginning the next stepper motion until all active steppers have completed their moves (i.e. moving to an X, Y where there may be 1-2us delay between the X arrival and Y arrival). A generic "startWaveform(pin, high-US, low-US, runtime-US)" and "stopWaveform(pin)" allow for further types of interfaces. Using a simple USB DSO shows that with only a single channel running, a "tone(1000)" generates a waveform of 999.8Hz (500.1us per high/low). Running additional generators or steppers will potentially increase the jitter. The implementation also is limited in the time of the shortest pulse it can generate (mostly an issue for analogWrite at values <1% or >99% of the scale). Presently measurements of ~2-3us are seen on the shortest of pulses. There is no optimization to allow generating two edges on a single interrupt, which leads to this minimum pulse width. The current implementation was written focusing on readability and flexibility, not squeezing every cycle out of interrupt loops. There are probably many places where the code could be optimized (after it's fully validated!) to reduce CPU load and timing errors.
1 parent 3a110aa commit d48b3ba

11 files changed

+840
-846
lines changed

cores/esp8266/Tone.cpp

+20-99
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
44
A Tone Generator Library for the ESP8266
55
6-
Copyright (c) 2016 Ben Pirt. All rights reserved.
6+
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
77
This file is part of the esp8266 core for Arduino environment.
88
99
This library is free software; you can redistribute it and/or
@@ -22,115 +22,36 @@
2222
*/
2323

2424
#include "Arduino.h"
25-
#include "pins_arduino.h"
25+
#include "core_esp8266_waveform.h"
2626

27-
#define AVAILABLE_TONE_PINS 1
28-
const uint8_t tone_timers[] = { 1 };
29-
static uint8_t tone_pins[AVAILABLE_TONE_PINS] = { 255, };
30-
static long toggle_counts[AVAILABLE_TONE_PINS] = { 0, };
31-
#define T1INDEX 0
27+
// Which pins have a tone running on them?
28+
static uint32_t _toneMap = 0;
3229

33-
void t1IntHandler();
34-
35-
static int8_t toneBegin(uint8_t _pin) {
36-
int8_t _index = -1;
37-
38-
// if we're already using the pin, reuse it.
39-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
40-
if (tone_pins[i] == _pin) {
41-
return i;
42-
}
30+
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
31+
if (_pin > 16) {
32+
return;
4333
}
4434

45-
// search for an unused timer.
46-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
47-
if (tone_pins[i] == 255) {
48-
tone_pins[i] = _pin;
49-
_index = i;
50-
break;
51-
}
35+
if (frequency == 0) {
36+
noTone(_pin);
37+
return;
5238
}
5339

54-
return _index;
55-
}
56-
57-
// frequency (in hertz) and duration (in milliseconds).
58-
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
59-
int8_t _index;
60-
61-
_index = toneBegin(_pin);
62-
63-
if (_index >= 0) {
64-
// Set the pinMode as OUTPUT
65-
pinMode(_pin, OUTPUT);
66-
67-
// Alternate handling of zero freqency to avoid divide by zero errors
68-
if (frequency == 0)
69-
{
70-
noTone(_pin);
71-
return;
72-
}
73-
74-
// Calculate the toggle count
75-
if (duration > 0) {
76-
toggle_counts[_index] = 2 * frequency * duration / 1000;
77-
} else {
78-
toggle_counts[_index] = -1;
79-
}
80-
81-
// set up the interrupt frequency
82-
switch (tone_timers[_index]) {
83-
case 0:
84-
// Not currently supported
85-
break;
86-
87-
case 1:
88-
timer1_disable();
89-
timer1_isr_init();
90-
timer1_attachInterrupt(t1IntHandler);
91-
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
92-
timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency);
93-
break;
94-
}
40+
uint32_t halfCycle = 500000L / frequency;
41+
if (halfCycle < 100) {
42+
halfCycle = 100;
9543
}
96-
}
97-
98-
void disableTimer(uint8_t _index) {
99-
tone_pins[_index] = 255;
10044

101-
switch (tone_timers[_index]) {
102-
case 0:
103-
// Not currently supported
104-
break;
105-
106-
case 1:
107-
timer1_disable();
108-
break;
45+
if (startWaveform(_pin, halfCycle, halfCycle, (uint32_t) duration * 1000)) {
46+
_toneMap |= 1 << _pin;
10947
}
11048
}
11149

11250
void noTone(uint8_t _pin) {
113-
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
114-
if (tone_pins[i] == _pin) {
115-
tone_pins[i] = 255;
116-
disableTimer(i);
117-
break;
118-
}
119-
}
120-
digitalWrite(_pin, LOW);
121-
}
122-
123-
ICACHE_RAM_ATTR void t1IntHandler() {
124-
if (toggle_counts[T1INDEX] != 0){
125-
// toggle the pin
126-
digitalWrite(tone_pins[T1INDEX], toggle_counts[T1INDEX] % 2);
127-
toggle_counts[T1INDEX]--;
128-
// handle the case of indefinite duration
129-
if (toggle_counts[T1INDEX] < -2){
130-
toggle_counts[T1INDEX] = -1;
131-
}
132-
}else{
133-
disableTimer(T1INDEX);
134-
digitalWrite(tone_pins[T1INDEX], LOW);
51+
if (_pin > 16) {
52+
return;
13553
}
54+
stopWaveform(_pin);
55+
_toneMap &= ~(1 << _pin);
56+
digitalWrite(_pin, 0);
13657
}

cores/esp8266/base64.cpp

100755100644
File mode changed.

cores/esp8266/base64.h

100755100644
File mode changed.

0 commit comments

Comments
 (0)