This repository has been archived by the owner on Jan 29, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathISR_16_PWMs_Array.ino
291 lines (230 loc) · 9.18 KB
/
ISR_16_PWMs_Array.ino
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
/****************************************************************************************************************************
ISR_16_PWMs_Array.ino
For ESP32, ESP32_S2, ESP32_S3, ESP32_C3 boards with ESP32 core v2.0.0+
Written by Khoi Hoang
Built by Khoi Hoang https://github.com/khoih-prog/ESP32_PWM
Licensed under MIT license
The ESP32, ESP32_S2, ESP32_S3, ESP32_C3 have two timer groups, TIMER_GROUP_0 and TIMER_GROUP_1
1) each group of ESP32, ESP32_S2, ESP32_S3 has two general purpose hardware timers, TIMER_0 and TIMER_1
2) each group of ESP32_C3 has ony one general purpose hardware timer, TIMER_0
All the timers are based on 64-bit counters (except 54-bit counter for ESP32_S3 counter) and 16 bit prescalers.
The timer counters can be configured to count up or down and support automatic reload and software reload.
They can also generate alarms when they reach a specific value, defined by the software.
The value of the counter can be read by the software program.
Now even you use all these new 16 ISR-based timers,with their maximum interval practically unlimited (limited only by
unsigned long miliseconds), you just consume only one ESP32-S2 timer and avoid conflicting with other cores' tasks.
The accuracy is nearly perfect compared to software timers. The most important feature is they're ISR-based timers
Therefore, their executions are not blocked by bad-behaving functions / tasks.
This important feature is absolutely necessary for mission-critical tasks.
*****************************************************************************************************************************/
#if !defined( ESP32 )
#error This code is designed to run on ESP32 platform, not Arduino nor ESP8266! Please check your Tools->Board setting.
#endif
// These define's must be placed at the beginning before #include "ESP32_PWM.h"
// _PWM_LOGLEVEL_ from 0 to 4
// Don't define _PWM_LOGLEVEL_ > 0. Only for special ISR debugging only. Can hang the system.
#define _PWM_LOGLEVEL_ 3
#define USING_MICROS_RESOLUTION true //false
// Default is true, uncomment to false
//#define CHANGING_PWM_END_OF_CYCLE false
// To be included only in main(), .ino with setup() to avoid `Multiple Definitions` Linker Error
#include "ESP32_PWM.h"
#ifndef LED_BUILTIN
#define LED_BUILTIN 2
#endif
#ifndef LED_BLUE
#define LED_BLUE 25
#endif
#ifndef LED_RED
#define LED_RED 27
#endif
#define HW_TIMER_INTERVAL_US 20L
uint32_t startMicros = 0;
// Init ESP32 timer 1
ESP32Timer ITimer(1);
// Init ESP32_ISR_PWM
ESP32_PWM ISR_PWM;
bool IRAM_ATTR TimerHandler(void * timerNo)
{
ISR_PWM.run();
return true;
}
//////////////////////////////////////////////////////
#if ( ARDUINO_ESP32C3_DEV )
#define NUMBER_ISR_PWMS 4
#elif ( ARDUINO_ESP32S3_DEV )
#define NUMBER_ISR_PWMS 16
#else
#define NUMBER_ISR_PWMS 16
#endif
#define PIN_D0 0 // Pin D0 mapped to pin GPIO0/BOOT/ADC11/TOUCH1 of ESP32
#define PIN_D1 1 // Pin D1 mapped to pin GPIO1/TX0 of ESP32
#define PIN_D2 2 // Pin D2 mapped to pin GPIO2/ADC12/TOUCH2 of ESP32
#define PIN_D3 3 // Pin D3 mapped to pin GPIO3/RX0 of ESP32
#define PIN_D4 4 // Pin D4 mapped to pin GPIO4/ADC10/TOUCH0 of ESP32
#define PIN_D5 5 // Pin D5 mapped to pin GPIO5/SPISS/VSPI_SS of ESP32
#define PIN_D6 6 // Pin D6 mapped to pin GPIO6 of ESP32
#define PIN_D7 7 // Pin D7 mapped to pin GPIO7 of ESP32
#define PIN_D8 8 // Pin D8 mapped to pin GPIO8 of ESP32
#define PIN_D9 9 // Pin D9 mapped to pin GPIO9 of ESP32
#define PIN_D10 10 // Pin D10 mapped to pin GPIO10 of ESP32
#define PIN_D11 11 // Pin D11 mapped to pin GPIO11 of ESP32
#define PIN_D12 12 // Pin D12 mapped to pin GPIO12/HSPI_MISO/ADC15/TOUCH5/TDI of ESP32
#define PIN_D13 13 // Pin D13 mapped to pin GPIO13/HSPI_MOSI/ADC14/TOUCH4/TCK of ESP32
#define PIN_D14 14 // Pin D14 mapped to pin GPIO14/HSPI_SCK/ADC16/TOUCH6/TMS of ESP32
#define PIN_D15 15 // Pin D15 mapped to pin GPIO15/HSPI_SS/ADC13/TOUCH3/TDO of ESP32
#define PIN_D16 16 // Pin D16 mapped to pin GPIO16/TX2 of ESP32
#define PIN_D17 17 // Pin D17 mapped to pin GPIO17/RX2 of ESP32
#define PIN_D18 18 // Pin D18 mapped to pin GPIO18/VSPI_SCK of ESP32
#define PIN_D19 19 // Pin D19 mapped to pin GPIO19/VSPI_MISO of ESP32
#define PIN_D21 21 // Pin D21 mapped to pin GPIO21/SDA of ESP32
#define PIN_D22 22 // Pin D22 mapped to pin GPIO22/SCL of ESP32
#define PIN_D23 23 // Pin D23 mapped to pin GPIO23/VSPI_MOSI of ESP32
#define PIN_D25 25 // Pin D25 mapped to pin GPIO25/ADC18/DAC1 of ESP32
#define PIN_D26 26 // Pin D26 mapped to pin GPIO26/ADC19/DAC2 of ESP32
#define PIN_D27 27 // Pin D27 mapped to pin GPIO27/ADC17/TOUCH7 of ESP32
//////////////////////////////////////////////////////
#define USING_PWM_FREQUENCY true
//////////////////////////////////////////////////////
// You can assign pins here. Be carefull to select good pin to use or crash, e.g pin 6-11
// Can't use PIN_D1 for core v2.0.1+
#if ( ARDUINO_ESP32C3_DEV )
uint32_t PWM_Pin[] =
// Bad pins to use: PIN_D12-PIN_D24
{
LED_BUILTIN, PIN_D3, PIN_D4, PIN_D5
};
#elif ( ARDUINO_ESP32S3_DEV )
uint32_t PWM_Pin[] =
// Bad pins to use: PIN_D24
{
PIN_D1, PIN_D2, PIN_D3, PIN_D4, PIN_D5, PIN_D6, PIN_D7, PIN_D8,
PIN_D9, PIN_D10, PIN_D11, PIN_D12, PIN_D13, PIN_D14, PIN_D15, PIN_D16,
};
#else
// Bad pins to use: PIN_D24
uint32_t PWM_Pin[] =
{
LED_BUILTIN, PIN_D25, PIN_D3, PIN_D4, PIN_D5, PIN_D12, PIN_D13, PIN_D14,
PIN_D15, PIN_D16, PIN_D17, PIN_D18, PIN_D19, PIN_D21, PIN_D22, PIN_D23
};
#endif
// You can assign any interval for any timer here, in microseconds
uint32_t PWM_Period[] =
{
1000000, 500000, 333333, 250000, 200000, 166667, 142857, 125000,
111111, 100000, 66667, 50000, 40000, 33333, 25000, 20000
};
// You can assign any interval for any timer here, in Hz
float PWM_Freq[] =
{
1.0f, 2.0f, 3.0f, 4.0f, 5.0f, 6.0f, 7.0f, 8.0f,
9.0f, 10.0f, 15.0f, 20.0f, 25.0f, 30.0f, 40.0f, 50.0f
};
// You can assign any interval for any timer here, in milliseconds
float PWM_DutyCycle[] =
{
5.00, 10.00, 20.00, 30.00, 40.00, 45.00, 50.00, 55.00,
60.00, 65.00, 70.00, 75.00, 80.00, 85.00, 90.00, 95.00
};
typedef void (*irqCallback) ();
// In ESP32, avoid doing something fancy in ISR, for example complex Serial.print with String() argument
// The pure simple Serial.prints here are just for demonstration and testing. Must be eliminate in working environment
// Or you can get this run-time error / crash
void doingSomething0()
{
}
void doingSomething1()
{
}
void doingSomething2()
{
}
void doingSomething3()
{
}
void doingSomething4()
{
}
void doingSomething5()
{
}
void doingSomething6()
{
}
void doingSomething7()
{
}
void doingSomething8()
{
}
void doingSomething9()
{
}
void doingSomething10()
{
}
void doingSomething11()
{
}
void doingSomething12()
{
}
void doingSomething13()
{
}
void doingSomething14()
{
}
void doingSomething15()
{
}
irqCallback irqCallbackStartFunc[] =
{
doingSomething0, doingSomething1, doingSomething2, doingSomething3,
doingSomething4, doingSomething5, doingSomething6, doingSomething7,
doingSomething8, doingSomething9, doingSomething10, doingSomething11,
doingSomething12, doingSomething13, doingSomething14, doingSomething15
};
////////////////////////////////////////////////
void setup()
{
pinMode(PIN_D1, OUTPUT);
Serial.begin(115200);
while (!Serial);
delay(2000);
Serial.print(F("\nStarting ISR_16_PWMs_Array on ")); Serial.println(ARDUINO_BOARD);
Serial.println(ESP32_PWM_VERSION);
Serial.print(F("CPU Frequency = ")); Serial.print(F_CPU / 1000000); Serial.println(F(" MHz"));
// Interval in microsecs
if (ITimer.attachInterruptInterval(HW_TIMER_INTERVAL_US, TimerHandler))
{
startMicros = micros();
Serial.print(F("Starting ITimer OK, micros() = ")); Serial.println(startMicros);
}
else
Serial.println(F("Can't set ITimer. Select another freq. or timer"));
// Just to demonstrate, don't use too many ISR Timers if not absolutely necessary
// You can use up to 16 timer for each ISR_PWM
for (uint16_t i = 0; i < NUMBER_ISR_PWMS; i++)
{
//void setPWM(uint32_t pin, float frequency, float dutycycle
// , timer_callback_p StartCallback = nullptr, timer_callback_p StopCallback = nullptr)
#if USING_PWM_FREQUENCY
// You can use this with PWM_Freq in Hz
ISR_PWM.setPWM(PWM_Pin[i], PWM_Freq[i], PWM_DutyCycle[i], irqCallbackStartFunc[i]);
#else
#if USING_MICROS_RESOLUTION
// Or using period in microsecs resolution
ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i], PWM_DutyCycle[i], irqCallbackStartFunc[i]);
#else
// Or using period in millisecs resolution
ISR_PWM.setPWM_Period(PWM_Pin[i], PWM_Period[i] / 1000, PWM_DutyCycle[i], irqCallbackStartFunc[i]);
#endif
#endif
}
}
void loop()
{
delay(1);
}