diff options
Diffstat (limited to 'examples/mcxa')
| -rw-r--r-- | examples/mcxa/src/bin/adc_interrupt.rs | 49 | ||||
| -rw-r--r-- | examples/mcxa/src/bin/adc_polling.rs | 32 | ||||
| -rw-r--r-- | examples/mcxa/src/lib.rs | 16 |
3 files changed, 37 insertions, 60 deletions
diff --git a/examples/mcxa/src/bin/adc_interrupt.rs b/examples/mcxa/src/bin/adc_interrupt.rs index c88b1fe8d..5876923a1 100644 --- a/examples/mcxa/src/bin/adc_interrupt.rs +++ b/examples/mcxa/src/bin/adc_interrupt.rs | |||
| @@ -2,34 +2,31 @@ | |||
| 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, InterruptHandler, LpadcConfig, TriggerPriorityPolicy}; |
| 6 | use hal::adc::{LpadcConfig, TriggerPriorityPolicy}; | 6 | use hal::bind_interrupts; |
| 7 | use hal::clocks::PoweredClock; | 7 | use hal::clocks::PoweredClock; |
| 8 | use hal::clocks::config::Div8; | ||
| 8 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | 9 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; |
| 10 | use hal::config::Config; | ||
| 9 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; | 11 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; |
| 10 | use hal::pac::adc1::cmdl1::{Adch, Mode}; | 12 | use hal::pac::adc1::cmdl1::{Adch, Mode}; |
| 11 | use hal::pac::adc1::ctrl::CalAvgs; | 13 | use hal::pac::adc1::ctrl::CalAvgs; |
| 12 | use hal::pac::adc1::tctrl::Tcmd; | 14 | use hal::pac::adc1::tctrl::Tcmd; |
| 13 | use hal::{InterruptExt, 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 | |||
| 24 | #[embassy_executor::main] | 22 | #[embassy_executor::main] |
| 25 | async fn main(_spawner: Spawner) { | 23 | async fn main(_spawner: Spawner) { |
| 26 | let p = hal::init(hal::config::Config::default()); | 24 | let mut config = Config::default(); |
| 25 | config.clock_cfg.sirc.fro_lf_div = Div8::from_divisor(1); | ||
| 27 | 26 | ||
| 28 | defmt::info!("ADC interrupt Example"); | 27 | let p = hal::init(config); |
| 29 | 28 | ||
| 30 | unsafe { | 29 | defmt::info!("ADC interrupt Example"); |
| 31 | init_adc_pins(); | ||
| 32 | } | ||
| 33 | 30 | ||
| 34 | let adc_config = LpadcConfig { | 31 | let adc_config = LpadcConfig { |
| 35 | enable_in_doze_mode: true, | 32 | enable_in_doze_mode: true, |
| @@ -46,7 +43,7 @@ async fn main(_spawner: Spawner) { | |||
| 46 | source: AdcClockSel::FroLfDiv, | 43 | source: AdcClockSel::FroLfDiv, |
| 47 | div: Div4::no_div(), | 44 | div: Div4::no_div(), |
| 48 | }; | 45 | }; |
| 49 | let adc = hal::adc::Adc::<hal::adc::Adc1>::new(p.ADC1, adc_config); | 46 | let mut adc = Adc::new_async(p.ADC1, p.P1_10, Irqs, adc_config).unwrap(); |
| 50 | 47 | ||
| 51 | adc.do_offset_calibration(); | 48 | adc.do_offset_calibration(); |
| 52 | adc.do_auto_calibration(); | 49 | adc.do_auto_calibration(); |
| @@ -54,7 +51,7 @@ async fn main(_spawner: Spawner) { | |||
| 54 | let mut conv_command_config = adc.get_default_conv_command_config(); | 51 | let mut conv_command_config = adc.get_default_conv_command_config(); |
| 55 | conv_command_config.channel_number = Adch::SelectCorrespondingChannel8; | 52 | conv_command_config.channel_number = Adch::SelectCorrespondingChannel8; |
| 56 | conv_command_config.conversion_resolution_mode = Mode::Data16Bits; | 53 | conv_command_config.conversion_resolution_mode = Mode::Data16Bits; |
| 57 | adc.set_conv_command_config(1, &conv_command_config); | 54 | adc.set_conv_command_config(1, &conv_command_config).unwrap(); |
| 58 | 55 | ||
| 59 | let mut conv_trigger_config = adc.get_default_conv_trigger_config(); | 56 | let mut conv_trigger_config = adc.get_default_conv_trigger_config(); |
| 60 | conv_trigger_config.target_command_id = Tcmd::ExecuteCmd1; | 57 | conv_trigger_config.target_command_id = Tcmd::ExecuteCmd1; |
| @@ -63,22 +60,14 @@ async fn main(_spawner: Spawner) { | |||
| 63 | 60 | ||
| 64 | defmt::info!("ADC configuration done..."); | 61 | defmt::info!("ADC configuration done..."); |
| 65 | 62 | ||
| 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 { | 63 | loop { |
| 77 | adc.do_software_trigger(1); | 64 | match adc.read().await { |
| 78 | while !adc.is_interrupt_triggered() { | 65 | Ok(value) => { |
| 79 | // Wait until the interrupt is triggered | 66 | defmt::info!("*** ADC interrupt TRIGGERED! *** -- value: {}", value); |
| 67 | } | ||
| 68 | Err(e) => { | ||
| 69 | defmt::error!("ADC read error: {:?}", e); | ||
| 70 | } | ||
| 80 | } | 71 | } |
| 81 | defmt::info!("*** ADC interrupt TRIGGERED! ***"); | ||
| 82 | //TBD need to print the value | ||
| 83 | } | 72 | } |
| 84 | } | 73 | } |
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 | } |
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 | } | ||
