diff options
| author | Raul Alimbekov <[email protected]> | 2025-12-16 09:05:22 +0300 |
|---|---|---|
| committer | GitHub <[email protected]> | 2025-12-16 09:05:22 +0300 |
| commit | c9a04b4b732b7a3b696eb8223664c1a7942b1875 (patch) | |
| tree | 6dbe5c02e66eed8d8762f13f95afd24f8db2b38c /examples/mcxa/src/bin/adc_polling.rs | |
| parent | cde24a3ef1117653ba5ed4184102b33f745782fb (diff) | |
| parent | 5ae6e060ec1c90561719aabdc29d5b6e7b8b0a82 (diff) | |
Merge branch 'main' into main
Diffstat (limited to 'examples/mcxa/src/bin/adc_polling.rs')
| -rw-r--r-- | examples/mcxa/src/bin/adc_polling.rs | 78 |
1 files changed, 78 insertions, 0 deletions
diff --git a/examples/mcxa/src/bin/adc_polling.rs b/examples/mcxa/src/bin/adc_polling.rs new file mode 100644 index 000000000..5c4d5524c --- /dev/null +++ b/examples/mcxa/src/bin/adc_polling.rs | |||
| @@ -0,0 +1,78 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use embassy_mcxa::adc::{ConvCommandConfig, ConvTriggerConfig}; | ||
| 6 | use embassy_time::{Duration, Ticker}; | ||
| 7 | use hal::adc::{Adc, LpadcConfig, TriggerPriorityPolicy}; | ||
| 8 | use hal::clocks::PoweredClock; | ||
| 9 | use hal::clocks::config::Div8; | ||
| 10 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | ||
| 11 | use hal::config::Config; | ||
| 12 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; | ||
| 13 | use hal::pac::adc1::cmdl1::Mode; | ||
| 14 | use hal::pac::adc1::ctrl::CalAvgs; | ||
| 15 | use hal::pac::adc1::tctrl::Tcmd; | ||
| 16 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 17 | |||
| 18 | const G_LPADC_RESULT_SHIFT: u32 = 0; | ||
| 19 | |||
| 20 | #[embassy_executor::main] | ||
| 21 | async fn main(_spawner: Spawner) { | ||
| 22 | let mut config = Config::default(); | ||
| 23 | config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1); | ||
| 24 | |||
| 25 | let p = hal::init(config); | ||
| 26 | |||
| 27 | defmt::info!("=== ADC polling Example ==="); | ||
| 28 | |||
| 29 | let adc_config = LpadcConfig { | ||
| 30 | enable_in_doze_mode: true, | ||
| 31 | conversion_average_mode: CalAvgs::Average128, | ||
| 32 | enable_analog_preliminary: true, | ||
| 33 | power_up_delay: 0x80, | ||
| 34 | reference_voltage_source: Refsel::Option3, | ||
| 35 | power_level_mode: Pwrsel::Lowest, | ||
| 36 | trigger_priority_policy: TriggerPriorityPolicy::ConvPreemptImmediatelyNotAutoResumed, | ||
| 37 | enable_conv_pause: false, | ||
| 38 | conv_pause_delay: 0, | ||
| 39 | power: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 40 | source: AdcClockSel::FroLfDiv, | ||
| 41 | div: Div4::no_div(), | ||
| 42 | }; | ||
| 43 | let adc = Adc::new_blocking(p.ADC1, p.P1_10, adc_config).unwrap(); | ||
| 44 | |||
| 45 | adc.do_offset_calibration(); | ||
| 46 | adc.do_auto_calibration(); | ||
| 47 | |||
| 48 | let conv_command_config = ConvCommandConfig { | ||
| 49 | conversion_resolution_mode: Mode::Data16Bits, | ||
| 50 | ..ConvCommandConfig::default() | ||
| 51 | }; | ||
| 52 | adc.set_conv_command_config(1, &conv_command_config).unwrap(); | ||
| 53 | |||
| 54 | let conv_trigger_config = ConvTriggerConfig { | ||
| 55 | target_command_id: Tcmd::ExecuteCmd1, | ||
| 56 | enable_hardware_trigger: false, | ||
| 57 | ..Default::default() | ||
| 58 | }; | ||
| 59 | adc.set_conv_trigger_config(0, &conv_trigger_config).unwrap(); | ||
| 60 | |||
| 61 | defmt::info!("=== ADC configuration done... ==="); | ||
| 62 | let mut tick = Ticker::every(Duration::from_millis(100)); | ||
| 63 | |||
| 64 | loop { | ||
| 65 | tick.next().await; | ||
| 66 | adc.do_software_trigger(1).unwrap(); | ||
| 67 | let result = loop { | ||
| 68 | match adc.get_conv_result() { | ||
| 69 | Ok(res) => break res, | ||
| 70 | Err(_) => { | ||
| 71 | // Conversion not ready, continue polling | ||
| 72 | } | ||
| 73 | } | ||
| 74 | }; | ||
| 75 | let value = result.conv_value >> G_LPADC_RESULT_SHIFT; | ||
| 76 | defmt::info!("ADC value: {=u16}", value); | ||
| 77 | } | ||
| 78 | } | ||
