From c108a5f72b43e70fdd8047dce79df5733dbb6278 Mon Sep 17 00:00:00 2001 From: Cristian Eigel Date: Mon, 21 Feb 2022 22:05:42 +0100 Subject: [PATCH] Add new example of ADC with RTIC using SysTick as monotonic --- boards/feather_m0/Cargo.toml | 6 ++ boards/feather_m0/examples/adc_rtic.rs | 77 ++++++++++++++++++++++++++ boards/feather_m0/src/lib.rs | 3 +- 3 files changed, 85 insertions(+), 1 deletion(-) create mode 100644 boards/feather_m0/examples/adc_rtic.rs diff --git a/boards/feather_m0/Cargo.toml b/boards/feather_m0/Cargo.toml index fd7a50472ca..36e84af5a56 100644 --- a/boards/feather_m0/Cargo.toml +++ b/boards/feather_m0/Cargo.toml @@ -44,6 +44,8 @@ nom = { version = "5", default-features = false } heapless = "0.7" panic-halt = "0.2" panic-semihosting = "0.5" +systick-monotonic = "1.0.0" +rtt-target = {version= "0.3.1", features = ["cortex-m"] } [features] # ask the HAL to enable atsamd21g support @@ -133,6 +135,10 @@ required-features = ["adalogger", "usb", "sdmmc", "unproven"] name = "blinky_rtic" required-features = ["rtic", "unproven"] +[[example]] +name = "adc_rtic" +required-features = ["rtic", "unproven"] + [[example]] name = "uart" required-features = ["dma"] diff --git a/boards/feather_m0/examples/adc_rtic.rs b/boards/feather_m0/examples/adc_rtic.rs new file mode 100644 index 00000000000..25264a8cbad --- /dev/null +++ b/boards/feather_m0/examples/adc_rtic.rs @@ -0,0 +1,77 @@ +//! Uses RTIC with the RTC as time source to read the battery level. +//! This example uses the SysTick as the Monotonic tier. +//! +//! 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 feather_m0 as bsp; + +#[cfg(not(feature = "use_semihosting"))] +use panic_halt as _; + +#[cfg(feature = "use_semihosting")] +use panic_semihosting as _; + +#[rtic::app(device = bsp::pac, peripherals = true, dispatchers = [SERCOM5])] +mod app { + use super::*; + use bsp::{hal, pin_alias}; + use hal::adc::{Adc, Resolution}; + use hal::clock::GenericClockController; + use hal::pac::{Peripherals, ADC}; + use hal::prelude::*; + use hal::time::Hertz; + use rtt_target::{rprintln, rtt_init_print}; + use systick_monotonic::*; + + #[local] + struct Local { + adc: Adc, + adc_pin: bsp::Battery, + } + + #[shared] + struct Shared {} + + #[monotonic(binds = SysTick, default = true)] + type SysTickMonotonic = Systick<100>; + + #[init] + fn init(cx: init::Context) -> (Shared, Local, init::Monotonics) { + rtt_init_print!(); + 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 freq: Hertz = gclk.into(); + let systick = Systick::new(core.SYST, freq.0); + + let mut adc = Adc::adc(peripherals.ADC, &mut peripherals.PM, &mut clocks); + adc.resolution(Resolution::_12BIT); + let adc_pin: bsp::Battery = pin_alias!(pins.battery).into(); + // We can use the RTC in standby for maximum power savings + core.SCB.set_sleepdeep(); + + read_battery::spawn().unwrap(); + + (Shared {}, Local { adc, adc_pin }, init::Monotonics(systick)) + } + + #[task(local = [adc, adc_pin])] + fn read_battery(cx: read_battery::Context) { + let data: u16 = cx.local.adc.read(cx.local.adc_pin).unwrap(); + const RESOLUTION_BITS: u32 = 12; + let max_range = 1 << RESOLUTION_BITS; + let voltage = ((data as f32) * 3.3 * 2.0) / (max_range as f32); + rprintln!("Battery level {} - {}", voltage, data); + read_battery::spawn_after(fugit::Duration::::secs(5)).ok(); + } +} diff --git a/boards/feather_m0/src/lib.rs b/boards/feather_m0/src/lib.rs index 76b8f623556..5f689334bbe 100644 --- a/boards/feather_m0/src/lib.rs +++ b/boards/feather_m0/src/lib.rs @@ -94,7 +94,8 @@ pub mod pins { /// Pin 9, PWM capable. Also analog input (A7) name: d9 aliases: { - PushPullOutput: Ssd1306Rst + PushPullOutput: Ssd1306Rst, + AlternateB: Battery } } PA18 {