diff options
Diffstat (limited to 'examples/src/bin/adc_interrupt.rs')
| -rw-r--r-- | examples/src/bin/adc_interrupt.rs | 103 |
1 files changed, 103 insertions, 0 deletions
diff --git a/examples/src/bin/adc_interrupt.rs b/examples/src/bin/adc_interrupt.rs new file mode 100644 index 000000000..be08ebf8c --- /dev/null +++ b/examples/src/bin/adc_interrupt.rs | |||
| @@ -0,0 +1,103 @@ | |||
| 1 | #![no_std] | ||
| 2 | #![no_main] | ||
| 3 | |||
| 4 | use embassy_executor::Spawner; | ||
| 5 | use hal::adc::{LpadcConfig, TriggerPriorityPolicy}; | ||
| 6 | use hal::clocks::periph_helpers::{AdcClockSel, Div4}; | ||
| 7 | use hal::clocks::PoweredClock; | ||
| 8 | use hal::lpuart::{Config, Lpuart}; | ||
| 9 | use hal::pac::adc1::cfg::{Pwrsel, Refsel}; | ||
| 10 | use hal::pac::adc1::cmdl1::{Adch, Mode}; | ||
| 11 | use hal::pac::adc1::ctrl::CalAvgs; | ||
| 12 | use hal::pac::adc1::tctrl::Tcmd; | ||
| 13 | use hal::{bind_interrupts, InterruptExt}; | ||
| 14 | use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; | ||
| 15 | |||
| 16 | bind_interrupts!(struct Irqs { | ||
| 17 | ADC1 => hal::adc::AdcHandler; | ||
| 18 | }); | ||
| 19 | |||
| 20 | #[used] | ||
| 21 | #[no_mangle] | ||
| 22 | static KEEP_ADC: unsafe extern "C" fn() = ADC1; | ||
| 23 | |||
| 24 | #[embassy_executor::main] | ||
| 25 | async fn main(_spawner: Spawner) { | ||
| 26 | let p = hal::init(hal::config::Config::default()); | ||
| 27 | |||
| 28 | // Create UART configuration | ||
| 29 | let config = Config { | ||
| 30 | baudrate_bps: 115_200, | ||
| 31 | enable_tx: true, | ||
| 32 | enable_rx: true, | ||
| 33 | ..Default::default() | ||
| 34 | }; | ||
| 35 | |||
| 36 | // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX | ||
| 37 | unsafe { | ||
| 38 | embassy_mcxa_examples::init_uart2(hal::pac()); | ||
| 39 | } | ||
| 40 | let mut uart = Lpuart::new_blocking( | ||
| 41 | p.LPUART2, // Peripheral | ||
| 42 | p.PIO2_2, // TX pin | ||
| 43 | p.PIO2_3, // RX pin | ||
| 44 | config, | ||
| 45 | ) | ||
| 46 | .unwrap(); | ||
| 47 | uart.write_str_blocking("\r\n=== ADC interrupt Example ===\r\n"); | ||
| 48 | |||
| 49 | unsafe { | ||
| 50 | embassy_mcxa_examples::init_adc(hal::pac()); | ||
| 51 | } | ||
| 52 | |||
| 53 | let adc_config = LpadcConfig { | ||
| 54 | enable_in_doze_mode: true, | ||
| 55 | conversion_average_mode: CalAvgs::Average128, | ||
| 56 | enable_analog_preliminary: true, | ||
| 57 | power_up_delay: 0x80, | ||
| 58 | reference_voltage_source: Refsel::Option3, | ||
| 59 | power_level_mode: Pwrsel::Lowest, | ||
| 60 | trigger_priority_policy: TriggerPriorityPolicy::ConvPreemptImmediatelyNotAutoResumed, | ||
| 61 | enable_conv_pause: false, | ||
| 62 | conv_pause_delay: 0, | ||
| 63 | fifo_watermark: 0, | ||
| 64 | power: PoweredClock::NormalEnabledDeepSleepDisabled, | ||
| 65 | source: AdcClockSel::FroLfDiv, | ||
| 66 | div: Div4::no_div(), | ||
| 67 | }; | ||
| 68 | let adc = hal::adc::Adc::<hal::adc::Adc1>::new(p.ADC1, adc_config); | ||
| 69 | |||
| 70 | adc.do_offset_calibration(); | ||
| 71 | adc.do_auto_calibration(); | ||
| 72 | |||
| 73 | let mut conv_command_config = adc.get_default_conv_command_config(); | ||
| 74 | conv_command_config.channel_number = Adch::SelectCorrespondingChannel8; | ||
| 75 | conv_command_config.conversion_resolution_mode = Mode::Data16Bits; | ||
| 76 | adc.set_conv_command_config(1, &conv_command_config); | ||
| 77 | |||
| 78 | let mut conv_trigger_config = adc.get_default_conv_trigger_config(); | ||
| 79 | conv_trigger_config.target_command_id = Tcmd::ExecuteCmd1; | ||
| 80 | conv_trigger_config.enable_hardware_trigger = false; | ||
| 81 | adc.set_conv_trigger_config(0, &conv_trigger_config); | ||
| 82 | |||
| 83 | uart.write_str_blocking("\r\n=== ADC configuration done... ===\r\n"); | ||
| 84 | |||
| 85 | adc.enable_interrupt(0x1); | ||
| 86 | |||
| 87 | unsafe { | ||
| 88 | hal::interrupt::ADC1.enable(); | ||
| 89 | } | ||
| 90 | |||
| 91 | unsafe { | ||
| 92 | cortex_m::interrupt::enable(); | ||
| 93 | } | ||
| 94 | |||
| 95 | loop { | ||
| 96 | adc.do_software_trigger(1); | ||
| 97 | while !adc.is_interrupt_triggered() { | ||
| 98 | // Wait until the interrupt is triggered | ||
| 99 | } | ||
| 100 | uart.write_str_blocking("\r\n*** ADC interrupt TRIGGERED! ***\r\n"); | ||
| 101 | //TBD need to print the value | ||
| 102 | } | ||
| 103 | } | ||
