Skip to content

Commit

Permalink
Add log enabled version to teensy4-blinky example
Browse files Browse the repository at this point in the history
  • Loading branch information
Finomnis committed Nov 3, 2023
1 parent e862d71 commit 4dc3b43
Show file tree
Hide file tree
Showing 5 changed files with 174 additions and 2 deletions.
5 changes: 5 additions & 0 deletions examples/teensy4_blinky/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 9 additions & 1 deletion examples/teensy4_blinky/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,12 +19,20 @@ features = ["imxrt_gpt1"]

[dependencies]
embedded-hal = "0.2.7"
teensy4-panic = "0.2.3"
teensy4-panic = { version = "0.2.3", default-features = false }

[dependencies.teensy4-bsp]
features = ["rt"]
version = "0.4.4"

[dev-dependencies]
nb = "1.1.0" # Async
imxrt-log = { version = "0.1.1", default-features = false, features = [
"log",
"lpuart",
] }
log = "0.4.20"

# this lets you use `cargo fix`!
[[bin]]
name = "imxrt-blinky"
Expand Down
42 changes: 42 additions & 0 deletions examples/teensy4_blinky/examples/common/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
macro_rules! uart_panic_handler {
($uart: ident, $tx_pin: ident, $rx_pin: ident, $baud: expr) => {
#[panic_handler]
fn panic(info: &::core::panic::PanicInfo) -> ! {
use ::core::fmt::Write as _;
use ::embedded_hal::serial::Write as _;

let ::teensy4_bsp::board::Resources {
$uart: uart, pins, ..
} = ::teensy4_bsp::board::t40(unsafe { ::teensy4_bsp::ral::Instances::instances() });

let uart = ::teensy4_bsp::board::lpuart(uart, pins.$tx_pin, pins.$rx_pin, $baud);

struct UartWriter<P, const N: u8> {
uart: ::teensy4_bsp::hal::lpuart::Lpuart<P, N>,
}
impl<P, const N: u8> ::core::fmt::Write for UartWriter<P, N> {
fn write_str(&mut self, s: &str) -> ::core::fmt::Result {
for &b in s.as_bytes() {
if b == b'\n' {
let _ = ::nb::block!(self.uart.write(b'\r'));
}
let _ = ::nb::block!(self.uart.write(b));
}
Ok(())
}
}

let mut uart = UartWriter { uart };

::core::writeln!(uart).ok();
::core::writeln!(uart, "{}", info).ok();
::core::writeln!(uart).ok();

let _ = ::nb::block!(uart.uart.flush());

::teensy4_panic::sos()
}
};
}

pub(crate) use uart_panic_handler;
113 changes: 113 additions & 0 deletions examples/teensy4_blinky/examples/with_logs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,113 @@
#![deny(warnings)]
#![no_main]
#![no_std]
#![feature(type_alias_impl_trait)]

mod common;
common::uart_panic_handler!(lpuart6, p1, p0, 115200);

use teensy4_bsp as bsp;

use bsp::board;
use bsp::hal;
use bsp::logging;

use embedded_hal::serial::Write;

use rtic_monotonics::imxrt::Gpt1 as Mono;
use rtic_monotonics::imxrt::*;
use rtic_monotonics::Monotonic;

#[rtic::app(device = teensy4_bsp, dispatchers = [LPSPI1])]
mod app {
use super::*;

const LOG_POLL_INTERVAL: u32 = board::PERCLK_FREQUENCY / 100;
const LOG_DMA_CHANNEL: usize = 0;

#[shared]
struct Shared {}

#[local]
struct Local {
led: board::Led,
poll_log: hal::pit::Pit<3>,
log_poller: logging::Poller,
}

#[init]
fn init(cx: init::Context) -> (Shared, Local) {
let board::Resources {
mut dma,
pit: (_, _, _, mut poll_log),
pins,
lpuart6,
mut gpio2,
mut gpt1,
..
} = board::t40(cx.device);

// Logging
let log_dma = dma[LOG_DMA_CHANNEL].take().unwrap();
let mut log_uart = board::lpuart(lpuart6, pins.p1, pins.p0, 115200);
for &ch in "\r\n===== Teensy4 Rtic Blinky =====\r\n\r\n".as_bytes() {
nb::block!(log_uart.write(ch)).unwrap();
}
nb::block!(log_uart.flush()).unwrap();
let log_poller =
logging::log::lpuart(log_uart, log_dma, logging::Interrupts::Enabled).unwrap();
poll_log.set_interrupt_enable(true);
poll_log.set_load_timer_value(LOG_POLL_INTERVAL);
poll_log.enable();

// Initialize the systick interrupt & obtain the token to prove that we did
gpt1.set_clock_source(hal::gpt::ClockSource::PeripheralClock);
let gpt1_mono_token = rtic_monotonics::create_imxrt_gpt1_token!();
Mono::start(board::PERCLK_FREQUENCY, gpt1.release(), gpt1_mono_token);

// Setup LED
let led = board::led(&mut gpio2, pins.p13);
led.set();

// Schedule the blinking task
blink::spawn().ok();

(
Shared {},
Local {
log_poller,
poll_log,
led,
},
)
}

#[task(local = [led])]
async fn blink(cx: blink::Context) {
let blink::LocalResources { led, .. } = cx.local;

let mut next_update = Mono::now();

loop {
led.toggle();
log::info!("Time: {:?}", Mono::now());
next_update += 1000.millis();
Mono::delay_until(next_update).await;
}
}

#[task(binds = PIT, priority = 1, local = [poll_log, log_poller])]
fn logger(cx: logger::Context) {
let logger::LocalResources {
poll_log,
log_poller,
..
} = cx.local;

if poll_log.is_elapsed() {
poll_log.clear_elapsed();

log_poller.poll();
}
}
}
6 changes: 5 additions & 1 deletion examples/teensy4_blinky/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,12 @@
#![no_std]
#![feature(type_alias_impl_trait)]

#[panic_handler]
fn panic(_: &::core::panic::PanicInfo) -> ! {
::teensy4_panic::sos()
}

use teensy4_bsp::{board, hal};
use teensy4_panic as _;

use rtic_monotonics::imxrt::Gpt1 as Mono;
use rtic_monotonics::imxrt::*;
Expand Down

0 comments on commit 4dc3b43

Please sign in to comment.