Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix incorrect PWM bit depth on Esp32 with XTAL clock #4082

Merged
merged 4 commits into from
Aug 4, 2024
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 1 addition & 14 deletions wled00/bus_manager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -40,19 +40,6 @@ uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, byte
#define DEBUG_PRINTF_P(x...)
#endif

// ESP8266 has 1 MHz clock
blazoncek marked this conversation as resolved.
Show resolved Hide resolved
#ifdef ESP8266
#define CLOCK_FREQUENCY 1e6f
#else
// Use XTAL clock if possible to avoid timer frequency error when setting APB clock < 80 Mhz
// https://github.com/espressif/arduino-esp32/blob/2.0.2/cores/esp32/esp32-hal-ledc.c
#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
#define CLOCK_FREQUENCY 40e6f
#else
#define CLOCK_FREQUENCY 80e6f
#endif
#endif

//color mangling macros
#define RGBW32(r,g,b,w) (uint32_t((byte(w) << 24) | (byte(r) << 16) | (byte(g) << 8) | (byte(b))))
#define R(c) (byte((c) >> 16))
Expand Down Expand Up @@ -398,7 +385,7 @@ BusPwm::BusPwm(BusConfig &bc)
unsigned numPins = NUM_PWM_PINS(bc.type);
_frequency = bc.frequency ? bc.frequency : WLED_PWM_FREQ;
// duty cycle resolution (_depth) can be extracted from this formula: CLOCK_FREQUENCY > _frequency * 2^_depth
_depth = uint8_t(log((float)CLOCK_FREQUENCY / (float)_frequency) / log(2.0));
for (_depth=MAX_BIT_WIDTH; _depth>8; _depth--) if (((uint32_t(CLOCK_FREQUENCY)/_frequency)>>_depth) > 0) break;
blazoncek marked this conversation as resolved.
Show resolved Hide resolved

#ifdef ESP8266
analogWriteRange((1<<_depth)-1);
Expand Down
29 changes: 29 additions & 0 deletions wled00/const.h
Original file line number Diff line number Diff line change
Expand Up @@ -521,6 +521,35 @@
#endif
#endif

#ifndef CLOCK_FREQUENCY
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Perhaps change to MAX_PWM_CLOCK_FREQUENCY or something similar to avoid possible future clash.

#ifdef ESP8266
// 1 MHz clock
#define CLOCK_FREQUENCY 1e6f
blazoncek marked this conversation as resolved.
Show resolved Hide resolved
#else
// Use XTAL clock if possible to avoid timer frequency error when setting APB clock < 80 Mhz
// https://github.com/espressif/arduino-esp32/blob/2.0.2/cores/esp32/esp32-hal-ledc.c
#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
#define CLOCK_FREQUENCY 40e6f
#else
#define CLOCK_FREQUENCY 80e6f
#endif
#endif
#endif

#ifndef MAX_BIT_WIDTH
#ifdef ESP8266
#define MAX_BIT_WIDTH 10
#else
#ifdef SOC_LEDC_TIMER_BIT_WIDE_NUM
// C6/H2/P4: 20 bit, S2/S3/C2/C3: 14 bit
#define MAX_BIT_WIDTH SOC_LEDC_TIMER_BIT_WIDE_NUM
#else
// ESP32: 32 bit
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#define MAX_BIT_WIDTH 20
#endif
#endif
#endif

#define TOUCH_THRESHOLD 32 // limit to recognize a touch, higher value means more sensitive

// Size of buffer for API JSON object (increase for more segments)
Expand Down