| 54 | /// @tparam SPI_CLOCK_DIVIDER SPI clock divider (see NRF52HardwareSPIOutput for details) |
| 55 | template<u8 DATA_PIN, u8 CLOCK_PIN, u32 SPI_CLOCK_DIVIDER> |
| 56 | class SPIDeviceProxy { |
| 57 | private: |
| 58 | SPIBusHandle mHandle; // Handle from SPIBusManager |
| 59 | SPIBusManager* mBusManager; // Pointer to global bus manager |
| 60 | fl::unique_ptr<NRF52HardwareSPIOutput<DATA_PIN, CLOCK_PIN, SPI_CLOCK_DIVIDER>> mSingleSPI; |
| 61 | fl::vector<u8> mWriteBuffer; // Buffered writes (for multi-lane SPI) |
| 62 | bool mInitialized; // Whether init() was called |
| 63 | bool mBusInitialized; // Whether bus manager has been initialized |
| 64 | bool mInTransaction; // Whether select() was called |
| 65 | |
| 66 | public: |
| 67 | /// Constructor - just stores pins, actual setup happens in init() |
| 68 | SPIDeviceProxy() |
| 69 | : mHandle() |
| 70 | , mBusManager(nullptr) |
| 71 | , mInitialized(false) |
| 72 | , mBusInitialized(false) |
| 73 | , mInTransaction(false) |
| 74 | { |
| 75 | } |
| 76 | |
| 77 | /// Destructor - cleanup owned resources and unregister from bus manager |
| 78 | ~SPIDeviceProxy() { |
| 79 | // Unregister from bus manager (releases multi-lane SPI if last device) |
| 80 | if (mBusManager && mHandle.is_valid) { |
| 81 | mBusManager->unregisterDevice(mHandle); |
| 82 | mHandle = SPIBusHandle(); // Invalidate handle |
| 83 | } |
| 84 | |
| 85 | mSingleSPI.reset(); |
| 86 | } |
| 87 | |
| 88 | /// Initialize SPI device and register with bus manager |
| 89 | /// Called by LED controller's init() method |
| 90 | void init() FL_NOEXCEPT { |
| 91 | if (mInitialized) { |
| 92 | return; // Already initialized |
| 93 | } |
| 94 | |
| 95 | // Get global bus manager |
| 96 | mBusManager = &getSPIBusManager(); |
| 97 | |
| 98 | // Register with bus manager |
| 99 | // NOTE: Bus manager will determine if we use Single/Dual/Quad/Octal SPI |
| 100 | // based on how many devices share our clock pin |
| 101 | // NOTE: NRF52 uses SPI_CLOCK_DIVIDER instead of SPI_SPEED (hardware limitation) |
| 102 | mHandle = mBusManager->registerDevice(CLOCK_PIN, DATA_PIN, SPI_CLOCK_DIVIDER, this); |
| 103 | |
| 104 | if (!mHandle.is_valid) { |
| 105 | FL_WARN("SPIDeviceProxy: Failed to register with bus manager (pin " |
| 106 | << static_cast<int>(CLOCK_PIN) << ":" << static_cast<int>(DATA_PIN) << ")"); |
| 107 | return; |
| 108 | } |
| 109 | |
| 110 | // IMPORTANT: DO NOT initialize bus manager here! |
| 111 | // We defer initialization until the first transmit() call (lazy initialization). |
| 112 | // This allows all devices on the same clock pin to register before the bus |
| 113 | // decides whether to use Single-SPI, Dual-SPI, or Quad-SPI mode. |
nothing calls this directly
no test coverage detected