esp_hal/lcd_cam/lcd/
mod.rs1use core::marker::PhantomData;
11
12use super::GenericPeripheralGuard;
13use crate::{
14 Async,
15 Blocking,
16 DriverMode,
17 clock::ll::{ClockTree, LcdCamInstance, LcdCamLcdClockConfig},
18 lcd_cam::{ClockError, calculate_clkm},
19 peripherals::LCD_CAM,
20 system,
21 time::Rate,
22};
23
24pub mod dpi;
25pub mod i8080;
26
27pub(super) struct Inner<'d> {
28 pub lcd_cam: LCD_CAM<'d>,
30
31 pub _guard: GenericPeripheralGuard<{ system::Peripheral::LcdCam as u8 }>,
32
33 pub clock_requested: bool,
34}
35
36impl<'a> Drop for Inner<'a> {
37 fn drop(&mut self) {
38 if self.clock_requested {
39 ClockTree::with(|clocks| LcdCamInstance::LcdCam.release_lcd_clock(clocks));
40 }
41 }
42}
43
44pub struct Lcd<'d, Dm: DriverMode> {
46 pub(super) inner: Inner<'d>,
47
48 pub(super) _mode: PhantomData<Dm>,
50}
51
52struct ClockConfig {
53 pub clock_mode: ClockMode,
55
56 pub frequency: Rate,
58}
59
60impl<'d, Dm: DriverMode> Lcd<'d, Dm> {
61 fn regs(&self) -> &crate::pac::lcd_cam::RegisterBlock {
62 self.inner.lcd_cam.register_block()
63 }
64
65 fn configure_clocks(&mut self, config: &ClockConfig) -> Result<(), ClockError> {
66 let sources = property!("clock_tree.lcd_cam.lcd_clock.sclk");
67 let (i, divider) = calculate_clkm(
68 config.frequency.as_hz(),
69 &sources.map(LcdCamInstance::lcd_clock_source_frequency),
70 )?;
71 let clock_config =
72 LcdCamLcdClockConfig::new(sources[i], divider.div_num, divider.div_a, divider.div_b);
73
74 self.regs().lcd_clock().write(|w| unsafe {
75 w.clk_en().set_bit();
77 if cfg!(esp32s3) {
78 w.lcd_clk_equ_sysclk().clear_bit();
80 w.lcd_clkcnt_n().bits(2 - 1); } else {
82 w.lcd_clk_equ_sysclk().set_bit();
84 }
85 w.lcd_ck_idle_edge()
86 .bit(config.clock_mode.polarity == Polarity::IdleHigh);
87 w.lcd_ck_out_edge()
88 .bit(config.clock_mode.phase == Phase::ShiftHigh)
89 });
90 ClockTree::with(|clocks| {
91 LcdCamInstance::LcdCam.configure_lcd_clock(clocks, clock_config);
92 if !self.inner.clock_requested {
93 LcdCamInstance::LcdCam.request_lcd_clock(clocks);
94 self.inner.clock_requested = true;
95 }
96 });
97
98 Ok(())
99 }
100
101 pub(super) fn into_async(self) -> Lcd<'d, Async> {
102 Lcd {
103 inner: self.inner,
104 _mode: PhantomData,
105 }
106 }
107}
108
109impl<'d> Lcd<'d, Async> {
110 pub(super) fn into_blocking(self) -> Lcd<'d, Blocking> {
111 Lcd {
112 inner: self.inner,
113 _mode: PhantomData,
114 }
115 }
116}
117
118#[derive(Debug, Clone, Copy, PartialEq, Default)]
119#[cfg_attr(feature = "defmt", derive(defmt::Format))]
120pub struct ClockMode {
122 pub polarity: Polarity,
124
125 pub phase: Phase,
127}
128
129#[derive(Debug, Clone, Copy, PartialEq, Default)]
130#[cfg_attr(feature = "defmt", derive(defmt::Format))]
131pub enum Polarity {
133 #[default]
135 IdleLow,
136
137 IdleHigh,
139}
140
141#[derive(Debug, Clone, Copy, PartialEq, Default)]
142#[cfg_attr(feature = "defmt", derive(defmt::Format))]
143pub enum Phase {
145 #[default]
147 ShiftLow,
148
149 ShiftHigh,
151}
152
153#[derive(Debug, Clone, Copy, PartialEq, Default)]
154#[cfg_attr(feature = "defmt", derive(defmt::Format))]
155pub enum DelayMode {
157 #[default]
159 None = 0,
160 RaisingEdge = 1,
162 FallingEdge = 2,
164}