FastLED 3.10.9
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 294 of file pin.cpp.hpp.

294 {
295 // Check if pin already has a channel
297
298 if (ch) {
299 // Pin already configured — release and reconfigure
301 ch = nullptr;
302 }
303
304 // Validate frequency
305 if (frequency_hz == 0) {
306 FL_WARN("setPwmFrequency: Frequency must be > 0");
307 return -1;
308 }
309
310 // Query platform: can it handle this natively?
311 bool needs_isr = platforms::needsPwmIsrFallback(pin, frequency_hz);
312
313 if (!needs_isr) {
314 // Native path
315 int result = platforms::setPwmFrequencyNative(pin, frequency_hz);
316 if (result != 0) {
317 FL_WARN("setPwmFrequency: Native backend failed: " << result);
318 return result;
319 }
320
321 // Allocate tracking slot
322 ch = pwm_state::allocate();
323 if (!ch) {
324 FL_WARN("setPwmFrequency: All " << (static_cast<int>(pwm_state::MAX_PWM_CHANNELS)) << " channels in use");
325 return -2;
326 }
327
328 ch->pin = pin;
329 ch->frequency_hz = frequency_hz;
331 ch->duty_cycle = 0;
332 return 0;
333 }
334
335 // ISR software fallback path
336 if (frequency_hz > pwm_state::MAX_ISR_PWM_FREQUENCY) {
337 FL_WARN("setPwmFrequency: ISR fallback max " << (pwm_state::MAX_ISR_PWM_FREQUENCY) << " Hz, requested " << frequency_hz);
338 return -1;
339 }
340
341 // Allocate channel
342 ch = pwm_state::allocate();
343 if (!ch) {
344 FL_WARN("setPwmFrequency: All " << (static_cast<int>(pwm_state::MAX_PWM_CHANNELS)) << " channels in use");
345 return -2;
346 }
347
348 // Ensure ISR is running
349 int isr_result = pwm_state::ensureIsrActive();
350 if (isr_result != 0) {
351 return -3;
352 }
353
354 // Configure GPIO
357
358 // Initialize channel (atomic)
359 {
360 fl::isr::critical_section cs;
361 ch->pin = pin;
362 ch->frequency_hz = frequency_hz;
364 ch->period_ticks = pwm_state::ISR_FREQUENCY_HZ / frequency_hz;
365 ch->duty_cycle = 0;
366 ch->high_ticks = 0;
367 ch->tick_counter = 0;
368 ch->pin_state = false;
369 }
370
371 return 0;
372}
#define FL_WARN(...)
Definition log.h:437
void releaseChannel(PwmPinState *ch) FL_NO_EXCEPT
Definition pin.cpp.hpp:225
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:191
PwmPinState * findByPin(int pin) FL_NO_EXCEPT
Definition pin.cpp.hpp:155
PwmPinState * allocate() FL_NO_EXCEPT
Definition pin.cpp.hpp:167
@ 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:398

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: