Skip to content

Commit 73bcdd1

Browse files
committed
fail
1 parent 298401d commit 73bcdd1

File tree

1 file changed

+180
-44
lines changed

1 file changed

+180
-44
lines changed

examples/src/bin/lcd_dpi.rs

Lines changed: 180 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -29,24 +29,20 @@
2929
#![no_std]
3030
#![no_main]
3131

32-
use core::iter::empty;
32+
use core::iter::{empty, once};
3333

3434
use esp_backtrace as _;
35-
use esp_hal::{
36-
dma::{Dma, DmaPriority},
37-
dma_loop_buffer,
38-
gpio::{Io, Level},
39-
lcd_cam::{
40-
lcd::{
41-
dpi::{Config, Dpi, Format, FrameTiming},
42-
ClockMode,
43-
Phase,
44-
Polarity,
45-
},
46-
LcdCam,
35+
use esp_hal::{dma::{Dma, DmaPriority}, dma_loop_buffer, gpio::{Io, Level}, i2c, lcd_cam::{
36+
lcd::{
37+
dpi::{Config, Dpi, Format, FrameTiming},
38+
ClockMode,
39+
Phase,
40+
Polarity,
4741
},
48-
prelude::*,
49-
};
42+
LcdCam,
43+
}, prelude::*, Blocking};
44+
use esp_hal::delay::Delay;
45+
use esp_hal::i2c::I2c;
5046
use esp_println::println;
5147

5248
#[entry]
@@ -57,10 +53,61 @@ fn main() -> ! {
5753

5854
let io = Io::new(peripherals.GPIO, peripherals.IO_MUX);
5955

56+
let i2c = I2c::new(peripherals.I2C0, io.pins.gpio47, io.pins.gpio48, 100.kHz());
6057
let dma = Dma::new(peripherals.DMA);
6158
let channel = dma.channel2.configure(true, DmaPriority::Priority0);
6259
let lcd_cam = LcdCam::new(peripherals.LCD_CAM);
6360

61+
let mut expander = Tca9554::new(i2c);
62+
expander.write_output_reg(0b1111_0011).unwrap();
63+
expander.write_direction_reg(0b1111_0001).unwrap();
64+
65+
let delay = Delay::new();
66+
67+
println!("Ughh....");
68+
69+
let mut write_byte = |b: u8, is_cmd: bool| {
70+
// const fn compose(cs: bool, scl: bool, sda: bool) -> u8 {
71+
// let mut base = 0b1111_0001;
72+
// if cs {
73+
// base |= 0b0000_0010;
74+
// }
75+
// if scl {
76+
// base |= 0b0000_0100;
77+
// }
78+
// if sda {
79+
// base |= 0b0000_1000;
80+
// }
81+
// base
82+
// }
83+
84+
expander.write_output_reg(0b1111_0101).unwrap();
85+
for bit in once(!is_cmd).chain((0..8).map(|i| (b >> i) & 0b1 != 0).rev()) {
86+
let sda = if bit { 0b0000_1000 } else { 0b0000_0000 };
87+
// Set SDA
88+
expander.write_output_reg(0b1111_0101 | sda).unwrap();
89+
90+
// Toggle SCL
91+
expander.write_output_reg(0b1111_0001 | sda).unwrap();
92+
expander.write_output_reg(0b1111_0101 | sda).unwrap();
93+
}
94+
expander.write_output_reg(0b1111_0111).unwrap();
95+
};
96+
97+
for &init in INIT_CMDS.iter() {
98+
match init {
99+
InitCmd::Cmd(cmd, args) => {
100+
write_byte(cmd, true);
101+
for &arg in args {
102+
write_byte(arg, false);
103+
}
104+
}
105+
InitCmd::Delay(ms) => {
106+
delay.delay_millis(ms as _);
107+
}
108+
}
109+
}
110+
64111
let mut dma_buf = dma_loop_buffer!(2 * 16);
65112

66113
let config = Config {
@@ -74,54 +121,52 @@ fn main() -> ! {
74121
},
75122
// https://www.makerfabs.com/desfile/files/QT4300H40R10-V03-Spec.pdf
76123
timing: FrameTiming {
77-
horizontal_active_width: 800,
78-
horizontal_total_width: 928, // 889..1143
79-
horizontal_blank_front_porch: 40, // 1..255
124+
horizontal_active_width: 480,
125+
horizontal_total_width: 608,
126+
horizontal_blank_front_porch: 40,
80127

81128
vertical_active_height: 480,
82-
vertical_total_height: 525, // 513..767
83-
vertical_blank_front_porch: 13, // 1..255
129+
vertical_total_height: 525,
130+
vertical_blank_front_porch: 13,
84131

85132
hsync_width: 48, // 1..255
86133
vsync_width: 3, // 3..255
87134

88135
hsync_position: 0,
89136
},
90-
vsync_idle_level: Level::High,
91-
hsync_idle_level: Level::High,
92-
de_idle_level: Level::Low,
137+
vsync_idle_level: Level::Low,
138+
hsync_idle_level: Level::Low,
139+
de_idle_level: Level::High,
93140
disable_black_region: false,
94141
..Default::default()
95142
};
96143

97-
// https://raw.githubusercontent.com/Makerfabs/ESP32-S3-Parallel-TFT-with-Touch-4.3inch/main/hardware/ESP32-S3%20Parallel%20TFT%20with%20Touch%204.3%E2%80%9C%20V3.1.PDF
98144
let mut dpi = Dpi::new(lcd_cam.lcd, channel.tx, 30.MHz(), config)
99145
.with_ctrl_pins(
100-
io.pins.gpio41,
101-
io.pins.gpio39,
102-
io.pins.gpio40,
103-
io.pins.gpio42,
104-
)
105-
.with_data_pins(
106-
// Blue
107-
io.pins.gpio8,
108146
io.pins.gpio3,
109147
io.pins.gpio46,
148+
io.pins.gpio17,
110149
io.pins.gpio9,
111-
io.pins.gpio1,
150+
)
151+
.with_data_pins(
152+
io.pins.gpio10,
153+
io.pins.gpio11,
154+
io.pins.gpio12,
155+
io.pins.gpio13,
156+
io.pins.gpio14,
112157
// Green
113-
io.pins.gpio5,
114-
io.pins.gpio6,
115-
io.pins.gpio7,
116-
io.pins.gpio15,
117-
io.pins.gpio16,
118-
io.pins.gpio4,
119-
// Red
120-
io.pins.gpio45,
121-
io.pins.gpio48,
122-
io.pins.gpio47,
123158
io.pins.gpio21,
124-
io.pins.gpio14,
159+
io.pins.gpio8,
160+
io.pins.gpio18,
161+
io.pins.gpio45,
162+
io.pins.gpio38,
163+
io.pins.gpio39,
164+
// Red
165+
io.pins.gpio40,
166+
io.pins.gpio41,
167+
io.pins.gpio42,
168+
io.pins.gpio2,
169+
io.pins.gpio1,
125170
);
126171

127172
const MAX_RED: u16 = (1 << 5) - 1;
@@ -160,3 +205,94 @@ fn main() -> ! {
160205
}
161206
}
162207
}
208+
209+
struct Tca9554 {
210+
i2c: I2c<'static, Blocking>,
211+
address: u8,
212+
}
213+
214+
impl Tca9554 {
215+
pub fn new(i2c: I2c<'static, Blocking>) -> Self {
216+
Self { i2c, address: 0x20 }
217+
}
218+
219+
pub fn write_direction_reg(&mut self, value: u8) -> Result<(), i2c::Error> {
220+
self.i2c.write(self.address, &[0x03, value])
221+
}
222+
223+
pub fn write_output_reg(&mut self, value: u8) -> Result<(), i2c::Error> {
224+
self.i2c.write(self.address, &[0x01, value])
225+
}
226+
}
227+
228+
#[derive(Copy, Clone)]
229+
enum InitCmd {
230+
Cmd(u8, &'static [u8]),
231+
Delay(u8)
232+
}
233+
234+
const INIT_CMDS: &[InitCmd] = &[
235+
InitCmd::Cmd(0xf0, &[0x55, 0xaa, 0x52, 0x08, 0x00]),
236+
InitCmd::Cmd(0xf6, &[0x5a, 0x87]),
237+
InitCmd::Cmd(0xc1, &[0x3f]),
238+
InitCmd::Cmd(0xc2, &[0x0e]),
239+
InitCmd::Cmd(0xc6, &[0xf8]),
240+
InitCmd::Cmd(0xc9, &[0x10]),
241+
InitCmd::Cmd(0xcd, &[0x25]),
242+
InitCmd::Cmd(0xf8, &[0x8a]),
243+
InitCmd::Cmd(0xac, &[0x45]),
244+
InitCmd::Cmd(0xa0, &[0xdd]),
245+
InitCmd::Cmd(0xa7, &[0x47]),
246+
InitCmd::Cmd(0xfa, &[0x00, 0x00, 0x00, 0x04]),
247+
InitCmd::Cmd(0x86, &[0x99, 0xa3, 0xa3, 0x51]),
248+
InitCmd::Cmd(0xa3, &[0xee]),
249+
InitCmd::Cmd(0xfd, &[0x3c, 0x3]),
250+
InitCmd::Cmd(0x71, &[0x48]),
251+
InitCmd::Cmd(0x72, &[0x48]),
252+
InitCmd::Cmd(0x73, &[0x00, 0x44]),
253+
InitCmd::Cmd(0x97, &[0xee]),
254+
InitCmd::Cmd(0x83, &[0x93]),
255+
InitCmd::Cmd(0x9a, &[0x72]),
256+
InitCmd::Cmd(0x9b, &[0x5a]),
257+
InitCmd::Cmd(0x82, &[0x2c, 0x2c]),
258+
InitCmd::Cmd(0x6d, &[0x00, 0x1f, 0x19, 0x1a, 0x10, 0x0e, 0x0c, 0x0a, 0x02, 0x07, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x1e, 0x08, 0x01, 0x09, 0x0b, 0x0d, 0x0f, 0x1a, 0x19, 0x1f, 0x00]),
259+
InitCmd::Cmd(0x64, &[0x38, 0x05, 0x01, 0xdb, 0x03, 0x03, 0x38, 0x04, 0x01, 0xdc, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a]),
260+
InitCmd::Cmd(0x65, &[0x38, 0x03, 0x01, 0xdd, 0x03, 0x03, 0x38, 0x02, 0x01, 0xde, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a]),
261+
InitCmd::Cmd(0x66, &[0x38, 0x01, 0x01, 0xdf, 0x03, 0x03, 0x38, 0x00, 0x01, 0xe0, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a]),
262+
InitCmd::Cmd(0x67, &[0x30, 0x01, 0x01, 0xe1, 0x03, 0x03, 0x30, 0x02, 0x01, 0xe2, 0x03, 0x03, 0x7a, 0x7a, 0x7a, 0x7a]),
263+
InitCmd::Cmd(0x68, &[0x00, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a, 0x08, 0x15, 0x08, 0x15, 0x7a, 0x7a]),
264+
InitCmd::Cmd(0x60, &[0x38, 0x08, 0x7a, 0x7a, 0x38, 0x09, 0x7a, 0x7a]),
265+
InitCmd::Cmd(0x63, &[0x31, 0xe4, 0x7a, 0x7a, 0x31, 0xe5, 0x7a, 0x7a]),
266+
InitCmd::Cmd(0x69, &[0x04, 0x22, 0x14, 0x22, 0x14, 0x22, 0x08]),
267+
InitCmd::Cmd(0x6b, &[0x07]),
268+
InitCmd::Cmd(0x7a, &[0x08, 0x13]),
269+
InitCmd::Cmd(0x7b, &[0x08, 0x13]),
270+
InitCmd::Cmd(0xd1, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
271+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
272+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
273+
0xff]),
274+
InitCmd::Cmd(0xd2, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
275+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
276+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
277+
0xff]),
278+
InitCmd::Cmd(0xd3, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
279+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
280+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
281+
0xff]),
282+
InitCmd::Cmd(0xd4, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
283+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
284+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
285+
0xff]),
286+
InitCmd::Cmd(0xd5, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
287+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
288+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
289+
0xff]),
290+
InitCmd::Cmd(0xd6, &[0x00, 0x00, 0x00, 0x04, 0x00, 0x12, 0x00, 0x18, 0x00, 0x21, 0x00, 0x2a, 0x00, 0x35, 0x00, 0x47, 0x00,
291+
0x56, 0x00, 0x90, 0x00, 0xe5, 0x01, 0x68, 0x01, 0xd5, 0x01, 0xd7, 0x02, 0x36, 0x02, 0xa6, 0x02, 0xee,
292+
0x03, 0x48, 0x03, 0xa0, 0x03, 0xba, 0x03, 0xc5, 0x03, 0xd0, 0x03, 0xe0, 0x03, 0xea, 0x03, 0xfa, 0x03,
293+
0xff]),
294+
InitCmd::Cmd(0x11, &[]),
295+
InitCmd::Delay(120),
296+
InitCmd::Cmd(0x29, &[]),
297+
InitCmd::Delay(20),
298+
];

0 commit comments

Comments
 (0)