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
// Licensed under the Apache License, Version 2.0 or the MIT License.
// SPDX-License-Identifier: Apache-2.0 OR MIT
// Copyright Tock Contributors 2022.
//! Provides userspace access to the analog comparators on a board.
//!
//! Usage
//! -----
//!
//! ```rust,ignore
//! # use kernel::static_init;
//!
//! let ac_channels = static_init!(
//! [&'static sam4l::acifc::AcChannel; 2],
//! [
//! &sam4l::acifc::CHANNEL_AC0,
//! &sam4l::acifc::CHANNEL_AC1,
//! ]
//! );
//! let analog_comparator = static_init!(
//! capsules::analog_comparator::AnalogComparator<'static, sam4l::acifc::Acifc>,
//! capsules::analog_comparator::AnalogComparator::new(&mut sam4l::acifc::ACIFC, ac_channels)
//! );
//! sam4l::acifc::ACIFC.set_client(analog_comparator);
//! ```
//!
//! ## Number of Analog Comparators
//! The number of analog comparators available depends on the microcontroller/board used.
//!
//! ## Normal or Interrupt-based Comparison
//! For a normal comparison or an interrupt-based comparison, just one analog
//! comparator is necessary.
//!
//! For more information on how this capsule works, please take a look at the
//! README: 00007_analog_comparator.md in doc/syscalls.
// Author: Danilo Verhaert <verhaert@cs.stanford.edu>
// Last modified August 9th, 2018
/// Syscall driver number.
use capsules_core::driver;
pub const DRIVER_NUM: usize = driver::NUM::AnalogComparator as usize;
use kernel::grant::{AllowRoCount, AllowRwCount, Grant, UpcallCount};
use kernel::hil;
use kernel::syscall::{CommandReturn, SyscallDriver};
use kernel::utilities::cells::OptionalCell;
use kernel::{ErrorCode, ProcessId};
pub struct AnalogComparator<'a, A: hil::analog_comparator::AnalogComparator<'a> + 'a> {
// Analog Comparator driver
analog_comparator: &'a A,
channels: &'a [&'a <A as hil::analog_comparator::AnalogComparator<'a>>::Channel],
grants: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
current_process: OptionalCell<ProcessId>,
}
#[derive(Default)]
pub struct App {}
impl<'a, A: hil::analog_comparator::AnalogComparator<'a>> AnalogComparator<'a, A> {
pub fn new(
analog_comparator: &'a A,
channels: &'a [&'a <A as hil::analog_comparator::AnalogComparator<'a>>::Channel],
grant: Grant<App, UpcallCount<1>, AllowRoCount<0>, AllowRwCount<0>>,
) -> AnalogComparator<'a, A> {
AnalogComparator {
// Analog Comparator driver
analog_comparator,
channels,
grants: grant,
current_process: OptionalCell::empty(),
}
}
// Do a single comparison on a channel
fn comparison(&self, channel: usize) -> Result<bool, ErrorCode> {
if channel >= self.channels.len() {
return Err(ErrorCode::INVAL);
}
// Convert channel index
let chan = self.channels[channel];
let result = self.analog_comparator.comparison(chan);
Ok(result)
}
// Start comparing on a channel
fn start_comparing(&self, channel: usize) -> Result<(), ErrorCode> {
if channel >= self.channels.len() {
return Err(ErrorCode::INVAL);
}
// Convert channel index
let chan = self.channels[channel];
let result = self.analog_comparator.start_comparing(chan);
result
}
// Stop comparing on a channel
fn stop_comparing(&self, channel: usize) -> Result<(), ErrorCode> {
if channel >= self.channels.len() {
return Err(ErrorCode::INVAL);
}
// Convert channel index
let chan = self.channels[channel];
let result = self.analog_comparator.stop_comparing(chan);
result
}
}
impl<'a, A: hil::analog_comparator::AnalogComparator<'a>> SyscallDriver
for AnalogComparator<'a, A>
{
/// Control the analog comparator.
///
/// ### `command_num`
///
/// - `0`: Driver existence check.
/// - `1`: Perform a simple comparison.
/// Input x chooses the desired comparator ACx (e.g. 0 or 1 for
/// hail, 0-3 for imix)
/// - `2`: Start interrupt-based comparisons.
/// Input x chooses the desired comparator ACx (e.g. 0 or 1 for
/// hail, 0-3 for imix)
/// - `3`: Stop interrupt-based comparisons.
/// Input x chooses the desired comparator ACx (e.g. 0 or 1 for
/// hail, 0-3 for imix)
/// - `4`: Get number of channels.
fn command(
&self,
command_num: usize,
channel: usize,
_: usize,
processid: ProcessId,
) -> CommandReturn {
if command_num == 0 {
// Handle unconditional driver existence check.
return CommandReturn::success();
}
// Check if this driver is free, or already dedicated to this process.
let match_or_empty_or_nonexistant = self.current_process.map_or(true, |current_process| {
self.grants
.enter(current_process, |_, _| current_process == processid)
.unwrap_or(true)
});
if match_or_empty_or_nonexistant {
self.current_process.set(processid);
} else {
return CommandReturn::failure(ErrorCode::NOMEM);
}
match command_num {
0 => CommandReturn::success_u32(self.channels.len() as u32),
1 => match self.comparison(channel) {
Ok(b) => CommandReturn::success_u32(b as u32),
Err(e) => CommandReturn::failure(e),
},
2 => self.start_comparing(channel).into(),
3 => self.stop_comparing(channel).into(),
4 => CommandReturn::success_u32(self.channels.len() as u32),
_ => CommandReturn::failure(ErrorCode::NOSUPPORT),
}
}
fn allocate_grant(&self, processid: ProcessId) -> Result<(), kernel::process::Error> {
self.grants.enter(processid, |_, _| {})
}
}
impl<'a, A: hil::analog_comparator::AnalogComparator<'a>> hil::analog_comparator::Client
for AnalogComparator<'a, A>
{
/// Upcall to userland, signaling the application
fn fired(&self, channel: usize) {
self.current_process.map(|processid| {
let _ = self.grants.enter(processid, |_app, upcalls| {
upcalls.schedule_upcall(0, (channel, 0, 0)).ok();
});
});
}
}