capsules_extra/
dac.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//! Provides a DAC interface for userspace.
6//!
7//! Usage
8//! -----
9//!
10//! ```rust,ignore
11//! # use kernel::static_init;
12//!
13//! let dac = static_init!(
14//!     capsules::dac::Dac<'static>,
15//!     capsules::dac::Dac::new(&mut sam4l::dac::DAC));
16//! ```
17
18/// Syscall driver number.
19use capsules_core::driver;
20pub const DRIVER_NUM: usize = driver::NUM::Dac as usize;
21
22use kernel::hil;
23use kernel::syscall::{CommandReturn, SyscallDriver};
24use kernel::{ErrorCode, ProcessId};
25
26pub struct Dac<'a> {
27    dac: &'a dyn hil::dac::DacChannel,
28}
29
30impl<'a> Dac<'a> {
31    pub fn new(dac: &'a dyn hil::dac::DacChannel) -> Dac<'a> {
32        Dac { dac }
33    }
34}
35
36impl SyscallDriver for Dac<'_> {
37    /// Control the DAC.
38    ///
39    /// ### `command_num`
40    ///
41    /// - `0`: Driver existence check.
42    /// - `1`: Initialize and enable the DAC.
43    /// - `2`: Set the output to `data1`, a scaled output value.
44    fn command(&self, command_num: usize, data: usize, _: usize, _: ProcessId) -> CommandReturn {
45        match command_num {
46            0 => CommandReturn::success(),
47
48            // enable the dac. no-op as using the dac will enable it.
49            1 => CommandReturn::success(),
50
51            // set the dac output
52            2 => CommandReturn::from(self.dac.set_value(data)),
53
54            _ => CommandReturn::failure(ErrorCode::NOSUPPORT),
55        }
56    }
57
58    fn allocate_grant(&self, _processid: ProcessId) -> Result<(), kernel::process::Error> {
59        Ok(())
60    }
61}