components/
adc.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.

//! Components for using ADC capsules.

use capsules_core::adc::AdcDedicated;
use capsules_core::adc::AdcVirtualized;
use capsules_core::virtualizers::virtual_adc::{AdcDevice, MuxAdc};
use core::mem::MaybeUninit;
use kernel::capabilities;
use kernel::component::Component;
use kernel::create_capability;
use kernel::hil::adc;

#[macro_export]
macro_rules! adc_mux_component_static {
    ($A:ty $(,)?) => {{
        kernel::static_buf!(capsules_core::virtualizers::virtual_adc::MuxAdc<'static, $A>)
    };};
}

#[macro_export]
macro_rules! adc_component_static {
    ($A:ty $(,)?) => {{
        kernel::static_buf!(capsules_core::virtualizers::virtual_adc::AdcDevice<'static, $A>)
    };};
}

#[macro_export]
macro_rules! adc_syscall_component_helper {
    ($($P:expr),+ $(,)?) => {{
        use kernel::count_expressions;
        use kernel::static_init;
        const NUM_DRIVERS: usize = count_expressions!($($P),+);

        let drivers = static_init!(
            [&'static dyn kernel::hil::adc::AdcChannel; NUM_DRIVERS],
            [
                $($P,)*
            ]
        );
        let adc_virtualized = kernel::static_buf!(capsules_core::adc::AdcVirtualized<'static>);
        (adc_virtualized, drivers)
    };};
}

#[macro_export]
macro_rules! adc_dedicated_component_static {
    ($A:ty $(,)?) => {{
        let adc = kernel::static_buf!(capsules_core::adc::AdcDedicated<'static, $A>);
        let buffer1 = kernel::static_buf!([u16; capsules_core::adc::BUF_LEN]);
        let buffer2 = kernel::static_buf!([u16; capsules_core::adc::BUF_LEN]);
        let buffer3 = kernel::static_buf!([u16; capsules_core::adc::BUF_LEN]);

        (adc, buffer1, buffer2, buffer3)
    };};
}

pub struct AdcMuxComponent<A: 'static + adc::Adc<'static>> {
    adc: &'static A,
}

impl<A: 'static + adc::Adc<'static>> AdcMuxComponent<A> {
    pub fn new(adc: &'static A) -> Self {
        AdcMuxComponent { adc }
    }
}

impl<A: 'static + adc::Adc<'static>> Component for AdcMuxComponent<A> {
    type StaticInput = &'static mut MaybeUninit<MuxAdc<'static, A>>;
    type Output = &'static MuxAdc<'static, A>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let adc_mux = static_buffer.write(MuxAdc::new(self.adc));

        self.adc.set_client(adc_mux);

        adc_mux
    }
}

pub struct AdcComponent<A: 'static + adc::Adc<'static>> {
    adc_mux: &'static MuxAdc<'static, A>,
    channel: A::Channel,
}

impl<A: 'static + adc::Adc<'static>> AdcComponent<A> {
    pub fn new(mux: &'static MuxAdc<'static, A>, channel: A::Channel) -> Self {
        AdcComponent {
            adc_mux: mux,
            channel,
        }
    }
}

impl<A: 'static + adc::Adc<'static>> Component for AdcComponent<A> {
    type StaticInput = &'static mut MaybeUninit<AdcDevice<'static, A>>;
    type Output = &'static AdcDevice<'static, A>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let adc_device = static_buffer.write(AdcDevice::new(self.adc_mux, self.channel));

        adc_device.add_to_mux();

        adc_device
    }
}

pub struct AdcVirtualComponent {
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
}

impl AdcVirtualComponent {
    pub fn new(board_kernel: &'static kernel::Kernel, driver_num: usize) -> AdcVirtualComponent {
        AdcVirtualComponent {
            board_kernel,
            driver_num,
        }
    }
}

impl Component for AdcVirtualComponent {
    type StaticInput = (
        &'static mut MaybeUninit<AdcVirtualized<'static>>,
        &'static [&'static dyn kernel::hil::adc::AdcChannel<'static>],
    );
    type Output = &'static capsules_core::adc::AdcVirtualized<'static>;

    fn finalize(self, static_buffer: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);
        let grant_adc = self.board_kernel.create_grant(self.driver_num, &grant_cap);

        let adc = static_buffer
            .0
            .write(capsules_core::adc::AdcVirtualized::new(
                static_buffer.1,
                grant_adc,
            ));

        for driver in static_buffer.1 {
            kernel::hil::adc::AdcChannel::set_client(*driver, adc);
        }

        adc
    }
}

pub type AdcDedicatedComponentType<A> = capsules_core::adc::AdcDedicated<'static, A>;

pub struct AdcDedicatedComponent<
    A: kernel::hil::adc::Adc<'static> + kernel::hil::adc::AdcHighSpeed<'static> + 'static,
> {
    adc: &'static A,
    channels: &'static [A::Channel],
    board_kernel: &'static kernel::Kernel,
    driver_num: usize,
}

impl<A: kernel::hil::adc::Adc<'static> + kernel::hil::adc::AdcHighSpeed<'static> + 'static>
    AdcDedicatedComponent<A>
{
    pub fn new(
        adc: &'static A,
        channels: &'static [A::Channel],
        board_kernel: &'static kernel::Kernel,
        driver_num: usize,
    ) -> AdcDedicatedComponent<A> {
        AdcDedicatedComponent {
            adc,
            channels,
            board_kernel,
            driver_num,
        }
    }
}

impl<A: kernel::hil::adc::Adc<'static> + kernel::hil::adc::AdcHighSpeed<'static> + 'static>
    Component for AdcDedicatedComponent<A>
{
    type StaticInput = (
        &'static mut MaybeUninit<AdcDedicated<'static, A>>,
        &'static mut MaybeUninit<[u16; capsules_core::adc::BUF_LEN]>,
        &'static mut MaybeUninit<[u16; capsules_core::adc::BUF_LEN]>,
        &'static mut MaybeUninit<[u16; capsules_core::adc::BUF_LEN]>,
    );
    type Output = &'static AdcDedicated<'static, A>;

    fn finalize(self, s: Self::StaticInput) -> Self::Output {
        let grant_cap = create_capability!(capabilities::MemoryAllocationCapability);

        let buffer1 = s.1.write([0; capsules_core::adc::BUF_LEN]);
        let buffer2 = s.2.write([0; capsules_core::adc::BUF_LEN]);
        let buffer3 = s.3.write([0; capsules_core::adc::BUF_LEN]);

        let adc = s.0.write(AdcDedicated::new(
            self.adc,
            self.board_kernel.create_grant(self.driver_num, &grant_cap),
            self.channels,
            buffer1,
            buffer2,
            buffer3,
        ));
        self.adc.set_client(adc);
        self.adc.set_highspeed_client(adc);

        adc
    }
}