Skip to content

Commit c76defa

Browse files
committed
Merge branch 'ref/monotonic' into 'master'
atsamv71_xult: use Monotonic timers instead of asm::delay Closes atsams-rs#21 See merge request embedded-rust/atsamx7x-hal!12
2 parents 466f36f + b6a26b7 commit c76defa

File tree

4 files changed

+82
-28
lines changed

4 files changed

+82
-28
lines changed

boards/atsamv71_xult/Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ panic-rtt-target = { version = "0.1.2", features = ["cortex-m"] }
1414
rtt-target = { version = "0.3.1", features = ["cortex-m"] }
1515
heapless = "0.7"
1616
usbd-serial = "0.1.1"
17+
dwt-systick-monotonic = "1"
1718

1819
[dependencies.atsamx7x-hal]
1920
version = "0.1.0"

boards/atsamv71_xult/examples/blinky.rs

Lines changed: 38 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,20 @@
44

55
use panic_rtt_target as _;
66

7-
#[rtic::app(device = hal::target_device, peripherals = true)]
7+
#[rtic::app(device = hal::target_device, peripherals = true, dispatchers = [UART0])]
88
mod app {
99
use atsamx7x_hal as hal;
10+
use dwt_systick_monotonic::{DwtSystick, ExtU32};
11+
use hal::efc::*;
1012
use hal::ehal::digital::v2::ToggleableOutputPin;
13+
use hal::fugit::RateExtU32;
1114
use hal::pio::*;
15+
use hal::pmc::*;
1216
use rtt_target::{rprintln, rtt_init_print};
1317

18+
#[monotonic(binds = SysTick, default = true)]
19+
type Mono = DwtSystick<12_000_000>;
20+
1421
#[shared]
1522
struct Shared {}
1623

@@ -20,26 +27,43 @@ mod app {
2027
}
2128

2229
#[init]
23-
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
30+
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
2431
rtt_init_print!();
25-
rprintln!("init");
2632

27-
// Disable the watchdog.
28-
let wd = hal::watchdog::Watchdog::new(ctx.device.WDT).disable();
33+
let mut pmc = Pmc::new(ctx.device.PMC, &ctx.device.WDT.into());
34+
let mainck = pmc
35+
.get_mainck(MainCkSource::ExternalNormal(12.MHz()))
36+
.unwrap();
37+
let (hclk, _mck) = pmc
38+
.get_hclk(
39+
HostClockConfig {
40+
pres: MckPrescaler::CLK_1,
41+
div: MckDivider::EQ_PCK,
42+
},
43+
&mainck,
44+
&mut Efc::new(ctx.device.EFC, VddioLevel::V3),
45+
)
46+
.unwrap();
47+
48+
let mono = DwtSystick::new(
49+
&mut ctx.core.DCB,
50+
ctx.core.DWT,
51+
ctx.core.SYST,
52+
hclk.systick_freq().to_Hz(),
53+
);
2954

30-
let mut pmc = hal::pmc::Pmc::new(ctx.device.PMC, &wd);
3155
let banka = hal::pio::BankA::new(ctx.device.PIOA, &mut pmc, BankConfiguration::default());
3256
let led = banka.pa23.into_output();
3357

34-
(Shared {}, Local { led }, init::Monotonics())
58+
toggle_led::spawn().unwrap();
59+
60+
(Shared {}, Local { led }, init::Monotonics(mono))
3561
}
3662

37-
#[idle(local = [led])]
38-
fn idle(ctx: idle::Context) -> ! {
39-
loop {
40-
ctx.local.led.toggle().unwrap();
41-
rprintln!("LED0 toggled");
42-
cortex_m::asm::delay(12_000_000);
43-
}
63+
#[task(local = [led])]
64+
fn toggle_led(ctx: toggle_led::Context) {
65+
ctx.local.led.toggle().unwrap();
66+
rprintln!("LED0 toggled");
67+
toggle_led::spawn_after(1.secs()).unwrap();
4468
}
4569
}

boards/atsamv71_xult/examples/periodic_adc_sample.rs

Lines changed: 25 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -4,16 +4,20 @@
44

55
use panic_rtt_target as _;
66

