|
1 | | -/* |
2 | | - esp8266_waveform - General purpose waveform generation and control, |
3 | | - supporting outputs on all pins in parallel. |
4 | | -
|
5 | | - Copyright (c) 2018 Earle F. Philhower, III. All rights reserved. |
6 | | - Copyright (c) 2020 Dirk O. Kaar. |
7 | | -
|
8 | | - The core idea is to have a programmable waveform generator with a unique |
9 | | - high and low period (defined in microseconds or CPU clock cycles). TIMER1 is |
10 | | - set to 1-shot mode and is always loaded with the time until the next edge |
11 | | - of any live waveforms. |
12 | | -
|
13 | | - Up to one waveform generator per pin supported. |
14 | | -
|
15 | | - Each waveform generator is synchronized to the ESP clock cycle counter, not the |
16 | | - timer. This allows for removing interrupt jitter and delay as the counter |
17 | | - always increments once per 80MHz clock. Changes to a waveform are |
18 | | - contiguous and only take effect on the next waveform transition, |
19 | | - allowing for smooth transitions. |
20 | | -
|
21 | | - This replaces older tone(), analogWrite(), and the Servo classes. |
22 | | -
|
23 | | - Everywhere in the code where "ccy" or "ccys" is used, it means ESP.getCycleCount() |
24 | | - clock cycle count, or an interval measured in CPU clock cycles, but not TIMER1 |
25 | | - cycles (which may be 2 CPU clock cycles @ 160MHz). |
26 | | -
|
27 | | - This library is free software; you can redistribute it and/or |
28 | | - modify it under the terms of the GNU Lesser General Public |
29 | | - License as published by the Free Software Foundation; either |
30 | | - version 2.1 of the License, or (at your option) any later version. |
31 | | -
|
32 | | - This library is distributed in the hope that it will be useful, |
33 | | - but WITHOUT ANY WARRANTY; without even the implied warranty of |
34 | | - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
35 | | - Lesser General Public License for more details. |
36 | | -
|
37 | | - You should have received a copy of the GNU Lesser General Public |
38 | | - License along with this library; if not, write to the Free Software |
39 | | - Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA |
40 | | -*/ |
| 1 | +// Wrapper to include both versions of the waveform generator |
41 | 2 |
|
42 | 3 | #ifdef WAVEFORM_LOCKED_PHASE |
43 | | - |
44 | | -#include <Arduino.h> |
45 | | - |
46 | | -#ifndef __ESP8266_WAVEFORM_H |
47 | | -#define __ESP8266_WAVEFORM_H |
48 | | - |
49 | | -#ifdef __cplusplus |
50 | | -extern "C" { |
| 4 | + #include "core_esp8266_waveform_phase.h" |
| 5 | +#else |
| 6 | + #include "core_esp8266_waveform_pwm.h" |
51 | 7 | #endif |
52 | | - |
53 | | -// Start or change a waveform of the specified high and low times on specific pin. |
54 | | -// If runtimeUS > 0 then automatically stop it after that many usecs, relative to the next |
55 | | -// full period. |
56 | | -// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running, |
57 | | -// the new waveform is started at phaseOffsetUS phase offset, in microseconds, to that. |
58 | | -// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio |
59 | | -// under load, for applications where frequency or duty cycle must not change, leave false. |
60 | | -// Returns true or false on success or failure. |
61 | | -int startWaveform(uint8_t pin, uint32_t timeHighUS, uint32_t timeLowUS, |
62 | | - uint32_t runTimeUS = 0, int8_t alignPhase = -1, uint32_t phaseOffsetUS = 0, bool autoPwm = false); |
63 | | -// Start or change a waveform of the specified high and low CPU clock cycles on specific pin. |
64 | | -// If runtimeCycles > 0 then automatically stop it after that many CPU clock cycles, relative to the next |
65 | | -// full period. |
66 | | -// If waveform is not yet started on pin, and on pin == alignPhase a waveform is running, |
67 | | -// the new waveform is started at phaseOffsetCcys phase offset, in CPU clock cycles, to that. |
68 | | -// Setting autoPwm to true allows the wave generator to maintain PWM duty to idle cycle ratio |
69 | | -// under load, for applications where frequency or duty cycle must not change, leave false. |
70 | | -// Returns true or false on success or failure. |
71 | | -int startWaveformClockCycles(uint8_t pin, uint32_t timeHighCcys, uint32_t timeLowCcys, |
72 | | - uint32_t runTimeCcys = 0, int8_t alignPhase = -1, uint32_t phaseOffsetCcys = 0, bool autoPwm = false); |
73 | | -// Stop a waveform, if any, on the specified pin. |
74 | | -// Returns true or false on success or failure. |
75 | | -int stopWaveform(uint8_t pin); |
76 | | - |
77 | | -// Add a callback function to be called on *EVERY* timer1 trigger. The |
78 | | -// callback returns the number of microseconds until the next desired call. |
79 | | -// However, since it is called every timer1 interrupt, it may be called |
80 | | -// again before this period. It should therefore use the ESP Cycle Counter |
81 | | -// to determine whether or not to perform an operation. |
82 | | -// Pass in NULL to disable the callback and, if no other waveforms being |
83 | | -// generated, stop the timer as well. |
84 | | -// Make sure the CB function has the ICACHE_RAM_ATTR decorator. |
85 | | -void setTimer1Callback(uint32_t (*fn)()); |
86 | | - |
87 | | -#ifdef __cplusplus |
88 | | -} |
89 | | -#endif |
90 | | - |
91 | | -#endif // __ESP8266_WAVEFORM_H |
92 | | - |
93 | | -#endif // WAVEFORM_LOCKED_PHASE |
0 commit comments