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

Add ADC Channel parameter to the ADC API #555

Open
wants to merge 9 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from 6 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
42 changes: 32 additions & 10 deletions apis/peripherals/adc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,20 @@ impl<S: Syscalls> Adc<S> {
.to_result::<u32, ErrorCode>()
.and(Ok(()))
}
//Returns the number of channels
pub fn get_number_of_channels() -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, EXISTS, 0, 0).to_result::<u32, ErrorCode>()
}

// Initiate a sample reading
pub fn read_single_sample() -> Result<(), ErrorCode> {
S::command(DRIVER_NUM, SINGLE_SAMPLE, 0, 0).to_result()
pub fn read_single_sample(channel: usize) -> Result<(), ErrorCode> {
S::command(
DRIVER_NUM,
SINGLE_SAMPLE,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}

// Register a listener to be called when the ADC conversion is finished
Expand All @@ -43,14 +53,14 @@ impl<S: Syscalls> Adc<S> {

/// Initiates a synchronous ADC conversion
/// Returns the converted ADC value or an error
pub fn read_single_sample_sync() -> Result<u16, ErrorCode> {
pub fn read_single_sample_sync(channel: usize) -> Result<u16, ErrorCode> {
let sample: Cell<Option<u16>> = Cell::new(None);
let listener = ADCListener(|adc_val| {
sample.set(Some(adc_val));
});
share::scope(|subscribe| {
Self::register_listener(&listener, subscribe)?;
Self::read_single_sample()?;
Self::read_single_sample(channel)?;
while sample.get().is_none() {
S::yield_wait();
}
Expand All @@ -63,21 +73,33 @@ impl<S: Syscalls> Adc<S> {
}

/// Returns the number of ADC resolution bits
pub fn get_resolution_bits() -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_RES_BITS, 0, 0).to_result()
pub fn get_resolution_bits(channel: usize) -> Result<u32, ErrorCode> {
S::command(
DRIVER_NUM,
GET_RES_BITS,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}

/// Returns the reference voltage in millivolts (mV)
pub fn get_reference_voltage_mv() -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_VOLTAGE_REF, 0, 0).to_result()
pub fn get_reference_voltage_mv(channel: usize) -> Result<u32, ErrorCode> {
S::command(
DRIVER_NUM,
GET_VOLTAGE_REF,
(channel as usize).try_into().unwrap(),
0,
)
.to_result()
}
}

pub struct ADCListener<F: Fn(u16)>(pub F);

impl<F: Fn(u16)> Upcall<OneId<DRIVER_NUM, 0>> for ADCListener<F> {
fn upcall(&self, adc_val: u32, _arg1: u32, _arg2: u32) {
self.0(adc_val as u16)
fn upcall(&self, _adc_mode: u32, _channel: u32, sample: u32) {
self.0(sample as u16)
}
}

Expand Down
2 changes: 1 addition & 1 deletion build_scripts/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ const PLATFORMS: &[(&str, &str, &str, &str, &str)] = &[
("hifive1" , "0x20040000", "32M" , "0x80003000", "0x01000"),
("imix" , "0x00040000", "0x0040000", "0x20008000", "62K" ),
("imxrt1050" , "0x63002000", "0x1000000", "0x20004000", "112K" ),
("microbit_v2" , "0x00040000", "256K" , "0x20004000", "112K" ),
("microbit_v2" , "0x00040000", "256K" , "0x20006000", "112K" ),
("msp432" , "0x00020000", "0x0020000", "0x20004000", "0x02000"),
("nano_rp2040_connect", "0x10020000", "256K" , "0x20004000", "248K" ),
("nrf52" , "0x00030000", "0x0060000", "0x20004000", "62K" ),
Expand Down
21 changes: 15 additions & 6 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,22 @@ fn main() {
writeln!(Console::writer(), "adc driver unavailable").unwrap();
return;
}

loop {
match Adc::read_single_sample_sync() {
Ok(adc_val) => writeln!(Console::writer(), "Sample: {}\n", adc_val).unwrap(),
Err(_) => writeln!(Console::writer(), "error while reading sample",).unwrap(),
//getting the number of channels
let number_channels: usize = match Adc::get_number_of_channels() {
Ok(channel) => channel as usize,
Err(_) => {
writeln!(Console::writer(), "can't get number of channels").unwrap();
return;
}
};
loop {
for i in 0..number_channels {
match Adc::read_single_sample_sync(i) {
Ok(adc_val) => writeln!(Console::writer(), "Sample: {}\n", adc_val).unwrap(),
Err(_) => writeln!(Console::writer(), "error while reading sample",).unwrap(),
}

Alarm::sleep_for(Milliseconds(2000)).unwrap();
Alarm::sleep_for(Milliseconds(2000)).unwrap();
}
}
}
Loading