7-
#[rtic::app(device = hal::target_device, peripherals = true)]
7+
#[rtic::app(device = hal::target_device, peripherals = true, dispatchers = [UART0])]
88
mod app {
99
use atsamx7x_hal as hal;
10+
use dwt_systick_monotonic::{DwtSystick, ExtU32};
1011
use hal::afec::*;
1112
use hal::efc::*;
1213
use hal::ehal::adc::OneShot;
1314
use hal::pio::*;
1415
use hal::pmc::*;
1516
use rtt_target::{rprintln, rtt_init_print};
1617

18+
#[monotonic(binds = SysTick, default = true)]
19+
type Mono = DwtSystick<12_000_000>;
20+
1721
#[shared]
1822
struct Shared {}
1923

@@ -24,7 +28,7 @@ mod app {
2428
}
2529

2630
#[init]
27-
fn init(ctx: init::Context) -> (Shared, Local, init::Monotonics) {
31+
fn init(mut ctx: init::Context) -> (Shared, Local, init::Monotonics) {
2832
rtt_init_print!();
2933
rprintln!("init");
3034

@@ -34,7 +38,7 @@ mod app {
3438
let mainck = pmc
3539
.get_mainck(MainCkSource::InternalRC(MainRcFreq::_12_MHZ))
3640
.unwrap();
37-
let (_, mck) = pmc
41+
let (hclk, mck) = pmc
3842
.get_hclk(
3943
HostClockConfig {
4044
pres: MckPrescaler::CLK_1,
@@ -45,20 +49,29 @@ mod app {
4549
)
4650
.unwrap();
4751

52+
let mono = DwtSystick::new(
53+
&mut ctx.core.DCB,
54+
ctx.core.DWT,
55+
ctx.core.SYST,
56+
hclk.systick_freq().to_Hz(),
57+
);
58+
4859
let banka = hal::pio::BankA::new(ctx.device.PIOA, &mut pmc, BankConfiguration::default());
4960
let afec = Afec::new_afec0(ctx.device.AFEC0, &mut pmc, &mck).unwrap();
5061
let pin = banka.pa17.into_input(PullDir::PullUp);
5162

52-
(Shared {}, Local { afec, pin }, init::Monotonics())
63+
adc_sample::spawn().unwrap();
64+
65+
(Shared {}, Local { afec, pin }, init::Monotonics(mono))
5366
}
5467

55-
#[idle(local = [afec, pin])]
56-
fn idle(ctx: idle::Context) -> ! {
57-
let idle::LocalResources { afec, pin } = ctx.local;
58-
loop {
59-
let v: f32 = afec.read(pin).unwrap();
60-
rprintln!("PA17 (channel 6) = {:.2}V", v);
61-
cortex_m::asm::delay(12_000_000);
62-
}
68+
#[task(local = [afec, pin])]
69+
fn adc_sample(ctx: adc_sample::Context) {
70+
let adc_sample::LocalResources { afec, pin } = ctx.local;
71+
72+
let v: f32 = afec.read(pin).unwrap();
73+
rprintln!("PA17 (channel 6) = {:.2}V", v);
74+
75+
adc_sample::spawn_after(1.secs()).unwrap();
6376
}
6477
}

hal/src/pmc.rs

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -206,7 +206,21 @@ impl HostClock {
206206
}
207207

208208
/// HCLK token
209-
pub struct ProcessorClock {}
209+
pub struct ProcessorClock {
210+
freq: Hertz,
211+
}
212+
213+
impl ProcessorClock {
214+
pub fn freq(&self) -> Hertz {
215+
self.freq
216+
}
217+
218+
pub fn systick_freq(&self) -> Hertz {
219+
// §31.3 would suggest that SysTick is HCLK/2, but experiments
220+
// show that it is equal to HCLK.
221+
self.freq
222+
}
223+
}
210224

211225
pub trait PckId {
212226
const ID: u8;
@@ -626,7 +640,9 @@ impl Pmc {
626640
}
627641

628642
Ok((
629-
ProcessorClock {},
643+
ProcessorClock {
644+
freq: freq / pres.value(),
645+
},
630646
HostClock {
631647
freq: freq / pres.value() / div.value(),
632648
},

0 commit comments

Comments
 (0)