mirror of
https://github.com/esp8266/Arduino.git
synced 2025-07-17 12:02:15 +03:00
Support multiple tone(), analogWrite(), and Servo (#4640)
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 #4321. Fixes 4349.
This commit is contained in:
committed by
GitHub
parent
ea4720b03e
commit
ebda795f34
@ -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
|
||||
@ -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);
|
||||
|
||||
if (startWaveform(_pin, high, low, (uint32_t) duration * 1000)) {
|
||||
_toneMap |= 1 << _pin;
|
||||
}
|
||||
}
|
||||
|
||||
// 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 (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 disableTimer(uint8_t _index) {
|
||||
tone_pins[_index] = 255;
|
||||
|
||||
switch (tone_timers[_index]) {
|
||||
case 0:
|
||||
// Not currently supported
|
||||
break;
|
||||
|
||||
case 1:
|
||||
timer1_disable();
|
||||
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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
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);
|
||||
if (_pin > 16) {
|
||||
return;
|
||||
}
|
||||
stopWaveform(_pin);
|
||||
_toneMap &= ~(1 << _pin);
|
||||
digitalWrite(_pin, 0);
|
||||
}
|
||||
|
Reference in New Issue
Block a user