cycle_engine.h 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  1. #pragma once
  2. #include <cstdint>
  3. #include "esphome/core/gpio.h"
  4. #include "esphome/core/hal.h"
  5. #include "esphome/core/log.h"
  6. #include "esphome/components/spi/spi.h"
  7. #include "epd_command.h"
  8. namespace esphome::gxepd2 {
  9. using EpdSPIDevice = spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
  10. spi::DATA_RATE_20MHZ>;
  11. // Mechanically replicates one GxEPD2 driver's refresh cycle - reset pulse,
  12. // init command table, RAM-window set, per-plane byte burst, refresh
  13. // trigger, busy-pin wait - as a state machine stepped from loop(), instead
  14. // of GxEPD2's own synchronous top-to-bottom call stack. See the plan this
  15. // was built from (and gxepd2_task_watchdog_status in project memory) for
  16. // why: that call stack has no yield points, so making it non-blocking used
  17. // to require running the whole thing on a separate FreeRTOS task, which
  18. // intermittently tripped the ESP-IDF watchdog anyway. step() never blocks -
  19. // every state does a small bounded unit of work and returns - so there is
  20. // nothing here that can starve another task or need a watchdog subscription
  21. // at all.
  22. class CycleEngine {
  23. public:
  24. void begin(const EpdPanelDescriptor *desc, GPIOPin *cs, GPIOPin *dc, GPIOPin *rst, GPIOPin *busy,
  25. EpdSPIDevice *spi) {
  26. this->desc_ = desc;
  27. this->cs_ = cs;
  28. this->dc_ = dc;
  29. this->rst_ = rst;
  30. this->busy_ = busy;
  31. this->spi_ = spi;
  32. this->cs_->setup();
  33. this->dc_->setup();
  34. if (this->rst_ != nullptr)
  35. this->rst_->setup();
  36. if (this->busy_ != nullptr)
  37. this->busy_->setup();
  38. this->cs_->digital_write(true);
  39. this->dc_->digital_write(true);
  40. }
  41. // Arms the state machine; returns immediately. planes[1] is ignored when
  42. // desc->plane_count == 1. Undefined to call while !idle().
  43. void request_full_update(const uint8_t *plane0, const uint8_t *plane1) {
  44. this->planes_[0] = plane0;
  45. this->planes_[1] = plane1;
  46. this->plane_bytes_ = (static_cast<uint32_t>(this->desc_->width) * this->desc_->height) / 8;
  47. this->cursor_ = 0;
  48. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  49. this->set_state_(this->rst_ != nullptr ? State::RESET_LOW : State::INIT_CMD);
  50. }
  51. bool idle() const { return this->state_ == State::IDLE; }
  52. // Called once per loop() tick. Does one bounded unit of work.
  53. void step() {
  54. switch (this->state_) {
  55. case State::IDLE:
  56. return;
  57. case State::RESET_LOW:
  58. this->rst_->digital_write(true); // preset high, then pulse low - see GxEPD2_EPD::_reset()
  59. this->deadline_ = millis() + 10;
  60. this->set_state_(State::RESET_LOW_WAIT);
  61. return;
  62. case State::RESET_LOW_WAIT:
  63. if (millis() < this->deadline_)
  64. return;
  65. this->rst_->digital_write(false);
  66. this->deadline_ = millis() + 10;
  67. this->set_state_(State::RESET_HIGH_WAIT);
  68. return;
  69. case State::RESET_HIGH_WAIT:
  70. if (millis() < this->deadline_)
  71. return;
  72. this->rst_->digital_write(true);
  73. this->deadline_ = millis() + 10;
  74. this->set_state_(State::RESET_SETTLE);
  75. return;
  76. case State::RESET_SETTLE:
  77. if (millis() < this->deadline_)
  78. return;
  79. this->cursor_ = 0;
  80. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  81. this->set_state_(State::INIT_CMD);
  82. return;
  83. case State::INIT_CMD:
  84. this->step_sequence_(this->desc_->init_sequence, this->desc_->init_sequence_len, State::INIT_CMD,
  85. State::SET_WINDOW);
  86. return;
  87. case State::SET_WINDOW:
  88. this->set_full_window_();
  89. this->cursor_ = 0;
  90. this->plane_ = 0;
  91. this->set_state_(State::WRITE_PLANE);
  92. return;
  93. case State::WRITE_PLANE:
  94. this->step_write_plane_();
  95. return;
  96. case State::REFRESH_CMD:
  97. this->step_sequence_(this->desc_->refresh_sequence, this->desc_->refresh_sequence_len, State::REFRESH_CMD,
  98. State::DONE);
  99. return;
  100. case State::POST_DELAY:
  101. if (millis() < this->deadline_)
  102. return;
  103. this->set_state_(this->delay_return_state_);
  104. return;
  105. case State::WAIT_BUSY:
  106. this->step_wait_busy_();
  107. return;
  108. case State::DONE:
  109. ESP_LOGD("gxepd2", "Display update finished");
  110. this->set_state_(State::IDLE);
  111. return;
  112. }
  113. }
  114. private:
  115. enum class State {
  116. IDLE,
  117. RESET_LOW,
  118. RESET_LOW_WAIT,
  119. RESET_HIGH_WAIT,
  120. RESET_SETTLE,
  121. INIT_CMD,
  122. SET_WINDOW,
  123. WRITE_PLANE,
  124. REFRESH_CMD,
  125. POST_DELAY,
  126. WAIT_BUSY,
  127. DONE,
  128. };
  129. enum class CmdPhase { BEFORE_DELAY, ISSUE };
  130. // Every command byte and every data byte is its own SPI transaction, CS
  131. // toggled around each one - this looks wasteful, but it's mechanically
  132. // what GxEPD2_EPD::_writeCommand()/_writeData(uint8_t) do (see
  133. // vendor/src/GxEPD2_EPD.cpp), and matching the vendor cycle byte-for-byte
  134. // is the point of this engine.
  135. void write_byte_(uint8_t b, bool dc_high) {
  136. this->dc_->digital_write(dc_high);
  137. this->spi_->enable();
  138. this->cs_->digital_write(false);
  139. this->spi_->write_byte(b);
  140. this->cs_->digital_write(true);
  141. this->spi_->disable();
  142. }
  143. void write_command_(uint8_t cmd, const uint8_t *data, uint8_t data_len) {
  144. this->write_byte_(cmd, false);
  145. for (uint8_t i = 0; i < data_len; i++)
  146. this->write_byte_(data[i], true);
  147. }
  148. // Steps through one EpdCommand table one entry (and one delay/busy-wait,
  149. // if that entry has one) at a time, resuming in self_state after any
  150. // wait so the same call re-enters here for the next entry. Shared by
  151. // INIT_CMD and REFRESH_CMD - which table is being stepped is entirely
  152. // determined by which state called this.
  153. void step_sequence_(const EpdCommand *seq, uint8_t len, State self_state, State done_state) {
  154. if (this->cursor_ >= len) {
  155. this->cursor_ = 0;
  156. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  157. this->set_state_(done_state);
  158. return;
  159. }
  160. const EpdCommand &c = seq[this->cursor_];
  161. if (this->cmd_phase_ == CmdPhase::BEFORE_DELAY) {
  162. this->cmd_phase_ = CmdPhase::ISSUE;
  163. if (c.delay_before_ms > 0) {
  164. this->start_delay_(c.delay_before_ms, self_state);
  165. return;
  166. }
  167. }
  168. this->write_command_(c.cmd, c.data, c.data_len);
  169. this->cursor_++;
  170. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  171. if (c.wait_busy_after) {
  172. this->start_busy_wait_(self_state);
  173. } else if (c.delay_after_ms > 0) {
  174. this->start_delay_(c.delay_after_ms, self_state);
  175. }
  176. // else: stays in self_state; the next step() call processes the next entry.
  177. }
  178. // GxEPD2_EPD::_setPartialRamArea(0, 0, width, height) - full frame only,
  179. // this slice doesn't do partial windows yet.
  180. void set_full_window_() {
  181. uint16_t w = this->desc_->width, h = this->desc_->height;
  182. uint16_t xe = w - 1, ye = h - 1;
  183. const uint8_t x44[] = {0, static_cast<uint8_t>(xe / 8)};
  184. this->write_command_(0x44, x44, 2);
  185. const uint8_t x45[] = {0, 0, static_cast<uint8_t>(ye % 256), static_cast<uint8_t>(ye / 256)};
  186. this->write_command_(0x45, x45, 4);
  187. const uint8_t x4e[] = {0};
  188. this->write_command_(0x4E, x4e, 1);
  189. const uint8_t x4f[] = {0, 0};
  190. this->write_command_(0x4F, x4f, 2);
  191. }
  192. // Writes up to BYTE_BUDGET bytes of the current plane per call, then
  193. // returns - this bound is what keeps a full-frame write (thousands of
  194. // bytes) from ever making one loop() tick run long, replacing the old
  195. // per-256-byte esp_task_wdt_reset()+delay(1) throttle with simply not
  196. // doing enough work in one call to need it.
  197. static constexpr uint32_t BYTE_BUDGET = 64;
  198. void step_write_plane_() {
  199. if (this->cursor_ == 0)
  200. this->write_byte_(this->desc_->plane_write_cmd[this->plane_], false);
  201. const uint8_t *buf = this->planes_[this->plane_];
  202. bool invert = this->desc_->plane_invert[this->plane_];
  203. uint32_t end = this->cursor_ + BYTE_BUDGET;
  204. if (end > this->plane_bytes_)
  205. end = this->plane_bytes_;
  206. for (; this->cursor_ < end; this->cursor_++) {
  207. uint8_t b = buf != nullptr ? buf[this->cursor_] : 0xFF;
  208. this->write_byte_(invert ? static_cast<uint8_t>(~b) : b, true);
  209. }
  210. if (this->cursor_ >= this->plane_bytes_) {
  211. this->cursor_ = 0;
  212. this->plane_++;
  213. if (this->plane_ >= this->desc_->plane_count) {
  214. this->plane_ = 0;
  215. this->cursor_ = 0;
  216. this->cmd_phase_ = CmdPhase::BEFORE_DELAY;
  217. this->set_state_(State::REFRESH_CMD);
  218. }
  219. }
  220. }
  221. void start_delay_(uint16_t delay_ms, State return_to) {
  222. this->deadline_ = millis() + delay_ms;
  223. this->delay_return_state_ = return_to;
  224. this->set_state_(State::POST_DELAY);
  225. }
  226. void start_busy_wait_(State next) {
  227. this->busy_wait_next_ = next;
  228. this->busy_wait_deadline_ = millis() + this->desc_->busy_timeout_ms;
  229. this->set_state_(State::WAIT_BUSY);
  230. }
  231. void step_wait_busy_() {
  232. if (this->busy_ == nullptr) {
  233. this->set_state_(this->busy_wait_next_);
  234. return;
  235. }
  236. bool busy = this->busy_->digital_read() == this->desc_->busy_active_level;
  237. if (!busy) {
  238. this->set_state_(this->busy_wait_next_);
  239. return;
  240. }
  241. if (millis() >= this->busy_wait_deadline_) {
  242. ESP_LOGW("gxepd2", "Busy timeout waiting for panel");
  243. this->set_state_(this->busy_wait_next_);
  244. }
  245. }
  246. // Logs every state transition (only when the state actually changes -
  247. // WAIT_BUSY and POST_DELAY re-enter their own case on every step() call
  248. // while polling, which would otherwise spam this at loop() rate) so a
  249. // first hardware run can be checked against the vendor cycle it's meant
  250. // to replicate before this gets dialed back to WARN-only.
  251. void set_state_(State s) {
  252. if (s != this->state_)
  253. ESP_LOGD("gxepd2", "state: %s -> %s", state_name_(this->state_), state_name_(s));
  254. this->state_ = s;
  255. }
  256. static const char *state_name_(State s) {
  257. switch (s) {
  258. case State::IDLE:
  259. return "IDLE";
  260. case State::RESET_LOW:
  261. return "RESET_LOW";
  262. case State::RESET_LOW_WAIT:
  263. return "RESET_LOW_WAIT";
  264. case State::RESET_HIGH_WAIT:
  265. return "RESET_HIGH_WAIT";
  266. case State::RESET_SETTLE:
  267. return "RESET_SETTLE";
  268. case State::INIT_CMD:
  269. return "INIT_CMD";
  270. case State::SET_WINDOW:
  271. return "SET_WINDOW";
  272. case State::WRITE_PLANE:
  273. return "WRITE_PLANE";
  274. case State::REFRESH_CMD:
  275. return "REFRESH_CMD";
  276. case State::POST_DELAY:
  277. return "POST_DELAY";
  278. case State::WAIT_BUSY:
  279. return "WAIT_BUSY";
  280. case State::DONE:
  281. return "DONE";
  282. }
  283. return "?";
  284. }
  285. const EpdPanelDescriptor *desc_{nullptr};
  286. GPIOPin *cs_{nullptr};
  287. GPIOPin *dc_{nullptr};
  288. GPIOPin *rst_{nullptr};
  289. GPIOPin *busy_{nullptr};
  290. EpdSPIDevice *spi_{nullptr};
  291. State state_{State::IDLE};
  292. uint32_t cursor_{0};
  293. CmdPhase cmd_phase_{CmdPhase::BEFORE_DELAY};
  294. uint8_t plane_{0};
  295. const uint8_t *planes_[2]{nullptr, nullptr};
  296. uint32_t plane_bytes_{0};
  297. uint32_t deadline_{0};
  298. State delay_return_state_{State::DONE};
  299. uint32_t busy_wait_deadline_{0};
  300. State busy_wait_next_{State::DONE};
  301. };
  302. } // namespace esphome::gxepd2