Skip to content

Commit 5709087

Browse files
committed
Update
- This version (2.0.9) removes the existing fix for [#5050](espressif/arduino-esp32#5050) because ESP32 2.0.0-rc2 or newer already includes a [solution](espressif/arduino-esp32@65eafd1). - Constant strings now use program memory instead of ram by using `Serial.print(F())`function.
1 parent 8893e16 commit 5709087

File tree

5 files changed

+41
-60
lines changed

5 files changed

+41
-60
lines changed

Diff for: analogWrite.cpp

+38-55
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************************
2-
AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 2.0.8
2+
AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 2.0.9
33
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
44
This Library is licensed under the MIT License
55
**********************************************************************************/
@@ -29,15 +29,6 @@ pinStatus_t pinsStatus[8] = {
2929
const uint8_t chd = 2;
3030
#endif
3131

32-
float awLedcSetup(uint8_t ch, double frequency, uint8_t bits) {
33-
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
34-
frequency *= 80; //workaround for issue 5050
35-
return ledcSetup(ch, frequency, bits) / 80;
36-
#else //ESP32
37-
return ledcSetup(ch, frequency, bits);
38-
#endif
39-
}
40-
4132
void awDetachPin(uint8_t pin, uint8_t ch) {
4233
pinsStatus[ch / chd].pin = -1;
4334
pinsStatus[ch / chd].value = 0;
@@ -50,14 +41,6 @@ void awDetachPin(uint8_t pin, uint8_t ch) {
5041
REG_SET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL, GPIO_MODE_DEF_DISABLE);
5142
}
5243

53-
float awLedcReadFreq(uint8_t ch) {
54-
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
55-
return ledcReadFreq(ch) / 80; //workaround for issue 5050
56-
#else //ESP32
57-
return ledcReadFreq(ch);
58-
#endif
59-
}
60-
6144
int8_t awGetChannel(int8_t pin) {
6245
if (!((pinMask >> pin) & 1)) return -1; //not pwm pin
6346
for (int8_t i = 0; i < 8; i++) {
@@ -73,7 +56,7 @@ int8_t awGetChannel(int8_t pin) {
7356
if (pinsStatus[ch / chd].pin == -1) { //free channel
7457
if ((ledcRead(ch) < 1) && (ledcReadFreq(ch) < 1)) { //free timer
7558
pinsStatus[ch / chd].pin = pin;
76-
aw::awLedcSetup(ch, pinsStatus[ch / chd].frequency, pinsStatus[ch / chd].resolution);
59+
ledcSetup(ch, pinsStatus[ch / chd].frequency, pinsStatus[ch / chd].resolution);
7760
ledcAttachPin(pin, ch);
7861
return ch;
7962
break;
@@ -109,7 +92,7 @@ float analogWrite(int8_t pin, int32_t value) {
10992
aw::pinsStatus[ch / aw::chd].value = value;
11093
}
11194
}
112-
return aw::awLedcReadFreq(ch);
95+
return ledcReadFreq(ch);
11396
}
11497
return 0;
11598
}
@@ -128,7 +111,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency) {
128111
if (value > ((1 << bits) - 1)) value = (1 << bits); //constrain
129112
if ((bits > 7) && (value == ((1 << bits) - 1))) value = (1 << bits); //keep PWM high
130113
if (aw::pinsStatus[ch / aw::chd].frequency != frequency) {
131-
aw::awLedcSetup(ch, frequency, bits);
114+
ledcSetup(ch, frequency, bits);
132115
ledcWrite(ch, value);
133116
aw::pinsStatus[ch / aw::chd].frequency = frequency;
134117
}
@@ -138,7 +121,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency) {
138121
}
139122
}
140123
}
141-
return aw::awLedcReadFreq(ch);
124+
return ledcReadFreq(ch);
142125
}
143126
return 0;
144127
}
@@ -157,7 +140,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency, uint8_t resolution
157140
if (value > ((1 << bits) - 1)) value = (1 << bits); //constrain
158141
if ((bits > 7) && (value == ((1 << bits) - 1))) value = (1 << bits); //keep PWM high
159142
if ((aw::pinsStatus[ch / aw::chd].frequency != frequency) || (aw::pinsStatus[ch / aw::chd].resolution != bits)) {
160-
aw::awLedcSetup(ch, frequency, bits);
143+
ledcSetup(ch, frequency, bits);
161144
ledcWrite(ch, value);
162145
aw::pinsStatus[ch / aw::chd].frequency = frequency;
163146
aw::pinsStatus[ch / aw::chd].resolution = bits;
@@ -168,7 +151,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency, uint8_t resolution
168151
}
169152
}
170153
}
171-
return aw::awLedcReadFreq(ch);
154+
return ledcReadFreq(ch);
172155
}
173156
return 0;
174157
}
@@ -187,7 +170,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency, uint8_t resolution
187170
if (value > ((1 << bits) - 1)) value = (1 << bits); //constrain
188171
if ((bits > 7) && (value == ((1 << bits) - 1))) value = (1 << bits); //keep PWM high
189172
if ((aw::pinsStatus[ch / aw::chd].frequency != frequency) || (aw::pinsStatus[ch / aw::chd].resolution != bits)) {
190-
aw::awLedcSetup(ch, frequency, bits);
173+
ledcSetup(ch, frequency, bits);
191174
ledcWrite(ch, value);
192175
aw::pinsStatus[ch / aw::chd].frequency = frequency;
193176
aw::pinsStatus[ch / aw::chd].resolution = bits;
@@ -213,7 +196,7 @@ float analogWrite(int8_t pin, int32_t value, float frequency, uint8_t resolution
213196
}
214197
}
215198
}
216-
return aw::awLedcReadFreq(ch);
199+
return ledcReadFreq(ch);
217200
}
218201
return 0;
219202
}
@@ -223,12 +206,12 @@ float analogWriteFrequency(int8_t pin, float frequency) {
223206
if (ch >= 0) {
224207
if ((aw::pinsStatus[ch / aw::chd].pin) > 47) return -1;
225208
if (aw::pinsStatus[ch / aw::chd].frequency != frequency) {
226-
aw::awLedcSetup(ch, frequency, aw::pinsStatus[ch / aw::chd].resolution);
209+
ledcSetup(ch, frequency, aw::pinsStatus[ch / aw::chd].resolution);
227210
ledcWrite(ch, aw::pinsStatus[ch / aw::chd].value);
228211
aw::pinsStatus[ch / aw::chd].frequency = frequency;
229212
}
230213
}
231-
return aw::awLedcReadFreq(ch);
214+
return ledcReadFreq(ch);
232215
}
233216

