opentitanlib/test_utils/bitbanging/
pwm.rs

1// Copyright lowRISC contributors (OpenTitan project).
2// Licensed under the Apache License, Version 2.0, see LICENSE for details.
3// SPDX-License-Identifier: Apache-2.0
4
5use super::Bit;
6use anyhow::Result;
7
8#[derive(Clone, Debug)]
9pub struct PwmPeriod {
10    pub period: std::time::Duration,
11    pub duty_cycle: f32,
12}
13
14pub mod decoder {
15    use super::*;
16
17    #[derive(Clone, Debug)]
18    struct Sample<const PIN: u8> {
19        raw: u8,
20    }
21
22    impl<const PIN: u8> Sample<PIN> {
23        fn pin(&self) -> Bit {
24            ((self.raw >> PIN) & 0x01).into()
25        }
26    }
27
28    pub struct Decoder<const PIN: u8> {
29        pub active_level: Bit,
30        pub sampling_period: std::time::Duration,
31    }
32
33    impl<const PIN: u8> Decoder<PIN> {
34        fn decode_period<I>(&self, samples: &mut I) -> Option<PwmPeriod>
35        where
36            I: Iterator<Item = Sample<PIN>>,
37        {
38            let num_active_samples =
39                samples.position(|sample| sample.pin() != self.active_level)? + 1;
40
41            let num_inactive_samples =
42                samples.position(|sample| sample.pin() == self.active_level)? + 1;
43
44            let period = (num_active_samples + num_inactive_samples) as u32;
45            let duty_cycle = num_active_samples as f32 * 100.0 / period as f32;
46
47            Some(PwmPeriod {
48                period: period * self.sampling_period,
49                duty_cycle,
50            })
51        }
52
53        pub fn run(&mut self, samples: Vec<u8>) -> Result<Vec<PwmPeriod>> {
54            let mut samples = samples.into_iter().map(|raw| Sample::<PIN> { raw });
55            // Discard the first period because it may be distorted by initial conditions.
56            let _ = self.decode_period(&mut samples);
57            let mut pwms = Vec::new();
58            while let Some(pwm) = self.decode_period(&mut samples) {
59                pwms.push(pwm);
60            }
61            Ok(pwms)
62        }
63    }
64}