Skip to content

Commit 5c247d2

Browse files
committed
first implementation
1 parent ef8f23c commit 5c247d2

File tree

1 file changed

+16
-10
lines changed

1 file changed

+16
-10
lines changed

wled00/bus_manager.cpp

Lines changed: 16 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ uint8_t realtimeBroadcast(uint8_t type, IPAddress client, uint16_t length, byte
4040
#define DEBUG_PRINTF_P(x...)
4141
#endif
4242

43+
// ESP8266 has 1 MHz clock
44+
#ifdef ESP8266
45+
#define CLOCK_FREQUENCY 1e6f
46+
#else
47+
// Use XTAL clock if possible to avoid timer frequency error when setting APB clock < 80 Mhz
48+
// https://github.com/espressif/arduino-esp32/blob/2.0.2/cores/esp32/esp32-hal-ledc.c
49+
#ifdef SOC_LEDC_SUPPORT_XTAL_CLOCK
50+
#define CLOCK_FREQUENCY 40e6f
51+
#else
52+
#define CLOCK_FREQUENCY 80e6f
53+
#endif
54+
#endif
55+
4356
//color mangling macros
4457
#define RGBW32(r,g,b,w) (uint32_t((byte(w) << 24) | (byte(r) << 16) | (byte(g) << 8) | (byte(b))))
4558
#define R(c) (byte((c) >> 16))
@@ -384,24 +397,17 @@ BusPwm::BusPwm(BusConfig &bc)
384397
if (!IS_PWM(bc.type)) return;
385398
unsigned numPins = NUM_PWM_PINS(bc.type);
386399
_frequency = bc.frequency ? bc.frequency : WLED_PWM_FREQ;
400+
// duty cycle resolution (_depth) can be extracted from this formula: CLOCK_FREQUENCY > _frequency * 2^_depth
401+
_depth = uint8_t(log((float)CLOCK_FREQUENCY / (float)_frequency) / log(2.0));
387402

388403
#ifdef ESP8266
389-
// duty cycle resolution (_depth) can be extracted from this formula: 1MHz > _frequency * 2^_depth
390-
if (_frequency > 1760) _depth = 8;
391-
else if (_frequency > 880) _depth = 9;
392-
else _depth = 10; // WLED_PWM_FREQ <= 880Hz
393404
analogWriteRange((1<<_depth)-1);
394405
analogWriteFreq(_frequency);
395406
#else
396407
_ledcStart = pinManager.allocateLedc(numPins);
397408
if (_ledcStart == 255) { //no more free LEDC channels
398409
deallocatePins(); return;
399410
}
400-
// duty cycle resolution (_depth) can be extracted from this formula: 80MHz > _frequency * 2^_depth
401-
if (_frequency > 78124) _depth = 9;
402-
else if (_frequency > 39062) _depth = 10;
403-
else if (_frequency > 19531) _depth = 11;
404-
else _depth = 12; // WLED_PWM_FREQ <= 19531Hz
405411
#endif
406412

407413
for (unsigned i = 0; i < numPins; i++) {
@@ -419,7 +425,7 @@ BusPwm::BusPwm(BusConfig &bc)
419425
}
420426
_data = _pwmdata; // avoid malloc() and use stack
421427
_valid = true;
422-
DEBUG_PRINTF_P(PSTR("%successfully inited PWM strip with type %u and pins %u,%u,%u,%u,%u\n"), _valid?"S":"Uns", bc.type, _pins[0], _pins[1], _pins[2], _pins[3], _pins[4]);
428+
DEBUG_PRINTF_P(PSTR("%successfully inited PWM strip with type %u, frequency %u, bit depth %u and pins %u,%u,%u,%u,%u\n"), _valid?"S":"Uns", bc.type, _frequency, _depth, _pins[0], _pins[1], _pins[2], _pins[3], _pins[4]);
423429
}
424430

425431
void BusPwm::setPixelColor(uint16_t pix, uint32_t c) {

0 commit comments

Comments
 (0)