1
1
/* *********************************************************************************
2
- AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 1.0 .0
2
+ AnalogWrite Library for ESP32-ESP32S2 Arduino core - Version 1.1 .0
3
3
by dlloydev https://github.com/Dlloydev/ESP32-ESP32S2-AnalogWrite
4
4
This Library is licensed under the MIT License
5
5
**********************************************************************************/
6
6
7
7
#include < Arduino.h>
8
8
#include " analogWrite.h"
9
9
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 }
15
16
};
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
16
27
17
28
void analogWrite (int8_t pin, int32_t value) {
18
29
if (pin == DAC1 || pin == DAC2) { // dac
@@ -22,23 +33,23 @@ void analogWrite(int8_t pin, int32_t value) {
22
33
int8_t ch = getChannel (pin);
23
34
if (ch >= 0 ) {
24
35
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 );
29
40
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
33
44
value = valueMax + 1 ;
34
45
ledcDetachPin (pin);
35
46
pinMode (pin, OUTPUT);
36
47
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 );
39
50
ledcWrite (ch, value);
40
51
}
41
- pinsStatus[ch / 2 ].value = value;
52
+ pinsStatus[ch / chd ].value = value;
42
53
}
43
54
}
44
55
}
@@ -47,82 +58,92 @@ void analogWrite(int8_t pin, int32_t value) {
47
58
float analogWriteFrequency (int8_t pin, float frequency) {
48
59
int8_t ch = getChannel (pin);
49
60
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 );
54
67
}
55
68
return ledcReadFreq (ch);
56
69
}
57
70
58
71
int32_t analogWriteResolution (int8_t pin, uint8_t resolution) {
59
72
int8_t ch = getChannel (pin);
60
73
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 );
65
79
}
66
80
return pow (2 , resolution);
67
81
}
68
82
69
83
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;
75
100
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 ;
85
104
break ;
86
105
}
87
106
}
107
+ } else {
108
+ return -1 ; // occupied pin
109
+ break ;
88
110
}
89
111
}
90
- return -88 ; // no available resources
112
+ return -1 ;
91
113
}
92
114
93
115
void printPinsStatus () {
94
116
Serial.print (" PWM pins: " );
95
117
for (int i = 0 ; i < muxSize; i++) {
96
118
if ((pinMask >> i) & 1 ) {
97
119
Serial.print (i); Serial.print (" , " );
98
- if (i == 18 ) Serial.println ();
99
- if (i == 18 ) Serial.print (" " );
100
120
}
101
121
}
102
122
Serial.println ();
123
+
103
124
Serial.println ();
104
125
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 );
126
147
Serial.println ();
127
148
}
128
149
}
0 commit comments