From bf29904b7af214baf8ed2d6083b8a70c6ed913d7 Mon Sep 17 00:00:00 2001 From: gemu2015 Date: Fri, 1 May 2020 17:38:40 +0200 Subject: [PATCH] esp32 initial pwm support --- .../src/esp8266toEsp32.h | 42 ++++++++++++++++++- tasmota/support_command.ino | 8 ++++ tasmota/support_tasmota.ino | 9 ++++ 3 files changed, 58 insertions(+), 1 deletion(-) diff --git a/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h b/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h index 6023507ae..3cfc54994 100644 --- a/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h +++ b/libesp32/ESP32-to-ESP8266-compat/src/esp8266toEsp32.h @@ -29,8 +29,23 @@ #include // 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) { + uint32_t channel=pin2chan(pin); + ledcWrite(channel,val); + Serial.printf("write %d - %d\n",channel,val); } 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 typedef double real64_t; @@ -57,7 +97,7 @@ typedef double real64_t; // Serial minimal type to hold the config typedef int SerConfu8; typedef int SerialConfig; -#define analogWrite(a, b) +//#define analogWrite(a, b) // // WS2812 diff --git a/tasmota/support_command.ino b/tasmota/support_command.ino index a446fec30..babd54928 100644 --- a/tasmota/support_command.ino +++ b/tasmota/support_command.ino @@ -1228,7 +1228,11 @@ void CmndPwmfrequency(void) { if ((1 == XdrvMailbox.payload) || ((XdrvMailbox.payload >= PWM_MIN) && (XdrvMailbox.payload <= PWM_MAX))) { Settings.pwm_frequency = (1 == XdrvMailbox.payload) ? PWM_FREQ : XdrvMailbox.payload; +#ifdef ESP8266 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); } @@ -1242,7 +1246,11 @@ void CmndPwmrange(void) Settings.pwm_value[i] = Settings.pwm_range; } } +#ifdef ESP8266 analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h) +#else + analogWriteFreqRange(0,Settings.pwm_frequency,Settings.pwm_range); +#endif } ResponseCmndNumber(Settings.pwm_range); } diff --git a/tasmota/support_tasmota.ino b/tasmota/support_tasmota.ino index d49633939..2d2d8d5d3 100644 --- a/tasmota/support_tasmota.ino +++ b/tasmota/support_tasmota.ino @@ -1448,8 +1448,12 @@ void GpioInit(void) if ((2 == Pin(GPIO_TXD)) || (H801 == my_module_type)) { Serial.set_tx(2); } #endif // ESP8266 +#ifdef ESP8266 analogWriteRange(Settings.pwm_range); // Default is 1023 (Arduino.h) 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 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 if (PinUsed(GPIO_PWM1, i)) { 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) { // force PWM GPIOs to low or high mode, see #7165 analogWrite(Pin(GPIO_PWM1, i), bitRead(pwm_inverted, i) ? Settings.pwm_range : 0);