cycle_engine.h 13 KB

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