Skip to content
Open
Show file tree
Hide file tree
Changes from 5 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(),
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
(channel as usize).try_into().unwrap(),
channel as u32,

I would avoid the panic as most probably usize will be 32 bits.

@jrvanwhy I would like your input on this.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Without looking at the entire CL: it looks like read_single_sample should take channel as a u32.

One question to ask that helps decide usize versus u32 is "in the unit test environment, which runs on 64-bit platforms, should this number get larger?" I don't think a unit test environment should support more channels than the real hardware, so this should be a u32. usize makes sense for memory addresses and offsets, but not necessarily for other hardware addresses.

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 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
14 changes: 8 additions & 6 deletions examples/adc.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,13 +19,15 @@ 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
for i in 0..2 {
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();
}
}
}