-
Notifications
You must be signed in to change notification settings - Fork 215
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add log enabled version to teensy4-blinky example
- Loading branch information
Showing
5 changed files
with
174 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters