Skip to content
New issue

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

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

Already on GitHub? # to your account

Timer V2 API (for thumbv7) #690

Open
wants to merge 3 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions boards/atsame54_xpro/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ rt = ["cortex-m-rt", "atsamd-hal/same54p-rt"]
unproven = ["atsamd-hal/unproven"]
usb = ["atsamd-hal/usb", "usb-device"]
can = ["atsamd-hal/can"]
rtic = ["atsamd-hal/rtic"]

[profile.dev]
incremental = false
Expand All @@ -52,3 +53,7 @@ opt-level = "s"
[[example]]
name = "mcan"
required-features = ["can"]

[[example]]
name = "timer_v2"
required-features = ["rtic"]
169 changes: 169 additions & 0 deletions boards/atsame54_xpro/examples/timer_v2.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,169 @@
//! Timer V2 example
//! WIP

#![no_std]
#![no_main]

use atsame54_xpro as bsp;
use bsp::hal;
use hal::clock::v2 as clock;
use hal::clock::v2::gclk::Gclk1Id;
use hal::clock::v2::types::*;
// TODO: Prelude enforces import of `fugit::ExtU32` that ruins everything..
// Temporarily remove `crate::prelude::*` import and take what's needed.
use hal::ehal::digital::v2::OutputPin as _;
use hal::eic::pin::*;
use hal::fugit::ExtU64;
use hal::gpio::{Interrupt as GpioInterrupt, *};
use hal::timer::v2::*;

use panic_rtt_target as _;
use rtt_target::{rprintln, rtt_init_print};

type Button = ExtInt15<Pin<PB31, GpioInterrupt<PullUp>>>;

const TIMER_HZ: u32 = 32;

#[rtic::app(device = hal::pac, peripherals = true, dispatchers = [FREQM])]
mod app {
use super::*;

#[monotonic(binds = TC0, default = true)]
type Mono = MonotonicTimer<TIMER_HZ, Tc0Tc1, Gclk1Id, state::Enabled>;

#[shared]
struct Shared {}

#[local]
struct Local {
countdown: CountdownTimer<TIMER_HZ, Tc7, Gclk1Id, u8, state::Enabled>,
button: Button,
led: bsp::Led,
}

#[init]
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
rtt_init_print!();
rprintln!("Application up!");

let (mut buses, clocks, tokens) = clock::clock_system_at_reset(
ctx.device.OSCCTRL,
ctx.device.OSC32KCTRL,
ctx.device.GCLK,
ctx.device.MCLK,
&mut ctx.device.NVMCTRL,
);

let (_, _, _, mut mclk) = unsafe { clocks.pac.steal() };

let pins = bsp::Pins::new(ctx.device.PORT);

let (pclk_eic, _gclk0) = clock::pclk::Pclk::enable(tokens.pclks.eic, clocks.gclk0);

let mut eic = hal::eic::init_with_ulp32k(&mut mclk, pclk_eic.into(), ctx.device.EIC);
let mut button = bsp::pin_alias!(pins.button).into_pull_up_ei();
eic.button_debounce_pins(&[button.id()]);
button.sense(&mut eic, Sense::FALL);
button.enable_interrupt(&mut eic);
eic.finalize();

let (osculp32k, _) =
clock::osculp32k::OscUlp32k::enable(tokens.osculp32k.osculp32k, clocks.osculp32k_base);

let (gclk1, _osculp32k) = clock::gclk::Gclk::from_source(tokens.gclks.gclk1, osculp32k);
let gclk1 = gclk1.enable();

let tc0_clk = buses.apb.enable(tokens.apbs.tc0);
let tc1_clk = buses.apb.enable(tokens.apbs.tc1);

let (tc0_tc1_pclk, gclk1) = clock::pclk::Pclk::enable(tokens.pclks.tc0_tc1, gclk1);

let monotonic = TimerBuilder::paired(
ctx.device.TC0,
tc0_clk,
ctx.device.TC1,
tc1_clk,
tc0_tc1_pclk,
)
.into_monotonic()
.unwrap()
.enable();

let tc6_clk = buses.apb.enable(tokens.apbs.tc6);
let tc7_clk = buses.apb.enable(tokens.apbs.tc7);

let (tc6_tc7_pclk, _gclk1) = clock::pclk::Pclk::enable(tokens.pclks.tc6_tc7, gclk1);

let (_, countdown) = TimerBuilder::disjoint(
ctx.device.TC6,
tc6_clk,
ctx.device.TC7,
tc7_clk,
tc6_tc7_pclk,
);

// TODO: Create a PR to fugit to fix extension traits
use atsamd_hal::fugit::ExtU32;
let countdown = countdown
.into_8_bit()
.into_countdown()
.unwrap()
.with_saturated_period(1_u32.secs())
.with_interrupt();

let countdown = countdown.enable();

let led: bsp::Led = bsp::pin_alias!(pins.led).into();
bump_activity_led();

(
Shared {},
Local {
button,
led,
countdown,
},
init::Monotonics(monotonic),
)
}
#[task(binds = TC7, local = [countdown])]
fn countdown(ctx: countdown::Context) {
ctx.local.countdown.clear_interrupt_flag();
rprintln!("Countdown timer run out!");
bump_activity_led();
}

