FastLED 3.9.15
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 274 of file pin.cpp.hpp.

274 {
275 // Check if pin already has a channel
277
278 if (ch) {
279 // Pin already configured — release and reconfigure
281 ch = nullptr;
282 }
283
284 // Validate frequency
285 if (frequency_hz == 0) {
286 FL_WARN("setPwmFrequency: Frequency must be > 0");
287 return -1;
288 }
289
290 // Query platform: can it handle this natively?
291 bool needs_isr = platforms::needsPwmIsrFallback(pin, frequency_hz);
292
293 if (!needs_isr) {
294 // Native path
295 int result = platforms::setPwmFrequencyNative(pin, frequency_hz);
296 if (result != 0) {
297 FL_WARN("setPwmFrequency: Native backend failed: " << result);
298 return result;
299 }
300
301 // Allocate tracking slot
302 ch = pwm_state::allocate();
303 if (!ch) {
304 FL_WARN("setPwmFrequency: All " << static_cast<int>(pwm_state::MAX_PWM_CHANNELS) << " channels in use");
305 return -2;
306 }
307
308 ch->pin = pin;
309 ch->frequency_hz = frequency_hz;
311 ch->duty_cycle = 0;
312 return 0;
313 }
314
315 // ISR software fallback path
316 if (frequency_hz > pwm_state::MAX_ISR_PWM_FREQUENCY) {
317 FL_WARN("setPwmFrequency: ISR fallback max " << pwm_state::MAX_ISR_PWM_FREQUENCY << " Hz, requested " << frequency_hz);
318 return -1;
319 }
320
321 // Allocate channel
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 // Ensure ISR is running
329 int isr_result = pwm_state::ensureIsrActive();
330 if (isr_result != 0) {
331 return -3;
332 }
333
334 // Configure GPIO
337
338 // Initialize channel (atomic)
339 {
340 fl::isr::critical_section cs;
341 ch->pin = pin;
342 ch->frequency_hz = frequency_hz;
344 ch->period_ticks = pwm_state::ISR_FREQUENCY_HZ / frequency_hz;
345 ch->duty_cycle = 0;
346 ch->high_ticks = 0;
347 ch->tick_counter = 0;
348 ch->pin_state = false;
349 }
350
351 return 0;
352}
#define FL_WARN(X)
Definition log.h:276
constexpr u8 MAX_PWM_CHANNELS
Definition pin.cpp.hpp:79
constexpr u32 MAX_ISR_PWM_FREQUENCY
Definition pin.cpp.hpp:81
PwmPinState * allocate()
Definition pin.cpp.hpp:152
void releaseChannel(PwmPinState *ch)
Definition pin.cpp.hpp:205
constexpr u32 ISR_FREQUENCY_HZ
Definition pin.cpp.hpp:80
PwmPinState * findByPin(int pin)
Definition pin.cpp.hpp:141
int ensureIsrActive()
Definition pin.cpp.hpp:175
@ Low
Logic low (0V / GND)
Definition pin.h:50
expected< T, E > result
Alias for expected (Rust-style naming)
Definition result.h:31
void pinMode(int pin, PinMode mode)
Set pin mode (input, output, pull-up, pull-down)
Definition pin.cpp.hpp:378
void digitalWrite(int pin, PinValue val)
Write digital value to pin.
Definition pin.cpp.hpp:51
@ Output
Digital output (push-pull)
Definition pin.h:43

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_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 initPanels().

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