Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

ADC API #21

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ libtock_low_level_debug = { path = "apis/low_level_debug" }
libtock_platform = { path = "platform" }
libtock_runtime = { path = "runtime" }
libtock_temperature = { path = "apis/temperature" }
libtock_adc = { path = "apis/adc"}

[profile.dev]
panic = "abort"
Expand Down Expand Up @@ -50,4 +51,5 @@ members = [
"tools/print_sizes",
"ufmt",
"unittest",
"apis/adc",
]
14 changes: 14 additions & 0 deletions apis/adc/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "libtock_adc"
version = "0.1.0"
authors = ["Tock Project Developers <tock-dev@googlegroups.com>"]
license = "MIT/Apache-2.0"
edition = "2021"
repository = "https://github.com/tock/libtock-rs"
description = "libtock adc driver"

[dependencies]
libtock_platform = { path = "../../platform" }

[dev-dependencies]
libtock_unittest = { path = "../../unittest" }
46 changes: 46 additions & 0 deletions apis/adc/src/lib.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
#![no_std]

//use core::cell::Cell;
use libtock_platform::{
share, subscribe::OneId, CommandReturn, DefaultConfig, ErrorCode, Subscribe, Syscalls, Upcall,
};

pub struct Adc<S: Syscalls>(S);

impl<S: Syscalls> Adc<S> {
pub fn count(x: &mut u32) -> Result<(), ErrorCode> {
match S::command(DRIVER_NUM, COUNT, 0, 0).get_success_u32() {
Some(value) => {
*x = value;
Ok(())
}
None => Err(ErrorCode::Fail),
}
}

pub fn exists() -> Result<(), ErrorCode> {
let mut check = 0;
match Self::count(&mut check) {
Ok(_) if check >= 1 => Ok(()),
Ok(_) => Err(ErrorCode::Fail),
Err(_) => Err(ErrorCode::Fail),
}
}
}

pub struct AdcListener<F: Fn(i32)>(pub F);
impl<F: Fn(i32)> Upcall<OneId<DRIVER_NUM, 0>> for AdcListener<F> {
fn upcall(&self, adc_val: u32, _arg1: u32, _arg2: u32) {
self.0(adc_val as i32)
}
}

// -----------------------------------------------------------------------------
// Driver number and command IDs
// -----------------------------------------------------------------------------

const DRIVER_NUM: u32 = 0x00005;

// Command IDs

const COUNT: u32 = 0;
Empty file added apis/adc/src/tests.rs
Empty file.
22 changes: 22 additions & 0 deletions examples/adc.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
#![no_main]
#![no_std]

use core::fmt::Write;
use libtock::console::Console;

use libtock::adc::Adc;
use libtock::runtime::{set_main, stack_size};

set_main! {main}
stack_size! {0x200}

fn main() {
// let mut data = 0;
match Adc::exists() {
Ok(()) => writeln!(Console::writer(), "adc driver available").unwrap(),
Err(_) => {
writeln!(Console::writer(), "adc driver unavailable").unwrap();
return;
}
}
}
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,3 +44,9 @@ pub mod temperature {
pub type Temperature = temperature::Temperature<super::runtime::TockSyscalls>;
pub use temperature::TemperatureListener;
}

pub mod adc {
use libtock_adc as adc;
pub type Adc = adc::Adc<super::runtime::TockSyscalls>;
pub use adc::AdcListener;
}