diff options
Diffstat (limited to 'examples/mcxa/src/bin/adc_polling.rs')
| -rw-r--r-- | examples/mcxa/src/bin/adc_polling.rs | 32 |
1 files changed, 18 insertions, 14 deletions
diff --git a/examples/mcxa/src/bin/adc_polling.rs b/examples/mcxa/src/bin/adc_polling.rs index 07c50f224..d048bb56f 100644 --- a/examples/mcxa/src/bin/adc_polling.rs +++ b/examples/mcxa/src/bin/adc_polling.rs | |||
| @@ -2,10 +2,11 @@ | |||
| 2 | #![no_main] | 2 | #![no_main] |
| 3 | 3 | ||
| 4 | use embassy_executor::Spawner; | 4 | use embassy_executor::Spawner; |
| 5 | use embassy_mcxa_examples::init_adc_pins; | 5 | use hal::adc::{Adc, LpadcConfig, TriggerPriorityPolicy}; |
| 6 | use hal::adc::{ConvResult, LpadcConfig, TriggerPriorityPolicy}; | ||
| 7 | use hal::clocks::PoweredClock; | 6 | use hal::clocks::PoweredClock; |
| 7 | use hal::clocks::config::Div8; | ||
| 8 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | 8 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; |
| 9 | use hal::config::Config; | ||
| 9 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; | 10 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; |
| 10 | use hal::pac::adc1::cmdl1::{Adch, Mode}; | 11 | use hal::pac::adc1::cmdl1::{Adch, Mode}; |
| 11 | use hal::pac::adc1::ctrl::CalAvgs; | 12 | use hal::pac::adc1::ctrl::CalAvgs; |
| @@ -16,11 +17,10 @@ const G_LPADC_RESULT_SHIFT: u32 = 0; | |||
| 16 | 17 | ||
| 17 | #[embassy_executor::main] | 18 | #[embassy_executor::main] |
| 18 | async fn main(_spawner: Spawner) { | 19 | async fn main(_spawner: Spawner) { |
| 19 | let p = hal::init(hal::config::Config::default()); | 20 | let mut config = Config::default(); |
| 21 | config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1); | ||
| 20 | 22 | ||
| 21 | unsafe { | 23 | let p = hal::init(config); |
| 22 | init_adc_pins(); | ||
| 23 | } | ||
| 24 | 24 | ||
| 25 | defmt::info!("=== ADC polling Example ==="); | 25 | defmt::info!("=== ADC polling Example ==="); |
| 26 | 26 | ||
| @@ -39,7 +39,7 @@ async fn main(_spawner: Spawner) { | |||
| 39 | source: AdcClockSel::FroLfDiv, | 39 | source: AdcClockSel::FroLfDiv, |
| 40 | div: Div4::no_div(), | 40 | div: Div4::no_div(), |
| 41 | }; | 41 | }; |
| 42 | let adc = hal::adc::Adc::<hal::adc::Adc1>::new(p.ADC1, adc_config); | 42 | let adc = Adc::new_blocking(p.ADC1, p.P1_10, adc_config).unwrap(); |
| 43 | 43 | ||
| 44 | adc.do_offset_calibration(); | 44 | adc.do_offset_calibration(); |
| 45 | adc.do_auto_calibration(); | 45 | adc.do_auto_calibration(); |
| @@ -47,7 +47,7 @@ async fn main(_spawner: Spawner) { | |||
| 47 | let mut conv_command_config = adc.get_default_conv_command_config(); | 47 | let mut conv_command_config = adc.get_default_conv_command_config(); |
| 48 | conv_command_config.channel_number = Adch::SelectCorrespondingChannel8; | 48 | conv_command_config.channel_number = Adch::SelectCorrespondingChannel8; |
| 49 | conv_command_config.conversion_resolution_mode = Mode::Data16Bits; | 49 | conv_command_config.conversion_resolution_mode = Mode::Data16Bits; |
| 50 | adc.set_conv_command_config(1, &conv_command_config); | 50 | adc.set_conv_command_config(1, &conv_command_config).unwrap(); |
| 51 | 51 | ||
| 52 | let mut conv_trigger_config = adc.get_default_conv_trigger_config(); | 52 | let mut conv_trigger_config = adc.get_default_conv_trigger_config(); |
| 53 | conv_trigger_config.target_command_id = Tcmd::ExecuteCmd1; | 53 | conv_trigger_config.target_command_id = Tcmd::ExecuteCmd1; |
| @@ -58,11 +58,15 @@ async fn main(_spawner: Spawner) { | |||
| 58 | 58 | ||
| 59 | loop { | 59 | loop { |
| 60 | adc.do_software_trigger(1); | 60 | adc.do_software_trigger(1); |
| 61 | let mut result: Option<ConvResult> = None; | 61 | let result = loop { |
| 62 | while result.is_none() { | 62 | match adc.get_conv_result() { |
| 63 | result = hal::adc::get_conv_result(); | 63 | Ok(res) => break res, |
| 64 | } | 64 | Err(_) => { |
| 65 | let value = result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT; | 65 | // Conversion not ready, continue polling |
| 66 | defmt::info!("value: {=u16}", value); | 66 | } |
| 67 | } | ||
| 68 | }; | ||
| 69 | let value = result.conv_value >> G_LPADC_RESULT_SHIFT; | ||
| 70 | defmt::info!("ADC value: {=u16}", value); | ||
| 67 | } | 71 | } |
| 68 | } | 72 | } |
