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 COMP module #189

Merged
merged 10 commits into from
Aug 2, 2020
Merged
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
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ members = [
"examples/ppi-demo",
"examples/gpiote-demo",
"examples/wdt-demo",
"examples/comp-demo",
]

[profile.dev]
Expand Down
17 changes: 17 additions & 0 deletions examples/comp-demo/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
[package]
name = "comp-demo"
version = "0.1.0"
authors = ["Henrik Alsér"]
edition = "2018"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
cortex-m = "0.6.2"
cortex-m-rtic = "0.5.3"
rtt-target = {version = "0.2.0", features = ["cortex-m"] }
nrf52840-hal = { features = ["rt"], path = "../../nrf52840-hal" }

[dependencies.embedded-hal]
version = "0.2.3"
features = ["unproven"]
46 changes: 46 additions & 0 deletions examples/comp-demo/Embed.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
[default.probe]
# The index of the probe in the connected probe list.
# probe_index = 0
# The protocol to be used for communicating with the target.
protocol = "Swd"
# The speed in kHz of the data link to the target.
# speed = 1337

[default.flashing]
# Whether or not the target should be flashed.
enabled = true
# Whether or not the target should be halted after flashing.
halt_afterwards = false
# Whether or not bytes erased but not rewritten with data from the ELF
# should be restored with their contents before erasing.
restore_unwritten_bytes = false
# The path where an SVG of the assembled flash layout should be written to.
# flash_layout_output_path = "out.svg"

[default.general]
# The chip name of the chip to be debugged.
chip = "nRF52840"
# A list of chip descriptions to be loaded during runtime.
chip_descriptions = []
# The default log level to be used.
log_level = "Warn"

[default.rtt]
# Whether or not an RTTUI should be opened after flashing.
# This is exclusive and cannot be used with GDB at the moment.
enabled = true
# A list of channel associations to be displayed. If left empty, all channels are displayed.
channels = [
# { up = 0, down = 0, name = "name" }
]
# The duration in ms for which the logger should retry to attach to RTT.
timeout = 3000
# Whether timestamps in the RTTUI are enabled
show_timestamps = true

[default.gdb]
# Whether or not a GDB server should be opened after flashing.
# This is exclusive and cannot be used with RTT at the moment.
enabled = false
# The connection string in host:port format wher the GDB server will open a socket.
# gdb_connection_string
68 changes: 68 additions & 0 deletions examples/comp-demo/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#![no_std]
#![no_main]

use embedded_hal::digital::v2::OutputPin;
use {
core::{
panic::PanicInfo,
sync::atomic::{compiler_fence, Ordering},
},
hal::{
comp::*,
gpio::{Level, Output, Pin, PushPull},
},
nrf52840_hal as hal,
rtt_target::{rprintln, rtt_init_print},
};

#[rtic::app(device = crate::hal::pac, peripherals = true)]
const APP: () = {
struct Resources {
comp: Comp,
led1: Pin<Output<PushPull>>,
}

#[init]
fn init(ctx: init::Context) -> init::LateResources {
let _clocks = hal::clocks::Clocks::new(ctx.device.CLOCK).enable_ext_hfosc();
rtt_init_print!();

let p0 = hal::gpio::p0::Parts::new(ctx.device.P0);
let led1 = p0.p0_13.into_push_pull_output(Level::High).degrade();
let in_pin = p0.p0_30.into_floating_input();
let ref_pin = p0.p0_31.into_floating_input();

let comp = Comp::new(ctx.device.COMP, &in_pin);
comp.differential(&ref_pin)
.hysteresis(true)
.enable_interrupt(Transition::Cross)
.enable();

init::LateResources { comp, led1 }
}

#[task(binds = COMP_LPCOMP, resources = [comp, led1])]
fn on_comp(ctx: on_comp::Context) {
ctx.resources.comp.reset_event(Transition::Cross);
match ctx.resources.comp.read() {
CompResult::Above => {
rprintln!("Vin > Vref");
ctx.resources.led1.set_low().ok();
}
CompResult::Below => {
rprintln!("Vin < Vref");
ctx.resources.led1.set_high().ok();
}
}
}
};

#[inline(never)]
#[panic_handler]
fn panic(info: &PanicInfo) -> ! {
cortex_m::interrupt::disable();
rprintln!("{}", info);
loop {
compiler_fence(Ordering::SeqCst);
}
}
Loading