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

Enable optional RTIC support for Xiao_m0 #576

Draft
wants to merge 7 commits into
base: master
Choose a base branch
from
Draft
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
3 changes: 3 additions & 0 deletions boards/xiao_m0/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# Unreleased

- Updated to 2021 edition, updated dependencies, removed unused dependencies (#562)
- added `rtic` feature flag to enable `rtic` feature in the hal
- added `blinky_rtic` example to demonstrate a simple rtic app on the xiao_m0

# 0.12.0

- bump hal dependency to 0.14.0
Expand Down
8 changes: 7 additions & 1 deletion boards/xiao_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,14 +15,15 @@ version = "0.7"
optional = true

[dependencies.atsamd-hal]
version = "0.14"
version = "0.15.1"
default-features = false

[dependencies.usb-device]
version = "0.2"
optional = true

[dev-dependencies]
cortex-m-rtic = "1.0.0"
cortex-m = "0.7"
usbd-serial = "0.1"
panic-halt = "0.2"
Expand All @@ -36,11 +37,16 @@ default = ["rt", "atsamd-hal/samd21g", "atsamd-hal/unproven"]
rt = ["cortex-m-rt", "atsamd-hal/samd21g-rt"]
unproven = ["atsamd-hal/unproven"]
usb = ["atsamd-hal/usb", "usb-device"]
rtic = [ "atsamd-hal/rtic"]

[[example]]
name = "blink"
required-features = ["unproven"]

[[example]]
name = "blinky_rtic"
required-features = ["rtic","unproven"]

[[example]]
name = "eic"
required-features = ["unproven"]
Expand Down
74 changes: 74 additions & 0 deletions boards/xiao_m0/examples/blinky_rtic.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
//! Uses RTIC with the RTC as time source to blink an LED.
//!
//! The idle task is sleeping the CPU, so in practice this gives similar power
//! figure as the "sleeping_timer_rtc" example.
#![no_std]
#![no_main]

use xiao_m0 as bsp;

#[cfg(not(feature = "use_semihosting"))]
use panic_halt as _;
#[cfg(feature = "use_semihosting")]
use panic_semihosting as _;

use rtic;

#[rtic::app(device = bsp::pac, peripherals = true, dispatchers = [EVSYS])]
mod app {
use super::*;
use bsp::hal;
use hal::clock::{ClockGenId, ClockSource, GenericClockController};
use hal::pac::Peripherals;
use hal::prelude::*;
use hal::rtc::{Count32Mode, Duration, Rtc};

#[local]
struct Local {}

#[shared]
struct Shared {
// The LED could be a local resource, since it is only used in one task
// But we want to showcase shared resources and locking
led: bsp::Led0,
}

#[monotonic(binds = RTC, default = true)]
type RtcMonotonic = Rtc<Count32Mode>;

#[init]
fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) {
let mut peripherals: Peripherals = cx.device;
let pins = bsp::Pins::new(peripherals.PORT);
let mut core: rtic::export::Peripherals = cx.core;
let mut clocks = GenericClockController::with_external_32kosc(
peripherals.GCLK,
&mut peripherals.PM,
&mut peripherals.SYSCTRL,
&mut peripherals.NVMCTRL,
);
let _gclk = clocks.gclk0();
let rtc_clock_src = clocks
.configure_gclk_divider_and_source(ClockGenId::GCLK2, 1, ClockSource::XOSC32K, false)
.unwrap();
clocks.configure_standby(ClockGenId::GCLK2, true);
let rtc_clock = clocks.rtc(&rtc_clock_src).unwrap();
let rtc = Rtc::count32_mode(peripherals.RTC, rtc_clock.freq(), &mut peripherals.PM);
let led: bsp::Led0 = pins.led0.into();

// We can use the RTC in standby for maximum power savings
core.SCB.set_sleepdeep();

// Start the blink task
blink::spawn().unwrap();

(Shared { led }, Local {}, init::Monotonics(rtc))
}

#[task(shared = [led])]
fn blink(mut cx: blink::Context) {
// If the LED were a local resource, the lock would not be necessary
cx.shared.led.lock(|led| led.toggle().unwrap());
blink::spawn_after(Duration::secs(1)).ok();
}
}