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

Add new example of ADC with RTIC using SysTick as monotonic #575

Open
wants to merge 1 commit 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
6 changes: 6 additions & 0 deletions boards/feather_m0/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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"]
Expand Down
77 changes: 77 additions & 0 deletions boards/feather_m0/examples/adc_rtic.rs
Original file line number Diff line number Diff line change
@@ -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>,
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::<u64, 1, 100>::secs(5)).ok();
}
}
3 changes: 2 additions & 1 deletion boards/feather_m0/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down