aboutsummaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorJames Munns <[email protected]>2025-11-25 18:08:13 +0100
committerGitHub <[email protected]>2025-11-25 18:08:13 +0100
commit05bf2d0438fe8c41d0fe26b85106ff73a6e273e9 (patch)
tree8a2969ac89735154f8090d629d9c37814a25190a /examples
parent7ee5cb570f0c0daeb2e6a9d5120fd96ee885025f (diff)
Wire up Lpuart pins (#43)
Allow the HAL lpuart driver to correctly configure pins as requested on init.
Diffstat (limited to 'examples')
-rw-r--r--examples/Cargo.lock2
-rw-r--r--examples/src/bin/adc_interrupt.rs26
-rw-r--r--examples/src/bin/adc_polling.rs31
-rw-r--r--examples/src/bin/hello.rs3
-rw-r--r--examples/src/bin/lpuart_buffered.rs29
-rw-r--r--examples/src/bin/lpuart_polling.rs12
-rw-r--r--examples/src/bin/rtc_alarm.rs33
-rw-r--r--examples/src/lib.rs9
8 files changed, 28 insertions, 117 deletions
diff --git a/examples/Cargo.lock b/examples/Cargo.lock
index 56ae41d48..263807120 100644
--- a/examples/Cargo.lock
+++ b/examples/Cargo.lock
@@ -442,7 +442,7 @@ checksum = "11d3d7f243d5c5a8b9bb5d6dd2b1602c0cb0b9db1621bafc7ed66e35ff9fe092"
442[[package]] 442[[package]]
443name = "mcxa-pac" 443name = "mcxa-pac"
444version = "0.1.0" 444version = "0.1.0"
445source = "git+https://github.com/OpenDevicePartnership/mcxa-pac#9a857ec9780527679978b42cc60288aeef03baa2" 445source = "git+https://github.com/OpenDevicePartnership/mcxa-pac#e18dfb52500ca77b8d8326662b966a80251182ca"
446dependencies = [ 446dependencies = [
447 "cortex-m", 447 "cortex-m",
448 "cortex-m-rt", 448 "cortex-m-rt",
diff --git a/examples/src/bin/adc_interrupt.rs b/examples/src/bin/adc_interrupt.rs
index 0d3a75a28..83d8046b3 100644
--- a/examples/src/bin/adc_interrupt.rs
+++ b/examples/src/bin/adc_interrupt.rs
@@ -6,7 +6,6 @@ use embassy_mcxa_examples::init_adc_pins;
6use hal::adc::{LpadcConfig, TriggerPriorityPolicy}; 6use hal::adc::{LpadcConfig, TriggerPriorityPolicy};
7use hal::clocks::periph_helpers::{AdcClockSel, Div4}; 7use hal::clocks::periph_helpers::{AdcClockSel, Div4};
8use hal::clocks::PoweredClock; 8use hal::clocks::PoweredClock;
9use hal::lpuart::{Config, Lpuart};
10use hal::pac::adc1::cfg::{Pwrsel, Refsel}; 9use hal::pac::adc1::cfg::{Pwrsel, Refsel};
11use hal::pac::adc1::cmdl1::{Adch, Mode}; 10use hal::pac::adc1::cmdl1::{Adch, Mode};
12use hal::pac::adc1::ctrl::CalAvgs; 11use hal::pac::adc1::ctrl::CalAvgs;
@@ -26,26 +25,7 @@ static KEEP_ADC: unsafe extern "C" fn() = ADC1;
26async fn main(_spawner: Spawner) { 25async fn main(_spawner: Spawner) {
27 let p = hal::init(hal::config::Config::default()); 26 let p = hal::init(hal::config::Config::default());
28 27
29 // Create UART configuration 28 defmt::info!("ADC interrupt Example");
30 let config = Config {
31 baudrate_bps: 115_200,
32 enable_tx: true,
33 enable_rx: true,
34 ..Default::default()
35 };
36
37 // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX
38 unsafe {
39 embassy_mcxa_examples::init_uart2_pins();
40 }
41 let mut uart = Lpuart::new_blocking(
42 p.LPUART2, // Peripheral
43 p.P2_2, // TX pin
44 p.P2_3, // RX pin
45 config,
46 )
47 .unwrap();
48 uart.write_str_blocking("\r\n=== ADC interrupt Example ===\r\n");
49 29
50 unsafe { 30 unsafe {
51 init_adc_pins(); 31 init_adc_pins();
@@ -81,7 +61,7 @@ async fn main(_spawner: Spawner) {
81 conv_trigger_config.enable_hardware_trigger = false; 61 conv_trigger_config.enable_hardware_trigger = false;
82 adc.set_conv_trigger_config(0, &conv_trigger_config); 62 adc.set_conv_trigger_config(0, &conv_trigger_config);
83 63
84 uart.write_str_blocking("\r\n=== ADC configuration done... ===\r\n"); 64 defmt::info!("ADC configuration done...");
85 65
86 adc.enable_interrupt(0x1); 66 adc.enable_interrupt(0x1);
87 67
@@ -98,7 +78,7 @@ async fn main(_spawner: Spawner) {
98 while !adc.is_interrupt_triggered() { 78 while !adc.is_interrupt_triggered() {
99 // Wait until the interrupt is triggered 79 // Wait until the interrupt is triggered
100 } 80 }
101 uart.write_str_blocking("\r\n*** ADC interrupt TRIGGERED! ***\r\n"); 81 defmt::info!("*** ADC interrupt TRIGGERED! ***");
102 //TBD need to print the value 82 //TBD need to print the value
103 } 83 }
104} 84}
diff --git a/examples/src/bin/adc_polling.rs b/examples/src/bin/adc_polling.rs
index 02ac321b5..ddf3f586b 100644
--- a/examples/src/bin/adc_polling.rs
+++ b/examples/src/bin/adc_polling.rs
@@ -1,19 +1,15 @@
1#![no_std] 1#![no_std]
2#![no_main] 2#![no_main]
3 3
4use core::fmt::Write;
5
6use embassy_executor::Spawner; 4use embassy_executor::Spawner;
7use embassy_mcxa_examples::{init_adc_pins, init_uart2_pins}; 5use embassy_mcxa_examples::init_adc_pins;
8use hal::adc::{ConvResult, LpadcConfig, TriggerPriorityPolicy}; 6use hal::adc::{ConvResult, LpadcConfig, TriggerPriorityPolicy};
9use hal::clocks::periph_helpers::{AdcClockSel, Div4}; 7use hal::clocks::periph_helpers::{AdcClockSel, Div4};
10use hal::clocks::PoweredClock; 8use hal::clocks::PoweredClock;
11use hal::lpuart::{Config, Lpuart};
12use hal::pac::adc1::cfg::{Pwrsel, Refsel}; 9use hal::pac::adc1::cfg::{Pwrsel, Refsel};
13use hal::pac::adc1::cmdl1::{Adch, Mode}; 10use hal::pac::adc1::cmdl1::{Adch, Mode};
14use hal::pac::adc1::ctrl::CalAvgs; 11use hal::pac::adc1::ctrl::CalAvgs;
15use hal::pac::adc1::tctrl::Tcmd; 12use hal::pac::adc1::tctrl::Tcmd;
16use heapless::String;
17use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; 13use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
18 14
19const G_LPADC_RESULT_SHIFT: u32 = 0; 15const G_LPADC_RESULT_SHIFT: u32 = 0;
@@ -22,28 +18,11 @@ const G_LPADC_RESULT_SHIFT: u32 = 0;
22async fn main(_spawner: Spawner) { 18async fn main(_spawner: Spawner) {
23 let p = hal::init(hal::config::Config::default()); 19 let p = hal::init(hal::config::Config::default());
24 20
25 // Create UART configuration
26 let config = Config {
27 baudrate_bps: 115_200,
28 enable_tx: true,
29 enable_rx: true,
30 ..Default::default()
31 };
32
33 // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX
34 unsafe { 21 unsafe {
35 init_uart2_pins();
36 init_adc_pins(); 22 init_adc_pins();
37 } 23 }
38 let mut uart = Lpuart::new_blocking(
39 p.LPUART2, // Peripheral
40 p.P2_2, // TX pin
41 p.P2_3, // RX pin
42 config,
43 )
44 .unwrap();
45 24
46 uart.write_str_blocking("\r\n=== ADC polling Example ===\r\n"); 25 defmt::info!("=== ADC polling Example ===");
47 26
48 let adc_config = LpadcConfig { 27 let adc_config = LpadcConfig {
49 enable_in_doze_mode: true, 28 enable_in_doze_mode: true,
@@ -75,7 +54,7 @@ async fn main(_spawner: Spawner) {
75 conv_trigger_config.enable_hardware_trigger = false; 54 conv_trigger_config.enable_hardware_trigger = false;
76 adc.set_conv_trigger_config(0, &conv_trigger_config); 55 adc.set_conv_trigger_config(0, &conv_trigger_config);
77 56
78 uart.write_str_blocking("\r\n=== ADC configuration done... ===\r\n"); 57 defmt::info!("=== ADC configuration done... ===");
79 58
80 loop { 59 loop {
81 adc.do_software_trigger(1); 60 adc.do_software_trigger(1);
@@ -84,8 +63,6 @@ async fn main(_spawner: Spawner) {
84 result = hal::adc::get_conv_result(); 63 result = hal::adc::get_conv_result();
85 } 64 }
86 let value = result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT; 65 let value = result.unwrap().conv_value >> G_LPADC_RESULT_SHIFT;
87 let mut buf: String<16> = String::new(); // adjust size as needed 66 defmt::info!("value: {=u16}", value);
88 write!(buf, "\r\nvalue: {}\r\n", value).unwrap();
89 uart.write_str_blocking(&buf);
90 } 67 }
91} 68}
diff --git a/examples/src/bin/hello.rs b/examples/src/bin/hello.rs
index 0362480c1..f426d1898 100644
--- a/examples/src/bin/hello.rs
+++ b/examples/src/bin/hello.rs
@@ -27,9 +27,6 @@ async fn main(_spawner: Spawner) {
27 }; 27 };
28 28
29 // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX 29 // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX
30 unsafe {
31 embassy_mcxa_examples::init_uart2_pins();
32 }
33 let mut uart = Lpuart::new_blocking( 30 let mut uart = Lpuart::new_blocking(
34 p.LPUART2, // Peripheral 31 p.LPUART2, // Peripheral
35 p.P2_2, // TX pin 32 p.P2_2, // TX pin
diff --git a/examples/src/bin/lpuart_buffered.rs b/examples/src/bin/lpuart_buffered.rs
index 4c9294f57..7f77d557d 100644
--- a/examples/src/bin/lpuart_buffered.rs
+++ b/examples/src/bin/lpuart_buffered.rs
@@ -2,39 +2,28 @@
2#![no_main] 2#![no_main]
3 3
4use embassy_executor::Spawner; 4use embassy_executor::Spawner;
5use embassy_mcxa as hal; 5use embassy_mcxa::clocks::config::Div8;
6use embassy_mcxa::interrupt::typelevel::Handler;
7use embassy_mcxa::lpuart::buffered::BufferedLpuart; 6use embassy_mcxa::lpuart::buffered::BufferedLpuart;
8use embassy_mcxa::lpuart::Config; 7use embassy_mcxa::lpuart::Config;
9use embassy_mcxa::{bind_interrupts, lpuart}; 8use embassy_mcxa::{bind_interrupts, lpuart};
10use embassy_mcxa_examples::init_uart2_pins; 9use embedded_io_async::Write;
11use embedded_io_async::{Read, Write}; 10use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
12 11
13// Bind OS_EVENT for timers plus LPUART2 IRQ for the buffered driver 12// Bind OS_EVENT for timers plus LPUART2 IRQ for the buffered driver
14bind_interrupts!(struct Irqs { 13bind_interrupts!(struct Irqs {
15 LPUART2 => lpuart::buffered::BufferedInterruptHandler::<hal::peripherals::LPUART2>; 14 LPUART2 => lpuart::buffered::BufferedInterruptHandler::<hal::peripherals::LPUART2>;
16}); 15});
17 16
18// Wrapper function for the interrupt handler
19unsafe extern "C" fn lpuart2_handler() {
20 lpuart::buffered::BufferedInterruptHandler::<hal::peripherals::LPUART2>::on_interrupt();
21}
22
23#[embassy_executor::main] 17#[embassy_executor::main]
24async fn main(_spawner: Spawner) { 18async fn main(_spawner: Spawner) {
25 let p = hal::init(hal::config::Config::default()); 19 let mut cfg = hal::config::Config::default();
26 20 cfg.clock_cfg.sirc.fro_12m_enabled = true;
27 unsafe { 21 cfg.clock_cfg.sirc.fro_lf_div = Some(Div8::no_div());
28 hal::interrupt::install_irq_handler(hal::pac::Interrupt::LPUART2, lpuart2_handler); 22 let p = hal::init(cfg);
29 }
30 23
31 // Configure NVIC for LPUART2 24 // Configure NVIC for LPUART2
32 hal::interrupt::LPUART2.configure_for_uart(hal::interrupt::Priority::P3); 25 hal::interrupt::LPUART2.configure_for_uart(hal::interrupt::Priority::P3);
33 26
34 unsafe {
35 init_uart2_pins();
36 }
37
38 // UART configuration (enable both TX and RX) 27 // UART configuration (enable both TX and RX)
39 let config = Config { 28 let config = Config {
40 baudrate_bps: 115_200, 29 baudrate_bps: 115_200,
@@ -69,7 +58,7 @@ async fn main(_spawner: Spawner) {
69 // Echo loop 58 // Echo loop
70 let mut buf = [0u8; 4]; 59 let mut buf = [0u8; 4];
71 loop { 60 loop {
72 rx.read_exact(&mut buf[..]).await.unwrap(); 61 let used = rx.read(&mut buf).await.unwrap();
73 tx.write_all(&buf[..]).await.unwrap(); 62 tx.write_all(&buf[..used]).await.unwrap();
74 } 63 }
75} 64}
diff --git a/examples/src/bin/lpuart_polling.rs b/examples/src/bin/lpuart_polling.rs
index c8666e64a..9cea418cc 100644
--- a/examples/src/bin/lpuart_polling.rs
+++ b/examples/src/bin/lpuart_polling.rs
@@ -2,22 +2,20 @@
2#![no_main] 2#![no_main]
3 3
4use embassy_executor::Spawner; 4use embassy_executor::Spawner;
5use embassy_mcxa_examples::init_uart2_pins; 5use embassy_mcxa::clocks::config::Div8;
6use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; 6use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
7 7
8use crate::hal::lpuart::{Config, Lpuart}; 8use crate::hal::lpuart::{Config, Lpuart};
9 9
10#[embassy_executor::main] 10#[embassy_executor::main]
11async fn main(_spawner: Spawner) { 11async fn main(_spawner: Spawner) {
12 let p = hal::init(hal::config::Config::default()); 12 let mut cfg = hal::config::Config::default();
13 cfg.clock_cfg.sirc.fro_12m_enabled = true;
14 cfg.clock_cfg.sirc.fro_lf_div = Some(Div8::no_div());
15 let p = hal::init(cfg);
13 16
14 defmt::info!("boot"); 17 defmt::info!("boot");
15 18
16 // Board-level init for UART2 clocks and pins.
17 unsafe {
18 init_uart2_pins();
19 }
20
21 // Create UART configuration 19 // Create UART configuration
22 let config = Config { 20 let config = Config {
23 baudrate_bps: 115_200, 21 baudrate_bps: 115_200,
diff --git a/examples/src/bin/rtc_alarm.rs b/examples/src/bin/rtc_alarm.rs
index 6f8a77101..a7800a2d1 100644
--- a/examples/src/bin/rtc_alarm.rs
+++ b/examples/src/bin/rtc_alarm.rs
@@ -3,7 +3,6 @@
3 3
4use embassy_executor::Spawner; 4use embassy_executor::Spawner;
5use embassy_mcxa as hal; 5use embassy_mcxa as hal;
6use hal::lpuart::{Config, Lpuart};
7use hal::rtc::{RtcDateTime, RtcInterruptEnable}; 6use hal::rtc::{RtcDateTime, RtcInterruptEnable};
8use hal::InterruptExt; 7use hal::InterruptExt;
9 8
@@ -24,27 +23,7 @@ static KEEP_RTC: unsafe extern "C" fn() = RTC;
24async fn main(_spawner: Spawner) { 23async fn main(_spawner: Spawner) {
25 let p = hal::init(hal::config::Config::default()); 24 let p = hal::init(hal::config::Config::default());
26 25
27 // Create UART configuration 26 defmt::info!("=== RTC Alarm Example ===");
28 let config = Config {
29 baudrate_bps: 115_200,
30 enable_tx: true,
31 enable_rx: true,
32 ..Default::default()
33 };
34
35 // Create UART instance using LPUART2 with P2_2 as TX and P2_3 as RX
36 unsafe {
37 embassy_mcxa_examples::init_uart2_pins();
38 }
39 let mut uart = Lpuart::new_blocking(
40 p.LPUART2, // Peripheral
41 p.P2_2, // TX pin
42 p.P2_3, // RX pin
43 config,
44 )
45 .unwrap();
46
47 uart.write_str_blocking("\r\n=== RTC Alarm Example ===\r\n");
48 27
49 let rtc_config = hal::rtc::get_default_config(); 28 let rtc_config = hal::rtc::get_default_config();
50 29
@@ -61,14 +40,14 @@ async fn main(_spawner: Spawner) {
61 40
62 rtc.stop(); 41 rtc.stop();
63 42
64 uart.write_str_blocking("Time set to: 2025-10-15 14:30:00\r\n"); 43 defmt::info!("Time set to: 2025-10-15 14:30:00");
65 rtc.set_datetime(now); 44 rtc.set_datetime(now);
66 45
67 let mut alarm = now; 46 let mut alarm = now;
68 alarm.second += 10; 47 alarm.second += 10;
69 48
70 rtc.set_alarm(alarm); 49 rtc.set_alarm(alarm);
71 uart.write_str_blocking("Alarm set for: 2025-10-15 14:30:10 (+10 seconds)\r\n"); 50 defmt::info!("Alarm set for: 2025-10-15 14:30:10 (+10 seconds)");
72 51
73 rtc.set_interrupt(RtcInterruptEnable::RTC_ALARM_INTERRUPT_ENABLE); 52 rtc.set_interrupt(RtcInterruptEnable::RTC_ALARM_INTERRUPT_ENABLE);
74 53
@@ -82,14 +61,14 @@ async fn main(_spawner: Spawner) {
82 61
83 rtc.start(); 62 rtc.start();
84 63
85 uart.write_str_blocking("RTC started, waiting for alarm...\r\n"); 64 defmt::info!("RTC started, waiting for alarm...");
86 65
87 loop { 66 loop {
88 if rtc.is_alarm_triggered() { 67 if rtc.is_alarm_triggered() {
89 uart.write_str_blocking("\r\n*** ALARM TRIGGERED! ***\r\n"); 68 defmt::info!("*** ALARM TRIGGERED! ***");
90 break; 69 break;
91 } 70 }
92 } 71 }
93 72
94 uart.write_str_blocking("Example complete - Test PASSED!\r\n"); 73 defmt::info!("Example complete - Test PASSED!");
95} 74}
diff --git a/examples/src/lib.rs b/examples/src/lib.rs
index f5f6124c0..2573a6adc 100644
--- a/examples/src/lib.rs
+++ b/examples/src/lib.rs
@@ -7,15 +7,6 @@
7use hal::{clocks, pins}; 7use hal::{clocks, pins};
8use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _}; 8use {defmt_rtt as _, embassy_mcxa as hal, panic_probe as _};
9 9
10/// Initialize clocks and pin muxing for UART2 debug console.
11/// Safe to call multiple times; writes are idempotent for our use.
12pub unsafe fn init_uart2_pins() {
13 // NOTE: Lpuart has been updated to properly enable + reset its own clocks.
14 // GPIO has not.
15 _ = clocks::enable_and_reset::<hal::peripherals::PORT2>(&clocks::periph_helpers::NoConfig);
16 pins::configure_uart2_pins_port2();
17}
18
19/// Initialize clocks and pin muxing for ADC. 10/// Initialize clocks and pin muxing for ADC.
20pub unsafe fn init_adc_pins() { 11pub unsafe fn init_adc_pins() {
21 // NOTE: Lpuart has been updated to properly enable + reset its own clocks. 12 // NOTE: Lpuart has been updated to properly enable + reset its own clocks.