Skip to main content

culvert_async/client/
plumbob_client.rs

1//! `plumbob_async::ScdcClient` implementation for [`Scdc`].
2//!
3//! Each trait method awaits one culvert-async method. The conversions from culvert's
4//! register types to plumbob's are culvert's own (its `plumbob` feature, which this
5//! feature enables), so the sync and async adapters convert identically.
6
7use hdmi_hal_async::scdc::ScdcTransport;
8use plumbob_async::ScdcClient;
9
10use crate::{Config0, ScdcError};
11
12use super::Scdc;
13
14/// Typed SCDC access for plumbob-async's link training.
15///
16/// `Scdc` does not wait between calls. plumbob's state machine polls `read_flt_ready` and
17/// `read_update_flags` in a loop whose limits assume one poll every 2 ms by default; await
18/// that interval inside those two methods, in an `ScdcClient` of your own that wraps
19/// `Scdc<T>` and forwards the other five (`doc/architecture.md` shows one). A deadline or
20/// cancel signal belongs there too, following plumbob's "Stopping early" rule. Not in the
21/// transport: there a wait applies to every register access, and a transport cannot tell
22/// the polled reads from LTS:L's.
23#[cfg_attr(docsrs, doc(cfg(feature = "plumbob-async")))]
24impl<T: ScdcTransport> ScdcClient for Scdc<T> {
25    type Error = ScdcError<T::Error>;
26
27    async fn read_flt_ready(&mut self) -> Result<bool, Self::Error> {
28        Ok(Scdc::read_status_flags(self).await?.flt_ready)
29    }
30
31    async fn read_update_flags(&mut self) -> Result<plumbob_async::UpdateFlags, Self::Error> {
32        Scdc::read_update_flags(self).await.map(Into::into)
33    }
34
35    async fn clear_update_flags(
36        &mut self,
37        flags: plumbob_async::ClearableUpdateFlags,
38    ) -> Result<(), Self::Error> {
39        Scdc::clear_update_flags(self, flags.into()).await
40    }
41
42    async fn read_ltp_requests(&mut self) -> Result<plumbob_async::LtpRequests, Self::Error> {
43        Scdc::read_ltp_requests(self).await.map(Into::into)
44    }
45
46    async fn read_source_test_config(
47        &mut self,
48    ) -> Result<plumbob_async::SourceTestConfig, Self::Error> {
49        Scdc::read_source_test_config(self).await.map(Into::into)
50    }
51
52    async fn write_config_0_defaults(&mut self) -> Result<(), Self::Error> {
53        Scdc::write_config_0(self, Config0::default()).await
54    }
55
56    async fn write_frl_config(
57        &mut self,
58        config: plumbob_async::FrlConfig,
59    ) -> Result<(), Self::Error> {
60        Scdc::write_frl_config(self, config.into()).await
61    }
62}
63
64#[cfg(test)]
65mod tests {
66    use super::super::Scdc;
67    use super::super::test_transport::TestTransport;
68    use crate::{ProtocolError, ScdcError};
69    use display_types::HdmiForumFrl;
70    use plumbob_async::{
71        ClearableUpdateFlags, FfeLevels, FrlConfig, LtpReq, LtpRequests, ScdcClient,
72        SourceTestConfig, UpdateFlags,
73    };
74
75    fn scdc_with(regs: &[(u8, u8)]) -> Scdc<TestTransport> {
76        let mut t = TestTransport::new();
77        for &(addr, value) in regs {
78            t.set(addr, value);
79        }
80        Scdc::new(t)
81    }
82
83    // --- read_flt_ready: Status_Flags_0 (0x40) bit 6
84
85    #[pollster::test]
86    async fn read_flt_ready_reads_status_flags_0_bit_6() {
87        assert!(
88            ScdcClient::read_flt_ready(&mut scdc_with(&[(0x40, 0x40)]))
89                .await
90                .unwrap()
91        );
92        assert!(
93            !ScdcClient::read_flt_ready(&mut scdc_with(&[(0x40, 0xBF)]))
94                .await
95                .unwrap()
96        );
97    }
98
99    // --- read_update_flags: Update_0 (0x10), all seven flags
100
101    #[pollster::test]
102    async fn read_update_flags_maps_every_flag() {
103        let read = |byte| {
104            pollster::block_on(ScdcClient::read_update_flags(&mut scdc_with(&[(
105                0x10, byte,
106            )])))
107            .unwrap()
108        };
109        let flag = |n: usize| {
110            let b = |i| i == n;
111            UpdateFlags::new(b(0), b(1), b(2), b(3), b(4), b(5), b(6))
112        };
113        for n in 0..7 {
114            assert_eq!(read(1 << n), flag(n), "bit {n}");
115        }
116        assert_eq!(
117            read(0x7F),
118            UpdateFlags::new(true, true, true, true, true, true, true)
119        );
120        // Bit 7 is reserved.
121        assert_eq!(read(0x80), UpdateFlags::default());
122    }
123
124    // --- clear_update_flags: write 1 to clear, only the given flags
125
126    #[pollster::test]
127    async fn clear_update_flags_writes_each_given_bit() {
128        let clear = |flags| {
129            let mut scdc = Scdc::new(TestTransport::new());
130            pollster::block_on(ScdcClient::clear_update_flags(&mut scdc, flags)).unwrap();
131            scdc.into_transport().get(0x10)
132        };
133        // Bit order without RR_Test (bit 2), which has no field.
134        for (n, bit) in [0, 1, 3, 4, 5, 6].into_iter().enumerate() {
135            let b = |i| i == n;
136            let flags = ClearableUpdateFlags::new(b(0), b(1), b(2), b(3), b(4), b(5));
137            assert_eq!(clear(flags), 1 << bit, "bit {bit}");
138        }
139        assert_eq!(clear(ClearableUpdateFlags::default()), 0x00);
140    }
141
142    // --- read_ltp_requests: Status_Flags_1/2 (0x41/0x42), one nibble per lane
143
144    #[pollster::test]
145    async fn read_ltp_requests_maps_each_lane() {
146        let mut scdc = scdc_with(&[(0x41, 0x65), (0x42, 0x87)]);
147        assert_eq!(
148            ScdcClient::read_ltp_requests(&mut scdc).await.unwrap(),
149            LtpRequests::new(LtpReq::Lfsr0, LtpReq::Lfsr1, LtpReq::Lfsr2, LtpReq::Lfsr3)
150        );
151    }
152
153    #[pollster::test]
154    async fn read_ltp_requests_maps_every_request_value() {
155        let cases = [
156            (0x0, LtpReq::None),
157            (0x1, LtpReq::AllOnes),
158            (0x2, LtpReq::AllZeros),
159            (0x3, LtpReq::NyquistClock),
160            (0x4, LtpReq::DdeCompliance),
161            (0x5, LtpReq::Lfsr0),
162            (0x6, LtpReq::Lfsr1),
163            (0x7, LtpReq::Lfsr2),
164            (0x8, LtpReq::Lfsr3),
165            (0xE, LtpReq::FfeChange),
166            (0xF, LtpReq::RateChange),
167        ];
168        for (nibble, expected) in cases {
169            let requests = ScdcClient::read_ltp_requests(&mut scdc_with(&[(0x41, nibble)]))
170                .await
171                .unwrap();
172            assert_eq!(requests.lane0, expected, "{nibble:#x}");
173        }
174    }
175
176    #[pollster::test]
177    async fn read_ltp_requests_passes_undefined_values_on() {
178        let requests = ScdcClient::read_ltp_requests(&mut scdc_with(&[(0x42, 0x90)]))
179            .await
180            .unwrap();
181        assert_eq!(requests.lane3, LtpReq::Reserved(0x9));
182        assert_eq!(requests.lane2, LtpReq::None);
183    }
184
185    // --- read_source_test_config: Source_Test_Configuration (0x35), all six bits
186
187    #[pollster::test]
188    async fn read_source_test_config_maps_every_bit() {
189        let read = |byte| {
190            pollster::block_on(ScdcClient::read_source_test_config(&mut scdc_with(&[(
191                0x35, byte,
192            )])))
193            .unwrap()
194        };
195        // Bits 1–3 and 5–7, in the order of the fields; bits 0 and 4 are reserved.
196        for (n, bit) in [1, 2, 3, 5, 6, 7].into_iter().enumerate() {
197            let b = |i| i == n;
198            let expected = SourceTestConfig::new(b(0), b(1), b(2), b(3), b(4), b(5));
199            assert_eq!(read(1 << bit), expected, "bit {bit}");
200        }
201        assert_eq!(read(0x11), SourceTestConfig::default());
202    }
203
204    // --- write_config_0_defaults: Config_0 (0x30) cleared
205
206    #[pollster::test]
207    async fn write_config_0_defaults_clears_config_0() {
208        let mut scdc = scdc_with(&[(0x30, 0xFF)]);
209        ScdcClient::write_config_0_defaults(&mut scdc)
210            .await
211            .unwrap();
212        assert_eq!(scdc.into_transport().get(0x30), 0x00);
213    }
214
215    // --- write_frl_config: Config_1 (0x31), rate in bits 3:0, FFE levels in bits 7:4
216
217    #[pollster::test]
218    async fn write_frl_config_encodes_rate_and_ffe_levels() {
219        let mut scdc = Scdc::new(TestTransport::new());
220        ScdcClient::write_frl_config(
221            &mut scdc,
222            FrlConfig::new(HdmiForumFrl::Rate12Gbps4Lanes, FfeLevels::new(3).unwrap()),
223        )
224        .await
225        .unwrap();
226        assert_eq!(scdc.into_transport().get(0x31), 0x36);
227    }
228
229    #[pollster::test]
230    async fn write_frl_config_turns_frl_off() {
231        let mut scdc = scdc_with(&[(0x31, 0x36)]);
232        ScdcClient::write_frl_config(
233            &mut scdc,
234            FrlConfig::new(HdmiForumFrl::NotSupported, FfeLevels::default()),
235        )
236        .await
237        .unwrap();
238        assert_eq!(scdc.into_transport().get(0x31), 0x00);
239    }
240
241    #[pollster::test]
242    async fn write_frl_config_keeps_culverts_rate_check() {
243        // plumbob limits levels per rate itself; culvert still rejects a level above it.
244        let mut scdc = Scdc::new(TestTransport::new());
245        let result = ScdcClient::write_frl_config(
246            &mut scdc,
247            FrlConfig::new(HdmiForumFrl::Rate6Gbps4Lanes, FfeLevels::MAX),
248        )
249        .await;
250        assert!(matches!(
251            result,
252            Err(ScdcError::Protocol(ProtocolError::FfeLevelsOutOfRange {
253                levels: 7,
254                ..
255            }))
256        ));
257    }
258
259    // --- transport errors
260
261    #[pollster::test]
262    async fn transport_errors_are_returned() {
263        let fails = || Scdc::new(TestTransport::failing_after(0));
264        assert!(matches!(
265            ScdcClient::read_flt_ready(&mut fails()).await,
266            Err(ScdcError::Transport(()))
267        ));
268        assert!(
269            ScdcClient::read_update_flags(&mut fails())
270                .await
271                .is_err_and(|error| matches!(error, ScdcError::Transport(())))
272        );
273        assert!(
274            ScdcClient::clear_update_flags(&mut fails(), ClearableUpdateFlags::default())
275                .await
276                .is_err_and(|error| matches!(error, ScdcError::Transport(())))
277        );
278        assert!(
279            ScdcClient::read_ltp_requests(&mut fails())
280                .await
281                .is_err_and(|error| matches!(error, ScdcError::Transport(())))
282        );
283        assert!(
284            ScdcClient::read_source_test_config(&mut fails())
285                .await
286                .is_err_and(|error| matches!(error, ScdcError::Transport(())))
287        );
288        assert!(
289            ScdcClient::write_config_0_defaults(&mut fails())
290                .await
291                .is_err_and(|error| matches!(error, ScdcError::Transport(())))
292        );
293        assert!(
294            ScdcClient::write_frl_config(
295                &mut fails(),
296                FrlConfig::new(HdmiForumFrl::Rate6Gbps4Lanes, FfeLevels::default())
297            )
298            .await
299            .is_err_and(|error| matches!(error, ScdcError::Transport(())))
300        );
301    }
302}