aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorMathis Deroo <[email protected]>2025-12-08 14:08:34 -0800
committerMathis Deroo <[email protected]>2025-12-09 10:52:11 -0800
commit93b0f0308abee5607efae799039e0f4ccf2914b6 (patch)
tree7f83d39265c01124e4abe84a0f2632ed5d8ea8e4
parentff068148fcbc7ea148454cffa81921cc0e0da3e9 (diff)
Use Result enum for ConvResult structure and read function
Signed-off-by: Mathis Deroo <[email protected]>
-rw-r--r--embassy-mcxa/src/adc.rs24
-rw-r--r--examples/mcxa/src/bin/adc_interrupt.rs10
2 files changed, 25 insertions, 9 deletions
diff --git a/embassy-mcxa/src/adc.rs b/embassy-mcxa/src/adc.rs
index 8852fd737..81b0b02f8 100644
--- a/embassy-mcxa/src/adc.rs
+++ b/embassy-mcxa/src/adc.rs
@@ -123,6 +123,16 @@ pub struct ConvTriggerConfig {
123 pub enable_hardware_trigger: bool, 123 pub enable_hardware_trigger: bool,
124} 124}
125 125
126/// ADC Error types
127#[derive(Debug, Clone, Copy, PartialEq, Eq)]
128#[cfg_attr(feature = "defmt", derive(defmt::Format))]
129pub enum AdcError {
130 /// FIFO is empty, no conversion result available
131 FifoEmpty,
132 /// Invalid configuration
133 InvalidConfig,
134}
135
126/// Result of an ADC conversion. 136/// Result of an ADC conversion.
127/// 137///
128/// Contains the conversion value and metadata about the conversion. 138/// Contains the conversion value and metadata about the conversion.
@@ -498,16 +508,16 @@ impl<'a, I: Instance> Adc<'a, I> {
498 /// 508 ///
499 /// # Returns 509 /// # Returns
500 /// - `Some(ConvResult)` if a result is available 510 /// - `Some(ConvResult)` if a result is available
501 /// - `None` if the FIFO is empty 511 /// - `Err(AdcError::FifoEmpty)` if the FIFO is empty
502 pub fn get_conv_result(&self) -> Option<ConvResult> { 512 pub fn get_conv_result(&self) -> Result<ConvResult, AdcError> {
503 let adc = &*I::ptr(); 513 let adc = &*I::ptr();
504 let fifo = adc.resfifo0().read().bits(); 514 let fifo = adc.resfifo0().read().bits();
505 const VALID_MASK: u32 = 1 << 31; 515 const VALID_MASK: u32 = 1 << 31;
506 if fifo & VALID_MASK == 0 { 516 if fifo & VALID_MASK == 0 {
507 return None; 517 return Err(AdcError::FifoEmpty);
508 } 518 }
509 519
510 Some(ConvResult { 520 Ok(ConvResult {
511 command_id_source: (fifo >> 24) & 0x0F, 521 command_id_source: (fifo >> 24) & 0x0F,
512 loop_count_index: (fifo >> 20) & 0x0F, 522 loop_count_index: (fifo >> 20) & 0x0F,
513 trigger_id_source: (fifo >> 16) & 0x0F, 523 trigger_id_source: (fifo >> 16) & 0x0F,
@@ -527,7 +537,7 @@ impl<'a, I: Instance> Adc<'a, I> {
527 /// 537 ///
528 /// # Returns 538 /// # Returns
529 /// 16-bit ADC conversion value 539 /// 16-bit ADC conversion value
530 pub async fn read(&mut self) -> u16 { 540 pub async fn read(&mut self) -> Result<u16, AdcError> {
531 let wait = I::wait_cell().subscribe().await; 541 let wait = I::wait_cell().subscribe().await;
532 542
533 self.enable_interrupt(0x1); 543 self.enable_interrupt(0x1);
@@ -535,8 +545,8 @@ impl<'a, I: Instance> Adc<'a, I> {
535 545
536 let _ = wait.await; 546 let _ = wait.await;
537 547
538 let result = self.get_conv_result(); 548 let result = self.get_conv_result().unwrap().conv_value >> G_LPADC_RESULT_SHIFT;
539 result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT 549 Ok(result)
540 } 550 }
541} 551}
542 552
diff --git a/examples/mcxa/src/bin/adc_interrupt.rs b/examples/mcxa/src/bin/adc_interrupt.rs
index 9db1173e3..a0e392634 100644
--- a/examples/mcxa/src/bin/adc_interrupt.rs
+++ b/examples/mcxa/src/bin/adc_interrupt.rs
@@ -62,7 +62,13 @@ async fn main(_spawner: Spawner) {
62 defmt::info!("ADC configuration done..."); 62 defmt::info!("ADC configuration done...");
63 63
64 loop { 64 loop {
65 let value = adc.read().await; 65 match adc.read().await {
66 defmt::info!("*** ADC interrupt TRIGGERED! *** -- value: {}", value); 66 Ok(value) => {
67 defmt::info!("*** ADC interrupt TRIGGERED! *** -- value: {}", value);
68 }
69 Err(e) => {
70 defmt::error!("ADC read error: {:?}", e);
71 }
72 }
67 } 73 }
68} 74}