Skip to main content

esp_hal/lcd_cam/lcd/
mod.rs

1//! LCD
2//!
3//! ## Overview
4//! The LCD module is designed to send parallel video data signals, and its bus
5//! supports RGB, MOTO6800, and I8080 interface timing.
6//!
7//! For more information on these modes, please refer to the documentation in
8//! their respective modules.
9
10use 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    /// The `LCD_CAM` peripheral reference for managing the LCD functionality.
29    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
44/// Represents an LCD interface.
45pub struct Lcd<'d, Dm: DriverMode> {
46    pub(super) inner: Inner<'d>,
47
48    /// A marker for the mode of operation (blocking or asynchronous).
49    pub(super) _mode: PhantomData<Dm>,
50}
51
52struct ClockConfig {
53    /// Specifies the clock mode, including polarity and phase settings.
54    pub clock_mode: ClockMode,
55
56    /// The frequency of the pixel clock.
57    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            // Force enable the clock for all configuration registers.
76            w.clk_en().set_bit();
77            w.lcd_clk_equ_sysclk().clear_bit();
78            w.lcd_clkcnt_n().bits(2 - 1); // Must not be 0.
79            w.lcd_ck_idle_edge()
80                .bit(config.clock_mode.polarity == Polarity::IdleHigh);
81            w.lcd_ck_out_edge()
82                .bit(config.clock_mode.phase == Phase::ShiftHigh)
83        });
84        ClockTree::with(|clocks| {
85            LcdCamInstance::LcdCam.configure_lcd_clock(clocks, clock_config);
86            if !self.inner.clock_requested {
87                LcdCamInstance::LcdCam.request_lcd_clock(clocks);
88                self.inner.clock_requested = true;
89            }
90        });
91
92        Ok(())
93    }
94
95    pub(super) fn into_async(self) -> Lcd<'d, Async> {
96        Lcd {
97            inner: self.inner,
98            _mode: PhantomData,
99        }
100    }
101}
102
103impl<'d> Lcd<'d, Async> {
104    pub(super) fn into_blocking(self) -> Lcd<'d, Blocking> {
105        Lcd {
106            inner: self.inner,
107            _mode: PhantomData,
108        }
109    }
110}
111
112#[derive(Debug, Clone, Copy, PartialEq, Default)]
113#[cfg_attr(feature = "defmt", derive(defmt::Format))]
114/// Represents the clock mode configuration for the LCD interface.
115pub struct ClockMode {
116    /// The polarity of the clock signal (idle high or low).
117    pub polarity: Polarity,
118
119    /// The phase of the clock signal (shift on the rising or falling edge).
120    pub phase: Phase,
121}
122
123#[derive(Debug, Clone, Copy, PartialEq, Default)]
124#[cfg_attr(feature = "defmt", derive(defmt::Format))]
125/// Represents the polarity of the clock signal for the LCD interface.
126pub enum Polarity {
127    /// The clock signal is low when idle.
128    #[default]
129    IdleLow,
130
131    /// The clock signal is high when idle.
132    IdleHigh,
133}
134
135#[derive(Debug, Clone, Copy, PartialEq, Default)]
136#[cfg_attr(feature = "defmt", derive(defmt::Format))]
137/// Represents the phase of the clock signal for the LCD interface.
138pub enum Phase {
139    /// Data is shifted on the low (falling) edge of the clock signal.
140    #[default]
141    ShiftLow,
142
143    /// Data is shifted on the high (rising) edge of the clock signal.
144    ShiftHigh,
145}
146
147#[derive(Debug, Clone, Copy, PartialEq, Default)]
148#[cfg_attr(feature = "defmt", derive(defmt::Format))]
149/// Represents the delay mode for the LCD signal output.
150pub enum DelayMode {
151    /// Output without delay.
152    #[default]
153    None        = 0,
154    /// Delayed by the rising edge of LCD_CLK.
155    RaisingEdge = 1,
156    /// Delayed by the falling edge of LCD_CLK.
157    FallingEdge = 2,
158}