use display_types::cea861::hdmi_forum::HdmiForumFrl;
pub use display_types::{ColorBitDepth, ColorFormat};
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[repr(u8)]
pub enum LtpPattern {
AllOnes = 1,
AllZeros = 2,
NyquistClock = 3,
DdeCompliance = 4,
Lfsr0 = 5,
Lfsr1 = 6,
Lfsr2 = 7,
Lfsr3 = 8,
}
impl LtpPattern {
pub const fn value(self) -> u8 {
self as u8
}
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct LanePatterns {
pub lane0: Option<LtpPattern>,
pub lane1: Option<LtpPattern>,
pub lane2: Option<LtpPattern>,
pub lane3: Option<LtpPattern>,
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum FrlOutput {
GapOnly,
Active,
}
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct TxFfeLevel(u8);
impl TxFfeLevel {
pub const MAX: Self = Self(7);
pub const fn new(level: u8) -> Option<Self> {
if level <= Self::MAX.0 {
Some(Self(level))
} else {
None
}
}
pub const fn value(self) -> u8 {
self.0
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct LaneEqParams {
pub tx_ffe_level: TxFfeLevel,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct TxFfeRestrictions {
pub pre_shoot_only: bool,
pub de_emphasis_only: bool,
pub no_ffe: bool,
}
impl TxFfeRestrictions {
pub const fn new(pre_shoot_only: bool, de_emphasis_only: bool, no_ffe: bool) -> Self {
Self {
pre_shoot_only,
de_emphasis_only,
no_ffe,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, Default, PartialEq, Eq, Hash)]
pub struct EqParams {
pub lane0: LaneEqParams,
pub lane1: LaneEqParams,
pub lane2: LaneEqParams,
pub lane3: Option<LaneEqParams>,
pub tx_ffe_restrictions: TxFfeRestrictions,
}
impl EqParams {
pub fn new() -> Self {
Self::default()
}
}
pub trait FrlPhy {
type Error: core::fmt::Debug;
fn set_frl_rate(&mut self, rate: HdmiForumFrl) -> Result<(), Self::Error>;
fn send_ltp(&mut self, patterns: LanePatterns) -> Result<(), Self::Error>;
fn set_frl_output(&mut self, output: FrlOutput) -> Result<(), Self::Error>;
fn adjust_equalization(&mut self, params: EqParams) -> Result<(), Self::Error>;
}
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub enum TmdsClockRatio {
Div10,
Div40,
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TmdsVideo {
pub pixel_clock_khz: u32,
pub color_depth: ColorBitDepth,
pub color_format: ColorFormat,
pub pixel_repetition: u8,
}
impl TmdsVideo {
pub const fn new(
pixel_clock_khz: u32,
color_depth: ColorBitDepth,
color_format: ColorFormat,
) -> Self {
Self {
pixel_clock_khz,
color_depth,
color_format,
pixel_repetition: 0,
}
}
pub const fn with_pixel_repetition(mut self, pixel_repetition: u8) -> Self {
self.pixel_repetition = pixel_repetition;
self
}
#[must_use]
pub const fn character_rate_khz(&self) -> Option<u32> {
let (num, den): (u64, u64) = match self.color_depth {
ColorBitDepth::Depth8 => (1, 1),
ColorBitDepth::Depth10 => (5, 4),
ColorBitDepth::Depth12 => (3, 2),
ColorBitDepth::Depth16 => (2, 1),
_ => return None,
};
let (num, den) = match self.color_format {
ColorFormat::Rgb444 | ColorFormat::YCbCr444 => (num, den),
ColorFormat::YCbCr420 => (num, den * 2),
ColorFormat::YCbCr422 if !matches!(self.color_depth, ColorBitDepth::Depth16) => (1, 1),
_ => return None,
};
let rate = (self.pixel_clock_khz as u64 * num).div_ceil(den);
if rate > u32::MAX as u64 {
None
} else {
Some(rate as u32)
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct TmdsPhyConfig {
pub character_rate_khz: u32,
pub clock_ratio: TmdsClockRatio,
pub video: TmdsVideo,
}
impl TmdsPhyConfig {
pub const fn new(
character_rate_khz: u32,
clock_ratio: TmdsClockRatio,
video: TmdsVideo,
) -> Self {
Self {
character_rate_khz,
clock_ratio,
video,
}
}
}
#[non_exhaustive]
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct TmdsPhyCapabilities {
pub max_character_rate_khz: u32,
pub scrambling: bool,
pub low_rate_scrambling: bool,
}
impl TmdsPhyCapabilities {
pub const fn new(max_character_rate_khz: u32) -> Self {
Self {
max_character_rate_khz,
scrambling: false,
low_rate_scrambling: false,
}
}
pub const fn with_scrambling(mut self, scrambling: bool) -> Self {
self.scrambling = scrambling;
self
}
pub const fn with_low_rate_scrambling(mut self, low_rate_scrambling: bool) -> Self {
self.low_rate_scrambling = low_rate_scrambling;
self
}
pub const fn scrambles_at_low_rate(&self) -> bool {
self.scrambling && self.low_rate_scrambling
}
}
pub trait TmdsPhy {
type Error: core::fmt::Debug;
fn capabilities(&self) -> TmdsPhyCapabilities;
fn configure(&mut self, config: TmdsPhyConfig) -> Result<(), Self::Error>;
fn set_scrambling(&mut self, enabled: bool) -> Result<(), Self::Error>;
fn start_output(&mut self) -> Result<(), Self::Error>;
fn stop_output(&mut self) -> Result<(), Self::Error>;
}
pub trait PhyPower {
type Error: core::fmt::Debug;
fn power_on(&mut self) -> Result<(), Self::Error>;
fn power_off(&mut self) -> Result<(), Self::Error>;
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn the_character_rate_follows_depth_and_format() {
let video = |clock, depth, format| TmdsVideo::new(clock, depth, format);
let rgb = ColorFormat::Rgb444;
assert_eq!(
video(594_000, ColorBitDepth::Depth8, rgb).character_rate_khz(),
Some(594_000)
);
assert_eq!(
video(297_000, ColorBitDepth::Depth10, rgb).character_rate_khz(),
Some(371_250)
);
assert_eq!(
video(297_000, ColorBitDepth::Depth12, rgb).character_rate_khz(),
Some(445_500)
);
assert_eq!(
video(297_000, ColorBitDepth::Depth16, rgb).character_rate_khz(),
Some(594_000)
);
let ycc = |format, depth| video(297_000, depth, format).character_rate_khz();
assert_eq!(
ycc(ColorFormat::YCbCr444, ColorBitDepth::Depth12),
Some(445_500)
);
assert_eq!(
ycc(ColorFormat::YCbCr422, ColorBitDepth::Depth8),
Some(297_000)
);
assert_eq!(
ycc(ColorFormat::YCbCr422, ColorBitDepth::Depth10),
Some(297_000)
);
assert_eq!(
ycc(ColorFormat::YCbCr422, ColorBitDepth::Depth12),
Some(297_000)
);
assert_eq!(ycc(ColorFormat::YCbCr422, ColorBitDepth::Depth16), None);
let yuv420 = ColorFormat::YCbCr420;
assert_eq!(
video(594_000, ColorBitDepth::Depth8, yuv420).character_rate_khz(),
Some(297_000)
);
assert_eq!(
video(594_000, ColorBitDepth::Depth10, yuv420).character_rate_khz(),
Some(371_250)
);
assert_eq!(
video(217_601, ColorBitDepth::Depth10, rgb).character_rate_khz(),
Some(272_002)
);
assert_eq!(
video(1, ColorBitDepth::Depth8, yuv420).character_rate_khz(),
Some(1)
);
assert_eq!(
video(148_500, ColorBitDepth::Depth6, rgb).character_rate_khz(),
None
);
assert_eq!(
video(148_500, ColorBitDepth::Depth14, rgb).character_rate_khz(),
None
);
assert_eq!(
video(u32::MAX, ColorBitDepth::Depth16, rgb).character_rate_khz(),
None
);
assert_eq!(
video(u32::MAX, ColorBitDepth::Depth8, rgb).character_rate_khz(),
Some(u32::MAX)
);
const RATE: Option<u32> =
TmdsVideo::new(148_500, ColorBitDepth::Depth8, ColorFormat::Rgb444)
.character_rate_khz();
assert_eq!(RATE, Some(148_500));
}
use display_types::cea861::hdmi_forum::HdmiForumFrl;
struct MockPhy {
frl_rate: Option<HdmiForumFrl>,
eq_calls: u32,
last_eq: Option<EqParams>,
frl_output: Option<FrlOutput>,
last_ltp: Option<LanePatterns>,
}
impl MockPhy {
fn new() -> Self {
Self {
frl_rate: None,
eq_calls: 0,
last_eq: None,
frl_output: None,
last_ltp: None,
}
}
}
impl FrlPhy for MockPhy {
type Error = core::convert::Infallible;
fn send_ltp(&mut self, patterns: LanePatterns) -> Result<(), Self::Error> {
self.last_ltp = Some(patterns);
Ok(())
}
fn set_frl_output(&mut self, output: FrlOutput) -> Result<(), Self::Error> {
self.frl_output = Some(output);
Ok(())
}
fn set_frl_rate(&mut self, rate: HdmiForumFrl) -> Result<(), Self::Error> {
self.frl_rate = Some(rate);
Ok(())
}
fn adjust_equalization(&mut self, params: EqParams) -> Result<(), Self::Error> {
self.eq_calls += 1;
self.last_eq = Some(params);
Ok(())
}
}
#[test]
fn ltp_pattern_values_match_scdc_encoding() {
assert_eq!(LtpPattern::AllOnes.value(), 1);
assert_eq!(LtpPattern::AllZeros.value(), 2);
assert_eq!(LtpPattern::NyquistClock.value(), 3);
assert_eq!(LtpPattern::DdeCompliance.value(), 4);
assert_eq!(LtpPattern::Lfsr0.value(), 5);
assert_eq!(LtpPattern::Lfsr1.value(), 6);
assert_eq!(LtpPattern::Lfsr2.value(), 7);
assert_eq!(LtpPattern::Lfsr3.value(), 8);
}
#[test]
fn ltp_pattern_clone_eq() {
let a = LtpPattern::Lfsr1;
assert_eq!(a, a);
assert_ne!(LtpPattern::Lfsr0, LtpPattern::Lfsr1);
}
#[test]
fn lane_patterns_default_is_no_pattern() {
let p = LanePatterns::default();
assert_eq!(p.lane0, None);
assert_eq!(p.lane1, None);
assert_eq!(p.lane2, None);
assert_eq!(p.lane3, None);
}
#[test]
fn send_ltp_records_patterns() {
let mut phy = MockPhy::new();
let patterns = LanePatterns {
lane0: Some(LtpPattern::Lfsr0),
lane1: Some(LtpPattern::Lfsr1),
lane2: Some(LtpPattern::Lfsr2),
lane3: None,
};
phy.send_ltp(patterns).unwrap();
assert_eq!(phy.last_ltp, Some(patterns));
}
#[test]
fn send_ltp_updates_on_each_call() {
let mut phy = MockPhy::new();
let first = LanePatterns {
lane0: Some(LtpPattern::Lfsr0),
..LanePatterns::default()
};
let second = LanePatterns {
lane0: Some(LtpPattern::NyquistClock),
..first
};
phy.send_ltp(first).unwrap();
phy.send_ltp(second).unwrap();
assert_eq!(phy.last_ltp, Some(second));
}
#[test]
fn eq_params_constructors_are_equivalent() {
let _a = EqParams::new();
let _b = EqParams::default();
}
#[test]
fn set_frl_rate_records_rate() {
let mut phy = MockPhy::new();
phy.set_frl_rate(HdmiForumFrl::Rate6Gbps4Lanes).unwrap();
assert_eq!(phy.frl_rate, Some(HdmiForumFrl::Rate6Gbps4Lanes));
}
#[test]
fn set_frl_rate_not_supported_is_valid() {
let mut phy = MockPhy::new();
phy.set_frl_rate(HdmiForumFrl::NotSupported).unwrap();
assert_eq!(phy.frl_rate, Some(HdmiForumFrl::NotSupported));
}
#[test]
fn set_frl_rate_can_be_updated() {
let mut phy = MockPhy::new();
phy.set_frl_rate(HdmiForumFrl::Rate3Gbps3Lanes).unwrap();
phy.set_frl_rate(HdmiForumFrl::Rate12Gbps4Lanes).unwrap();
assert_eq!(phy.frl_rate, Some(HdmiForumFrl::Rate12Gbps4Lanes));
}
#[test]
fn set_frl_output_records_mode() {
let mut phy = MockPhy::new();
phy.set_frl_output(FrlOutput::GapOnly).unwrap();
assert_eq!(phy.frl_output, Some(FrlOutput::GapOnly));
}
#[test]
fn set_frl_output_can_be_switched() {
let mut phy = MockPhy::new();
phy.set_frl_output(FrlOutput::GapOnly).unwrap();
phy.set_frl_output(FrlOutput::Active).unwrap();
assert_eq!(phy.frl_output, Some(FrlOutput::Active));
}
#[test]
fn value_types_can_be_hashed() {
extern crate std;
use std::collections::HashSet;
let patterns: HashSet<LanePatterns> = [LanePatterns::default(), LanePatterns::default()]
.into_iter()
.collect();
assert_eq!(patterns.len(), 1);
let outputs: HashSet<FrlOutput> = [FrlOutput::GapOnly, FrlOutput::Active].into();
assert_eq!(outputs.len(), 2);
let levels: HashSet<TxFfeLevel> = [TxFfeLevel::MAX, TxFfeLevel::default()].into();
assert_eq!(levels.len(), 2);
let eq: HashSet<EqParams> = [EqParams::new(), EqParams::new()].into();
assert_eq!(eq.len(), 1);
let ltp: HashSet<LtpPattern> = [LtpPattern::Lfsr0, LtpPattern::Lfsr0].into();
assert_eq!(ltp.len(), 1);
}
#[test]
fn tx_ffe_level_accepts_0_to_7() {
for level in 0..=7 {
assert_eq!(TxFfeLevel::new(level).map(TxFfeLevel::value), Some(level));
}
}
#[test]
fn tx_ffe_level_rejects_above_7() {
assert_eq!(TxFfeLevel::new(8), None);
assert_eq!(TxFfeLevel::new(u8::MAX), None);
}
#[test]
fn tx_ffe_level_max_and_default() {
assert_eq!(TxFfeLevel::MAX.value(), 7);
assert_eq!(TxFfeLevel::default().value(), 0);
assert!(TxFfeLevel::default() < TxFfeLevel::MAX);
}
#[test]
fn lane_eq_params_default_tx_ffe_level_is_0() {
assert_eq!(LaneEqParams::default().tx_ffe_level, TxFfeLevel::default());
}
#[test]
fn tx_ffe_restrictions_new_takes_the_bits_in_register_order() {
let r = TxFfeRestrictions::new(true, false, false);
assert!(r.pre_shoot_only && !r.de_emphasis_only && !r.no_ffe);
let r = TxFfeRestrictions::new(false, true, false);
assert!(!r.pre_shoot_only && r.de_emphasis_only && !r.no_ffe);
let r = TxFfeRestrictions::new(false, false, true);
assert!(!r.pre_shoot_only && !r.de_emphasis_only && r.no_ffe);
let both = TxFfeRestrictions::new(true, true, false);
assert!(both.pre_shoot_only && both.de_emphasis_only);
}
#[test]
fn eq_params_restrict_nothing_by_default() {
assert_eq!(
TxFfeRestrictions::default(),
TxFfeRestrictions::new(false, false, false)
);
assert_eq!(
EqParams::new().tx_ffe_restrictions,
TxFfeRestrictions::default()
);
}
#[test]
fn adjust_equalization_records_per_lane_levels() {
let mut phy = MockPhy::new();
let mut params = EqParams::new();
params.lane1.tx_ffe_level = TxFfeLevel::MAX;
phy.adjust_equalization(params).unwrap();
let last = phy.last_eq.unwrap();
assert_eq!(last, params);
assert_eq!(last.lane0.tx_ffe_level.value(), 0);
assert_eq!(last.lane1.tx_ffe_level.value(), 7);
}
#[test]
fn adjust_equalization_tracks_call_count() {
let mut phy = MockPhy::new();
phy.adjust_equalization(EqParams::new()).unwrap();
phy.adjust_equalization(EqParams::new()).unwrap();
assert_eq!(phy.eq_calls, 2);
}
struct MockTmdsPhy {
config: Option<TmdsPhyConfig>,
scrambling: Option<bool>,
output: bool,
}
impl TmdsPhy for MockTmdsPhy {
type Error = core::convert::Infallible;
fn capabilities(&self) -> TmdsPhyCapabilities {
TmdsPhyCapabilities::new(600_000)
.with_scrambling(true)
.with_low_rate_scrambling(true)
}
fn configure(&mut self, config: TmdsPhyConfig) -> Result<(), Self::Error> {
self.config = Some(config);
Ok(())
}
fn set_scrambling(&mut self, enabled: bool) -> Result<(), Self::Error> {
self.scrambling = Some(enabled);
Ok(())
}
fn start_output(&mut self) -> Result<(), Self::Error> {
self.output = true;
Ok(())
}
fn stop_output(&mut self) -> Result<(), Self::Error> {
self.output = false;
Ok(())
}
}
const VIDEO_4K60: TmdsVideo = TmdsVideo::new(
594_000,
display_types::ColorBitDepth::Depth8,
display_types::ColorFormat::Rgb444,
);
#[test]
fn tmds_video_new_has_no_pixel_repetition() {
assert_eq!(VIDEO_4K60.pixel_clock_khz, 594_000);
assert_eq!(VIDEO_4K60.color_depth, display_types::ColorBitDepth::Depth8);
assert_eq!(VIDEO_4K60.color_format, display_types::ColorFormat::Rgb444);
assert_eq!(VIDEO_4K60.pixel_repetition, 0);
assert_eq!(VIDEO_4K60.with_pixel_repetition(1).pixel_repetition, 1);
let video = TmdsVideo::new(
594_000,
display_types::ColorBitDepth::Depth8,
display_types::ColorFormat::Rgb444,
);
assert_eq!(video, VIDEO_4K60);
}
#[test]
fn tmds_phy_config_new_takes_its_fields_in_order() {
const CONFIG: TmdsPhyConfig =
TmdsPhyConfig::new(594_000, TmdsClockRatio::Div40, VIDEO_4K60);
assert_eq!(CONFIG.character_rate_khz, 594_000);
assert_eq!(CONFIG.clock_ratio, TmdsClockRatio::Div40);
assert_eq!(CONFIG.video, VIDEO_4K60);
}
#[test]
fn tmds_phy_capabilities_new_reports_no_scrambler() {
let caps = TmdsPhyCapabilities::new(340_000);
assert_eq!(caps.max_character_rate_khz, 340_000);
assert!(!caps.scrambling);
assert!(!caps.low_rate_scrambling);
let caps = caps.with_scrambling(true).with_low_rate_scrambling(true);
assert!(caps.scrambling && caps.low_rate_scrambling);
}
#[test]
fn colour_types_are_display_types_at_both_paths() {
let depth: ColorBitDepth = display_types::ColorBitDepth::Depth10;
let _: crate::ColorBitDepth = depth;
let format: ColorFormat = display_types::ColorFormat::YCbCr420;
let _: crate::ColorFormat = format;
}
#[test]
fn low_rate_scrambling_counts_only_with_a_scrambler() {
let base = TmdsPhyCapabilities::new(600_000);
assert!(!base.scrambles_at_low_rate());
assert!(!base.with_low_rate_scrambling(true).scrambles_at_low_rate());
assert!(!base.with_scrambling(true).scrambles_at_low_rate());
let both = base.with_scrambling(true).with_low_rate_scrambling(true);
assert!(both.scrambles_at_low_rate());
}
#[test]
fn tmds_phy_records_configure_scrambling_and_output() {
let mut phy = MockTmdsPhy {
config: None,
scrambling: None,
output: false,
};
assert!(phy.capabilities().scrambling);
let config = TmdsPhyConfig::new(594_000, TmdsClockRatio::Div40, VIDEO_4K60);
phy.stop_output().unwrap();
phy.configure(config).unwrap();
phy.set_scrambling(true).unwrap();
phy.start_output().unwrap();
assert_eq!(phy.config, Some(config));
assert_eq!(phy.scrambling, Some(true));
assert!(phy.output);
phy.set_scrambling(false).unwrap();
phy.stop_output().unwrap();
assert_eq!(phy.scrambling, Some(false));
assert!(!phy.output);
}
struct PoweredPhy {
tmds: MockTmdsPhy,
powered: bool,
}
impl PhyPower for PoweredPhy {
type Error = core::convert::Infallible;
fn power_on(&mut self) -> Result<(), Self::Error> {
self.powered = true;
Ok(())
}
fn power_off(&mut self) -> Result<(), Self::Error> {
self.powered = false;
self.tmds.output = false;
self.tmds.config = None;
Ok(())
}
}
impl TmdsPhy for PoweredPhy {
type Error = <Self as PhyPower>::Error;
fn capabilities(&self) -> TmdsPhyCapabilities {
self.tmds.capabilities()
}
fn configure(&mut self, config: TmdsPhyConfig) -> Result<(), Self::Error> {
self.tmds.configure(config)
}
fn set_scrambling(&mut self, enabled: bool) -> Result<(), Self::Error> {
self.tmds.set_scrambling(enabled)
}
fn start_output(&mut self) -> Result<(), Self::Error> {
self.tmds.start_output()
}
fn stop_output(&mut self) -> Result<(), Self::Error> {
self.tmds.stop_output()
}
}
fn power_cycle<P: PhyPower + TmdsPhy>(phy: &mut P, config: TmdsPhyConfig) {
phy.power_off().unwrap();
phy.power_off().unwrap();
phy.power_on().unwrap();
phy.power_on().unwrap();
phy.stop_output().unwrap();
phy.configure(config).unwrap();
let scrambling = phy.capabilities().scrambling;
phy.set_scrambling(scrambling).unwrap();
phy.start_output().unwrap();
}
#[test]
fn phy_power_composes_with_a_mode_trait() {
let mut phy = PoweredPhy {
tmds: MockTmdsPhy {
config: None,
scrambling: None,
output: true,
},
powered: true,
};
let config = TmdsPhyConfig::new(594_000, TmdsClockRatio::Div40, VIDEO_4K60);
power_cycle(&mut phy, config);
assert!(phy.powered && phy.tmds.output);
assert_eq!(phy.tmds.config, Some(config));
assert_eq!(phy.tmds.scrambling, Some(true));
phy.power_off().unwrap();
assert!(!phy.powered && !phy.tmds.output);
assert_eq!(phy.tmds.config, None);
}
#[derive(Debug, PartialEq, Eq)]
enum Mode {
None,
Frl(HdmiForumFrl),
Tmds,
}
struct FullPhy {
mode: Mode,
powered: bool,
}
#[derive(Debug, PartialEq, Eq)]
struct PhyError;
impl FrlPhy for FullPhy {
type Error = PhyError;
fn set_frl_rate(&mut self, rate: HdmiForumFrl) -> Result<(), PhyError> {
if !self.powered {
return Err(PhyError);
}
self.mode = match rate {
HdmiForumFrl::NotSupported => Mode::None,
rate => Mode::Frl(rate),
};
Ok(())
}
fn send_ltp(&mut self, _patterns: LanePatterns) -> Result<(), PhyError> {
Ok(())
}
fn set_frl_output(&mut self, _output: FrlOutput) -> Result<(), PhyError> {
Ok(())
}
fn adjust_equalization(&mut self, _params: EqParams) -> Result<(), PhyError> {
Ok(())
}
}
impl TmdsPhy for FullPhy {
type Error = PhyError;
fn capabilities(&self) -> TmdsPhyCapabilities {
TmdsPhyCapabilities::new(600_000).with_scrambling(true)
}
fn configure(&mut self, _config: TmdsPhyConfig) -> Result<(), PhyError> {
if !self.powered {
return Err(PhyError);
}
self.mode = Mode::Tmds;
Ok(())
}
fn set_scrambling(&mut self, _enabled: bool) -> Result<(), PhyError> {
Ok(())
}
fn start_output(&mut self) -> Result<(), PhyError> {
if self.mode == Mode::Tmds {
Ok(())
} else {
Err(PhyError)
}
}
fn stop_output(&mut self) -> Result<(), PhyError> {
if matches!(self.mode, Mode::Frl(_)) {
self.mode = Mode::None;
}
Ok(())
}
}
impl PhyPower for FullPhy {
type Error = PhyError;
fn power_on(&mut self) -> Result<(), PhyError> {
self.powered = true;
Ok(())
}
fn power_off(&mut self) -> Result<(), PhyError> {
self.powered = false;
self.mode = Mode::None;
Ok(())
}
}
fn fall_back_to_tmds<P>(phy: &mut P, config: TmdsPhyConfig) -> Result<(), <P as TmdsPhy>::Error>
where
P: TmdsPhy
+ FrlPhy<Error = <P as TmdsPhy>::Error>
+ PhyPower<Error = <P as TmdsPhy>::Error>,
{
phy.power_on()?;
phy.set_frl_rate(HdmiForumFrl::Rate6Gbps4Lanes)?;
phy.send_ltp(LanePatterns::default())?;
phy.adjust_equalization(EqParams::new())?;
phy.set_frl_output(FrlOutput::GapOnly)?;
phy.set_frl_rate(HdmiForumFrl::NotSupported)?;
phy.stop_output()?;
phy.configure(config)?;
let scrambling = phy.capabilities().scrambling;
phy.set_scrambling(scrambling)?;
phy.start_output()
}
#[test]
fn one_type_implements_all_three_traits_with_one_error_type() {
let mut phy = FullPhy {
mode: Mode::None,
powered: false,
};
let config = TmdsPhyConfig::new(594_000, TmdsClockRatio::Div40, VIDEO_4K60);
assert_eq!(phy.configure(config), Err(PhyError));
assert_eq!(
phy.set_frl_rate(HdmiForumFrl::Rate3Gbps3Lanes),
Err(PhyError)
);
fall_back_to_tmds(&mut phy, config).unwrap();
assert_eq!(phy.mode, Mode::Tmds);
phy.set_frl_rate(HdmiForumFrl::Rate3Gbps3Lanes).unwrap();
assert_eq!(phy.mode, Mode::Frl(HdmiForumFrl::Rate3Gbps3Lanes));
TmdsPhy::stop_output(&mut phy).unwrap();
assert_eq!(phy.mode, Mode::None);
assert_eq!(phy.start_output(), Err(PhyError));
phy.power_off().unwrap();
assert!(!phy.powered);
}
}