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

Class SPIDeviceProxy

src/platforms/arm/stm32/spi_device_proxy.h:43–246  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

41/// @tparam SPI_SPEED SPI clock speed in Hz
42template<u8 DATA_PIN, u8 CLOCK_PIN, u32 SPI_SPEED>
43class SPIDeviceProxy {
44private:
45 SPIBusHandle mHandle; // Handle from SPIBusManager
46 SPIBusManager* mBusManager; // Pointer to global bus manager
47 fl::unique_ptr<STM32SPIOutput<DATA_PIN, CLOCK_PIN, SPI_SPEED>> mSingleSPI;
48 fl::vector<u8> mWriteBuffer; // Buffered writes (for Multi-lane SPI)
49 bool mInitialized; // Whether init() was called
50 bool mBusInitialized; // Whether bus manager has been initialized
51 bool mInTransaction; // Whether select() was called
52
53public:
54 /// Constructor - just stores pins, actual setup happens in init()
55 SPIDeviceProxy()
56 : mHandle()
57 , mBusManager(nullptr)
58 , mInitialized(false)
59 , mBusInitialized(false)
60 , mInTransaction(false)
61 {
62 }
63
64 /// Destructor - cleanup owned resources and unregister from bus manager
65 ~SPIDeviceProxy() {
66 // Unregister from bus manager (releases Multi-lane SPI if last device)
67 if (mBusManager && mHandle.is_valid) {
68 mBusManager->unregisterDevice(mHandle);
69 mHandle = SPIBusHandle(); // Invalidate handle
70 }
71
72 mSingleSPI.reset();
73 }
74
75 /// Initialize SPI device and register with bus manager
76 /// Called by LED controller's init() method
77 void init() FL_NOEXCEPT {
78 if (mInitialized) {
79 return; // Already initialized
80 }
81
82 // Get global bus manager
83 mBusManager = &getSPIBusManager();
84
85 // Register with bus manager
86 // NOTE: Bus manager will determine if we use Single/Dual/Quad/Octal SPI
87 // based on how many devices share our clock pin
88 mHandle = mBusManager->registerDevice(CLOCK_PIN, DATA_PIN, SPI_SPEED, this);
89
90 if (!mHandle.is_valid) {
91 FL_WARN("SPIDeviceProxy: Failed to register with bus manager (pin "
92 << static_cast<int>(CLOCK_PIN) << ":" << static_cast<int>(DATA_PIN) << ")");
93 return;
94 }
95
96 // IMPORTANT: DO NOT initialize bus manager here!
97 // We defer initialization until the first transmit() call (lazy initialization).
98 // This allows all devices on the same clock pin to register before the bus
99 // decides whether to use Single-SPI, Dual-SPI, or Quad-SPI mode.
100 // If we initialize here, the first device gets SINGLE_SPI mode before other

Callers

nothing calls this directly

Calls 4

registerDeviceMethod · 0.80
clearMethod · 0.45
selectMethod · 0.45
releaseMethod · 0.45

Tested by

no test coverage detected