Skip to content

Commit 099be92

Browse files
committed
Added ESP32-S2 support
1 parent c05efc2 commit 099be92

File tree

5 files changed

+95
-66
lines changed

5 files changed

+95
-66
lines changed

QuickPID.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/**********************************************************************************
2-
QuickPID Library for Arduino - Version 2.2.3
2+
QuickPID Library for Arduino - Version 2.2.4
33
by dlloydev https://github.com/Dlloydev/QuickPID
44
Based on the Arduino PID Library, licensed under the MIT License
55
**********************************************************************************/

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,10 @@ Use this link for reference. Note that if you're using QuickPID, there's no need
180180
181181
#### [![arduino-library-badge](https://www.ardu-badge.com/badge/QuickPID.svg?)](https://www.ardu-badge.com/QuickPID)
182182
183+
- Added ESP32-S2 support
184+
185+
#### Version 2.2.3
186+
183187
- Updated compatibility with the ESP32 Arduino framework
184188
185189
#### Version 2.2.2

library.properties

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
name=QuickPID
2-
version=2.2.3
2+
version=2.2.4
33
author=David Lloyd
44
maintainer=David Lloyd <[email protected]>
55
sentence=A fast fixed/floating point PID controller with AutoTune and 9 tuning rules to choose from.

utility/analogWrite.cpp

Lines changed: 83 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,29 @@
11
/**********************************************************************************
2-
AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 1.0.0
2+
AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 1.1.0
33
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
44
This Library is licensed under the MIT License
55
**********************************************************************************/
66

77
#include <Arduino.h>
88
#include "analogWrite.h"
99

