MCPcopy Create free account
hub / github.com/FastLED/FastLED / analogWrite

Function analogWrite

src/platforms/arm/stm32/pin_stm32_native.hpp:317–490  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

315}
316
317inline void analogWrite(int pin, u16 val) FL_NOEXCEPT {
318 // NOTE: analogWrite requires STM32duino pinmap functions for Timer channel mapping
319 // This is because there's no direct way to map Arduino pin numbers to Timer channels
320 // without the pinmap tables provided by STM32duino core.
321 // For non-Arduino STM32 builds, PWM must be configured manually using HAL TIM APIs.
322 (void)pin;
323 (void)val;
324 FL_WARN("STM32: analogWrite not available without STM32duino core");
325#if 0 // Disabled: requires STM32duino pinmap functions
326#if defined(HAL_TIM_MODULE_ENABLED) && defined(FL_STM32_HAS_PINMAP)
327 // Implementation based on STM32duino analog.cpp pwm_start()
328 // Uses HAL Timer PWM APIs for 8-bit PWM output (Arduino-compatible)
329
330 // Note: PWM pinmapping requires STM32duino pinmap functions (pinmap_find_function, pinmap_peripheral)
331 // These are only available when STM32duino core is present
332 PinName pin_name = digitalPinToPinName(pin);
333 if (pin_name == NC) {
334 FL_WARN("STM32: Invalid pin " << pin);
335 return;
336 }
337
338 // Clamp value to 8-bit range (0-255) for Arduino compatibility
339 if (val > 255) val = 255;
340
341 // Find Timer instance and channel for this pin using STM32duino pinmap
342 u32 function = pinmap_find_function(pin_name, PinMap_TIM);
343 if (function == (u32)NC) {
344 // Pin doesn't support PWM - fall back to digital write
345 fl::pinMode(pin, PinMode::Output);
346 fl::digitalWrite(pin, val >= 128 ? PinValue::High : PinValue::Low);
347 return;
348 }
349
350 // Extract timer instance and channel from pinmap
351 TIM_TypeDef* timer_instance = (TIM_TypeDef*)pinmap_peripheral(pin_name, PinMap_TIM);
352 u32 timer_channel = STM_PIN_CHANNEL(function);
353
354 if (timer_instance == nullptr) {
355 FL_WARN("STM32: Failed to get Timer instance for pin " << pin);
356 return;
357 }
358
359 // Convert STM32 channel number to HAL channel constant
360 u32 hal_channel;
361 switch (timer_channel) {
362 case 1: hal_channel = TIM_CHANNEL_1; break;
363 case 2: hal_channel = TIM_CHANNEL_2; break;
364 case 3: hal_channel = TIM_CHANNEL_3; break;
365 case 4: hal_channel = TIM_CHANNEL_4; break;
366 default:
367 FL_WARN("STM32: Invalid timer channel " << timer_channel);
368 return;
369 }
370
371 // Configure GPIO pin as Timer alternate function
372 // This enables the timer to control the pin for PWM output
373 GPIO_TypeDef* port = get_GPIO_Port(STM_PORT(pin_name));
374 u32 pin_mask = STM_GPIO_PIN(pin_name);

Callers 1

setPwm16Function · 0.70

Calls 5

enableGPIOClockFunction · 0.85
enableTimerClockFunction · 0.85
getTimerClockFreqFunction · 0.85
pinModeFunction · 0.70
digitalWriteFunction · 0.70

Tested by

no test coverage detected