SPI.h 1.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546
  1. #pragma once
  2. // Minimal SPIClass/SPISettings substitute so GxEPD2 compiles under ESP-IDF,
  3. // backed by esphome::spi::SPIDevice underneath. GxEPD2 always drives
  4. // chip-select itself around each transfer, so this only needs to own the
  5. // clock/data lines - which is what lets a gxepd2 instance share one spi:
  6. // bus with other devices, each with its own SPIDevice registration.
  7. #include <cstdint>
  8. #include "esphome/components/spi/spi.h"
  9. class SPISettings {
  10. public:
  11. SPISettings() = default;
  12. SPISettings(uint32_t clock, uint8_t bit_order, uint8_t data_mode) {}
  13. };
  14. #define MSBFIRST 0
  15. #define SPI_MODE0 0
  16. class SPIClass : public esphome::spi::SPIDevice<esphome::spi::BIT_ORDER_MSB_FIRST, esphome::spi::CLOCK_POLARITY_LOW,
  17. esphome::spi::CLOCK_PHASE_LEADING, esphome::spi::DATA_RATE_4MHZ> {
  18. public:
  19. void begin() {}
  20. void end() {}
  21. void beginTransaction(SPISettings settings) { this->enable(); }
  22. void endTransaction() { this->disable(); }
  23. // GxEPD2 panels (all but the IT8951-based ones, which aren't supported -
  24. // see display.py) never read anything back over MISO; e-paper wiring
  25. // normally leaves it unconnected, and esphome's spi: bus then registers
  26. // this device as write-only. transfer_byte()/delegate transfer() ask for
  27. // a real bidirectional exchange regardless, which a write-only device
  28. // refuses outright - it doesn't just skip the read, it drops the write
  29. // too (see SPIDelegateHw::transfer in spi_esp_idf.cpp), silently bricking
  30. // every panel command. write_byte() only ever asks to write, so it works
  31. // whether or not MISO is wired.
  32. uint8_t transfer(uint8_t data) {
  33. this->write_byte(data);
  34. return 0;
  35. }
  36. };
  37. // Unused: every gxepd2 instance overrides this via selectSPI() with its own
  38. // SPIClass member - see gxepd2_display.h.
  39. inline SPIClass SPI; // NOLINT(cppcoreguidelines-avoid-non-const-global-variables)