FastLED 3.10.10
Loading...
Searching...
No Matches
parallel_device.cpp.hpp
Go to the documentation of this file.
3#include "fl/log/log.h"
4#include "fl/log/log.h"
5
6// Include parallel SPI backends
7// IWYU pragma: begin_keep
8#include "platforms/shared/spi_bitbang/spi_isr_1.h" // ok platform headers
9#include "platforms/shared/spi_bitbang/spi_isr_2.h" // ok platform headers
10#include "platforms/shared/spi_bitbang/spi_isr_4.h" // ok platform headers
11#include "platforms/shared/spi_bitbang/spi_isr_8.h" // ok platform headers
12#include "platforms/shared/spi_bitbang/spi_isr_16.h" // ok platform headers
13#include "platforms/shared/spi_bitbang/spi_isr_32.h" // ok platform headers
14#include "platforms/shared/spi_bitbang/spi_block_1.h" // ok platform headers
15#include "platforms/shared/spi_bitbang/spi_block_2.h" // ok platform headers
16#include "platforms/shared/spi_bitbang/spi_block_4.h" // ok platform headers
17#include "platforms/shared/spi_bitbang/spi_block_8.h" // ok platform headers
18#include "platforms/shared/spi_bitbang/spi_block_16.h" // ok platform headers
19#include "platforms/shared/spi_bitbang/spi_block_32.h" // ok platform headers
20#include "platforms/shared/spi_bitbang/spi_pin_mapping.h" // ok platform headers
21#include "fl/stl/noexcept.h"
22// IWYU pragma: end_keep
23
24namespace fl {
25namespace spi {
26
27// ============================================================================
28// Config Implementation (to avoid circular dependency with Spi class)
29// ============================================================================
30
31ParallelDevice::Config::Config() FL_NO_EXCEPT
32 : clock_pin(0xFF)
34 , timer_hz(1600000) {
35}
36
37// ============================================================================
38// Implementation Details (pImpl pattern)
39// ============================================================================
40
44 void* backend; // Points to SpiIsr* or SpiBlock* (not owned by shared_ptr)
46 u8 backend_width; // 1, 2, 4, 8, 16, or 32
47
48 Impl(const Config& cfg)
49 FL_NO_EXCEPT : config(cfg)
50 , initialized(false)
51 , backend(nullptr)
52 , is_isr_mode(false)
53 , backend_width(0) {
54 }
55
57 if (initialized && backend) {
59 }
60 }
61
63 if (!backend) return;
64
65 // Stop ISR if in ISR mode
66 if (is_isr_mode) {
67 // ISR backends have stopISR() method
68 if (backend_width == 1) {
69 static_cast<SpiIsr1*>(backend)->stopISR();
70 } else if (backend_width == 2) {
71 static_cast<SpiIsr2*>(backend)->stopISR();
72 } else if (backend_width == 4) {
73 static_cast<SpiIsr4*>(backend)->stopISR();
74 } else if (backend_width == 8) {
75 static_cast<SpiIsr8*>(backend)->stopISR();
76 } else if (backend_width == 16) {
77 static_cast<SpiIsr16*>(backend)->stopISR();
78 } else if (backend_width == 32) {
79 static_cast<SpiIsr32*>(backend)->stopISR();
80 }
81 }
82
83 // Note: SpiIsr* and SpiBlock* backends don't need delete
84 // (they're typically stack-allocated in this impl)
85 backend = nullptr;
86 initialized = false;
87 }
88};
89
90// ============================================================================
91// Helper Functions
92// ============================================================================
93
94namespace {
95
100void buildDefaultLUT(const fl::vector<u8>& gpio_pins,
101 u32* set_masks,
102 u32* clear_masks) FL_NO_EXCEPT {
103 size_t num_pins = gpio_pins.size();
104
105 // For each possible byte value (0-255)
106 for (int byte_val = 0; byte_val < 256; byte_val++) {
107 u32 set_mask = 0;
108 u32 clear_mask = 0;
109
110 // Map byte bits to GPIO pins
111 for (size_t bit_pos = 0; bit_pos < num_pins && bit_pos < 8; bit_pos++) {
112 u32 pin_mask = u32(1) << gpio_pins[bit_pos];
113
114 if (byte_val & (1 << bit_pos)) {
115 set_mask |= pin_mask; // Set this pin high
116 } else {
117 clear_mask |= pin_mask; // Clear this pin low
118 }
119 }
120
121 // For pins beyond bit 7, always clear them (if num_pins > 8)
122 for (size_t pin_idx = 8; pin_idx < num_pins; pin_idx++) {
123 clear_mask |= (u32(1) << gpio_pins[pin_idx]);
124 }
125
126 set_masks[byte_val] = set_mask;
127 clear_masks[byte_val] = clear_mask;
128 }
129}
130
131} // anonymous namespace
132
133// ============================================================================
134// ParallelDevice Implementation
135// ============================================================================
136
139
140 // Validate configuration
141 size_t num_pins = config.gpio_pins.size();
142 if (num_pins == 0 || num_pins > 32) {
143 FL_WARN("ParallelDevice: Invalid number of GPIO pins (" << num_pins << "), must be 1-32");
144 }
145
146 FL_DBG("ParallelDevice: Created with " << num_pins << " GPIO pins");
147}
148
150 if (pImpl && pImpl->initialized) {
151 end();
152 }
153}
154
156 if (!pImpl) {
157 return fl::task::Error("Device not initialized");
158 }
159
160 if (pImpl->initialized) {
161 // Already initialized - idempotent
162 return fl::nullopt;
163 }
164
165 size_t num_pins = pImpl->config.gpio_pins.size();
166
167 // Validate pin count
168 if (num_pins == 0 || num_pins > 32) {
169 return fl::task::Error("Invalid number of GPIO pins (must be 1-32)");
170 }
171
172 // buildDefaultLUT shifts each pin into a 32-bit mask, so reject anything it
173 // cannot represent before it runs.
174 if (!spiPinMappingValid(pImpl->config.gpio_pins.data(), num_pins,
175 pImpl->config.clock_pin)) {
176 return fl::task::Error("Invalid pin mapping (pins 0-31; data lanes 0-7 distinct and off the clock)");
177 }
178
179 // Determine backend width (round up to next supported width: 1,2,4,8,16,32)
180 u8 backend_width = 1;
181 if (num_pins > 16) backend_width = 32;
182 else if (num_pins > 8) backend_width = 16;
183 else if (num_pins > 4) backend_width = 8;
184 else if (num_pins > 2) backend_width = 4;
185 else if (num_pins > 1) backend_width = 2;
186
187 pImpl->backend_width = backend_width;
188
189 // Determine execution mode
190 bool use_isr = (pImpl->config.mode == SpiParallelMode::ISR_ASYNC) ||
191 (pImpl->config.mode == SpiParallelMode::AUTO); // Default to ISR if AUTO
192 pImpl->is_isr_mode = use_isr;
193
194 // Build default LUT
195 u32 set_masks[256];
196 u32 clear_masks[256];
197 buildDefaultLUT(pImpl->config.gpio_pins, set_masks, clear_masks);
198
199 // Create and initialize backend
200 // Note: We can't use polymorphism here due to different backend types
201 // Instead, we use type-erasure with void* and manual dispatch
202
203 if (use_isr) {
204 // ISR-based backend
205 FL_DBG("ParallelDevice: Initializing ISR mode (width=" << ((int)backend_width) << ")");
206
207 // Note: These need to be heap-allocated for type-erasure
208 // For now, return error indicating ISR mode not yet implemented
209 return fl::task::Error("ISR mode not yet implemented for ParallelDevice");
210
211 } else {
212 // Bit-bang (blocking) backend
213 FL_DBG("ParallelDevice: Initializing bit-bang mode (width=" << ((int)backend_width) << ")");
214
215 // Note: These need to be heap-allocated for type-erasure
216 // For now, return error indicating bit-bang mode not yet implemented
217 return fl::task::Error("Bit-bang mode not yet implemented for ParallelDevice");
218 }
219
220 pImpl->initialized = true;
221 return fl::nullopt;
222}
223
225 if (!pImpl || !pImpl->initialized) {
226 return;
227 }
228
229 // Wait for pending operations
230 waitComplete();
231
232 // Release backend
233 pImpl->releaseBackend();
234
235 FL_DBG("ParallelDevice: Shutdown complete");
236}
237
239 return pImpl && pImpl->initialized && pImpl->backend != nullptr;
240}
241
243 if (!isReady()) {
244 return Result<Transaction>::failure(SPIError::NOT_INITIALIZED,
245 "Device not initialized");
246 }
247
248 if (!data || size == 0) {
249 return Result<Transaction>::failure(SPIError::ALLOCATION_FAILED,
250 "Invalid data or size");
251 }
252
253 // Write implementation depends on backend type
254 // For now, return not implemented
255 return Result<Transaction>::failure(SPIError::NOT_SUPPORTED,
256 "ParallelDevice::write() not yet implemented");
257}
258
260 (void)timeout_ms; // Unused parameter - reserved for future implementation
261 if (!isReady()) {
262 return false;
263 }
264
265 // Wait implementation depends on backend type
266 // For ISR mode, check status flags
267 // For bit-bang mode, transmission is already complete (blocking)
268
269 return true; // Placeholder
270}
271
273 if (!isReady()) {
274 return false;
275 }
276
277 // Busy check depends on backend type
278 return false; // Placeholder
279}
280
281void ParallelDevice::configureLUT(const u32* set_masks, const u32* clear_masks) FL_NO_EXCEPT {
282 if (!set_masks || !clear_masks) {
283 FL_WARN("ParallelDevice: Invalid LUT pointers");
284 return;
285 }
286
287 // LUT configuration depends on backend type
288 // For now, this is a no-op
289 FL_DBG("ParallelDevice: LUT configuration not yet implemented");
290}
291
293 static const Config empty_config;
294 return pImpl ? pImpl->config : empty_config;
295}
296
297} // namespace spi
298} // namespace fl
static expected failure(E err, const char *msg=nullptr) FL_NO_EXCEPT
Create error result.
Definition expected.h:115
bool isBusy() const FL_NO_EXCEPT
Check if transmission is in progress.
void configureLUT(const u32 *set_masks, const u32 *clear_masks) FL_NO_EXCEPT
Configure custom LUT (advanced)
bool isReady() const FL_NO_EXCEPT
Check if device is initialized.
void end() FL_NO_EXCEPT
Shutdown hardware and release resources.
fl::optional< fl::task::Error > begin() FL_NO_EXCEPT
Initialize hardware and setup LUT.
~ParallelDevice() FL_NO_EXCEPT
Destructor - releases hardware resources.
Result< Transaction > write(const u8 *data, size_t size) FL_NO_EXCEPT
Write data (single stream drives all pins via LUT)
const Config & getConfig() const FL_NO_EXCEPT
Get current configuration.
ParallelDevice(const Config &config) FL_NO_EXCEPT
Construct parallel device.
bool waitComplete(u32 timeout_ms=(fl::numeric_limits< u32 >::max)()) FL_NO_EXCEPT
Wait for pending transmission to complete.
fl::unique_ptr< Impl > pImpl
Shared implementation helpers for SPI device classes.
#define FL_WARN(...)
Definition log.h:437
#define FL_DBG
Definition log.h:553
Centralized logging categories for FastLED hardware interfaces and subsystems.
fl::enable_if<!fl::is_array< T >::value, unique_ptr< T > >::type make_unique(Args &&... args) FL_NO_EXCEPT
Definition unique_ptr.h:263
void buildDefaultLUT(const fl::vector< u8 > &gpio_pins, u32 *set_masks, u32 *clear_masks) FL_NO_EXCEPT
Build default LUT for parallel GPIO mapping.
fl::result< T, SPIError > Result
Definition transaction.h:29
unsigned char u8
Definition stdint.h:132
u8 u8 fract8 blur_amount FL_NO_EXCEPT
Definition blur.h:186
Optional< T > optional
Definition optional.h:16
@ AUTO
Definition bus.h:32
constexpr nullopt_t nullopt
Definition optional.h:13
SpiParallelMode
Parallel device execution modes.
Definition config.h:33
@ ISR_ASYNC
ISR-driven async mode.
Definition config.h:35
@ AUTO
Auto-select best mode (default: ISR)
Definition config.h:34
Base definition for an LED controller.
Parallel GPIO SPI device for 1-32 outputs driven from single data stream.
u32 timer_hz
Timer frequency for ISR mode.
SpiParallelMode mode
Execution mode (ISR vs bit-bang)
Configuration for parallel GPIO SPI.
Impl(const Config &cfg) FL_NO_EXCEPT
Error type for promises.
Definition promise.h:39