esp32 initial pwm support

This commit is contained in:
gemu2015 2020-05-01 17:38:40 +02:00
parent 7177c7d8e0
commit bf29904b7a
3 changed files with 58 additions and 1 deletions

View File

@ -29,8 +29,23 @@
#include <Esp.h> #include <Esp.h>
// Analog // Analog
uint8_t pwm_channel[8]={99,99,99,99,99,99,99,99};
inline uint32_t pin2chan(uint32_t pin) {
for (uint32_t cnt=0;cnt<8;cnt++) {
if ((pwm_channel[cnt]<99) && (pwm_channel[cnt]==pin)) {
return cnt;
}
}
return 0;
}
inline void analogWrite(uint8_t pin, int val) inline void analogWrite(uint8_t pin, int val)
{ {
uint32_t channel=pin2chan(pin);
ledcWrite(channel,val);
Serial.printf("write %d - %d\n",channel,val);
} }
inline void analogWriteFreq(uint32_t freq) inline void analogWriteFreq(uint32_t freq)
@ -40,6 +55,31 @@ inline void analogWriteRange(uint32_t range)
{ {
} }
inline void analogAttach(uint32_t pin, uint32_t channel) {
pwm_channel[channel&7]=pin;
ledcAttachPin(pin,channel);
Serial.printf("attach %d - %d\n",channel,pin);
}
inline uint32_t pow2(uint32_t x) {
uint32_t power = 1,bits=0;
while (power < x) {
power*=2;
bits++;
}
return bits-1;
}
// input range is in full range, ledc needs bits
inline void analogWriteFreqRange(uint32_t channel,uint32_t freq, uint32_t irange) {
uint32_t range=pow2(irange);
for (uint32_t cnt=0;cnt<8;cnt++) {
if (pwm_channel[cnt]<99) {
ledcSetup(cnt,freq,range);
}
}
Serial.printf("freq - range %d - %d\n",freq,range);
}
#define INPUT_PULLDOWN_16 INPUT_PULLUP #define INPUT_PULLDOWN_16 INPUT_PULLUP
typedef double real64_t; typedef double real64_t;
@ -57,7 +97,7 @@ typedef double real64_t;
// Serial minimal type to hold the config // Serial minimal type to hold the config
typedef int SerConfu8; typedef int SerConfu8;
typedef int SerialConfig; typedef int SerialConfig;
#define analogWrite(a, b) //#define analogWrite(a, b)
// //
// WS2812 // WS2812

View File

@ -1228,7 +1228,11 @@ void CmndPwmfrequency(void)
{ {
if ((1 == XdrvMailbox.payload) || ((XdrvMailbox.payload >= PWM_MIN) && (XdrvMailbox.payload <= PWM_MAX))) { if ((1 == XdrvMailbox.payload) || ((XdrvMailbox.payload >= PWM_MIN) && (XdrvMailbox.payload <= PWM_MAX))) {
Settings.pwm_frequency = (1 == XdrvMailbox.payload) ? PWM_FREQ : XdrvMailbox.payload; Settings.pwm_frequency = (1 == XdrvMailbox.payload) ? PWM_FREQ : XdrvMailbox.payload;
#ifdef ESP8266
analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c) analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c)
#else
analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range);
#endif
} }
ResponseCmndNumber(Settings.pwm_frequency); ResponseCmndNumber(Settings.pwm_frequency);
} }
@ -1242,7 +1246,11 @@ void CmndPwmrange(void)
Settings.pwm_value[i] = Settings.pwm_range; Settings.pwm_value[i] = Settings.pwm_range;
} }
} }
#ifdef ESP8266
analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h) analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h)
#else
analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range);
#endif
} }
ResponseCmndNumber(Settings.pwm_range); ResponseCmndNumber(Settings.pwm_range);
} }

View File

@ -1448,8 +1448,12 @@ void GpioInit(void)
if ((2 == Pin(GPIO_TXD)) || (H801 == my_module_type)) { Serial.set_tx(2); } if ((2 == Pin(GPIO_TXD)) || (H801 == my_module_type)) { Serial.set_tx(2); }
#endif // ESP8266 #endif // ESP8266
#ifdef ESP8266
analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h) analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h)
analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c) analogWriteFreq(Settings.pwm_frequency); // Default is 1000 (core_esp8266_wiring_pwm.c)
#else
analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range);
#endif
#ifdef USE_SPI #ifdef USE_SPI
spi_flg = (((PinUsed(GPIO_SPI_CS) && (Pin(GPIO_SPI_CS) > 14)) || (Pin(GPIO_SPI_CS) < 12)) || ((PinUsed(GPIO_SPI_DC) && (Pin(GPIO_SPI_DC) > 14)) || (Pin(GPIO_SPI_DC) < 12))); spi_flg = (((PinUsed(GPIO_SPI_CS) && (Pin(GPIO_SPI_CS) > 14)) || (Pin(GPIO_SPI_CS) < 12)) || ((PinUsed(GPIO_SPI_DC) && (Pin(GPIO_SPI_DC) > 14)) || (Pin(GPIO_SPI_DC) < 12)));
@ -1516,6 +1520,11 @@ void GpioInit(void)
for (uint32_t i = 0; i < MAX_PWMS; i++) { // Basic PWM control only for (uint32_t i = 0; i < MAX_PWMS; i++) { // Basic PWM control only
if (PinUsed(GPIO_PWM1, i)) { if (PinUsed(GPIO_PWM1, i)) {
pinMode(Pin(GPIO_PWM1, i), OUTPUT); pinMode(Pin(GPIO_PWM1, i), OUTPUT);
#ifdef ESP32
analogAttach(Pin(GPIO_PWM1, i),i);
analogWriteFreqRange(i,Settings.pwm_frequency,Settings.pwm_range);
#endif
if (light_type) { if (light_type) {
// force PWM GPIOs to low or high mode, see #7165 // force PWM GPIOs to low or high mode, see #7165
analogWrite(Pin(GPIO_PWM1, i), bitRead(pwm_inverted, i) ? Settings.pwm_range : 0); analogWrite(Pin(GPIO_PWM1, i), bitRead(pwm_inverted, i) ? Settings.pwm_range : 0);