10-
pinStatus_t pinsStatus[16] = {
11-
{ 1, -1, 5000, 13, 0 }, { 3, -1, 5000, 13, 0 },
12-
{ 5, -1, 5000, 13, 0 }, { 7, -1, 5000, 13, 0 },
13-
{ 9, -1, 5000, 13, 0 }, {11, -1, 5000, 13, 0 },
14-
{13, -1, 5000, 13, 0 }, {15, -1, 5000, 13, 0 }
10+
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
11+
pinStatus_t pinsStatus[8] = {
12+
{0, -1, 5000, 13, 0 }, {2, -1, 5000, 13, 0 },
13+
{4, -1, 5000, 13, 0 }, {6, -1, 5000, 13, 0 },
14+
{1, -1, 5000, 13, 0 }, {3, -1, 5000, 13, 0 },
15+
{5, -1, 5000, 13, 0 }, {7, -1, 5000, 13, 0 }
1516
};
17+
const uint8_t chd = 1;
18+
#else //ESP32
19+
pinStatus_t pinsStatus[8] = {
20+
{ 0, -1, 5000, 13, 0 }, { 2, -1, 5000, 13, 0 },
21+
{ 4, -1, 5000, 13, 0 }, { 6, -1, 5000, 13, 0 },
22+
{ 8, -1, 5000, 13, 0 }, {10, -1, 5000, 13, 0 },
23+
{12, -1, 5000, 13, 0 }, {14, -1, 5000, 13, 0 }
24+
};
25+
const uint8_t chd = 2;
26+
#endif
1627

1728
void analogWrite(int8_t pin, int32_t value) {
1829
if (pin == DAC1 || pin == DAC2) { //dac
@@ -22,23 +33,23 @@ void analogWrite(int8_t pin, int32_t value) {
2233
int8_t ch = getChannel(pin);
2334
if (ch >= 0) {
2435
if (value == -1) { //detach pin
25-
pinsStatus[ch / 2].pin = -1;
26-
pinsStatus[ch / 2].frequency = 5000;
27-
pinsStatus[ch / 2].resolution = 13;
28-
ledcDetachPin(pinsStatus[ch / 2].pin);
36+
pinsStatus[ch / chd].pin = -1;
37+
pinsStatus[ch / chd].frequency = 5000;
38+
pinsStatus[ch / chd].resolution = 13;
39+
ledcDetachPin(pinsStatus[ch / chd].pin);
2940
REG_SET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL, GPIO_MODE_DEF_DISABLE);
30-
} else {
31-
int32_t valueMax = (pow(2, pinsStatus[ch / 2].resolution)) - 1;
32-
if (value > valueMax) {
41+
} else { // attached
42+
int32_t valueMax = (pow(2, pinsStatus[ch / chd].resolution)) - 1;
43+
if (value > valueMax) { // full ON
3344
value = valueMax + 1;
3445
ledcDetachPin(pin);
3546
pinMode(pin, OUTPUT);
3647
digitalWrite(pin, HIGH);
37-
} else {
38-
ledcSetup(ch, pinsStatus[ch / 2].frequency, pinsStatus[ch / 2].resolution);
48+
} else { // write PWM
49+
ledcSetup(ch, pinsStatus[ch / chd].frequency, pinsStatus[ch / chd].resolution);
3950
ledcWrite(ch, value);
4051
}
41-
pinsStatus[ch / 2].value = value;
52+
pinsStatus[ch / chd].value = value;
4253
}
4354
}
4455
}
@@ -47,82 +58,92 @@ void analogWrite(int8_t pin, int32_t value) {
4758
float analogWriteFrequency(int8_t pin, float frequency) {
4859
int8_t ch = getChannel(pin);
4960
if (ch >= 0) {
50-
pinsStatus[ch / 2].frequency = frequency;
51-
pinsStatus[ch / 2].pin = pin;
52-
ledcSetup(ch, frequency, pinsStatus[ch / 2].resolution);
53-
ledcWrite(ch, pinsStatus[ch / 2].value);
61+
if ((pinsStatus[ch / chd].pin) > 47) return -1;
62+
pinsStatus[ch / chd].pin = pin;
63+
pinsStatus[ch / chd].frequency = frequency;
64+
//ledcChangeFrequency(ch, frequency, pinsStatus[ch / chd].resolution);
65+
ledcSetup(ch, frequency, pinsStatus[ch / chd].resolution);
66+
ledcWrite(ch, pinsStatus[ch / chd].value);
5467
}
5568
return ledcReadFreq(ch);
5669
}
5770

5871
int32_t analogWriteResolution(int8_t pin, uint8_t resolution) {
5972
int8_t ch = getChannel(pin);
6073
if (ch >= 0) {
61-
pinsStatus[ch / 2].resolution = resolution;
62-
pinsStatus[ch / 2].pin = pin;
63-
ledcSetup(ch, pinsStatus[ch].frequency, resolution);
64-
ledcWrite(ch, pinsStatus[ch].value);
74+
if ((pinsStatus[ch / chd].pin) > 47) return -1;
75+
pinsStatus[ch / chd].pin = pin;
76+
pinsStatus[ch / chd].resolution = resolution;
77+
ledcSetup(ch, pinsStatus[ch / chd].frequency, resolution);
78+
ledcWrite(ch, pinsStatus[ch / chd].value);
6579
}
6680
return pow(2, resolution);
6781
}
6882

6983
int8_t getChannel(int8_t pin) {
70-
if ((pinMask >> pin) & 1) { //valid pin number?
71-
if (REG_GET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL)) { //gpio pin function?
72-
for (int8_t i = 0; i < 8; i++) { //search channels for the pin
73-
if (pinsStatus[i].pin == pin) { //pin found
74-
return pinsStatus[i].channel;
84+
if (!((pinMask >> pin) & 1)) return -1; //not pwm pin
85+
for (int8_t i = 0; i < 8; i++) {
86+
int8_t ch = pinsStatus[i].channel;
87+
if (pinsStatus[ch / chd].pin == pin) {
88+
return ch;
89+
break;
90+
}
91+
}
92+
for (int8_t i = 0; i < 8; i++) {
93+
int8_t ch = pinsStatus[i].channel;
94+
if ((REG_GET_FIELD(GPIO_PIN_MUX_REG[pin], MCU_SEL)) == 0) { //free pin
95+
if (pinsStatus[ch / chd].pin == -1) { //free channel
96+
if ((ledcRead(ch) < 1) && (ledcReadFreq(ch) < 1)) { //free timer
97+
pinsStatus[ch / chd].pin = pin;
98+
ledcAttachPin(pin, ch);
99+
return ch;
75100
break;
76-
}
77-
}
78-
return -99; //pin is being used externally
79-
} else { //pin is not used
80-
for (int8_t i = 0; i < 8; i++) { //search for free channel
81-
if (pinsStatus[i].pin == -1) { //channel is free
82-
pinsStatus[i].pin = pin;
83-
ledcAttachPin(pin, pinsStatus[i].channel);
84-
return pinsStatus[i].channel;
101+
} else {
102+
pinsStatus[ch / chd].pin = 88; //occupied timer
103+
return -1;
85104
break;
86105
}
87106
}
107+
} else {
108+
return -1; //occupied pin
109+
break;
88110
}
89111
}
90-
return -88; //no available resources
112+
return -1;
91113
}
92114

93115
void printPinsStatus() {
94116
Serial.print("PWM pins: ");
95117
for (int i = 0; i < muxSize; i++) {
96118
if ((pinMask >> i) & 1) {
97119
Serial.print(i); Serial.print(", ");
98-
if (i == 18) Serial.println();
99-
if (i == 18) Serial.print(" ");
100120
}
101121
}
102122
Serial.println();
123+
103124
Serial.println();
104125
for (int i = 0; i < 8; i++) {
105-
Serial.print("ch"); Serial.print(pinsStatus[i].channel); Serial.print(" ");
106-
if (pinsStatus[i].channel < 10) Serial.print(" ");
107-
if (pinsStatus[i].pin == -1) {
108-
Serial.print(pinsStatus[i].pin); Serial.print(" ");
109-
} else {
110-
Serial.print("pin"); Serial.print(pinsStatus[i].pin); Serial.print(" ");
111-
}
112-
if (pinsStatus[i].pin < 10) Serial.print(" ");
113-
if (pinsStatus[i].frequency < 10000) Serial.print(" ");
114-
if (pinsStatus[i].frequency < 1000) Serial.print(" ");
115-
if (pinsStatus[i].frequency < 100) Serial.print(" ");
116-
if (pinsStatus[i].frequency < 10) Serial.print(" ");
117-
Serial.print(pinsStatus[i].frequency); Serial.print(" Hz ");
118-
if (pinsStatus[i].resolution < 10) Serial.print(" ");
119-
Serial.print(pinsStatus[i].resolution); Serial.print("-bit ");
120-
Serial.print("val ");
121-
if (pinsStatus[i].value < 10000) Serial.print(" ");
122-
if (pinsStatus[i].value < 1000) Serial.print(" ");
123-
if (pinsStatus[i].value < 100) Serial.print(" ");
124-
if (pinsStatus[i].value < 10) Serial.print(" ");
125-
Serial.print(pinsStatus[i].value);
126+
int ch = pinsStatus[i].channel;
127+
Serial.print("ch: ");
128+
if (ch < 10) Serial.print(" "); Serial.print(ch); Serial.print(" ");
129+
Serial.print("Pin: ");
130+
if ((pinsStatus[ch / chd].pin >= 0) && (pinsStatus[ch / chd].pin < 10)) Serial.print(" ");
131+
Serial.print(pinsStatus[ch / chd].pin); Serial.print(" ");
132+
Serial.print("Hz: ");
133+
if (ledcReadFreq(ch) < 10000) Serial.print(" ");
134+
if (ledcReadFreq(ch) < 1000) Serial.print(" ");
135+
if (ledcReadFreq(ch) < 100) Serial.print(" ");
136+
if (ledcReadFreq(ch) < 10) Serial.print(" ");
137+
Serial.print(ledcReadFreq(ch)); Serial.print(" ");
138+
Serial.print("Bits: ");
139+
if (pinsStatus[ch / chd].resolution < 10) Serial.print(" ");
140+
Serial.print(pinsStatus[ch / chd].resolution); Serial.print(" ");
141+
Serial.print("Duty: ");
142+
if (pinsStatus[ch / chd].value < 10000) Serial.print(" ");
143+
if (pinsStatus[ch / chd].value < 1000) Serial.print(" ");
144+
if (pinsStatus[ch / chd].value < 100) Serial.print(" ");
145+
if (pinsStatus[ch / chd].value < 10) Serial.print(" ");
146+
Serial.print(pinsStatus[ch / chd].value);
126147
Serial.println();
127148
}
128149
}

utility/analogWrite.h

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,16 @@
11
#ifndef _ESP32_ESP32S2_ANALOG_WRITE_
22
#define _ESP32_ESP32S2_ANALOG_WRITE_
3+
34
#include <Arduino.h>
45

6+
#if (defined(ESP32) || defined(ARDUINO_ARCH_ESP32))
7+
58
#if (CONFIG_IDF_TARGET_ESP32S2 || CONFIG_IDF_TARGET_ESP32S3)
69
#define NUM_OUTPUT_PINS 45
710
#define DAC1 17
811
#define DAC2 18
912
const uint8_t muxSize = 48;
10-
const uint64_t pinMask = 0x7FE00207FFE; //PWM
13+
const uint64_t pinMask = 0x27FE00207FFE; //PWM
1114
#else
1215
#define NUM_OUTPUT_PINS 34
1316
#define DAC1 25
@@ -30,4 +33,5 @@ void analogWrite(int8_t pin, int32_t value = 0);
3033
int8_t getChannel(int8_t pin);
3134
void printPinsStatus(void);
3235

33-
#endif
36+
#endif //ESP32 or ARDUINO_ARCH_ESP32
37+
#endif //_ESP32_ESP32S2_ANALOG_WRITE_

0 commit comments

Comments
 (0)