1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
//! # Chipone CO5300 AMOLED Display Controller Driver
//!
//! A `no_std` driver for the CO5300, following the CO5300 datasheet V0.01 (Chipone, 24 July
//! 2023). The driver speaks commands; how they reach the chip is up to an [`Interface`]
//! implementation, so the same driver runs over:
//!
//! - 4-wire SPI with a D/C pin: [`SpiInterface`], on `embedded-hal` [`SpiDevice`] and
//! [`OutputPin`];
//! - single-line SPI with the datasheet's instruction/address framing:
//! [`FramedSpiInterface`];
//! - Quad-SPI, through your HAL's half-duplex transfer and the [`qspi`] framing helper;
//! - a MIPI-DSI panel IO, such as ESP-IDF's `esp_lcd_panel_io_tx_param`/`tx_color`
//! (see [below](#mipi-dsi-esp-idf-panel-io)).
//!
//! Enable the `async` feature for an async driver ([`asynch::Co5300`]) on [`AsyncInterface`],
//! and the `embedded-graphics` feature for a [`DrawTarget`](embedded_graphics_core::draw_target::DrawTarget)
//! that writes straight to the chip's frame memory, without a frame buffer in the host.
//!
//! ```
//! # use core::cell::RefCell;
//! # use co5300_sim::{Co5300Model, FakeDc, FakeDelay, FakeSpi};
//! use co5300::{Co5300, Config, SpiInterface};
//!
//! # let model = RefCell::new(Co5300Model::new(410, 502));
//! # let (spi, dc, mut delay) = (FakeSpi::new(&model), FakeDc::new(&model), FakeDelay::new(&model));
//! let iface = SpiInterface::new(spi, dc);
//! let mut display = Co5300::new(iface, Config::new(410, 502));
//!
//! // After a hardware reset (`co5300::reset`): sleep out, RGB565, full brightness, display on.
//! display.init(&mut delay, &[])?;
//!
//! // Fill the top-left 4 x 2 pixels with red (RGB565, MSB first).
//! display.set_window(0, 0, 4, 2)?;
//! display.write_pixels(&[0xF8, 0x00].repeat(8))?;
//! # assert_eq!(model.borrow().pixel(3, 1), 0xFF0000);
//! # Ok::<(), co5300::Error<co5300::SpiError<embedded_hal::spi::ErrorKind, core::convert::Infallible>>>(())
//! ```
//!
//! ## Panel initialization
//!
//! The CO5300 drives many different AMOLED panels; their power, gamma and timing settings
//! live in the manufacturer command pages and come from the panel vendor, not from the chip
//! datasheet. [`Co5300::init`] therefore only sends the chip-level commands the datasheet
//! requires and accepts a panel-specific command list, sent first:
//!
//! ```rust,ignore
//! const PANEL: &[PanelCommand] = &[
//! (0xFE, &[0x20]), // vendor page, unlock, settings ...
//! (0xFE, &[0x00]),
//! ];
//! display.init(&mut delay, PANEL)?;
//! ```
//!
//! [`panel::ESPRESSIF_DEFAULT`] holds the default sequence of Espressif's `esp_lcd_co5300`
//! component.
//!
//! ## Window alignment
//!
//! `CASET` and `RASET` require an even start and an even length (datasheet sections 7.5.21
//! and 7.5.22). [`Co5300::set_window`] rejects other windows with [`Error::InvalidWindow`];
//! the `embedded-graphics` target widens them, see [its documentation](crate::graphics).
//!
//! ## MIPI-DSI: ESP-IDF panel IO
//!
//! On a board where the CO5300 hangs off MIPI-DSI (such as the Kode Dot below), the ESP-IDF
//! DSI bus stays in charge; an adapter in the board crate forwards the two calls:
//!
//! ```rust,ignore
//! struct PanelIo(esp_lcd_panel_io_handle_t);
//!
//! impl co5300::Interface for PanelIo {
//! type Error = EspError;
//! // MIPI-DSI: no SetSPIMode (C4h) in `init`.
//! const SPI_RAM_WRITE: bool = false;
//!
//! fn write_command(&mut self, command: u8, params: &[u8]) -> Result<(), EspError> {
//! esp!(unsafe {
//! esp_lcd_panel_io_tx_param(self.0, command.into(), params.as_ptr().cast(), params.len())
//! })
//! }
//!
//! fn write_pixels(&mut self, command: u8, data: &[u8]) -> Result<(), EspError> {
//! esp!(unsafe {
//! esp_lcd_panel_io_tx_color(self.0, command.into(), data.as_ptr().cast(), data.len())
//! })
//! }
//! }
//! ```
//!
//! When the pixels come from a DPI frame buffer (video mode), only the command path is used.
//!
//! ## Board: Kode Dot
//!
//! The Kode Dot (ESP32-P4) drives the CO5300 over MIPI-DSI; ESP-IDF owns the DSI bus, the
//! DPI interface and the frame buffer. The panel is 410 × 502 pixels (`KODE_DOT` in the
//! handheld screen constants), so use `Config::new(410, 502)`. Before `init`:
//!
//! 1. The panel supply `VCI_EN` comes from TCA6408A pin P7: latch P7 high, then make it an
//! output, then wait 20 ms.
//! 2. Reset is shared with the touch controller on GPIO7: low for 10 ms, high, then wait
//! 120 ms ([`reset`] does exactly this).
//! 3. The DSI PHY LDO runs at 2.5 V.
//!
//! The datasheet lists resolutions up to 480 lines (and 192 × 960); 502 lines is the
//! board's value, not one of the datasheet's.
//!
//! [`SpiDevice`]: embedded_hal::spi::SpiDevice
//! [`OutputPin`]: embedded_hal::digital::OutputPin
// Enables the `doc_cfg` feature when the `docsrs` configuration attribute is defined.
pub use AsyncInterface;
pub use ;
pub use ;
pub use co5300_registers as registers;
pub use ;
/// A panel-specific command: the command code and its parameter bytes.
pub type PanelCommand<'a> = ;
/// How long [`reset`] holds `RESX` low, in milliseconds.
///
/// The datasheet requires at least 10 µs (`tRESW`, section 6.6) and more than 3 ms to leave
/// deep standby (section 7.5.37); 10 ms covers both.
pub const RESET_LOW_MS: u32 = 10;
/// How long [`reset`] waits after releasing `RESX`, in milliseconds.
///
/// The reset completes within 120 ms when applied in Sleep Out mode (`tRESET`), and
/// `SLPOUT` may not be sent earlier (section 6.6, note 5).
pub const RESET_WAIT_MS: u32 = 120;
/// The wait after `SLPIN` or `SLPOUT` before the next command, in milliseconds
/// (sections 7.5.11 and 7.5.12).
pub const SLEEP_WAIT_MS: u32 = 5;
/// The wait between `SLPOUT` and `DISPON`, in milliseconds (`T4`, section 5.6.2).
pub const SLEEP_OUT_TO_DISPLAY_ON_MS: u32 = 60;
/// The wait after `SWRESET` before `SLPOUT` when reset in Sleep Out mode, in milliseconds
/// (section 7.5.2). Other commands may follow after 5 ms.
pub const SOFTWARE_RESET_WAIT_MS: u32 = 120;
/// A driver error.
/// The tearing effect output mode (`TEON`, 35h).
/// Display geometry and the settings [`Co5300::init`] applies.
/// The blocking driver.