mciantyre / teensy4-rs

Rust support for the Teensy 4
Apache License 2.0
271 stars 31 forks source link

Expose LPUART1 resource from BSP #152

Closed mciantyre closed 8 months ago

mciantyre commented 9 months ago

Noted as missing in #151. I don't have hardware that makes this easy to test, but I ensured that modified *_uart.rs examples built as expected.

blocking_uart example with LPUART1

```rust //! Demonstrates a loopback UART peripheral. //! //! It uses the alpha board, with the following pinout: //! //! - Pin 24 is TX. //! - Pin 25 is RX. //! //! Baud rate is 115200bps. //! //! Every time you send the Teensy a character, it replies with //! that same character, and it toggles the LED. #![no_std] #![no_main] use teensy4_bsp as bsp; use teensy4_panic as _; use bsp::board; use embedded_hal::serial::{Read, Write}; #[bsp::rt::entry] fn main() -> ! { let board::Resources { pins, mut gpio2, lpuart1, .. } = board::t40(board::instances()); let led = board::led(&mut gpio2, pins.p13); let mut lpuart1: board::Lpuart1 = board::lpuart(lpuart1, pins.p24, pins.p25, 115200); loop { led.toggle(); let byte = nb::block!(lpuart1.read()).unwrap(); nb::block!(lpuart1.write(byte)).unwrap(); } } ```

rtic_uart example with LPUART1

```rust //! A loopback device. Send characters, and you should see //! the exact same characters sent back. The LED toggles for //! every exchanged character. //! //! - Pin 24 is the Teensy's TX. //! - Pin 25 is the Teensy's RX. //! //! Baud: 115200bps. #![no_std] #![no_main] use teensy4_panic as _; #[rtic::app(device = teensy4_bsp, peripherals = true)] mod app { use bsp::board; use bsp::hal::lpuart; use teensy4_bsp as bsp; #[local] struct Local { led: board::Led, lpuart1: board::Lpuart1, } #[shared] struct Shared {} #[init] fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { let board::Resources { pins, lpuart1, mut gpio2, .. } = board::t40(cx.device); let led = board::led(&mut gpio2, pins.p13); led.set(); let mut lpuart1: board::Lpuart1 = board::lpuart(lpuart1, pins.p24, pins.p25, 115200); lpuart1.disable(|lpuart1| { lpuart1.disable_fifo(lpuart::Direction::Tx); lpuart1.disable_fifo(lpuart::Direction::Rx); lpuart1.set_interrupts(lpuart::Interrupts::RECEIVE_FULL); lpuart1.set_parity(None); }); (Shared {}, Local { led, lpuart1 }, init::Monotonics()) } #[idle] fn idle(_: idle::Context) -> ! { loop { cortex_m::asm::wfi(); } } #[task(binds = LPUART1, local = [led, lpuart1])] fn lpuart1_interrupt(cx: lpuart1_interrupt::Context) { use lpuart::Status; let lpuart1 = cx.local.lpuart1; let led = cx.local.led; let status = lpuart1.status(); lpuart1.clear_status(Status::W1C); if status.contains(Status::RECEIVE_FULL) { loop { let data = lpuart1.read_data(); if data.flags().contains(lpuart::ReadFlags::RXEMPT) { break; } if lpuart1.status().contains(Status::TRANSMIT_EMPTY) { lpuart1.write_byte(data.into()); } } led.toggle(); } } } ```