ESPHome 2026.12.0-dev
Loading...
Searching...
No Matches
it8951.h
Go to the documentation of this file.
1#pragma once
2
3#include <cstddef>
4#include <utility>
5#include <vector>
6
11
12#include "it8951_defs.h"
13
14namespace esphome::it8951 {
15
16using namespace display;
17
18// Deepest observed: UPDATE_REFRESH (10 ops) + 5 CHECK_LUT_IDLE reschedules = 14; 32 for margin
19static constexpr size_t OP_QUEUE_SIZE = 32;
20
21// --- Op queue ---------------------------------------------------------------
22// Each Op is a single CS-asserted SPI transaction (or a tiny bookkeeping
23// step). The loop processes one Op per iteration after gating on HW_RDY, so
24// the natural ESPHome loop cadence (~8-16 ms) provides inter-op pacing
25// without any blocking waits.
26//
27// Compound Ops (READ_DEV_INFO, XFER_*, DPY_BUF_AREA, ENABLE_1BPP, ...) are
28// short self-contained methods that do all their SPI work inside a single
29// CS cycle (or a small handful of cycles) and complete well under 2ms, so
30// they don't break the no-blocking budget.
31//
32// Each write-type op is a SINGLE CS-asserted transaction. The loop-level
33// HW_RDY gate ensures the controller is ready before dispatching any op, so
34// no blocking waits are needed within write ops.
35//
36// Read ops are decomposed: the command/address that triggers data preparation
37// is sent as write ops (CMD, WRITE_W), then a separate read op runs only
38// after the loop confirms HW_RDY is back HIGH (data ready). No blocking.
39enum class OpType : uint8_t {
40 CMD, // single CS: CMD preamble + command word (a)
41 WRITE_W, // single CS: WRITE preamble + data word (a)
42 WRITE_REG, // single CS: WRITE preamble + addr(a) + value(b)
43 // (caller must enqueue CMD(TCON_REG_WR) before this)
44 READ_DEV_INFO, // single CS: READ preamble + dummy + read DevInfo struct
45 // (caller enqueues CMD(GET_DEV_INFO) first; loop HW_RDY gate
46 // ensures data is ready before this op runs)
47 READ_WORD, // single CS: READ preamble + dummy + read one 16-bit word
48 // into read_result_. Loop HW_RDY gate ensures data ready.
49 CHECK_LUT_IDLE, // checks read_result_; if non-zero, re-enqueues read sequence
50 SET_1BPP, // uses read_result_ to set UP1SR bit 2, enqueues writes
51 XFER_LISAR, // set image-buffer target address (2× reg write: 4 CS transactions)
52 XFER_AREA_CMD, // single CS: CMD preamble + TCON_LD_IMG_AREA
53 XFER_AREA_ARGS, // single CS: WRITE preamble + 5 area-parameter words
54 XFER_ROWS, // single CS: WRITE preamble + row pixel data (time-sliced)
55 XFER_AREA_END, // single CS: CMD preamble + TCON_LD_IMG_END
56 DPY_BUF_CMD, // single CS: CMD preamble + I80_CMD_DPY_BUF_AREA
57 DPY_BUF_ARGS, // single CS: WRITE preamble + 7 display-area words
58 GPIO_RESET_LOW, // drive RESET pin low
59 GPIO_RESET_HIGH, // drive RESET pin high
60 DELAY_MS, // park `delay_until_` for a few ms (no SPI)
61};
62
63struct Op {
65 uint16_t a{0};
66 uint16_t b{0};
67};
68
69// High-level controller phases. Each phase enqueues a sequence of Ops; when
70// the queue drains, advance_phase_() runs the next phase.
71// This separation keeps per-Op work tiny and predictable.
72enum class Phase : uint8_t {
73 IDLE,
74 // Initialisation
75 INIT_RESET, // reset pulse + wake controller + packed-write enable
76 INIT_DEV_INFO, // GET_DEV_INFO and validate
77 INIT_VCOM, // write configured VCOM
78 INIT_TEMP, // force temperature for waveform LUT selection
79 INIT_DONE, // allocate framebuffer; transition to IDLE
80 // Update flow
81 UPDATE_PREPARE, // do_update_, compute dirty region, decide 4bpp/1bpp
82 UPDATE_TRANSFER, // one LD_IMG_AREA, time-sliced row streaming, one LD_IMG_END
83 UPDATE_REFRESH, // wait LUT idle, optionally enable 1bpp, send DPY_BUF_AREA
84 UPDATE_SLEEP, // optional deep sleep
85 // Shutdown flow
86 SHUTDOWN_WAIT, // wait for the panel refresh to end, then put the controller to sleep
88};
89
90class IT8951Display : public Display,
91 public spi::SPIDevice<spi::BIT_ORDER_MSB_FIRST, spi::CLOCK_POLARITY_LOW, spi::CLOCK_PHASE_LEADING,
92 spi::DATA_RATE_2MHZ> {
93 public:
94 IT8951Display(const char *name, uint16_t width, uint16_t height) : name_(name), width_(width), height_(height) {
95 this->row_width_ = this->compute_row_width_();
96 this->buffer_length_ = static_cast<size_t>(this->row_width_) * static_cast<size_t>(height);
97 }
98
99 // --- Component lifecycle ---
100 void setup() override;
101 void loop() override;
102 void dump_config() override;
103 bool teardown() override;
104 void on_powerdown() override;
105 float get_setup_priority() const override { return setup_priority::PROCESSOR; }
106
107 // --- Config setters (called from generated code) ---
108 void set_reset_pin(GPIOPin *pin) { this->reset_pin_ = pin; }
109 void set_busy_pin(GPIOPin *pin) { this->busy_pin_ = pin; }
110 void set_enable_pins(std::vector<GPIOPin *> pins) { this->enable_pins_ = std::move(pins); }
112 void set_full_update_every(uint8_t n) {
113 this->full_update_every_ = n;
114 // Seed the counter so the very first update trips the full-update branch in
115 // prepare_update_region_, giving a freshly-booted panel a clean GC16 refresh
116 // before any partial (fast-waveform) updates begin.
117 this->partial_update_count_ = n;
118 }
119 void set_invert_colors(bool invert_colors) { this->invert_colors_ = invert_colors; }
120 void set_sleep_when_done(bool s) { this->sleep_when_done_ = s; }
121 void set_vcom(uint16_t vcom_mv) { this->vcom_ = vcom_mv; }
122 void set_vcom_register(uint16_t selector) { this->vcom_register_ = selector; }
123 void set_force_temperature(int16_t celsius) {
124 this->force_temperature_ = celsius;
125 this->force_temperature_set_ = true;
126 }
127 void set_use_legacy_dpy_area(bool use) { this->use_legacy_dpy_area_ = use; }
128 // Pixel format: true = 4bpp grayscale framebuffer, false = packed 1bpp
129 // monochrome framebuffer. Chosen at config time; the framebuffer is stored
130 // in this native format and every update uses the matching transfer path.
131 void set_grayscale(bool g) { this->grayscale_ = g; }
132 // Monochrome only: ordered-dither pale colours (true) vs a hard 50% threshold.
133 void set_dithering(bool d) { this->dithering_ = d; }
134 void set_update_mode(uint16_t m) { this->default_update_mode_ = static_cast<UpdateMode>(m); }
135 void set_transform(uint8_t t) {
136 this->transform_ = t;
138 }
139 void set_rotation(DisplayRotation rotation) override {
140 Display::set_rotation(rotation);
142 }
143
144 // --- Display API ---
145 void update() override;
148 void fill(Color color) override;
149 void clear() override { this->fill(Color::WHITE); }
150 void draw_pixel_at(int x, int y, Color color) override;
151 int get_width() override { return (this->effective_transform_ & TRANSFORM_SWAP_XY) ? this->height_ : this->width_; }
152 int get_height() override { return (this->effective_transform_ & TRANSFORM_SWAP_XY) ? this->width_ : this->height_; }
153 bool pixel_axes_swapped() const override { return this->effective_transform_ & TRANSFORM_SWAP_XY; }
154
155 protected:
156 // Bulk pixel blit (used by LVGL and image rendering). Overridden to write
157 // straight into the framebuffer, avoiding the base class's per-pixel
158 // draw_pixel_at overhead (watchdog feed, clipping test, dirty-box clamps).
159 void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness,
160 bool big_endian, int x_offset, int y_offset, int x_pad) override;
161
162 int get_height_internal() override { return this->height_; }
163 int get_width_internal() override { return this->width_; }
164
165 // --- Coord transform / dirty region ---
167 // Map display (logical) coordinates to native framebuffer coordinates by
168 // applying effective_transform_ (swap/mirror). Shared by rotate_coordinates_
169 // and the bulk draw_pixels_at path.
170 void apply_transform_(int &x, int &y) const;
171 bool rotate_coordinates_(int &x, int &y);
172 void reset_dirty_region_();
173
174 // --- Framebuffer geometry / monochrome packing ---
175 // Bytes per row for the configured pixel format: 4bpp grayscale packs two
176 // pixels per byte; monochrome packs eight bits per byte, rounded up to a
177 // whole 16-pixel group (matching the controller's 8bpp-load / 1bpp trick).
178 uint16_t compute_row_width_() const {
179 return this->grayscale_ ? static_cast<uint16_t>((static_cast<uint32_t>(this->width_) + 1) / 2)
180 : static_cast<uint16_t>(((static_cast<uint32_t>(this->width_) + 15) / 16) * 2);
181 }
182 void set_mono_pixel_(uint16_t x, uint16_t y, bool value) const;
183 // Write a 4bpp grayscale nibble into the framebuffer (two pixels per byte).
184 void set_gray_pixel_(uint16_t x, uint16_t y, uint8_t nibble) const;
185 // Convert a color and write it at native framebuffer coordinates: a 4bpp
186 // nibble in grayscale mode, or an ordered-dithered bit in monochrome mode.
187 void write_pixel_native_(uint16_t x, uint16_t y, const Color &color) const;
188
189 // --- Op queue / loop machinery ---
190 void enqueue_(OpType type, uint16_t a = 0, uint16_t b = 0);
191 void prepend_(OpType type, uint16_t a = 0, uint16_t b = 0);
192 bool is_busy_() const;
193 void run_next_op_();
194 void process_op_(const Op &op);
195 void advance_phase_();
196 void set_phase_(Phase next);
198
199 // --- SPI primitives (each is one CS-asserted burst, fully non-blocking) ---
200 void spi_cmd_(uint16_t cmd);
201 void spi_write_word_(uint16_t value);
202 void spi_write_reg_(uint16_t addr, uint16_t value);
203 void spi_write_args_(const uint16_t *args, uint16_t count);
204 uint16_t spi_read_word_(); // non-blocking: HW_RDY confirmed by loop gate
205 void spi_read_dev_info_(); // non-blocking: HW_RDY confirmed by loop gate
206
207 // --- Compound Ops (small bounded helpers) ---
208 void op_xfer_lisar_();
209 void op_xfer_area_args_();
210 void op_xfer_area_end_();
211 bool op_xfer_rows_(); // returns true when current update area fully sent
212 void op_dpy_buf_args_();
213 void op_check_lut_idle_();
214 void op_set_1bpp_();
215
216 // --- Phase enqueuers ---
217 void enqueue_init_reset_();
219 void enqueue_init_vcom_();
220 void enqueue_init_temp_();
225
227
228 // --- Recovery ---
229 void recover_();
230
231 // --- State ---
232 static constexpr uint32_t BUSY_TIMEOUT_MS = 5000;
233
238 // Requests a continuous (non-throttled) main loop while streaming image data
239 // so 20ms transfer slices aren't separated by the ~16ms default loop interval.
241
242 // Pending update bookkeeping
243 bool update_pending_{false};
246 uint16_t area_x_{0}, area_y_{0}, area_w_{0}, area_h_{0};
247 uint16_t transfer_row_{0};
248 bool initialised_{false};
249 // True once TCON_SLEEP has been sent and the controller has not been woken
250 // since. The next update must issue TCON_SYS_RUN before any SPI op.
251 bool asleep_{false};
254
255 // Read result storage for decomposed read-modify-write op sequences
256 uint16_t read_result_{0};
257
258 // Device info
260 uint16_t img_buf_addr_l_{0};
261 uint16_t img_buf_addr_h_{0};
262
263 // Configured properties
264 const char *name_;
265 uint16_t width_;
266 uint16_t height_;
267 uint16_t row_width_;
269 uint8_t *buffer_{};
270 uint8_t transform_{0};
274 uint16_t vcom_{2300};
275 uint16_t vcom_register_{I80_CMD_VCOM_WRITE};
276 int16_t force_temperature_{DEFAULT_FORCE_TEMP_C};
279 bool invert_colors_{false};
280 bool sleep_when_done_{false};
281 // Pixel format selector (see set_grayscale): true = 4bpp grayscale,
282 // false = packed 1bpp monochrome.
283 bool grayscale_{true};
284 // Monochrome dithering (see set_dithering): true = ordered dither.
285 bool dithering_{true};
289 // GPIOs driven high during setup to power on the panel (empty if unused).
290 std::vector<GPIOPin *> enable_pins_;
291
292 // Dirty region (pixel coordinates of bounding box of changes since last update)
293 uint16_t x_low_{0}, y_low_{0}, x_high_{0}, y_high_{0};
294
295 // Saved data rate so we can probe slow then run fast
297
298 // Consecutive recovery attempts; used to give up rather than infinite-loop
299 // when the controller is unresponsive (e.g. wiring issue).
301
302 // DevInfo read retry counter (controller often returns garbage on the first
303 // read after reset; the original driver retried up to 3 times with 100ms
304 // between attempts).
306};
307
308} // namespace esphome::it8951
BedjetMode mode
BedJet operating mode.
uint8_t m
Definition bl0906.h:1
Helper class to request loop() to be called as fast as possible.
Definition helpers.h:2601
Fixed-size circular buffer with FIFO semantics and iteration support.
Definition helpers.h:468
int int int const uint8_t ColorOrder ColorBitness bitness
Definition display.h:394
int int int const uint8_t ColorOrder ColorBitness bool int int y_offset
Definition display.h:394
int int int const uint8_t ColorOrder ColorBitness bool int int int x_pad
Definition display.h:394
int int int const uint8_t * ptr
Definition display.h:393
virtual void set_rotation(DisplayRotation rotation)
Internal method to set the display rotation with.
Definition display.h:748
int int int const uint8_t ColorOrder ColorBitness bool big_endian
Definition display.h:394
int int int const uint8_t ColorOrder ColorBitness bool int x_offset
Definition display.h:394
int get_height() override
Definition it8951.h:152
void on_powerdown() override
Definition it8951.cpp:365
void update_mode(UpdateMode mode)
Definition it8951.cpp:802
float get_setup_priority() const override
Definition it8951.h:105
void start_update_(UpdateMode mode)
Definition it8951.cpp:777
void spi_write_args_(const uint16_t *args, uint16_t count)
Definition it8951.cpp:535
bool pixel_axes_swapped() const override
Definition it8951.h:153
HighFrequencyLoopRequester high_freq_
Definition it8951.h:240
void set_invert_colors(bool invert_colors)
Definition it8951.h:119
void set_transform(uint8_t t)
Definition it8951.h:135
void set_phase_(Phase next)
Definition it8951.cpp:173
void spi_cmd_(uint16_t cmd)
Definition it8951.cpp:508
bool rotate_coordinates_(int &x, int &y)
Definition it8951.cpp:880
void enqueue_(OpType type, uint16_t a=0, uint16_t b=0)
Definition it8951.cpp:21
void set_reset_pin(GPIOPin *pin)
Definition it8951.h:108
IT8951Display(const char *name, uint16_t width, uint16_t height)
Definition it8951.h:94
StaticRingBuffer< Op, OP_QUEUE_SIZE > queue_
Definition it8951.h:234
uint16_t compute_row_width_() const
Definition it8951.h:178
void spi_write_reg_(uint16_t addr, uint16_t value)
Definition it8951.cpp:524
void set_force_temperature(int16_t celsius)
Definition it8951.h:123
void prepend_(OpType type, uint16_t a=0, uint16_t b=0)
Definition it8951.cpp:28
void set_update_mode(uint16_t m)
Definition it8951.h:134
std::vector< GPIOPin * > enable_pins_
Definition it8951.h:290
int get_height_internal() override
Definition it8951.h:162
void draw_pixels_internal(int x_start, int y_start, int w, int h, const uint8_t *ptr, ColorBitness bitness, bool big_endian, int x_offset, int y_offset, int x_pad) override
Definition it8951.cpp:979
void set_mono_pixel_(uint16_t x, uint16_t y, bool value) const
Definition it8951.cpp:1043
void set_busy_pin(GPIOPin *pin)
Definition it8951.h:109
void set_enable_pins(std::vector< GPIOPin * > pins)
Definition it8951.h:110
void set_reset_duration(uint32_t ms)
Definition it8951.h:111
void write_pixel_native_(uint16_t x, uint16_t y, const Color &color) const
Definition it8951.cpp:959
void set_rotation(DisplayRotation rotation) override
Definition it8951.h:139
void apply_transform_(int &x, int &y) const
Definition it8951.cpp:871
void process_op_(const Op &op)
Definition it8951.cpp:100
void fill(Color color) override
Definition it8951.cpp:927
void spi_write_word_(uint16_t value)
Definition it8951.cpp:516
static constexpr uint32_t BUSY_TIMEOUT_MS
Definition it8951.h:232
void set_vcom(uint16_t vcom_mv)
Definition it8951.h:121
void set_full_update_every(uint8_t n)
Definition it8951.h:112
void set_sleep_when_done(bool s)
Definition it8951.h:120
void set_use_legacy_dpy_area(bool use)
Definition it8951.h:127
bool prepare_update_region_(UpdateMode &mode)
Definition it8951.cpp:700
DisplayType get_display_type() override
Definition it8951.h:147
void set_gray_pixel_(uint16_t x, uint16_t y, uint8_t nibble) const
Definition it8951.cpp:1062
void set_vcom_register(uint16_t selector)
Definition it8951.h:122
int get_width_internal() override
Definition it8951.h:163
void draw_pixel_at(int x, int y, Color color) override
Definition it8951.cpp:950
The SPIDevice is what components using the SPI will create.
Definition spi.h:445
uint8_t type
NameCopy * next
unsigned count
Definition gif.cpp:51
static void int x_start
Definition display.cpp:61
constexpr float PROCESSOR
For components that use data from sensors like displays.
Definition component.h:48
uint32_t value
Definition hal.cpp:59
static void uint32_t
static const Color WHITE
Definition color.h:168
uint16_t x
Definition tt21100.cpp:5
uint16_t y
Definition tt21100.cpp:6