234217
int32_t analogWriteResolution(int8_t pin, uint8_t resolution) {
@@ -237,7 +220,7 @@ int32_t analogWriteResolution(int8_t pin, uint8_t resolution) {
237220
if ((aw::pinsStatus[ch / aw::chd].pin) > 47) return -1;
238221
if (aw::pinsStatus[ch / aw::chd].resolution != resolution) {
239222
ledcDetachPin(pin);
240-
aw::awLedcSetup(ch, aw::pinsStatus[ch / aw::chd].frequency, resolution & 0xF);
223+
ledcSetup(ch, aw::pinsStatus[ch / aw::chd].frequency, resolution & 0xF);
241224
ledcAttachPin(pin, ch);
242225
ledcWrite(ch, aw::pinsStatus[ch / aw::chd].value);
243226
aw::pinsStatus[ch / aw::chd].resolution = resolution & 0xF;
@@ -256,41 +239,41 @@ void setPinsStatusDefaults(int32_t value, float frequency, uint8_t resolution, u
256239
}
257240

258241
void printPinsStatus() {
259-
Serial.print("PWM pins: ");
242+
Serial.print(F("PWM pins: "));
260243
for (int i = 0; i < aw::muxSize; i++) {
261244
if ((aw::pinMask >> i) & 1) {
262-
Serial.print(i); Serial.print(", ");
245+
Serial.print(i); Serial.print(F(", "));
263246
}
264247
}
265248
Serial.println();
266249

267250
Serial.println();
268251
for (int i = 0; i < 8; i++) {
269252
int ch = aw::pinsStatus[i].channel;
270-
Serial.print("ch: ");
271-
if (ch < 10) Serial.print(" "); Serial.print(ch); Serial.print(" ");
272-
Serial.print("Pin: ");
273-
if ((aw::pinsStatus[ch / aw::chd].pin >= 0) && (aw::pinsStatus[ch / aw::chd].pin < 10)) Serial.print(" ");
274-
Serial.print(aw::pinsStatus[ch / aw::chd].pin); Serial.print(" ");
275-
Serial.print("Hz: ");
276-
if (aw::awLedcReadFreq(ch) < 10000) Serial.print(" ");
277-
if (aw::awLedcReadFreq(ch) < 1000) Serial.print(" ");
278-
if (aw::awLedcReadFreq(ch) < 100) Serial.print(" ");
279-
if (aw::awLedcReadFreq(ch) < 10) Serial.print(" ");
280-
Serial.print(aw::awLedcReadFreq(ch)); Serial.print(" ");
281-
Serial.print("Bits: ");
282-
if (aw::pinsStatus[ch / aw::chd].resolution < 10) Serial.print(" ");
283-
Serial.print(aw::pinsStatus[ch / aw::chd].resolution); Serial.print(" ");
284-
Serial.print("Duty: ");
285-
if (aw::pinsStatus[ch / aw::chd].value < 10000) Serial.print(" ");
286-
if (aw::pinsStatus[ch / aw::chd].value < 1000) Serial.print(" ");
287-
if (aw::pinsStatus[ch / aw::chd].value < 100) Serial.print(" ");
288-
if (aw::pinsStatus[ch / aw::chd].value < 10) Serial.print(" ");
289-
Serial.print(aw::pinsStatus[ch / aw::chd].value); Serial.print(" ");
290-
Serial.print("Ø: ");
291-
if (aw::pinsStatus[ch / aw::chd].phase < 1000) Serial.print(" ");
292-
if (aw::pinsStatus[ch / aw::chd].phase < 100) Serial.print(" ");
293-
if (aw::pinsStatus[ch / aw::chd].phase < 10) Serial.print(" ");
253+
Serial.print(F("ch: "));
254+
if (ch < 10) Serial.print(F(" ")); Serial.print(ch); Serial.print(F(" "));
255+
Serial.print(F("Pin: "));
256+
if ((aw::pinsStatus[ch / aw::chd].pin >= 0) && (aw::pinsStatus[ch / aw::chd].pin < 10)) Serial.print(F(" "));
257+
Serial.print(aw::pinsStatus[ch / aw::chd].pin); Serial.print(F(" "));
258+
Serial.print(F("Hz: "));
259+
if (ledcReadFreq(ch) < 10000) Serial.print(F(" "));
260+
if (ledcReadFreq(ch) < 1000) Serial.print(F(" "));
261+
if (ledcReadFreq(ch) < 100) Serial.print(F(" "));
262+
if (ledcReadFreq(ch) < 10) Serial.print(F(" "));
263+
Serial.print(ledcReadFreq(ch)); Serial.print(F(" "));
264+
Serial.print(F("Bits: "));
265+
if (aw::pinsStatus[ch / aw::chd].resolution < 10) Serial.print(F(" "));
266+
Serial.print(aw::pinsStatus[ch / aw::chd].resolution); Serial.print(F(" "));
267+
Serial.print(F("Duty: "));
268+
if (aw::pinsStatus[ch / aw::chd].value < 10000) Serial.print(F(" "));
269+
if (aw::pinsStatus[ch / aw::chd].value < 1000) Serial.print(F(" "));
270+
if (aw::pinsStatus[ch / aw::chd].value < 100) Serial.print(F(" "));
271+
if (aw::pinsStatus[ch / aw::chd].value < 10) Serial.print(F(" "));
272+
Serial.print(aw::pinsStatus[ch / aw::chd].value); Serial.print(F(" "));
273+
Serial.print(F("Ø: "));
274+
if (aw::pinsStatus[ch / aw::chd].phase < 1000) Serial.print(F(" "));
275+
if (aw::pinsStatus[ch / aw::chd].phase < 100) Serial.print(F(" "));
276+
if (aw::pinsStatus[ch / aw::chd].phase < 10) Serial.print(F(" "));
294277
Serial.print(aw::pinsStatus[ch / aw::chd].phase);
295278
Serial.println();
296279
}

Diff for: analogWrite.h

-2
Original file line numberDiff line numberDiff line change
@@ -35,9 +35,7 @@ typedef struct pinStatus {
3535
uint32_t phase;
3636
} pinStatus_t;
3737

38-
float awLedcSetup(uint8_t ch, double frequency, uint8_t bits);
3938
void awDetachPin(uint8_t pin, uint8_t ch);
40-
float awLedcReadFreq(uint8_t ch);
4139
int8_t awGetChannel(int8_t pin);
4240

4341
} //namespace aw

Diff for: examples/AnalogWriteTest/AnalogWriteTest.ino

+1-1
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
void setup() {
66

77
Serial.begin(115200);
8-
Serial.println("----------------");
8+
Serial.println(F("----------------"));
99

1010
// 3-phase PWM
1111
analogWrite(4, 341, 100, 10, 0);

Diff for: library.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
"keywords": "esp32, analogWrite, esp32-s2, esp32-c3, LEDC",
44
"description": "AnalogWrite for ESP32 and ESP32-S2 with LEDC PWM. Includes PWM Phase Control, DAC and Smart GPIO resource management.",
55
"license": "MIT",
6-
"version": "2.0.8",
6+
"version": "2.0.9",
77
"frameworks": "arduino",
88
"platforms": "espressif32",
99
"repository": {

Diff for: library.properties

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=ESP32 ESP32S2 AnalogWrite
2-
version=2.0.8
2+
version=2.0.9
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=ESP32 ESP32-S2 analogWrite functions

0 commit comments

Comments
 (0)