aboutsummaryrefslogtreecommitdiff
path: root/examples/uart_interrupt.rs
diff options
context:
space:
mode:
authorBogdan Petru Chircu Mare <[email protected]>2025-10-31 21:44:48 -0700
committerBogdan Petru Chircu Mare <[email protected]>2025-10-31 21:44:48 -0700
commit0443134bc47918d2f8f0ede1b292b372629f8894 (patch)
tree823f569464a6e001b4eb4246c9ad8083c8c20847 /examples/uart_interrupt.rs
parent47e383545f4aac3bfaec0563429cc721540e665a (diff)
feat(mcxa276): initial HAL import
Diffstat (limited to 'examples/uart_interrupt.rs')
-rw-r--r--examples/uart_interrupt.rs86
1 files changed, 86 insertions, 0 deletions
diff --git a/examples/uart_interrupt.rs b/examples/uart_interrupt.rs
new file mode 100644
index 000000000..b309ce973
--- /dev/null
+++ b/examples/uart_interrupt.rs
@@ -0,0 +1,86 @@
1#![no_std]
2#![no_main]
3
4use embassy_executor::Spawner;
5use embassy_mcxa276 as hal;
6use hal::interrupt::typelevel::Handler;
7use hal::uart;
8
9mod common;
10
11#[cfg(all(feature = "defmt", feature = "defmt-rtt"))]
12use defmt_rtt as _;
13#[cfg(feature = "defmt")]
14use panic_probe as _;
15#[cfg(all(feature = "defmt", feature = "defmt-rtt"))]
16use rtt_target as _;
17
18use embassy_mcxa276::bind_interrupts;
19
20// Bind LPUART2 interrupt to our handler
21bind_interrupts!(struct Irqs {
22 LPUART2 => hal::uart::UartInterruptHandler;
23});
24
25#[used]
26#[no_mangle]
27static KEEP_LPUART2: unsafe extern "C" fn() = LPUART2;
28
29// Wrapper function for the interrupt handler
30unsafe extern "C" fn lpuart2_handler() {
31 hal::uart::UartInterruptHandler::on_interrupt();
32}
33
34#[embassy_executor::main]
35async fn main(_spawner: Spawner) {
36 let _p = hal::init(hal::config::Config::default());
37
38 // Enable/clock UART2 before touching its registers
39 unsafe {
40 common::init_uart2(hal::pac());
41 }
42 let src = unsafe { hal::clocks::uart2_src_hz(hal::pac()) };
43 let uart = uart::Uart::<uart::Lpuart2>::new(_p.LPUART2, uart::Config::new(src));
44
45 // Configure LPUART2 interrupt for UART operation BEFORE any UART usage
46 hal::interrupt::LPUART2.configure_for_uart(hal::interrupt::Priority::from(3));
47
48 // Manually install the interrupt handler
49 unsafe {
50 hal::interrupt::LPUART2.install_handler(lpuart2_handler);
51 }
52
53 // Print welcome message
54 uart.write_str_blocking("UART interrupt echo demo starting...\r\n");
55 uart.write_str_blocking("Type characters to echo them back.\r\n");
56
57 // Log using defmt if enabled
58 #[cfg(feature = "defmt")]
59 defmt::info!("UART interrupt echo demo starting...");
60
61 loop {
62 // Check if we have received any data
63 if uart.rx_data_available() {
64 if let Some(byte) = uart.try_read_byte() {
65 // Echo it back
66 uart.write_byte(byte);
67 uart.write_str_blocking(" (received)\r\n");
68 }
69 } else {
70 // No data available, wait a bit before checking again
71 cortex_m::asm::delay(12_000_000); // ~1 second at 12MHz
72 }
73 }
74}
75
76#[cfg(feature = "defmt")]
77#[export_name = "_defmt_timestamp"]
78fn defmt_timestamp(_fmt: defmt::Formatter<'_>) {
79 // Return empty timestamp for now
80}
81
82#[cfg(not(feature = "defmt"))]
83#[panic_handler]
84fn panic(_info: &core::panic::PanicInfo) -> ! {
85 loop {}
86}