#[task(binds = EIC_EXTINT_15, local = [button])]
fn button(ctx: button::Context) {
ctx.local.button.clear_interrupt();

rprintln!("Button pressed!");
hello::spawn().unwrap();
hello::spawn_after(50.millis()).unwrap();
hello::spawn_after(100.millis()).unwrap();
hello::spawn_after(500.millis()).unwrap();
hello::spawn_after(1.secs()).unwrap();
hello::spawn_after(5.secs()).unwrap();
}

#[task(capacity = 30)]
fn hello(_: hello::Context) {
bump_activity_led();
rprintln!(
"Hello at {} ms since epoch!",
monotonics::now().duration_since_epoch().to_millis()
);
}

#[task(local = [led])]
fn activity_led(ctx: activity_led::Context, led_on: bool) {
let _ = ctx.local.led.set_state((!led_on).into());
if led_on {
let _ = activity_led::spawn_after(100.millis(), false);
}
}

fn bump_activity_led() {
let _ = activity_led::spawn(true);
}
}
2 changes: 2 additions & 0 deletions hal/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -40,13 +40,15 @@ fugit = "0.3"
modular-bitfield = "0.11"
nb = "1.0"
num-traits = {version = "0.2.14", default-features = false}
num-derive = "0.3"
opaque-debug = "0.3.0"
paste = "1.0.11"
rand_core = "0.6"
seq-macro = "0.3"
typenum = "1.12.0"
vcell = "0.1"
void = {version = "1.0", default-features = false}
rtt-target = { version = "0.3", features = ["cortex-m"] }

#===============================================================================
# Optional depdendencies
Expand Down
3 changes: 3 additions & 0 deletions hal/src/prelude.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ pub use crate::timer_traits::InterruptDrivenTimer as _atsamd_hal_timer_traits_In
pub use fugit::ExtU32 as _;
pub use fugit::RateExtU32 as _;

#[cfg(feature = "thumbv7")]
pub use crate::timer::v2::prelude::*;

// embedded-hal doesn’t yet have v2 in its prelude, so we need to
// export it ourselves
#[cfg(feature = "unproven")]
Expand Down
4 changes: 2 additions & 2 deletions hal/src/thumbv7em/clock/v2/apb.rs
Original file line number Diff line number Diff line change
Expand Up @@ -472,7 +472,7 @@ impl<A: ApbId> ApbToken<A> {
/// instances with the same [`ApbId`]. See the notes on `Token` types and
/// memory safety in the root of the `clock` module for more details.
#[inline]
unsafe fn new() -> Self {
pub(crate) unsafe fn new() -> Self {
ApbToken { id: PhantomData }
}
}
Expand All @@ -492,7 +492,7 @@ pub struct ApbClk<A: ApbId> {

impl<A: ApbId> ApbClk<A> {
#[inline]
fn new(token: ApbToken<A>) -> Self {
pub(crate) fn new(token: ApbToken<A>) -> Self {
ApbClk { token }
}

Expand Down
4 changes: 2 additions & 2 deletions hal/src/thumbv7em/clock/v2/pclk.rs
Original file line number Diff line number Diff line change
Expand Up @@ -105,7 +105,7 @@ impl<P: PclkId> PclkToken<P> {
/// instances with the same [`PclkId`]. See the notes on `Token` types and
/// memory safety in the root of the `clock` module for more details.
#[inline]
pub(super) unsafe fn new() -> Self {
pub(crate) unsafe fn new() -> Self {
PclkToken { pclk: PhantomData }
}

Expand Down Expand Up @@ -443,7 +443,7 @@ where
P: PclkId,
I: PclkSourceId,
{
pub(super) fn new(token: PclkToken<P>, freq: Hertz) -> Self {
pub(crate) fn new(token: PclkToken<P>, freq: Hertz) -> Self {
Self {
token,
src: PhantomData,
Expand Down
Loading