#![deny(unsafe_code)]
#![no_main]
#![no_std]
extern crate cortex_m;
#[macro_use(entry, exception)]
extern crate cortex_m_rt as rt;
extern crate panic_semihosting;
extern crate stm32l4xx_hal as hal;
use crate::hal::prelude::*;
use crate::hal::tsc::Tsc;
use crate::rt::ExceptionFrame;
#[entry]
fn main() -> ! {
let p = hal::stm32::Peripherals::take().unwrap();
let mut flash = p.FLASH.constrain();
let mut rcc = p.RCC.constrain();
let mut gpiob = p.GPIOB.split(&mut rcc.ahb2);
let _clocks = rcc.cfgr.freeze(&mut flash.acr);
let mut led = gpiob.pb3.into_push_pull_output(&mut gpiob.moder, &mut gpiob.otyper);
let sample_pin = gpiob.pb4.into_touch_sample(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut c1 = gpiob.pb5.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let mut c2 = gpiob.pb6.into_touch_channel(&mut gpiob.moder, &mut gpiob.otyper, &mut gpiob.afrl);
let tsc = Tsc::tsc(p.TSC, sample_pin, &mut rcc.ahb1, None);
let baseline = tsc.acquire(&mut c1).unwrap();
let threshold = (baseline / 100) * 60;
loop {
let touched = tsc.acquire(&mut c1).unwrap();
let _touched_c2 = tsc.acquire(&mut c2).unwrap();
let _touched_c2_again = tsc.read(&mut c2).unwrap();
if touched < threshold {
led.set_high();
} else {
led.set_low();
}
}
}
#[exception]
fn HardFault(ef: &ExceptionFrame) -> ! {
panic!("{:#?}", ef);
}