From ffe3e5acae6c0038db4176dc7d031b57f865e07f Mon Sep 17 00:00:00 2001 From: Felipe Balbi Date: Tue, 18 Nov 2025 12:16:14 -0800 Subject: Correct gpio driver (#9) * Correct gpio driver Signed-off-by: Felipe Balbi * Simplify blinky example Make it look like every other HAL for consistency. While at that, also rename the example to match the name used by other HALs. Signed-off-by: Felipe Balbi * Add some documentation to GPIO driver Signed-off-by: Felipe Balbi * Enable GPIO clocks during HAL initialization Provide the user with working GPIO clocks. Signed-off-by: Felipe Balbi --------- Signed-off-by: Felipe Balbi Co-authored-by: Felipe Balbi --- examples/src/bin/adc_interrupt.rs | 6 +-- examples/src/bin/adc_polling.rs | 6 +-- examples/src/bin/blink.rs | 81 ----------------------------------- examples/src/bin/blinky.rs | 49 +++++++++++++++++++++ examples/src/bin/hello.rs | 6 +-- examples/src/bin/lpuart_buffered.rs | 4 +- examples/src/bin/lpuart_polling.rs | 6 +-- examples/src/bin/ostimer_alarm.rs | 6 +-- examples/src/bin/ostimer_async.rs | 6 +-- examples/src/bin/ostimer_counter.rs | 6 +-- examples/src/bin/ostimer_race_test.rs | 6 +-- examples/src/bin/rtc_alarm.rs | 6 +-- examples/src/lib.rs | 6 --- 13 files changed, 78 insertions(+), 116 deletions(-) delete mode 100644 examples/src/bin/blink.rs create mode 100644 examples/src/bin/blinky.rs (limited to 'examples') diff --git a/examples/src/bin/adc_interrupt.rs b/examples/src/bin/adc_interrupt.rs index 6812ba5d3..9fed052fd 100644 --- a/examples/src/bin/adc_interrupt.rs +++ b/examples/src/bin/adc_interrupt.rs @@ -34,14 +34,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { embassy_mcxa_examples::init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/adc_polling.rs b/examples/src/bin/adc_polling.rs index 421306e9b..545f8f77a 100644 --- a/examples/src/bin/adc_polling.rs +++ b/examples/src/bin/adc_polling.rs @@ -34,14 +34,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/blink.rs b/examples/src/bin/blink.rs deleted file mode 100644 index d8b158d50..000000000 --- a/examples/src/bin/blink.rs +++ /dev/null @@ -1,81 +0,0 @@ -#![no_std] -#![no_main] - -use embassy_executor::Spawner; -use embassy_mcxa as hal; -use embassy_mcxa::bind_interrupts; -use embassy_mcxa_examples::init_led_gpio_clocks; -use embassy_time::{Duration, Timer}; -use hal::gpio::pins::PIO3_18; -use hal::gpio::{Level, Output}; - -// Bind only OS_EVENT for timer interrupts -bind_interrupts!(struct Irqs { - OS_EVENT => hal::ostimer::time_driver::OsEventHandler; -}); - -#[used] -#[no_mangle] -static KEEP_OS_EVENT: unsafe extern "C" fn() = OS_EVENT; - -#[embassy_executor::main] -async fn main(_spawner: Spawner) { - let _p = hal::init(hal::config::Config::default()); - - unsafe { - init_led_gpio_clocks(hal::pac()); - } - - defmt::info!("Blink example"); - - // Initialize embassy-time global driver backed by OSTIMER0 - hal::ostimer::time_driver::init(hal::config::Config::default().time_interrupt_priority, 1_000_000); - - // Configure LED pin for GPIO mode - PIO3_18::set_mux_gpio(); - - let mut led = Output::new(PIO3_18::degrade(), Level::High); - - // Complex blinking pattern: SOS in Morse code - // S: ... (3 short) - // O: --- (3 long) - // S: ... (3 short) - // With pauses between letters and words - - loop { - defmt::info!("SOS"); - - // S: three short blinks - for _ in 0..3 { - led.set_low(); - Timer::after(Duration::from_millis(150)).await; - led.set_high(); - Timer::after(Duration::from_millis(150)).await; - } - - // Pause between letters - Timer::after(Duration::from_millis(300)).await; - - // O: three long blinks - for _ in 0..3 { - led.set_low(); - Timer::after(Duration::from_millis(450)).await; - led.set_high(); - Timer::after(Duration::from_millis(150)).await; - } - - // Pause between letters - Timer::after(Duration::from_millis(300)).await; - - // S: three short blinks - for _ in 0..3 { - led.set_low(); - Timer::after(Duration::from_millis(150)).await; - led.set_high(); - Timer::after(Duration::from_millis(150)).await; - } - - // Long pause between words (SOS repeats) - Timer::after(Duration::from_millis(1000)).await; - } -} diff --git a/examples/src/bin/blinky.rs b/examples/src/bin/blinky.rs new file mode 100644 index 000000000..28d83a12e --- /dev/null +++ b/examples/src/bin/blinky.rs @@ -0,0 +1,49 @@ +#![no_std] +#![no_main] + +use embassy_executor::Spawner; +use embassy_mcxa::bind_interrupts; +use embassy_time::Timer; +use hal::gpio::{Level, Output}; +use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; + +// Bind only OS_EVENT for timer interrupts +bind_interrupts!(struct Irqs { + OS_EVENT => hal::ostimer::time_driver::OsEventHandler; +}); + +#[used] +#[no_mangle] +static KEEP_OS_EVENT: unsafe extern "C" fn() = OS_EVENT; + +#[embassy_executor::main] +async fn main(_spawner: Spawner) { + let p = hal::init(hal::config::Config::default()); + + defmt::info!("Blink example"); + + // Initialize embassy-time global driver backed by OSTIMER0 + hal::ostimer::time_driver::init(hal::config::Config::default().time_interrupt_priority, 1_000_000); + + let mut red = Output::new(p.P3_18, Level::High); + let mut green = Output::new(p.P3_19, Level::High); + let mut blue = Output::new(p.P3_21, Level::High); + + loop { + defmt::info!("Toggle LEDs"); + + red.toggle(); + Timer::after_millis(250).await; + + red.toggle(); + green.toggle(); + Timer::after_millis(250).await; + + green.toggle(); + blue.toggle(); + Timer::after_millis(250).await; + blue.toggle(); + + Timer::after_millis(250).await; + } +} diff --git a/examples/src/bin/hello.rs b/examples/src/bin/hello.rs index 207c157c3..e2d0b413d 100644 --- a/examples/src/bin/hello.rs +++ b/examples/src/bin/hello.rs @@ -26,14 +26,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { embassy_mcxa_examples::init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/lpuart_buffered.rs b/examples/src/bin/lpuart_buffered.rs index 642d4af65..b0d19ef16 100644 --- a/examples/src/bin/lpuart_buffered.rs +++ b/examples/src/bin/lpuart_buffered.rs @@ -51,8 +51,8 @@ async fn main(_spawner: Spawner) { // Create a buffered LPUART2 instance with both TX and RX let mut uart = BufferedLpuart::new( p.LPUART2, - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin Irqs, &mut tx_buf, &mut rx_buf, diff --git a/examples/src/bin/lpuart_polling.rs b/examples/src/bin/lpuart_polling.rs index bea82c33e..525d42e2c 100644 --- a/examples/src/bin/lpuart_polling.rs +++ b/examples/src/bin/lpuart_polling.rs @@ -26,11 +26,11 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX let lpuart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/ostimer_alarm.rs b/examples/src/bin/ostimer_alarm.rs index 03fb93319..6d38741b7 100644 --- a/examples/src/bin/ostimer_alarm.rs +++ b/examples/src/bin/ostimer_alarm.rs @@ -40,14 +40,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/ostimer_async.rs b/examples/src/bin/ostimer_async.rs index 881f09374..f043184e7 100644 --- a/examples/src/bin/ostimer_async.rs +++ b/examples/src/bin/ostimer_async.rs @@ -29,14 +29,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/ostimer_counter.rs b/examples/src/bin/ostimer_counter.rs index 2fbc251b9..f36915ff2 100644 --- a/examples/src/bin/ostimer_counter.rs +++ b/examples/src/bin/ostimer_counter.rs @@ -30,14 +30,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { embassy_mcxa_examples::init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/ostimer_race_test.rs b/examples/src/bin/ostimer_race_test.rs index 168a952cd..0106b92a7 100644 --- a/examples/src/bin/ostimer_race_test.rs +++ b/examples/src/bin/ostimer_race_test.rs @@ -80,14 +80,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { embassy_mcxa_examples::init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/bin/rtc_alarm.rs b/examples/src/bin/rtc_alarm.rs index 40a1207df..a54b4a817 100644 --- a/examples/src/bin/rtc_alarm.rs +++ b/examples/src/bin/rtc_alarm.rs @@ -32,14 +32,14 @@ async fn main(_spawner: Spawner) { ..Default::default() }; - // Create UART instance using LPUART2 with PIO2_2 as TX and PIO2_3 as RX + // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX unsafe { embassy_mcxa_examples::init_uart2_pins(hal::pac()); } let mut uart = Lpuart::new_blocking( p.LPUART2, // Peripheral - p.PIO2_2, // TX pin - p.PIO2_3, // RX pin + p.P2_2, // TX pin + p.P2_3, // RX pin config, ) .unwrap(); diff --git a/examples/src/lib.rs b/examples/src/lib.rs index 4bb334da5..66b93450a 100644 --- a/examples/src/lib.rs +++ b/examples/src/lib.rs @@ -16,12 +16,6 @@ pub unsafe fn init_uart2_pins(_p: &hal::pac::Peripherals) { pins::configure_uart2_pins_port2(); } -/// Initialize clocks for the LED GPIO/PORT used by the blink example. -pub unsafe fn init_led_gpio_clocks(_p: &hal::pac::Peripherals) { - _ = clocks::enable_and_reset::(&clocks::periph_helpers::NoConfig); - _ = clocks::enable_and_reset::(&clocks::periph_helpers::NoConfig); -} - /// Initialize clocks and pin muxing for ADC. pub unsafe fn init_adc_pins(_p: &hal::pac::Peripherals) { // NOTE: Lpuart has been updated to properly enable + reset its own clocks. -- cgit