попытки запустится до 1 МГц

This commit is contained in:
2026-08-05 18:54:42 +03:00
parent c4c9167df1
commit 1edc420b77
11 changed files with 182 additions and 63 deletions

View File

@@ -14,12 +14,12 @@ bool PwmGenerator::start(uint32_t hz, uint8_t dutyPct, ActualPwm &a) {
const uint8_t bits = choosePwmResolution(hz, LEDC_SOURCE_CLOCK_HZ, LEDC_MAX_BITS);
if (!bits) return false;
if (!ledcAttachChannel(GPIO_PWM, hz, bits, LEDC_CHANNEL)) return false;
const uint32_t top = (1UL << bits) - 1UL;
const uint32_t duty = (static_cast<uint64_t>(top) * dutyPct + 50U) / 100U;
const uint32_t levels = 1UL << bits;
const uint32_t duty = (static_cast<uint64_t>(levels) * dutyPct + 50U) / 100U;
if (!ledcWriteChannel(LEDC_CHANNEL, duty)) { ledcDetach(GPIO_PWM); return false; }
const uint32_t actualHz = ledcReadFreq(GPIO_PWM);
if (!actualHz) { ledcDetach(GPIO_PWM); return false; }
a = {hz, actualHz, 100.0f * duty / top, bits};
a = {hz, actualHz, 100.0f * duty / levels, bits};
running_ = true;
return true;
}