diff options
| author | Mathis Deroo <[email protected]> | 2025-12-05 14:37:19 -0800 |
|---|---|---|
| committer | Mathis Deroo <[email protected]> | 2025-12-09 10:51:55 -0800 |
| commit | 1ccf45058db4e77ac2c59357cab196b659201b63 (patch) | |
| tree | bfe1fa9c96db00b8551e44a67d4ecf65139d7061 /examples | |
| parent | 23623d634b88da7bc398f092ac4ab9e571c6e6e1 (diff) | |
ADC driver improvement
Signed-off-by: Mathis Deroo <[email protected]>
Diffstat (limited to 'examples')
| -rw-r--r-- | examples/mcxa/src/bin/adc_interrupt.rs | 42 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/adc_polling.rs | 18 | ||||
| -rw-r--r-- | examples/mcxa/src/lib.rs | 16 |
3 files changed, 22 insertions, 54 deletions
diff --git a/examples/mcxa/src/bin/adc_interrupt.rs b/examples/mcxa/src/bin/adc_interrupt.rs index c88b1fe8d..9db1173e3 100644 --- a/examples/mcxa/src/bin/adc_interrupt.rs +++ b/examples/mcxa/src/bin/adc_interrupt.rs | |||
| @@ -2,35 +2,33 @@ | |||
| 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, InterruptHandler}; |
| 6 | use hal::adc::{LpadcConfig, TriggerPriorityPolicy}; | ||
| 7 | use hal::clocks::PoweredClock; | 6 | use hal::clocks::PoweredClock; |
| 7 | use hal::config::Config; | ||
| 8 | use hal::clocks::config::Div8; | ||
| 8 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | 9 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; |
| 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; |
| 12 | use hal::pac::adc1::tctrl::Tcmd; | 13 | use hal::pac::adc1::tctrl::Tcmd; |
| 13 | use hal::{InterruptExt, bind_interrupts}; | 14 | use hal::bind_interrupts; |
| 15 | use hal::peripherals::ADC1; | ||
| 14 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | 16 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; |
| 15 | 17 | ||
| 16 | bind_interrupts!(struct Irqs { | 18 | bind_interrupts!(struct Irqs { |
| 17 | ADC1 => hal::adc::AdcHandler; | 19 | ADC1 => InterruptHandler<ADC1>; |
| 18 | }); | 20 | }); |
| 19 | 21 | ||
| 20 | #[used] | ||
| 21 | #[no_mangle] | ||
| 22 | static KEEP_ADC: unsafe extern "C" fn() = ADC1; | ||
| 23 | 22 | ||
| 24 | #[embassy_executor::main] | 23 | #[embassy_executor::main] |
| 25 | async fn main(_spawner: Spawner) { | 24 | async fn main(_spawner: Spawner) { |
| 26 | let p = hal::init(hal::config::Config::default()); | 25 | let mut config = Config::default(); |
| 26 | config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1); | ||
| 27 | |||
| 28 | let p = hal::init(config); | ||
| 27 | 29 | ||
| 28 | defmt::info!("ADC interrupt Example"); | 30 | defmt::info!("ADC interrupt Example"); |
| 29 | 31 | ||
| 30 | unsafe { | ||
| 31 | init_adc_pins(); | ||
| 32 | } | ||
| 33 | |||
| 34 | let adc_config = LpadcConfig { | 32 | let adc_config = LpadcConfig { |
| 35 | enable_in_doze_mode: true, | 33 | enable_in_doze_mode: true, |
| 36 | conversion_average_mode: CalAvgs::Average128, | 34 | conversion_average_mode: CalAvgs::Average128, |
| @@ -46,7 +44,7 @@ async fn main(_spawner: Spawner) { | |||
| 46 | source: AdcClockSel::FroLfDiv, | 44 | source: AdcClockSel::FroLfDiv, |
| 47 | div: Div4::no_div(), | 45 | div: Div4::no_div(), |
| 48 | }; | 46 | }; |
| 49 | let adc = hal::adc::Adc::<hal::adc::Adc1>::new(p.ADC1, adc_config); | 47 | let mut adc = Adc::new(p.ADC1, p.P1_10, Irqs, adc_config); |
| 50 | 48 | ||
| 51 | adc.do_offset_calibration(); | 49 | adc.do_offset_calibration(); |
| 52 | adc.do_auto_calibration(); | 50 | adc.do_auto_calibration(); |
| @@ -63,22 +61,8 @@ async fn main(_spawner: Spawner) { | |||
| 63 | 61 | ||
| 64 | defmt::info!("ADC configuration done..."); | 62 | defmt::info!("ADC configuration done..."); |
| 65 | 63 | ||
| 66 | adc.enable_interrupt(0x1); | ||
| 67 | |||
| 68 | unsafe { | ||
| 69 | hal::interrupt::ADC1.enable(); | ||
| 70 | } | ||
| 71 | |||
| 72 | unsafe { | ||
| 73 | cortex_m::interrupt::enable(); | ||
| 74 | } | ||
| 75 | |||
| 76 | loop { | 64 | loop { |
| 77 | adc.do_software_trigger(1); | 65 | let value = adc.read().await; |
| 78 | while !adc.is_interrupt_triggered() { | 66 | defmt::info!("*** ADC interrupt TRIGGERED! *** -- value: {}", value); |
| 79 | // Wait until the interrupt is triggered | ||
| 80 | } | ||
| 81 | defmt::info!("*** ADC interrupt TRIGGERED! ***"); | ||
| 82 | //TBD need to print the value | ||
| 83 | } | 67 | } |
| 84 | } | 68 | } |
diff --git a/examples/mcxa/src/bin/adc_polling.rs b/examples/mcxa/src/bin/adc_polling.rs index 07c50f224..65b66c8dd 100644 --- a/examples/mcxa/src/bin/adc_polling.rs +++ b/examples/mcxa/src/bin/adc_polling.rs | |||
| @@ -2,9 +2,10 @@ | |||
| 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, ConvResult, LpadcConfig, TriggerPriorityPolicy}; |
| 6 | use hal::adc::{ConvResult, LpadcConfig, TriggerPriorityPolicy}; | ||
| 7 | use hal::clocks::PoweredClock; | 6 | use hal::clocks::PoweredClock; |
| 7 | use hal::config::Config; | ||
| 8 | use hal::clocks::config::Div8; | ||
| 8 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | 9 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; |
| 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}; |
| @@ -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(); |
| 20 | 21 | config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1); | |
| 21 | unsafe { | 22 | |
| 22 | init_adc_pins(); | 23 | let p = hal::init(config); |
| 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_polling(p.ADC1, p.P1_10, adc_config); |
| 43 | 43 | ||
| 44 | adc.do_offset_calibration(); | 44 | adc.do_offset_calibration(); |
| 45 | adc.do_auto_calibration(); | 45 | adc.do_auto_calibration(); |
| @@ -60,7 +60,7 @@ async fn main(_spawner: Spawner) { | |||
| 60 | adc.do_software_trigger(1); | 60 | adc.do_software_trigger(1); |
| 61 | let mut result: Option<ConvResult> = None; | 61 | let mut result: Option<ConvResult> = None; |
| 62 | while result.is_none() { | 62 | while result.is_none() { |
| 63 | result = hal::adc::get_conv_result(); | 63 | result = adc.get_conv_result(); |
| 64 | } | 64 | } |
| 65 | let value = result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT; | 65 | let value = result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT; |
| 66 | defmt::info!("value: {=u16}", value); | 66 | defmt::info!("value: {=u16}", value); |
diff --git a/examples/mcxa/src/lib.rs b/examples/mcxa/src/lib.rs deleted file mode 100644 index 2573a6adc..000000000 --- a/examples/mcxa/src/lib.rs +++ /dev/null | |||
| @@ -1,16 +0,0 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![allow(clippy::missing_safety_doc)] | ||
| 3 | |||
| 4 | //! Shared board-specific helpers for the FRDM-MCXA276 examples. | ||
| 5 | //! These live with the examples so the HAL stays generic. | ||
| 6 | |||
| 7 | use hal::{clocks, pins}; | ||
| 8 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 9 | |||
| 10 | /// Initialize clocks and pin muxing for ADC. | ||
| 11 | pub unsafe fn init_adc_pins() { | ||
| 12 | // NOTE: Lpuart has been updated to properly enable + reset its own clocks. | ||
| 13 | // GPIO has not. | ||
| 14 | _ = clocks::enable_and_reset::<hal::peripherals::PORT1>(&clocks::periph_helpers::NoConfig); | ||
| 15 | pins::configure_adc_pins(); | ||
| 16 | } | ||
