Skip to main content

capsules_core/virtualizers/
virtual_adc.rs

1// Licensed under the Apache License, Version 2.0 or the MIT License.
2// SPDX-License-Identifier: Apache-2.0 OR MIT
3// Copyright Tock Contributors 2022.
4
5//! Virtual ADC Capsule
6//!
7//! Support Single Sample for now.
8
9use kernel::ErrorCode;
10use kernel::collections::list::{List, ListLink, ListNode};
11use kernel::hil;
12use kernel::utilities::cells::OptionalCell;
13
14/// ADC Mux
15pub struct MuxAdc<'a, A: hil::adc::Adc<'a>> {
16    adc: &'a A,
17    devices: List<'a, AdcDevice<'a, A>>,
18    inflight: OptionalCell<&'a AdcDevice<'a, A>>,
19}
20
21impl<'a, A: hil::adc::Adc<'a>> hil::adc::Client for MuxAdc<'a, A> {
22    fn sample_ready(&self, sample: u16) {
23        self.inflight.take().map(|inflight| {
24            for node in self.devices.iter() {
25                if node.channel == inflight.channel {
26                    node.operation.take().map(|operation| match operation {
27                        Operation::OneSample => {
28                            node.client.map(|client| client.sample_ready(sample))
29                        }
30                    });
31                }
32            }
33        });
34        self.do_next_op();
35    }
36}
37
38impl<'a, A: hil::adc::Adc<'a>> MuxAdc<'a, A> {
39    pub const fn new(adc: &'a A) -> MuxAdc<'a, A> {
40        MuxAdc {
41            adc,
42            devices: List::new(),
43            inflight: OptionalCell::empty(),
44        }
45    }
46
47    fn do_next_op(&self) {
48        if self.inflight.is_none() {
49            let mnode = self.devices.iter().find(|node| node.operation.is_some());
50            mnode.map(|node| {
51                let started = node.operation.map_or(false, |operation| match operation {
52                    Operation::OneSample => {
53                        let _ = self.adc.sample(&node.channel);
54                        true
55                    }
56                });
57                if started {
58                    self.inflight.set(node);
59                } else {
60                    self.do_next_op();
61                }
62            });
63        }
64    }
65
66    pub fn get_resolution_bits(&self) -> usize {
67        self.adc.get_resolution_bits()
68    }
69
70    pub fn get_voltage_reference_mv(&self) -> Option<usize> {
71        self.adc.get_voltage_reference_mv()
72    }
73}
74
75#[derive(Copy, Clone, PartialEq)]
76pub(crate) enum Operation {
77    OneSample,
78}
79
80/// Virtual ADC device
81pub struct AdcDevice<'a, A: hil::adc::Adc<'a>> {
82    mux: &'a MuxAdc<'a, A>,
83    channel: A::Channel,
84    operation: OptionalCell<Operation>,
85    next: ListLink<'a, AdcDevice<'a, A>>,
86    client: OptionalCell<&'a dyn hil::adc::Client>,
87}
88
89impl<'a, A: hil::adc::Adc<'a>> AdcDevice<'a, A> {
90    pub const fn new(mux: &'a MuxAdc<'a, A>, channel: A::Channel) -> AdcDevice<'a, A> {
91        AdcDevice {
92            mux,
93            channel,
94            operation: OptionalCell::empty(),
95            next: ListLink::empty(),
96            client: OptionalCell::empty(),
97        }
98    }
99
100    pub fn add_to_mux(&'a self) {
101        self.mux.devices.push_head(self);
102    }
103}
104
105impl<'a, A: hil::adc::Adc<'a>> ListNode<'a, AdcDevice<'a, A>> for AdcDevice<'a, A> {
106    fn next(&'a self) -> &'a ListLink<'a, AdcDevice<'a, A>> {
107        &self.next
108    }
109}
110
111impl<'a, A: hil::adc::Adc<'a>> hil::adc::AdcChannel<'a> for AdcDevice<'a, A> {
112    fn sample(&self) -> Result<(), ErrorCode> {
113        self.operation.set(Operation::OneSample);
114        self.mux.do_next_op();
115        Ok(())
116    }
117
118    fn stop_sampling(&self) -> Result<(), ErrorCode> {
119        self.operation.clear();
120        self.mux.do_next_op();
121        Ok(())
122    }
123
124    fn sample_continuous(&self) -> Result<(), ErrorCode> {
125        Err(ErrorCode::NOSUPPORT)
126    }
127
128    fn get_resolution_bits(&self) -> usize {
129        self.mux.get_resolution_bits()
130    }
131
132    fn get_voltage_reference_mv(&self) -> Option<usize> {
133        self.mux.get_voltage_reference_mv()
134    }
135    fn set_client(&self, client: &'a dyn hil::adc::Client) {
136        self.client.set(client);
137    }
138}