FastLED 3.9.15
Loading...
Searching...
No Matches
fastspi.h
Go to the documentation of this file.
1#pragma once
2
23
24#ifndef __INC_FASTSPI_H
25#define __INC_FASTSPI_H
26
27// ============================================================================
28// ALL INCLUDES FIRST (before any namespace declarations)
29// This satisfies the namespace-include-order linting requirement
30// ============================================================================
31
32#include "controller.h"
33#include "lib8tion.h"
34#include "fastspi_types.h"
35#include "platforms/shared/spi_bitbang/generic_software_spi.h" // ok platform headers
36#include "fl/stl/int.h"
37#include "platforms/cpu_frequency.h"
38
39// Include platform-specific SPI device proxy implementations BEFORE FastLED.h
40// These provide the hardware SPI abstractions for each platform
41// This must happen before FastLED.h includes chipsets.h
42#include "platforms/spi_device_proxy.h"
43
44// Include platform-specific SPIOutput template implementations BEFORE FastLED.h
45// The dispatch header selects the appropriate SPIOutput for the current platform
46// This must happen BEFORE FastLED.h includes chipsets.h
47#include "platforms/spi_output_template.h"
48
49// NOW include the internal FastLED header, which defines core types without cycles
50// At this point, SPIOutput is already defined for the current platform
51// Note: We use fl/system/fastled.h instead of FastLED.h to avoid cyclic dependencies,
52// since this file is included by FastLED.h
53#include "fl/system/fastled.h"
54
55// ============================================================================
56// DATA RATE MACROS (platform-specific clock calculations)
57// ============================================================================
58//
59// These macros provide platform-specific clock rate calculations for SPI:
60// - Hardware SPI platforms (ESP32, Teensy4, etc.): Return frequency in Hz
61// - Software SPI platforms (AVR, etc.): Return clock divider (CPU cycles per bit)
62//
63// New macros with semantic clarity (recommended):
64// FL_DATA_RATE_MHZ(X) - Returns either Hz (hardware) or divider (software) depending on platform
65// FL_DATA_RATE_KHZ(X) - Same as above but for kHz input
66// FL_TO_CLOCK_DIVIDER(FREQ_MHZ, CPU_FREQ_MHZ) - Explicit clock divider calculation
67//
68// Legacy macros (backwards compatibility):
69// DATA_RATE_MHZ(X) - Alias for FL_DATA_RATE_MHZ
70// DATA_RATE_KHZ(X) - Alias for FL_DATA_RATE_KHZ
71
72#if defined(FASTLED_TEENSY3) && (F_CPU > 48000000)
73// Teensy 3.x with overclocking: clock divider based on 48 MHz
74#define FL_DATA_RATE_MHZ(X) (((48000000L / 1000000L) / X))
75#define FL_DATA_RATE_KHZ(X) (((48000000L / 1000L) / X))
76#define FL_TO_CLOCK_DIVIDER(FREQ_MHZ, CPU_FREQ_MHZ) ((CPU_FREQ_MHZ) / (FREQ_MHZ))
77
78#elif defined(FASTLED_TEENSY4) || defined(ESP32) || (defined(ESP8266) && defined(FASTLED_ALL_PINS_HARDWARE_SPI)) || defined(FASTLED_STUB_IMPL)
79// Hardware SPI platforms: return frequency in Hz
80// ESP32 always uses hardware SPI via GPIO matrix (no conditional needed)
81#define FL_DATA_RATE_MHZ(X) (1000000 * (X))
82#define FL_DATA_RATE_KHZ(X) (1000 * (X))
83#define FL_TO_CLOCK_DIVIDER(FREQ_MHZ, CPU_FREQ_MHZ) ((CPU_FREQ_MHZ) / (FREQ_MHZ))
84
85#else
86// Software SPI platforms: return clock divider (CPU cycles per bit)
87// Use GET_CPU_FREQUENCY() for compile-time constant, avoiding issues with
88// STM32duino where F_CPU may be defined as runtime SystemCoreClock variable
89#define FL_DATA_RATE_MHZ(X) ((GET_CPU_FREQUENCY() / 1000000L) / X)
90#define FL_DATA_RATE_KHZ(X) ((GET_CPU_FREQUENCY() / 1000L) / X)
91#define FL_TO_CLOCK_DIVIDER(FREQ_MHZ, CPU_FREQ_MHZ) ((CPU_FREQ_MHZ) / (FREQ_MHZ))
92#endif
93
94// Backwards compatibility aliases
95#define DATA_RATE_MHZ FL_DATA_RATE_MHZ
96#define DATA_RATE_KHZ FL_DATA_RATE_KHZ
97
98// ============================================================================
99// GENERIC SOFTWARE SPI OUTPUT
100// ============================================================================
101// This template provides bit-banging SPI for any platform that supports GPIO
102// It's a fallback for platforms without dedicated hardware SPI support
103
106template<fl::u8 _DATA_PIN, fl::u8 _CLOCK_PIN, fl::u32 _SPI_CLOCK_DIVIDER>
107class SoftwareSPIOutput : public fl::GenericSoftwareSPIOutput<_DATA_PIN, _CLOCK_PIN, _SPI_CLOCK_DIVIDER> {};
108
109#endif // __INC_FASTSPI_H
Software SPI output (generic cross-platform bit-banging) NOTE: This is NOT in the fl namespace per fa...
Definition fastspi.h:107
Data types and constants used by SPI interfaces.
Internal FastLED header for implementation files.
Fast, efficient 8-bit math functions specifically designed for high-performance LED programming.