Skip to content

Commit

Permalink
Add new example of ADC with RTIC using SysTick as monotonic
Browse files Browse the repository at this point in the history
  • Loading branch information
ceigel committed Sep 21, 2022
1 parent 7b94c2c commit 7a608e4
Show file tree
Hide file tree
Showing 4 changed files with 86 additions and 1 deletion.
1 change: 1 addition & 0 deletions boards/feather_m0/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

- Update to `atsamd-hal` version `0.15.1`
- Update .cargo/config
- Add battery pin as alias of PA07

# v0.12.0

Expand Down
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

0 comments on commit 7a608e4

Please sign in to comment.