| 41 | /// @tparam SPI_SPEED SPI clock speed in Hz |
| 42 | template<u8 DATA_PIN, u8 CLOCK_PIN, u32 SPI_SPEED> |
| 43 | class SPIDeviceProxy { |
| 44 | private: |
| 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 | |
| 53 | public: |
| 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 |
nothing calls this directly
no test coverage detected