Skip to content

Commit

Permalink
Support multiple tone(), analogWrite(), and Servo (esp8266#4640)
Browse files Browse the repository at this point in the history
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 so are all compatible
and can be used simultaneously.

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.

A generic "startWaveform(pin, high-US, low-US, runtime-US)" and
"stopWaveform(pin)" allow for further types of interfaces.  Minimum
high or low period is ~1 us.

Add a tone(float) method, useful when working with lower frequencies.

Fixes esp8266#4321.  Fixes 4349.
  • Loading branch information
earlephilhower authored Jun 8, 2018
1 parent ea4720b commit ebda795
Show file tree
Hide file tree
Showing 12 changed files with 600 additions and 841 deletions.
1 change: 1 addition & 0 deletions cores/esp8266/Arduino.h
Original file line number Diff line number Diff line change
Expand Up @@ -279,6 +279,7 @@ unsigned long pulseIn(uint8_t pin, uint8_t state, unsigned long timeout = 100000
unsigned long pulseInLong(uint8_t pin, uint8_t state, unsigned long timeout = 1000000L);

void tone(uint8_t _pin, unsigned int frequency, unsigned long duration = 0);
void tone(uint8_t _pin, double frequency, unsigned long duration = 0);
void noTone(uint8_t _pin);

// WMath prototypes
Expand Down
132 changes: 38 additions & 94 deletions cores/esp8266/Tone.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
A Tone Generator Library for the ESP8266
Copyright (c) 2016 Ben Pirt. All rights reserved.
Original Copyright (c) 2016 Ben Pirt. All rights reserved.
This file is part of the esp8266 core for Arduino environment.
This library is free software; you can redistribute it and/or
Expand All @@ -22,115 +22,59 @@
*/

#include "Arduino.h"
#include "pins_arduino.h"
#include "core_esp8266_waveform.h"

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

void t1IntHandler();

static int8_t toneBegin(uint8_t _pin) {
int8_t _index = -1;

// if we're already using the pin, reuse it.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
return i;
}
static void _startTone(uint8_t _pin, uint32_t high, uint32_t low, unsigned long duration) {
if (_pin > 16) {
return;
}

// search for an unused timer.
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == 255) {
tone_pins[i] = _pin;
_index = i;
break;
}
}
pinMode(_pin, OUTPUT);

return _index;
}
high = std::max(high, (uint32_t)100);
low = std::max(low, (uint32_t)100);

// frequency (in hertz) and duration (in milliseconds).
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
int8_t _index;

_index = toneBegin(_pin);

if (_index >= 0) {
// Set the pinMode as OUTPUT
pinMode(_pin, OUTPUT);

// Alternate handling of zero freqency to avoid divide by zero errors
if (frequency == 0)
{
noTone(_pin);
return;
}

// Calculate the toggle count
if (duration > 0) {
toggle_counts[_index] = 2 * frequency * duration / 1000;
} else {
toggle_counts[_index] = -1;
}

// set up the interrupt frequency
switch (tone_timers[_index]) {
case 0:
// Not currently supported
break;

case 1:
timer1_disable();
timer1_isr_init();
timer1_attachInterrupt(t1IntHandler);
timer1_enable(TIM_DIV1, TIM_EDGE, TIM_LOOP);
timer1_write((clockCyclesPerMicrosecond() * 500000) / frequency);
break;
}
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
_toneMap |= 1 << _pin;
}
}

void disableTimer(uint8_t _index) {
tone_pins[_index] = 255;

switch (tone_timers[_index]) {
case 0:
// Not currently supported
break;

case 1:
timer1_disable();
break;
void tone(uint8_t _pin, unsigned int frequency, unsigned long duration) {
if (frequency == 0) {
noTone(_pin);
} else {
uint32_t period = 1000000L / frequency;
uint32_t high = period / 2;
uint32_t low = period - high;
_startTone(_pin, high, low, duration);
}
}

void noTone(uint8_t _pin) {
for (int i = 0; i < AVAILABLE_TONE_PINS; i++) {
if (tone_pins[i] == _pin) {
tone_pins[i] = 255;
disableTimer(i);
break;
}

// Separate tone(float) to hopefully not pull in floating point libs unless
// it's called with a float.
void tone(uint8_t _pin, double frequency, unsigned long duration) {
if (frequency < 1.0) { // FP means no exact comparisons
noTone(_pin);
} else {
double period = 1000000.0 / frequency;
uint32_t high = (uint32_t)((period / 2.0) + 0.5);
uint32_t low = (uint32_t)(period + 0.5) - high;
_startTone(_pin, high, low, duration);
}
digitalWrite(_pin, LOW);
}

ICACHE_RAM_ATTR void t1IntHandler() {
if (toggle_counts[T1INDEX] != 0){
// toggle the pin
digitalWrite(tone_pins[T1INDEX], toggle_counts[T1INDEX] % 2);
toggle_counts[T1INDEX]--;
// handle the case of indefinite duration
if (toggle_counts[T1INDEX] < -2){
toggle_counts[T1INDEX] = -1;
}
}else{
disableTimer(T1INDEX);
digitalWrite(tone_pins[T1INDEX], LOW);

void noTone(uint8_t _pin) {
if (_pin > 16) {
return;
}
stopWaveform(_pin);
_toneMap &= ~(1 << _pin);
digitalWrite(_pin, 0);
}
Empty file modified cores/esp8266/base64.cpp
100755 → 100644
Empty file.
Empty file modified cores/esp8266/base64.h
100755 → 100644
Empty file.
Loading

0 comments on commit ebda795

Please sign in to comment.