Skip to content
Open
Show file tree
Hide file tree
Changes from 1 commit
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
24 changes: 14 additions & 10 deletions apis/peripherals/adc/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,10 +22,14 @@ 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: u32) -> Result<(), ErrorCode> {
S::command(DRIVER_NUM, SINGLE_SAMPLE, channel, 0).to_result()
}

// Register a listener to be called when the ADC conversion is finished
Expand All @@ -43,14 +47,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: u32) -> 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 +67,21 @@ 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: u32) -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_RES_BITS, channel, 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: u32) -> Result<u32, ErrorCode> {
S::command(DRIVER_NUM, GET_VOLTAGE_REF, channel, 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 u32 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
26 changes: 20 additions & 6 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,27 @@ 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(),
}
//testing for channels 0, 1 and 2
while Adc::exists().is_ok() {
match Adc::read_single_sample_sync(0) {
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();
match Adc::read_single_sample_sync(1) {
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();

match Adc::read_single_sample_sync(2) {
Ok(adc_val) => writeln!(Console::writer(), "Sample: {}\n", adc_val).unwrap(),
Err(_) => writeln!(Console::writer(), "error while reading sample",).unwrap(),
}
}
}
}