FastLED 3.10.8
Loading...
Searching...
No Matches

◆ setPwmFrequency()

int fl::setPwmFrequency ( int pin,
u32 frequency_hz )

Set PWM frequency for a pin.

Subsequent analogWrite()/setPwm16() calls use this frequency. Automatically selects native hardware PWM or ISR-based software PWM depending on platform.

Parameters
pinPin number (platform-specific numbering)
frequency_hzDesired PWM frequency in Hz (1-500 Hz for ISR, platform-dependent for native HW)
Returns
0 on success, negative error code on failure (-1: invalid freq, -2: all channels in use, -3: ISR setup failed, -4: native HW failed)

Definition at line 290 of file pin.cpp.hpp.

290 {
291 // Check if pin already has a channel
293
294 if (ch) {
295 // Pin already configured — release and reconfigure
297 ch = nullptr;
298 }
299
300 // Validate frequency
301 if (frequency_hz == 0) {
302 FL_WARN("setPwmFrequency: Frequency must be > 0");
303 return -1;
304 }
305
306 // Query platform: can it handle this natively?
307 bool needs_isr = platforms::needsPwmIsrFallback(pin, frequency_hz);
308
309 if (!needs_isr) {
310 // Native path
311 int result = platforms::setPwmFrequencyNative(pin, frequency_hz);
312 if (result != 0) {
313 FL_WARN("setPwmFrequency: Native backend failed: " << result);
314 return result;
315 }
316
317 // Allocate tracking slot
318 ch = pwm_state::allocate();
319 if (!ch) {
320 FL_WARN("setPwmFrequency: All " << (static_cast<int>(pwm_state::MAX_PWM_CHANNELS)) << " channels in use");
321 return -2;
322 }
323
324 ch->pin = pin;
325 ch->frequency_hz = frequency_hz;
327 ch->duty_cycle = 0;
328 return 0;
329 }
330
331 // ISR software fallback path
332 if (frequency_hz > pwm_state::MAX_ISR_PWM_FREQUENCY) {
333 FL_WARN("setPwmFrequency: ISR fallback max " << (pwm_state::MAX_ISR_PWM_FREQUENCY) << " Hz, requested " << frequency_hz);
334 return -1;
335 }
336
337 // Allocate channel
338 ch = pwm_state::allocate();
339 if (!ch) {
340 FL_WARN("setPwmFrequency: All " << (static_cast<int>(pwm_state::MAX_PWM_CHANNELS)) << " channels in use");
341 return -2;
342 }
343
344 // Ensure ISR is running
345 int isr_result = pwm_state::ensureIsrActive();
346 if (isr_result != 0) {
347 return -3;
348 }
349
350 // Configure GPIO
353
354 // Initialize channel (atomic)
355 {
356 fl::isr::critical_section cs;
357 ch->pin = pin;
358 ch->frequency_hz = frequency_hz;
360 ch->period_ticks = pwm_state::ISR_FREQUENCY_HZ / frequency_hz;
361 ch->duty_cycle = 0;
362 ch->high_ticks = 0;
363 ch->tick_counter = 0;
364 ch->pin_state = false;
365 }
366
367 return 0;
368}
#define FL_WARN(...)
Definition log.h:437
void releaseChannel(PwmPinState *ch) FL_NO_EXCEPT
Definition pin.cpp.hpp:221
constexpr u8 MAX_PWM_CHANNELS
Definition pin.cpp.hpp:78
constexpr u32 MAX_ISR_PWM_FREQUENCY
Definition pin.cpp.hpp:80
constexpr u32 ISR_FREQUENCY_HZ
Definition pin.cpp.hpp:79
int ensureIsrActive() FL_NO_EXCEPT
Definition pin.cpp.hpp:190
PwmPinState * findByPin(int pin) FL_NO_EXCEPT
Definition pin.cpp.hpp:154
PwmPinState * allocate() FL_NO_EXCEPT
Definition pin.cpp.hpp:166
@ Low
Logic low (0V / GND)
Definition pin.h:51
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
void digitalWrite(int pin, PinValue val) FL_NO_EXCEPT
Write digital value to pin.
Definition pin.cpp.hpp:51
@ Output
Digital output (push-pull)
Definition pin.h:44
void pinMode(int pin, PinMode mode) FL_NO_EXCEPT
Set pin mode (input, output, pull-up, pull-down)
Definition pin.cpp.hpp:394

References fl::pwm_state::allocate(), fl::pwm_state::PwmPinState::backend, digitalWrite(), fl::pwm_state::PwmPinState::duty_cycle, fl::pwm_state::ensureIsrActive(), fl::pwm_state::findByPin(), FL_NO_EXCEPT, FL_WARN, fl::pwm_state::PwmPinState::frequency_hz, fl::pwm_state::PwmPinState::high_ticks, fl::pwm_state::ISR_FREQUENCY_HZ, fl::pwm_state::IsrSoftware, Low, fl::pwm_state::MAX_ISR_PWM_FREQUENCY, fl::pwm_state::MAX_PWM_CHANNELS, fl::pwm_state::Native, Output, fl::pwm_state::PwmPinState::period_ticks, fl::pwm_state::PwmPinState::pin, fl::pwm_state::PwmPinState::pin_state, pinMode(), fl::pwm_state::releaseChannel(), and fl::pwm_state::PwmPinState::tick_counter.

Referenced by fl::AnalogRGBController< RED_PIN, GREEN_PIN, BLUE_PIN >::init(), and initPanels().

+ Here is the call graph for this function:
+ Here is the caller graph for this function: