Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Expose LPUART1 resource from BSP #152

Merged
merged 1 commit into from
Dec 26, 2023
Merged

Expose LPUART1 resource from BSP #152

merged 1 commit into from
Dec 26, 2023

Conversation

mciantyre
Copy link
Owner

@mciantyre mciantyre commented Dec 17, 2023

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

//! 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

//! 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();
        }
    }
}

@mciantyre mciantyre merged commit 7940271 into master Dec 26, 2023
@mciantyre mciantyre deleted the lpuart1 branch December 26, 2023 13:18
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

1 participant